aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew den Exter <andrew.den.exter@jollamobile.com>2013-03-14 12:36:20 +1000
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-03-14 10:30:06 +0100
commitf52227f66a7af5692140ad036c06857cd2e7abcf (patch)
tree465b0cd7a9d23b4921a508cdb6f3e3504dbcc3a5
parentb58953fae3dc80c52e846d2d87856dd20b8986ab (diff)
Smooth dragging an item with MouseArea.
Calculate drag distance relative to the mouse position when a drag is detected rather than the press position so there isn't a large change in position on the first move. Task-number: QTBUG-30188 Change-Id: I5c2269c2228668a3842e18f65167637497b3beb4 Reviewed-by: Joona Petrell <joona.petrell@jollamobile.com>
-rw-r--r--src/quick/items/qquickmousearea.cpp1
-rw-r--r--tests/auto/qmltest/events/tst_drag.qml10
-rw-r--r--tests/auto/quick/qquickmousearea/tst_qquickmousearea.cpp38
3 files changed, 28 insertions, 21 deletions
diff --git a/src/quick/items/qquickmousearea.cpp b/src/quick/items/qquickmousearea.cpp
index 2fa52d9ed2..ad0a265035 100644
--- a/src/quick/items/qquickmousearea.cpp
+++ b/src/quick/items/qquickmousearea.cpp
@@ -828,6 +828,7 @@ void QQuickMouseArea::mouseMoveEvent(QMouseEvent *event)
|| QQuickWindowPrivate::dragOverThreshold(dragPos.y() - startPos.y(), Qt::YAxis, event))) {
setKeepMouseGrab(true);
d->stealMouse = true;
+ d->startScene = event->windowPos();
}
d->moved = true;
diff --git a/tests/auto/qmltest/events/tst_drag.qml b/tests/auto/qmltest/events/tst_drag.qml
index 7a17007495..70e0ea2096 100644
--- a/tests/auto/qmltest/events/tst_drag.qml
+++ b/tests/auto/qmltest/events/tst_drag.qml
@@ -47,6 +47,10 @@ Rectangle{
width:200
height:200
+ TestUtil {
+ id: util
+ }
+
Rectangle {
id:container
width:20
@@ -67,8 +71,8 @@ Rectangle{
when:windowShown
function test_mouseDrag() {
mouseDrag(container, 10, 10, 20, 30);
- compare(container.x, 20);
- compare(container.y, 30);
+ compare(container.x, 20 - util.dragThreshold - 1);
+ compare(container.y, 30 - util.dragThreshold - 1);
}
}
-} \ No newline at end of file
+}
diff --git a/tests/auto/quick/qquickmousearea/tst_qquickmousearea.cpp b/tests/auto/quick/qquickmousearea/tst_qquickmousearea.cpp
index 327abbebd5..a4bc92449b 100644
--- a/tests/auto/quick/qquickmousearea/tst_qquickmousearea.cpp
+++ b/tests/auto/quick/qquickmousearea/tst_qquickmousearea.cpp
@@ -276,20 +276,22 @@ void tst_QQuickMouseArea::dragging()
// First move event triggers drag, second is acted upon.
// This is due to possibility of higher stacked area taking precedence.
-
+ // The item is moved relative to the position of the mouse when the drag
+ // was triggered, this prevents a sudden change in position when the drag
+ // threshold is exceeded.
QTest::mouseMove(window, QPoint(111,111), 50);
QTest::mouseMove(window, QPoint(116,116), 50);
QTest::mouseMove(window, QPoint(122,122), 50);
QTRY_VERIFY(drag->active());
- QTRY_COMPARE(blackRect->x(), 72.0);
- QCOMPARE(blackRect->y(), 72.0);
+ QTRY_COMPARE(blackRect->x(), 61.0);
+ QCOMPARE(blackRect->y(), 61.0);
QTest::mouseRelease(window, button, 0, QPoint(122,122));
QTRY_VERIFY(!drag->active());
- QCOMPARE(blackRect->x(), 72.0);
- QCOMPARE(blackRect->y(), 72.0);
+ QCOMPARE(blackRect->x(), 61.0);
+ QCOMPARE(blackRect->y(), 61.0);
delete window;
}
@@ -384,14 +386,14 @@ void tst_QQuickMouseArea::setDragOnPressed()
QTest::qWait(50);
QVERIFY(drag->active());
- QCOMPARE(target->x(), 72.0);
+ QCOMPARE(target->x(), 61.0);
QCOMPARE(target->y(), 50.0);
QTest::mouseRelease(window, Qt::LeftButton, 0, QPoint(122,122));
QTest::qWait(50);
QVERIFY(!drag->active());
- QCOMPARE(target->x(), 72.0);
+ QCOMPARE(target->x(), 61.0);
QCOMPARE(target->y(), 50.0);
delete window;
@@ -1052,8 +1054,8 @@ void tst_QQuickMouseArea::disableAfterPress()
QTRY_COMPARE(mousePositionSpy.count(), 2);
QVERIFY(drag->active());
- QCOMPARE(blackRect->x(), 72.0);
- QCOMPARE(blackRect->y(), 72.0);
+ QCOMPARE(blackRect->x(), 61.0);
+ QCOMPARE(blackRect->y(), 61.0);
mouseArea->setEnabled(false);
@@ -1065,8 +1067,8 @@ void tst_QQuickMouseArea::disableAfterPress()
QTRY_COMPARE(mousePositionSpy.count(), 4);
QVERIFY(drag->active());
- QCOMPARE(blackRect->x(), 94.0);
- QCOMPARE(blackRect->y(), 94.0);
+ QCOMPARE(blackRect->x(), 83.0);
+ QCOMPARE(blackRect->y(), 83.0);
QVERIFY(mouseArea->pressed());
QVERIFY(mouseArea->hovered());
@@ -1076,8 +1078,8 @@ void tst_QQuickMouseArea::disableAfterPress()
QTRY_COMPARE(mouseReleaseSpy.count(), 1);
QVERIFY(!drag->active());
- QCOMPARE(blackRect->x(), 94.0);
- QCOMPARE(blackRect->y(), 94.0);
+ QCOMPARE(blackRect->x(), 83.0);
+ QCOMPARE(blackRect->y(), 83.0);
QVERIFY(!mouseArea->pressed());
QVERIFY(!mouseArea->hovered()); // since hover is not enabled
@@ -1348,25 +1350,25 @@ void tst_QQuickMouseArea::changeAxis()
QTest::mouseMove(view, QPoint(122, 122));
QTRY_VERIFY(drag->active());
- QCOMPARE(blackRect->x(), 72.0);
- QCOMPARE(blackRect->y(), 72.0);
+ QCOMPARE(blackRect->x(), 61.0);
+ QCOMPARE(blackRect->y(), 61.0);
QCOMPARE(drag->axis(), QQuickDrag::XAndYAxis);
/* When blackRect.x becomes bigger than 75, the drag axis is changed to
* Drag.YAxis by the QML code. Verify that this happens, and that the drag
* movement is effectively constrained to the Y axis. */
- QTest::mouseMove(view, QPoint(133, 133));
+ QTest::mouseMove(view, QPoint(144, 144));
QTRY_COMPARE(blackRect->x(), 83.0);
QTRY_COMPARE(blackRect->y(), 83.0);
QTRY_COMPARE(drag->axis(), QQuickDrag::YAxis);
- QTest::mouseMove(view, QPoint(144, 144));
+ QTest::mouseMove(view, QPoint(155, 155));
QTRY_COMPARE(blackRect->y(), 94.0);
QCOMPARE(blackRect->x(), 83.0);
- QTest::mouseRelease(view, Qt::LeftButton, 0, QPoint(144, 144));
+ QTest::mouseRelease(view, Qt::LeftButton, 0, QPoint(155, 155));
QTRY_VERIFY(!drag->active());
QCOMPARE(blackRect->x(), 83.0);