aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorShawn Rutledge <shawn.rutledge@qt.io>2019-04-05 15:00:59 +0200
committerJan Arve Sæther <jan-arve.saether@qt.io>2019-04-05 14:31:56 +0000
commit0c02691f7ba646e782b04347fde3d75f9582b317 (patch)
treedd8b7c690a86a03faaa0831dff934ee8629cc1dd
parentaba6cea90e023c99ac55b0e8dcdeae0af8305ef2 (diff)
If DragHandler is dragged within its margin, don't jump
Not being pressed inside the target is a necessary but not sufficient reason to reset m_pressTargetPos to the center of the target. The intention was rather to make the target jump into position when the parent was a different item: e.g. if a Slider has a DragHandler whose target is the slider's knob, you can start dragging anywhere on the whole Slider but you want the knob to jump to the cursor position when the drag begins. While we're at it, both branches of the if in onGrabChanged() are checking that target() isn't null, so we can move that check out. Fixes: QTBUG-74966 Change-Id: I05be11d27422b070d941b9e43d4e1157e071c3a5 Reviewed-by: Jan Arve Sæther <jan-arve.saether@qt.io>
-rw-r--r--src/quick/handlers/qquickdraghandler.cpp7
-rw-r--r--tests/auto/quick/pointerhandlers/qquickdraghandler/data/dragMargin.qml36
-rw-r--r--tests/auto/quick/pointerhandlers/qquickdraghandler/tst_qquickdraghandler.cpp33
3 files changed, 72 insertions, 4 deletions
diff --git a/src/quick/handlers/qquickdraghandler.cpp b/src/quick/handlers/qquickdraghandler.cpp
index 48f0599284..e5531369cf 100644
--- a/src/quick/handlers/qquickdraghandler.cpp
+++ b/src/quick/handlers/qquickdraghandler.cpp
@@ -112,14 +112,13 @@ QPointF QQuickDragHandler::targetCentroidPosition()
void QQuickDragHandler::onGrabChanged(QQuickPointerHandler *grabber, QQuickEventPoint::GrabTransition transition, QQuickEventPoint *point)
{
QQuickMultiPointHandler::onGrabChanged(grabber, transition, point);
- if (grabber == this && transition == QQuickEventPoint::GrabExclusive) {
+ if (grabber == this && transition == QQuickEventPoint::GrabExclusive && target()) {
// In case the grab got handed over from another grabber, we might not get the Press.
if (!m_pressedInsideTarget) {
- if (target())
+ if (target() != parentItem())
m_pressTargetPos = QPointF(target()->width(), target()->height()) / 2;
} else if (m_pressTargetPos.isNull()) {
- if (target())
- m_pressTargetPos = targetCentroidPosition();
+ m_pressTargetPos = targetCentroidPosition();
}
}
}
diff --git a/tests/auto/quick/pointerhandlers/qquickdraghandler/data/dragMargin.qml b/tests/auto/quick/pointerhandlers/qquickdraghandler/data/dragMargin.qml
new file mode 100644
index 0000000000..e5ca681bd5
--- /dev/null
+++ b/tests/auto/quick/pointerhandlers/qquickdraghandler/data/dragMargin.qml
@@ -0,0 +1,36 @@
+import QtQuick 2.12
+
+Rectangle {
+ color: "#333"
+ width: 480; height: 480
+
+ Rectangle {
+ color: "#112"
+ width: 100
+ height: 100
+ x: 50; y: 50
+
+ DragHandler {
+ id: dragHandler
+ margin: 20
+ }
+
+ Rectangle {
+ id: rect
+ anchors.fill: parent
+ anchors.margins: -dragHandler.margin
+ color: "transparent"
+ border.color: "cyan"
+ border.width: 2
+ radius: 10
+ antialiasing: true
+
+ Text {
+ color: "cyan"
+ text: "drag this margin area"
+ font.pixelSize: 10
+ anchors.horizontalCenter: parent.horizontalCenter
+ }
+ }
+ }
+}
diff --git a/tests/auto/quick/pointerhandlers/qquickdraghandler/tst_qquickdraghandler.cpp b/tests/auto/quick/pointerhandlers/qquickdraghandler/tst_qquickdraghandler.cpp
index eb210c2112..cc8c567e5c 100644
--- a/tests/auto/quick/pointerhandlers/qquickdraghandler/tst_qquickdraghandler.cpp
+++ b/tests/auto/quick/pointerhandlers/qquickdraghandler/tst_qquickdraghandler.cpp
@@ -55,6 +55,7 @@ private slots:
void defaultPropertyValues();
void touchDrag();
void mouseDrag();
+ void dragFromMargin();
void touchDragMulti();
void touchDragMultiSliders_data();
void touchDragMultiSliders();
@@ -251,6 +252,38 @@ void tst_DragHandler::mouseDrag()
QCOMPARE(centroidChangedSpy.count(), 5);
}
+void tst_DragHandler::dragFromMargin() // QTBUG-74966
+{
+ const int dragThreshold = QGuiApplication::styleHints()->startDragDistance();
+ QScopedPointer<QQuickView> windowPtr;
+ createView(windowPtr, "dragMargin.qml");
+ QQuickView * window = windowPtr.data();
+
+ QQuickItem *draggableItem = window->rootObject()->childItems().first();
+ QVERIFY(draggableItem);
+ QQuickDragHandler *dragHandler = draggableItem->findChild<QQuickDragHandler*>();
+ QVERIFY(dragHandler);
+
+ QPointF originalPos = draggableItem->position();
+ QPointF scenePressPos = originalPos - QPointF(10, 0);
+ QPoint p1 = scenePressPos.toPoint();
+ QTest::mousePress(window, Qt::LeftButton, Qt::NoModifier, p1);
+ QVERIFY(!dragHandler->active());
+ QCOMPARE(dragHandler->centroid().scenePosition(), scenePressPos);
+ QCOMPARE(dragHandler->centroid().scenePressPosition(), scenePressPos);
+ p1 += QPoint(dragThreshold * 2, 0);
+ QTest::mouseMove(window, p1);
+ QTRY_VERIFY(dragHandler->active());
+ QCOMPARE(dragHandler->centroid().scenePressPosition(), scenePressPos);
+ QCOMPARE(dragHandler->centroid().sceneGrabPosition(), p1);
+ QCOMPARE(dragHandler->translation().x(), 0.0); // hmm that's odd
+ QCOMPARE(dragHandler->translation().y(), 0.0);
+ QCOMPARE(draggableItem->position(), originalPos + QPointF(dragThreshold * 2, 0));
+ QTest::mouseRelease(window, Qt::LeftButton, Qt::NoModifier, p1);
+ QTRY_VERIFY(!dragHandler->active());
+ QCOMPARE(dragHandler->centroid().pressedButtons(), Qt::NoButton);
+}
+
void tst_DragHandler::touchDragMulti()
{
const int dragThreshold = QGuiApplication::styleHints()->startDragDistance();