From 038cb3b97393ede6e0dc8d9e788108265b58bfd3 Mon Sep 17 00:00:00 2001 From: Jon Beniston Date: Tue, 20 Dec 2022 11:31:01 +0000 Subject: [PATCH] Add pinch gesture to GraphicsViewZoom --- sdrgui/gui/graphicsviewzoom.cpp | 17 +++++++++++++++++ sdrgui/gui/graphicsviewzoom.h | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/sdrgui/gui/graphicsviewzoom.cpp b/sdrgui/gui/graphicsviewzoom.cpp index a210cfb07..4ab6daca5 100644 --- a/sdrgui/gui/graphicsviewzoom.cpp +++ b/sdrgui/gui/graphicsviewzoom.cpp @@ -18,6 +18,9 @@ #include #include #include +#include +#include + #include #include "graphicsviewzoom.h" @@ -30,6 +33,7 @@ GraphicsViewZoom::GraphicsViewZoom(QGraphicsView* view) : { m_view->viewport()->installEventFilter(this); m_view->setMouseTracking(true); + m_view->viewport()->grabGesture(Qt::PinchGesture); } void GraphicsViewZoom::gentleZoom(double factor) @@ -79,6 +83,19 @@ bool GraphicsViewZoom::eventFilter(QObject *object, QEvent *event) } } } + else if (event->type() == QEvent::Gesture) + { + QGestureEvent *gestureEvent = static_cast(event); + if (QPinchGesture *pinchGesture = static_cast(gestureEvent->gesture(Qt::PinchGesture))) + { + if (pinchGesture->changeFlags() & QPinchGesture::ScaleFactorChanged) + { + m_view->scale(pinchGesture->scaleFactor(), pinchGesture->scaleFactor()); + emit zoomed(); + } + return true; + } + } Q_UNUSED(object) return false; diff --git a/sdrgui/gui/graphicsviewzoom.h b/sdrgui/gui/graphicsviewzoom.h index 7751b4454..0e4af0e6b 100644 --- a/sdrgui/gui/graphicsviewzoom.h +++ b/sdrgui/gui/graphicsviewzoom.h @@ -23,7 +23,7 @@ #include "export.h" -// GraphicsView that allows scroll wheel to be used for zoom +// GraphicsView that allows scroll wheel and pinch gesture to be used for zoom // https://stackoverflow.com/questions/19113532/qgraphicsview-zooming-in-and-out-under-mouse-position-using-mouse-wheel class SDRGUI_API GraphicsViewZoom : public QObject { Q_OBJECT @@ -39,7 +39,7 @@ private: Qt::KeyboardModifiers m_modifiers; double m_zoomFactorBase; QPointF m_targetScenePos, m_targetViewportPos; - bool eventFilter(QObject* object, QEvent* event); + bool eventFilter(QObject* object, QEvent* event) override; signals: void zoomed();