##// END OF EJS Templates
New helper class for the drag&drop
trabillard -
r837:deba06e71141
parent child
Show More
@@ -0,0 +1,42
1 #ifndef DRAGDROPHELPER_H
2 #define DRAGDROPHELPER_H
3
4 #include <Common/spimpl.h>
5 #include <QWidget>
6
7 class QVBoxLayout;
8 class QScrollArea;
9 class VisualizationDragWidget;
10 class QMimeData;
11
12 /**
13 * @brief Helper class for drag&drop operations.
14 */
15 class DragDropHelper
16 {
17 public:
18 DragDropHelper();
19 ~DragDropHelper();
20
21 static const QString MIME_TYPE_GRAPH;
22 static const QString MIME_TYPE_ZONE;
23
24 void setCurrentDragWidget(VisualizationDragWidget* dragWidget);
25 VisualizationDragWidget* getCurrentDragWidget() const;
26
27 QWidget &placeHolder() const;
28 void insertPlaceHolder(QVBoxLayout* layout, int index);
29 void removePlaceHolder();
30 bool isPlaceHolderSet() const;
31
32 void addDragDropScrollArea(QScrollArea* scrollArea);
33 void removeDragDropScrollArea(QScrollArea* scrollArea);
34
35 QUrl imageTemporaryUrl(const QImage& image) const;
36
37 private:
38 class DragDropHelperPrivate;
39 spimpl::unique_impl_ptr<DragDropHelperPrivate> impl;
40 };
41
42 #endif // DRAGDROPHELPER_H
@@ -0,0 +1,105
1 #include "DragDropHelper.h"
2 #include "Visualization/VisualizationDragWidget.h"
3 #include "SqpApplication.h"
4
5 #include <QDragMoveEvent>
6 #include <QDragEnterEvent>
7 #include <QScrollBar>
8 #include <QScrollArea>
9 #include <QVBoxLayout>
10 #include <QTimer>
11 #include <QDir>
12
13 const QString DragDropHelper::MIME_TYPE_GRAPH = "scqlop/graph";
14 const QString DragDropHelper::MIME_TYPE_ZONE = "scqlop/zone";
15
16
17 struct DragDropHelper::DragDropHelperPrivate {
18
19 VisualizationDragWidget* m_currentDragWidget = nullptr;
20 std::unique_ptr<QWidget> m_placeHolder = nullptr;
21 std::unique_ptr<DragDropScroller> m_dragDropScroller = nullptr;
22 QString m_imageTempUrl; //Temporary file for image url generated by the drag & drop. Not using QTemporaryFile to have a name which is not generated.
23
24 explicit DragDropHelperPrivate()
25 : m_placeHolder{std::make_unique<QWidget>()},
26 m_dragDropScroller{std::make_unique<DragDropScroller>()}
27 {
28 m_placeHolder->setStyleSheet("background-color: #BBD5EE; border:2px solid #2A7FD4");
29 sqpApp->installEventFilter(m_dragDropScroller.get());
30
31
32 m_imageTempUrl = QDir::temp().absoluteFilePath("Scqlop_graph.png");
33 }
34
35 void preparePlaceHolder() const
36 {
37 if (m_currentDragWidget)
38 {
39 m_placeHolder->setMinimumSize(m_currentDragWidget->size());
40 m_placeHolder->setSizePolicy(m_currentDragWidget->sizePolicy());
41 }
42 else
43 {
44 m_placeHolder->setMinimumSize(200, 200);
45 }
46 }
47 };
48
49
50 DragDropHelper::DragDropHelper() :
51 impl{spimpl::make_unique_impl<DragDropHelperPrivate>()}
52 {
53 }
54
55 DragDropHelper::~DragDropHelper()
56 {
57 QFile::remove(impl->m_imageTempUrl);
58 }
59
60 void DragDropHelper::setCurrentDragWidget(VisualizationDragWidget *dragWidget)
61 {
62 impl->m_currentDragWidget = dragWidget;
63 }
64
65 VisualizationDragWidget *DragDropHelper::getCurrentDragWidget() const
66 {
67 return impl->m_currentDragWidget;
68 }
69
70
71 QWidget& DragDropHelper::placeHolder() const
72 {
73 return *impl->m_placeHolder;
74 }
75
76 void DragDropHelper::insertPlaceHolder(QVBoxLayout *layout, int index)
77 {
78 removePlaceHolder();
79 impl->preparePlaceHolder();
80 layout->insertWidget(index, impl->m_placeHolder.get());
81 impl->m_placeHolder->show();
82 }
83
84 void DragDropHelper::removePlaceHolder()
85 {
86 auto parentWidget = impl->m_placeHolder->parentWidget();
87 if (parentWidget)
88 {
89 parentWidget->layout()->removeWidget(impl->m_placeHolder.get());
90 impl->m_placeHolder->setParent(nullptr);
91 impl->m_placeHolder->hide();
92 }
93 }
94
95 bool DragDropHelper::isPlaceHolderSet() const
96 {
97 return impl->m_placeHolder->parentWidget();
98 }
99
100 QUrl DragDropHelper::imageTemporaryUrl(const QImage& image) const
101 {
102 image.save(impl->m_imageTempUrl);
103 return QUrl::fromLocalFile(impl->m_imageTempUrl);
104 }
105
@@ -20,6 +20,7 class NetworkController;
20 class TimeController;
20 class TimeController;
21 class VariableController;
21 class VariableController;
22 class VisualizationController;
22 class VisualizationController;
23 class DragDropHelper;
23
24
24 /**
25 /**
25 * @brief The SqpApplication class aims to make the link between SciQlop
26 * @brief The SqpApplication class aims to make the link between SciQlop
@@ -44,6 +45,9 public:
44 VariableController &variableController() noexcept;
45 VariableController &variableController() noexcept;
45 VisualizationController &visualizationController() noexcept;
46 VisualizationController &visualizationController() noexcept;
46
47
48 /// Accessors for the differents sciqlop helpers
49 DragDropHelper &dragDropHelper() noexcept;
50
47 private:
51 private:
48 class SqpApplicationPrivate;
52 class SqpApplicationPrivate;
49 spimpl::unique_impl_ptr<SqpApplicationPrivate> impl;
53 spimpl::unique_impl_ptr<SqpApplicationPrivate> impl;
@@ -8,6 +8,7
8 #include <Variable/Variable.h>
8 #include <Variable/Variable.h>
9 #include <Variable/VariableController.h>
9 #include <Variable/VariableController.h>
10 #include <Visualization/VisualizationController.h>
10 #include <Visualization/VisualizationController.h>
11 #include <DragDropHelper.h>
11
12
12 Q_LOGGING_CATEGORY(LOG_SqpApplication, "SqpApplication")
13 Q_LOGGING_CATEGORY(LOG_SqpApplication, "SqpApplication")
13
14
@@ -18,7 +19,8 public:
18 m_NetworkController{std::make_unique<NetworkController>()},
19 m_NetworkController{std::make_unique<NetworkController>()},
19 m_TimeController{std::make_unique<TimeController>()},
20 m_TimeController{std::make_unique<TimeController>()},
20 m_VariableController{std::make_unique<VariableController>()},
21 m_VariableController{std::make_unique<VariableController>()},
21 m_VisualizationController{std::make_unique<VisualizationController>()}
22 m_VisualizationController{std::make_unique<VisualizationController>()},
23 m_DragDropHelper{std::make_unique<DragDropHelper>()}
22 {
24 {
23 // /////////////////////////////// //
25 // /////////////////////////////// //
24 // Connections between controllers //
26 // Connections between controllers //
@@ -82,6 +84,8 public:
82 QThread m_NetworkControllerThread;
84 QThread m_NetworkControllerThread;
83 QThread m_VariableControllerThread;
85 QThread m_VariableControllerThread;
84 QThread m_VisualizationControllerThread;
86 QThread m_VisualizationControllerThread;
87
88 std::unique_ptr<DragDropHelper> m_DragDropHelper;
85 };
89 };
86
90
87
91
@@ -148,3 +152,8 VisualizationController &SqpApplication::visualizationController() noexcept
148 {
152 {
149 return *impl->m_VisualizationController;
153 return *impl->m_VisualizationController;
150 }
154 }
155
156 DragDropHelper &SqpApplication::dragDropHelper() noexcept
157 {
158 return *impl->m_DragDropHelper;
159 }
General Comments 0
You need to be logged in to leave comments. Login now