From e09f4d47cdbcf7f139cb37f136b67a0a06169aac Mon Sep 17 00:00:00 2001 From: imonlyfourteen <155100608+imonlyfourteen@users.noreply.github.com> Date: Tue, 4 Jun 2024 08:35:27 +0000 Subject: [PATCH 1/2] Add middle mouse button drag in the GraphView Handle middle mouse button in `mouseMoveEvent()` (new) and `mousePressEvent()` methods. --- src/GUI/graphview.cpp | 25 +++++++++++++++++++++++++ src/GUI/graphview.h | 2 ++ 2 files changed, 27 insertions(+) diff --git a/src/GUI/graphview.cpp b/src/GUI/graphview.cpp index c2039142..30dc35e2 100644 --- a/src/GUI/graphview.cpp +++ b/src/GUI/graphview.cpp @@ -342,10 +342,35 @@ void GraphView::resizeEvent(QResizeEvent *e) QGraphicsView::resizeEvent(e); } +void GraphView::mouseMoveEvent(QMouseEvent *e) +{ + if (e->buttons() & Qt::MiddleButton) { + QScrollBar *sb = horizontalScrollBar(); +#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) + int x = e->x(); +#else // QT 5.15 + int x = e->position().toPoint().x(); +#endif // QT 5.15 + sb->setSliderPosition(sb->sliderPosition() - (x - _xStartDrag)); + _xStartDrag = x; + } + + QGraphicsView::mouseMoveEvent(e); +} + void GraphView::mousePressEvent(QMouseEvent *e) { +#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) if (e->button() == Qt::LeftButton) newSliderPosition(mapToScene(e->pos())); + else if (e->button() == Qt::MiddleButton) + _xStartDrag = e->x(); +#else // QT 5.15 + if (e->button() == Qt::LeftButton) + newSliderPosition(mapToScene(e->position().toPoint())); + else if (e->button() == Qt::MiddleButton) + _xStartDrag = e->position().toPoint().x(); +#endif // QT 5.15 QGraphicsView::mousePressEvent(e); } diff --git a/src/GUI/graphview.h b/src/GUI/graphview.h index 21ac3ecd..fc08100d 100644 --- a/src/GUI/graphview.h +++ b/src/GUI/graphview.h @@ -57,6 +57,7 @@ protected: void setUnits(Units units); void resizeEvent(QResizeEvent *e); + void mouseMoveEvent(QMouseEvent *e); void mousePressEvent(QMouseEvent *e); void wheelEvent(QWheelEvent *e); void changeEvent(QEvent *e); @@ -122,6 +123,7 @@ private: qreal _minYRange; qreal _zoom; + int _xStartDrag; }; #endif // GRAPHVIEW_H From 9bb04d7154cc226ccce73cd7f2b00e66aa7e5b46 Mon Sep 17 00:00:00 2001 From: imonlyfourteen <155100608+imonlyfourteen@users.noreply.github.com> Date: Tue, 4 Jun 2024 11:34:44 +0000 Subject: [PATCH 2/2] Add middle mouse button drag in the GraphView Handle middle mouse button in `mouseMoveEvent()` (new) and `mousePressEvent()` methods. (Removed preprocessor conditions; deprecated QMouseEvent::pos() / QMouseEvent::x() were chosen.) --- src/GUI/graphview.cpp | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/GUI/graphview.cpp b/src/GUI/graphview.cpp index 30dc35e2..08a6b946 100644 --- a/src/GUI/graphview.cpp +++ b/src/GUI/graphview.cpp @@ -346,11 +346,7 @@ void GraphView::mouseMoveEvent(QMouseEvent *e) { if (e->buttons() & Qt::MiddleButton) { QScrollBar *sb = horizontalScrollBar(); -#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) int x = e->x(); -#else // QT 5.15 - int x = e->position().toPoint().x(); -#endif // QT 5.15 sb->setSliderPosition(sb->sliderPosition() - (x - _xStartDrag)); _xStartDrag = x; } @@ -360,17 +356,10 @@ void GraphView::mouseMoveEvent(QMouseEvent *e) void GraphView::mousePressEvent(QMouseEvent *e) { -#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) if (e->button() == Qt::LeftButton) newSliderPosition(mapToScene(e->pos())); else if (e->button() == Qt::MiddleButton) _xStartDrag = e->x(); -#else // QT 5.15 - if (e->button() == Qt::LeftButton) - newSliderPosition(mapToScene(e->position().toPoint())); - else if (e->button() == Qt::MiddleButton) - _xStartDrag = e->position().toPoint().x(); -#endif // QT 5.15 QGraphicsView::mousePressEvent(e); }