1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-24 03:35:53 +01:00

Compare commits

..

2 Commits

Author SHA1 Message Date
40febb8c0e Use the correct super class 2022-04-28 22:36:04 +02:00
1e5b18d86a Added support for pinch zoom 2022-04-28 21:54:00 +02:00
2 changed files with 55 additions and 5 deletions

View File

@ -1,6 +1,7 @@
#include <QGraphicsView> #include <QGraphicsView>
#include <QGraphicsScene> #include <QGraphicsScene>
#include <QWheelEvent> #include <QWheelEvent>
#include <QGestureEvent>
#include <QApplication> #include <QApplication>
#include <QScrollBar> #include <QScrollBar>
#include <QClipboard> #include <QClipboard>
@ -49,6 +50,8 @@ MapView::MapView(Map *map, POI *poi, QGeoPositionInfoSource *source,
setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
setResizeAnchor(QGraphicsView::AnchorViewCenter); setResizeAnchor(QGraphicsView::AnchorViewCenter);
setAcceptDrops(false); setAcceptDrops(false);
viewport()->setAttribute(Qt::WA_AcceptTouchEvents);
grabGesture(Qt::PinchGesture);
_mapScale = new ScaleItem(); _mapScale = new ScaleItem();
_mapScale->setZValue(2.0); _mapScale->setZValue(2.0);
@ -125,6 +128,8 @@ MapView::MapView(Map *map, POI *poi, QGeoPositionInfoSource *source,
_opengl = false; _opengl = false;
_plot = false; _plot = false;
_digitalZoom = 0; _digitalZoom = 0;
_pinchZoom = 0;
_wheelDelta = 0;
_res = _map->resolution(_map->bounds()); _res = _map->resolution(_map->bounds());
_scene->setSceneRect(_map->bounds()); _scene->setSceneRect(_map->bounds());
@ -617,19 +622,40 @@ void MapView::zoom(int zoom, const QPoint &pos, bool shift)
} }
} }
void MapView::pinchGesture(QPinchGesture *gesture)
{
QPinchGesture::ChangeFlags changeFlags = gesture->changeFlags();
qreal scaleFactor = gesture->totalScaleFactor();
if (changeFlags & QPinchGesture::ScaleFactorChanged) {
int z = 0;
for (qreal sc = scaleFactor; sc > 1.25; sc *= 0.8)
z += 1;
for (qreal sc = scaleFactor; sc < 0.8; sc *= 1.25)
z -= 1;
if (_pinchZoom != z) {
zoom(z - _pinchZoom, gesture->centerPoint().toPoint(), false);
_pinchZoom = z;
}
}
if (gesture->state() == Qt::GestureFinished)
_pinchZoom = 0;
}
void MapView::wheelEvent(QWheelEvent *event) void MapView::wheelEvent(QWheelEvent *event)
{ {
static int deg8 = 0;
bool shift = (event->modifiers() & MODIFIER) ? true : false; bool shift = (event->modifiers() & MODIFIER) ? true : false;
// Shift inverts the wheel axis on OS X, so use scrolling in both axes for // Shift inverts the wheel axis on OS X, so use scrolling in both axes for
// the zoom. // the zoom.
int delta = event->angleDelta().y() int delta = event->angleDelta().y()
? event->angleDelta().y() : event->angleDelta().x(); ? event->angleDelta().y() : event->angleDelta().x();
deg8 += delta; _wheelDelta += delta;
if (qAbs(deg8) < (15 * 8)) if (qAbs(_wheelDelta) < (15 * 8))
return; return;
deg8 = deg8 % (15 * 8); _wheelDelta = _wheelDelta % (15 * 8);
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
zoom((delta > 0) ? 1 : -1, event->pos(), shift); zoom((delta > 0) ? 1 : -1, event->pos(), shift);
@ -1186,6 +1212,22 @@ void MapView::leaveEvent(QEvent *event)
QGraphicsView::leaveEvent(event); QGraphicsView::leaveEvent(event);
} }
bool MapView::event(QEvent *event)
{
if (event->type() == QEvent::Gesture)
return gestureEvent(static_cast<QGestureEvent*>(event));
return QGraphicsView::event(event);
}
bool MapView::gestureEvent(QGestureEvent *event)
{
if (QGesture *pinch = event->gesture(Qt::PinchGesture))
pinchGesture(static_cast<QPinchGesture *>(pinch));
return true;
}
void MapView::useOpenGL(bool use) void MapView::useOpenGL(bool use)
{ {
_opengl = use; _opengl = use;

View File

@ -19,6 +19,8 @@
class QGeoPositionInfoSource; class QGeoPositionInfoSource;
class QGeoPositionInfo; class QGeoPositionInfo;
class QGestureEvent;
class QPinchGesture;
class Data; class Data;
class POI; class POI;
class Map; class Map;
@ -147,6 +149,8 @@ private:
void zoom(int zoom, const QPoint &pos, bool shift); void zoom(int zoom, const QPoint &pos, bool shift);
void digitalZoom(int zoom); void digitalZoom(int zoom);
void updatePOIVisibility(); void updatePOIVisibility();
bool gestureEvent(QGestureEvent *event);
void pinchGesture(QPinchGesture *gesture);
void skipColor() {_palette.nextColor();} void skipColor() {_palette.nextColor();}
void mouseMoveEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event);
@ -157,8 +161,9 @@ private:
void keyReleaseEvent(QKeyEvent *event); void keyReleaseEvent(QKeyEvent *event);
void drawBackground(QPainter *painter, const QRectF &rect); void drawBackground(QPainter *painter, const QRectF &rect);
void paintEvent(QPaintEvent *event); void paintEvent(QPaintEvent *event);
void scrollContentsBy(int dx, int dy);
void leaveEvent(QEvent *event); void leaveEvent(QEvent *event);
bool event(QEvent *event);
void scrollContentsBy(int dx, int dy);
GraphicsScene *_scene; GraphicsScene *_scene;
ScaleItem *_mapScale; ScaleItem *_mapScale;
@ -201,6 +206,9 @@ private:
qreal _deviceRatio; qreal _deviceRatio;
qreal _mapRatio; qreal _mapRatio;
bool _opengl; bool _opengl;
int _pinchZoom;
int _wheelDelta;
}; };
#endif // MAPVIEW_H #endif // MAPVIEW_H