From 0f1f53414de3e7d1b62149ee42a334c6f2d01f78 Mon Sep 17 00:00:00 2001 From: Thomas Zander Date: Mon, 19 Oct 2009 19:11:19 +0200 Subject: Add QGestureEvent::mapToScene for better graphicsView integration --- src/gui/kernel/qevent.cpp | 18 ++++++++++++++++++ src/gui/kernel/qevent.h | 2 ++ src/gui/kernel/qgesture.cpp | 4 ++++ tests/auto/gestures/tst_gestures.cpp | 26 ++++++++++++++++++++++++++ 4 files changed, 50 insertions(+) diff --git a/src/gui/kernel/qevent.cpp b/src/gui/kernel/qevent.cpp index 1c6a820..ef74f06 100644 --- a/src/gui/kernel/qevent.cpp +++ b/src/gui/kernel/qevent.cpp @@ -45,6 +45,7 @@ #include "private/qapplication_p.h" #include "private/qkeysequence_p.h" #include "qwidget.h" +#include "qgraphicsview.h" #include "qdebug.h" #include "qmime.h" #include "qdnd_p.h" @@ -4345,6 +4346,23 @@ QWidget *QGestureEvent::widget() const } /*! + Returns the scene-local coordinates if the \a gesturePoint is inside a graphics view. + + \sa QPointF::isNull(). +*/ +QPointF QGestureEvent::mapToScene(const QPointF &gesturePoint) const +{ + QWidget *w = widget(); + if (w) // we get the viewport as widget, not the graphics view + w = w->parentWidget(); + QGraphicsView *view = qobject_cast(w); + if (view) { + return view->mapToScene(view->mapFromGlobal(gesturePoint.toPoint())); + } + return QPointF(); +} + +/*! \internal */ QGestureEventPrivate *QGestureEvent::d_func() diff --git a/src/gui/kernel/qevent.h b/src/gui/kernel/qevent.h index 5eefc2d..249c45a 100644 --- a/src/gui/kernel/qevent.h +++ b/src/gui/kernel/qevent.h @@ -855,6 +855,8 @@ public: void setWidget(QWidget *widget); QWidget *widget() const; + QPointF mapToScene(const QPointF &gesturePoint) const; + private: QGestureEventPrivate *d_func(); const QGestureEventPrivate *d_func() const; diff --git a/src/gui/kernel/qgesture.cpp b/src/gui/kernel/qgesture.cpp index e48fd8e..f044c09 100644 --- a/src/gui/kernel/qgesture.cpp +++ b/src/gui/kernel/qgesture.cpp @@ -129,6 +129,10 @@ QGesture::~QGesture() \brief The point that is used to find the receiver for the gesture event. + The hot-spot is a point in the global coordinate system, use + QWidget::mapFromGlobal() or QGestureEvent::mapToScene() to get a + local hot-spot. + If the hot-spot is not set, the targetObject is used as the receiver of the gesture event. */ diff --git a/tests/auto/gestures/tst_gestures.cpp b/tests/auto/gestures/tst_gestures.cpp index 0c09265..09da7d6 100644 --- a/tests/auto/gestures/tst_gestures.cpp +++ b/tests/auto/gestures/tst_gestures.cpp @@ -320,6 +320,7 @@ private slots: void twoGesturesOnDifferentLevel(); void multipleGesturesInTree(); void multipleGesturesInComplexTree(); + void testMapToScene(); }; tst_Gestures::tst_Gestures() @@ -1046,5 +1047,30 @@ void tst_Gestures::multipleGesturesInComplexTree() QCOMPARE(A->events.all.count(SeventhGesture), 0); } +void tst_Gestures::testMapToScene() +{ + QGesture gesture; + QList list; + list << &gesture; + QGestureEvent event(list); + QCOMPARE(event.mapToScene(gesture.hotSpot()), QPointF()); // not set, can't do much + + QGraphicsScene scene; + QGraphicsView view(&scene); + + GestureItem *item0 = new GestureItem; + scene.addItem(item0); + item0->setPos(14, 16); + + view.show(); // need to show to give it a global coordinate + QTest::qWaitForWindowShown(&view); + view.ensureVisible(scene.sceneRect()); + + QPoint origin = view.mapToGlobal(QPoint()); + event.setWidget(view.viewport()); + + QCOMPARE(event.mapToScene(origin + QPoint(100, 200)), view.mapToScene(QPoint(100, 200))); +} + QTEST_MAIN(tst_Gestures) #include "tst_gestures.moc" -- cgit v0.12