aboutsummaryrefslogtreecommitdiffstats
path: root/src/quicktemplates2/qquickswipeview.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/quicktemplates2/qquickswipeview.cpp')
-rw-r--r--src/quicktemplates2/qquickswipeview.cpp155
1 files changed, 122 insertions, 33 deletions
diff --git a/src/quicktemplates2/qquickswipeview.cpp b/src/quicktemplates2/qquickswipeview.cpp
index 82d05fbe..ef98c59b 100644
--- a/src/quicktemplates2/qquickswipeview.cpp
+++ b/src/quicktemplates2/qquickswipeview.cpp
@@ -69,6 +69,29 @@ QT_BEGIN_NAMESPACE
\l {Container::moveItem()}{move}, and \l {Container::removeItem()}{remove}
pages dynamically at run time.
+ It is generally not advisable to add excessive amounts of pages to a
+ SwipeView. However, when the amount of pages grows larger, or individual
+ pages are relatively complex, it may be desired free up resources by
+ unloading pages that are outside the reach. The following example presents
+ how to use \l Loader to keep a maximum of three pages simultaneously
+ instantiated.
+
+ \code
+ SwipeView {
+ Repeater {
+ model: 6
+ Loader {
+ active: SwipeView.isCurrentItem || SwipeView.isNextItem || SwipeView.isPreviousItem
+ sourceComponent: Text {
+ text: index
+ Component.onCompleted: console.log("created:", index)
+ Component.onDestruction: console.log("destroyed:", index)
+ }
+ }
+ }
+ }
+ \endcode
+
\note SwipeView takes over the geometry management of items added to the
view. Using anchors on the items is not supported, and any \c width
or \c height assignment will be overridden by the view. Notice that
@@ -83,10 +106,14 @@ class QQuickSwipeViewPrivate : public QQuickContainerPrivate
Q_DECLARE_PUBLIC(QQuickSwipeView)
public:
+ QQuickSwipeViewPrivate() : interactive(true) { }
+
void resizeItem(QQuickItem *item);
void resizeItems();
static QQuickSwipeViewPrivate *get(QQuickSwipeView *view);
+
+ bool interactive;
};
void QQuickSwipeViewPrivate::resizeItems()
@@ -120,15 +147,34 @@ QQuickSwipeView::QQuickSwipeView(QQuickItem *parent) :
setActiveFocusOnTab(true);
}
-QQuickSwipeViewAttached *QQuickSwipeView::qmlAttachedProperties(QObject *object)
+/*!
+ \since QtQuick.Controls 2.1
+ \qmlproperty bool QtQuick.Controls::SwipeView::interactive
+
+ This property describes whether the user can interact with the SwipeView.
+ The user cannot swipe a view that is not interactive.
+
+ The default value is \c true.
+*/
+bool QQuickSwipeView::isInteractive() const
{
- QQuickItem *item = qobject_cast<QQuickItem *>(object);
- if (!item) {
- qWarning() << "SwipeView: attached properties must be accessed from within a child item";
- return nullptr;
- }
+ Q_D(const QQuickSwipeView);
+ return d->interactive;
+}
+
+void QQuickSwipeView::setInteractive(bool interactive)
+{
+ Q_D(QQuickSwipeView);
+ if (d->interactive == interactive)
+ return;
- return new QQuickSwipeViewAttached(item);
+ d->interactive = interactive;
+ emit interactiveChanged();
+}
+
+QQuickSwipeViewAttached *QQuickSwipeView::qmlAttachedProperties(QObject *object)
+{
+ return new QQuickSwipeViewAttached(object);
}
void QQuickSwipeView::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
@@ -172,6 +218,26 @@ QAccessible::Role QQuickSwipeView::accessibleRole() const
*/
/*!
+ \qmlattachedproperty bool QtQuick.Controls::SwipeView::isNextItem
+ \since QtQuick.Controls 2.1
+ \readonly
+
+ This attached property is \c true if this child is the next item.
+
+ It is attached to each child item of the SwipeView.
+*/
+
+/*!
+ \qmlattachedproperty bool QtQuick.Controls::SwipeView::isPreviousItem
+ \since QtQuick.Controls 2.1
+ \readonly
+
+ This attached property is \c true if this child is the previous item.
+
+ It is attached to each child item of the SwipeView.
+*/
+
+/*!
\qmlattachedproperty SwipeView QtQuick.Controls::SwipeView::view
\readonly
@@ -184,11 +250,11 @@ class QQuickSwipeViewAttachedPrivate : public QObjectPrivate, public QQuickItemC
{
Q_DECLARE_PUBLIC(QQuickSwipeViewAttached)
public:
- QQuickSwipeViewAttachedPrivate(QQuickItem *item) :
- item(item),
+ QQuickSwipeViewAttachedPrivate() :
+ item(nullptr),
swipeView(nullptr),
index(-1),
- isCurrent(false)
+ currentIndex(-1)
{
}
@@ -203,19 +269,16 @@ public:
void itemDestroyed(QQuickItem *) override;
void updateIndex();
- void updateIsCurrent();
+ void updateCurrentIndex();
void setView(QQuickSwipeView *view);
void setIndex(int i);
- void setIsCurrent(bool current);
+ void setCurrentIndex(int i);
QQuickItem *item;
QQuickSwipeView *swipeView;
int index;
- // Better to store this so that we don't need to lump its calculation
- // together with index's calculation, as it would otherwise need to know
- // the old index to know if it should emit the change signal.
- bool isCurrent;
+ int currentIndex;
};
void QQuickSwipeViewAttachedPrivate::updateIndex()
@@ -223,9 +286,9 @@ void QQuickSwipeViewAttachedPrivate::updateIndex()
setIndex(swipeView ? QQuickSwipeViewPrivate::get(swipeView)->contentModel->indexOf(item, nullptr) : -1);
}
-void QQuickSwipeViewAttachedPrivate::updateIsCurrent()
+void QQuickSwipeViewAttachedPrivate::updateCurrentIndex()
{
- setIsCurrent(swipeView ? swipeView->currentIndex() == index : false);
+ setCurrentIndex(swipeView ? swipeView->currentIndex() : -1);
}
void QQuickSwipeViewAttachedPrivate::setView(QQuickSwipeView *view)
@@ -238,7 +301,7 @@ void QQuickSwipeViewAttachedPrivate::setView(QQuickSwipeView *view)
p->removeItemChangeListener(this, QQuickItemPrivate::Children);
disconnect(swipeView, &QQuickSwipeView::currentIndexChanged,
- this, &QQuickSwipeViewAttachedPrivate::updateIsCurrent);
+ this, &QQuickSwipeViewAttachedPrivate::updateCurrentIndex);
disconnect(swipeView, &QQuickSwipeView::contentChildrenChanged,
this, &QQuickSwipeViewAttachedPrivate::updateIndex);
}
@@ -250,7 +313,7 @@ void QQuickSwipeViewAttachedPrivate::setView(QQuickSwipeView *view)
p->addItemChangeListener(this, QQuickItemPrivate::Children);
connect(swipeView, &QQuickSwipeView::currentIndexChanged,
- this, &QQuickSwipeViewAttachedPrivate::updateIsCurrent);
+ this, &QQuickSwipeViewAttachedPrivate::updateCurrentIndex);
connect(swipeView, &QQuickSwipeView::contentChildrenChanged,
this, &QQuickSwipeViewAttachedPrivate::updateIndex);
}
@@ -259,17 +322,26 @@ void QQuickSwipeViewAttachedPrivate::setView(QQuickSwipeView *view)
emit q->viewChanged();
updateIndex();
- updateIsCurrent();
+ updateCurrentIndex();
}
-void QQuickSwipeViewAttachedPrivate::setIsCurrent(bool current)
+void QQuickSwipeViewAttachedPrivate::setCurrentIndex(int i)
{
- if (current == isCurrent)
+ if (i == currentIndex)
return;
- isCurrent = current;
Q_Q(QQuickSwipeViewAttached);
- emit q->isCurrentItemChanged();
+ const bool wasCurrent = q->isCurrentItem();
+ const bool wasNext = q->isNextItem();
+ const bool wasPrevious = q->isPreviousItem();
+
+ currentIndex = i;
+ if (wasCurrent != q->isCurrentItem())
+ emit q->isCurrentItemChanged();
+ if (wasNext != q->isNextItem())
+ emit q->isNextItemChanged();
+ if (wasPrevious != q->isPreviousItem())
+ emit q->isPreviousItemChanged();
}
void QQuickSwipeViewAttachedPrivate::setIndex(int i)
@@ -323,15 +395,20 @@ void QQuickSwipeViewAttachedPrivate::itemDestroyed(QQuickItem *item)
QQuickItemPrivate::get(item)->removeItemChangeListener(this, QQuickItemPrivate::Parent | QQuickItemPrivate::Destroyed);
}
-QQuickSwipeViewAttached::QQuickSwipeViewAttached(QQuickItem *item) :
- QObject(*(new QQuickSwipeViewAttachedPrivate(item)), item)
+QQuickSwipeViewAttached::QQuickSwipeViewAttached(QObject *parent) :
+ QObject(*(new QQuickSwipeViewAttachedPrivate), parent)
{
Q_D(QQuickSwipeViewAttached);
- if (item->parentItem())
- d->updateView(item->parentItem());
-
- QQuickItemPrivate *p = QQuickItemPrivate::get(item);
- p->addItemChangeListener(d, QQuickItemPrivate::Parent | QQuickItemPrivate::Destroyed);
+ d->item = qobject_cast<QQuickItem *>(parent);
+ if (d->item) {
+ if (d->item->parentItem())
+ d->updateView(d->item->parentItem());
+
+ QQuickItemPrivate *p = QQuickItemPrivate::get(d->item);
+ p->addItemChangeListener(d, QQuickItemPrivate::Parent | QQuickItemPrivate::Destroyed);
+ } else if (parent) {
+ qmlInfo(parent) << "SwipeView: attached properties must be accessed from within a child item";
+ }
}
QQuickSwipeViewAttached::~QQuickSwipeViewAttached()
@@ -357,7 +434,19 @@ int QQuickSwipeViewAttached::index() const
bool QQuickSwipeViewAttached::isCurrentItem() const
{
Q_D(const QQuickSwipeViewAttached);
- return d->swipeView ? d->swipeView->currentIndex() == d->index : false;
+ return d->index != -1 && d->currentIndex != -1 && d->index == d->currentIndex;
+}
+
+bool QQuickSwipeViewAttached::isNextItem() const
+{
+ Q_D(const QQuickSwipeViewAttached);
+ return d->index != -1 && d->currentIndex != -1 && d->index == d->currentIndex + 1;
+}
+
+bool QQuickSwipeViewAttached::isPreviousItem() const
+{
+ Q_D(const QQuickSwipeViewAttached);
+ return d->index != -1 && d->currentIndex != -1 && d->index == d->currentIndex - 1;
}
QT_END_NAMESPACE