##// END OF EJS Templates
Implements zoom box interaction mode
trabillard -
r1002:6b01e312fcf2
parent child
Show More
@@ -34,6 +34,9 const auto PAN_SPEED = 5;
34 34 /// Key pressed to enable a calibration pan
35 35 const auto VERTICAL_PAN_MODIFIER = Qt::AltModifier;
36 36
37 /// Minimum size for the zoom box, in percentage of the axis range
38 const auto ZOOM_BOX_MIN_SIZE = 0.8;
39
37 40 } // namespace
38 41
39 42 struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate {
@@ -53,6 +56,46 struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate {
53 56 bool m_IsCalibration;
54 57 /// Delegate used to attach rendering features to the plot
55 58 std::unique_ptr<VisualizationGraphRenderingDelegate> m_RenderingDelegate;
59
60 QCPItemRect *m_DrawingRect = nullptr;
61
62 void configureDrawingRect()
63 {
64 if (m_DrawingRect) {
65 QPen p;
66 p.setWidth(2);
67 m_DrawingRect->setPen(p);
68 }
69 }
70
71 void startDrawingRect(const QPoint &pos, QCustomPlot &plot)
72 {
73 removeDrawingRect(plot);
74
75 auto axisPos = posToAxisPos(pos, plot);
76
77 m_DrawingRect = new QCPItemRect{&plot};
78 configureDrawingRect();
79
80 m_DrawingRect->topLeft->setCoords(axisPos);
81 m_DrawingRect->bottomRight->setCoords(axisPos);
82 }
83
84 void removeDrawingRect(QCustomPlot &plot)
85 {
86 if (m_DrawingRect) {
87 plot.removeItem(m_DrawingRect); // the item is deleted by QCustomPlot
88 m_DrawingRect = nullptr;
89 plot.replot(QCustomPlot::rpQueuedReplot);
90 }
91 }
92
93 QPointF posToAxisPos(const QPoint &pos, QCustomPlot &plot) const
94 {
95 auto axisX = plot.axisRect()->axis(QCPAxis::atBottom);
96 auto axisY = plot.axisRect()->axis(QCPAxis::atLeft);
97 return QPointF{axisX->pixelToCoord(pos.x()), axisY->pixelToCoord(pos.y())};
98 }
56 99 };
57 100
58 101 VisualizationGraphWidget::VisualizationGraphWidget(const QString &name, QWidget *parent)
@@ -344,6 +387,11 void VisualizationGraphWidget::onMouseMove(QMouseEvent *event) noexcept
344 387 // Handles plot rendering when mouse is moving
345 388 impl->m_RenderingDelegate->onMouseMove(event);
346 389
390 if (impl->m_DrawingRect) {
391 auto axisPos = impl->posToAxisPos(event->pos(), plot());
392 impl->m_DrawingRect->bottomRight->setCoords(axisPos);
393 }
394
347 395 VisualizationDragWidget::mouseMoveEvent(event);
348 396 }
349 397
@@ -380,11 +428,37 void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept
380 428
381 429 void VisualizationGraphWidget::onMousePress(QMouseEvent *event) noexcept
382 430 {
431 if (sqpApp->plotsInteractionMode() == SqpApplication::PlotsInteractionMode::ZoomBox) {
432 impl->startDrawingRect(event->pos(), plot());
433 }
434
383 435 VisualizationDragWidget::mousePressEvent(event);
384 436 }
385 437
386 438 void VisualizationGraphWidget::onMouseRelease(QMouseEvent *event) noexcept
387 439 {
440 if (impl->m_DrawingRect) {
441
442 auto axisX = plot().axisRect()->axis(QCPAxis::atBottom);
443 auto axisY = plot().axisRect()->axis(QCPAxis::atLeft);
444
445 auto newAxisXRange = QCPRange{impl->m_DrawingRect->topLeft->coords().x(),
446 impl->m_DrawingRect->bottomRight->coords().x()};
447
448 auto newAxisYRange = QCPRange{impl->m_DrawingRect->topLeft->coords().y(),
449 impl->m_DrawingRect->bottomRight->coords().y()};
450
451 impl->removeDrawingRect(plot());
452
453 if (newAxisXRange.size() > axisX->range().size() * (ZOOM_BOX_MIN_SIZE / 100.0)
454 && newAxisYRange.size() > axisY->range().size() * (ZOOM_BOX_MIN_SIZE / 100.0)) {
455 axisX->setRange(newAxisXRange);
456 axisY->setRange(newAxisYRange);
457
458 plot().replot(QCustomPlot::rpQueuedReplot);
459 }
460 }
461
388 462 impl->m_IsCalibration = false;
389 463 }
390 464
General Comments 1
Under Review
author

Auto status change to "Under Review"

You need to be logged in to leave comments. Login now