aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMitch Curtis <mitch.curtis@qt.io>2019-09-24 10:09:12 +0200
committerMitch Curtis <mitch.curtis@qt.io>2019-09-24 14:53:38 +0200
commitd39a6d80f63414a4e933ac2f859805635850942a (patch)
tree4c212c06d8bb669f5643cfb35e3942ce884732ba
parent2275541b52a8879cd8c7bf281b385738e5a664fe (diff)
Tumbler: fix displacement calculation when wrap is false
Use the position of the item and the currentItem in the calculation in order to get reliable results. This fixes the displacement being off by a small margin, which increased as the delegate height became smaller. Fixes: QTBUG-66799 Change-Id: Ieca5033fb4c0ed62f5965a21fcab7aa558bd40e6 Reviewed-by: Andy Shaw <andy.shaw@qt.io> Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
-rw-r--r--src/quicktemplates2/qquicktumbler.cpp17
-rw-r--r--tests/auto/controls/data/tst_tumbler.qml27
2 files changed, 38 insertions, 6 deletions
diff --git a/src/quicktemplates2/qquicktumbler.cpp b/src/quicktemplates2/qquicktumbler.cpp
index 8b702c60..85c70b1b 100644
--- a/src/quicktemplates2/qquicktumbler.cpp
+++ b/src/quicktemplates2/qquicktumbler.cpp
@@ -949,9 +949,20 @@ void QQuickTumblerAttachedPrivate::calculateDisplacement()
const qreal contentY = tumblerPrivate->viewContentY;
const qreal delegateH = delegateHeight(tumbler);
const qreal preferredHighlightBegin = tumblerPrivate->view->property("preferredHighlightBegin").toReal();
- // Tumbler's displacement goes from negative at the top to positive towards the bottom, so we must switch this around.
- const qreal reverseDisplacement = (contentY + preferredHighlightBegin) / delegateH;
- displacement = reverseDisplacement - index;
+ const qreal itemY = qobject_cast<QQuickItem*>(parent)->y();
+ qreal currentItemY = 0;
+ auto currentItem = tumblerPrivate->view->property("currentItem").value<QQuickItem*>();
+ if (currentItem)
+ currentItemY = currentItem->y();
+ // Start from the y position of the current item.
+ const qreal topOfCurrentItemInViewport = currentItemY - contentY;
+ // Then, calculate the distance between it and the preferredHighlightBegin.
+ const qreal relativePositionToPreferredHighlightBegin = topOfCurrentItemInViewport - preferredHighlightBegin;
+ // Next, calculate the distance between us and the current item.
+ const qreal distanceFromCurrentItem = currentItemY - itemY;
+ const qreal displacementInPixels = distanceFromCurrentItem - relativePositionToPreferredHighlightBegin;
+ // Convert it from pixels to a floating point index.
+ displacement = displacementInPixels / delegateH;
}
emitIfDisplacementChanged(previousDisplacement, displacement);
diff --git a/tests/auto/controls/data/tst_tumbler.qml b/tests/auto/controls/data/tst_tumbler.qml
index c9cc10d7..5b3ef6e3 100644
--- a/tests/auto/controls/data/tst_tumbler.qml
+++ b/tests/auto/controls/data/tst_tumbler.qml
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2017 The Qt Company Ltd.
+** Copyright (C) 2019 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
@@ -54,8 +54,8 @@ import QtQuick.Controls 2.12
TestCase {
id: testCase
- width: 200
- height: 200
+ width: 300
+ height: 300
visible: true
when: windowShown
name: "Tumbler"
@@ -513,6 +513,7 @@ TestCase {
Text {
text: parent.displacement.toFixed(2)
anchors.right: parent.right
+ anchors.verticalCenter: parent.verticalCenter
}
property real displacement: Tumbler.displacement
@@ -1236,4 +1237,24 @@ TestCase {
// 5 - 2 = 3
compare(tumbler.currentIndex, 3);
}
+
+ function test_displacementAfterResizing() {
+ createTumbler({
+ width: 200,
+ wrap: false,
+ delegate: displacementDelegate,
+ model: 30,
+ visibleItemCount: 7,
+ currentIndex: 15
+ })
+
+ var delegate = findChild(tumblerView, "delegate15")
+ verify(delegate)
+
+ tryCompare(delegate, "displacement", 0)
+
+ // Resizing the Tumbler shouldn't affect the displacement.
+ tumbler.height *= 1.4
+ tryCompare(delegate, "displacement", 0)
+ }
}