VisualizationDragDropContainer.cpp
392 lines
| 13.1 KiB
| text/x-c
|
CppLexer
r838 | #include "Visualization/VisualizationDragDropContainer.h" | |||
#include "DragDropHelper.h" | ||||
r844 | #include "SqpApplication.h" | |||
#include "Visualization/VisualizationDragWidget.h" | ||||
r838 | ||||
r851 | #include "Common/VisualizationDef.h" | |||
r838 | #include <QDrag> | |||
#include <QDragEnterEvent> | ||||
r844 | #include <QVBoxLayout> | |||
r838 | ||||
r845 | #include <cmath> | |||
r838 | #include <memory> | |||
r850 | Q_LOGGING_CATEGORY(LOG_VisualizationDragDropContainer, "VisualizationDragDropContainer") | |||
r838 | struct VisualizationDragDropContainer::VisualizationDragDropContainerPrivate { | |||
r844 | QVBoxLayout *m_Layout; | |||
QStringList m_AcceptedMimeTypes; | ||||
QStringList m_MergeAllowedMimeTypes; | ||||
r850 | VisualizationDragDropContainer::AcceptMimeDataFunction m_AcceptMimeDataFun | |||
= [](auto mimeData) { return true; }; | ||||
r873 | int m_MinContainerHeight = 0; | |||
r838 | ||||
r844 | explicit VisualizationDragDropContainerPrivate(QWidget *widget) | |||
r838 | { | |||
r844 | m_Layout = new QVBoxLayout(widget); | |||
m_Layout->setContentsMargins(0, 0, 0, 0); | ||||
r838 | } | |||
r844 | bool acceptMimeData(const QMimeData *data) const | |||
r838 | { | |||
r844 | for (const auto &type : m_AcceptedMimeTypes) { | |||
r850 | if (data->hasFormat(type) && m_AcceptMimeDataFun(data)) { | |||
r838 | return true; | |||
r844 | } | |||
r838 | } | |||
return false; | ||||
} | ||||
r844 | bool allowMergeMimeData(const QMimeData *data) const | |||
r838 | { | |||
r844 | for (const auto &type : m_MergeAllowedMimeTypes) { | |||
if (data->hasFormat(type)) { | ||||
r838 | return true; | |||
r844 | } | |||
r838 | } | |||
return false; | ||||
} | ||||
bool hasPlaceHolder() const | ||||
{ | ||||
r844 | return sqpApp->dragDropHelper().placeHolder().parentWidget() == m_Layout->parentWidget(); | |||
r838 | } | |||
r873 | VisualizationDragWidget *getChildDragWidgetAt(const QWidget *parent, const QPoint &pos) const | |||
r838 | { | |||
r844 | VisualizationDragWidget *dragWidget = nullptr; | |||
for (auto child : parent->children()) { | ||||
auto widget = qobject_cast<VisualizationDragWidget *>(child); | ||||
if (widget && widget->isVisible()) { | ||||
if (widget->frameGeometry().contains(pos)) { | ||||
r838 | dragWidget = widget; | |||
break; | ||||
} | ||||
} | ||||
} | ||||
return dragWidget; | ||||
} | ||||
r844 | bool cursorIsInContainer(QWidget *container) const | |||
r838 | { | |||
r874 | return container->isAncestorOf(sqpApp->widgetAt(QCursor::pos())); | |||
r838 | } | |||
r873 | ||||
int countDragWidget(const QWidget *parent) const | ||||
{ | ||||
auto nbGraph = 0; | ||||
for (auto child : parent->children()) { | ||||
if (qobject_cast<VisualizationDragWidget *>(child)) { | ||||
nbGraph += 1; | ||||
} | ||||
} | ||||
return nbGraph; | ||||
} | ||||
void findPlaceHolderPosition(const QPoint &pos, bool canMerge, | ||||
const VisualizationDragDropContainer *container); | ||||
r838 | }; | |||
VisualizationDragDropContainer::VisualizationDragDropContainer(QWidget *parent) | ||||
r844 | : QWidget{parent}, | |||
impl{spimpl::make_unique_impl<VisualizationDragDropContainerPrivate>(this)} | ||||
r838 | { | |||
setAcceptDrops(true); | ||||
} | ||||
void VisualizationDragDropContainer::addDragWidget(VisualizationDragWidget *dragWidget) | ||||
{ | ||||
r844 | impl->m_Layout->addWidget(dragWidget); | |||
r838 | disconnect(dragWidget, &VisualizationDragWidget::dragDetected, nullptr, nullptr); | |||
r844 | connect(dragWidget, &VisualizationDragWidget::dragDetected, this, | |||
&VisualizationDragDropContainer::startDrag); | ||||
r838 | } | |||
r844 | void VisualizationDragDropContainer::insertDragWidget(int index, | |||
VisualizationDragWidget *dragWidget) | ||||
r838 | { | |||
r844 | impl->m_Layout->insertWidget(index, dragWidget); | |||
r838 | disconnect(dragWidget, &VisualizationDragWidget::dragDetected, nullptr, nullptr); | |||
r844 | connect(dragWidget, &VisualizationDragWidget::dragDetected, this, | |||
&VisualizationDragDropContainer::startDrag); | ||||
r838 | } | |||
void VisualizationDragDropContainer::setAcceptedMimeTypes(const QStringList &mimeTypes) | ||||
{ | ||||
r844 | impl->m_AcceptedMimeTypes = mimeTypes; | |||
r838 | } | |||
void VisualizationDragDropContainer::setMergeAllowedMimeTypes(const QStringList &mimeTypes) | ||||
{ | ||||
r844 | impl->m_MergeAllowedMimeTypes = mimeTypes; | |||
r838 | } | |||
int VisualizationDragDropContainer::countDragWidget() const | ||||
{ | ||||
r873 | return impl->countDragWidget(this); | |||
r838 | } | |||
r850 | void VisualizationDragDropContainer::setAcceptMimeDataFunction( | |||
VisualizationDragDropContainer::AcceptMimeDataFunction fun) | ||||
{ | ||||
impl->m_AcceptMimeDataFun = fun; | ||||
} | ||||
r844 | void VisualizationDragDropContainer::startDrag(VisualizationDragWidget *dragWidget, | |||
const QPoint &dragPosition) | ||||
r838 | { | |||
r844 | auto &helper = sqpApp->dragDropHelper(); | |||
r850 | helper.resetDragAndDrop(); | |||
r838 | ||||
r844 | // Note: The management of the drag object is done by Qt | |||
r846 | auto drag = new QDrag{dragWidget}; | |||
r838 | drag->setHotSpot(dragPosition); | |||
auto mimeData = dragWidget->mimeData(); | ||||
drag->setMimeData(mimeData); | ||||
auto pixmap = QPixmap(dragWidget->size()); | ||||
dragWidget->render(&pixmap); | ||||
drag->setPixmap(pixmap); | ||||
auto image = pixmap.toImage(); | ||||
mimeData->setImageData(image); | ||||
mimeData->setUrls({helper.imageTemporaryUrl(image)}); | ||||
r844 | if (impl->m_Layout->indexOf(dragWidget) >= 0) { | |||
r838 | helper.setCurrentDragWidget(dragWidget); | |||
r844 | if (impl->cursorIsInContainer(this)) { | |||
auto dragWidgetIndex = impl->m_Layout->indexOf(dragWidget); | ||||
helper.insertPlaceHolder(impl->m_Layout, dragWidgetIndex); | ||||
r838 | dragWidget->setVisible(false); | |||
} | ||||
r874 | else { | |||
// The drag starts directly outside the drop zone | ||||
// do not add the placeHolder | ||||
} | ||||
r838 | ||||
r850 | // Note: The exec() is blocking on windows but not on linux and macOS | |||
drag->exec(Qt::MoveAction | Qt::CopyAction); | ||||
} | ||||
else { | ||||
qCWarning(LOG_VisualizationDragDropContainer()) | ||||
<< tr("VisualizationDragDropContainer::startDrag, drag aborted, the specified " | ||||
"VisualizationDragWidget is not found in this container."); | ||||
} | ||||
r838 | } | |||
void VisualizationDragDropContainer::dragEnterEvent(QDragEnterEvent *event) | ||||
{ | ||||
r844 | if (impl->acceptMimeData(event->mimeData())) { | |||
r838 | event->acceptProposedAction(); | |||
r844 | auto &helper = sqpApp->dragDropHelper(); | |||
r838 | ||||
r844 | if (!impl->hasPlaceHolder()) { | |||
r838 | auto dragWidget = helper.getCurrentDragWidget(); | |||
r850 | ||||
if (dragWidget) { | ||||
// If the drag&drop is internal to the visualization, entering the container hide | ||||
r873 | // the dragWidget which was made visible by the dragLeaveEvent | |||
r850 | auto parentWidget | |||
= qobject_cast<VisualizationDragDropContainer *>(dragWidget->parentWidget()); | ||||
if (parentWidget) { | ||||
dragWidget->setVisible(false); | ||||
} | ||||
r838 | } | |||
r873 | auto canMerge = impl->allowMergeMimeData(event->mimeData()); | |||
impl->findPlaceHolderPosition(event->pos(), canMerge, this); | ||||
r838 | } | |||
r850 | else { | |||
// do nothing | ||||
} | ||||
r838 | } | |||
r846 | else { | |||
r838 | event->ignore(); | |||
r846 | } | |||
r838 | ||||
QWidget::dragEnterEvent(event); | ||||
} | ||||
void VisualizationDragDropContainer::dragLeaveEvent(QDragLeaveEvent *event) | ||||
{ | ||||
Q_UNUSED(event); | ||||
r844 | auto &helper = sqpApp->dragDropHelper(); | |||
r838 | ||||
r844 | if (!impl->cursorIsInContainer(this)) { | |||
r838 | helper.removePlaceHolder(); | |||
r873 | helper.setHightlightedDragWidget(nullptr); | |||
impl->m_MinContainerHeight = 0; | ||||
r838 | ||||
r850 | auto dragWidget = helper.getCurrentDragWidget(); | |||
if (dragWidget) { | ||||
// dragWidget has a value only if the drag is started from the visualization | ||||
// In that case, shows the drag widget at its original place | ||||
r844 | // So the drag widget doesn't stay hidden if the drop occurs outside the visualization | |||
// drop zone (It is not possible to catch a drop event outside of the application) | ||||
r838 | ||||
r844 | if (dragWidget) { | |||
r838 | dragWidget->setVisible(true); | |||
} | ||||
} | ||||
} | ||||
r850 | else { | |||
// Leave event probably received for a child widget. | ||||
// Do nothing. | ||||
// Note: The DragLeave event, doesn't have any mean to determine who sent it. | ||||
} | ||||
r838 | ||||
QWidget::dragLeaveEvent(event); | ||||
} | ||||
void VisualizationDragDropContainer::dragMoveEvent(QDragMoveEvent *event) | ||||
{ | ||||
r844 | if (impl->acceptMimeData(event->mimeData())) { | |||
r873 | auto canMerge = impl->allowMergeMimeData(event->mimeData()); | |||
impl->findPlaceHolderPosition(event->pos(), canMerge, this); | ||||
r838 | } | |||
r846 | else { | |||
r838 | event->ignore(); | |||
r846 | } | |||
r838 | ||||
QWidget::dragMoveEvent(event); | ||||
} | ||||
void VisualizationDragDropContainer::dropEvent(QDropEvent *event) | ||||
{ | ||||
r875 | auto &helper = sqpApp->dragDropHelper(); | |||
r844 | if (impl->acceptMimeData(event->mimeData())) { | |||
r875 | auto dragWidget = helper.getCurrentDragWidget(); | |||
r850 | if (impl->hasPlaceHolder()) { | |||
r875 | // drop where the placeHolder is located | |||
r838 | ||||
r844 | auto droppedIndex = impl->m_Layout->indexOf(&helper.placeHolder()); | |||
r838 | ||||
r850 | if (dragWidget) { | |||
auto dragWidgetIndex = impl->m_Layout->indexOf(dragWidget); | ||||
if (dragWidgetIndex >= 0 && dragWidgetIndex < droppedIndex) { | ||||
// Correction of the index if the drop occurs in the same container | ||||
// and if the drag is started from the visualization (in that case, the | ||||
// dragWidget is hidden) | ||||
droppedIndex -= 1; | ||||
} | ||||
r838 | ||||
r850 | dragWidget->setVisible(true); | |||
} | ||||
r838 | ||||
event->acceptProposedAction(); | ||||
helper.removePlaceHolder(); | ||||
r875 | emit dropOccuredInContainer(droppedIndex, event->mimeData()); | |||
r838 | } | |||
r875 | else if (helper.getHightlightedDragWidget()) { | |||
// drop on the highlighted widget | ||||
auto canMerge = impl->allowMergeMimeData(event->mimeData()); | ||||
if (canMerge) { | ||||
event->acceptProposedAction(); | ||||
emit dropOccuredOnWidget(helper.getHightlightedDragWidget(), event->mimeData()); | ||||
} | ||||
else { | ||||
qCWarning(LOG_VisualizationDragDropContainer()) | ||||
<< tr("VisualizationDragDropContainer::dropEvent, dropping on a widget, but " | ||||
"the merge is forbidden."); | ||||
Q_ASSERT(false); | ||||
} | ||||
r850 | } | |||
r838 | } | |||
r846 | else { | |||
r838 | event->ignore(); | |||
r846 | } | |||
r838 | ||||
r873 | sqpApp->dragDropHelper().setHightlightedDragWidget(nullptr); | |||
impl->m_MinContainerHeight = 0; | ||||
r838 | QWidget::dropEvent(event); | |||
} | ||||
r873 | ||||
void VisualizationDragDropContainer::VisualizationDragDropContainerPrivate::findPlaceHolderPosition( | ||||
const QPoint &pos, bool canMerge, const VisualizationDragDropContainer *container) | ||||
{ | ||||
auto &helper = sqpApp->dragDropHelper(); | ||||
auto dragWidgetHovered = getChildDragWidgetAt(container, pos); | ||||
if (dragWidgetHovered) { | ||||
auto nbDragWidget = countDragWidget(container); | ||||
if (nbDragWidget > 0) { | ||||
if (m_MinContainerHeight == 0) { | ||||
m_MinContainerHeight = container->size().height(); | ||||
} | ||||
m_MinContainerHeight = qMin(m_MinContainerHeight, container->size().height()); | ||||
auto graphHeight = qMax(m_MinContainerHeight / nbDragWidget, GRAPH_MINIMUM_HEIGHT); | ||||
auto posY = pos.y(); | ||||
auto dropIndex = floor(posY / graphHeight); | ||||
auto zoneSize = qMin(graphHeight / 4.0, 75.0); | ||||
auto isOnTop = posY < dropIndex * graphHeight + zoneSize; | ||||
auto isOnBottom = posY > (dropIndex + 1) * graphHeight - zoneSize; | ||||
auto placeHolderIndex = m_Layout->indexOf(&(helper.placeHolder())); | ||||
if (isOnTop || isOnBottom || !canMerge) { | ||||
if (isOnBottom) { | ||||
dropIndex += 1; | ||||
} | ||||
if (helper.getCurrentDragWidget()) { | ||||
auto dragWidgetIndex = m_Layout->indexOf(helper.getCurrentDragWidget()); | ||||
if (dragWidgetIndex >= 0 && dragWidgetIndex <= dropIndex) { | ||||
// Correction of the index if the drop occurs in the same container | ||||
// and if the drag is started from the visualization (in that case, the | ||||
// dragWidget is hidden) | ||||
dropIndex += 1; | ||||
} | ||||
} | ||||
if (dropIndex != placeHolderIndex) { | ||||
helper.insertPlaceHolder(m_Layout, dropIndex); | ||||
} | ||||
helper.setHightlightedDragWidget(nullptr); | ||||
} | ||||
else if (canMerge) { | ||||
// drop on the middle -> merge | ||||
if (hasPlaceHolder()) { | ||||
helper.removePlaceHolder(); | ||||
} | ||||
helper.setHightlightedDragWidget(dragWidgetHovered); | ||||
} | ||||
} | ||||
else { | ||||
qCWarning(LOG_VisualizationDragDropContainer()) | ||||
<< tr("VisualizationDragDropContainer::dragMoveEvent, no widget found in the " | ||||
"container"); | ||||
} | ||||
} | ||||
else if (!hasPlaceHolder()) { | ||||
// Drop on an empty container, just add the placeHolder at the top | ||||
helper.insertPlaceHolder(m_Layout, 0); | ||||
} | ||||
else { | ||||
// No hovered drag widget, the mouse is probably hover the placeHolder | ||||
// Do nothing | ||||
} | ||||
} | ||||