DragDropHelper.cpp
381 lines
| 13.3 KiB
| text/x-c
|
CppLexer
r884 | #include "DragAndDrop/DragDropHelper.h" | |||
r837 | #include "SqpApplication.h" | |||
r850 | #include "Visualization/VisualizationDragDropContainer.h" | |||
r844 | #include "Visualization/VisualizationDragWidget.h" | |||
r850 | #include "Visualization/VisualizationWidget.h" | |||
#include "Visualization/operations/FindVariableOperation.h" | ||||
r876 | #include "Variable/Variable.h" | |||
r850 | #include "Variable/VariableController.h" | |||
#include "Common/MimeTypesDef.h" | ||||
#include "Common/VisualizationDef.h" | ||||
r837 | ||||
r844 | #include <QDir> | |||
r837 | #include <QDragEnterEvent> | |||
r844 | #include <QDragMoveEvent> | |||
r881 | #include <QLabel> | |||
r837 | #include <QScrollArea> | |||
r844 | #include <QScrollBar> | |||
r837 | #include <QTimer> | |||
r844 | #include <QVBoxLayout> | |||
r837 | ||||
r840 | const int SCROLL_SPEED = 5; | |||
const int SCROLL_ZONE_SIZE = 50; | ||||
r850 | Q_LOGGING_CATEGORY(LOG_DragDropHelper, "DragDrophelper") | |||
r840 | struct DragDropScroller::DragDropScrollerPrivate { | |||
r844 | QList<QScrollArea *> m_ScrollAreas; | |||
QScrollArea *m_CurrentScrollArea = nullptr; | ||||
std::unique_ptr<QTimer> m_Timer = nullptr; | ||||
r840 | ||||
r844 | enum class ScrollDirection { up, down, unknown }; | |||
ScrollDirection m_Direction = ScrollDirection::unknown; | ||||
r840 | ||||
r844 | explicit DragDropScrollerPrivate() : m_Timer{std::make_unique<QTimer>()} | |||
r840 | { | |||
r844 | m_Timer->setInterval(0); | |||
r840 | } | |||
}; | ||||
r844 | DragDropScroller::DragDropScroller(QObject *parent) | |||
: QObject{parent}, impl{spimpl::make_unique_impl<DragDropScrollerPrivate>()} | ||||
r840 | { | |||
r844 | connect(impl->m_Timer.get(), &QTimer::timeout, this, &DragDropScroller::onTimer); | |||
r840 | } | |||
r844 | void DragDropScroller::addScrollArea(QScrollArea *scrollArea) | |||
r840 | { | |||
r844 | impl->m_ScrollAreas << scrollArea; | |||
r840 | scrollArea->viewport()->setAcceptDrops(true); | |||
} | ||||
r844 | void DragDropScroller::removeScrollArea(QScrollArea *scrollArea) | |||
r840 | { | |||
r844 | impl->m_ScrollAreas.removeAll(scrollArea); | |||
r840 | scrollArea->viewport()->setAcceptDrops(false); | |||
} | ||||
bool DragDropScroller::eventFilter(QObject *obj, QEvent *event) | ||||
{ | ||||
r844 | if (event->type() == QEvent::DragMove) { | |||
auto w = static_cast<QWidget *>(obj); | ||||
r840 | ||||
r844 | if (impl->m_CurrentScrollArea && impl->m_CurrentScrollArea->isAncestorOf(w)) { | |||
auto moveEvent = static_cast<QDragMoveEvent *>(event); | ||||
r840 | ||||
auto pos = moveEvent->pos(); | ||||
r844 | if (impl->m_CurrentScrollArea->viewport() != w) { | |||
r840 | auto globalPos = w->mapToGlobal(moveEvent->pos()); | |||
r844 | pos = impl->m_CurrentScrollArea->viewport()->mapFromGlobal(globalPos); | |||
r840 | } | |||
r844 | auto isInTopZone = pos.y() > impl->m_CurrentScrollArea->viewport()->size().height() | |||
- SCROLL_ZONE_SIZE; | ||||
r840 | auto isInBottomZone = pos.y() < SCROLL_ZONE_SIZE; | |||
r844 | if (!isInTopZone && !isInBottomZone) { | |||
impl->m_Direction = DragDropScrollerPrivate::ScrollDirection::unknown; | ||||
impl->m_Timer->stop(); | ||||
r840 | } | |||
r844 | else if (!impl->m_Timer->isActive()) { | |||
impl->m_Direction = isInTopZone ? DragDropScrollerPrivate::ScrollDirection::up | ||||
: DragDropScrollerPrivate::ScrollDirection::down; | ||||
impl->m_Timer->start(); | ||||
r840 | } | |||
} | ||||
} | ||||
r844 | else if (event->type() == QEvent::DragEnter) { | |||
auto w = static_cast<QWidget *>(obj); | ||||
r840 | ||||
r844 | for (auto scrollArea : impl->m_ScrollAreas) { | |||
if (impl->m_CurrentScrollArea != scrollArea && scrollArea->isAncestorOf(w)) { | ||||
auto enterEvent = static_cast<QDragEnterEvent *>(event); | ||||
r840 | enterEvent->acceptProposedAction(); | |||
enterEvent->setDropAction(Qt::IgnoreAction); | ||||
r844 | impl->m_CurrentScrollArea = scrollArea; | |||
r840 | break; | |||
} | ||||
} | ||||
} | ||||
r844 | else if (event->type() == QEvent::DragLeave) { | |||
if (impl->m_CurrentScrollArea) { | ||||
if (!QRect(QPoint(), impl->m_CurrentScrollArea->size()) | ||||
.contains(impl->m_CurrentScrollArea->mapFromGlobal(QCursor::pos()))) { | ||||
impl->m_CurrentScrollArea = nullptr; | ||||
impl->m_Direction = DragDropScrollerPrivate::ScrollDirection::unknown; | ||||
impl->m_Timer->stop(); | ||||
r840 | } | |||
} | ||||
} | ||||
r844 | else if (event->type() == QEvent::Drop) { | |||
if (impl->m_CurrentScrollArea) { | ||||
impl->m_CurrentScrollArea = nullptr; | ||||
impl->m_Direction = DragDropScrollerPrivate::ScrollDirection::unknown; | ||||
impl->m_Timer->stop(); | ||||
r840 | } | |||
} | ||||
return false; | ||||
} | ||||
void DragDropScroller::onTimer() | ||||
{ | ||||
r844 | if (impl->m_CurrentScrollArea) { | |||
r840 | auto mvt = 0; | |||
r844 | switch (impl->m_Direction) { | |||
case DragDropScrollerPrivate::ScrollDirection::up: | ||||
mvt = SCROLL_SPEED; | ||||
break; | ||||
case DragDropScrollerPrivate::ScrollDirection::down: | ||||
mvt = -SCROLL_SPEED; | ||||
break; | ||||
default: | ||||
break; | ||||
r840 | } | |||
r844 | impl->m_CurrentScrollArea->verticalScrollBar()->setValue( | |||
impl->m_CurrentScrollArea->verticalScrollBar()->value() + mvt); | ||||
r840 | } | |||
} | ||||
r837 | ||||
struct DragDropHelper::DragDropHelperPrivate { | ||||
r844 | VisualizationDragWidget *m_CurrentDragWidget = nullptr; | |||
std::unique_ptr<QWidget> m_PlaceHolder = nullptr; | ||||
r881 | QLabel *m_PlaceHolderLabel; | |||
QWidget *m_PlaceBackground; | ||||
r844 | std::unique_ptr<DragDropScroller> m_DragDropScroller = nullptr; | |||
QString m_ImageTempUrl; // Temporary file for image url generated by the drag & drop. Not using | ||||
// QTemporaryFile to have a name which is not generated. | ||||
r837 | ||||
r873 | VisualizationDragWidget *m_HighlightedDragWidget = nullptr; | |||
r874 | QMetaObject::Connection m_DragWidgetDestroyedConnection; | |||
QMetaObject::Connection m_HighlightedWidgetDestroyedConnection; | ||||
r837 | explicit DragDropHelperPrivate() | |||
r844 | : m_PlaceHolder{std::make_unique<QWidget>()}, | |||
m_DragDropScroller{std::make_unique<DragDropScroller>()} | ||||
r837 | { | |||
r881 | auto layout = new QVBoxLayout{m_PlaceHolder.get()}; | |||
layout->setSpacing(0); | ||||
layout->setContentsMargins(0, 0, 0, 0); | ||||
m_PlaceHolderLabel = new QLabel{"", m_PlaceHolder.get()}; | ||||
m_PlaceHolderLabel->setMinimumHeight(25); | ||||
layout->addWidget(m_PlaceHolderLabel); | ||||
m_PlaceBackground = new QWidget{m_PlaceHolder.get()}; | ||||
m_PlaceBackground->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); | ||||
layout->addWidget(m_PlaceBackground); | ||||
sqpApp->installEventFilter(m_DragDropScroller.get()); | ||||
r837 | ||||
r876 | m_ImageTempUrl = QDir::temp().absoluteFilePath("Sciqlop_graph.png"); | |||
r837 | } | |||
r881 | void preparePlaceHolder(DragDropHelper::PlaceHolderType type, const QString &topLabelText) const | |||
r837 | { | |||
r844 | if (m_CurrentDragWidget) { | |||
m_PlaceHolder->setMinimumSize(m_CurrentDragWidget->size()); | ||||
m_PlaceHolder->setSizePolicy(m_CurrentDragWidget->sizePolicy()); | ||||
r837 | } | |||
r844 | else { | |||
r850 | // Configuration of the placeHolder when there is no dragWidget | |||
// (for instance with a drag from a variable) | ||||
r851 | m_PlaceHolder->setMinimumSize(0, GRAPH_MINIMUM_HEIGHT); | |||
r850 | m_PlaceHolder->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); | |||
r837 | } | |||
r881 | ||||
switch (type) { | ||||
case DragDropHelper::PlaceHolderType::Graph: | ||||
m_PlaceBackground->setStyleSheet( | ||||
"background-color: #BBD5EE; border: 1px solid #2A7FD4"); | ||||
break; | ||||
case DragDropHelper::PlaceHolderType::Zone: | ||||
case DragDropHelper::PlaceHolderType::Default: | ||||
m_PlaceBackground->setStyleSheet( | ||||
"background-color: #BBD5EE; border: 2px solid #2A7FD4"); | ||||
m_PlaceHolderLabel->setStyleSheet("color: #2A7FD4"); | ||||
break; | ||||
} | ||||
m_PlaceHolderLabel->setText(topLabelText); | ||||
m_PlaceHolderLabel->setVisible(!topLabelText.isEmpty()); | ||||
r837 | } | |||
}; | ||||
r884 | DragDropHelper::DragDropHelper() : impl{spimpl::make_unique_impl<DragDropHelperPrivate>()} {} | |||
r837 | ||||
DragDropHelper::~DragDropHelper() | ||||
{ | ||||
r844 | QFile::remove(impl->m_ImageTempUrl); | |||
r837 | } | |||
r850 | void DragDropHelper::resetDragAndDrop() | |||
{ | ||||
setCurrentDragWidget(nullptr); | ||||
r873 | impl->m_HighlightedDragWidget = nullptr; | |||
r850 | } | |||
r837 | void DragDropHelper::setCurrentDragWidget(VisualizationDragWidget *dragWidget) | |||
{ | ||||
r874 | if (impl->m_CurrentDragWidget) { | |||
QObject::disconnect(impl->m_DragWidgetDestroyedConnection); | ||||
} | ||||
if (dragWidget) { | ||||
// ensures the impl->m_CurrentDragWidget is reset when the widget is destroyed | ||||
impl->m_DragWidgetDestroyedConnection | ||||
= QObject::connect(dragWidget, &VisualizationDragWidget::destroyed, | ||||
[this]() { impl->m_CurrentDragWidget = nullptr; }); | ||||
} | ||||
r844 | impl->m_CurrentDragWidget = dragWidget; | |||
r837 | } | |||
VisualizationDragWidget *DragDropHelper::getCurrentDragWidget() const | ||||
{ | ||||
r844 | return impl->m_CurrentDragWidget; | |||
r837 | } | |||
r844 | QWidget &DragDropHelper::placeHolder() const | |||
r837 | { | |||
r844 | return *impl->m_PlaceHolder; | |||
r837 | } | |||
r881 | void DragDropHelper::insertPlaceHolder(QVBoxLayout *layout, int index, PlaceHolderType type, | |||
const QString &topLabelText) | ||||
r837 | { | |||
removePlaceHolder(); | ||||
r881 | impl->preparePlaceHolder(type, topLabelText); | |||
r844 | layout->insertWidget(index, impl->m_PlaceHolder.get()); | |||
impl->m_PlaceHolder->show(); | ||||
r837 | } | |||
void DragDropHelper::removePlaceHolder() | ||||
{ | ||||
r844 | auto parentWidget = impl->m_PlaceHolder->parentWidget(); | |||
if (parentWidget) { | ||||
parentWidget->layout()->removeWidget(impl->m_PlaceHolder.get()); | ||||
impl->m_PlaceHolder->setParent(nullptr); | ||||
impl->m_PlaceHolder->hide(); | ||||
r837 | } | |||
} | ||||
bool DragDropHelper::isPlaceHolderSet() const | ||||
{ | ||||
r844 | return impl->m_PlaceHolder->parentWidget(); | |||
r837 | } | |||
r840 | void DragDropHelper::addDragDropScrollArea(QScrollArea *scrollArea) | |||
{ | ||||
r844 | impl->m_DragDropScroller->addScrollArea(scrollArea); | |||
r840 | } | |||
void DragDropHelper::removeDragDropScrollArea(QScrollArea *scrollArea) | ||||
{ | ||||
r844 | impl->m_DragDropScroller->removeScrollArea(scrollArea); | |||
r840 | } | |||
r844 | QUrl DragDropHelper::imageTemporaryUrl(const QImage &image) const | |||
r837 | { | |||
r844 | image.save(impl->m_ImageTempUrl); | |||
return QUrl::fromLocalFile(impl->m_ImageTempUrl); | ||||
r837 | } | |||
r850 | ||||
r873 | void DragDropHelper::setHightlightedDragWidget(VisualizationDragWidget *dragWidget) | |||
{ | ||||
if (impl->m_HighlightedDragWidget) { | ||||
impl->m_HighlightedDragWidget->highlightForMerge(false); | ||||
r874 | QObject::disconnect(impl->m_HighlightedWidgetDestroyedConnection); | |||
r873 | } | |||
if (dragWidget) { | ||||
r874 | dragWidget->highlightForMerge(true); | |||
// ensures the impl->m_HighlightedDragWidget is reset when the widget is destroyed | ||||
impl->m_DragWidgetDestroyedConnection | ||||
= QObject::connect(dragWidget, &VisualizationDragWidget::destroyed, | ||||
[this]() { impl->m_HighlightedDragWidget = nullptr; }); | ||||
r873 | } | |||
r874 | ||||
impl->m_HighlightedDragWidget = dragWidget; | ||||
r873 | } | |||
r875 | VisualizationDragWidget *DragDropHelper::getHightlightedDragWidget() const | |||
{ | ||||
return impl->m_HighlightedDragWidget; | ||||
} | ||||
r850 | bool DragDropHelper::checkMimeDataForVisualization(const QMimeData *mimeData, | |||
VisualizationDragDropContainer *dropContainer) | ||||
{ | ||||
r868 | if (!mimeData || !dropContainer) { | |||
qCWarning(LOG_DragDropHelper()) << QObject::tr( | ||||
"DragDropHelper::checkMimeDataForVisualization, invalid input parameters."); | ||||
Q_ASSERT(false); | ||||
r876 | return false; | |||
r868 | } | |||
r876 | auto result = false; | |||
r850 | ||||
if (mimeData->hasFormat(MIME_TYPE_VARIABLE_LIST)) { | ||||
auto variables = sqpApp->variableController().variablesForMimeData( | ||||
mimeData->data(MIME_TYPE_VARIABLE_LIST)); | ||||
if (variables.count() == 1) { | ||||
r876 | auto variable = variables.first(); | |||
if (variable->dataSeries() != nullptr) { | ||||
// Check that the variable is not already in a graph | ||||
r850 | ||||
r876 | auto parent = dropContainer->parentWidget(); | |||
while (parent && qobject_cast<VisualizationWidget *>(parent) == nullptr) { | ||||
parent = parent->parentWidget(); // Search for the top level VisualizationWidget | ||||
} | ||||
r850 | ||||
r876 | if (parent) { | |||
auto visualizationWidget = static_cast<VisualizationWidget *>(parent); | ||||
FindVariableOperation findVariableOperation{variable}; | ||||
visualizationWidget->accept(&findVariableOperation); | ||||
auto variableContainers = findVariableOperation.result(); | ||||
if (variableContainers.empty()) { | ||||
result = true; | ||||
} | ||||
else { | ||||
// result = false: the variable already exist in the visualisation | ||||
} | ||||
} | ||||
else { | ||||
qCWarning(LOG_DragDropHelper()) << QObject::tr( | ||||
"DragDropHelper::checkMimeDataForVisualization, the parent " | ||||
"VisualizationWidget cannot be found. Cannot check if the variable is " | ||||
"already used or not."); | ||||
r850 | } | |||
} | ||||
else { | ||||
r876 | // result = false: the variable is not fully loaded | |||
r850 | } | |||
} | ||||
else { | ||||
r876 | // result = false: cannot drop multiple variables in the visualisation | |||
r850 | } | |||
} | ||||
r876 | else { | |||
// Other MIME data | ||||
// no special rules, accepted by default | ||||
result = true; | ||||
} | ||||
r850 | ||||
return result; | ||||
} | ||||