diff options
-rw-r--r-- | src/gui/kernel/qgesture.cpp | 48 | ||||
-rw-r--r-- | src/gui/kernel/qgesture.h | 2 | ||||
-rw-r--r-- | src/gui/kernel/qstandardgestures.cpp | 43 | ||||
-rw-r--r-- | src/gui/kernel/qwidget_win.cpp | 2 |
4 files changed, 55 insertions, 40 deletions
diff --git a/src/gui/kernel/qgesture.cpp b/src/gui/kernel/qgesture.cpp index 8075ad9..38e8851 100644 --- a/src/gui/kernel/qgesture.cpp +++ b/src/gui/kernel/qgesture.cpp @@ -197,11 +197,44 @@ Qt::GestureState QGesture::state() const } /*! - Sets this gesture's recognition state to \a state. + Sets this gesture's recognition state to \a state and emits appropriate + signals. + + This functions emits the signals according to the old state and the new + \a state, and it should be called after all the internal properties have been + initialized. + + \sa started, triggered, finished, cancelled */ -void QGesture::setState(Qt::GestureState state) +void QGesture::updateState(Qt::GestureState state) { - d_func()->state = state; + Q_D(QGesture); + if (d->state == state) { + if (state == Qt::GestureUpdated) + emit triggered(); + return; + } + const Qt::GestureState oldState = d->state; + d->state = state; + if (state != Qt::NoGesture && oldState > state) { + // comparing the state as ints: state should only be changed from + // started to (optionally) updated and to finished. + qWarning("QGesture::updateState: incorrect new state"); + return; + } + if (oldState == Qt::NoGesture) + emit started(); + if (state == Qt::GestureUpdated) + emit triggered(); + else if (state == Qt::GestureFinished) + emit finished(); + else if (state == Qt::NoGesture) + emit cancelled(); + + if (state == Qt::GestureFinished) { + // gesture is finished, so we reset the internal state. + d->state = Qt::NoGesture; + } } /*! @@ -238,14 +271,13 @@ QGraphicsItem* QGesture::graphicsItem() const Resets the internal state of the gesture. This function might be called by the filterEvent() implementation in a derived class, or by the user to - cancel a gesture. The base class implementation emits the cancelled() - signal if the state() of the gesture wasn't empty. + cancel a gesture. The base class implementation calls + updateState(Qt::NoGesture) which emits the cancelled() + signal if the state() of the gesture indicated it was active. */ void QGesture::reset() { - if (state() != Qt::NoGesture) - emit cancelled(); - setState(Qt::NoGesture); + updateState(Qt::NoGesture); } QT_END_NAMESPACE diff --git a/src/gui/kernel/qgesture.h b/src/gui/kernel/qgesture.h index 0735160..7da37c4 100644 --- a/src/gui/kernel/qgesture.h +++ b/src/gui/kernel/qgesture.h @@ -81,7 +81,7 @@ protected: QGesture(QGesturePrivate &dd, QObject *parent); bool eventFilter(QObject*, QEvent*); - void setState(Qt::GestureState state); + void updateState(Qt::GestureState state); Q_SIGNALS: void started(); diff --git a/src/gui/kernel/qstandardgestures.cpp b/src/gui/kernel/qstandardgestures.cpp index 2a5e7e8..89de7c8 100644 --- a/src/gui/kernel/qstandardgestures.cpp +++ b/src/gui/kernel/qstandardgestures.cpp @@ -114,9 +114,7 @@ bool QPanGesture::event(QEvent *event) killTimer(d->panFinishedTimer); d->panFinishedTimer = 0; d->lastOffset = QSize(0, 0); - setState(Qt::GestureFinished); - emit triggered(); - setState(Qt::NoGesture); + updateState(Qt::GestureFinished); } } #endif @@ -127,6 +125,7 @@ bool QPanGesture::event(QEvent *event) bool QPanGesture::eventFilter(QObject *receiver, QEvent *event) { #ifdef Q_WS_WIN + Q_D(QPanGesture); if (receiver->isWidgetType() && event->type() == QEvent::NativeGesture) { QNativeGestureEvent *ev = static_cast<QNativeGestureEvent*>(event); QApplicationPrivate *qAppPriv = getQApplicationPrivateInternal(); @@ -135,30 +134,28 @@ bool QPanGesture::eventFilter(QObject *receiver, QEvent *event) if (it == qAppPriv->widgetGestures.end()) return false; QPanGesture *gesture = it.value().pan; - if (!gesture) + if (this != gesture) return false; - Qt::GestureState nextState = state(); + Qt::GestureState nextState = Qt::NoGesture; switch(ev->gestureType) { case QNativeGestureEvent::GestureBegin: // next we might receive the first gesture update event, so we // prepare for it. - setState(Qt::GestureStarted); + d->state = Qt::NoGesture; return false; case QNativeGestureEvent::Pan: nextState = Qt::GestureUpdated; + event->accept(); break; case QNativeGestureEvent::GestureEnd: - if (state() != QNativeGestureEvent::Pan) + if (state() == Qt::NoGesture) return false; // some other gesture has ended - setState(Qt::GestureFinished); nextState = Qt::GestureFinished; break; default: return false; } - QPanGesturePrivate *d = gesture->d_func(); - if (state() == Qt::GestureStarted) { - d->lastPosition = ev->position; + if (state() == Qt::NoGesture) { d->lastOffset = d->totalOffset = QSize(); } else { d->lastOffset = QSize(ev->position.x() - d->lastPosition.x(), @@ -166,14 +163,7 @@ bool QPanGesture::eventFilter(QObject *receiver, QEvent *event) d->totalOffset += d->lastOffset; } d->lastPosition = ev->position; - - if (state() == Qt::GestureStarted) - emit gesture->started(); - emit gesture->triggered(); - if (state() == Qt::GestureFinished) - emit gesture->finished(); - event->accept(); - gesture->setState(nextState); + gesture->updateState(nextState); return true; } #endif @@ -193,7 +183,6 @@ bool QPanGesture::filterEvent(QEvent *event) d->lastOffset = d->totalOffset = QSize(); } else if (event->type() == QEvent::TouchEnd) { if (state() != Qt::NoGesture) { - setState(Qt::GestureFinished); if (!ev->touchPoints().isEmpty()) { QTouchEvent::TouchPoint p = ev->touchPoints().at(0); const QPoint pos = p.pos().toPoint(); @@ -202,10 +191,8 @@ bool QPanGesture::filterEvent(QEvent *event) d->lastOffset = QSize(pos.x() - lastPos.x(), pos.y() - lastPos.y()); d->totalOffset = QSize(pos.x() - startPos.x(), pos.y() - startPos.y()); } - emit triggered(); - emit finished(); + updateState(Qt::GestureFinished); } - setState(Qt::NoGesture); reset(); } else if (event->type() == QEvent::TouchUpdate) { QTouchEvent::TouchPoint p = ev->touchPoints().at(0); @@ -216,11 +203,7 @@ bool QPanGesture::filterEvent(QEvent *event) d->totalOffset = QSize(pos.x() - startPos.x(), pos.y() - startPos.y()); if (d->totalOffset.width() > 10 || d->totalOffset.height() > 10 || d->totalOffset.width() < -10 || d->totalOffset.height() < -10) { - if (state() == Qt::NoGesture) - setState(Qt::GestureStarted); - else - setState(Qt::GestureUpdated); - emit triggered(); + updateState(Qt::GestureUpdated); } } #ifdef Q_OS_MAC @@ -239,16 +222,14 @@ bool QPanGesture::filterEvent(QEvent *event) d->lastOffset = wev->orientation() == Qt::Horizontal ? QSize(offset, 0) : QSize(0, offset); if (state() == Qt::NoGesture) { - setState(Qt::GestureStarted); d->totalOffset = d->lastOffset; } else { - setState(Qt::GestureUpdated); d->totalOffset += d->lastOffset; } killTimer(d->panFinishedTimer); d->panFinishedTimer = startTimer(200); - emit triggered(); + updateState(Qt::GestureUpdated); #endif return true; } diff --git a/src/gui/kernel/qwidget_win.cpp b/src/gui/kernel/qwidget_win.cpp index ce853d2..6647f3f 100644 --- a/src/gui/kernel/qwidget_win.cpp +++ b/src/gui/kernel/qwidget_win.cpp @@ -2074,6 +2074,8 @@ void QWidgetPrivate::winSetupGestures() needv = (vbarpolicy == Qt::ScrollBarAlwaysOn || (vbarpolicy == Qt::ScrollBarAsNeeded && vbar->minimum() < vbar->maximum())); singleFingerPanEnabled = asa->d_func()->singleFingerPanEnabled; + } else { + winid = q->winId(); } if (qAppPriv->SetGestureConfig) { GESTURECONFIG gc[2]; |