aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/quicktemplates2/qquicktumbler.cpp50
-rw-r--r--src/quicktemplates2/qquicktumbler_p.h13
-rw-r--r--src/quicktemplates2/qquicktumbler_p_p.h1
-rw-r--r--tests/auto/controls/data/tst_tumbler.qml64
4 files changed, 126 insertions, 2 deletions
diff --git a/src/quicktemplates2/qquicktumbler.cpp b/src/quicktemplates2/qquicktumbler.cpp
index 7e9d70be..fa3a5ce4 100644
--- a/src/quicktemplates2/qquicktumbler.cpp
+++ b/src/quicktemplates2/qquicktumbler.cpp
@@ -70,7 +70,8 @@ QT_BEGIN_NAMESPACE
The API is similar to that of views like \l ListView and \l PathView; a
\l model and \l delegate can be set, and the \l count and \l currentItem
- properties provide read-only access to information about the view.
+ properties provide read-only access to information about the view. To
+ position the view at a certain index, use \l positionViewAtIndex().
Unlike views like \l PathView and \l ListView, however, there is always a
current item (when the model isn't empty). This means that when \l count is
@@ -350,6 +351,8 @@ int QQuickTumbler::count() const
The value of this property is \c -1 when \l count is equal to \c 0. In all
other cases, it will be greater than or equal to \c 0.
+
+ \sa currentItem, positionViewAtIndex()
*/
int QQuickTumbler::currentIndex() const
{
@@ -410,6 +413,8 @@ void QQuickTumbler::setCurrentIndex(int currentIndex)
\readonly
This property holds the item at the current index.
+
+ \sa currentIndex, positionViewAtIndex()
*/
QQuickItem *QQuickTumbler::currentItem() const
{
@@ -511,6 +516,41 @@ bool QQuickTumbler::isMoving() const
return d->view && d->view->property("moving").toBool();
}
+/*!
+ \qmlmethod void QtQuick.Controls::Tumbler::positionViewAtIndex(int index, PositionMode mode)
+ \since QtQuick.Controls 2.5 (Qt 5.12)
+
+ Positions the view so that the \a index is at the position specified by \a mode.
+
+ For example:
+
+ \code
+ positionViewAtIndex(10, Tumbler.Center)
+ \endcode
+
+ If \l wrap is true (the default), the modes available to \l {PathView}'s
+ \l {PathView::}{positionViewAtIndex()} function
+ are available, otherwise the modes available to \l {ListView}'s
+ \l {ListView::}{positionViewAtIndex()} function
+ are available.
+
+ \note There is a known limitation that using \c Tumbler.Beginning when \l
+ wrap is \c true will result in the wrong item being positioned at the top
+ of view. As a workaround, pass \c {index - 1}.
+
+ \sa currentIndex
+*/
+void QQuickTumbler::positionViewAtIndex(int index, QQuickTumbler::PositionMode mode)
+{
+ Q_D(QQuickTumbler);
+ if (!d->view) {
+ d->warnAboutIncorrectContentItem();
+ return;
+ }
+
+ QMetaObject::invokeMethod(d->view, "positionViewAtIndex", Q_ARG(int, index), Q_ARG(int, mode));
+}
+
void QQuickTumbler::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
{
Q_D(QQuickTumbler);
@@ -609,7 +649,7 @@ void QQuickTumblerPrivate::setupViewData(QQuickItem *newControlContentItem)
return;
if (viewContentItemType == QQuickTumblerPrivate::UnsupportedContentItemType) {
- qWarning() << "Tumbler: contentItem must contain either a PathView or a ListView";
+ warnAboutIncorrectContentItem();
return;
}
@@ -636,6 +676,12 @@ void QQuickTumblerPrivate::setupViewData(QQuickItem *newControlContentItem)
calculateDisplacements();
}
+void QQuickTumblerPrivate::warnAboutIncorrectContentItem()
+{
+ Q_Q(QQuickTumbler);
+ qmlWarning(q) << "Tumbler: contentItem must contain either a PathView or a ListView";
+}
+
void QQuickTumblerPrivate::syncCurrentIndex()
{
const int actualViewIndex = view->property("currentIndex").toInt();
diff --git a/src/quicktemplates2/qquicktumbler_p.h b/src/quicktemplates2/qquicktumbler_p.h
index 5d4df4a7..3f7c06db 100644
--- a/src/quicktemplates2/qquicktumbler_p.h
+++ b/src/quicktemplates2/qquicktumbler_p.h
@@ -100,6 +100,19 @@ public:
// 2.2 (Qt 5.9)
bool isMoving() const;
+ enum PositionMode {
+ Beginning,
+ Center,
+ End,
+ Visible, // ListView-only
+ Contain,
+ SnapPosition
+ };
+ Q_ENUM(PositionMode)
+
+ // 2.5 (Qt 5.12)
+ Q_REVISION(5) Q_INVOKABLE void positionViewAtIndex(int index, PositionMode mode);
+
Q_SIGNALS:
void modelChanged();
void countChanged();
diff --git a/src/quicktemplates2/qquicktumbler_p_p.h b/src/quicktemplates2/qquicktumbler_p_p.h
index 75b1a396..6f0879b3 100644
--- a/src/quicktemplates2/qquicktumbler_p_p.h
+++ b/src/quicktemplates2/qquicktumbler_p_p.h
@@ -105,6 +105,7 @@ public:
void disconnectFromView();
void setupViewData(QQuickItem *newControlContentItem);
+ void warnAboutIncorrectContentItem();
void syncCurrentIndex();
void setCount(int newCount);
diff --git a/tests/auto/controls/data/tst_tumbler.qml b/tests/auto/controls/data/tst_tumbler.qml
index 058cbeca..d64f7c9c 100644
--- a/tests/auto/controls/data/tst_tumbler.qml
+++ b/tests/auto/controls/data/tst_tumbler.qml
@@ -107,6 +107,10 @@ TestCase {
return Qt.point(tumblerXCenter(), yCenter);
}
+ function itemTopLeftPos(visualItemIndex) {
+ return Qt.point(tumbler.leftPadding, tumbler.topPadding + (tumblerDelegateHeight * visualItemIndex));
+ }
+
function checkItemSizes() {
var contentChildren = tumbler.wrap ? tumblerView.children : tumblerView.contentItem.children;
verify(contentChildren.length >= tumbler.count);
@@ -131,6 +135,21 @@ TestCase {
return null;
}
+ function findDelegateWithText(parent, text) {
+ for (var i = 0; i < parent.children.length; ++i) {
+ var child = parent.children[i];
+ if (child.hasOwnProperty("text") && child.text === text) {
+ return child;
+ }
+
+ var grandChild = findDelegateWithText(child, text);
+ if (grandChild)
+ return grandChild;
+ }
+
+ return null;
+ }
+
property Component noAttachedPropertiesDelegate: Text {
text: modelData
}
@@ -1112,4 +1131,49 @@ TestCase {
var label = row.label;
compare(label.text, "2");
}
+
+ function test_positionViewAtIndex_data() {
+ return [
+ // Should be 20, 21, ... but there is a documented limitation for this in positionViewAtIndex()'s docs.
+ { tag: "wrap=true, mode=Beginning", wrap: true, mode: Tumbler.Beginning, expectedVisibleIndices: [21, 22, 23, 24, 25] },
+ { tag: "wrap=true, mode=Center", wrap: true, mode: Tumbler.Center, expectedVisibleIndices: [18, 19, 20, 21, 22] },
+ { tag: "wrap=true, mode=End", wrap: true, mode: Tumbler.End, expectedVisibleIndices: [16, 17, 18, 19, 20] },
+ // Same as Beginning; should start at 20.
+ { tag: "wrap=true, mode=Contain", wrap: true, mode: Tumbler.Contain, expectedVisibleIndices: [21, 22, 23, 24, 25] },
+ { tag: "wrap=true, mode=SnapPosition", wrap: true, mode: Tumbler.SnapPosition, expectedVisibleIndices: [18, 19, 20, 21, 22] },
+ { tag: "wrap=false, mode=Beginning", wrap: false, mode: Tumbler.Beginning, expectedVisibleIndices: [20, 21, 22, 23, 24] },
+ { tag: "wrap=false, mode=Center", wrap: false, mode: Tumbler.Center, expectedVisibleIndices: [18, 19, 20, 21, 22] },
+ { tag: "wrap=false, mode=End", wrap: false, mode: Tumbler.End, expectedVisibleIndices: [16, 17, 18, 19, 20] },
+ { tag: "wrap=false, mode=Visible", wrap: false, mode: Tumbler.Visible, expectedVisibleIndices: [16, 17, 18, 19, 20] },
+ { tag: "wrap=false, mode=Contain", wrap: false, mode: Tumbler.Contain, expectedVisibleIndices: [16, 17, 18, 19, 20] },
+ { tag: "wrap=false, mode=SnapPosition", wrap: false, mode: Tumbler.SnapPosition, expectedVisibleIndices: [18, 19, 20, 21, 22] }
+ ]
+ }
+
+ function test_positionViewAtIndex(data) {
+ createTumbler({ wrap: data.wrap, model: 40, visibleItemCount: 5 })
+ compare(tumbler.wrap, data.wrap)
+
+ waitForRendering(tumbler)
+
+ tumbler.positionViewAtIndex(20, data.mode)
+ tryCompare(tumbler, "moving", false)
+
+ compare(tumbler.visibleItemCount, 5)
+ for (var i = 0; i < 5; ++i) {
+ // Find the item through its text, as that's easier than child/itemAt().
+ var text = data.expectedVisibleIndices[i].toString()
+ var item = findDelegateWithText(tumblerView, text)
+ verify(item, "found no item with text \"" + text + "\"")
+ compare(item.text, data.expectedVisibleIndices[i].toString())
+
+ // Ensure that it's at the position we expect.
+ var expectedPos = itemTopLeftPos(i)
+ var actualPos = testCase.mapFromItem(item, 0, 0)
+ compare(actualPos.x, expectedPos.x, "expected delegate with text " + item.text
+ + " to have an x pos of " + expectedPos.x + " but it was " + actualPos.x)
+ compare(actualPos.y, expectedPos.y, "expected delegate with text " + item.text
+ + " to have an y pos of " + expectedPos.y + " but it was " + actualPos.y)
+ }
+ }
}