From 9c4140af1cbc650905de698daa4ab0183efba477 Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Wed, 7 Apr 2010 11:56:07 +1000 Subject: Allow MouseArea.Drag.target to be reset. Task-number: QTBUG-9637 --- .../graphicsitems/qdeclarativemousearea.cpp | 8 ++++ .../graphicsitems/qdeclarativemousearea_p.h | 3 +- .../qdeclarativemousearea/data/dragreset.qml | 28 ++++++++++++++ .../tst_qdeclarativemousearea.cpp | 45 +++++++++++++++++++--- 4 files changed, 77 insertions(+), 7 deletions(-) create mode 100644 tests/auto/declarative/qdeclarativemousearea/data/dragreset.qml diff --git a/src/declarative/graphicsitems/qdeclarativemousearea.cpp b/src/declarative/graphicsitems/qdeclarativemousearea.cpp index 06ecb61..c95bd29 100644 --- a/src/declarative/graphicsitems/qdeclarativemousearea.cpp +++ b/src/declarative/graphicsitems/qdeclarativemousearea.cpp @@ -71,6 +71,14 @@ void QDeclarativeDrag::setTarget(QGraphicsObject *t) emit targetChanged(); } +void QDeclarativeDrag::resetTarget() +{ + if (!_target) + return; + _target = 0; + emit targetChanged(); +} + QDeclarativeDrag::Axis QDeclarativeDrag::axis() const { return _axis; diff --git a/src/declarative/graphicsitems/qdeclarativemousearea_p.h b/src/declarative/graphicsitems/qdeclarativemousearea_p.h index db49b57..58faac1 100644 --- a/src/declarative/graphicsitems/qdeclarativemousearea_p.h +++ b/src/declarative/graphicsitems/qdeclarativemousearea_p.h @@ -55,7 +55,7 @@ class Q_DECLARATIVE_EXPORT QDeclarativeDrag : public QObject Q_OBJECT Q_ENUMS(Axis) - Q_PROPERTY(QGraphicsObject *target READ target WRITE setTarget NOTIFY targetChanged) + Q_PROPERTY(QGraphicsObject *target READ target WRITE setTarget NOTIFY targetChanged RESET resetTarget) Q_PROPERTY(Axis axis READ axis WRITE setAxis NOTIFY axisChanged) Q_PROPERTY(qreal minimumX READ xmin WRITE setXmin NOTIFY minimumXChanged) Q_PROPERTY(qreal maximumX READ xmax WRITE setXmax NOTIFY maximumXChanged) @@ -69,6 +69,7 @@ public: QGraphicsObject *target() const; void setTarget(QGraphicsObject *); + void resetTarget(); enum Axis { XAxis=0x01, YAxis=0x02, XandYAxis=0x03 }; Axis axis() const; diff --git a/tests/auto/declarative/qdeclarativemousearea/data/dragreset.qml b/tests/auto/declarative/qdeclarativemousearea/data/dragreset.qml new file mode 100644 index 0000000..4bfb9c3 --- /dev/null +++ b/tests/auto/declarative/qdeclarativemousearea/data/dragreset.qml @@ -0,0 +1,28 @@ +import Qt 4.6 +Rectangle { + id: whiteRect + width: 200 + height: 200 + color: "white" + Rectangle { + id: blackRect + objectName: "blackrect" + color: "black" + y: 50 + x: 50 + width: 100 + height: 100 + opacity: (whiteRect.width-blackRect.x+whiteRect.height-blackRect.y-199)/200 + Text { text: blackRect.opacity} + MouseArea { + objectName: "mouseregion" + anchors.fill: parent + drag.target: haveTarget ? blackRect : undefined + drag.axis: Drag.XandYAxis + drag.minimumX: 0 + drag.maximumX: whiteRect.width-blackRect.width + drag.minimumY: 0 + drag.maximumY: whiteRect.height-blackRect.height + } + } + } diff --git a/tests/auto/declarative/qdeclarativemousearea/tst_qdeclarativemousearea.cpp b/tests/auto/declarative/qdeclarativemousearea/tst_qdeclarativemousearea.cpp index 9b664e5..dfe46e4 100644 --- a/tests/auto/declarative/qdeclarativemousearea/tst_qdeclarativemousearea.cpp +++ b/tests/auto/declarative/qdeclarativemousearea/tst_qdeclarativemousearea.cpp @@ -44,20 +44,23 @@ #include #include #include +#include class tst_QDeclarativeMouseArea: public QObject { Q_OBJECT private slots: void dragProperties(); + void resetDrag(); void updateMouseAreaPosOnClick(); private: - QDeclarativeView *createView(const QString &filename); + QDeclarativeView *createView(); }; void tst_QDeclarativeMouseArea::dragProperties() { - QDeclarativeView *canvas = createView(SRCDIR "/data/dragproperties.qml"); + QDeclarativeView *canvas = createView(); + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/dragproperties.qml")); canvas->show(); canvas->setFocus(); QVERIFY(canvas->rootObject() != 0); @@ -127,19 +130,49 @@ void tst_QDeclarativeMouseArea::dragProperties() delete canvas; } -QDeclarativeView *tst_QDeclarativeMouseArea::createView(const QString &filename) +void tst_QDeclarativeMouseArea::resetDrag() +{ + QDeclarativeView *canvas = createView(); + + canvas->rootContext()->setContextProperty("haveTarget", QVariant(true)); + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/dragreset.qml")); + canvas->show(); + canvas->setFocus(); + QVERIFY(canvas->rootObject() != 0); + + QDeclarativeMouseArea *mouseRegion = canvas->rootObject()->findChild("mouseregion"); + QDeclarativeDrag *drag = mouseRegion->drag(); + QVERIFY(mouseRegion != 0); + QVERIFY(drag != 0); + + // target + QDeclarativeItem *blackRect = canvas->rootObject()->findChild("blackrect"); + QVERIFY(blackRect != 0); + QVERIFY(blackRect == drag->target()); + QDeclarativeItem *rootItem = qobject_cast(canvas->rootObject()); + QVERIFY(rootItem != 0); + QSignalSpy targetSpy(drag, SIGNAL(targetChanged())); + QVERIFY(drag->target() != 0); + canvas->rootContext()->setContextProperty("haveTarget", QVariant(false)); + QCOMPARE(targetSpy.count(),1); + QVERIFY(drag->target() == 0); + + delete canvas; +} + + +QDeclarativeView *tst_QDeclarativeMouseArea::createView() { QDeclarativeView *canvas = new QDeclarativeView(0); canvas->setFixedSize(240,320); - canvas->setSource(QUrl::fromLocalFile(filename)); - return canvas; } void tst_QDeclarativeMouseArea::updateMouseAreaPosOnClick() { - QDeclarativeView *canvas = createView(SRCDIR "/data/updateMousePosOnClick.qml"); + QDeclarativeView *canvas = createView(); + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/updateMousePosOnClick.qml")); canvas->show(); canvas->setFocus(); QVERIFY(canvas->rootObject() != 0); -- cgit v0.12