aboutsummaryrefslogtreecommitdiffstats
path: root/src/quicktemplates2
diff options
context:
space:
mode:
authorMitch Curtis <mitch.curtis@qt.io>2018-02-13 16:18:26 +0100
committerMitch Curtis <mitch.curtis@qt.io>2018-02-16 15:17:30 +0000
commitf6eb6a3661f5c8c47c539d38b5bcb0a33c22d904 (patch)
tree72dbe00758ea1565f554834d6a71d86984891e85 /src/quicktemplates2
parentacfe7fc8bb19c591a4c44d88aac95b3caa3d445f (diff)
Tumbler: add positionViewAtIndex() function
This calls the respective PathView/ListView function (depending on the value of wrap), and allows users to change the current index without animations. [ChangeLog][Controls][Tumbler] Added positionViewAtIndex() function that calls the respective PathView/ListView function, depending on the value of wrap. This allows changing currentIndex without animations. Task-number: QTBUG-66358 Change-Id: I7fe73bd3e53548ef41a165b68637d99f3171e02e Reviewed-by: J-P Nurmi <jpnurmi@qt.io>
Diffstat (limited to 'src/quicktemplates2')
-rw-r--r--src/quicktemplates2/qquicktumbler.cpp50
-rw-r--r--src/quicktemplates2/qquicktumbler_p.h13
-rw-r--r--src/quicktemplates2/qquicktumbler_p_p.h1
3 files changed, 62 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);