diff options
author | Denis Dzyubenko <denis.dzyubenko@nokia.com> | 2009-05-19 16:52:11 (GMT) |
---|---|---|
committer | Denis Dzyubenko <denis.dzyubenko@nokia.com> | 2009-05-20 13:10:04 (GMT) |
commit | 7a667dd45130142589c6a72f663ee647b1e563fd (patch) | |
tree | fc986a741d863c8de826e0937a713b283026b861 /tests | |
parent | cc1486132fcb47f74c132f3f17f7f2759b0ba165 (diff) | |
download | Qt-7a667dd45130142589c6a72f663ee647b1e563fd.zip Qt-7a667dd45130142589c6a72f663ee647b1e563fd.tar.gz Qt-7a667dd45130142589c6a72f663ee647b1e563fd.tar.bz2 |
Added a simple test for gesture event delivery to graphicsitems
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/gestures/customgesturerecognizer.cpp | 2 | ||||
-rw-r--r-- | tests/auto/gestures/customgesturerecognizer.h | 6 | ||||
-rw-r--r-- | tests/auto/gestures/tst_gestures.cpp | 139 |
3 files changed, 143 insertions, 4 deletions
diff --git a/tests/auto/gestures/customgesturerecognizer.cpp b/tests/auto/gestures/customgesturerecognizer.cpp index 906fd32..898071a 100644 --- a/tests/auto/gestures/customgesturerecognizer.cpp +++ b/tests/auto/gestures/customgesturerecognizer.cpp @@ -14,6 +14,7 @@ SingleshotGestureRecognizer::SingleshotGestureRecognizer(QObject *parent) QGestureRecognizer::Result SingleshotGestureRecognizer::filterEvent(const QEvent *event) { if (event->type() == SingleshotEvent::Type) { + gesture->setHotSpot(static_cast<const SingleshotEvent*>(event)->point); return QGestureRecognizer::GestureFinished; } return QGestureRecognizer::NotGesture; @@ -21,6 +22,7 @@ QGestureRecognizer::Result SingleshotGestureRecognizer::filterEvent(const QEvent void SingleshotGestureRecognizer::reset() { + gesture->setHotSpot(QPoint()); gesture->offset = QPoint(); } diff --git a/tests/auto/gestures/customgesturerecognizer.h b/tests/auto/gestures/customgesturerecognizer.h index b9ab421..9b56433 100644 --- a/tests/auto/gestures/customgesturerecognizer.h +++ b/tests/auto/gestures/customgesturerecognizer.h @@ -9,7 +9,11 @@ class SingleshotEvent : public QEvent { public: static const int Type = QEvent::User + 1; - SingleshotEvent() : QEvent(QEvent::Type(Type)) { } + + QPoint point; + + explicit SingleshotEvent(int x = 0, int y = 0) + : QEvent(QEvent::Type(Type)), point(x, y) { } }; class SingleshotGesture : public QGesture diff --git a/tests/auto/gestures/tst_gestures.cpp b/tests/auto/gestures/tst_gestures.cpp index 614474f9..22d16fd 100644 --- a/tests/auto/gestures/tst_gestures.cpp +++ b/tests/auto/gestures/tst_gestures.cpp @@ -269,15 +269,20 @@ class GraphicsScene : public QGraphicsScene public: GraphicsScene() { - shouldAcceptSingleshotGesture = false; - shouldAcceptPinchGesture = false; - shouldAcceptSecondFingerGesture = false; + reset(); } bool shouldAcceptSingleshotGesture; bool shouldAcceptPinchGesture; bool shouldAcceptSecondFingerGesture; GestureState gesture; + void reset() + { + shouldAcceptSingleshotGesture = false; + shouldAcceptPinchGesture = false; + shouldAcceptSecondFingerGesture = false; + gesture.reset(); + } protected: bool event(QEvent *event) { @@ -319,6 +324,103 @@ protected: } }; +class GraphicsItem : public QGraphicsItem +{ +public: + GraphicsItem() + { + reset(); + } + + QRectF boundingRect() const + { + return QRectF(0, 0, 100, 100); + } + + void paint(QPainter *painter, const QStyleOptionGraphicsItem*, QWidget*) + { + painter->setBrush(Qt::green); + painter->drawRect(0, 0, 100, 100); + } + + void grabSingleshotGesture() + { + singleshotGestureId = grabGesture(SingleshotGestureRecognizer::Name); + } + void grabPinchGesture() + { + pinchGestureId = grabGesture(PinchGestureRecognizer::Name); + } + void grabSecondFingerGesture() + { + secondFingerGestureId = grabGesture(SecondFingerGestureRecognizer::Name); + } + void ungrabGestures() + { + releaseGesture(singleshotGestureId); + singleshotGestureId = -1; + releaseGesture(pinchGestureId); + pinchGestureId = -1; + releaseGesture(secondFingerGestureId); + secondFingerGestureId = -1; + } + + int singleshotGestureId; + int pinchGestureId; + int secondFingerGestureId; + + bool shouldAcceptSingleshotGesture; + bool shouldAcceptPinchGesture; + bool shouldAcceptSecondFingerGesture; + GestureState gesture; + + void reset() + { + shouldAcceptSingleshotGesture = true; + shouldAcceptPinchGesture = true; + shouldAcceptSecondFingerGesture = true; + gesture.reset(); + } +protected: + bool sceneEvent(QEvent *event) + { + if (event->type() == QEvent::GraphicsSceneGesture) { + QGraphicsSceneGestureEvent *e = static_cast<QGraphicsSceneGestureEvent*>(event); + ++gesture.seenGestureEvent; + if (SingleshotGesture *g = (SingleshotGesture*)e->gesture(SingleshotGestureRecognizer::Name)) { + gesture.last.singleshot.delivered = true; + gesture.last.singleshot.offset = g->offset; + if (shouldAcceptSingleshotGesture) + g->accept(); + } + if (PinchGesture *g = (PinchGesture*)e->gesture(PinchGestureRecognizer::Name)) { + gesture.last.pinch.delivered = true; + gesture.last.pinch.startPoints[0] = g->startPoints[0]; + gesture.last.pinch.startPoints[1] = g->startPoints[1]; + gesture.last.pinch.lastPoints[0] = g->lastPoints[0]; + gesture.last.pinch.lastPoints[1] = g->lastPoints[1]; + gesture.last.pinch.points[0] = g->points[0]; + gesture.last.pinch.points[1] = g->points[1]; + gesture.last.pinch.offset = g->offset; + if (shouldAcceptPinchGesture) + g->accept(); + } + if (SecondFingerGesture *g = (SecondFingerGesture*)e->gesture(SecondFingerGestureRecognizer::Name)) { + gesture.last.secondfinger.delivered = true; + gesture.last.secondfinger.startPoint = g->startPoint; + gesture.last.secondfinger.lastPoint = g->lastPoint; + gesture.last.secondfinger.point = g->point; + gesture.last.secondfinger.offset = g->offset; + if (shouldAcceptSecondFingerGesture) + g->accept(); + } + gesture.last.cancelled = e->cancelledGestures(); + return true; + } + return QGraphicsItem::sceneEvent(event); + } +}; + class tst_Gestures : public QObject { Q_OBJECT @@ -342,6 +444,7 @@ private slots: void acceptedGesturePropagation(); void simpleGraphicsView(); + void simpleGraphicsItem(); private: SingleshotGestureRecognizer *singleshotRecognizer; @@ -566,5 +669,35 @@ void tst_Gestures::simpleGraphicsView() QVERIFY(scene.gesture.last.cancelled.isEmpty()); } +void tst_Gestures::simpleGraphicsItem() +{ + mainWidget->grabSingleshotGesture(); + GraphicsScene scene; + QGraphicsView view(&scene); + mainWidget->layout()->addWidget(&view); + GraphicsItem *item = new GraphicsItem; + item->grabSingleshotGesture(); + item->setPos(30, 50); + scene.addItem(item); + QApplication::processEvents(); + + SingleshotEvent event(50, 80); + sendSpontaneousEvent(&view, &event); + QVERIFY(item->gesture.seenGestureEvent); + QVERIFY(scene.gesture.seenGestureEvent); + QVERIFY(!mainWidget->gesture.seenGestureEvent); + + item->reset(); + scene.reset(); + mainWidget->reset(); + + item->shouldAcceptSingleshotGesture = false; + SingleshotEvent event2(20, 40); + sendSpontaneousEvent(&view, &event2); + QVERIFY(!item->gesture.seenGestureEvent); + QVERIFY(scene.gesture.seenGestureEvent); + QVERIFY(mainWidget->gesture.seenGestureEvent); +} + QTEST_MAIN(tst_Gestures) #include "tst_gestures.moc" |