VisualizationDragDropContainer.cpp
301 lines
| 10.1 KiB
| text/x-c
|
CppLexer
r841 | #include "Visualization/VisualizationDragDropContainer.h" | |||
#include "DragDropHelper.h" | ||||
r847 | #include "SqpApplication.h" | |||
#include "Visualization/VisualizationDragWidget.h" | ||||
r841 | ||||
#include <QDrag> | ||||
#include <QDragEnterEvent> | ||||
r847 | #include <QVBoxLayout> | |||
r841 | ||||
#include <memory> | ||||
struct VisualizationDragDropContainer::VisualizationDragDropContainerPrivate { | ||||
r847 | QVBoxLayout *m_Layout; | |||
QStringList m_AcceptedMimeTypes; | ||||
QStringList m_MergeAllowedMimeTypes; | ||||
r841 | ||||
r847 | explicit VisualizationDragDropContainerPrivate(QWidget *widget) | |||
r841 | { | |||
r847 | m_Layout = new QVBoxLayout(widget); | |||
m_Layout->setContentsMargins(0, 0, 0, 0); | ||||
r841 | } | |||
r847 | bool acceptMimeData(const QMimeData *data) const | |||
r841 | { | |||
r847 | for (const auto &type : m_AcceptedMimeTypes) { | |||
if (data->hasFormat(type)) { | ||||
r841 | return true; | |||
r847 | } | |||
r841 | } | |||
return false; | ||||
} | ||||
r847 | bool allowMergeMimeData(const QMimeData *data) const | |||
r841 | { | |||
r847 | for (const auto &type : m_MergeAllowedMimeTypes) { | |||
if (data->hasFormat(type)) { | ||||
r841 | return true; | |||
r847 | } | |||
r841 | } | |||
return false; | ||||
} | ||||
bool hasPlaceHolder() const | ||||
{ | ||||
r847 | return sqpApp->dragDropHelper().placeHolder().parentWidget() == m_Layout->parentWidget(); | |||
r841 | } | |||
r847 | VisualizationDragWidget *getChildDragWidgetAt(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 | { | |||
r847 | auto adustNum = 18; // to be safe, in case of scrollbar on the side | |||
auto containerRect = QRect(QPoint(), container->contentsRect().size()) | ||||
.adjusted(adustNum, adustNum, -adustNum, -adustNum); | ||||
r841 | qDebug() << containerRect << container->mapFromGlobal(QCursor::pos()); | |||
return containerRect.contains(container->mapFromGlobal(QCursor::pos())); | ||||
} | ||||
}; | ||||
VisualizationDragDropContainer::VisualizationDragDropContainer(QWidget *parent) | ||||
r847 | : QWidget{parent}, | |||
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 | } | |||
void VisualizationDragDropContainer::setAcceptedMimeTypes(const QStringList &mimeTypes) | ||||
{ | ||||
r847 | impl->m_AcceptedMimeTypes = mimeTypes; | |||
r841 | } | |||
void VisualizationDragDropContainer::setMergeAllowedMimeTypes(const QStringList &mimeTypes) | ||||
{ | ||||
r847 | impl->m_MergeAllowedMimeTypes = mimeTypes; | |||
r841 | } | |||
int VisualizationDragDropContainer::countDragWidget() const | ||||
{ | ||||
auto nbGraph = 0; | ||||
r847 | for (auto child : children()) { | |||
auto widget = qobject_cast<VisualizationDragWidget *>(child); | ||||
if (widget) { | ||||
r841 | nbGraph += 1; | |||
} | ||||
} | ||||
return nbGraph; | ||||
} | ||||
r847 | void VisualizationDragDropContainer::startDrag(VisualizationDragWidget *dragWidget, | |||
const QPoint &dragPosition) | ||||
r841 | { | |||
r847 | auto &helper = sqpApp->dragDropHelper(); | |||
r841 | ||||
r847 | // Note: The management of the drag object is done by Qt | |||
r841 | auto *drag = new QDrag{dragWidget}; | |||
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)}); | ||||
r847 | if (impl->m_Layout->indexOf(dragWidget) >= 0) { | |||
r841 | helper.setCurrentDragWidget(dragWidget); | |||
r847 | if (impl->cursorIsInContainer(this)) { | |||
auto dragWidgetIndex = impl->m_Layout->indexOf(dragWidget); | ||||
helper.insertPlaceHolder(impl->m_Layout, dragWidgetIndex); | ||||
r841 | dragWidget->setVisible(false); | |||
} | ||||
} | ||||
r847 | // Note: The exec() is blocking on windows but not on linux and macOS | |||
r841 | drag->exec(Qt::MoveAction | Qt::CopyAction); | |||
} | ||||
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(); | |||
r847 | auto parentWidget | |||
= qobject_cast<VisualizationDragDropContainer *>(dragWidget->parentWidget()); | ||||
if (parentWidget) { | ||||
r841 | dragWidget->setVisible(false); | |||
} | ||||
auto dragWidgetHovered = impl->getChildDragWidgetAt(this, event->pos()); | ||||
r847 | if (dragWidgetHovered) { | |||
auto hoveredWidgetIndex = impl->m_Layout->indexOf(dragWidgetHovered); | ||||
auto dragWidgetIndex = impl->m_Layout->indexOf(helper.getCurrentDragWidget()); | ||||
if (dragWidgetIndex >= 0 && dragWidgetIndex <= hoveredWidgetIndex) { | ||||
hoveredWidgetIndex | ||||
+= 1; // Correction of the index if the drop occurs in the same container | ||||
} | ||||
r841 | ||||
r847 | helper.insertPlaceHolder(impl->m_Layout, hoveredWidgetIndex); | |||
r841 | } | |||
r847 | else { | |||
helper.insertPlaceHolder(impl->m_Layout, 0); | ||||
r841 | } | |||
} | ||||
} | ||||
else | ||||
event->ignore(); | ||||
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(); | |||
bool isInternal = true; | ||||
r847 | if (isInternal) { | |||
// Only if the drag is strated from the visualization | ||||
// Show the drag widget at its original place | ||||
// 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 | ||||
auto dragWidget = sqpApp->dragDropHelper().getCurrentDragWidget(); | ||||
r847 | if (dragWidget) { | |||
r841 | dragWidget->setVisible(true); | |||
} | ||||
} | ||||
} | ||||
QWidget::dragLeaveEvent(event); | ||||
} | ||||
void VisualizationDragDropContainer::dragMoveEvent(QDragMoveEvent *event) | ||||
{ | ||||
r847 | if (impl->acceptMimeData(event->mimeData())) { | |||
r841 | auto dragWidgetHovered = impl->getChildDragWidgetAt(this, event->pos()); | |||
r847 | if (dragWidgetHovered) { | |||
r841 | auto canMerge = impl->allowMergeMimeData(event->mimeData()); | |||
auto nbDragWidget = countDragWidget(); | ||||
r847 | if (nbDragWidget > 0) { | |||
r841 | auto graphHeight = size().height() / nbDragWidget; | |||
auto dropIndex = floor(event->pos().y() / graphHeight); | ||||
auto zoneSize = qMin(graphHeight / 3.0, 150.0); | ||||
auto isOnTop = event->pos().y() < dropIndex * graphHeight + zoneSize; | ||||
auto isOnBottom = event->pos().y() > (dropIndex + 1) * graphHeight - zoneSize; | ||||
r847 | auto &helper = sqpApp->dragDropHelper(); | |||
auto placeHolderIndex = impl->m_Layout->indexOf(&(helper.placeHolder())); | ||||
r841 | ||||
r847 | if (isOnTop || isOnBottom) { | |||
if (isOnBottom) { | ||||
r841 | dropIndex += 1; | |||
r847 | } | |||
r841 | ||||
r847 | auto dragWidgetIndex = impl->m_Layout->indexOf(helper.getCurrentDragWidget()); | |||
if (dragWidgetIndex >= 0 && dragWidgetIndex <= dropIndex) { | ||||
dropIndex += 1; // Correction of the index if the drop occurs in the same | ||||
// container | ||||
} | ||||
r841 | ||||
r847 | if (dropIndex != placeHolderIndex) { | |||
helper.insertPlaceHolder(impl->m_Layout, dropIndex); | ||||
r841 | } | |||
} | ||||
r847 | else if (canMerge) { | |||
// drop on the middle -> merge | ||||
if (impl->hasPlaceHolder()) { | ||||
r841 | helper.removePlaceHolder(); | |||
} | ||||
} | ||||
} | ||||
} | ||||
} | ||||
else | ||||
event->ignore(); | ||||
QWidget::dragMoveEvent(event); | ||||
} | ||||
void VisualizationDragDropContainer::dropEvent(QDropEvent *event) | ||||
{ | ||||
r847 | if (impl->acceptMimeData(event->mimeData())) { | |||
r841 | auto dragWidget = sqpApp->dragDropHelper().getCurrentDragWidget(); | |||
r847 | if (impl->hasPlaceHolder() && dragWidget) { | |||
auto &helper = sqpApp->dragDropHelper(); | ||||
r841 | ||||
r847 | auto droppedIndex = impl->m_Layout->indexOf(&helper.placeHolder()); | |||
r841 | ||||
r847 | auto dragWidgetIndex = impl->m_Layout->indexOf(dragWidget); | |||
if (dragWidgetIndex >= 0 && dragWidgetIndex < droppedIndex) { | ||||
droppedIndex | ||||
-= 1; // Correction of the index if the drop occurs in the same container | ||||
} | ||||
r841 | ||||
dragWidget->setVisible(true); | ||||
dragWidget->setStyleSheet(""); | ||||
event->acceptProposedAction(); | ||||
helper.removePlaceHolder(); | ||||
emit dropOccured(droppedIndex, event->mimeData()); | ||||
} | ||||
} | ||||
else | ||||
event->ignore(); | ||||
QWidget::dropEvent(event); | ||||
} | ||||