##// END OF EJS Templates
Merge branch 'feature/DragAndDrop' into develop
trabillard -
r888:7e81b9ccb6b1 merge
parent child
Show More
@@ -0,0 +1,14
1 #ifndef SCIQLOP_VARIABLEINSPECTORTABLEVIEW_H
2 #define SCIQLOP_VARIABLEINSPECTORTABLEVIEW_H
3
4 #include <QTableView>
5
6 class VariableInspectorTableView : public QTableView {
7 public:
8 VariableInspectorTableView(QWidget *parent = nullptr);
9
10 protected:
11 void startDrag(Qt::DropActions supportedActions);
12 };
13
14 #endif // SCIQLOP_VARIABLEINSPECTORTABLEVIEW_H
@@ -0,0 +1,13
1 #include "Variable/VariableInspectorTableView.h"
2
3 #include "DragDropHelper.h"
4 #include "SqpApplication.h"
5
6 VariableInspectorTableView::VariableInspectorTableView(QWidget *parent) : QTableView(parent) {}
7
8 void VariableInspectorTableView::startDrag(Qt::DropActions supportedActions)
9 {
10 // Resets the drag&drop operations before it's starting
11 sqpApp->dragDropHelper().resetDragAndDrop();
12 QTableView::startDrag(supportedActions);
13 }
@@ -64,8 +64,11 public:
64 64 */
65 65 void loadProductItem(const QUuid &dataSourceUid, const DataSourceItem &productItem) noexcept;
66 66
67 QByteArray mimeDataForProductsData(const QVariantList &productsData) const;
68 QVariantList productsDataForMimeData(const QByteArray &mimeData) const;
67 /// Returns the MIME data associated to a list of product meta data
68 static QByteArray mimeDataForProductsData(const QVariantList &productsData);
69
70 /// Returns the list of meta data contained in a MIME data
71 static QVariantList productsDataForMimeData(const QByteArray &mimeData);
69 72
70 73 public slots:
71 74 /// Manage init/end of the controller
@@ -23,6 +23,12 public:
23 23
24 24 SqpRange dateTime() const noexcept;
25 25
26 /// Returns the MIME data associated to a time range
27 static QByteArray mimeDataForTimeRange(const SqpRange &timeRange);
28
29 /// Returns the time range contained in a MIME data
30 static SqpRange timeRangeForMimeData(const QByteArray &mimeData);
31
26 32 signals:
27 33 /// Signal emitted to notify that time parameters has beed updated
28 34 void timeUpdated(SqpRange time);
@@ -68,7 +68,10 public:
68 68 */
69 69 void deleteVariables(const QVector<std::shared_ptr<Variable> > &variables) noexcept;
70 70
71 /// Returns the MIME data associated to a list of variables
71 72 QByteArray mimeDataForVariables(const QList<std::shared_ptr<Variable> > &variables) const;
73
74 /// Returns the list of variables contained in a MIME data
72 75 QList<std::shared_ptr<Variable> > variablesForMimeData(const QByteArray &mimeData) const;
73 76
74 77 static AcquisitionZoomType getZoomType(const SqpRange &range, const SqpRange &oldRange);
@@ -139,7 +139,7 void DataSourceController::loadProductItem(const QUuid &dataSourceUid,
139 139 }
140 140 }
141 141
142 QByteArray DataSourceController::mimeDataForProductsData(const QVariantList &productsData) const
142 QByteArray DataSourceController::mimeDataForProductsData(const QVariantList &productsData)
143 143 {
144 144 QByteArray encodedData;
145 145 QDataStream stream{&encodedData, QIODevice::WriteOnly};
@@ -149,7 +149,7 QByteArray DataSourceController::mimeDataForProductsData(const QVariantList &pro
149 149 return encodedData;
150 150 }
151 151
152 QVariantList DataSourceController::productsDataForMimeData(const QByteArray &mimeData) const
152 QVariantList DataSourceController::productsDataForMimeData(const QByteArray &mimeData)
153 153 {
154 154 QDataStream stream{mimeData};
155 155
@@ -174,7 +174,7 void DataSourceController::finalize()
174 174
175 175 void DataSourceController::requestVariable(const QVariantHash &productData)
176 176 {
177 DataSourceItem *sourceItem = impl->findDataSourceItem(productData);
177 auto sourceItem = impl->findDataSourceItem(productData);
178 178
179 179 if (sourceItem) {
180 180 auto sourceName = sourceItem->rootItem().name();
@@ -131,8 +131,7 DataSourceItem *DataSourceItem::findItem(const QVariantHash &data, bool recursiv
131 131 }
132 132
133 133 if (recursive) {
134 auto foundItem = child->findItem(data, true);
135 if (foundItem) {
134 if (auto foundItem = child->findItem(data, true)) {
136 135 return foundItem;
137 136 }
138 137 }
@@ -1,5 +1,7
1 1 #include "Time/TimeController.h"
2 2
3 #include <QDataStream>
4
3 5 Q_LOGGING_CATEGORY(LOG_TimeController, "TimeController")
4 6
5 7 struct TimeController::TimeControllerPrivate {
@@ -18,6 +20,26 SqpRange TimeController::dateTime() const noexcept
18 20 return impl->m_DateTime;
19 21 }
20 22
23 QByteArray TimeController::mimeDataForTimeRange(const SqpRange &timeRange)
24 {
25 QByteArray encodedData;
26 QDataStream stream{&encodedData, QIODevice::WriteOnly};
27
28 stream << timeRange.m_TStart << timeRange.m_TEnd;
29
30 return encodedData;
31 }
32
33 SqpRange TimeController::timeRangeForMimeData(const QByteArray &mimeData)
34 {
35 QDataStream stream{mimeData};
36
37 SqpRange timeRange;
38 stream >> timeRange.m_TStart >> timeRange.m_TEnd;
39
40 return timeRange;
41 }
42
21 43 void TimeController::onTimeToUpdate(SqpRange dateTime)
22 44 {
23 45 impl->m_DateTime = dateTime;
@@ -8,7 +8,9
8 8
9 9 #include <Data/IDataSeries.h>
10 10
11 #include <QDataStream>
11 #include <DataSource/DataSourceController.h>
12 #include <Time/TimeController.h>
13
12 14 #include <QMimeData>
13 15 #include <QSize>
14 16 #include <unordered_map>
@@ -278,7 +280,7 Qt::DropActions VariableModel::supportedDragActions() const
278 280
279 281 QStringList VariableModel::mimeTypes() const
280 282 {
281 return {MIME_TYPE_VARIABLE_LIST};
283 return {MIME_TYPE_VARIABLE_LIST, MIME_TYPE_TIME_RANGE};
282 284 }
283 285
284 286 QMimeData *VariableModel::mimeData(const QModelIndexList &indexes) const
@@ -287,17 +289,31 QMimeData *VariableModel::mimeData(const QModelIndexList &indexes) const
287 289
288 290 QList<std::shared_ptr<Variable> > variableList;
289 291
292
293 SqpRange firstTimeRange;
290 294 for (const auto &index : indexes) {
291 295 if (index.column() == 0) { // only the first column
292 296 auto variable = impl->m_Variables.at(index.row());
293 297 if (variable.get() && index.isValid()) {
298
299 if (variableList.isEmpty()) {
300 // Gets the range of the first variable
301 firstTimeRange = std::move(variable->range());
302 }
303
294 304 variableList << variable;
295 305 }
296 306 }
297 307 }
298 308
299 auto encodedData = impl->m_VariableController->mimeDataForVariables(variableList);
300 mimeData->setData(MIME_TYPE_VARIABLE_LIST, encodedData);
309 auto variablesEncodedData = impl->m_VariableController->mimeDataForVariables(variableList);
310 mimeData->setData(MIME_TYPE_VARIABLE_LIST, variablesEncodedData);
311
312 if (variableList.count() == 1) {
313 // No time range MIME data if multiple variables are dragged
314 auto timeEncodedData = TimeController::mimeDataForTimeRange(firstTimeRange);
315 mimeData->setData(MIME_TYPE_TIME_RANGE, timeEncodedData);
316 }
301 317
302 318 return mimeData;
303 319 }
@@ -312,13 +328,12 bool VariableModel::canDropMimeData(const QMimeData *data, Qt::DropAction action
312 328 bool VariableModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column,
313 329 const QModelIndex &parent)
314 330 {
315 bool dropDone = false;
331 auto dropDone = false;
316 332
317 333 if (data->hasFormat(MIME_TYPE_PRODUCT_LIST)) {
318 QDataStream stream(data->data(MIME_TYPE_PRODUCT_LIST));
319 334
320 QVariantList productList;
321 stream >> productList;
335 auto productList
336 = DataSourceController::productsDataForMimeData(data->data(MIME_TYPE_PRODUCT_LIST));
322 337
323 338 for (auto metaData : productList) {
324 339 emit requestVariable(metaData.toHash());
@@ -9,6 +9,7 public:
9 9
10 10 protected:
11 11 QMimeData *mimeData(const QList<QTreeWidgetItem *> items) const override;
12 void startDrag(Qt::DropActions supportedActions) override;
12 13 };
13 14
14 15 #endif // SCIQLOP_DATASOURCETREEWIDGET_H
@@ -25,6 +25,8 public:
25 25 static const QString MIME_TYPE_GRAPH;
26 26 static const QString MIME_TYPE_ZONE;
27 27
28 enum class PlaceHolderType { Default, Graph, Zone };
29
28 30 DragDropHelper();
29 31 virtual ~DragDropHelper();
30 32
@@ -40,7 +42,8 public:
40 42 VisualizationDragWidget *getCurrentDragWidget() const;
41 43
42 44 QWidget &placeHolder() const;
43 void insertPlaceHolder(QVBoxLayout *layout, int index);
45 void insertPlaceHolder(QVBoxLayout *layout, int index, PlaceHolderType type,
46 const QString &topLabelText);
44 47 void removePlaceHolder();
45 48 bool isPlaceHolderSet() const;
46 49
@@ -53,6 +56,9 public:
53 56
54 57 QUrl imageTemporaryUrl(const QImage &image) const;
55 58
59 void setHightlightedDragWidget(VisualizationDragWidget *dragWidget);
60 VisualizationDragWidget *getHightlightedDragWidget() const;
61
56 62 private:
57 63 class DragDropHelperPrivate;
58 64 spimpl::unique_impl_ptr<DragDropHelperPrivate> impl;
@@ -5,6 +5,8
5 5
6 6 #include <Data/SqpRange.h>
7 7
8 #include <Common/spimpl.h>
9
8 10 namespace Ui {
9 11 class TimeWidget;
10 12 } // Ui
@@ -16,6 +18,9 public:
16 18 explicit TimeWidget(QWidget *parent = 0);
17 19 virtual ~TimeWidget();
18 20
21 void setTimeRange(SqpRange time);
22 SqpRange timeRange() const;
23
19 24 signals:
20 25 /// Signal emitted when the time parameters has beed updated
21 26 void timeUpdated(SqpRange time);
@@ -24,9 +29,19 public slots:
24 29 /// slot called when time parameters update has ben requested
25 30 void onTimeUpdateRequested();
26 31
32 protected:
33 void dragEnterEvent(QDragEnterEvent *event) override;
34 void dragLeaveEvent(QDragLeaveEvent *event) override;
35 void dropEvent(QDropEvent *event) override;
36
37 void mousePressEvent(QMouseEvent *event) override;
38 void mouseMoveEvent(QMouseEvent *event) override;
27 39
28 40 private:
29 41 Ui::TimeWidget *ui;
42
43 class TimeWidgetPrivate;
44 spimpl::unique_impl_ptr<TimeWidgetPrivate> impl;
30 45 };
31 46
32 47 #endif // SCIQLOP_ SQPSIDEPANE_H
@@ -2,24 +2,28
2 2 #define SCIQLOP_VISUALIZATIONDRAGDROPCONTAINER_H
3 3
4 4 #include <Common/spimpl.h>
5 #include <QFrame>
5 6 #include <QLoggingCategory>
6 7 #include <QMimeData>
7 8 #include <QVBoxLayout>
8 #include <QWidget>
9 9
10 10 #include <functional>
11 11
12 #include <DragDropHelper.h>
13
12 14 Q_DECLARE_LOGGING_CATEGORY(LOG_VisualizationDragDropContainer)
13 15
14 16 class VisualizationDragWidget;
15 17
16 class VisualizationDragDropContainer : public QWidget {
18 class VisualizationDragDropContainer : public QFrame {
17 19 Q_OBJECT
18 20
19 21 signals:
20 void dropOccured(int dropIndex, const QMimeData *mimeData);
22 void dropOccuredInContainer(int dropIndex, const QMimeData *mimeData);
23 void dropOccuredOnWidget(VisualizationDragWidget *dragWidget, const QMimeData *mimeData);
21 24
22 25 public:
26 enum class DropBehavior { Inserted, Merged, InsertedAndMerged };
23 27 using AcceptMimeDataFunction = std::function<bool(const QMimeData *mimeData)>;
24 28
25 29 VisualizationDragDropContainer(QWidget *parent = nullptr);
@@ -27,13 +31,15 public:
27 31 void addDragWidget(VisualizationDragWidget *dragWidget);
28 32 void insertDragWidget(int index, VisualizationDragWidget *dragWidget);
29 33
30 void setAcceptedMimeTypes(const QStringList &mimeTypes);
31 void setMergeAllowedMimeTypes(const QStringList &mimeTypes);
34 void addAcceptedMimeType(const QString &mimeType, DropBehavior behavior);
32 35
33 36 int countDragWidget() const;
34 37
35 38 void setAcceptMimeDataFunction(AcceptMimeDataFunction fun);
36 39
40 void setPlaceHolderType(DragDropHelper::PlaceHolderType type,
41 const QString &placeHolderText = QString());
42
37 43 protected:
38 44 void dragEnterEvent(QDragEnterEvent *event);
39 45 void dragLeaveEvent(QDragLeaveEvent *event);
@@ -13,6 +13,7 public:
13 13
14 14 virtual QMimeData *mimeData() const = 0;
15 15 virtual bool isDragAllowed() const = 0;
16 virtual void highlightForMerge(bool highlighted) { Q_UNUSED(highlighted); };
16 17
17 18 protected:
18 19 virtual void mousePressEvent(QMouseEvent *event) override;
@@ -59,6 +59,7 public:
59 59 // VisualisationDragWidget
60 60 QMimeData *mimeData() const override;
61 61 bool isDragAllowed() const override;
62 void highlightForMerge(bool highlighted) override;
62 63
63 64 signals:
64 65 void synchronize(const SqpRange &range, const SqpRange &oldRange);
@@ -85,6 +85,7 private slots:
85 85 void onVariableAboutToBeRemoved(std::shared_ptr<Variable> variable);
86 86
87 87 void dropMimeData(int index, const QMimeData *mimeData);
88 void dropMimeDataOnGraph(VisualizationDragWidget *dragWidget, const QMimeData *mimeData);
88 89 };
89 90
90 91 #endif // SCIQLOP_VISUALIZATIONZONEWIDGET_H
@@ -8,6 +8,7 gui_moc_headers = [
8 8 'include/DragDropHelper.h',
9 9 'include/TimeWidget/TimeWidget.h',
10 10 'include/Variable/VariableInspectorWidget.h',
11 'include/Variable/VariableInspectorTableView.h',
11 12 'include/Variable/RenameVariableDialog.h',
12 13 'include/Visualization/qcustomplot.h',
13 14 'include/Visualization/VisualizationGraphWidget.h',
@@ -52,6 +53,7 gui_sources = [
52 53 'src/SidePane/SqpSidePane.cpp',
53 54 'src/TimeWidget/TimeWidget.cpp',
54 55 'src/Variable/VariableInspectorWidget.cpp',
56 'src/Variable/VariableInspectorTableView.cpp',
55 57 'src/Variable/VariableMenuHeaderWidget.cpp',
56 58 'src/Variable/RenameVariableDialog.cpp',
57 59 'src/Visualization/VisualizationGraphHelper.cpp',
@@ -4,6 +4,7
4 4 #include "DataSource/DataSourceItem.h"
5 5 #include "DataSource/DataSourceTreeWidgetItem.h"
6 6
7 #include "DragDropHelper.h"
7 8 #include "SqpApplication.h"
8 9
9 10 #include <QMimeData>
@@ -35,3 +36,10 QMimeData *DataSourceTreeWidget::mimeData(const QList<QTreeWidgetItem *> items)
35 36
36 37 return mimeData;
37 38 }
39
40 void DataSourceTreeWidget::startDrag(Qt::DropActions supportedActions)
41 {
42 // Resets the drag&drop operations before it's starting
43 sqpApp->dragDropHelper().resetDragAndDrop();
44 QTreeWidget::startDrag(supportedActions);
45 }
@@ -5,6 +5,7
5 5 #include "Visualization/VisualizationWidget.h"
6 6 #include "Visualization/operations/FindVariableOperation.h"
7 7
8 #include "Variable/Variable.h"
8 9 #include "Variable/VariableController.h"
9 10
10 11 #include "Common/MimeTypesDef.h"
@@ -13,6 +14,7
13 14 #include <QDir>
14 15 #include <QDragEnterEvent>
15 16 #include <QDragMoveEvent>
17 #include <QLabel>
16 18 #include <QScrollArea>
17 19 #include <QScrollBar>
18 20 #include <QTimer>
@@ -29,7 +31,6 struct DragDropScroller::DragDropScrollerPrivate {
29 31 QScrollArea *m_CurrentScrollArea = nullptr;
30 32 std::unique_ptr<QTimer> m_Timer = nullptr;
31 33
32
33 34 enum class ScrollDirection { up, down, unknown };
34 35 ScrollDirection m_Direction = ScrollDirection::unknown;
35 36
@@ -144,22 +145,40 struct DragDropHelper::DragDropHelperPrivate {
144 145
145 146 VisualizationDragWidget *m_CurrentDragWidget = nullptr;
146 147 std::unique_ptr<QWidget> m_PlaceHolder = nullptr;
148 QLabel *m_PlaceHolderLabel;
149 QWidget *m_PlaceBackground;
147 150 std::unique_ptr<DragDropScroller> m_DragDropScroller = nullptr;
148 151 QString m_ImageTempUrl; // Temporary file for image url generated by the drag & drop. Not using
149 152 // QTemporaryFile to have a name which is not generated.
150 153
154 VisualizationDragWidget *m_HighlightedDragWidget = nullptr;
155
156 QMetaObject::Connection m_DragWidgetDestroyedConnection;
157 QMetaObject::Connection m_HighlightedWidgetDestroyedConnection;
158
151 159 explicit DragDropHelperPrivate()
152 160 : m_PlaceHolder{std::make_unique<QWidget>()},
153 161 m_DragDropScroller{std::make_unique<DragDropScroller>()}
154 162 {
155 m_PlaceHolder->setStyleSheet("background-color: #BBD5EE; border:2px solid #2A7FD4");
156 sqpApp->installEventFilter(m_DragDropScroller.get());
157 163
164 auto layout = new QVBoxLayout{m_PlaceHolder.get()};
165 layout->setSpacing(0);
166 layout->setContentsMargins(0, 0, 0, 0);
167
168 m_PlaceHolderLabel = new QLabel{"", m_PlaceHolder.get()};
169 m_PlaceHolderLabel->setMinimumHeight(25);
170 layout->addWidget(m_PlaceHolderLabel);
158 171
159 m_ImageTempUrl = QDir::temp().absoluteFilePath("Scqlop_graph.png");
172 m_PlaceBackground = new QWidget{m_PlaceHolder.get()};
173 m_PlaceBackground->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
174 layout->addWidget(m_PlaceBackground);
175
176 sqpApp->installEventFilter(m_DragDropScroller.get());
177
178 m_ImageTempUrl = QDir::temp().absoluteFilePath("Sciqlop_graph.png");
160 179 }
161 180
162 void preparePlaceHolder() const
181 void preparePlaceHolder(DragDropHelper::PlaceHolderType type, const QString &topLabelText) const
163 182 {
164 183 if (m_CurrentDragWidget) {
165 184 m_PlaceHolder->setMinimumSize(m_CurrentDragWidget->size());
@@ -172,6 +191,22 struct DragDropHelper::DragDropHelperPrivate {
172 191 m_PlaceHolder->setMinimumSize(0, GRAPH_MINIMUM_HEIGHT);
173 192 m_PlaceHolder->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
174 193 }
194
195 switch (type) {
196 case DragDropHelper::PlaceHolderType::Graph:
197 m_PlaceBackground->setStyleSheet(
198 "background-color: #BBD5EE; border: 1px solid #2A7FD4");
199 break;
200 case DragDropHelper::PlaceHolderType::Zone:
201 case DragDropHelper::PlaceHolderType::Default:
202 m_PlaceBackground->setStyleSheet(
203 "background-color: #BBD5EE; border: 2px solid #2A7FD4");
204 m_PlaceHolderLabel->setStyleSheet("color: #2A7FD4");
205 break;
206 }
207
208 m_PlaceHolderLabel->setText(topLabelText);
209 m_PlaceHolderLabel->setVisible(!topLabelText.isEmpty());
175 210 }
176 211 };
177 212
@@ -188,10 +223,23 DragDropHelper::~DragDropHelper()
188 223 void DragDropHelper::resetDragAndDrop()
189 224 {
190 225 setCurrentDragWidget(nullptr);
226 impl->m_HighlightedDragWidget = nullptr;
191 227 }
192 228
193 229 void DragDropHelper::setCurrentDragWidget(VisualizationDragWidget *dragWidget)
194 230 {
231 if (impl->m_CurrentDragWidget) {
232
233 QObject::disconnect(impl->m_DragWidgetDestroyedConnection);
234 }
235
236 if (dragWidget) {
237 // ensures the impl->m_CurrentDragWidget is reset when the widget is destroyed
238 impl->m_DragWidgetDestroyedConnection
239 = QObject::connect(dragWidget, &VisualizationDragWidget::destroyed,
240 [this]() { impl->m_CurrentDragWidget = nullptr; });
241 }
242
195 243 impl->m_CurrentDragWidget = dragWidget;
196 244 }
197 245
@@ -200,16 +248,16 VisualizationDragWidget *DragDropHelper::getCurrentDragWidget() const
200 248 return impl->m_CurrentDragWidget;
201 249 }
202 250
203
204 251 QWidget &DragDropHelper::placeHolder() const
205 252 {
206 253 return *impl->m_PlaceHolder;
207 254 }
208 255
209 void DragDropHelper::insertPlaceHolder(QVBoxLayout *layout, int index)
256 void DragDropHelper::insertPlaceHolder(QVBoxLayout *layout, int index, PlaceHolderType type,
257 const QString &topLabelText)
210 258 {
211 259 removePlaceHolder();
212 impl->preparePlaceHolder();
260 impl->preparePlaceHolder(type, topLabelText);
213 261 layout->insertWidget(index, impl->m_PlaceHolder.get());
214 262 impl->m_PlaceHolder->show();
215 263 }
@@ -245,6 +293,30 QUrl DragDropHelper::imageTemporaryUrl(const QImage &image) const
245 293 return QUrl::fromLocalFile(impl->m_ImageTempUrl);
246 294 }
247 295
296 void DragDropHelper::setHightlightedDragWidget(VisualizationDragWidget *dragWidget)
297 {
298 if (impl->m_HighlightedDragWidget) {
299 impl->m_HighlightedDragWidget->highlightForMerge(false);
300 QObject::disconnect(impl->m_HighlightedWidgetDestroyedConnection);
301 }
302
303 if (dragWidget) {
304 dragWidget->highlightForMerge(true);
305
306 // ensures the impl->m_HighlightedDragWidget is reset when the widget is destroyed
307 impl->m_DragWidgetDestroyedConnection
308 = QObject::connect(dragWidget, &VisualizationDragWidget::destroyed,
309 [this]() { impl->m_HighlightedDragWidget = nullptr; });
310 }
311
312 impl->m_HighlightedDragWidget = dragWidget;
313 }
314
315 VisualizationDragWidget *DragDropHelper::getHightlightedDragWidget() const
316 {
317 return impl->m_HighlightedDragWidget;
318 }
319
248 320 bool DragDropHelper::checkMimeDataForVisualization(const QMimeData *mimeData,
249 321 VisualizationDragDropContainer *dropContainer)
250 322 {
@@ -252,44 +324,60 bool DragDropHelper::checkMimeDataForVisualization(const QMimeData *mimeData,
252 324 qCWarning(LOG_DragDropHelper()) << QObject::tr(
253 325 "DragDropHelper::checkMimeDataForVisualization, invalid input parameters.");
254 326 Q_ASSERT(false);
327 return false;
255 328 }
256 329
257 auto result = true;
330 auto result = false;
258 331
259 332 if (mimeData->hasFormat(MIME_TYPE_VARIABLE_LIST)) {
260 333 auto variables = sqpApp->variableController().variablesForMimeData(
261 334 mimeData->data(MIME_TYPE_VARIABLE_LIST));
262 335
263 336 if (variables.count() == 1) {
264 // Check that the viariable is not already in a graph
265 337
266 // Search for the top level VisualizationWidget
267 auto parent = dropContainer->parentWidget();
268 while (parent && qobject_cast<VisualizationWidget *>(parent) == nullptr) {
269 parent = parent->parentWidget();
270 }
338 auto variable = variables.first();
339 if (variable->dataSeries() != nullptr) {
271 340
272 if (parent) {
273 auto visualizationWidget = static_cast<VisualizationWidget *>(parent);
341 // Check that the variable is not already in a graph
274 342
275 FindVariableOperation findVariableOperation{variables.first()};
276 visualizationWidget->accept(&findVariableOperation);
277 auto variableContainers = findVariableOperation.result();
278 if (!variableContainers.empty()) {
279 result = false;
343 auto parent = dropContainer->parentWidget();
344 while (parent && qobject_cast<VisualizationWidget *>(parent) == nullptr) {
345 parent = parent->parentWidget(); // Search for the top level VisualizationWidget
346 }
347
348 if (parent) {
349 auto visualizationWidget = static_cast<VisualizationWidget *>(parent);
350
351 FindVariableOperation findVariableOperation{variable};
352 visualizationWidget->accept(&findVariableOperation);
353 auto variableContainers = findVariableOperation.result();
354 if (variableContainers.empty()) {
355 result = true;
356 }
357 else {
358 // result = false: the variable already exist in the visualisation
359 }
360 }
361 else {
362 qCWarning(LOG_DragDropHelper()) << QObject::tr(
363 "DragDropHelper::checkMimeDataForVisualization, the parent "
364 "VisualizationWidget cannot be found. Cannot check if the variable is "
365 "already used or not.");
280 366 }
281 367 }
282 368 else {
283 qCWarning(LOG_DragDropHelper()) << QObject::tr(
284 "DragDropHelper::checkMimeDataForVisualization, the parent "
285 "VisualizationWidget cannot be found.");
286 result = false;
369 // result = false: the variable is not fully loaded
287 370 }
288 371 }
289 372 else {
290 result = false;
373 // result = false: cannot drop multiple variables in the visualisation
291 374 }
292 375 }
376 else {
377 // Other MIME data
378 // no special rules, accepted by default
379 result = true;
380 }
293 381
294 382 return result;
295 383 }
@@ -2,10 +2,29
2 2 #include "ui_TimeWidget.h"
3 3
4 4 #include <Common/DateUtils.h>
5 #include <Common/MimeTypesDef.h>
6
7 #include <DragDropHelper.h>
5 8 #include <SqpApplication.h>
6 9 #include <Time/TimeController.h>
7 10
8 TimeWidget::TimeWidget(QWidget *parent) : QWidget{parent}, ui{new Ui::TimeWidget}
11 #include <QDrag>
12 #include <QDragEnterEvent>
13 #include <QDropEvent>
14 #include <QMimeData>
15
16
17 struct TimeWidget::TimeWidgetPrivate {
18
19 explicit TimeWidgetPrivate() {}
20
21 QPoint m_DragStartPosition;
22 };
23
24 TimeWidget::TimeWidget(QWidget *parent)
25 : QWidget{parent},
26 ui{new Ui::TimeWidget},
27 impl{spimpl::make_unique_impl<TimeWidgetPrivate>()}
9 28 {
10 29 ui->setupUi(this);
11 30
@@ -41,10 +60,97 TimeWidget::~TimeWidget()
41 60 delete ui;
42 61 }
43 62
44 void TimeWidget::onTimeUpdateRequested()
63 void TimeWidget::setTimeRange(SqpRange time)
64 {
65 auto startDateTime = DateUtils::dateTime(time.m_TStart);
66 auto endDateTime = DateUtils::dateTime(time.m_TEnd);
67
68 ui->startDateTimeEdit->setDateTime(startDateTime);
69 ui->endDateTimeEdit->setDateTime(endDateTime);
70 }
71
72 SqpRange TimeWidget::timeRange() const
45 73 {
46 auto dateTime = SqpRange{DateUtils::secondsSinceEpoch(ui->startDateTimeEdit->dateTime()),
47 DateUtils::secondsSinceEpoch(ui->endDateTimeEdit->dateTime())};
74 return SqpRange{DateUtils::secondsSinceEpoch(ui->startDateTimeEdit->dateTime()),
75 DateUtils::secondsSinceEpoch(ui->endDateTimeEdit->dateTime())};
76 }
48 77
78 void TimeWidget::onTimeUpdateRequested()
79 {
80 auto dateTime = timeRange();
49 81 emit timeUpdated(std::move(dateTime));
50 82 }
83
84 void TimeWidget::dragEnterEvent(QDragEnterEvent *event)
85 {
86 if (event->mimeData()->hasFormat(MIME_TYPE_TIME_RANGE)) {
87 event->acceptProposedAction();
88 setStyleSheet("QDateTimeEdit{background-color: #BBD5EE; border:2px solid #2A7FD4}");
89 }
90 else {
91 event->ignore();
92 }
93 }
94
95 void TimeWidget::dragLeaveEvent(QDragLeaveEvent *event)
96 {
97 setStyleSheet(QString());
98 }
99
100 void TimeWidget::dropEvent(QDropEvent *event)
101 {
102 if (event->mimeData()->hasFormat(MIME_TYPE_TIME_RANGE)) {
103 auto mimeData = event->mimeData()->data(MIME_TYPE_TIME_RANGE);
104 auto timeRange = TimeController::timeRangeForMimeData(mimeData);
105
106 setTimeRange(timeRange);
107 }
108 else {
109 event->ignore();
110 }
111
112 setStyleSheet(QString());
113 }
114
115
116 void TimeWidget::mousePressEvent(QMouseEvent *event)
117 {
118 if (event->button() == Qt::LeftButton) {
119 impl->m_DragStartPosition = event->pos();
120 }
121
122 QWidget::mousePressEvent(event);
123 }
124
125 void TimeWidget::mouseMoveEvent(QMouseEvent *event)
126 {
127 if (!(event->buttons() & Qt::LeftButton)) {
128 return;
129 }
130
131 if ((event->pos() - impl->m_DragStartPosition).manhattanLength()
132 < QApplication::startDragDistance()) {
133 return;
134 }
135
136 // Note: The management of the drag object is done by Qt
137 auto drag = new QDrag{this};
138
139 auto mimeData = new QMimeData;
140 auto timeData = TimeController::mimeDataForTimeRange(timeRange());
141 mimeData->setData(MIME_TYPE_TIME_RANGE, timeData);
142
143 drag->setMimeData(mimeData);
144
145 auto pixmap = QPixmap(size());
146 render(&pixmap);
147 drag->setPixmap(pixmap);
148 drag->setHotSpot(impl->m_DragStartPosition);
149
150 sqpApp->dragDropHelper().resetDragAndDrop();
151
152 // Note: The exec() is blocking on windows but not on linux and macOS
153 drag->exec(Qt::MoveAction | Qt::CopyAction);
154
155 QWidget::mouseMoveEvent(event);
156 }
@@ -152,11 +152,6 VariableInspectorWidget::VariableInspectorWidget(QWidget *parent)
152 152 ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu);
153 153 connect(ui->tableView, &QTableView::customContextMenuRequested, this,
154 154 &VariableInspectorWidget::onTableMenuRequested);
155
156 // Resets the drag&drop operation on a left-click (the drag&drop is also started by a left
157 // click).
158 connect(ui->tableView, &QTableView::clicked,
159 [](const auto &modelIndex) { sqpApp->dragDropHelper().resetDragAndDrop(); });
160 155 }
161 156
162 157 VariableInspectorWidget::~VariableInspectorWidget()
@@ -17,11 +17,15 Q_LOGGING_CATEGORY(LOG_VisualizationDragDropContainer, "VisualizationDragDropCon
17 17 struct VisualizationDragDropContainer::VisualizationDragDropContainerPrivate {
18 18
19 19 QVBoxLayout *m_Layout;
20 QStringList m_AcceptedMimeTypes;
21 QStringList m_MergeAllowedMimeTypes;
20 QHash<QString, VisualizationDragDropContainer::DropBehavior> m_AcceptedMimeTypes;
21 QString m_PlaceHolderText;
22 DragDropHelper::PlaceHolderType m_PlaceHolderType = DragDropHelper::PlaceHolderType::Graph;
23
22 24 VisualizationDragDropContainer::AcceptMimeDataFunction m_AcceptMimeDataFun
23 25 = [](auto mimeData) { return true; };
24 26
27 int m_MinContainerHeight = 0;
28
25 29 explicit VisualizationDragDropContainerPrivate(QWidget *widget)
26 30 {
27 31 m_Layout = new QVBoxLayout(widget);
@@ -30,7 +34,7 struct VisualizationDragDropContainer::VisualizationDragDropContainerPrivate {
30 34
31 35 bool acceptMimeData(const QMimeData *data) const
32 36 {
33 for (const auto &type : m_AcceptedMimeTypes) {
37 for (const auto &type : m_AcceptedMimeTypes.keys()) {
34 38 if (data->hasFormat(type) && m_AcceptMimeDataFun(data)) {
35 39 return true;
36 40 }
@@ -39,10 +43,38 struct VisualizationDragDropContainer::VisualizationDragDropContainerPrivate {
39 43 return false;
40 44 }
41 45
42 bool allowMergeMimeData(const QMimeData *data) const
46 bool allowMergeForMimeData(const QMimeData *data) const
47 {
48 bool result = false;
49 for (auto it = m_AcceptedMimeTypes.constBegin(); it != m_AcceptedMimeTypes.constEnd();
50 ++it) {
51
52 if (data->hasFormat(it.key())
53 && (it.value() == VisualizationDragDropContainer::DropBehavior::Merged
54 || it.value()
55 == VisualizationDragDropContainer::DropBehavior::InsertedAndMerged)) {
56 result = true;
57 }
58 else if (data->hasFormat(it.key())
59 && it.value() == VisualizationDragDropContainer::DropBehavior::Inserted) {
60 // Merge is forbidden if the mime data contain an acceptable type which cannot be
61 // merged
62 result = false;
63 break;
64 }
65 }
66
67 return result;
68 }
69
70 bool allowInsertForMimeData(const QMimeData *data) const
43 71 {
44 for (const auto &type : m_MergeAllowedMimeTypes) {
45 if (data->hasFormat(type)) {
72 for (auto it = m_AcceptedMimeTypes.constBegin(); it != m_AcceptedMimeTypes.constEnd();
73 ++it) {
74 if (data->hasFormat(it.key())
75 && (it.value() == VisualizationDragDropContainer::DropBehavior::Inserted
76 || it.value()
77 == VisualizationDragDropContainer::DropBehavior::InsertedAndMerged)) {
46 78 return true;
47 79 }
48 80 }
@@ -55,7 +87,7 struct VisualizationDragDropContainer::VisualizationDragDropContainerPrivate {
55 87 return sqpApp->dragDropHelper().placeHolder().parentWidget() == m_Layout->parentWidget();
56 88 }
57 89
58 VisualizationDragWidget *getChildDragWidgetAt(QWidget *parent, const QPoint &pos) const
90 VisualizationDragWidget *getChildDragWidgetAt(const QWidget *parent, const QPoint &pos) const
59 91 {
60 92 VisualizationDragWidget *dragWidget = nullptr;
61 93
@@ -74,15 +106,29 struct VisualizationDragDropContainer::VisualizationDragDropContainerPrivate {
74 106
75 107 bool cursorIsInContainer(QWidget *container) const
76 108 {
77 auto adustNum = 18; // to be safe, in case of scrollbar on the side
78 auto containerRect = QRect(QPoint(), container->contentsRect().size())
79 .adjusted(adustNum, adustNum, -adustNum, -adustNum);
80 return containerRect.contains(container->mapFromGlobal(QCursor::pos()));
109 return container->isAncestorOf(sqpApp->widgetAt(QCursor::pos()));
110 }
111
112 int countDragWidget(const QWidget *parent, bool onlyVisible = false) const
113 {
114 auto nbGraph = 0;
115 for (auto child : parent->children()) {
116 if (qobject_cast<VisualizationDragWidget *>(child)) {
117 if (!onlyVisible || qobject_cast<VisualizationDragWidget *>(child)->isVisible()) {
118 nbGraph += 1;
119 }
120 }
121 }
122
123 return nbGraph;
81 124 }
125
126 void findPlaceHolderPosition(const QPoint &pos, bool canInsert, bool canMerge,
127 const VisualizationDragDropContainer *container);
82 128 };
83 129
84 130 VisualizationDragDropContainer::VisualizationDragDropContainer(QWidget *parent)
85 : QWidget{parent},
131 : QFrame{parent},
86 132 impl{spimpl::make_unique_impl<VisualizationDragDropContainerPrivate>(this)}
87 133 {
88 134 setAcceptDrops(true);
@@ -105,26 +151,15 void VisualizationDragDropContainer::insertDragWidget(int index,
105 151 &VisualizationDragDropContainer::startDrag);
106 152 }
107 153
108 void VisualizationDragDropContainer::setAcceptedMimeTypes(const QStringList &mimeTypes)
109 {
110 impl->m_AcceptedMimeTypes = mimeTypes;
111 }
112
113 void VisualizationDragDropContainer::setMergeAllowedMimeTypes(const QStringList &mimeTypes)
154 void VisualizationDragDropContainer::addAcceptedMimeType(
155 const QString &mimeType, VisualizationDragDropContainer::DropBehavior behavior)
114 156 {
115 impl->m_MergeAllowedMimeTypes = mimeTypes;
157 impl->m_AcceptedMimeTypes[mimeType] = behavior;
116 158 }
117 159
118 160 int VisualizationDragDropContainer::countDragWidget() const
119 161 {
120 auto nbGraph = 0;
121 for (auto child : children()) {
122 if (qobject_cast<VisualizationDragWidget *>(child)) {
123 nbGraph += 1;
124 }
125 }
126
127 return nbGraph;
162 return impl->countDragWidget(this);
128 163 }
129 164
130 165 void VisualizationDragDropContainer::setAcceptMimeDataFunction(
@@ -133,6 +168,13 void VisualizationDragDropContainer::setAcceptMimeDataFunction(
133 168 impl->m_AcceptMimeDataFun = fun;
134 169 }
135 170
171 void VisualizationDragDropContainer::setPlaceHolderType(DragDropHelper::PlaceHolderType type,
172 const QString &placeHolderText)
173 {
174 impl->m_PlaceHolderType = type;
175 impl->m_PlaceHolderText = placeHolderText;
176 }
177
136 178 void VisualizationDragDropContainer::startDrag(VisualizationDragWidget *dragWidget,
137 179 const QPoint &dragPosition)
138 180 {
@@ -159,9 +201,14 void VisualizationDragDropContainer::startDrag(VisualizationDragWidget *dragWidg
159 201
160 202 if (impl->cursorIsInContainer(this)) {
161 203 auto dragWidgetIndex = impl->m_Layout->indexOf(dragWidget);
162 helper.insertPlaceHolder(impl->m_Layout, dragWidgetIndex);
204 helper.insertPlaceHolder(impl->m_Layout, dragWidgetIndex, impl->m_PlaceHolderType,
205 impl->m_PlaceHolderText);
163 206 dragWidget->setVisible(false);
164 207 }
208 else {
209 // The drag starts directly outside the drop zone
210 // do not add the placeHolder
211 }
165 212
166 213 // Note: The exec() is blocking on windows but not on linux and macOS
167 214 drag->exec(Qt::MoveAction | Qt::CopyAction);
@@ -185,7 +232,7 void VisualizationDragDropContainer::dragEnterEvent(QDragEnterEvent *event)
185 232
186 233 if (dragWidget) {
187 234 // If the drag&drop is internal to the visualization, entering the container hide
188 // the dragWidget which was hidden by the dragLeaveEvent
235 // the dragWidget which was made visible by the dragLeaveEvent
189 236 auto parentWidget
190 237 = qobject_cast<VisualizationDragDropContainer *>(dragWidget->parentWidget());
191 238 if (parentWidget) {
@@ -193,26 +240,9 void VisualizationDragDropContainer::dragEnterEvent(QDragEnterEvent *event)
193 240 }
194 241 }
195 242
196 auto dragWidgetHovered = impl->getChildDragWidgetAt(this, event->pos());
197
198 if (dragWidgetHovered) {
199 auto hoveredWidgetIndex = impl->m_Layout->indexOf(dragWidgetHovered);
200
201 if (dragWidget) {
202 auto dragWidgetIndex = impl->m_Layout->indexOf(helper.getCurrentDragWidget());
203 if (dragWidgetIndex >= 0 && dragWidgetIndex <= hoveredWidgetIndex) {
204 // Correction of the index if the drop occurs in the same container
205 // and if the drag is started from the visualization (in that case, the
206 // dragWidget is hidden)
207 hoveredWidgetIndex += 1;
208 }
209 }
210
211 helper.insertPlaceHolder(impl->m_Layout, hoveredWidgetIndex);
212 }
213 else {
214 helper.insertPlaceHolder(impl->m_Layout, 0);
215 }
243 auto canMerge = impl->allowMergeForMimeData(event->mimeData());
244 auto canInsert = impl->allowInsertForMimeData(event->mimeData());
245 impl->findPlaceHolderPosition(event->pos(), canInsert, canMerge, this);
216 246 }
217 247 else {
218 248 // do nothing
@@ -233,6 +263,8 void VisualizationDragDropContainer::dragLeaveEvent(QDragLeaveEvent *event)
233 263
234 264 if (!impl->cursorIsInContainer(this)) {
235 265 helper.removePlaceHolder();
266 helper.setHightlightedDragWidget(nullptr);
267 impl->m_MinContainerHeight = 0;
236 268
237 269 auto dragWidget = helper.getCurrentDragWidget();
238 270 if (dragWidget) {
@@ -258,105 +290,164 void VisualizationDragDropContainer::dragLeaveEvent(QDragLeaveEvent *event)
258 290 void VisualizationDragDropContainer::dragMoveEvent(QDragMoveEvent *event)
259 291 {
260 292 if (impl->acceptMimeData(event->mimeData())) {
261 auto dragWidgetHovered = impl->getChildDragWidgetAt(this, event->pos());
262 if (dragWidgetHovered) {
263 auto canMerge = impl->allowMergeMimeData(event->mimeData());
264
265 auto nbDragWidget = countDragWidget();
266 if (nbDragWidget > 0) {
267 auto graphHeight = qMax(size().height() / nbDragWidget, GRAPH_MINIMUM_HEIGHT);
293 auto canMerge = impl->allowMergeForMimeData(event->mimeData());
294 auto canInsert = impl->allowInsertForMimeData(event->mimeData());
295 impl->findPlaceHolderPosition(event->pos(), canInsert, canMerge, this);
296 }
297 else {
298 event->ignore();
299 }
268 300
269 auto dropIndex = floor(event->pos().y() / graphHeight);
270 auto zoneSize = qMin(graphHeight / 3.0, 150.0);
301 QWidget::dragMoveEvent(event);
302 }
271 303
272 auto isOnTop = event->pos().y() < dropIndex * graphHeight + zoneSize;
273 auto isOnBottom = event->pos().y() > (dropIndex + 1) * graphHeight - zoneSize;
304 void VisualizationDragDropContainer::dropEvent(QDropEvent *event)
305 {
306 auto &helper = sqpApp->dragDropHelper();
274 307
275 auto &helper = sqpApp->dragDropHelper();
276 auto placeHolderIndex = impl->m_Layout->indexOf(&(helper.placeHolder()));
308 if (impl->acceptMimeData(event->mimeData())) {
309 auto dragWidget = helper.getCurrentDragWidget();
310 if (impl->hasPlaceHolder()) {
311 // drop where the placeHolder is located
277 312
278 if (isOnTop || isOnBottom) {
279 if (isOnBottom) {
280 dropIndex += 1;
281 }
313 auto canInsert = impl->allowInsertForMimeData(event->mimeData());
314 if (canInsert) {
315 auto droppedIndex = impl->m_Layout->indexOf(&helper.placeHolder());
282 316
283 if (helper.getCurrentDragWidget()) {
284 auto dragWidgetIndex
285 = impl->m_Layout->indexOf(helper.getCurrentDragWidget());
286 if (dragWidgetIndex >= 0 && dragWidgetIndex <= dropIndex) {
287 // Correction of the index if the drop occurs in the same container
288 // and if the drag is started from the visualization (in that case, the
289 // dragWidget is hidden)
290 dropIndex += 1;
291 }
317 if (dragWidget) {
318 auto dragWidgetIndex = impl->m_Layout->indexOf(dragWidget);
319 if (dragWidgetIndex >= 0 && dragWidgetIndex < droppedIndex) {
320 // Correction of the index if the drop occurs in the same container
321 // and if the drag is started from the visualization (in that case, the
322 // dragWidget is hidden)
323 droppedIndex -= 1;
292 324 }
293 325
294 if (dropIndex != placeHolderIndex) {
295 helper.insertPlaceHolder(impl->m_Layout, dropIndex);
296 }
297 }
298 else if (canMerge) {
299 // drop on the middle -> merge
300 if (impl->hasPlaceHolder()) {
301 helper.removePlaceHolder();
302 }
326 dragWidget->setVisible(true);
303 327 }
328
329 event->acceptProposedAction();
330
331 helper.removePlaceHolder();
332
333 emit dropOccuredInContainer(droppedIndex, event->mimeData());
304 334 }
305 335 else {
306 qCWarning(LOG_VisualizationDragDropContainer())
307 << tr("VisualizationDragDropContainer::dragMoveEvent, no widget found in the "
308 "container");
336 qCWarning(LOG_VisualizationDragDropContainer()) << tr(
337 "VisualizationDragDropContainer::dropEvent, dropping on the placeHolder, but "
338 "the insertion is forbidden.");
339 Q_ASSERT(false);
309 340 }
310 341 }
311 else {
312 // No hovered drag widget, the mouse is probably hover the placeHolder
313 // Do nothing
342 else if (helper.getHightlightedDragWidget()) {
343 // drop on the highlighted widget
344
345 auto canMerge = impl->allowMergeForMimeData(event->mimeData());
346 if (canMerge) {
347 event->acceptProposedAction();
348 emit dropOccuredOnWidget(helper.getHightlightedDragWidget(), event->mimeData());
349 }
350 else {
351 qCWarning(LOG_VisualizationDragDropContainer())
352 << tr("VisualizationDragDropContainer::dropEvent, dropping on a widget, but "
353 "the merge is forbidden.");
354 Q_ASSERT(false);
355 }
314 356 }
315 357 }
316 358 else {
317 359 event->ignore();
318 360 }
319 361
320 QWidget::dragMoveEvent(event);
362 sqpApp->dragDropHelper().setHightlightedDragWidget(nullptr);
363 impl->m_MinContainerHeight = 0;
364
365 QWidget::dropEvent(event);
321 366 }
322 367
323 void VisualizationDragDropContainer::dropEvent(QDropEvent *event)
368
369 void VisualizationDragDropContainer::VisualizationDragDropContainerPrivate::findPlaceHolderPosition(
370 const QPoint &pos, bool canInsert, bool canMerge,
371 const VisualizationDragDropContainer *container)
324 372 {
325 if (impl->acceptMimeData(event->mimeData())) {
326 auto dragWidget = sqpApp->dragDropHelper().getCurrentDragWidget();
327 if (impl->hasPlaceHolder()) {
328 auto &helper = sqpApp->dragDropHelper();
373 auto &helper = sqpApp->dragDropHelper();
329 374
330 auto droppedIndex = impl->m_Layout->indexOf(&helper.placeHolder());
375 auto absPos = container->mapToGlobal(pos);
376 auto isOnPlaceHolder = sqpApp->widgetAt(absPos) == &(helper.placeHolder());
331 377
332 if (dragWidget) {
333 auto dragWidgetIndex = impl->m_Layout->indexOf(dragWidget);
334 if (dragWidgetIndex >= 0 && dragWidgetIndex < droppedIndex) {
335 // Correction of the index if the drop occurs in the same container
336 // and if the drag is started from the visualization (in that case, the
337 // dragWidget is hidden)
338 droppedIndex -= 1;
339 }
378 if (countDragWidget(container, true) == 0) {
379 // Drop on an empty container, just add the placeHolder at the top
380 helper.insertPlaceHolder(m_Layout, 0, m_PlaceHolderType, m_PlaceHolderText);
381 }
382 else if (!isOnPlaceHolder) {
383 auto nbDragWidget = countDragWidget(container);
384 if (nbDragWidget > 0) {
340 385
341 dragWidget->setVisible(true);
386 if (m_MinContainerHeight == 0) {
387 m_MinContainerHeight = container->size().height();
342 388 }
343 389
344 event->acceptProposedAction();
390 m_MinContainerHeight = qMin(m_MinContainerHeight, container->size().height());
391 auto graphHeight = qMax(m_MinContainerHeight / nbDragWidget, GRAPH_MINIMUM_HEIGHT);
392
393 auto posY = pos.y();
394 auto dropIndex = floor(posY / graphHeight);
395 auto zoneSize = qMin(graphHeight / 4.0, 75.0);
396
397
398 auto isOnTop = posY < dropIndex * graphHeight + zoneSize;
399 auto isOnBottom = posY > (dropIndex + 1) * graphHeight - zoneSize;
400
401 auto placeHolderIndex = m_Layout->indexOf(&(helper.placeHolder()));
402
403 auto dragWidgetHovered = getChildDragWidgetAt(container, pos);
345 404
346 helper.removePlaceHolder();
405 if (canInsert && (isOnTop || isOnBottom || !canMerge)) {
406 if (isOnBottom) {
407 dropIndex += 1;
408 }
347 409
348 emit dropOccured(droppedIndex, event->mimeData());
410 if (helper.getCurrentDragWidget()) {
411 auto dragWidgetIndex = m_Layout->indexOf(helper.getCurrentDragWidget());
412 if (dragWidgetIndex >= 0 && dragWidgetIndex <= dropIndex) {
413 // Correction of the index if the drop occurs in the same container
414 // and if the drag is started from the visualization (in that case, the
415 // dragWidget is hidden)
416 dropIndex += 1;
417 }
418 }
419
420 if (dropIndex != placeHolderIndex) {
421 helper.insertPlaceHolder(m_Layout, dropIndex, m_PlaceHolderType,
422 m_PlaceHolderText);
423 }
424
425 helper.setHightlightedDragWidget(nullptr);
426 }
427 else if (canMerge && dragWidgetHovered) {
428 // drop on the middle -> merge
429 if (hasPlaceHolder()) {
430 helper.removePlaceHolder();
431 }
432
433 helper.setHightlightedDragWidget(dragWidgetHovered);
434 }
435 else {
436 qCWarning(LOG_VisualizationDragDropContainer())
437 << tr("VisualizationDragDropContainer::findPlaceHolderPosition, no valid drop "
438 "action.");
439 Q_ASSERT(false);
440 }
349 441 }
350 442 else {
351 443 qCWarning(LOG_VisualizationDragDropContainer())
352 << tr("VisualizationDragDropContainer::dropEvent, couldn't drop because the "
353 "placeHolder is not found.");
354 Q_ASSERT(false);
444 << tr("VisualizationDragDropContainer::findPlaceHolderPosition, no widget "
445 "found in the "
446 "container");
355 447 }
356 448 }
357 449 else {
358 event->ignore();
450 // the mouse is hover the placeHolder
451 // Do nothing
359 452 }
360
361 QWidget::dropEvent(event);
362 453 }
@@ -12,6 +12,7
12 12 #include <DragDropHelper.h>
13 13 #include <Settings/SqpSettingsDefs.h>
14 14 #include <SqpApplication.h>
15 #include <Time/TimeController.h>
15 16 #include <Variable/Variable.h>
16 17 #include <Variable/VariableController.h>
17 18
@@ -235,6 +236,9 QMimeData *VisualizationGraphWidget::mimeData() const
235 236 auto mimeData = new QMimeData;
236 237 mimeData->setData(MIME_TYPE_GRAPH, QByteArray{});
237 238
239 auto timeRangeData = TimeController::mimeDataForTimeRange(graphRange());
240 mimeData->setData(MIME_TYPE_TIME_RANGE, timeRangeData);
241
238 242 return mimeData;
239 243 }
240 244
@@ -243,6 +247,18 bool VisualizationGraphWidget::isDragAllowed() const
243 247 return true;
244 248 }
245 249
250 void VisualizationGraphWidget::highlightForMerge(bool highlighted)
251 {
252 if (highlighted) {
253 plot().setBackground(QBrush(QColor("#BBD5EE")));
254 }
255 else {
256 plot().setBackground(QBrush(Qt::white));
257 }
258
259 plot().update();
260 }
261
246 262 void VisualizationGraphWidget::closeEvent(QCloseEvent *event)
247 263 {
248 264 Q_UNUSED(event);
@@ -68,14 +68,23 VisualizationTabWidget::VisualizationTabWidget(const QString &name, QWidget *par
68 68 {
69 69 ui->setupUi(this);
70 70
71 ui->dragDropContainer->setAcceptedMimeTypes(
72 {MIME_TYPE_GRAPH, MIME_TYPE_ZONE, MIME_TYPE_VARIABLE_LIST});
73 connect(ui->dragDropContainer, &VisualizationDragDropContainer::dropOccured, this,
74 &VisualizationTabWidget::dropMimeData);
71 ui->dragDropContainer->setPlaceHolderType(DragDropHelper::PlaceHolderType::Zone, "Zone");
72 ui->dragDropContainer->layout()->setContentsMargins(0, 0, 0, 5);
73 ui->dragDropContainer->addAcceptedMimeType(
74 MIME_TYPE_GRAPH, VisualizationDragDropContainer::DropBehavior::Inserted);
75 ui->dragDropContainer->addAcceptedMimeType(
76 MIME_TYPE_ZONE, VisualizationDragDropContainer::DropBehavior::Inserted);
77 ui->dragDropContainer->addAcceptedMimeType(
78 MIME_TYPE_VARIABLE_LIST, VisualizationDragDropContainer::DropBehavior::Inserted);
79
75 80 ui->dragDropContainer->setAcceptMimeDataFunction([this](auto mimeData) {
76 81 return sqpApp->dragDropHelper().checkMimeDataForVisualization(mimeData,
77 82 ui->dragDropContainer);
78 83 });
84
85 connect(ui->dragDropContainer, &VisualizationDragDropContainer::dropOccuredInContainer, this,
86 &VisualizationTabWidget::dropMimeData);
87
79 88 sqpApp->dragDropHelper().addDragDropScrollArea(ui->scrollArea);
80 89
81 90 // Widget is deleted when closed
@@ -10,6 +10,7
10 10 #include "Common/VisualizationDef.h"
11 11
12 12 #include <Data/SqpRange.h>
13 #include <Time/TimeController.h>
13 14 #include <Variable/Variable.h>
14 15 #include <Variable/VariableController.h>
15 16
@@ -71,6 +72,21 struct VisualizationZoneWidget::VisualizationZoneWidgetPrivate {
71 72 QUuid m_SynchronisationGroupId;
72 73 std::unique_ptr<IGraphSynchronizer> m_Synchronizer;
73 74
75 // Returns the first graph in the zone or nullptr if there is no graph inside
76 VisualizationGraphWidget *firstGraph(const VisualizationZoneWidget *zoneWidget) const
77 {
78 VisualizationGraphWidget *firstGraph = nullptr;
79 auto layout = zoneWidget->ui->dragDropContainer->layout();
80 if (layout->count() > 0) {
81 if (auto visualizationGraphWidget
82 = qobject_cast<VisualizationGraphWidget *>(layout->itemAt(0)->widget())) {
83 firstGraph = visualizationGraphWidget;
84 }
85 }
86
87 return firstGraph;
88 }
89
74 90 void dropGraph(int index, VisualizationZoneWidget *zoneWidget);
75 91 void dropVariables(const QList<std::shared_ptr<Variable> > &variables, int index,
76 92 VisualizationZoneWidget *zoneWidget);
@@ -85,13 +101,22 VisualizationZoneWidget::VisualizationZoneWidget(const QString &name, QWidget *p
85 101
86 102 ui->zoneNameLabel->setText(name);
87 103
88 ui->dragDropContainer->setAcceptedMimeTypes({MIME_TYPE_GRAPH, MIME_TYPE_VARIABLE_LIST});
104 ui->dragDropContainer->setPlaceHolderType(DragDropHelper::PlaceHolderType::Graph);
105 ui->dragDropContainer->addAcceptedMimeType(
106 MIME_TYPE_GRAPH, VisualizationDragDropContainer::DropBehavior::Inserted);
107 ui->dragDropContainer->addAcceptedMimeType(
108 MIME_TYPE_VARIABLE_LIST, VisualizationDragDropContainer::DropBehavior::InsertedAndMerged);
109 ui->dragDropContainer->addAcceptedMimeType(
110 MIME_TYPE_TIME_RANGE, VisualizationDragDropContainer::DropBehavior::Merged);
89 111 ui->dragDropContainer->setAcceptMimeDataFunction([this](auto mimeData) {
90 112 return sqpApp->dragDropHelper().checkMimeDataForVisualization(mimeData,
91 113 ui->dragDropContainer);
92 114 });
93 connect(ui->dragDropContainer, &VisualizationDragDropContainer::dropOccured, this,
115
116 connect(ui->dragDropContainer, &VisualizationDragDropContainer::dropOccuredInContainer, this,
94 117 &VisualizationZoneWidget::dropMimeData);
118 connect(ui->dragDropContainer, &VisualizationDragDropContainer::dropOccuredOnWidget, this,
119 &VisualizationZoneWidget::dropMimeDataOnGraph);
95 120
96 121 // 'Close' options : widget is deleted when closed
97 122 setAttribute(Qt::WA_DeleteOnClose);
@@ -234,15 +259,9 VisualizationGraphWidget *VisualizationZoneWidget::createGraph(std::shared_ptr<V
234 259 &VisualizationZoneWidget::onVariableAboutToBeRemoved);
235 260
236 261 auto range = SqpRange{};
237
238 // Apply visitor to graph children
239 auto layout = ui->dragDropContainer->layout();
240 if (layout->count() > 0) {
262 if (auto firstGraph = impl->firstGraph(this)) {
241 263 // Case of a new graph in a existant zone
242 if (auto visualizationGraphWidget
243 = dynamic_cast<VisualizationGraphWidget *>(layout->itemAt(0)->widget())) {
244 range = visualizationGraphWidget->graphRange();
245 }
264 range = firstGraph->graphRange();
246 265 }
247 266 else {
248 267 // Case of a new graph as the first of the zone
@@ -380,6 +399,35 void VisualizationZoneWidget::dropMimeData(int index, const QMimeData *mimeData)
380 399 }
381 400 }
382 401
402 void VisualizationZoneWidget::dropMimeDataOnGraph(VisualizationDragWidget *dragWidget,
403 const QMimeData *mimeData)
404 {
405 auto graphWidget = qobject_cast<VisualizationGraphWidget *>(dragWidget);
406 if (!graphWidget) {
407 qCWarning(LOG_VisualizationZoneWidget())
408 << tr("VisualizationZoneWidget::dropMimeDataOnGraph, dropping in an unknown widget, "
409 "drop aborted");
410 Q_ASSERT(false);
411 return;
412 }
413
414 if (mimeData->hasFormat(MIME_TYPE_VARIABLE_LIST)) {
415 auto variables = sqpApp->variableController().variablesForMimeData(
416 mimeData->data(MIME_TYPE_VARIABLE_LIST));
417 for (const auto &var : variables) {
418 graphWidget->addVariable(var, graphWidget->graphRange());
419 }
420 }
421 else if (mimeData->hasFormat(MIME_TYPE_TIME_RANGE)) {
422 auto range = TimeController::timeRangeForMimeData(mimeData->data(MIME_TYPE_TIME_RANGE));
423 graphWidget->setGraphRange(range);
424 }
425 else {
426 qCWarning(LOG_VisualizationZoneWidget())
427 << tr("VisualizationZoneWidget::dropMimeDataOnGraph, unknown MIME data received.");
428 }
429 }
430
383 431 void VisualizationZoneWidget::VisualizationZoneWidgetPrivate::dropGraph(
384 432 int index, VisualizationZoneWidget *zoneWidget)
385 433 {
@@ -16,9 +16,15
16 16 <verstretch>0</verstretch>
17 17 </sizepolicy>
18 18 </property>
19 <property name="acceptDrops">
20 <bool>true</bool>
21 </property>
19 22 <property name="windowTitle">
20 23 <string>Form</string>
21 24 </property>
25 <property name="styleSheet">
26 <string notr="true">b</string>
27 </property>
22 28 <layout class="QHBoxLayout" name="horizontalLayout_2">
23 29 <item>
24 30 <widget class="QLabel" name="label">
@@ -15,7 +15,7
15 15 </property>
16 16 <layout class="QGridLayout" name="gridLayout">
17 17 <item row="0" column="0">
18 <widget class="QTableView" name="tableView">
18 <widget class="VariableInspectorTableView" name="tableView">
19 19 <property name="acceptDrops">
20 20 <bool>true</bool>
21 21 </property>
@@ -35,6 +35,13
35 35 </item>
36 36 </layout>
37 37 </widget>
38 <customwidgets>
39 <customwidget>
40 <class>VariableInspectorTableView</class>
41 <extends>QTableView</extends>
42 <header>Variable/VariableInspectorTableView.h</header>
43 </customwidget>
44 </customwidgets>
38 45 <resources/>
39 46 <connections/>
40 47 </ui>
@@ -63,7 +63,11
63 63 <number>0</number>
64 64 </property>
65 65 <item>
66 <widget class="VisualizationDragDropContainer" name="dragDropContainer" native="true"/>
66 <widget class="VisualizationDragDropContainer" name="dragDropContainer">
67 <property name="lineWidth">
68 <number>0</number>
69 </property>
70 </widget>
67 71 </item>
68 72 </layout>
69 73 </widget>
@@ -74,7 +78,7
74 78 <customwidgets>
75 79 <customwidget>
76 80 <class>VisualizationDragDropContainer</class>
77 <extends>QWidget</extends>
81 <extends>QFrame</extends>
78 82 <header>Visualization/VisualizationDragDropContainer.h</header>
79 83 <container>1</container>
80 84 </customwidget>
@@ -78,7 +78,7
78 78 </widget>
79 79 </item>
80 80 <item>
81 <widget class="QFrame" name="visualizationZoneFrame">
81 <widget class="VisualizationDragDropContainer" name="dragDropContainer">
82 82 <property name="sizePolicy">
83 83 <sizepolicy hsizetype="Preferred" vsizetype="Expanding">
84 84 <horstretch>0</horstretch>
@@ -107,9 +107,6
107 107 <property name="bottomMargin">
108 108 <number>0</number>
109 109 </property>
110 <item>
111 <widget class="VisualizationDragDropContainer" name="dragDropContainer" native="true"/>
112 </item>
113 110 </layout>
114 111 </widget>
115 112 </item>
@@ -118,7 +115,7
118 115 <customwidgets>
119 116 <customwidget>
120 117 <class>VisualizationDragDropContainer</class>
121 <extends>QWidget</extends>
118 <extends>QFrame</extends>
122 119 <header>Visualization/VisualizationDragDropContainer.h</header>
123 120 <container>1</container>
124 121 </customwidget>
General Comments 3
Under Review
author

Pull request updated. Auto status change to "Under Review"

Changed commits:
  * 1 added
  * 0 removed

Changed files:
  * A core/tests/meson.build
You need to be logged in to leave comments. Login now