VisualizationDragDropContainer.cpp
496 lines
| 17.9 KiB
| text/x-c
|
CppLexer
r838 | #include "Visualization/VisualizationDragDropContainer.h" | |||
r1075 | #include "DragAndDrop/DragDropGuiController.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") | |||
r934 | auto DRAGGED_MINIATURE_WIDTH = 200; // in pixels | |||
r838 | struct VisualizationDragDropContainer::VisualizationDragDropContainerPrivate { | |||
r844 | QVBoxLayout *m_Layout; | |||
r879 | QHash<QString, VisualizationDragDropContainer::DropBehavior> m_AcceptedMimeTypes; | |||
r881 | QString m_PlaceHolderText; | |||
r1075 | DragDropGuiController::PlaceHolderType m_PlaceHolderType; | |||
r879 | ||||
r850 | VisualizationDragDropContainer::AcceptMimeDataFunction m_AcceptMimeDataFun | |||
= [](auto mimeData) { return true; }; | ||||
Alexandre Leroux
|
r1023 | VisualizationDragDropContainer::AcceptDragWidgetFunction m_AcceptDragWidgetFun | ||
= [](auto dragWidget, auto mimeData) { return true; }; | ||||
r879 | ||||
r873 | int m_MinContainerHeight = 0; | |||
r838 | ||||
r844 | explicit VisualizationDragDropContainerPrivate(QWidget *widget) | |||
r1075 | : m_PlaceHolderType(DragDropGuiController::PlaceHolderType::Graph) | |||
r838 | { | |||
r844 | m_Layout = new QVBoxLayout(widget); | |||
m_Layout->setContentsMargins(0, 0, 0, 0); | ||||
r838 | } | |||
r844 | bool acceptMimeData(const QMimeData *data) const | |||
r838 | { | |||
r936 | auto accepted = false; | |||
for (auto it = m_AcceptedMimeTypes.constBegin(); it != m_AcceptedMimeTypes.constEnd(); | ||||
++it) { | ||||
const auto &type = it.key(); | ||||
const auto &behavior = it.value(); | ||||
if (data->hasFormat(type)) { | ||||
if (behavior != DropBehavior::Forbidden) { | ||||
accepted = true; | ||||
} | ||||
else { | ||||
accepted = false; | ||||
break; | ||||
} | ||||
r844 | } | |||
r838 | } | |||
r936 | if (accepted) { | |||
accepted = m_AcceptMimeDataFun(data); | ||||
} | ||||
return accepted; | ||||
r838 | } | |||
r879 | bool allowMergeForMimeData(const QMimeData *data) const | |||
r838 | { | |||
r939 | auto result = false; | |||
r879 | for (auto it = m_AcceptedMimeTypes.constBegin(); it != m_AcceptedMimeTypes.constEnd(); | |||
++it) { | ||||
if (data->hasFormat(it.key()) | ||||
&& (it.value() == VisualizationDragDropContainer::DropBehavior::Merged | ||||
|| it.value() | ||||
== VisualizationDragDropContainer::DropBehavior::InsertedAndMerged)) { | ||||
result = true; | ||||
} | ||||
else if (data->hasFormat(it.key()) | ||||
&& it.value() == VisualizationDragDropContainer::DropBehavior::Inserted) { | ||||
// Merge is forbidden if the mime data contain an acceptable type which cannot be | ||||
// merged | ||||
result = false; | ||||
break; | ||||
} | ||||
} | ||||
return result; | ||||
} | ||||
bool allowInsertForMimeData(const QMimeData *data) const | ||||
{ | ||||
for (auto it = m_AcceptedMimeTypes.constBegin(); it != m_AcceptedMimeTypes.constEnd(); | ||||
++it) { | ||||
if (data->hasFormat(it.key()) | ||||
&& (it.value() == VisualizationDragDropContainer::DropBehavior::Inserted | ||||
|| it.value() | ||||
== VisualizationDragDropContainer::DropBehavior::InsertedAndMerged)) { | ||||
r838 | return true; | |||
r844 | } | |||
r838 | } | |||
return false; | ||||
} | ||||
bool hasPlaceHolder() const | ||||
{ | ||||
r1075 | return sqpApp->dragDropGuiController().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 | { | |||
r938 | auto widgetUnderMouse = sqpApp->widgetAt(QCursor::pos()); | |||
return container->isAncestorOf(widgetUnderMouse) && widgetUnderMouse != container | ||||
r1075 | && sqpApp->dragDropGuiController().placeHolder().isAncestorOf(widgetUnderMouse); | |||
r838 | } | |||
r873 | ||||
r880 | int countDragWidget(const QWidget *parent, bool onlyVisible = false) const | |||
r873 | { | |||
auto nbGraph = 0; | ||||
for (auto child : parent->children()) { | ||||
if (qobject_cast<VisualizationDragWidget *>(child)) { | ||||
r880 | if (!onlyVisible || qobject_cast<VisualizationDragWidget *>(child)->isVisible()) { | |||
nbGraph += 1; | ||||
} | ||||
r873 | } | |||
} | ||||
return nbGraph; | ||||
} | ||||
Alexandre Leroux
|
r1023 | bool findPlaceHolderPosition(const QPoint &pos, const QMimeData *mimeData, bool canInsert, | ||
bool canMerge, const VisualizationDragDropContainer *container); | ||||
r838 | }; | |||
VisualizationDragDropContainer::VisualizationDragDropContainer(QWidget *parent) | ||||
r880 | : QFrame{parent}, | |||
r844 | 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 | } | |||
r936 | void VisualizationDragDropContainer::setMimeType( | |||
r879 | const QString &mimeType, VisualizationDragDropContainer::DropBehavior behavior) | |||
r838 | { | |||
r879 | impl->m_AcceptedMimeTypes[mimeType] = behavior; | |||
r838 | } | |||
int VisualizationDragDropContainer::countDragWidget() const | ||||
{ | ||||
r873 | return impl->countDragWidget(this); | |||
r838 | } | |||
r850 | void VisualizationDragDropContainer::setAcceptMimeDataFunction( | |||
VisualizationDragDropContainer::AcceptMimeDataFunction fun) | ||||
{ | ||||
impl->m_AcceptMimeDataFun = fun; | ||||
} | ||||
Alexandre Leroux
|
r1023 | void VisualizationDragDropContainer::setAcceptDragWidgetFunction( | ||
VisualizationDragDropContainer::AcceptDragWidgetFunction fun) | ||||
{ | ||||
impl->m_AcceptDragWidgetFun = fun; | ||||
} | ||||
r1075 | void VisualizationDragDropContainer::setPlaceHolderType(DragDropGuiController::PlaceHolderType type, | |||
r881 | const QString &placeHolderText) | |||
{ | ||||
impl->m_PlaceHolderType = type; | ||||
impl->m_PlaceHolderText = placeHolderText; | ||||
} | ||||
r844 | void VisualizationDragDropContainer::startDrag(VisualizationDragWidget *dragWidget, | |||
const QPoint &dragPosition) | ||||
r838 | { | |||
r1075 | auto &helper = sqpApp->dragDropGuiController(); | |||
r850 | helper.resetDragAndDrop(); | |||
r838 | ||||
r844 | // Note: The management of the drag object is done by Qt | |||
r846 | auto drag = new QDrag{dragWidget}; | |||
r838 | ||||
r1047 | auto mimeData = dragWidget->mimeData(dragPosition); | |||
r838 | drag->setMimeData(mimeData); | |||
r1047 | auto pixmap = dragWidget->customDragPixmap(dragPosition); | |||
if (pixmap.isNull()) { | ||||
pixmap = QPixmap{dragWidget->size()}; | ||||
dragWidget->render(&pixmap); | ||||
} | ||||
r934 | drag->setPixmap(pixmap.scaled(DRAGGED_MINIATURE_WIDTH, DRAGGED_MINIATURE_WIDTH, | |||
Qt::KeepAspectRatio, Qt::SmoothTransformation)); | ||||
r838 | ||||
auto image = pixmap.toImage(); | ||||
mimeData->setImageData(image); | ||||
mimeData->setUrls({helper.imageTemporaryUrl(image)}); | ||||
r844 | if (impl->m_Layout->indexOf(dragWidget) >= 0) { | |||
r838 | ||||
r1047 | if (impl->acceptMimeData(mimeData) && impl->allowInsertForMimeData(mimeData)) { | |||
helper.setCurrentDragWidget(dragWidget); | ||||
if (impl->cursorIsInContainer(this)) { | ||||
auto dragWidgetIndex = impl->m_Layout->indexOf(dragWidget); | ||||
helper.insertPlaceHolder(impl->m_Layout, dragWidgetIndex, impl->m_PlaceHolderType, | ||||
impl->m_PlaceHolderText); | ||||
dragWidget->setVisible(false); | ||||
} | ||||
else { | ||||
// The drag starts directly outside the drop zone | ||||
// do not add the placeHolder | ||||
} | ||||
r874 | } | |||
r838 | ||||
Thibaud Rabillard
|
r911 | drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::MoveAction); | ||
helper.doCloseWidgets(); | ||||
r850 | } | |||
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(); | |||
r1075 | auto &helper = sqpApp->dragDropGuiController(); | |||
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 | } | |||
r879 | auto canMerge = impl->allowMergeForMimeData(event->mimeData()); | |||
auto canInsert = impl->allowInsertForMimeData(event->mimeData()); | ||||
Alexandre Leroux
|
r1023 | if (!impl->findPlaceHolderPosition(event->pos(), event->mimeData(), canInsert, canMerge, | ||
this)) { | ||||
event->ignore(); | ||||
} | ||||
r838 | } | |||
r850 | else { | |||
// do nothing | ||||
} | ||||
r838 | } | |||
r846 | else { | |||
r838 | event->ignore(); | |||
r846 | } | |||
r838 | ||||
QWidget::dragEnterEvent(event); | ||||
} | ||||
void VisualizationDragDropContainer::dragLeaveEvent(QDragLeaveEvent *event) | ||||
{ | ||||
Q_UNUSED(event); | ||||
r1075 | auto &helper = sqpApp->dragDropGuiController(); | |||
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())) { | |||
r879 | auto canMerge = impl->allowMergeForMimeData(event->mimeData()); | |||
auto canInsert = impl->allowInsertForMimeData(event->mimeData()); | ||||
Alexandre Leroux
|
r1023 | impl->findPlaceHolderPosition(event->pos(), event->mimeData(), canInsert, canMerge, this); | ||
r838 | } | |||
r846 | else { | |||
r838 | event->ignore(); | |||
r846 | } | |||
r838 | ||||
QWidget::dragMoveEvent(event); | ||||
} | ||||
void VisualizationDragDropContainer::dropEvent(QDropEvent *event) | ||||
{ | ||||
r1075 | auto &helper = sqpApp->dragDropGuiController(); | |||
r875 | ||||
r844 | if (impl->acceptMimeData(event->mimeData())) { | |||
r875 | auto dragWidget = helper.getCurrentDragWidget(); | |||
r850 | if (impl->hasPlaceHolder()) { | |||
r875 | // drop where the placeHolder is located | |||
r838 | ||||
r879 | auto canInsert = impl->allowInsertForMimeData(event->mimeData()); | |||
if (canInsert) { | ||||
auto droppedIndex = impl->m_Layout->indexOf(&helper.placeHolder()); | ||||
r838 | ||||
r879 | 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 | ||||
r879 | dragWidget->setVisible(true); | |||
} | ||||
r838 | ||||
r879 | event->acceptProposedAction(); | |||
r838 | ||||
r879 | helper.removePlaceHolder(); | |||
r838 | ||||
r879 | emit dropOccuredInContainer(droppedIndex, event->mimeData()); | |||
} | ||||
else { | ||||
qCWarning(LOG_VisualizationDragDropContainer()) << tr( | ||||
"VisualizationDragDropContainer::dropEvent, dropping on the placeHolder, but " | ||||
"the insertion is forbidden."); | ||||
Q_ASSERT(false); | ||||
} | ||||
r838 | } | |||
r875 | else if (helper.getHightlightedDragWidget()) { | |||
// drop on the highlighted widget | ||||
r879 | auto canMerge = impl->allowMergeForMimeData(event->mimeData()); | |||
r875 | 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 | ||||
r1075 | sqpApp->dragDropGuiController().setHightlightedDragWidget(nullptr); | |||
r873 | impl->m_MinContainerHeight = 0; | |||
r838 | QWidget::dropEvent(event); | |||
} | ||||
r873 | ||||
Alexandre Leroux
|
r1023 | bool VisualizationDragDropContainer::VisualizationDragDropContainerPrivate::findPlaceHolderPosition( | ||
const QPoint &pos, const QMimeData *mimeData, bool canInsert, bool canMerge, | ||||
r879 | const VisualizationDragDropContainer *container) | |||
r873 | { | |||
r1075 | auto &helper = sqpApp->dragDropGuiController(); | |||
r873 | ||||
r880 | auto absPos = container->mapToGlobal(pos); | |||
r938 | auto isOnPlaceHolder = helper.placeHolder().isAncestorOf(sqpApp->widgetAt(absPos)); | |||
r880 | ||||
if (countDragWidget(container, true) == 0) { | ||||
// Drop on an empty container, just add the placeHolder at the top | ||||
r881 | helper.insertPlaceHolder(m_Layout, 0, m_PlaceHolderType, m_PlaceHolderText); | |||
r880 | } | |||
else if (!isOnPlaceHolder) { | ||||
r873 | 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); | ||||
r1091 | auto zoneSize = graphHeight / 4.0; | |||
r873 | ||||
auto isOnTop = posY < dropIndex * graphHeight + zoneSize; | ||||
auto isOnBottom = posY > (dropIndex + 1) * graphHeight - zoneSize; | ||||
auto placeHolderIndex = m_Layout->indexOf(&(helper.placeHolder())); | ||||
r880 | auto dragWidgetHovered = getChildDragWidgetAt(container, pos); | |||
r1090 | auto acceptMerge = m_AcceptDragWidgetFun(dragWidgetHovered, mimeData); | |||
if (canInsert && (isOnTop || isOnBottom || !canMerge || !acceptMerge)) { | ||||
if (posY > (dropIndex + 1) * graphHeight - graphHeight / 2.0) { | ||||
r873 | 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) { | ||||
r881 | helper.insertPlaceHolder(m_Layout, dropIndex, m_PlaceHolderType, | |||
m_PlaceHolderText); | ||||
r873 | } | |||
helper.setHightlightedDragWidget(nullptr); | ||||
} | ||||
r880 | else if (canMerge && dragWidgetHovered) { | |||
r873 | // drop on the middle -> merge | |||
if (hasPlaceHolder()) { | ||||
helper.removePlaceHolder(); | ||||
} | ||||
r1090 | helper.setHightlightedDragWidget(dragWidgetHovered); | |||
r873 | } | |||
r879 | else { | |||
r881 | qCWarning(LOG_VisualizationDragDropContainer()) | |||
<< tr("VisualizationDragDropContainer::findPlaceHolderPosition, no valid drop " | ||||
"action."); | ||||
r879 | } | |||
r873 | } | |||
else { | ||||
r1091 | qCInfo(LOG_VisualizationDragDropContainer()) | |||
r881 | << tr("VisualizationDragDropContainer::findPlaceHolderPosition, no widget " | |||
"found in the " | ||||
"container"); | ||||
r873 | } | |||
} | ||||
else { | ||||
r880 | // the mouse is hover the placeHolder | |||
r873 | // Do nothing | |||
} | ||||
Alexandre Leroux
|
r1023 | |||
return true; | ||||
r873 | } | |||