diff options
author | Denis Dzyubenko <denis.dzyubenko@nokia.com> | 2009-10-30 16:14:53 (GMT) |
---|---|---|
committer | Denis Dzyubenko <denis.dzyubenko@nokia.com> | 2009-11-03 10:26:05 (GMT) |
commit | 54db3afc9797a3adf5a38f35f6338960924e9e1a (patch) | |
tree | 7ef984555683aeeba5e8963ae9206786181d08f1 | |
parent | b7b4df9c87805be372fa6f74422e0f4648a5d520 (diff) | |
download | Qt-54db3afc9797a3adf5a38f35f6338960924e9e1a.zip Qt-54db3afc9797a3adf5a38f35f6338960924e9e1a.tar.gz Qt-54db3afc9797a3adf5a38f35f6338960924e9e1a.tar.bz2 |
Implemented pinch gesture recognizer.
Reviewed-by: trustme
-rw-r--r-- | examples/gestures/imagegestures/imagewidget.cpp | 18 | ||||
-rw-r--r-- | src/gui/kernel/qgesture_p.h | 3 | ||||
-rw-r--r-- | src/gui/kernel/qgesturemanager.cpp | 1 | ||||
-rw-r--r-- | src/gui/kernel/qstandardgestures.cpp | 177 | ||||
-rw-r--r-- | src/gui/kernel/qstandardgestures_p.h | 11 | ||||
-rw-r--r-- | src/gui/kernel/qwidget.cpp | 1 | ||||
-rw-r--r-- | src/gui/kernel/qwidget_win.cpp | 11 | ||||
-rw-r--r-- | src/gui/kernel/qwinnativepangesturerecognizer_win.cpp | 8 | ||||
-rw-r--r-- | src/gui/kernel/qwinnativepangesturerecognizer_win_p.h | 2 |
9 files changed, 130 insertions, 102 deletions
diff --git a/examples/gestures/imagegestures/imagewidget.cpp b/examples/gestures/imagegestures/imagewidget.cpp index 28de6da..c798fcc 100644 --- a/examples/gestures/imagegestures/imagewidget.cpp +++ b/examples/gestures/imagegestures/imagewidget.cpp @@ -102,17 +102,13 @@ void ImageWidget::mouseDoubleClickEvent(QMouseEvent *) //! [gesture event handler] bool ImageWidget::gestureEvent(QGestureEvent *event) { - if (QGesture *pan = event->gesture(Qt::PanGesture)) { - panTriggered(static_cast<QPanGesture*>(pan)); - return true; - } else if (QGesture *pinch = event->gesture(Qt::PinchGesture)) { - pinchTriggered(static_cast<QPinchGesture*>(pinch)); - return true; - } else if (QGesture *swipe = event->gesture(Qt::SwipeGesture)) { - swipeTriggered(static_cast<QSwipeGesture*>(swipe)); - return true; - } - return false; + if (QGesture *pan = event->gesture(Qt::PanGesture)) + panTriggered(static_cast<QPanGesture *>(pan)); + if (QGesture *pinch = event->gesture(Qt::PinchGesture)) + pinchTriggered(static_cast<QPinchGesture *>(pinch)); + if (QGesture *swipe = event->gesture(Qt::SwipeGesture)) + swipeTriggered(static_cast<QSwipeGesture *>(swipe)); + return true; } //! [gesture event handler] diff --git a/src/gui/kernel/qgesture_p.h b/src/gui/kernel/qgesture_p.h index 34fbb26..73a6bcf 100644 --- a/src/gui/kernel/qgesture_p.h +++ b/src/gui/kernel/qgesture_p.h @@ -121,6 +121,9 @@ public: qreal totalRotationAngle; qreal lastRotationAngle; qreal rotationAngle; + + bool isNewSequence; + QPointF startPosition[2]; }; class QSwipeGesturePrivate : public QGesturePrivate diff --git a/src/gui/kernel/qgesturemanager.cpp b/src/gui/kernel/qgesturemanager.cpp index 1d33c84..6ea350b 100644 --- a/src/gui/kernel/qgesturemanager.cpp +++ b/src/gui/kernel/qgesturemanager.cpp @@ -77,6 +77,7 @@ QGestureManager::QGestureManager(QObject *parent) #endif #else registerGestureRecognizer(new QPanGestureRecognizer); + registerGestureRecognizer(new QPinchGestureRecognizer); #endif } diff --git a/src/gui/kernel/qstandardgestures.cpp b/src/gui/kernel/qstandardgestures.cpp index dec2311..57cf12d 100644 --- a/src/gui/kernel/qstandardgestures.cpp +++ b/src/gui/kernel/qstandardgestures.cpp @@ -44,6 +44,7 @@ #include "qgesture_p.h" #include "qevent.h" #include "qwidget.h" +#include "qabstractscrollarea.h" QT_BEGIN_NAMESPACE @@ -54,7 +55,13 @@ QPanGestureRecognizer::QPanGestureRecognizer() QGesture *QPanGestureRecognizer::createGesture(QObject *target) { if (target && target->isWidgetType()) { +#if defined(Q_OS_WIN) + // for scroll areas on Windows we want to use native gestures instead + if (!qobject_cast<QAbstractScrollArea *>(target->parent())) + static_cast<QWidget *>(target)->setAttribute(Qt::WA_AcceptTouchEvents); +#else static_cast<QWidget *>(target)->setAttribute(Qt::WA_AcceptTouchEvents); +#endif } return new QPanGesture; } @@ -132,104 +139,124 @@ void QPanGestureRecognizer::reset(QGesture *state) d->lastPosition = QPoint(); d->acceleration = 0; -//#if defined(QT_MAC_USE_COCOA) -// d->singleTouchPanTimer.stop(); -// d->prevMousePos = QPointF(0, 0); -//#endif - QGestureRecognizer::reset(state); } -/* -bool QPanGestureRecognizer::event(QEvent *event) + +// +// QPinchGestureRecognizer +// + +QPinchGestureRecognizer::QPinchGestureRecognizer() { -#if defined(QT_MAC_USE_COCOA) - Q_D(QPanGesture); - if (event->type() == QEvent::Timer) { - const QTimerEvent *te = static_cast<QTimerEvent *>(event); - if (te->timerId() == d->singleTouchPanTimer.timerId()) { - d->singleTouchPanTimer.stop(); - updateState(Qt::GestureStarted); - } +} + +QGesture *QPinchGestureRecognizer::createGesture(QObject *target) +{ + if (target && target->isWidgetType()) { + static_cast<QWidget *>(target)->setAttribute(Qt::WA_AcceptTouchEvents); } -#endif + return new QPinchGesture; +} - bool consume = false; +QGestureRecognizer::Result QPinchGestureRecognizer::filterEvent(QGesture *state, QObject *, QEvent *event) +{ + QPinchGesture *q = static_cast<QPinchGesture *>(state); + QPinchGesturePrivate *d = q->d_func(); -#if defined(Q_WS_WIN) -#elif defined(QT_MAC_USE_COCOA) - // The following implements single touch - // panning on Mac: - const int panBeginDelay = 300; - const int panBeginRadius = 3; - const QTouchEvent *ev = static_cast<const QTouchEvent*>(event); + const QTouchEvent *ev = static_cast<const QTouchEvent *>(event); + + QGestureRecognizer::Result result; switch (event->type()) { case QEvent::TouchBegin: { - if (ev->touchPoints().size() == 1) { - d->delayManager->setEnabled(true); - consume = d->delayManager->append(d->gestureTarget, *event); - d->lastPosition = QCursor::pos(); - d->singleTouchPanTimer.start(panBeginDelay, this); - } - break;} + result = QGestureRecognizer::MaybeGesture; + break; + } case QEvent::TouchEnd: { - d->delayManager->setEnabled(false); - if (state() != Qt::NoGesture) { - updateState(Qt::GestureFinished); - consume = true; - d->delayManager->clear(); + if (q->state() != Qt::NoGesture) { + result = QGestureRecognizer::GestureFinished; } else { - d->delayManager->replay(); + result = QGestureRecognizer::NotGesture; } - reset(); - break;} + break; + } case QEvent::TouchUpdate: { - consume = d->delayManager->append(d->gestureTarget, *event); - if (ev->touchPoints().size() == 1) { - if (state() == Qt::NoGesture) { - // INVARIANT: The singleTouchTimer has still not fired. - // Lets check if the user moved his finger so much from - // the starting point that it makes sense to cancel: - const QPointF startPos = ev->touchPoints().at(0).startPos().toPoint(); - const QPointF p = ev->touchPoints().at(0).pos().toPoint(); - if ((startPos - p).manhattanLength() > panBeginRadius) { - d->delayManager->replay(); - consume = false; - reset(); - } else { - d->lastPosition = QCursor::pos(); - } + d->whatChanged = 0; + if (ev->touchPoints().size() == 2) { + QTouchEvent::TouchPoint p1 = ev->touchPoints().at(0); + QTouchEvent::TouchPoint p2 = ev->touchPoints().at(1); + + d->hotSpot = p1.screenPos(); + d->isHotSpotSet = true; + + if (d->isNewSequence) { + d->startPosition[0] = p1.screenPos(); + d->startPosition[1] = p2.screenPos(); + } + QLineF line(p1.screenPos(), p2.screenPos()); + QLineF tmp(line); + tmp.setLength(line.length() / 2.); + QPointF centerPoint = tmp.p2(); + + d->lastCenterPoint = d->centerPoint; + d->centerPoint = centerPoint; + d->whatChanged |= QPinchGesture::CenterPointChanged; + + const qreal scaleFactor = QLineF(p1.pos(), p2.pos()).length() + / QLineF(d->startPosition[0], d->startPosition[1]).length(); + if (d->isNewSequence) { + d->lastScaleFactor = scaleFactor; } else { - d->delayManager->clear(); - QPointF mousePos = QCursor::pos(); - QPointF dist = mousePos - d->lastPosition; - d->lastPosition = mousePos; - d->lastOffset = d->offset; - d->offset = QSizeF(dist.x(), dist.y()); - d->totalOffset += d->offset; - updateState(Qt::GestureUpdated); + d->lastScaleFactor = d->scaleFactor; } - } else if (state() == Qt::NoGesture) { - d->delayManager->replay(); - consume = false; - reset(); + d->scaleFactor = scaleFactor; + d->totalScaleFactor += d->scaleFactor - d->lastScaleFactor; + d->whatChanged |= QPinchGesture::ScaleFactorChanged; + + const qreal rotationAngle = -line.angle(); + if (d->isNewSequence) + d->lastRotationAngle = rotationAngle; + else + d->lastRotationAngle = d->rotationAngle; + d->rotationAngle = rotationAngle; + d->totalRotationAngle += d->rotationAngle - d->lastRotationAngle; + d->whatChanged |= QPinchGesture::RotationAngleChanged; + + d->isNewSequence = false; + result = QGestureRecognizer::GestureTriggered; + } else { + d->isNewSequence = true; + result = QGestureRecognizer::MaybeGesture; } - break;} + break; + } case QEvent::MouseButtonPress: case QEvent::MouseMove: case QEvent::MouseButtonRelease: - if (d->delayManager->isEnabled()) - consume = d->delayManager->append(d->gestureTarget, *event); + result = QGestureRecognizer::Ignore; break; default: - return false; + result = QGestureRecognizer::Ignore; + break; } -#else - Q_UNUSED(event); -#endif - return QGestureRecognizer::Ignore; + return result; +} + +void QPinchGestureRecognizer::reset(QGesture *state) +{ + QPinchGesture *pinch = static_cast<QPinchGesture*>(state); + QPinchGesturePrivate *d = pinch->d_func(); + + d->whatChanged = 0; + + d->startCenterPoint = d->lastCenterPoint = d->centerPoint = QPointF(); + d->totalScaleFactor = d->lastScaleFactor = d->scaleFactor = 0; + d->totalRotationAngle = d->lastRotationAngle = d->rotationAngle = 0; + + d->isNewSequence = true; + + QGestureRecognizer::reset(state); } - */ QT_END_NAMESPACE diff --git a/src/gui/kernel/qstandardgestures_p.h b/src/gui/kernel/qstandardgestures_p.h index fec5c2f..827b2a2 100644 --- a/src/gui/kernel/qstandardgestures_p.h +++ b/src/gui/kernel/qstandardgestures_p.h @@ -69,6 +69,17 @@ public: void reset(QGesture *state); }; +class QPinchGestureRecognizer : public QGestureRecognizer +{ +public: + QPinchGestureRecognizer(); + + QGesture *createGesture(QObject *target); + + QGestureRecognizer::Result filterEvent(QGesture *state, QObject *watched, QEvent *event); + void reset(QGesture *state); +}; + QT_END_NAMESPACE #endif // QSTANDARDGESTURES_P_H diff --git a/src/gui/kernel/qwidget.cpp b/src/gui/kernel/qwidget.cpp index ac2808c..00d9a99 100644 --- a/src/gui/kernel/qwidget.cpp +++ b/src/gui/kernel/qwidget.cpp @@ -197,6 +197,7 @@ QWidgetPrivate::QWidgetPrivate(int version) , picture(0) #elif defined(Q_WS_WIN) , noPaintOnScreen(0) + , nativeGesturePanEnabled(0) #elif defined(Q_WS_MAC) , needWindowChange(0) , isGLWidget(0) diff --git a/src/gui/kernel/qwidget_win.cpp b/src/gui/kernel/qwidget_win.cpp index 5bf7649..3a92735 100644 --- a/src/gui/kernel/qwidget_win.cpp +++ b/src/gui/kernel/qwidget_win.cpp @@ -2071,17 +2071,6 @@ void QWidgetPrivate::winSetupGestures() gc[0].dwBlock = GC_PAN; } -// gc[1].dwID = GID_ZOOM; -// if (gestures.pinch) -// gc[1].dwWant = GC_ZOOM; -// else -// gc[1].dwBlock = GC_ZOOM; -// gc[2].dwID = GID_ROTATE; -// if (gestures.pinch) -// gc[2].dwWant = GC_ROTATE; -// else -// gc[2].dwBlock = GC_ROTATE; - qAppPriv->SetGestureConfig(winid, 0, sizeof(gc)/sizeof(gc[0]), gc, sizeof(gc[0])); } } diff --git a/src/gui/kernel/qwinnativepangesturerecognizer_win.cpp b/src/gui/kernel/qwinnativepangesturerecognizer_win.cpp index 12d3058..c3c8a28 100644 --- a/src/gui/kernel/qwinnativepangesturerecognizer_win.cpp +++ b/src/gui/kernel/qwinnativepangesturerecognizer_win.cpp @@ -56,16 +56,16 @@ QWinNativePanGestureRecognizer::QWinNativePanGestureRecognizer() { } -QGesture* QWinNativePanGestureRecognizer::createGesture(QObject *target) const +QGesture *QWinNativePanGestureRecognizer::createGesture(QObject *target) { if (!target) return new QPanGesture; // a special case - if (qobject_cast<QGraphicsObject*>(target)) - return 0; if (!target->isWidgetType()) return 0; + if (qobject_cast<QGraphicsObject *>(target)) + return 0; - QWidget *q = static_cast<QWidget*>(target); + QWidget *q = static_cast<QWidget *>(target); QWidgetPrivate *d = q->d_func(); d->nativeGesturePanEnabled = true; d->winSetupGestures(); diff --git a/src/gui/kernel/qwinnativepangesturerecognizer_win_p.h b/src/gui/kernel/qwinnativepangesturerecognizer_win_p.h index a1e8511..1d723da 100644 --- a/src/gui/kernel/qwinnativepangesturerecognizer_win_p.h +++ b/src/gui/kernel/qwinnativepangesturerecognizer_win_p.h @@ -62,7 +62,7 @@ class QWinNativePanGestureRecognizer : public QGestureRecognizer public: QWinNativePanGestureRecognizer(); - QGesture* createGesture(QObject *target) const; + QGesture *createGesture(QObject *target); QGestureRecognizer::Result filterEvent(QGesture *state, QObject *watched, QEvent *event); void reset(QGesture *state); |