Add pinch gesture to GraphicsViewZoom

This commit is contained in:
Jon Beniston 2022-12-20 11:31:01 +00:00
parent 7e7a9fdb84
commit 038cb3b973
2 changed files with 19 additions and 2 deletions

View File

@ -18,6 +18,9 @@
#include <QMouseEvent>
#include <QApplication>
#include <QScrollBar>
#include <QGestureEvent>
#include <QPinchGesture>
#include <qmath.h>
#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<QGestureEvent *>(event);
if (QPinchGesture *pinchGesture = static_cast<QPinchGesture *>(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;

View File

@ -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();