From deba06e71141520e872cf4b9809cab64c2fa37c9 2017-10-25 07:11:22 From: Thibaud Rabillard Date: 2017-10-25 07:11:22 Subject: [PATCH] New helper class for the drag&drop --- diff --git a/gui/include/DragDropHelper.h b/gui/include/DragDropHelper.h new file mode 100644 index 0000000..d103039 --- /dev/null +++ b/gui/include/DragDropHelper.h @@ -0,0 +1,42 @@ +#ifndef DRAGDROPHELPER_H +#define DRAGDROPHELPER_H + +#include +#include + +class QVBoxLayout; +class QScrollArea; +class VisualizationDragWidget; +class QMimeData; + +/** + * @brief Helper class for drag&drop operations. + */ +class DragDropHelper +{ +public: + DragDropHelper(); + ~DragDropHelper(); + + static const QString MIME_TYPE_GRAPH; + static const QString MIME_TYPE_ZONE; + + void setCurrentDragWidget(VisualizationDragWidget* dragWidget); + VisualizationDragWidget* getCurrentDragWidget() const; + + QWidget &placeHolder() const; + void insertPlaceHolder(QVBoxLayout* layout, int index); + void removePlaceHolder(); + bool isPlaceHolderSet() const; + + void addDragDropScrollArea(QScrollArea* scrollArea); + void removeDragDropScrollArea(QScrollArea* scrollArea); + + QUrl imageTemporaryUrl(const QImage& image) const; + +private: + class DragDropHelperPrivate; + spimpl::unique_impl_ptr impl; +}; + +#endif // DRAGDROPHELPER_H diff --git a/gui/include/SqpApplication.h b/gui/include/SqpApplication.h index a23bd7f..d7049ba 100644 --- a/gui/include/SqpApplication.h +++ b/gui/include/SqpApplication.h @@ -20,6 +20,7 @@ class NetworkController; class TimeController; class VariableController; class VisualizationController; +class DragDropHelper; /** * @brief The SqpApplication class aims to make the link between SciQlop @@ -44,6 +45,9 @@ public: VariableController &variableController() noexcept; VisualizationController &visualizationController() noexcept; + /// Accessors for the differents sciqlop helpers + DragDropHelper &dragDropHelper() noexcept; + private: class SqpApplicationPrivate; spimpl::unique_impl_ptr impl; diff --git a/gui/src/DragDropHelper.cpp b/gui/src/DragDropHelper.cpp new file mode 100644 index 0000000..9f7cc58 --- /dev/null +++ b/gui/src/DragDropHelper.cpp @@ -0,0 +1,105 @@ +#include "DragDropHelper.h" +#include "Visualization/VisualizationDragWidget.h" +#include "SqpApplication.h" + +#include +#include +#include +#include +#include +#include +#include + +const QString DragDropHelper::MIME_TYPE_GRAPH = "scqlop/graph"; +const QString DragDropHelper::MIME_TYPE_ZONE = "scqlop/zone"; + + +struct DragDropHelper::DragDropHelperPrivate { + + VisualizationDragWidget* m_currentDragWidget = nullptr; + std::unique_ptr m_placeHolder = nullptr; + std::unique_ptr 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. + + explicit DragDropHelperPrivate() + : m_placeHolder{std::make_unique()}, + m_dragDropScroller{std::make_unique()} + { + m_placeHolder->setStyleSheet("background-color: #BBD5EE; border:2px solid #2A7FD4"); + sqpApp->installEventFilter(m_dragDropScroller.get()); + + + m_imageTempUrl = QDir::temp().absoluteFilePath("Scqlop_graph.png"); + } + + void preparePlaceHolder() const + { + if (m_currentDragWidget) + { + m_placeHolder->setMinimumSize(m_currentDragWidget->size()); + m_placeHolder->setSizePolicy(m_currentDragWidget->sizePolicy()); + } + else + { + m_placeHolder->setMinimumSize(200, 200); + } + } +}; + + +DragDropHelper::DragDropHelper() : + impl{spimpl::make_unique_impl()} +{ +} + +DragDropHelper::~DragDropHelper() +{ + QFile::remove(impl->m_imageTempUrl); +} + +void DragDropHelper::setCurrentDragWidget(VisualizationDragWidget *dragWidget) +{ + impl->m_currentDragWidget = dragWidget; +} + +VisualizationDragWidget *DragDropHelper::getCurrentDragWidget() const +{ + return impl->m_currentDragWidget; +} + + +QWidget& DragDropHelper::placeHolder() const +{ + return *impl->m_placeHolder; +} + +void DragDropHelper::insertPlaceHolder(QVBoxLayout *layout, int index) +{ + removePlaceHolder(); + impl->preparePlaceHolder(); + layout->insertWidget(index, impl->m_placeHolder.get()); + impl->m_placeHolder->show(); +} + +void DragDropHelper::removePlaceHolder() +{ + auto parentWidget = impl->m_placeHolder->parentWidget(); + if (parentWidget) + { + parentWidget->layout()->removeWidget(impl->m_placeHolder.get()); + impl->m_placeHolder->setParent(nullptr); + impl->m_placeHolder->hide(); + } +} + +bool DragDropHelper::isPlaceHolderSet() const +{ + return impl->m_placeHolder->parentWidget(); +} + +QUrl DragDropHelper::imageTemporaryUrl(const QImage& image) const +{ + image.save(impl->m_imageTempUrl); + return QUrl::fromLocalFile(impl->m_imageTempUrl); +} + diff --git a/gui/src/SqpApplication.cpp b/gui/src/SqpApplication.cpp index 4968eb0..1187cc9 100644 --- a/gui/src/SqpApplication.cpp +++ b/gui/src/SqpApplication.cpp @@ -8,6 +8,7 @@ #include #include #include +#include Q_LOGGING_CATEGORY(LOG_SqpApplication, "SqpApplication") @@ -18,7 +19,8 @@ public: m_NetworkController{std::make_unique()}, m_TimeController{std::make_unique()}, m_VariableController{std::make_unique()}, - m_VisualizationController{std::make_unique()} + m_VisualizationController{std::make_unique()}, + m_DragDropHelper{std::make_unique()} { // /////////////////////////////// // // Connections between controllers // @@ -82,6 +84,8 @@ public: QThread m_NetworkControllerThread; QThread m_VariableControllerThread; QThread m_VisualizationControllerThread; + + std::unique_ptr m_DragDropHelper; }; @@ -148,3 +152,8 @@ VisualizationController &SqpApplication::visualizationController() noexcept { return *impl->m_VisualizationController; } + +DragDropHelper &SqpApplication::dragDropHelper() noexcept +{ + return *impl->m_DragDropHelper; +}