From 7850ee84483b34ef2e97517c3bc9cfb93ea1bf1c Mon Sep 17 00:00:00 2001 From: Leonardo Sobral Cunha Date: Wed, 31 Mar 2010 10:44:39 +1000 Subject: Update mouseX(Y) when clicking on a mouse area This fixes the original commit, 56aa9370dbafa8ee4, for QTBUG-9383. We need to emit a positionChanged signal to update the mouseX(Y) for the mouse area. Reviewed-by: Martin Jones --- .../graphicsitems/qdeclarativemousearea.cpp | 1 + .../data/updateMousePosOnClick.qml | 20 ++++++++++++ .../tst_qdeclarativemousearea.cpp | 36 ++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 tests/auto/declarative/qdeclarativemousearea/data/updateMousePosOnClick.qml diff --git a/src/declarative/graphicsitems/qdeclarativemousearea.cpp b/src/declarative/graphicsitems/qdeclarativemousearea.cpp index dde3366..6126a6f 100644 --- a/src/declarative/graphicsitems/qdeclarativemousearea.cpp +++ b/src/declarative/graphicsitems/qdeclarativemousearea.cpp @@ -640,6 +640,7 @@ bool QDeclarativeMouseArea::setPressed(bool p) QDeclarativeMouseEvent me(d->lastPos.x(), d->lastPos.y(), d->lastButton, d->lastButtons, d->lastModifiers, isclick, d->longPress); if (d->pressed) { emit pressed(&me); + emit positionChanged(&me); } else { emit released(&me); if (isclick) diff --git a/tests/auto/declarative/qdeclarativemousearea/data/updateMousePosOnClick.qml b/tests/auto/declarative/qdeclarativemousearea/data/updateMousePosOnClick.qml new file mode 100644 index 0000000..0da7c45 --- /dev/null +++ b/tests/auto/declarative/qdeclarativemousearea/data/updateMousePosOnClick.qml @@ -0,0 +1,20 @@ +import Qt 4.6 + +Rectangle { + color: "#ffffff" + width: 320; height: 240 + MouseArea { + id: mouseRegion + objectName: "mouseregion" + anchors.fill: parent + Rectangle { + id: ball + objectName: "ball" + width: 20; height: 20 + radius: 10 + color: "#0000ff" + x: { mouseRegion.mouseX } + y: mouseRegion.mouseY + } + } +} diff --git a/tests/auto/declarative/qdeclarativemousearea/tst_qdeclarativemousearea.cpp b/tests/auto/declarative/qdeclarativemousearea/tst_qdeclarativemousearea.cpp index 769cf32..9b664e5 100644 --- a/tests/auto/declarative/qdeclarativemousearea/tst_qdeclarativemousearea.cpp +++ b/tests/auto/declarative/qdeclarativemousearea/tst_qdeclarativemousearea.cpp @@ -42,6 +42,7 @@ #include #include #include +#include #include class tst_QDeclarativeMouseArea: public QObject @@ -49,6 +50,7 @@ class tst_QDeclarativeMouseArea: public QObject Q_OBJECT private slots: void dragProperties(); + void updateMouseAreaPosOnClick(); private: QDeclarativeView *createView(const QString &filename); }; @@ -121,6 +123,8 @@ void tst_QDeclarativeMouseArea::dragProperties() QCOMPARE(xmaxSpy.count(),1); QCOMPARE(yminSpy.count(),1); QCOMPARE(ymaxSpy.count(),1); + + delete canvas; } QDeclarativeView *tst_QDeclarativeMouseArea::createView(const QString &filename) @@ -133,6 +137,38 @@ QDeclarativeView *tst_QDeclarativeMouseArea::createView(const QString &filename) return canvas; } +void tst_QDeclarativeMouseArea::updateMouseAreaPosOnClick() +{ + QDeclarativeView *canvas = createView(SRCDIR "/data/updateMousePosOnClick.qml"); + canvas->show(); + canvas->setFocus(); + QVERIFY(canvas->rootObject() != 0); + + QDeclarativeMouseArea *mouseRegion = canvas->rootObject()->findChild("mouseregion"); + QVERIFY(mouseRegion != 0); + + QDeclarativeRectangle *rect = canvas->rootObject()->findChild("ball"); + QVERIFY(rect != 0); + + QCOMPARE(mouseRegion->mouseX(), rect->x()); + QCOMPARE(mouseRegion->mouseY(), rect->y()); + + QGraphicsScene *scene = canvas->scene(); + QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMousePress); + event.setScenePos(QPointF(100, 100)); + event.setButton(Qt::LeftButton); + event.setButtons(Qt::LeftButton); + QApplication::sendEvent(scene, &event); + + QCOMPARE(mouseRegion->mouseX(), 100.0); + QCOMPARE(mouseRegion->mouseY(), 100.0); + + QCOMPARE(mouseRegion->mouseX(), rect->x()); + QCOMPARE(mouseRegion->mouseY(), rect->y()); + + delete canvas; +} + QTEST_MAIN(tst_QDeclarativeMouseArea) #include "tst_qdeclarativemousearea.moc" -- cgit v0.12