From 059c5151757abe7ba323fa25bd766f81b0f4394f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Thu, 29 Jun 2023 07:22:11 +0200 Subject: [PATCH] Added graph pinch zooming Fixes #501 --- src/GUI/graphview.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ src/GUI/graphview.h | 5 +++++ 2 files changed, 45 insertions(+) diff --git a/src/GUI/graphview.cpp b/src/GUI/graphview.cpp index 652689b4..3c958cb2 100644 --- a/src/GUI/graphview.cpp +++ b/src/GUI/graphview.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -37,6 +38,8 @@ GraphView::GraphView(QWidget *parent) setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setBackgroundBrush(QBrush(palette().brush(QPalette::Base))); + viewport()->setAttribute(Qt::WA_AcceptTouchEvents); + grabGesture(Qt::PinchGesture); _xAxis = new AxisItem(AxisItem::X); _xAxis->setZValue(1.0); @@ -381,6 +384,27 @@ void GraphView::wheelEvent(QWheelEvent *e) QGraphicsView::wheelEvent(e); } +void GraphView::pinchGesture(QPinchGesture *gesture) +{ + QPinchGesture::ChangeFlags changeFlags = gesture->changeFlags(); + + if (changeFlags & QPinchGesture::ScaleFactorChanged) { + QPointF pos = mapToScene(gesture->centerPoint().toPoint()); + QRectF gr(_grid->boundingRect()); + QPointF r(pos.x() / gr.width(), pos.y() / gr.height()); + + _zoom = qMax(_zoom * gesture->scaleFactor(), 1.0); + redraw(); + + QRectF ngr(_grid->boundingRect()); + QPointF npos(mapFromScene(QPointF(r.x() * ngr.width(), + r.y() * ngr.height()))); + QScrollBar *sb = horizontalScrollBar(); + sb->setSliderPosition(sb->sliderPosition() + npos.x() + - gesture->centerPoint().x()); + } +} + void GraphView::paintEvent(QPaintEvent *e) { QRectF viewRect(mapToScene(rect()).boundingRect()); @@ -577,3 +601,19 @@ void GraphView::changeEvent(QEvent *e) QGraphicsView::changeEvent(e); } + +bool GraphView::event(QEvent *event) +{ + if (event->type() == QEvent::Gesture) + return gestureEvent(static_cast(event)); + + return QGraphicsView::event(event); +} + +bool GraphView::gestureEvent(QGestureEvent *event) +{ + if (QGesture *pinch = event->gesture(Qt::PinchGesture)) + pinchGesture(static_cast(pinch)); + + return true; +} diff --git a/src/GUI/graphview.h b/src/GUI/graphview.h index d81dc4a1..21ac3ecd 100644 --- a/src/GUI/graphview.h +++ b/src/GUI/graphview.h @@ -18,6 +18,8 @@ class PathItem; class GridItem; class QGraphicsSimpleTextItem; class GraphicsScene; +class QGestureEvent; +class QPinchGesture; class GraphView : public QGraphicsView { @@ -59,6 +61,7 @@ protected: void wheelEvent(QWheelEvent *e); void changeEvent(QEvent *e); void paintEvent(QPaintEvent *e); + bool event(QEvent *event); const QString &yLabel() const {return _yLabel;} const QString &yUnits() const {return _yUnits;} @@ -94,6 +97,8 @@ private: void removeItem(QGraphicsItem *item); void addItem(QGraphicsItem *item); bool singleGraph() const; + bool gestureEvent(QGestureEvent *event); + void pinchGesture(QPinchGesture *gesture); GraphicsScene *_scene;