1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2025-01-19 04:02:09 +01:00

Added graph pinch zooming

Fixes #501
This commit is contained in:
Martin Tůma 2023-06-29 07:22:11 +02:00
parent 1dc963b133
commit 059c515175
2 changed files with 45 additions and 0 deletions

View File

@ -2,6 +2,7 @@
#include <QGraphicsScene>
#include <QEvent>
#include <QMouseEvent>
#include <QGestureEvent>
#include <QScrollBar>
#include <QGraphicsSimpleTextItem>
#include <QPalette>
@ -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<QGestureEvent*>(event));
return QGraphicsView::event(event);
}
bool GraphView::gestureEvent(QGestureEvent *event)
{
if (QGesture *pinch = event->gesture(Qt::PinchGesture))
pinchGesture(static_cast<QPinchGesture *>(pinch));
return true;
}

View File

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