aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/items/qquicklistview.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/quick/items/qquicklistview.cpp')
-rw-r--r--src/quick/items/qquicklistview.cpp273
1 files changed, 169 insertions, 104 deletions
diff --git a/src/quick/items/qquicklistview.cpp b/src/quick/items/qquicklistview.cpp
index beb607dde5..5e9a685cf5 100644
--- a/src/quick/items/qquicklistview.cpp
+++ b/src/quick/items/qquicklistview.cpp
@@ -93,7 +93,7 @@ public:
virtual FxViewItem *newViewItem(int index, QQuickItem *item);
virtual void initializeViewItem(FxViewItem *item);
- virtual void releaseItem(FxViewItem *item);
+ virtual bool releaseItem(FxViewItem *item);
virtual void repositionItemAt(FxViewItem *item, int index, qreal sizeBuffer);
virtual void repositionPackageItemAt(QQuickItem *item, int index);
virtual void resetFirstItemPosition(qreal pos = 0.0);
@@ -236,7 +236,7 @@ void QQuickViewSection::setLabelPositioning(int l)
class FxListItemSG : public FxViewItem
{
public:
- FxListItemSG(QQuickItem *i, QQuickListView *v, bool own) : FxViewItem(i, own), section(0), view(v) {
+ FxListItemSG(QQuickItem *i, QQuickListView *v, bool own) : FxViewItem(i, own), view(v) {
attached = static_cast<QQuickListViewAttached*>(qmlAttachedPropertiesObject<QQuickListView>(item));
if (attached)
static_cast<QQuickListViewAttached*>(attached)->setView(view);
@@ -244,12 +244,21 @@ public:
~FxListItemSG() {}
+ inline QQuickItem *section() const {
+ return attached ? static_cast<QQuickListViewAttached*>(attached)->m_sectionItem : 0;
+ }
+ void setSection(QQuickItem *s) {
+ if (!attached)
+ attached = static_cast<QQuickListViewAttached*>(qmlAttachedPropertiesObject<QQuickListView>(item));
+ static_cast<QQuickListViewAttached*>(attached)->m_sectionItem = s;
+ }
+
qreal position() const {
- if (section) {
+ if (section()) {
if (view->orientation() == QQuickListView::Vertical)
- return section->y();
+ return section()->y();
else
- return (view->effectiveLayoutDirection() == Qt::RightToLeft ? -section->width()-section->x() : section->x());
+ return (view->effectiveLayoutDirection() == Qt::RightToLeft ? -section()->width()-section()->x() : section()->x());
} else {
return itemPosition();
}
@@ -261,8 +270,8 @@ public:
return (view->effectiveLayoutDirection() == Qt::RightToLeft ? -item->width()-itemX() : itemX());
}
qreal size() const {
- if (section)
- return (view->orientation() == QQuickListView::Vertical ? item->height()+section->height() : item->width()+section->width());
+ if (section())
+ return (view->orientation() == QQuickListView::Vertical ? item->height()+section()->height() : item->width()+section()->width());
else
return (view->orientation() == QQuickListView::Vertical ? item->height() : item->width());
}
@@ -270,8 +279,8 @@ public:
return (view->orientation() == QQuickListView::Vertical ? item->height() : item->width());
}
qreal sectionSize() const {
- if (section)
- return (view->orientation() == QQuickListView::Vertical ? section->height() : section->width());
+ if (section())
+ return (view->orientation() == QQuickListView::Vertical ? section()->height() : section()->width());
return 0.0;
}
qreal endPosition() const {
@@ -285,14 +294,14 @@ public:
}
void setPosition(qreal pos) {
// position the section immediately even if there is a transition
- if (section) {
+ if (section()) {
if (view->orientation() == QQuickListView::Vertical) {
- section->setY(pos);
+ section()->setY(pos);
} else {
if (view->effectiveLayoutDirection() == Qt::RightToLeft)
- section->setX(-section->width()-pos);
+ section()->setX(-section()->width()-pos);
else
- section->setX(pos);
+ section()->setX(pos);
}
}
moveTo(pointForPosition(pos));
@@ -307,27 +316,23 @@ public:
return (x >= itemX() && x < itemX() + item->width() &&
y >= itemY() && y < itemY() + item->height());
}
- QQuickItemView *itemView() const {
- return view;
- }
- QQuickItem *section;
QQuickListView *view;
private:
QPointF pointForPosition(qreal pos) const {
if (view->orientation() == QQuickListView::Vertical) {
- if (section)
- pos += section->height();
+ if (section())
+ pos += section()->height();
return QPointF(itemX(), pos);
} else {
if (view->effectiveLayoutDirection() == Qt::RightToLeft) {
- if (section)
- pos += section->width();
+ if (section())
+ pos += section()->width();
return QPointF(-item->width() - pos, itemY());
} else {
- if (section)
- pos += section->width();
+ if (section())
+ pos += section()->width();
return QPointF(pos, itemY());
}
}
@@ -566,25 +571,31 @@ void QQuickListViewPrivate::initializeViewItem(FxViewItem *item)
}
}
-void QQuickListViewPrivate::releaseItem(FxViewItem *item)
+bool QQuickListViewPrivate::releaseItem(FxViewItem *item)
{
- if (item) {
- FxListItemSG* listItem = static_cast<FxListItemSG*>(item);
- if (listItem->section) {
- int i = 0;
- do {
- if (!sectionCache[i]) {
- sectionCache[i] = listItem->section;
- sectionCache[i]->setVisible(false);
- listItem->section = 0;
- break;
- }
- ++i;
- } while (i < sectionCacheSize);
- delete listItem->section;
- }
+ if (!item || !model)
+ return true;
+
+ QQuickListViewAttached *att = static_cast<QQuickListViewAttached*>(item->attached);
+
+ bool released = QQuickItemViewPrivate::releaseItem(item);
+ if (released && att && att->m_sectionItem) {
+ // We hold no more references to this item
+ int i = 0;
+ do {
+ if (!sectionCache[i]) {
+ sectionCache[i] = att->m_sectionItem;
+ sectionCache[i]->setVisible(false);
+ att->m_sectionItem = 0;
+ break;
+ }
+ ++i;
+ } while (i < sectionCacheSize);
+ delete att->m_sectionItem;
+ att->m_sectionItem = 0;
}
- QQuickItemViewPrivate::releaseItem(item);
+
+ return released;
}
bool QQuickListViewPrivate::addVisibleItems(qreal fillFrom, qreal fillTo, bool doBuffer)
@@ -629,7 +640,7 @@ bool QQuickListViewPrivate::addVisibleItems(qreal fillFrom, qreal fillTo, bool d
#endif
if (!(item = static_cast<FxListItemSG*>(createItem(modelIndex, doBuffer))))
break;
- if (!canTransition(FxViewItemTransitionManager::PopulateTransition, true)) // pos will be set by layoutVisibleItems()
+ if (!transitioner || !transitioner->canTransition(QQuickItemViewTransitioner::PopulateTransition, true)) // pos will be set by layoutVisibleItems()
item->setPosition(pos);
item->item->setVisible(!doBuffer);
pos += item->size() + spacing;
@@ -649,7 +660,7 @@ bool QQuickListViewPrivate::addVisibleItems(qreal fillFrom, qreal fillTo, bool d
break;
--visibleIndex;
visiblePos -= item->size() + spacing;
- if (!canTransition(FxViewItemTransitionManager::PopulateTransition, true)) // pos will be set by layoutVisibleItems()
+ if (!transitioner || !transitioner->canTransition(QQuickItemViewTransitioner::PopulateTransition, true)) // pos will be set by layoutVisibleItems()
item->setPosition(visiblePos);
item->item->setVisible(!doBuffer);
visibleItems.prepend(item);
@@ -944,18 +955,18 @@ void QQuickListViewPrivate::updateInlineSection(FxListItemSG *listItem)
if (listItem->attached->m_prevSection != listItem->attached->m_section
&& (sectionCriteria->labelPositioning() & QQuickViewSection::InlineLabels
|| (listItem->index == 0 && sectionCriteria->labelPositioning() & QQuickViewSection::CurrentLabelAtStart))) {
- if (!listItem->section) {
+ if (!listItem->section()) {
qreal pos = listItem->position();
- listItem->section = getSectionItem(listItem->attached->m_section);
+ listItem->setSection(getSectionItem(listItem->attached->m_section));
listItem->setPosition(pos);
} else {
- QQmlContext *context = QQmlEngine::contextForObject(listItem->section)->parentContext();
+ QQmlContext *context = QQmlEngine::contextForObject(listItem->section())->parentContext();
context->setContextProperty(QLatin1String("section"), listItem->attached->m_section);
}
- } else if (listItem->section) {
+ } else if (listItem->section()) {
qreal pos = listItem->position();
- releaseSectionItem(listItem->section);
- listItem->section = 0;
+ releaseSectionItem(listItem->section());
+ listItem->setSection(0);
listItem->setPosition(pos);
}
}
@@ -972,7 +983,7 @@ void QQuickListViewPrivate::updateStickySections()
QQuickItem *lastSectionItem = 0;
int index = 0;
while (index < visibleItems.count()) {
- if (QQuickItem *section = static_cast<FxListItemSG *>(visibleItems.at(index))->section) {
+ if (QQuickItem *section = static_cast<FxListItemSG *>(visibleItems.at(index))->section()) {
// Find the current section header and last visible section header
// and hide them if they will overlap a static section header.
qreal sectionPos = orient == QQuickListView::Vertical ? section->y() : section->x();
@@ -1173,9 +1184,9 @@ void QQuickListViewPrivate::initializeCurrentItem()
if (currentItem) {
FxListItemSG *listItem = static_cast<FxListItemSG *>(currentItem);
- // don't reposition the item if it's about to be transitioned to another position
+ // don't reposition the item if it is already in the visibleItems list
FxViewItem *actualItem = visibleItem(currentIndex);
- if ((!actualItem || !actualItem->transitionScheduledOrRunning())) {
+ if (!actualItem) {
if (currentIndex == visibleIndex - 1 && visibleItems.count()) {
// We can calculate exact postion in this case
listItem->setPosition(visibleItems.first()->position() - currentItem->size() - spacing);
@@ -1186,12 +1197,6 @@ void QQuickListViewPrivate::initializeCurrentItem()
}
}
- // Avoid showing section delegate twice. We still need the section heading so that
- // currentItem positioning works correctly.
- // This is slightly sub-optimal, but section heading caching minimizes the impact.
- if (listItem->section)
- listItem->section->setVisible(false);
-
if (visibleItems.isEmpty())
averageSize = listItem->size();
}
@@ -2245,10 +2250,11 @@ void QQuickListView::setSnapMode(SnapMode mode)
/*!
\qmlproperty Transition QtQuick2::ListView::populate
- This property holds the transition to apply to items that are initially created for a
- view.
- This transition is applied to all the items that are created when:
+ This property holds the transition to apply to the items that are initially created
+ for a view.
+
+ It is applied to all items that are created when:
\list
\o The view is first created
@@ -2278,10 +2284,10 @@ void QQuickListView::setSnapMode(SnapMode mode)
/*!
\qmlproperty Transition QtQuick2::ListView::add
- This property holds the transition to apply to items that are added within the view.
- The transition is applied to items that have been added to the visible area of the view. For
- example, here is a view that specifies such a transition:
+ This property holds the transition to apply to items that are added to the view.
+
+ For example, here is a view that specifies such a transition:
\code
ListView {
@@ -2295,8 +2301,8 @@ void QQuickListView::setSnapMode(SnapMode mode)
Whenever an item is added to the above view, the item will be animated from the position (100,100)
to its final x,y position within the view, over one second. The transition only applies to
the new items that are added to the view; it does not apply to the items below that are
- displaced by the addition of the new items. To animate the displaced items, set the \l
- addDisplaced property.
+ displaced by the addition of the new items. To animate the displaced items, set the \l displaced
+ or \l addDisplaced properties.
For more details and examples on how to use view transitions, see the ViewTransition
documentation.
@@ -2310,11 +2316,11 @@ void QQuickListView::setSnapMode(SnapMode mode)
/*!
\qmlproperty Transition QtQuick2::ListView::addDisplaced
- This property holds the transition to apply to items in the view that are displaced by other
- items that have been added to the view.
- The transition is applied to items that are currently visible and have been displaced by newly
- added items. For example, here is a view that specifies such a transition:
+ This property holds the transition to apply to items within the view that are displaced by
+ the addition of other items to the view.
+
+ For example, here is a view that specifies such a transition:
\code
ListView {
@@ -2332,6 +2338,11 @@ void QQuickListView::setSnapMode(SnapMode mode)
the new item that has been added to the view; to animate the added items, set the \l add
property.
+ If an item is displaced by multiple types of operations at the same time, it is not defined as to
+ whether the addDisplaced, moveDisplaced or removeDisplaced transition will be applied. Additionally,
+ if it is not necessary to specify different transitions depending on whether an item is displaced
+ by an add, move or remove operation, consider setting the \l displaced property instead.
+
For more details and examples on how to use view transitions, see the ViewTransition
documentation.
@@ -2339,16 +2350,16 @@ void QQuickListView::setSnapMode(SnapMode mode)
populated, or when the view's \l model changes. In those cases, the \l populate transition is
applied instead.
- \sa add, populate, ViewTransition
+ \sa displaced, add, populate, ViewTransition
*/
/*!
\qmlproperty Transition QtQuick2::ListView::move
- This property holds the transition to apply to items in the view that are moved by a move
- operation.
- The transition is applied to items that are moving within the view or are moving
- into the view as a result of a move operation in the view's model. For example:
+ This property holds the transition to apply to items in the view that are being moved due
+ to a move operation in the view's \l model.
+
+ For example, here is a view that specifies such a transition:
\code
ListView {
@@ -2359,10 +2370,11 @@ void QQuickListView::setSnapMode(SnapMode mode)
}
\endcode
- Whenever an item is moved within the above view, the item will be animated to its new position in
- the view over one second. The transition only applies to the items that are the subject of the
- move operation in the model; it does not apply to the items below them that are displaced by
- the move operation. To animate the displaced items, set the \l moveDisplaced property.
+ Whenever the \l model performs a move operation to move a particular set of indexes, the
+ respective items in the view will be animated to their new positions in the view over one
+ second. The transition only applies to the items that are the subject of the move operation
+ in the model; it does not apply to items below them that are displaced by the move operation.
+ To animate the displaced items, set the \l displaced or \l moveDisplaced properties.
For more details and examples on how to use view transitions, see the ViewTransition
documentation.
@@ -2372,11 +2384,11 @@ void QQuickListView::setSnapMode(SnapMode mode)
/*!
\qmlproperty Transition QtQuick2::ListView::moveDisplaced
- This property holds the transition to apply to items in the view that are displaced by a
- move operation in the view.
- The transition is applied to items that are currently visible and have been displaced following
- a move operation in the view's model. For example, here is a view that specifies such a transition:
+ This property holds the transition to apply to items that are displaced by a move operation in
+ the view's \l model.
+
+ For example, here is a view that specifies such a transition:
\code
ListView {
@@ -2387,25 +2399,31 @@ void QQuickListView::setSnapMode(SnapMode mode)
}
\endcode
- Whenever an item moves within (or moves into) the above view, all items beneath it are
- displaced, causing them to move upwards (or sideways, if horizontally orientated) within the
- view. As this displacement occurs, the items' movement to their new x,y positions within the
- view will be animated by a NumberAnimation over one second, as specified. This transition is
- not applied to the item that are actually the subject of the move operation; to animate the
- moved items, set the \l move property.
+ Whenever the \l model performs a move operation to move a particular set of indexes, the items
+ between the source and destination indexes of the move operation are displaced, causing them
+ to move upwards or downwards (or sideways, if horizontally orientated) within the view. As this
+ displacement occurs, the items' movement to their new x,y positions within the view will be
+ animated by a NumberAnimation over one second, as specified. This transition is not applied to
+ the items that are the actual subjects of the move operation; to animate the moved items, set
+ the \l move property.
+
+ If an item is displaced by multiple types of operations at the same time, it is not defined as to
+ whether the addDisplaced, moveDisplaced or removeDisplaced transition will be applied. Additionally,
+ if it is not necessary to specify different transitions depending on whether an item is displaced
+ by an add, move or remove operation, consider setting the \l displaced property instead.
For more details and examples on how to use view transitions, see the ViewTransition
documentation.
- \sa move, ViewTransition
+ \sa displaced, move, ViewTransition
*/
/*!
\qmlproperty Transition QtQuick2::ListView::remove
+
This property holds the transition to apply to items that are removed from the view.
- The transition is applied to items that have been removed from the visible area of the view. For
- example:
+ For example, here is a view that specifies such a transition:
\code
ListView {
@@ -2422,8 +2440,8 @@ void QQuickListView::setSnapMode(SnapMode mode)
Whenever an item is removed from the above view, the item will be animated to the position (100,100)
over one second, and in parallel will also change its opacity to 0. The transition
only applies to the items that are removed from the view; it does not apply to the items below
- them that are displaced by the removal of the items. To animate the displaced items, set the \l
- removeDisplaced property.
+ them that are displaced by the removal of the items. To animate the displaced items, set the
+ \l displaced or \l removeDisplaced properties.
Note that by the time the transition is applied, the item has already been removed from the
model; any references to the model data for the removed index will not be valid.
@@ -2439,11 +2457,11 @@ void QQuickListView::setSnapMode(SnapMode mode)
/*!
\qmlproperty Transition QtQuick2::ListView::removeDisplaced
+
This property holds the transition to apply to items in the view that are displaced by the
removal of other items in the view.
- The transition is applied to items that are currently visible and have been displaced by
- the removal of items. For example, here is a view that specifies such a transition:
+ For example, here is a view that specifies such a transition:
\code
ListView {
@@ -2461,12 +2479,52 @@ void QQuickListView::setSnapMode(SnapMode mode)
the item that has actually been removed from the view; to animate the removed items, set the
\l remove property.
+ If an item is displaced by multiple types of operations at the same time, it is not defined as to
+ whether the addDisplaced, moveDisplaced or removeDisplaced transition will be applied. Additionally,
+ if it is not necessary to specify different transitions depending on whether an item is displaced
+ by an add, move or remove operation, consider setting the \l displaced property instead.
+
For more details and examples on how to use view transitions, see the ViewTransition
documentation.
- \sa remove, ViewTransition
+ \sa displaced, remove, ViewTransition
*/
+/*!
+ \qmlproperty Transition QtQuick2::ListView::displaced
+ This property holds the generic transition to apply to items that have been displaced by
+ any model operation that affects the view.
+
+ This is a convenience for specifying the generic transition to be applied to any items
+ that are displaced by an add, move or remove operation, without having to specify the
+ individual addDisplaced, moveDisplaced and removeDisplaced properties. For example, here
+ is a view that specifies a displaced transition:
+
+ \code
+ ListView {
+ ...
+ displaced: Transition {
+ NumberAnimation { properties: "x,y"; duration: 1000 }
+ }
+ }
+ \endcode
+
+ When any item is added, moved or removed within the above view, the items below it are
+ displaced, causing them to move down (or sideways, if horizontally orientated) within the
+ view. As this displacement occurs, the items' movement to their new x,y positions within
+ the view will be animated by a NumberAnimation over one second, as specified.
+
+ If a view specifies this generic displaced transition as well as a specific addDisplaced,
+ moveDisplaced or removeDisplaced transition, the more specific transition will be used
+ instead of the generic displaced transition when the relevant operation occurs, providing that
+ the more specific transition has not been disabled (by setting \l {Transition::enabled}{enabled}
+ to false). If it has indeed been disabled, the generic displaced transition is applied instead.
+
+ For more details and examples on how to use view transitions, see the ViewTransition
+ documentation.
+
+ \sa addDisplaced, moveDisplaced, removeDisplaced, ViewTransition
+*/
void QQuickListView::viewportMoved()
{
@@ -2729,7 +2787,8 @@ bool QQuickListViewPrivate::applyInsertionChange(const QQuickChangeSet::Insert &
insertResult->changedFirstItem = true;
if (!change.isMove()) {
addedItems->append(item);
- transitionNextReposition(item, FxViewItemTransitionManager::AddTransition, true);
+ if (transitioner)
+ transitioner->transitionNextReposition(item, QQuickItemViewTransitioner::AddTransition, true);
}
insertResult->sizeChangesBeforeVisiblePos += item->size() + spacing;
pos -= item->size() + spacing;
@@ -2755,11 +2814,12 @@ bool QQuickListViewPrivate::applyInsertionChange(const QQuickChangeSet::Insert &
if (change.isMove()) {
// we know this is a move target, since move displaced items that are
// shuffled into view due to a move would be added in refill()
- if (canTransition(FxViewItemTransitionManager::MoveTransition, true) && newItem)
+ if (newItem && transitioner && transitioner->canTransition(QQuickItemViewTransitioner::MoveTransition, true))
movingIntoView->append(MovedItem(item, change.moveKey(item->index)));
} else {
addedItems->append(item);
- transitionNextReposition(item, FxViewItemTransitionManager::AddTransition, true);
+ if (transitioner)
+ transitioner->transitionNextReposition(item, QQuickItemViewTransitioner::AddTransition, true);
}
insertResult->sizeChangesAfterVisiblePos += item->size() + spacing;
pos += item->size() + spacing;
@@ -2771,10 +2831,12 @@ bool QQuickListViewPrivate::applyInsertionChange(const QQuickChangeSet::Insert &
FxViewItem *item = visibleItems.at(index);
if (item->index != -1)
item->index += count;
- if (change.isMove())
- transitionNextReposition(item, FxViewItemTransitionManager::MoveTransition, false);
- else
- transitionNextReposition(item, FxViewItemTransitionManager::AddTransition, false);
+ if (transitioner) {
+ if (change.isMove())
+ transitioner->transitionNextReposition(item, QQuickItemViewTransitioner::MoveTransition, false);
+ else
+ transitioner->transitionNextReposition(item, QQuickItemViewTransitioner::AddTransition, false);
+ }
}
updateVisibleIndex();
@@ -2786,6 +2848,9 @@ void QQuickListViewPrivate::translateAndTransitionItemsAfter(int afterModelIndex
{
Q_UNUSED(insertionResult);
+ if (!transitioner)
+ return;
+
int markerItemIndex = -1;
for (int i=0; i<visibleItems.count(); i++) {
if (visibleItems[i]->index == afterModelIndex) {
@@ -2805,7 +2870,7 @@ void QQuickListViewPrivate::translateAndTransitionItemsAfter(int afterModelIndex
if (!listItem->transitionScheduledOrRunning()) {
qreal pos = listItem->position();
listItem->setPosition(pos - sizeRemoved);
- transitionNextReposition(listItem, FxViewItemTransitionManager::RemoveTransition, false);
+ transitioner->transitionNextReposition(listItem, QQuickItemViewTransitioner::RemoveTransition, false);
listItem->setPosition(pos);
}
}