Skip to content

Commit

Permalink
Restore view to sensible position if grab is cancelled.
Browse files Browse the repository at this point in the history
If the mouse grab is stolen, return to allowed bounds.

Change-Id: Icc44da32ff62bed273f0ccbb5498766981cdf9a4
Reviewed-by: Michael Brasser <michael.brasser@nokia.com>
  • Loading branch information
Martin Jones authored and Qt by Nokia committed Mar 20, 2012
1 parent 610df5c commit 5d901a7
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/quick/items/qquickflickable.cpp
Expand Up @@ -1763,6 +1763,10 @@ void QQuickFlickable::mouseUngrabEvent()
d->draggingEnding();
d->stealMouse = false;
setKeepMouseGrab(false);
d->fixupX();
d->fixupY();
if (!d->timeline.isActive())
movementEnding();
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/quick/items/qquickpathview.cpp
Expand Up @@ -1422,6 +1422,9 @@ void QQuickPathView::mouseUngrabEvent()
d->stealMouse = false;
setKeepMouseGrab(false);
d->lastPosTime.invalidate();
d->fixOffset();
if (!d->tl.isActive())
movementEnding();
}
}

Expand Down
15 changes: 15 additions & 0 deletions tests/auto/quick/qquickflickable/data/cancel.qml
@@ -0,0 +1,15 @@
import QtQuick 2.0

Flickable {
width: 200; height: 200
contentWidth: row.width; contentHeight: row.height

Row {
id: row
objectName: "row"
Repeater {
model: 4
Rectangle { width: 400; height: 600; color: "yellow"; border.width: 1 }
}
}
}
36 changes: 36 additions & 0 deletions tests/auto/quick/qquickflickable/tst_qquickflickable.cpp
Expand Up @@ -77,6 +77,7 @@ private slots:
void disabled();
void flickVelocity();
void margins();
void cancel();

private:
QQmlEngine engine;
Expand Down Expand Up @@ -657,6 +658,41 @@ void tst_qquickflickable::margins()
delete root;
}

void tst_qquickflickable::cancel()
{
QQuickView *canvas = new QQuickView;
canvas->setSource(testFileUrl("cancel.qml"));
canvas->show();
canvas->requestActivateWindow();
QVERIFY(canvas->rootObject() != 0);

QQuickFlickable *flickable = qobject_cast<QQuickFlickable*>(canvas->rootObject());
QVERIFY(flickable != 0);

QTest::mousePress(canvas, Qt::LeftButton, 0, QPoint(10, 10));
// drag out of bounds
QTest::mouseMove(canvas, QPoint(50, 50));
QTest::mouseMove(canvas, QPoint(100, 100));
QTest::mouseMove(canvas, QPoint(150, 150));

QVERIFY(flickable->contentX() != 0);
QVERIFY(flickable->contentY() != 0);
QVERIFY(flickable->isMoving());
QVERIFY(flickable->isDragging());

// grabbing mouse will cancel flickable interaction.
QQuickItem *item = canvas->rootObject()->findChild<QQuickItem*>("row");
item->grabMouse();

QTRY_COMPARE(flickable->contentX(), 0.);
QTRY_COMPARE(flickable->contentY(), 0.);
QTRY_VERIFY(!flickable->isMoving());
QTRY_VERIFY(!flickable->isDragging());

QTest::mouseRelease(canvas, Qt::LeftButton, 0, QPoint(50, 10));
}


QTEST_MAIN(tst_qquickflickable)

#include "tst_qquickflickable.moc"
1 change: 1 addition & 0 deletions tests/auto/quick/qquickpathview/data/dragpath.qml
Expand Up @@ -14,6 +14,7 @@ PathView {
preferredHighlightBegin: 0.5
preferredHighlightEnd: 0.5
Text {
objectName: "text"
text: "current index: " + parent.currentIndex
}
}
36 changes: 36 additions & 0 deletions tests/auto/quick/qquickpathview/tst_qquickpathview.cpp
Expand Up @@ -123,6 +123,7 @@ private slots:
void creationContext();
void currentOffsetOnInsertion();
void asynchronous();
void cancelDrag();
};

class TestObject : public QObject
Expand Down Expand Up @@ -1453,6 +1454,41 @@ void tst_QQuickPathView::missingPercent()
delete obj;
}

void tst_QQuickPathView::cancelDrag()
{
QQuickView *canvas = createView();
canvas->setSource(testFileUrl("dragpath.qml"));
canvas->show();
canvas->requestActivateWindow();
QTest::qWaitForWindowShown(canvas);
QTRY_COMPARE(canvas, qGuiApp->focusWindow());

QQuickPathView *pathview = qobject_cast<QQuickPathView*>(canvas->rootObject());
QVERIFY(pathview != 0);

// drag between snap points
QTest::mousePress(canvas, Qt::LeftButton, 0, QPoint(10,100));
QTest::qWait(100);
QTest::mouseMove(canvas, QPoint(30, 100));
QTest::mouseMove(canvas, QPoint(85, 100));

QTRY_VERIFY(pathview->offset() != qFloor(pathview->offset()));
QTRY_VERIFY(pathview->isMoving());

// steal mouse grab - cancels PathView dragging
QQuickItem *item = canvas->rootObject()->findChild<QQuickItem*>("text");
item->grabMouse();

// returns to a snap point.
QTRY_VERIFY(pathview->offset() == qFloor(pathview->offset()));
QTRY_VERIFY(!pathview->isMoving());

QTest::mouseRelease(canvas, Qt::LeftButton, 0, QPoint(40,100));

delete canvas;
}


QTEST_MAIN(tst_QQuickPathView)

#include "tst_qquickpathview.moc"

0 comments on commit 5d901a7

Please sign in to comment.