diff options
author | Denis Dzyubenko <denis.dzyubenko@nokia.com> | 2009-11-11 10:02:09 (GMT) |
---|---|---|
committer | Jason McDonald <jason.mcdonald@nokia.com> | 2009-11-13 12:04:21 (GMT) |
commit | a81c7773fa8d0778fd63fab730f1320d12b80630 (patch) | |
tree | 66775fc8e6fd3224f16f80505487dc41e97a54f8 /examples | |
parent | fc3f0bcb2acf0d802c74ff4977fb1228d059e879 (diff) | |
download | Qt-a81c7773fa8d0778fd63fab730f1320d12b80630.zip Qt-a81c7773fa8d0778fd63fab730f1320d12b80630.tar.gz Qt-a81c7773fa8d0778fd63fab730f1320d12b80630.tar.bz2 |
Pinchzoom example zooms more smooth
Remember the scale factor so it won't reset each time a new touch sequence
starts.
Reviewed-by: Bradley T. Hughes
(cherry picked from commit 791c4035c28257e5a8426e6519866546faaf5286)
Diffstat (limited to 'examples')
-rw-r--r-- | examples/multitouch/pinchzoom/graphicsview.cpp | 20 | ||||
-rw-r--r-- | examples/multitouch/pinchzoom/graphicsview.h | 3 |
2 files changed, 18 insertions, 5 deletions
diff --git a/examples/multitouch/pinchzoom/graphicsview.cpp b/examples/multitouch/pinchzoom/graphicsview.cpp index 0c79073..00e620a 100644 --- a/examples/multitouch/pinchzoom/graphicsview.cpp +++ b/examples/multitouch/pinchzoom/graphicsview.cpp @@ -45,7 +45,7 @@ #include <QTouchEvent> GraphicsView::GraphicsView(QGraphicsScene *scene, QWidget *parent) - : QGraphicsView(scene, parent) + : QGraphicsView(scene, parent), totalScaleFactor(1) { viewport()->setAttribute(Qt::WA_AcceptTouchEvents); setDragMode(ScrollHandDrag); @@ -58,14 +58,24 @@ bool GraphicsView::viewportEvent(QEvent *event) case QEvent::TouchUpdate: case QEvent::TouchEnd: { - QList<QTouchEvent::TouchPoint> touchPoints = static_cast<QTouchEvent *>(event)->touchPoints(); + QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event); + QList<QTouchEvent::TouchPoint> touchPoints = touchEvent->touchPoints(); if (touchPoints.count() == 2) { // determine scale factor const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first(); const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last(); - const qreal scaleFactor = QLineF(touchPoint0.pos(), touchPoint1.pos()).length() - / QLineF(touchPoint0.startPos(), touchPoint1.startPos()).length(); - setTransform(QTransform().scale(scaleFactor, scaleFactor)); + qreal currentScaleFactor = + QLineF(touchPoint0.pos(), touchPoint1.pos()).length() + / QLineF(touchPoint0.startPos(), touchPoint1.startPos()).length(); + if (touchEvent->touchPointStates() & Qt::TouchPointReleased) { + // if one of the fingers is released, remember the current scale + // factor so that adding another finger later will continue zooming + // by adding new scale factor to the existing remembered value. + totalScaleFactor *= currentScaleFactor; + currentScaleFactor = 1; + } + setTransform(QTransform().scale(totalScaleFactor * currentScaleFactor, + totalScaleFactor * currentScaleFactor)); } return true; } diff --git a/examples/multitouch/pinchzoom/graphicsview.h b/examples/multitouch/pinchzoom/graphicsview.h index c5195cd..81a115d 100644 --- a/examples/multitouch/pinchzoom/graphicsview.h +++ b/examples/multitouch/pinchzoom/graphicsview.h @@ -50,4 +50,7 @@ public: GraphicsView(QGraphicsScene *scene = 0, QWidget *parent = 0); bool viewportEvent(QEvent *event); + +private: + qreal totalScaleFactor; }; |