VisualizationDragDropContainer.cpp
499 lines
| 17.8 KiB
| text/x-c
|
CppLexer
r841 | #include "Visualization/VisualizationDragDropContainer.h" | |||
r890 | #include "DragAndDrop/DragDropHelper.h" | |||
r847 | #include "SqpApplication.h" | |||
#include "Visualization/VisualizationDragWidget.h" | ||||
r841 | ||||
r853 | #include "Common/VisualizationDef.h" | |||
r841 | #include <QDrag> | |||
#include <QDragEnterEvent> | ||||
r847 | #include <QVBoxLayout> | |||
r841 | ||||
r848 | #include <cmath> | |||
r841 | #include <memory> | |||
r852 | Q_LOGGING_CATEGORY(LOG_VisualizationDragDropContainer, "VisualizationDragDropContainer") | |||
r975 | auto DRAGGED_MINIATURE_WIDTH = 200; // in pixels | |||
r841 | struct VisualizationDragDropContainer::VisualizationDragDropContainerPrivate { | |||
r847 | QVBoxLayout *m_Layout; | |||
r885 | QHash<QString, VisualizationDragDropContainer::DropBehavior> m_AcceptedMimeTypes; | |||
r887 | QString m_PlaceHolderText; | |||
r1003 | DragDropHelper::PlaceHolderType m_PlaceHolderType; | |||
r885 | ||||
r852 | VisualizationDragDropContainer::AcceptMimeDataFunction m_AcceptMimeDataFun | |||
= [](auto mimeData) { return true; }; | ||||
Alexandre Leroux
|
r1064 | VisualizationDragDropContainer::AcceptDragWidgetFunction m_AcceptDragWidgetFun | ||
= [](auto dragWidget, auto mimeData) { return true; }; | ||||
r885 | ||||
r879 | int m_MinContainerHeight = 0; | |||
r841 | ||||
r847 | explicit VisualizationDragDropContainerPrivate(QWidget *widget) | |||
r1003 | : m_PlaceHolderType(DragDropHelper::PlaceHolderType::Graph) | |||
r841 | { | |||
r847 | m_Layout = new QVBoxLayout(widget); | |||
m_Layout->setContentsMargins(0, 0, 0, 0); | ||||
r841 | } | |||
r847 | bool acceptMimeData(const QMimeData *data) const | |||
r841 | { | |||
r977 | 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; | ||||
} | ||||
r847 | } | |||
r841 | } | |||
r977 | if (accepted) { | |||
accepted = m_AcceptMimeDataFun(data); | ||||
} | ||||
return accepted; | ||||
r841 | } | |||
r885 | bool allowMergeForMimeData(const QMimeData *data) const | |||
r841 | { | |||
r980 | auto result = false; | |||
r885 | 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)) { | ||||
r841 | return true; | |||
r847 | } | |||
r841 | } | |||
return false; | ||||
} | ||||
bool hasPlaceHolder() const | ||||
{ | ||||
r847 | return sqpApp->dragDropHelper().placeHolder().parentWidget() == m_Layout->parentWidget(); | |||
r841 | } | |||
r879 | VisualizationDragWidget *getChildDragWidgetAt(const QWidget *parent, const QPoint &pos) const | |||
r841 | { | |||
r847 | VisualizationDragWidget *dragWidget = nullptr; | |||
for (auto child : parent->children()) { | ||||
auto widget = qobject_cast<VisualizationDragWidget *>(child); | ||||
if (widget && widget->isVisible()) { | ||||
if (widget->frameGeometry().contains(pos)) { | ||||
r841 | dragWidget = widget; | |||
break; | ||||
} | ||||
} | ||||
} | ||||
return dragWidget; | ||||
} | ||||
r847 | bool cursorIsInContainer(QWidget *container) const | |||
r841 | { | |||
r979 | auto widgetUnderMouse = sqpApp->widgetAt(QCursor::pos()); | |||
return container->isAncestorOf(widgetUnderMouse) && widgetUnderMouse != container | ||||
&& sqpApp->dragDropHelper().placeHolder().isAncestorOf(widgetUnderMouse); | ||||
r841 | } | |||
r879 | ||||
r886 | int countDragWidget(const QWidget *parent, bool onlyVisible = false) const | |||
r879 | { | |||
auto nbGraph = 0; | ||||
for (auto child : parent->children()) { | ||||
if (qobject_cast<VisualizationDragWidget *>(child)) { | ||||
r886 | if (!onlyVisible || qobject_cast<VisualizationDragWidget *>(child)->isVisible()) { | |||
nbGraph += 1; | ||||
} | ||||
r879 | } | |||
} | ||||
return nbGraph; | ||||
} | ||||
Alexandre Leroux
|
r1064 | bool findPlaceHolderPosition(const QPoint &pos, const QMimeData *mimeData, bool canInsert, | ||
bool canMerge, const VisualizationDragDropContainer *container); | ||||
r841 | }; | |||
VisualizationDragDropContainer::VisualizationDragDropContainer(QWidget *parent) | ||||
r886 | : QFrame{parent}, | |||
r847 | impl{spimpl::make_unique_impl<VisualizationDragDropContainerPrivate>(this)} | |||
r841 | { | |||
setAcceptDrops(true); | ||||
} | ||||
void VisualizationDragDropContainer::addDragWidget(VisualizationDragWidget *dragWidget) | ||||
{ | ||||
r847 | impl->m_Layout->addWidget(dragWidget); | |||
r841 | disconnect(dragWidget, &VisualizationDragWidget::dragDetected, nullptr, nullptr); | |||
r847 | connect(dragWidget, &VisualizationDragWidget::dragDetected, this, | |||
&VisualizationDragDropContainer::startDrag); | ||||
r841 | } | |||
r847 | void VisualizationDragDropContainer::insertDragWidget(int index, | |||
VisualizationDragWidget *dragWidget) | ||||
r841 | { | |||
r847 | impl->m_Layout->insertWidget(index, dragWidget); | |||
r841 | disconnect(dragWidget, &VisualizationDragWidget::dragDetected, nullptr, nullptr); | |||
r847 | connect(dragWidget, &VisualizationDragWidget::dragDetected, this, | |||
&VisualizationDragDropContainer::startDrag); | ||||
r841 | } | |||
r977 | void VisualizationDragDropContainer::setMimeType( | |||
r885 | const QString &mimeType, VisualizationDragDropContainer::DropBehavior behavior) | |||
r841 | { | |||
r885 | impl->m_AcceptedMimeTypes[mimeType] = behavior; | |||
r841 | } | |||
int VisualizationDragDropContainer::countDragWidget() const | ||||
{ | ||||
r879 | return impl->countDragWidget(this); | |||
r841 | } | |||
r852 | void VisualizationDragDropContainer::setAcceptMimeDataFunction( | |||
VisualizationDragDropContainer::AcceptMimeDataFunction fun) | ||||
{ | ||||
impl->m_AcceptMimeDataFun = fun; | ||||
} | ||||
Alexandre Leroux
|
r1064 | void VisualizationDragDropContainer::setAcceptDragWidgetFunction( | ||
VisualizationDragDropContainer::AcceptDragWidgetFunction fun) | ||||
{ | ||||
impl->m_AcceptDragWidgetFun = fun; | ||||
} | ||||
r887 | void VisualizationDragDropContainer::setPlaceHolderType(DragDropHelper::PlaceHolderType type, | |||
const QString &placeHolderText) | ||||
{ | ||||
impl->m_PlaceHolderType = type; | ||||
impl->m_PlaceHolderText = placeHolderText; | ||||
} | ||||
r847 | void VisualizationDragDropContainer::startDrag(VisualizationDragWidget *dragWidget, | |||
const QPoint &dragPosition) | ||||
r841 | { | |||
r847 | auto &helper = sqpApp->dragDropHelper(); | |||
r852 | helper.resetDragAndDrop(); | |||
r841 | ||||
r847 | // Note: The management of the drag object is done by Qt | |||
r849 | auto drag = new QDrag{dragWidget}; | |||
r841 | ||||
r1087 | auto mimeData = dragWidget->mimeData(dragPosition); | |||
r841 | drag->setMimeData(mimeData); | |||
r1087 | auto pixmap = dragWidget->customDragPixmap(dragPosition); | |||
if (pixmap.isNull()) { | ||||
pixmap = QPixmap{dragWidget->size()}; | ||||
dragWidget->render(&pixmap); | ||||
} | ||||
r975 | drag->setPixmap(pixmap.scaled(DRAGGED_MINIATURE_WIDTH, DRAGGED_MINIATURE_WIDTH, | |||
Qt::KeepAspectRatio, Qt::SmoothTransformation)); | ||||
r841 | ||||
auto image = pixmap.toImage(); | ||||
mimeData->setImageData(image); | ||||
mimeData->setUrls({helper.imageTemporaryUrl(image)}); | ||||
r847 | if (impl->m_Layout->indexOf(dragWidget) >= 0) { | |||
r841 | ||||
r1087 | 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 | ||||
} | ||||
r880 | } | |||
r841 | ||||
Thibaud Rabillard
|
r912 | drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::MoveAction); | ||
helper.doCloseWidgets(); | ||||
r852 | } | |||
else { | ||||
qCWarning(LOG_VisualizationDragDropContainer()) | ||||
<< tr("VisualizationDragDropContainer::startDrag, drag aborted, the specified " | ||||
"VisualizationDragWidget is not found in this container."); | ||||
} | ||||
r841 | } | |||
void VisualizationDragDropContainer::dragEnterEvent(QDragEnterEvent *event) | ||||
{ | ||||
r847 | if (impl->acceptMimeData(event->mimeData())) { | |||
r841 | event->acceptProposedAction(); | |||
r847 | auto &helper = sqpApp->dragDropHelper(); | |||
r841 | ||||
r847 | if (!impl->hasPlaceHolder()) { | |||
r841 | auto dragWidget = helper.getCurrentDragWidget(); | |||
r852 | ||||
if (dragWidget) { | ||||
// If the drag&drop is internal to the visualization, entering the container hide | ||||
r879 | // the dragWidget which was made visible by the dragLeaveEvent | |||
r852 | auto parentWidget | |||
= qobject_cast<VisualizationDragDropContainer *>(dragWidget->parentWidget()); | ||||
if (parentWidget) { | ||||
dragWidget->setVisible(false); | ||||
} | ||||
r841 | } | |||
r885 | auto canMerge = impl->allowMergeForMimeData(event->mimeData()); | |||
auto canInsert = impl->allowInsertForMimeData(event->mimeData()); | ||||
Alexandre Leroux
|
r1064 | if (!impl->findPlaceHolderPosition(event->pos(), event->mimeData(), canInsert, canMerge, | ||
this)) { | ||||
event->ignore(); | ||||
} | ||||
r841 | } | |||
r852 | else { | |||
// do nothing | ||||
} | ||||
r841 | } | |||
r849 | else { | |||
r841 | event->ignore(); | |||
r849 | } | |||
r841 | ||||
QWidget::dragEnterEvent(event); | ||||
} | ||||
void VisualizationDragDropContainer::dragLeaveEvent(QDragLeaveEvent *event) | ||||
{ | ||||
Q_UNUSED(event); | ||||
r847 | auto &helper = sqpApp->dragDropHelper(); | |||
r841 | ||||
r847 | if (!impl->cursorIsInContainer(this)) { | |||
r841 | helper.removePlaceHolder(); | |||
r879 | helper.setHightlightedDragWidget(nullptr); | |||
impl->m_MinContainerHeight = 0; | ||||
r841 | ||||
r852 | 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 | ||||
r847 | // 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) | ||||
r841 | ||||
r847 | if (dragWidget) { | |||
r841 | dragWidget->setVisible(true); | |||
} | ||||
} | ||||
} | ||||
r852 | 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. | ||||
} | ||||
r841 | ||||
QWidget::dragLeaveEvent(event); | ||||
} | ||||
void VisualizationDragDropContainer::dragMoveEvent(QDragMoveEvent *event) | ||||
{ | ||||
r847 | if (impl->acceptMimeData(event->mimeData())) { | |||
r885 | auto canMerge = impl->allowMergeForMimeData(event->mimeData()); | |||
auto canInsert = impl->allowInsertForMimeData(event->mimeData()); | ||||
Alexandre Leroux
|
r1064 | impl->findPlaceHolderPosition(event->pos(), event->mimeData(), canInsert, canMerge, this); | ||
r841 | } | |||
r849 | else { | |||
r841 | event->ignore(); | |||
r849 | } | |||
r841 | ||||
QWidget::dragMoveEvent(event); | ||||
} | ||||
void VisualizationDragDropContainer::dropEvent(QDropEvent *event) | ||||
{ | ||||
r881 | auto &helper = sqpApp->dragDropHelper(); | |||
r847 | if (impl->acceptMimeData(event->mimeData())) { | |||
r881 | auto dragWidget = helper.getCurrentDragWidget(); | |||
r852 | if (impl->hasPlaceHolder()) { | |||
r881 | // drop where the placeHolder is located | |||
r841 | ||||
r885 | auto canInsert = impl->allowInsertForMimeData(event->mimeData()); | |||
if (canInsert) { | ||||
auto droppedIndex = impl->m_Layout->indexOf(&helper.placeHolder()); | ||||
r841 | ||||
r885 | 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; | ||||
} | ||||
r841 | ||||
r885 | dragWidget->setVisible(true); | |||
} | ||||
r841 | ||||
r885 | event->acceptProposedAction(); | |||
r841 | ||||
r885 | helper.removePlaceHolder(); | |||
r841 | ||||
r885 | emit dropOccuredInContainer(droppedIndex, event->mimeData()); | |||
} | ||||
else { | ||||
qCWarning(LOG_VisualizationDragDropContainer()) << tr( | ||||
"VisualizationDragDropContainer::dropEvent, dropping on the placeHolder, but " | ||||
"the insertion is forbidden."); | ||||
Q_ASSERT(false); | ||||
} | ||||
r841 | } | |||
r881 | else if (helper.getHightlightedDragWidget()) { | |||
// drop on the highlighted widget | ||||
r885 | auto canMerge = impl->allowMergeForMimeData(event->mimeData()); | |||
r881 | 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); | ||||
} | ||||
r852 | } | |||
r841 | } | |||
r849 | else { | |||
r841 | event->ignore(); | |||
r849 | } | |||
r841 | ||||
r879 | sqpApp->dragDropHelper().setHightlightedDragWidget(nullptr); | |||
impl->m_MinContainerHeight = 0; | ||||
r841 | QWidget::dropEvent(event); | |||
} | ||||
r879 | ||||
Alexandre Leroux
|
r1064 | bool VisualizationDragDropContainer::VisualizationDragDropContainerPrivate::findPlaceHolderPosition( | ||
const QPoint &pos, const QMimeData *mimeData, bool canInsert, bool canMerge, | ||||
r885 | const VisualizationDragDropContainer *container) | |||
r879 | { | |||
auto &helper = sqpApp->dragDropHelper(); | ||||
r886 | auto absPos = container->mapToGlobal(pos); | |||
r979 | auto isOnPlaceHolder = helper.placeHolder().isAncestorOf(sqpApp->widgetAt(absPos)); | |||
r886 | ||||
if (countDragWidget(container, true) == 0) { | ||||
// Drop on an empty container, just add the placeHolder at the top | ||||
r887 | helper.insertPlaceHolder(m_Layout, 0, m_PlaceHolderType, m_PlaceHolderText); | |||
r886 | } | |||
else if (!isOnPlaceHolder) { | ||||
r879 | 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())); | ||||
r886 | auto dragWidgetHovered = getChildDragWidgetAt(container, pos); | |||
r885 | if (canInsert && (isOnTop || isOnBottom || !canMerge)) { | |||
r879 | 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) { | ||||
r887 | helper.insertPlaceHolder(m_Layout, dropIndex, m_PlaceHolderType, | |||
m_PlaceHolderText); | ||||
r879 | } | |||
helper.setHightlightedDragWidget(nullptr); | ||||
} | ||||
r886 | else if (canMerge && dragWidgetHovered) { | |||
r879 | // drop on the middle -> merge | |||
if (hasPlaceHolder()) { | ||||
helper.removePlaceHolder(); | ||||
} | ||||
Alexandre Leroux
|
r1064 | if (m_AcceptDragWidgetFun(dragWidgetHovered, mimeData)) { | ||
helper.setHightlightedDragWidget(dragWidgetHovered); | ||||
return true; | ||||
} | ||||
else { | ||||
return false; | ||||
} | ||||
r879 | } | |||
r885 | else { | |||
r887 | qCWarning(LOG_VisualizationDragDropContainer()) | |||
<< tr("VisualizationDragDropContainer::findPlaceHolderPosition, no valid drop " | ||||
"action."); | ||||
r885 | } | |||
r879 | } | |||
else { | ||||
r886 | qCWarning(LOG_VisualizationDragDropContainer()) | |||
r887 | << tr("VisualizationDragDropContainer::findPlaceHolderPosition, no widget " | |||
"found in the " | ||||
"container"); | ||||
r879 | } | |||
} | ||||
else { | ||||
r886 | // the mouse is hover the placeHolder | |||
r879 | // Do nothing | |||
} | ||||
Alexandre Leroux
|
r1064 | |||
return true; | ||||
r879 | } | |||