diff options
author | Alberto Mardegan <info@mardy.it> | 2012-07-05 13:56:51 (GMT) |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-07-25 07:03:22 (GMT) |
commit | ef4af1217af456dbddc3b5df4004378019a85404 (patch) | |
tree | 24db68526e6e58bfc07762aa2448dcfd1381d5ae | |
parent | c13df9f2147e85e104ac105f975ef87914c908e9 (diff) | |
download | Qt-ef4af1217af456dbddc3b5df4004378019a85404.zip Qt-ef4af1217af456dbddc3b5df4004378019a85404.tar.gz Qt-ef4af1217af456dbddc3b5df4004378019a85404.tar.bz2 |
MouseArea: use current value of drag.axis
If the drag.axis is changed while a drag operation is in progress, put
it into action immediately. This allows, for example, start a dragging
operation out of an item in a scrollable ListView to anywhere on the
screen.
See the linked bug number for an example.
Task-number: QTBUG-26440
Change-Id: I4ffa71c08b97a767aec7f69d19271000a2631327
Reviewed-by: Rick Stockton <rickstockton@reno-computerhelp.com>
Reviewed-by: Andrew den Exter <andrew.den-exter@nokia.com>
4 files changed, 109 insertions, 11 deletions
diff --git a/src/declarative/graphicsitems/qdeclarativemousearea.cpp b/src/declarative/graphicsitems/qdeclarativemousearea.cpp index c653893..60d86bb 100644 --- a/src/declarative/graphicsitems/qdeclarativemousearea.cpp +++ b/src/declarative/graphicsitems/qdeclarativemousearea.cpp @@ -483,10 +483,6 @@ void QDeclarativeMouseArea::mousePressEvent(QGraphicsSceneMouseEvent *event) else { d->longPress = false; d->saveEvent(event); - if (d->drag) { - d->dragX = drag()->axis() & QDeclarativeDrag::XAxis; - d->dragY = drag()->axis() & QDeclarativeDrag::YAxis; - } if (d->drag) d->drag->setActive(false); setHovered(true); @@ -540,7 +536,10 @@ void QDeclarativeMouseArea::mouseMoveEvent(QGraphicsSceneMouseEvent *event) if (keepMouseGrab() && d->stealMouse) d->drag->setActive(true); - if (d->dragX && d->drag->active()) { + bool dragX = drag()->axis() & QDeclarativeDrag::XAxis; + bool dragY = drag()->axis() & QDeclarativeDrag::YAxis; + + if (dragX && d->drag->active()) { qreal x = (curLocalPos.x() - startLocalPos.x()) + d->startX; if (x < drag()->xmin()) x = drag()->xmin(); @@ -548,7 +547,7 @@ void QDeclarativeMouseArea::mouseMoveEvent(QGraphicsSceneMouseEvent *event) x = drag()->xmax(); drag()->target()->setX(x); } - if (d->dragY && d->drag->active()) { + if (dragY && d->drag->active()) { qreal y = (curLocalPos.y() - startLocalPos.y()) + d->startY; if (y < drag()->ymin()) y = drag()->ymin(); @@ -558,9 +557,9 @@ void QDeclarativeMouseArea::mouseMoveEvent(QGraphicsSceneMouseEvent *event) } if (!keepMouseGrab()) { - if ((!d->dragY && dy < dragThreshold && d->dragX && dx > dragThreshold) - || (!d->dragX && dx < dragThreshold && d->dragY && dy > dragThreshold) - || (d->dragX && d->dragY && (dx > dragThreshold || dy > dragThreshold))) { + if ((!dragY && dy < dragThreshold && dragX && dx > dragThreshold) + || (!dragX && dx < dragThreshold && dragY && dy > dragThreshold) + || (dragX && dragY && (dx > dragThreshold || dy > dragThreshold))) { setKeepMouseGrab(true); d->stealMouse = true; } diff --git a/src/declarative/graphicsitems/qdeclarativemousearea_p_p.h b/src/declarative/graphicsitems/qdeclarativemousearea_p_p.h index 00c6eed..de34565 100644 --- a/src/declarative/graphicsitems/qdeclarativemousearea_p_p.h +++ b/src/declarative/graphicsitems/qdeclarativemousearea_p_p.h @@ -106,8 +106,6 @@ public: bool pressed : 1; bool longPress : 1; bool moved : 1; - bool dragX : 1; - bool dragY : 1; bool stealMouse : 1; bool doubleClick : 1; bool preventStealing : 1; diff --git a/tests/auto/declarative/qdeclarativemousearea/data/changeAxis.qml b/tests/auto/declarative/qdeclarativemousearea/data/changeAxis.qml new file mode 100644 index 0000000..e0b11a3 --- /dev/null +++ b/tests/auto/declarative/qdeclarativemousearea/data/changeAxis.qml @@ -0,0 +1,22 @@ +import QtQuick 1.0 +Rectangle { + id: whiteRect + width: 200 + height: 200 + color: "white" + Rectangle { + id: blackRect + objectName: "blackrect" + color: "black" + y: 50 + x: 50 + width: 100 + height: 100 + MouseArea { + objectName: "mouseregion" + anchors.fill: parent + drag.target: blackRect + drag.axis: blackRect.x <= 60 ? Drag.XandYAxis : Drag.YAxis + } + } + } diff --git a/tests/auto/declarative/qdeclarativemousearea/tst_qdeclarativemousearea.cpp b/tests/auto/declarative/qdeclarativemousearea/tst_qdeclarativemousearea.cpp index 98158e2..fcd9e09 100644 --- a/tests/auto/declarative/qdeclarativemousearea/tst_qdeclarativemousearea.cpp +++ b/tests/auto/declarative/qdeclarativemousearea/tst_qdeclarativemousearea.cpp @@ -74,6 +74,7 @@ private slots: #ifndef QT_NO_CONTEXTMENU void preventContextMenu(); #endif // QT_NO_CONTEXTMENU + void changeAxis(); private: QDeclarativeView *createView(); @@ -701,6 +702,84 @@ void tst_QDeclarativeMouseArea::preventContextMenu() } #endif // QT_NO_CONTEXTMENU +void tst_QDeclarativeMouseArea::changeAxis() +{ + QDeclarativeView *canvas = createView(); + + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/changeAxis.qml")); + canvas->show(); + canvas->setFocus(); + QVERIFY(canvas->rootObject() != 0); + + QDeclarativeMouseArea *mouseRegion = canvas->rootObject()->findChild<QDeclarativeMouseArea*>("mouseregion"); + QDeclarativeDrag *drag = mouseRegion->drag(); + QVERIFY(mouseRegion != 0); + QVERIFY(drag != 0); + + // target + QDeclarativeItem *blackRect = canvas->rootObject()->findChild<QDeclarativeItem*>("blackrect"); + QVERIFY(blackRect != 0); + QVERIFY(blackRect == drag->target()); + + QVERIFY(!drag->active()); + + // Start a diagonal drag + QGraphicsScene *scene = canvas->scene(); + QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress); + pressEvent.setScenePos(QPointF(100, 100)); + pressEvent.setButton(Qt::LeftButton); + pressEvent.setButtons(Qt::LeftButton); + QApplication::sendEvent(scene, &pressEvent); + + QGraphicsSceneMouseEvent moveEvent(QEvent::GraphicsSceneMouseMove); + moveEvent.setScenePos(QPointF(106, 106)); + moveEvent.setButton(Qt::LeftButton); + moveEvent.setButtons(Qt::LeftButton); + QApplication::sendEvent(scene, &moveEvent); + + moveEvent.setScenePos(QPointF(110, 110)); + moveEvent.setButton(Qt::LeftButton); + moveEvent.setButtons(Qt::LeftButton); + QApplication::sendEvent(scene, &moveEvent); + + QVERIFY(drag->active()); + QCOMPARE(blackRect->x(), 60.0); + QCOMPARE(blackRect->y(), 60.0); + QCOMPARE(drag->axis(), QDeclarativeDrag::XandYAxis); + + /* When blackRect.x becomes bigger than 60, the drag axis is change to + * Drag.YAxis by the QML code. Verify that this happens, and that the drag + * movement is effectively constrained to the Y axis. */ + moveEvent.setScenePos(QPointF(115, 115)); + moveEvent.setButton(Qt::LeftButton); + moveEvent.setButtons(Qt::LeftButton); + QApplication::sendEvent(scene, &moveEvent); + + QCOMPARE(blackRect->x(), 65.0); + QCOMPARE(blackRect->y(), 65.0); + QCOMPARE(drag->axis(), QDeclarativeDrag::YAxis); + + moveEvent.setScenePos(QPointF(120, 120)); + moveEvent.setButton(Qt::LeftButton); + moveEvent.setButtons(Qt::LeftButton); + QApplication::sendEvent(scene, &moveEvent); + + QCOMPARE(blackRect->x(), 65.0); + QCOMPARE(blackRect->y(), 70.0); + + QGraphicsSceneMouseEvent releaseEvent(QEvent::GraphicsSceneMouseRelease); + releaseEvent.setScenePos(QPointF(120, 120)); + releaseEvent.setButton(Qt::LeftButton); + releaseEvent.setButtons(Qt::LeftButton); + QApplication::sendEvent(scene, &releaseEvent); + + QVERIFY(!drag->active()); + QCOMPARE(blackRect->x(), 65.0); + QCOMPARE(blackRect->y(), 70.0); + + delete canvas; +} + QTEST_MAIN(tst_QDeclarativeMouseArea) #include "tst_qdeclarativemousearea.moc" |