From 25bc5c29db866d5abc3f9fbae7b5211e2e6b1f25 Mon Sep 17 00:00:00 2001 From: Thomas Zander Date: Wed, 21 Oct 2009 13:58:24 +0200 Subject: Add QWidget::ungrabGesture Reviewed-by: Denis Dzyubenko --- src/gui/kernel/qgesturemanager.cpp | 16 ++++++++ src/gui/kernel/qgesturemanager_p.h | 2 + src/gui/kernel/qwidget.cpp | 16 ++++++++ src/gui/kernel/qwidget.h | 1 + tests/auto/gestures/tst_gestures.cpp | 71 ++++++++++++++++++++++++++++++++++++ 5 files changed, 106 insertions(+) diff --git a/src/gui/kernel/qgesturemanager.cpp b/src/gui/kernel/qgesturemanager.cpp index dc76c3f..df88f9e 100644 --- a/src/gui/kernel/qgesturemanager.cpp +++ b/src/gui/kernel/qgesturemanager.cpp @@ -129,6 +129,21 @@ void QGestureManager::unregisterGestureRecognizer(Qt::GestureType type) } } +void QGestureManager::cleanupCachedGestures(QObject *target, Qt::GestureType type) +{ + QMap::Iterator iter = objectGestures.begin(); + while (iter != objectGestures.end()) { + ObjectGesture objectGesture = iter.key(); + if (objectGesture.gesture == type && target == objectGesture.object.data()) { + delete iter.value(); + iter = objectGestures.erase(iter); + } else { + ++iter; + } + } +} + +// get or create a QGesture object that will represent the state for a given object, used by the recognizer QGesture *QGestureManager::getState(QObject *object, Qt::GestureType type) { // if the widget is being deleted we should be carefull and not to @@ -332,6 +347,7 @@ void QGestureManager::cleanupGesturesForRemovedRecognizer(QGesture *gesture) } } +// return true if accepted (consumed) bool QGestureManager::filterEvent(QWidget *receiver, QEvent *event) { QSet types; diff --git a/src/gui/kernel/qgesturemanager_p.h b/src/gui/kernel/qgesturemanager_p.h index eef45d2..96c2fb7 100644 --- a/src/gui/kernel/qgesturemanager_p.h +++ b/src/gui/kernel/qgesturemanager_p.h @@ -79,6 +79,8 @@ public: // declared in qapplication.cpp static QGestureManager* instance(); + void cleanupCachedGestures(QObject *target, Qt::GestureType type); + protected: void timerEvent(QTimerEvent *event); bool filterEventThroughContexts(const QMap &contexts, diff --git a/src/gui/kernel/qwidget.cpp b/src/gui/kernel/qwidget.cpp index 5fa9a92..c10db90 100644 --- a/src/gui/kernel/qwidget.cpp +++ b/src/gui/kernel/qwidget.cpp @@ -11708,6 +11708,22 @@ void QWidget::grabGesture(Qt::GestureType gesture, Qt::GestureContext context) (void)QGestureManager::instance(); // create a gesture manager } +/*! + Unsubscribes the widget to a given \a gesture type + + \sa QGestureEvent + \since 4.6 +*/ +void QWidget::ungrabGesture(Qt::GestureType gesture) +{ + Q_D(QWidget); + if (d->gestureContext.remove(gesture)) { + QGestureManager *manager = QGestureManager::instance(); + manager->cleanupCachedGestures(this, gesture); + } +} + + QT_END_NAMESPACE #include "moc_qwidget.cpp" diff --git a/src/gui/kernel/qwidget.h b/src/gui/kernel/qwidget.h index e603a1a..fce3f09 100644 --- a/src/gui/kernel/qwidget.h +++ b/src/gui/kernel/qwidget.h @@ -355,6 +355,7 @@ public: void setGraphicsEffect(QGraphicsEffect *effect); void grabGesture(Qt::GestureType type, Qt::GestureContext context = Qt::WidgetWithChildrenGesture); + void ungrabGesture(Qt::GestureType type); public Q_SLOTS: void setWindowTitle(const QString &); diff --git a/tests/auto/gestures/tst_gestures.cpp b/tests/auto/gestures/tst_gestures.cpp index 800af1b..6e52de3 100644 --- a/tests/auto/gestures/tst_gestures.cpp +++ b/tests/auto/gestures/tst_gestures.cpp @@ -280,6 +280,7 @@ protected: } }; +// TODO rename to sendGestureSequence static void sendCustomGesture(CustomEvent *event, QObject *object, QGraphicsScene *scene = 0) { for (int i = CustomGesture::SerialMaybeThreshold; @@ -322,6 +323,7 @@ private slots: void multipleGesturesInTree(); void multipleGesturesInComplexTree(); void testMapToScene(); + void ungrabGesture(); }; tst_Gestures::tst_Gestures() @@ -1181,5 +1183,74 @@ void tst_Gestures::testMapToScene() QCOMPARE(event.mapToScene(origin + QPoint(100, 200)), view.mapToScene(QPoint(100, 200))); } +void tst_Gestures::ungrabGesture() // a method on QWidget +{ + class MockGestureWidget : public GestureWidget { + public: + MockGestureWidget(const char *name = 0, QWidget *parent = 0) + : GestureWidget(name, parent) { } + + + QSet gestures; + protected: + bool event(QEvent *event) + { + if (event->type() == QEvent::Gesture) { + QGestureEvent *gestureEvent = static_cast(event); + if (gestureEvent) + foreach (QGesture *g, gestureEvent->allGestures()) + gestures.insert(g); + } + return GestureWidget::event(event); + } + }; + + MockGestureWidget parent("A"); + MockGestureWidget *a = &parent; + MockGestureWidget *b = new MockGestureWidget("B", a); + + a->grabGesture(CustomGesture::GestureType, Qt::WidgetGesture); + b->grabGesture(CustomGesture::GestureType, Qt::WidgetWithChildrenGesture); + b->ignoredGestures << CustomGesture::GestureType; + + CustomEvent event; + // sending an event will cause the QGesture objects to be instantiated for the widgets + sendCustomGesture(&event, b); + + QCOMPARE(a->gestures.count(), 1); + QPointer customGestureA; + customGestureA = *(a->gestures.begin()); + QVERIFY(!customGestureA.isNull()); + QCOMPARE(customGestureA->gestureType(), CustomGesture::GestureType); + + QCOMPARE(b->gestures.count(), 1); + QPointer customGestureB; + customGestureB = *(b->gestures.begin()); + QVERIFY(!customGestureB.isNull()); + QVERIFY(customGestureA.data() == customGestureB.data()); + QCOMPARE(customGestureB->gestureType(), CustomGesture::GestureType); + + a->gestures.clear(); + // sending an event will cause the QGesture objects to be instantiated for the widget + sendCustomGesture(&event, a); + + QCOMPARE(a->gestures.count(), 1); + customGestureA = *(a->gestures.begin()); + QVERIFY(!customGestureA.isNull()); + QCOMPARE(customGestureA->gestureType(), CustomGesture::GestureType); + QVERIFY(customGestureA.data() != customGestureB.data()); + + a->ungrabGesture(CustomGesture::GestureType); + QVERIFY(customGestureA.isNull()); + QVERIFY(!customGestureB.isNull()); + + a->gestures.clear(); + a->reset(); + // send again to 'b' and make sure a never gets it. + sendCustomGesture(&event, b); + QCOMPARE(a->gestureEventsReceived, 0); + QCOMPARE(a->gestureOverrideEventsReceived, 0); +} + QTEST_MAIN(tst_Gestures) #include "tst_gestures.moc" -- cgit v0.12