DragDropHelper.cpp
294 lines
| 9.8 KiB
| text/x-c
|
CppLexer
r890 | #include "DragAndDrop/DragDropHelper.h" | |||
r891 | #include "DragAndDrop/DragDropScroller.h" | |||
r892 | #include "DragAndDrop/DragDropTabSwitcher.h" | |||
r840 | #include "SqpApplication.h" | |||
r852 | #include "Visualization/VisualizationDragDropContainer.h" | |||
r847 | #include "Visualization/VisualizationDragWidget.h" | |||
r852 | #include "Visualization/VisualizationWidget.h" | |||
#include "Visualization/operations/FindVariableOperation.h" | ||||
r882 | #include "Variable/Variable.h" | |||
r852 | #include "Variable/VariableController.h" | |||
#include "Common/MimeTypesDef.h" | ||||
#include "Common/VisualizationDef.h" | ||||
r840 | ||||
r847 | #include <QDir> | |||
r887 | #include <QLabel> | |||
r891 | #include <QUrl> | |||
r847 | #include <QVBoxLayout> | |||
r840 | ||||
r843 | ||||
r892 | Q_LOGGING_CATEGORY(LOG_DragDropHelper, "DragDropHelper") | |||
r852 | ||||
r840 | ||||
struct DragDropHelper::DragDropHelperPrivate { | ||||
r847 | VisualizationDragWidget *m_CurrentDragWidget = nullptr; | |||
std::unique_ptr<QWidget> m_PlaceHolder = nullptr; | ||||
r887 | QLabel *m_PlaceHolderLabel; | |||
QWidget *m_PlaceBackground; | ||||
r847 | std::unique_ptr<DragDropScroller> m_DragDropScroller = nullptr; | |||
r892 | std::unique_ptr<DragDropTabSwitcher> m_DragDropTabSwitcher = nullptr; | |||
r847 | QString m_ImageTempUrl; // Temporary file for image url generated by the drag & drop. Not using | |||
// QTemporaryFile to have a name which is not generated. | ||||
r840 | ||||
r879 | VisualizationDragWidget *m_HighlightedDragWidget = nullptr; | |||
r880 | QMetaObject::Connection m_DragWidgetDestroyedConnection; | |||
QMetaObject::Connection m_HighlightedWidgetDestroyedConnection; | ||||
Thibaud Rabillard
|
r912 | QList<QWidget *> m_WidgetToClose; | ||
r840 | explicit DragDropHelperPrivate() | |||
r847 | : m_PlaceHolder{std::make_unique<QWidget>()}, | |||
r892 | m_DragDropScroller{std::make_unique<DragDropScroller>()}, | |||
m_DragDropTabSwitcher{std::make_unique<DragDropTabSwitcher>()} | ||||
r840 | { | |||
r887 | 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()); | ||||
r892 | sqpApp->installEventFilter(m_DragDropTabSwitcher.get()); | |||
r840 | ||||
r882 | m_ImageTempUrl = QDir::temp().absoluteFilePath("Sciqlop_graph.png"); | |||
r840 | } | |||
r887 | void preparePlaceHolder(DragDropHelper::PlaceHolderType type, const QString &topLabelText) const | |||
r840 | { | |||
r847 | if (m_CurrentDragWidget) { | |||
m_PlaceHolder->setMinimumSize(m_CurrentDragWidget->size()); | ||||
m_PlaceHolder->setSizePolicy(m_CurrentDragWidget->sizePolicy()); | ||||
r840 | } | |||
r847 | else { | |||
r852 | // Configuration of the placeHolder when there is no dragWidget | |||
// (for instance with a drag from a variable) | ||||
r853 | m_PlaceHolder->setMinimumSize(0, GRAPH_MINIMUM_HEIGHT); | |||
r852 | m_PlaceHolder->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); | |||
r840 | } | |||
r887 | ||||
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()); | ||||
r840 | } | |||
}; | ||||
r913 | DragDropHelper::DragDropHelper() : impl{spimpl::make_unique_impl<DragDropHelperPrivate>()} | |||
{ | ||||
} | ||||
r840 | ||||
DragDropHelper::~DragDropHelper() | ||||
{ | ||||
r847 | QFile::remove(impl->m_ImageTempUrl); | |||
r840 | } | |||
r852 | void DragDropHelper::resetDragAndDrop() | |||
{ | ||||
setCurrentDragWidget(nullptr); | ||||
r879 | impl->m_HighlightedDragWidget = nullptr; | |||
r852 | } | |||
r840 | void DragDropHelper::setCurrentDragWidget(VisualizationDragWidget *dragWidget) | |||
{ | ||||
r880 | 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; }); | ||||
} | ||||
r847 | impl->m_CurrentDragWidget = dragWidget; | |||
r840 | } | |||
VisualizationDragWidget *DragDropHelper::getCurrentDragWidget() const | ||||
{ | ||||
r847 | return impl->m_CurrentDragWidget; | |||
r840 | } | |||
r847 | QWidget &DragDropHelper::placeHolder() const | |||
r840 | { | |||
r847 | return *impl->m_PlaceHolder; | |||
r840 | } | |||
r887 | void DragDropHelper::insertPlaceHolder(QVBoxLayout *layout, int index, PlaceHolderType type, | |||
const QString &topLabelText) | ||||
r840 | { | |||
removePlaceHolder(); | ||||
r887 | impl->preparePlaceHolder(type, topLabelText); | |||
r847 | layout->insertWidget(index, impl->m_PlaceHolder.get()); | |||
impl->m_PlaceHolder->show(); | ||||
r840 | } | |||
void DragDropHelper::removePlaceHolder() | ||||
{ | ||||
r847 | auto parentWidget = impl->m_PlaceHolder->parentWidget(); | |||
if (parentWidget) { | ||||
parentWidget->layout()->removeWidget(impl->m_PlaceHolder.get()); | ||||
impl->m_PlaceHolder->setParent(nullptr); | ||||
impl->m_PlaceHolder->hide(); | ||||
r840 | } | |||
} | ||||
bool DragDropHelper::isPlaceHolderSet() const | ||||
{ | ||||
r847 | return impl->m_PlaceHolder->parentWidget(); | |||
r840 | } | |||
r843 | void DragDropHelper::addDragDropScrollArea(QScrollArea *scrollArea) | |||
{ | ||||
r847 | impl->m_DragDropScroller->addScrollArea(scrollArea); | |||
r843 | } | |||
void DragDropHelper::removeDragDropScrollArea(QScrollArea *scrollArea) | ||||
{ | ||||
r847 | impl->m_DragDropScroller->removeScrollArea(scrollArea); | |||
r843 | } | |||
r892 | void DragDropHelper::addDragDropTabBar(QTabBar *tabBar) | |||
{ | ||||
impl->m_DragDropTabSwitcher->addTabBar(tabBar); | ||||
} | ||||
void DragDropHelper::removeDragDropTabBar(QTabBar *tabBar) | ||||
{ | ||||
impl->m_DragDropTabSwitcher->removeTabBar(tabBar); | ||||
} | ||||
r847 | QUrl DragDropHelper::imageTemporaryUrl(const QImage &image) const | |||
r840 | { | |||
r847 | image.save(impl->m_ImageTempUrl); | |||
return QUrl::fromLocalFile(impl->m_ImageTempUrl); | ||||
r840 | } | |||
r852 | ||||
r879 | void DragDropHelper::setHightlightedDragWidget(VisualizationDragWidget *dragWidget) | |||
{ | ||||
if (impl->m_HighlightedDragWidget) { | ||||
impl->m_HighlightedDragWidget->highlightForMerge(false); | ||||
r880 | QObject::disconnect(impl->m_HighlightedWidgetDestroyedConnection); | |||
r879 | } | |||
if (dragWidget) { | ||||
r880 | 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; }); | ||||
r879 | } | |||
r880 | ||||
impl->m_HighlightedDragWidget = dragWidget; | ||||
r879 | } | |||
r881 | VisualizationDragWidget *DragDropHelper::getHightlightedDragWidget() const | |||
{ | ||||
return impl->m_HighlightedDragWidget; | ||||
} | ||||
Thibaud Rabillard
|
r912 | void DragDropHelper::delayedCloseWidget(QWidget *widget) | ||
{ | ||||
widget->hide(); | ||||
impl->m_WidgetToClose << widget; | ||||
} | ||||
void DragDropHelper::doCloseWidgets() | ||||
{ | ||||
for (auto widget : impl->m_WidgetToClose) { | ||||
widget->close(); | ||||
} | ||||
impl->m_WidgetToClose.clear(); | ||||
} | ||||
r852 | bool DragDropHelper::checkMimeDataForVisualization(const QMimeData *mimeData, | |||
VisualizationDragDropContainer *dropContainer) | ||||
{ | ||||
r876 | if (!mimeData || !dropContainer) { | |||
qCWarning(LOG_DragDropHelper()) << QObject::tr( | ||||
"DragDropHelper::checkMimeDataForVisualization, invalid input parameters."); | ||||
Q_ASSERT(false); | ||||
r882 | return false; | |||
r876 | } | |||
r882 | auto result = false; | |||
r852 | ||||
if (mimeData->hasFormat(MIME_TYPE_VARIABLE_LIST)) { | ||||
auto variables = sqpApp->variableController().variablesForMimeData( | ||||
mimeData->data(MIME_TYPE_VARIABLE_LIST)); | ||||
if (variables.count() == 1) { | ||||
r882 | auto variable = variables.first(); | |||
if (variable->dataSeries() != nullptr) { | ||||
// Check that the variable is not already in a graph | ||||
r852 | ||||
r882 | auto parent = dropContainer->parentWidget(); | |||
while (parent && qobject_cast<VisualizationWidget *>(parent) == nullptr) { | ||||
parent = parent->parentWidget(); // Search for the top level VisualizationWidget | ||||
} | ||||
r852 | ||||
r882 | 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."); | ||||
r852 | } | |||
} | ||||
else { | ||||
r882 | // result = false: the variable is not fully loaded | |||
r852 | } | |||
} | ||||
else { | ||||
r882 | // result = false: cannot drop multiple variables in the visualisation | |||
r852 | } | |||
} | ||||
r882 | else { | |||
// Other MIME data | ||||
// no special rules, accepted by default | ||||
result = true; | ||||
} | ||||
r852 | ||||
return result; | ||||
} | ||||