##// END OF EJS Templates
Calls the update of the graph's units and range data of a variable have been loaded
Alexandre Leroux -
r1351:04056f776dd6
parent child
Show More
@@ -1,1055 +1,1054
1 1 #include "Visualization/VisualizationGraphWidget.h"
2 2 #include "Visualization/IVisualizationWidgetVisitor.h"
3 3 #include "Visualization/VisualizationCursorItem.h"
4 4 #include "Visualization/VisualizationDefs.h"
5 5 #include "Visualization/VisualizationGraphHelper.h"
6 6 #include "Visualization/VisualizationGraphRenderingDelegate.h"
7 7 #include "Visualization/VisualizationMultiZoneSelectionDialog.h"
8 8 #include "Visualization/VisualizationSelectionZoneItem.h"
9 9 #include "Visualization/VisualizationSelectionZoneManager.h"
10 10 #include "Visualization/VisualizationWidget.h"
11 11 #include "Visualization/VisualizationZoneWidget.h"
12 12 #include "ui_VisualizationGraphWidget.h"
13 13
14 14 #include <Actions/ActionsGuiController.h>
15 15 #include <Common/MimeTypesDef.h>
16 16 #include <Data/ArrayData.h>
17 17 #include <Data/IDataSeries.h>
18 18 #include <Data/SpectrogramSeries.h>
19 19 #include <DragAndDrop/DragDropGuiController.h>
20 20 #include <Settings/SqpSettingsDefs.h>
21 21 #include <SqpApplication.h>
22 22 #include <Time/TimeController.h>
23 23 #include <Variable/Variable.h>
24 24 #include <Variable/VariableController.h>
25 25
26 26 #include <unordered_map>
27 27
28 28 Q_LOGGING_CATEGORY(LOG_VisualizationGraphWidget, "VisualizationGraphWidget")
29 29
30 30 namespace {
31 31
32 32 /// Key pressed to enable drag&drop in all modes
33 33 const auto DRAG_DROP_MODIFIER = Qt::AltModifier;
34 34
35 35 /// Key pressed to enable zoom on horizontal axis
36 36 const auto HORIZONTAL_ZOOM_MODIFIER = Qt::ControlModifier;
37 37
38 38 /// Key pressed to enable zoom on vertical axis
39 39 const auto VERTICAL_ZOOM_MODIFIER = Qt::ShiftModifier;
40 40
41 41 /// Speed of a step of a wheel event for a pan, in percentage of the axis range
42 42 const auto PAN_SPEED = 5;
43 43
44 44 /// Key pressed to enable a calibration pan
45 45 const auto VERTICAL_PAN_MODIFIER = Qt::AltModifier;
46 46
47 47 /// Key pressed to enable multi selection of selection zones
48 48 const auto MULTI_ZONE_SELECTION_MODIFIER = Qt::ControlModifier;
49 49
50 50 /// Minimum size for the zoom box, in percentage of the axis range
51 51 const auto ZOOM_BOX_MIN_SIZE = 0.8;
52 52
53 53 /// Format of the dates appearing in the label of a cursor
54 54 const auto CURSOR_LABELS_DATETIME_FORMAT = QStringLiteral("yyyy/MM/dd\nhh:mm:ss:zzz");
55 55
56 56 } // namespace
57 57
58 58 struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate {
59 59
60 60 explicit VisualizationGraphWidgetPrivate(const QString &name)
61 61 : m_Name{name},
62 62 m_Flags{GraphFlag::EnableAll},
63 63 m_IsCalibration{false},
64 64 m_RenderingDelegate{nullptr}
65 65 {
66 66 }
67 67
68 68 void updateData(PlottablesMap &plottables, std::shared_ptr<Variable> variable,
69 69 const SqpRange &range)
70 70 {
71 71 VisualizationGraphHelper::updateData(plottables, variable, range);
72 72
73 73 // Prevents that data has changed to update rendering
74 74 m_RenderingDelegate->onPlotUpdated();
75 75 }
76 76
77 77 QString m_Name;
78 78 // 1 variable -> n qcpplot
79 79 std::map<std::shared_ptr<Variable>, PlottablesMap> m_VariableToPlotMultiMap;
80 80 GraphFlags m_Flags;
81 81 bool m_IsCalibration;
82 82 /// Delegate used to attach rendering features to the plot
83 83 std::unique_ptr<VisualizationGraphRenderingDelegate> m_RenderingDelegate;
84 84
85 85 QCPItemRect *m_DrawingZoomRect = nullptr;
86 86 QStack<QPair<QCPRange, QCPRange> > m_ZoomStack;
87 87
88 88 std::unique_ptr<VisualizationCursorItem> m_HorizontalCursor = nullptr;
89 89 std::unique_ptr<VisualizationCursorItem> m_VerticalCursor = nullptr;
90 90
91 91 VisualizationSelectionZoneItem *m_DrawingZone = nullptr;
92 92 VisualizationSelectionZoneItem *m_HoveredZone = nullptr;
93 93 QVector<VisualizationSelectionZoneItem *> m_SelectionZones;
94 94
95 95 bool m_HasMovedMouse = false; // Indicates if the mouse moved in a releaseMouse even
96 96
97 97 bool m_VariableAutoRangeOnInit = true;
98 98
99 99 void startDrawingRect(const QPoint &pos, QCustomPlot &plot)
100 100 {
101 101 removeDrawingRect(plot);
102 102
103 103 auto axisPos = posToAxisPos(pos, plot);
104 104
105 105 m_DrawingZoomRect = new QCPItemRect{&plot};
106 106 QPen p;
107 107 p.setWidth(2);
108 108 m_DrawingZoomRect->setPen(p);
109 109
110 110 m_DrawingZoomRect->topLeft->setCoords(axisPos);
111 111 m_DrawingZoomRect->bottomRight->setCoords(axisPos);
112 112 }
113 113
114 114 void removeDrawingRect(QCustomPlot &plot)
115 115 {
116 116 if (m_DrawingZoomRect) {
117 117 plot.removeItem(m_DrawingZoomRect); // the item is deleted by QCustomPlot
118 118 m_DrawingZoomRect = nullptr;
119 119 plot.replot(QCustomPlot::rpQueuedReplot);
120 120 }
121 121 }
122 122
123 123 void startDrawingZone(const QPoint &pos, VisualizationGraphWidget *graph)
124 124 {
125 125 endDrawingZone(graph);
126 126
127 127 auto axisPos = posToAxisPos(pos, graph->plot());
128 128
129 129 m_DrawingZone = new VisualizationSelectionZoneItem{&graph->plot()};
130 130 m_DrawingZone->setRange(axisPos.x(), axisPos.x());
131 131 m_DrawingZone->setEditionEnabled(false);
132 132 }
133 133
134 134 void endDrawingZone(VisualizationGraphWidget *graph)
135 135 {
136 136 if (m_DrawingZone) {
137 137 auto drawingZoneRange = m_DrawingZone->range();
138 138 if (qAbs(drawingZoneRange.m_TEnd - drawingZoneRange.m_TStart) > 0) {
139 139 m_DrawingZone->setEditionEnabled(true);
140 140 addSelectionZone(m_DrawingZone);
141 141 }
142 142 else {
143 143 graph->plot().removeItem(m_DrawingZone); // the item is deleted by QCustomPlot
144 144 }
145 145
146 146 graph->plot().replot(QCustomPlot::rpQueuedReplot);
147 147 m_DrawingZone = nullptr;
148 148 }
149 149 }
150 150
151 151 void setSelectionZonesEditionEnabled(bool value)
152 152 {
153 153 for (auto s : m_SelectionZones) {
154 154 s->setEditionEnabled(value);
155 155 }
156 156 }
157 157
158 158 void addSelectionZone(VisualizationSelectionZoneItem *zone) { m_SelectionZones << zone; }
159 159
160 160 VisualizationSelectionZoneItem *selectionZoneAt(const QPoint &pos,
161 161 const QCustomPlot &plot) const
162 162 {
163 163 VisualizationSelectionZoneItem *selectionZoneItemUnderCursor = nullptr;
164 164 auto minDistanceToZone = -1;
165 165 for (auto zone : m_SelectionZones) {
166 166 auto distanceToZone = zone->selectTest(pos, false);
167 167 if ((minDistanceToZone < 0 || distanceToZone <= minDistanceToZone)
168 168 && distanceToZone >= 0 && distanceToZone < plot.selectionTolerance()) {
169 169 selectionZoneItemUnderCursor = zone;
170 170 }
171 171 }
172 172
173 173 return selectionZoneItemUnderCursor;
174 174 }
175 175
176 176 QVector<VisualizationSelectionZoneItem *> selectionZonesAt(const QPoint &pos,
177 177 const QCustomPlot &plot) const
178 178 {
179 179 QVector<VisualizationSelectionZoneItem *> zones;
180 180 for (auto zone : m_SelectionZones) {
181 181 auto distanceToZone = zone->selectTest(pos, false);
182 182 if (distanceToZone >= 0 && distanceToZone < plot.selectionTolerance()) {
183 183 zones << zone;
184 184 }
185 185 }
186 186
187 187 return zones;
188 188 }
189 189
190 190 void moveSelectionZoneOnTop(VisualizationSelectionZoneItem *zone, QCustomPlot &plot)
191 191 {
192 192 if (!m_SelectionZones.isEmpty() && m_SelectionZones.last() != zone) {
193 193 zone->moveToTop();
194 194 m_SelectionZones.removeAll(zone);
195 195 m_SelectionZones.append(zone);
196 196 }
197 197 }
198 198
199 199 QPointF posToAxisPos(const QPoint &pos, QCustomPlot &plot) const
200 200 {
201 201 auto axisX = plot.axisRect()->axis(QCPAxis::atBottom);
202 202 auto axisY = plot.axisRect()->axis(QCPAxis::atLeft);
203 203 return QPointF{axisX->pixelToCoord(pos.x()), axisY->pixelToCoord(pos.y())};
204 204 }
205 205
206 206 bool pointIsInAxisRect(const QPointF &axisPoint, QCustomPlot &plot) const
207 207 {
208 208 auto axisX = plot.axisRect()->axis(QCPAxis::atBottom);
209 209 auto axisY = plot.axisRect()->axis(QCPAxis::atLeft);
210 210 return axisX->range().contains(axisPoint.x()) && axisY->range().contains(axisPoint.y());
211 211 }
212 212 };
213 213
214 214 VisualizationGraphWidget::VisualizationGraphWidget(const QString &name, QWidget *parent)
215 215 : VisualizationDragWidget{parent},
216 216 ui{new Ui::VisualizationGraphWidget},
217 217 impl{spimpl::make_unique_impl<VisualizationGraphWidgetPrivate>(name)}
218 218 {
219 219 ui->setupUi(this);
220 220
221 221 // 'Close' options : widget is deleted when closed
222 222 setAttribute(Qt::WA_DeleteOnClose);
223 223
224 224 // Set qcpplot properties :
225 225 // - zoom is enabled
226 226 // - Mouse wheel on qcpplot is intercepted to determine the zoom orientation
227 227 ui->widget->setInteractions(QCP::iRangeZoom);
228 228 ui->widget->axisRect()->setRangeDrag(Qt::Horizontal | Qt::Vertical);
229 229
230 230 // The delegate must be initialized after the ui as it uses the plot
231 231 impl->m_RenderingDelegate = std::make_unique<VisualizationGraphRenderingDelegate>(*this);
232 232
233 233 // Init the cursors
234 234 impl->m_HorizontalCursor = std::make_unique<VisualizationCursorItem>(&plot());
235 235 impl->m_HorizontalCursor->setOrientation(Qt::Horizontal);
236 236 impl->m_VerticalCursor = std::make_unique<VisualizationCursorItem>(&plot());
237 237 impl->m_VerticalCursor->setOrientation(Qt::Vertical);
238 238
239 239 connect(ui->widget, &QCustomPlot::mousePress, this, &VisualizationGraphWidget::onMousePress);
240 240 connect(ui->widget, &QCustomPlot::mouseRelease, this,
241 241 &VisualizationGraphWidget::onMouseRelease);
242 242 connect(ui->widget, &QCustomPlot::mouseMove, this, &VisualizationGraphWidget::onMouseMove);
243 243 connect(ui->widget, &QCustomPlot::mouseWheel, this, &VisualizationGraphWidget::onMouseWheel);
244 244 connect(ui->widget, &QCustomPlot::mouseDoubleClick, this,
245 245 &VisualizationGraphWidget::onMouseDoubleClick);
246 246 connect(ui->widget->xAxis, static_cast<void (QCPAxis::*)(const QCPRange &, const QCPRange &)>(
247 247 &QCPAxis::rangeChanged),
248 248 this, &VisualizationGraphWidget::onRangeChanged, Qt::DirectConnection);
249 249
250 250 // Activates menu when right clicking on the graph
251 251 ui->widget->setContextMenuPolicy(Qt::CustomContextMenu);
252 252 connect(ui->widget, &QCustomPlot::customContextMenuRequested, this,
253 253 &VisualizationGraphWidget::onGraphMenuRequested);
254 254
255 255 connect(this, &VisualizationGraphWidget::requestDataLoading, &sqpApp->variableController(),
256 256 &VariableController::onRequestDataLoading);
257 257
258 258 connect(&sqpApp->variableController(), &VariableController::updateVarDisplaying, this,
259 259 &VisualizationGraphWidget::onUpdateVarDisplaying);
260 260
261 261 #ifdef Q_OS_MAC
262 262 plot().setPlottingHint(QCP::phFastPolylines, true);
263 263 #endif
264 264 }
265 265
266 266
267 267 VisualizationGraphWidget::~VisualizationGraphWidget()
268 268 {
269 269 delete ui;
270 270 }
271 271
272 272 VisualizationZoneWidget *VisualizationGraphWidget::parentZoneWidget() const noexcept
273 273 {
274 274 auto parent = parentWidget();
275 275 while (parent != nullptr && !qobject_cast<VisualizationZoneWidget *>(parent)) {
276 276 parent = parent->parentWidget();
277 277 }
278 278
279 279 return qobject_cast<VisualizationZoneWidget *>(parent);
280 280 }
281 281
282 282 VisualizationWidget *VisualizationGraphWidget::parentVisualizationWidget() const
283 283 {
284 284 auto parent = parentWidget();
285 285 while (parent != nullptr && !qobject_cast<VisualizationWidget *>(parent)) {
286 286 parent = parent->parentWidget();
287 287 }
288 288
289 289 return qobject_cast<VisualizationWidget *>(parent);
290 290 }
291 291
292 292 void VisualizationGraphWidget::setFlags(GraphFlags flags)
293 293 {
294 294 impl->m_Flags = std::move(flags);
295 295 }
296 296
297 297 void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable, SqpRange range)
298 298 {
299 299 /// Lambda used to set graph's units and range according to the variable passed in parameter
300 300 auto loadRange = [this](std::shared_ptr<Variable> variable, const SqpRange &range) {
301 301 impl->m_RenderingDelegate->setAxesUnits(*variable);
302 302
303 303 this->setFlags(GraphFlag::DisableAll);
304 304 setGraphRange(range);
305 305 this->setFlags(GraphFlag::EnableAll);
306
307 306 emit requestDataLoading({variable}, range, false);
308 307 };
309 308
310 309 connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated()));
311 310
312 311 // Calls update of graph's range and units when the data of the variable have been initialized.
313 312 // Note: we use QueuedConnection here as the update event must be called in the UI thread
314 313 connect(variable.get(), &Variable::dataInitialized, this,
315 314 [ varW = std::weak_ptr<Variable>{variable}, range, loadRange, this ]() {
316 315 if (auto var = varW.lock()) {
317 316 // If the variable is the first added in the graph, we load its range
318 317 auto firstVariableInGraph = range == INVALID_RANGE;
319 318 auto loadedRange = graphRange();
320 319 if (impl->m_VariableAutoRangeOnInit) {
321 320 loadedRange = firstVariableInGraph ? var->range() : range;
322 321 }
323 322 loadRange(var, loadedRange);
324 323 setYRange(var);
325 324 }
326 325 },
327 326 Qt::QueuedConnection);
328 327
329 328 // Uses delegate to create the qcpplot components according to the variable
330 329 auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget);
331 330
332 331 // Sets graph properties
333 332 impl->m_RenderingDelegate->setGraphProperties(*variable, createdPlottables);
334 333
335 334 impl->m_VariableToPlotMultiMap.insert({variable, std::move(createdPlottables)});
336 335
337 336 // If the variable already has its data loaded, load its units and its range in the graph
338 337 if (variable->dataSeries() != nullptr) {
339 338 loadRange(variable, range);
340 339 }
341 340
342 341 emit variableAdded(variable);
343 342 }
344 343
345 344 void VisualizationGraphWidget::removeVariable(std::shared_ptr<Variable> variable) noexcept
346 345 {
347 346 // Each component associated to the variable :
348 347 // - is removed from qcpplot (which deletes it)
349 348 // - is no longer referenced in the map
350 349 auto variableIt = impl->m_VariableToPlotMultiMap.find(variable);
351 350 if (variableIt != impl->m_VariableToPlotMultiMap.cend()) {
352 351 emit variableAboutToBeRemoved(variable);
353 352
354 353 auto &plottablesMap = variableIt->second;
355 354
356 355 for (auto plottableIt = plottablesMap.cbegin(), plottableEnd = plottablesMap.cend();
357 356 plottableIt != plottableEnd;) {
358 357 ui->widget->removePlottable(plottableIt->second);
359 358 plottableIt = plottablesMap.erase(plottableIt);
360 359 }
361 360
362 361 impl->m_VariableToPlotMultiMap.erase(variableIt);
363 362 }
364 363
365 364 // Updates graph
366 365 ui->widget->replot();
367 366 }
368 367
369 368 QList<std::shared_ptr<Variable> > VisualizationGraphWidget::variables() const
370 369 {
371 370 auto variables = QList<std::shared_ptr<Variable> >{};
372 371 for (auto it = std::cbegin(impl->m_VariableToPlotMultiMap);
373 372 it != std::cend(impl->m_VariableToPlotMultiMap); ++it) {
374 373 variables << it->first;
375 374 }
376 375
377 376 return variables;
378 377 }
379 378
380 379 void VisualizationGraphWidget::setYRange(std::shared_ptr<Variable> variable)
381 380 {
382 381 if (!variable) {
383 382 qCCritical(LOG_VisualizationGraphWidget()) << "Can't set y-axis range: variable is null";
384 383 return;
385 384 }
386 385
387 386 VisualizationGraphHelper::setYAxisRange(variable, *ui->widget);
388 387 }
389 388
390 389 SqpRange VisualizationGraphWidget::graphRange() const noexcept
391 390 {
392 391 auto graphRange = ui->widget->xAxis->range();
393 392 return SqpRange{graphRange.lower, graphRange.upper};
394 393 }
395 394
396 395 void VisualizationGraphWidget::setGraphRange(const SqpRange &range, bool calibration)
397 396 {
398 397 qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::setGraphRange START");
399 398
400 399 if (calibration) {
401 400 impl->m_IsCalibration = true;
402 401 }
403 402
404 403 ui->widget->xAxis->setRange(range.m_TStart, range.m_TEnd);
405 404 ui->widget->replot();
406 405
407 406 if (calibration) {
408 407 impl->m_IsCalibration = false;
409 408 }
410 409
411 410 qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::setGraphRange END");
412 411 }
413 412
414 413 void VisualizationGraphWidget::setAutoRangeOnVariableInitialization(bool value)
415 414 {
416 415 impl->m_VariableAutoRangeOnInit = value;
417 416 }
418 417
419 418 QVector<SqpRange> VisualizationGraphWidget::selectionZoneRanges() const
420 419 {
421 420 QVector<SqpRange> ranges;
422 421 for (auto zone : impl->m_SelectionZones) {
423 422 ranges << zone->range();
424 423 }
425 424
426 425 return ranges;
427 426 }
428 427
429 428 void VisualizationGraphWidget::addSelectionZones(const QVector<SqpRange> &ranges)
430 429 {
431 430 for (const auto &range : ranges) {
432 431 // note: ownership is transfered to QCustomPlot
433 432 auto zone = new VisualizationSelectionZoneItem(&plot());
434 433 zone->setRange(range.m_TStart, range.m_TEnd);
435 434 impl->addSelectionZone(zone);
436 435 }
437 436
438 437 plot().replot(QCustomPlot::rpQueuedReplot);
439 438 }
440 439
441 440 VisualizationSelectionZoneItem *VisualizationGraphWidget::addSelectionZone(const QString &name,
442 441 const SqpRange &range)
443 442 {
444 443 // note: ownership is transfered to QCustomPlot
445 444 auto zone = new VisualizationSelectionZoneItem(&plot());
446 445 zone->setName(name);
447 446 zone->setRange(range.m_TStart, range.m_TEnd);
448 447 impl->addSelectionZone(zone);
449 448
450 449 plot().replot(QCustomPlot::rpQueuedReplot);
451 450
452 451 return zone;
453 452 }
454 453
455 454 void VisualizationGraphWidget::removeSelectionZone(VisualizationSelectionZoneItem *selectionZone)
456 455 {
457 456 parentVisualizationWidget()->selectionZoneManager().setSelected(selectionZone, false);
458 457
459 458 if (impl->m_HoveredZone == selectionZone) {
460 459 impl->m_HoveredZone = nullptr;
461 460 setCursor(Qt::ArrowCursor);
462 461 }
463 462
464 463 impl->m_SelectionZones.removeAll(selectionZone);
465 464 plot().removeItem(selectionZone);
466 465 plot().replot(QCustomPlot::rpQueuedReplot);
467 466 }
468 467
469 468 void VisualizationGraphWidget::undoZoom()
470 469 {
471 470 auto zoom = impl->m_ZoomStack.pop();
472 471 auto axisX = plot().axisRect()->axis(QCPAxis::atBottom);
473 472 auto axisY = plot().axisRect()->axis(QCPAxis::atLeft);
474 473
475 474 axisX->setRange(zoom.first);
476 475 axisY->setRange(zoom.second);
477 476
478 477 plot().replot(QCustomPlot::rpQueuedReplot);
479 478 }
480 479
481 480 void VisualizationGraphWidget::accept(IVisualizationWidgetVisitor *visitor)
482 481 {
483 482 if (visitor) {
484 483 visitor->visit(this);
485 484 }
486 485 else {
487 486 qCCritical(LOG_VisualizationGraphWidget())
488 487 << tr("Can't visit widget : the visitor is null");
489 488 }
490 489 }
491 490
492 491 bool VisualizationGraphWidget::canDrop(const Variable &variable) const
493 492 {
494 493 auto isSpectrogram = [](const auto &variable) {
495 494 return std::dynamic_pointer_cast<SpectrogramSeries>(variable.dataSeries()) != nullptr;
496 495 };
497 496
498 497 // - A spectrogram series can't be dropped on graph with existing plottables
499 498 // - No data series can be dropped on graph with existing spectrogram series
500 499 return isSpectrogram(variable)
501 500 ? impl->m_VariableToPlotMultiMap.empty()
502 501 : std::none_of(
503 502 impl->m_VariableToPlotMultiMap.cbegin(), impl->m_VariableToPlotMultiMap.cend(),
504 503 [isSpectrogram](const auto &entry) { return isSpectrogram(*entry.first); });
505 504 }
506 505
507 506 bool VisualizationGraphWidget::contains(const Variable &variable) const
508 507 {
509 508 // Finds the variable among the keys of the map
510 509 auto variablePtr = &variable;
511 510 auto findVariable
512 511 = [variablePtr](const auto &entry) { return variablePtr == entry.first.get(); };
513 512
514 513 auto end = impl->m_VariableToPlotMultiMap.cend();
515 514 auto it = std::find_if(impl->m_VariableToPlotMultiMap.cbegin(), end, findVariable);
516 515 return it != end;
517 516 }
518 517
519 518 QString VisualizationGraphWidget::name() const
520 519 {
521 520 return impl->m_Name;
522 521 }
523 522
524 523 QMimeData *VisualizationGraphWidget::mimeData(const QPoint &position) const
525 524 {
526 525 auto mimeData = new QMimeData;
527 526
528 527 auto selectionZoneItemUnderCursor = impl->selectionZoneAt(position, plot());
529 528 if (sqpApp->plotsInteractionMode() == SqpApplication::PlotsInteractionMode::SelectionZones
530 529 && selectionZoneItemUnderCursor) {
531 530 mimeData->setData(MIME_TYPE_TIME_RANGE, TimeController::mimeDataForTimeRange(
532 531 selectionZoneItemUnderCursor->range()));
533 532 mimeData->setData(MIME_TYPE_SELECTION_ZONE, TimeController::mimeDataForTimeRange(
534 533 selectionZoneItemUnderCursor->range()));
535 534 }
536 535 else {
537 536 mimeData->setData(MIME_TYPE_GRAPH, QByteArray{});
538 537
539 538 auto timeRangeData = TimeController::mimeDataForTimeRange(graphRange());
540 539 mimeData->setData(MIME_TYPE_TIME_RANGE, timeRangeData);
541 540 }
542 541
543 542 return mimeData;
544 543 }
545 544
546 545 QPixmap VisualizationGraphWidget::customDragPixmap(const QPoint &dragPosition)
547 546 {
548 547 auto selectionZoneItemUnderCursor = impl->selectionZoneAt(dragPosition, plot());
549 548 if (sqpApp->plotsInteractionMode() == SqpApplication::PlotsInteractionMode::SelectionZones
550 549 && selectionZoneItemUnderCursor) {
551 550
552 551 auto zoneTopLeft = selectionZoneItemUnderCursor->topLeft->pixelPosition();
553 552 auto zoneBottomRight = selectionZoneItemUnderCursor->bottomRight->pixelPosition();
554 553
555 554 auto zoneSize = QSizeF{qAbs(zoneBottomRight.x() - zoneTopLeft.x()),
556 555 qAbs(zoneBottomRight.y() - zoneTopLeft.y())}
557 556 .toSize();
558 557
559 558 auto pixmap = QPixmap(zoneSize);
560 559 render(&pixmap, QPoint(), QRegion{QRect{zoneTopLeft.toPoint(), zoneSize}});
561 560
562 561 return pixmap;
563 562 }
564 563
565 564 return QPixmap();
566 565 }
567 566
568 567 bool VisualizationGraphWidget::isDragAllowed() const
569 568 {
570 569 return true;
571 570 }
572 571
573 572 void VisualizationGraphWidget::highlightForMerge(bool highlighted)
574 573 {
575 574 if (highlighted) {
576 575 plot().setBackground(QBrush(QColor("#BBD5EE")));
577 576 }
578 577 else {
579 578 plot().setBackground(QBrush(Qt::white));
580 579 }
581 580
582 581 plot().update();
583 582 }
584 583
585 584 void VisualizationGraphWidget::addVerticalCursor(double time)
586 585 {
587 586 impl->m_VerticalCursor->setPosition(time);
588 587 impl->m_VerticalCursor->setVisible(true);
589 588
590 589 auto text
591 590 = DateUtils::dateTime(time).toString(CURSOR_LABELS_DATETIME_FORMAT).replace(' ', '\n');
592 591 impl->m_VerticalCursor->setLabelText(text);
593 592 }
594 593
595 594 void VisualizationGraphWidget::addVerticalCursorAtViewportPosition(double position)
596 595 {
597 596 impl->m_VerticalCursor->setAbsolutePosition(position);
598 597 impl->m_VerticalCursor->setVisible(true);
599 598
600 599 auto axis = plot().axisRect()->axis(QCPAxis::atBottom);
601 600 auto text
602 601 = DateUtils::dateTime(axis->pixelToCoord(position)).toString(CURSOR_LABELS_DATETIME_FORMAT);
603 602 impl->m_VerticalCursor->setLabelText(text);
604 603 }
605 604
606 605 void VisualizationGraphWidget::removeVerticalCursor()
607 606 {
608 607 impl->m_VerticalCursor->setVisible(false);
609 608 plot().replot(QCustomPlot::rpQueuedReplot);
610 609 }
611 610
612 611 void VisualizationGraphWidget::addHorizontalCursor(double value)
613 612 {
614 613 impl->m_HorizontalCursor->setPosition(value);
615 614 impl->m_HorizontalCursor->setVisible(true);
616 615 impl->m_HorizontalCursor->setLabelText(QString::number(value));
617 616 }
618 617
619 618 void VisualizationGraphWidget::addHorizontalCursorAtViewportPosition(double position)
620 619 {
621 620 impl->m_HorizontalCursor->setAbsolutePosition(position);
622 621 impl->m_HorizontalCursor->setVisible(true);
623 622
624 623 auto axis = plot().axisRect()->axis(QCPAxis::atLeft);
625 624 impl->m_HorizontalCursor->setLabelText(QString::number(axis->pixelToCoord(position)));
626 625 }
627 626
628 627 void VisualizationGraphWidget::removeHorizontalCursor()
629 628 {
630 629 impl->m_HorizontalCursor->setVisible(false);
631 630 plot().replot(QCustomPlot::rpQueuedReplot);
632 631 }
633 632
634 633 void VisualizationGraphWidget::closeEvent(QCloseEvent *event)
635 634 {
636 635 Q_UNUSED(event);
637 636
638 637 // Prevents that all variables will be removed from graph when it will be closed
639 638 for (auto &variableEntry : impl->m_VariableToPlotMultiMap) {
640 639 emit variableAboutToBeRemoved(variableEntry.first);
641 640 }
642 641 }
643 642
644 643 void VisualizationGraphWidget::enterEvent(QEvent *event)
645 644 {
646 645 Q_UNUSED(event);
647 646 impl->m_RenderingDelegate->showGraphOverlay(true);
648 647 }
649 648
650 649 void VisualizationGraphWidget::leaveEvent(QEvent *event)
651 650 {
652 651 Q_UNUSED(event);
653 652 impl->m_RenderingDelegate->showGraphOverlay(false);
654 653
655 654 if (auto parentZone = parentZoneWidget()) {
656 655 parentZone->notifyMouseLeaveGraph(this);
657 656 }
658 657 else {
659 658 qCWarning(LOG_VisualizationGraphWidget()) << "leaveEvent: No parent zone widget";
660 659 }
661 660
662 661 if (impl->m_HoveredZone) {
663 662 impl->m_HoveredZone->setHovered(false);
664 663 impl->m_HoveredZone = nullptr;
665 664 }
666 665 }
667 666
668 667 QCustomPlot &VisualizationGraphWidget::plot() const noexcept
669 668 {
670 669 return *ui->widget;
671 670 }
672 671
673 672 void VisualizationGraphWidget::onGraphMenuRequested(const QPoint &pos) noexcept
674 673 {
675 674 QMenu graphMenu{};
676 675
677 676 // Iterates on variables (unique keys)
678 677 for (auto it = impl->m_VariableToPlotMultiMap.cbegin(),
679 678 end = impl->m_VariableToPlotMultiMap.cend();
680 679 it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) {
681 680 // 'Remove variable' action
682 681 graphMenu.addAction(tr("Remove variable %1").arg(it->first->name()),
683 682 [ this, var = it->first ]() { removeVariable(var); });
684 683 }
685 684
686 685 if (!impl->m_ZoomStack.isEmpty()) {
687 686 if (!graphMenu.isEmpty()) {
688 687 graphMenu.addSeparator();
689 688 }
690 689
691 690 graphMenu.addAction(tr("Undo Zoom"), [this]() { undoZoom(); });
692 691 }
693 692
694 693 // Selection Zone Actions
695 694 auto selectionZoneItem = impl->selectionZoneAt(pos, plot());
696 695 if (selectionZoneItem) {
697 696 auto selectedItems = parentVisualizationWidget()->selectionZoneManager().selectedItems();
698 697 selectedItems.removeAll(selectionZoneItem);
699 698 selectedItems.prepend(selectionZoneItem); // Put the current selection zone first
700 699
701 700 auto zoneActions = sqpApp->actionsGuiController().selectionZoneActions();
702 701 if (!zoneActions.isEmpty() && !graphMenu.isEmpty()) {
703 702 graphMenu.addSeparator();
704 703 }
705 704
706 705 QHash<QString, QMenu *> subMenus;
707 706 QHash<QString, bool> subMenusEnabled;
708 707
709 708 for (auto zoneAction : zoneActions) {
710 709
711 710 auto isEnabled = zoneAction->isEnabled(selectedItems);
712 711
713 712 auto menu = &graphMenu;
714 713 for (auto subMenuName : zoneAction->subMenuList()) {
715 714 if (!subMenus.contains(subMenuName)) {
716 715 menu = menu->addMenu(subMenuName);
717 716 subMenus[subMenuName] = menu;
718 717 subMenusEnabled[subMenuName] = isEnabled;
719 718 }
720 719 else {
721 720 menu = subMenus.value(subMenuName);
722 721 if (isEnabled) {
723 722 // The sub menu is enabled if at least one of its actions is enabled
724 723 subMenusEnabled[subMenuName] = true;
725 724 }
726 725 }
727 726 }
728 727
729 728 auto action = menu->addAction(zoneAction->name());
730 729 action->setEnabled(isEnabled);
731 730 action->setShortcut(zoneAction->displayedShortcut());
732 731 QObject::connect(action, &QAction::triggered,
733 732 [zoneAction, selectedItems]() { zoneAction->execute(selectedItems); });
734 733 }
735 734
736 735 for (auto it = subMenus.cbegin(); it != subMenus.cend(); ++it) {
737 736 it.value()->setEnabled(subMenusEnabled[it.key()]);
738 737 }
739 738 }
740 739
741 740 if (!graphMenu.isEmpty()) {
742 741 graphMenu.exec(QCursor::pos());
743 742 }
744 743 }
745 744
746 745 void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1, const QCPRange &t2)
747 746 {
748 747 qCDebug(LOG_VisualizationGraphWidget()) << tr("TORM: VisualizationGraphWidget::onRangeChanged")
749 748 << QThread::currentThread()->objectName() << "DoAcqui"
750 749 << impl->m_Flags.testFlag(GraphFlag::EnableAcquisition);
751 750
752 751 auto graphRange = SqpRange{t1.lower, t1.upper};
753 752 auto oldGraphRange = SqpRange{t2.lower, t2.upper};
754 753
755 754 if (impl->m_Flags.testFlag(GraphFlag::EnableAcquisition)) {
756 755 QVector<std::shared_ptr<Variable> > variableUnderGraphVector;
757 756
758 757 for (auto it = impl->m_VariableToPlotMultiMap.begin(),
759 758 end = impl->m_VariableToPlotMultiMap.end();
760 759 it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) {
761 760 variableUnderGraphVector.push_back(it->first);
762 761 }
763 762 emit requestDataLoading(std::move(variableUnderGraphVector), graphRange,
764 763 !impl->m_IsCalibration);
765 764 }
766 765
767 766 if (impl->m_Flags.testFlag(GraphFlag::EnableSynchronization) && !impl->m_IsCalibration) {
768 767 qCDebug(LOG_VisualizationGraphWidget())
769 768 << tr("TORM: VisualizationGraphWidget::Synchronize notify !!")
770 769 << QThread::currentThread()->objectName() << graphRange << oldGraphRange;
771 770 emit synchronize(graphRange, oldGraphRange);
772 771 }
773 772
774 773 auto pos = mapFromGlobal(QCursor::pos());
775 774 auto axisPos = impl->posToAxisPos(pos, plot());
776 775 if (auto parentZone = parentZoneWidget()) {
777 776 if (impl->pointIsInAxisRect(axisPos, plot())) {
778 777 parentZone->notifyMouseMoveInGraph(pos, axisPos, this);
779 778 }
780 779 else {
781 780 parentZone->notifyMouseLeaveGraph(this);
782 781 }
783 782 }
784 783 else {
785 784 qCWarning(LOG_VisualizationGraphWidget()) << "onMouseMove: No parent zone widget";
786 785 }
787 786
788 787 // Quits calibration
789 788 impl->m_IsCalibration = false;
790 789 }
791 790
792 791 void VisualizationGraphWidget::onMouseDoubleClick(QMouseEvent *event) noexcept
793 792 {
794 793 impl->m_RenderingDelegate->onMouseDoubleClick(event);
795 794 }
796 795
797 796 void VisualizationGraphWidget::onMouseMove(QMouseEvent *event) noexcept
798 797 {
799 798 // Handles plot rendering when mouse is moving
800 799 impl->m_RenderingDelegate->onMouseMove(event);
801 800
802 801 auto axisPos = impl->posToAxisPos(event->pos(), plot());
803 802
804 803 // Zoom box and zone drawing
805 804 if (impl->m_DrawingZoomRect) {
806 805 impl->m_DrawingZoomRect->bottomRight->setCoords(axisPos);
807 806 }
808 807 else if (impl->m_DrawingZone) {
809 808 impl->m_DrawingZone->setEnd(axisPos.x());
810 809 }
811 810
812 811 // Cursor
813 812 if (auto parentZone = parentZoneWidget()) {
814 813 if (impl->pointIsInAxisRect(axisPos, plot())) {
815 814 parentZone->notifyMouseMoveInGraph(event->pos(), axisPos, this);
816 815 }
817 816 else {
818 817 parentZone->notifyMouseLeaveGraph(this);
819 818 }
820 819 }
821 820 else {
822 821 qCWarning(LOG_VisualizationGraphWidget()) << "onMouseMove: No parent zone widget";
823 822 }
824 823
825 824 // Search for the selection zone under the mouse
826 825 auto selectionZoneItemUnderCursor = impl->selectionZoneAt(event->pos(), plot());
827 826 if (selectionZoneItemUnderCursor && !impl->m_DrawingZone
828 827 && sqpApp->plotsInteractionMode() == SqpApplication::PlotsInteractionMode::SelectionZones) {
829 828
830 829 // Sets the appropriate cursor shape
831 830 auto cursorShape = selectionZoneItemUnderCursor->curshorShapeForPosition(event->pos());
832 831 setCursor(cursorShape);
833 832
834 833 // Manages the hovered zone
835 834 if (selectionZoneItemUnderCursor != impl->m_HoveredZone) {
836 835 if (impl->m_HoveredZone) {
837 836 impl->m_HoveredZone->setHovered(false);
838 837 }
839 838 selectionZoneItemUnderCursor->setHovered(true);
840 839 impl->m_HoveredZone = selectionZoneItemUnderCursor;
841 840 plot().replot(QCustomPlot::rpQueuedReplot);
842 841 }
843 842 }
844 843 else {
845 844 // There is no zone under the mouse or the interaction mode is not "selection zones"
846 845 if (impl->m_HoveredZone) {
847 846 impl->m_HoveredZone->setHovered(false);
848 847 impl->m_HoveredZone = nullptr;
849 848 }
850 849
851 850 setCursor(Qt::ArrowCursor);
852 851 }
853 852
854 853 impl->m_HasMovedMouse = true;
855 854 VisualizationDragWidget::mouseMoveEvent(event);
856 855 }
857 856
858 857 void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept
859 858 {
860 859 auto value = event->angleDelta().x() + event->angleDelta().y();
861 860 if (value != 0) {
862 861
863 862 auto direction = value > 0 ? 1.0 : -1.0;
864 863 auto isZoomX = event->modifiers().testFlag(HORIZONTAL_ZOOM_MODIFIER);
865 864 auto isZoomY = event->modifiers().testFlag(VERTICAL_ZOOM_MODIFIER);
866 865 impl->m_IsCalibration = event->modifiers().testFlag(VERTICAL_PAN_MODIFIER);
867 866
868 867 auto zoomOrientations = QFlags<Qt::Orientation>{};
869 868 zoomOrientations.setFlag(Qt::Horizontal, isZoomX);
870 869 zoomOrientations.setFlag(Qt::Vertical, isZoomY);
871 870
872 871 ui->widget->axisRect()->setRangeZoom(zoomOrientations);
873 872
874 873 if (!isZoomX && !isZoomY) {
875 874 auto axis = plot().axisRect()->axis(QCPAxis::atBottom);
876 875 auto diff = direction * (axis->range().size() * (PAN_SPEED / 100.0));
877 876
878 877 axis->setRange(axis->range() + diff);
879 878
880 879 if (plot().noAntialiasingOnDrag()) {
881 880 plot().setNotAntialiasedElements(QCP::aeAll);
882 881 }
883 882
884 883 plot().replot(QCustomPlot::rpQueuedReplot);
885 884 }
886 885 }
887 886 }
888 887
889 888 void VisualizationGraphWidget::onMousePress(QMouseEvent *event) noexcept
890 889 {
891 890 auto isDragDropClick = event->modifiers().testFlag(DRAG_DROP_MODIFIER);
892 891 auto isSelectionZoneMode
893 892 = sqpApp->plotsInteractionMode() == SqpApplication::PlotsInteractionMode::SelectionZones;
894 893 auto isLeftClick = event->buttons().testFlag(Qt::LeftButton);
895 894
896 895 if (!isDragDropClick && isLeftClick) {
897 896 if (sqpApp->plotsInteractionMode() == SqpApplication::PlotsInteractionMode::ZoomBox) {
898 897 // Starts a zoom box
899 898 impl->startDrawingRect(event->pos(), plot());
900 899 }
901 900 else if (isSelectionZoneMode && impl->m_DrawingZone == nullptr) {
902 901 // Starts a new selection zone
903 902 auto zoneAtPos = impl->selectionZoneAt(event->pos(), plot());
904 903 if (!zoneAtPos) {
905 904 impl->startDrawingZone(event->pos(), this);
906 905 }
907 906 }
908 907 }
909 908
910 909 // Allows mouse panning only in default mode
911 910 plot().setInteraction(QCP::iRangeDrag, sqpApp->plotsInteractionMode()
912 911 == SqpApplication::PlotsInteractionMode::None
913 912 && !isDragDropClick);
914 913
915 914 // Allows zone edition only in selection zone mode without drag&drop
916 915 impl->setSelectionZonesEditionEnabled(isSelectionZoneMode && !isDragDropClick);
917 916
918 917 // Selection / Deselection
919 918 if (isSelectionZoneMode) {
920 919 auto isMultiSelectionClick = event->modifiers().testFlag(MULTI_ZONE_SELECTION_MODIFIER);
921 920 auto selectionZoneItemUnderCursor = impl->selectionZoneAt(event->pos(), plot());
922 921
923 922
924 923 if (selectionZoneItemUnderCursor && !selectionZoneItemUnderCursor->selected()
925 924 && !isMultiSelectionClick) {
926 925 parentVisualizationWidget()->selectionZoneManager().select(
927 926 {selectionZoneItemUnderCursor});
928 927 }
929 928 else if (!selectionZoneItemUnderCursor && !isMultiSelectionClick && isLeftClick) {
930 929 parentVisualizationWidget()->selectionZoneManager().clearSelection();
931 930 }
932 931 else {
933 932 // No selection change
934 933 }
935 934
936 935 if (selectionZoneItemUnderCursor && isLeftClick) {
937 936 selectionZoneItemUnderCursor->setAssociatedEditedZones(
938 937 parentVisualizationWidget()->selectionZoneManager().selectedItems());
939 938 }
940 939 }
941 940
942 941
943 942 impl->m_HasMovedMouse = false;
944 943 VisualizationDragWidget::mousePressEvent(event);
945 944 }
946 945
947 946 void VisualizationGraphWidget::onMouseRelease(QMouseEvent *event) noexcept
948 947 {
949 948 if (impl->m_DrawingZoomRect) {
950 949
951 950 auto axisX = plot().axisRect()->axis(QCPAxis::atBottom);
952 951 auto axisY = plot().axisRect()->axis(QCPAxis::atLeft);
953 952
954 953 auto newAxisXRange = QCPRange{impl->m_DrawingZoomRect->topLeft->coords().x(),
955 954 impl->m_DrawingZoomRect->bottomRight->coords().x()};
956 955
957 956 auto newAxisYRange = QCPRange{impl->m_DrawingZoomRect->topLeft->coords().y(),
958 957 impl->m_DrawingZoomRect->bottomRight->coords().y()};
959 958
960 959 impl->removeDrawingRect(plot());
961 960
962 961 if (newAxisXRange.size() > axisX->range().size() * (ZOOM_BOX_MIN_SIZE / 100.0)
963 962 && newAxisYRange.size() > axisY->range().size() * (ZOOM_BOX_MIN_SIZE / 100.0)) {
964 963 impl->m_ZoomStack.push(qMakePair(axisX->range(), axisY->range()));
965 964 axisX->setRange(newAxisXRange);
966 965 axisY->setRange(newAxisYRange);
967 966
968 967 plot().replot(QCustomPlot::rpQueuedReplot);
969 968 }
970 969 }
971 970
972 971 impl->endDrawingZone(this);
973 972
974 973 // Selection / Deselection
975 974 auto isSelectionZoneMode
976 975 = sqpApp->plotsInteractionMode() == SqpApplication::PlotsInteractionMode::SelectionZones;
977 976 if (isSelectionZoneMode) {
978 977 auto isMultiSelectionClick = event->modifiers().testFlag(MULTI_ZONE_SELECTION_MODIFIER);
979 978 auto selectionZoneItemUnderCursor = impl->selectionZoneAt(event->pos(), plot());
980 979 if (selectionZoneItemUnderCursor && event->button() == Qt::LeftButton
981 980 && !impl->m_HasMovedMouse) {
982 981
983 982 auto zonesUnderCursor = impl->selectionZonesAt(event->pos(), plot());
984 983 if (zonesUnderCursor.count() > 1) {
985 984 // There are multiple zones under the mouse.
986 985 // Performs the selection with a selection dialog.
987 986 VisualizationMultiZoneSelectionDialog dialog{this};
988 987 dialog.setZones(zonesUnderCursor);
989 988 dialog.move(mapToGlobal(event->pos() - QPoint(dialog.width() / 2, 20)));
990 989 dialog.activateWindow();
991 990 dialog.raise();
992 991 if (dialog.exec() == QDialog::Accepted) {
993 992 auto selection = dialog.selectedZones();
994 993
995 994 if (!isMultiSelectionClick) {
996 995 parentVisualizationWidget()->selectionZoneManager().clearSelection();
997 996 }
998 997
999 998 for (auto it = selection.cbegin(); it != selection.cend(); ++it) {
1000 999 auto zone = it.key();
1001 1000 auto isSelected = it.value();
1002 1001 parentVisualizationWidget()->selectionZoneManager().setSelected(zone,
1003 1002 isSelected);
1004 1003
1005 1004 if (isSelected) {
1006 1005 // Puts the zone on top of the stack so it can be moved or resized
1007 1006 impl->moveSelectionZoneOnTop(zone, plot());
1008 1007 }
1009 1008 }
1010 1009 }
1011 1010 }
1012 1011 else {
1013 1012 if (!isMultiSelectionClick) {
1014 1013 parentVisualizationWidget()->selectionZoneManager().select(
1015 1014 {selectionZoneItemUnderCursor});
1016 1015 impl->moveSelectionZoneOnTop(selectionZoneItemUnderCursor, plot());
1017 1016 }
1018 1017 else {
1019 1018 parentVisualizationWidget()->selectionZoneManager().setSelected(
1020 1019 selectionZoneItemUnderCursor, !selectionZoneItemUnderCursor->selected()
1021 1020 || event->button() == Qt::RightButton);
1022 1021 }
1023 1022 }
1024 1023 }
1025 1024 else {
1026 1025 // No selection change
1027 1026 }
1028 1027 }
1029 1028 }
1030 1029
1031 1030 void VisualizationGraphWidget::onDataCacheVariableUpdated()
1032 1031 {
1033 1032 auto graphRange = ui->widget->xAxis->range();
1034 1033 auto dateTime = SqpRange{graphRange.lower, graphRange.upper};
1035 1034
1036 1035 for (auto &variableEntry : impl->m_VariableToPlotMultiMap) {
1037 1036 auto variable = variableEntry.first;
1038 1037 qCDebug(LOG_VisualizationGraphWidget())
1039 1038 << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated S" << variable->range();
1040 1039 qCDebug(LOG_VisualizationGraphWidget())
1041 1040 << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated E" << dateTime;
1042 1041 if (dateTime.contains(variable->range()) || dateTime.intersect(variable->range())) {
1043 1042 impl->updateData(variableEntry.second, variable, variable->range());
1044 1043 }
1045 1044 }
1046 1045 }
1047 1046
1048 1047 void VisualizationGraphWidget::onUpdateVarDisplaying(std::shared_ptr<Variable> variable,
1049 1048 const SqpRange &range)
1050 1049 {
1051 1050 auto it = impl->m_VariableToPlotMultiMap.find(variable);
1052 1051 if (it != impl->m_VariableToPlotMultiMap.end()) {
1053 1052 impl->updateData(it->second, variable, range);
1054 1053 }
1055 1054 }
General Comments 0
You need to be logged in to leave comments. Login now