2 * Copyright (C) 2013 Canonical, Ltd.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * Some documentation on how this thing works:
20 * A flickable has two very important concepts that define the top and
21 * height of the flickable area.
22 * The top is returned in minYExtent()
23 * The height is set using setContentHeight()
24 * By changing those two values we can make the list grow up or down
25 * as needed. e.g. if we are in the middle of the list
26 * and something that is above the viewport grows, since we do not
27 * want to change the viewport because of that we just adjust the
28 * minYExtent so that the list grows up.
30 * The implementation on the list relies on the delegateModel doing
31 * most of the instantiation work. You call createItem() when you
32 * need to create an item asking for it async or not. If returns null
33 * it means the item will be created async and the model will call the
34 * itemCreated slot with the item.
36 * updatePolish is the central point of dispatch for the work of the
37 * class. It is called by the scene graph just before drawing the class.
39 * * Make sure all items are positioned correctly
40 * * Add/Remove items if needed
41 * * Update the content height if it was dirty
43 * m_visibleItems contains all the items we have created at the moment.
44 * Actually not all of them are visible since it includes the ones
45 * in the cache area we create asynchronously to help performance.
46 * The first item in m_visibleItems has the m_firstVisibleIndex in
47 * the model. If you actually want to know what is the first
48 * item in the viewport you have to find the first non culled element
51 * All the items (except the header) are childs of m_clipItem which
52 * is a child of the contentItem() of the flickable (The contentItem()
53 * is what actually 'moves' in a a flickable). This way
54 * we can implement the clipping needed so we can have the header
55 * shown in the middle of the list over the items without the items
56 * leaking under the header in case it is transparent.
58 * The first item of m_visibleItems is the one that defines the
59 * positions of all the rest of items (see updatePolish()) and
60 * this is why sometimes we move it even if it's not the item
61 * that has triggered the function (i.e. in itemGeometryChanged())
63 * m_visibleItems is a list of ListItem. Each ListItem
64 * will contain a item and potentially a sectionItem. The sectionItem
65 * is only there when the list is using sectionDelegate+sectionProperty
66 * and this is the first item of the section. Each ListItem is vertically
67 * layouted with the sectionItem first and then the item.
69 * For sectioning we also have a section item alone (m_topSectionItem)
70 * that is used for the cases we need to show the sticky section item at
71 * the top of the view.
73 * Each delegate item has a context property called heightToClip that is
74 * used to communicate to the delegate implementation in case it has to
75 * clip itself because of overlapping with the top sticky section item.
76 * This is an implementation decision since it has been agreed it
77 * is easier to implement the clipping in QML with this info than to
78 * do it at the C++ level.
80 * Note that minYExtent and height are not always totally accurate, since
81 * we don't have the items created we can't guess their heights
82 * so we can only guarantee the values are correct when the first/last
83 * items of the list are visible, otherwise we just live with good enough
84 * values that make the list scrollable
86 * There are a few things that are not really implemented or tested properly
87 * which we don't use at the moment like changing the model, changing
88 * the section delegate, having a section delegate that changes its size, etc.
89 * The known missing features are marked with TODOs along the code.
92#include "listviewwithpageheader.h"
96#include <qqmlengine.h>
97#pragma GCC diagnostic push
98#pragma GCC diagnostic ignored "-pedantic"
99#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
100#include <private/qquickvisualdatamodel_p.h>
102#include <private/qqmldelegatemodel_p.h>
104#include <private/qqmlglobal_p.h>
105#include <private/qquickitem_p.h>
106#include <private/qquickanimation_p.h>
107#pragma GCC diagnostic pop
108// #include <private/qquickrectangle_p.h>
110static const qreal bufferRatio = 0.5;
112qreal ListViewWithPageHeader::ListItem::height() const
114 return m_item->height() + (m_sectionItem ? m_sectionItem->height() : 0);
117qreal ListViewWithPageHeader::ListItem::y() const
119 return m_item->y() - (m_sectionItem ? m_sectionItem->height() : 0);
122void ListViewWithPageHeader::ListItem::setY(qreal newY)
125 m_sectionItem->setY(newY);
126 m_item->setY(newY + m_sectionItem->height());
132bool ListViewWithPageHeader::ListItem::culled() const
134 return QQuickItemPrivate::get(m_item)->culled;
137void ListViewWithPageHeader::ListItem::setCulled(bool culled)
139 QQuickItemPrivate::get(m_item)->setCulled(culled);
141 QQuickItemPrivate::get(m_sectionItem)->setCulled(culled);
144ListViewWithPageHeader::ListViewWithPageHeader()
145 : m_delegateModel(nullptr)
146 , m_asyncRequestedIndex(-1)
147 , m_delegateValidated(false)
148 , m_firstVisibleIndex(-1)
150 , m_contentHeightDirty(false)
151 , m_headerItem(nullptr)
152 , m_previousContentY(0)
153 , m_headerItemShownHeight(0)
154 , m_sectionDelegate(nullptr)
155 , m_topSectionItem(nullptr)
156 , m_forceNoClip(false)
159 m_clipItem = new QQuickItem(contentItem());
160// m_clipItem = new QQuickRectangle(contentItem());
161// ((QQuickRectangle*)m_clipItem)->setColor(Qt::gray);
163 m_contentYAnimation = new QQuickNumberAnimation(this);
164 m_contentYAnimation->setEasing(QEasingCurve::OutQuad);
165 m_contentYAnimation->setProperty("contentY");
166 m_contentYAnimation->setDuration(200);
167 m_contentYAnimation->setTargetObject(this);
169 connect(this, SIGNAL(contentWidthChanged()), this, SLOT(onContentWidthChanged()));
170 connect(this, SIGNAL(contentHeightChanged()), this, SLOT(onContentHeightChanged()));
171 connect(this, SIGNAL(heightChanged()), this, SLOT(onHeightChanged()));
172 connect(m_contentYAnimation, SIGNAL(stopped()), this, SLOT(onShowHeaderAnimationFinished()));
174 setFlickableDirection(VerticalFlick);
177ListViewWithPageHeader::~ListViewWithPageHeader()
181QAbstractItemModel *ListViewWithPageHeader::model() const
183 return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
186void ListViewWithPageHeader::setModel(QAbstractItemModel *model)
188 if (model != this->model()) {
189 if (!m_delegateModel) {
190 createDelegateModel();
192#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
193 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
195 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
196 connect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
198 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
200 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
201 connect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
203 Q_EMIT modelChanged();
206// Q_EMIT contentHeightChanged();
207// Q_EMIT contentYChanged();
211QQmlComponent *ListViewWithPageHeader::delegate() const
213 return m_delegateModel ? m_delegateModel->delegate() : nullptr;
216void ListViewWithPageHeader::setDelegate(QQmlComponent *delegate)
218 if (delegate != this->delegate()) {
219 if (!m_delegateModel) {
220 createDelegateModel();
223 // Cleanup the existing items
224 Q_FOREACH(ListItem *item, m_visibleItems)
226 m_visibleItems.clear();
227 m_firstVisibleIndex = -1;
231 if (m_topSectionItem) {
232 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
235 m_delegateModel->setDelegate(delegate);
237 Q_EMIT delegateChanged();
238 m_delegateValidated = false;
239 m_contentHeightDirty = true;
244QQuickItem *ListViewWithPageHeader::header() const
249void ListViewWithPageHeader::setHeader(QQuickItem *headerItem)
251 if (m_headerItem != headerItem) {
252 qreal oldHeaderHeight = 0;
253 qreal oldHeaderY = 0;
255 oldHeaderHeight = m_headerItem->height();
256 oldHeaderY = m_headerItem->y();
257 m_headerItem->setParentItem(nullptr);
259 m_headerItem = headerItem;
261 m_headerItem->setParentItem(contentItem());
262 m_headerItem->setZ(1);
264 qreal newHeaderHeight = m_headerItem ? m_headerItem->height() : 0;
265 if (!m_visibleItems.isEmpty() && newHeaderHeight != oldHeaderHeight) {
266 headerHeightChanged(newHeaderHeight, oldHeaderHeight, oldHeaderY);
268 m_contentHeightDirty = true;
270 Q_EMIT headerChanged();
274QQmlComponent *ListViewWithPageHeader::sectionDelegate() const
276 return m_sectionDelegate;
279void ListViewWithPageHeader::setSectionDelegate(QQmlComponent *delegate)
281 if (delegate != m_sectionDelegate) {
282 // TODO clean existing sections
284 m_sectionDelegate = delegate;
286 m_topSectionItem = getSectionItem(QString());
287 m_topSectionItem->setZ(3);
288 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
290 // TODO create sections for existing items
292 Q_EMIT sectionDelegateChanged();
296QString ListViewWithPageHeader::sectionProperty() const
298 return m_sectionProperty;
301void ListViewWithPageHeader::setSectionProperty(const QString &property)
303 if (property != m_sectionProperty) {
304 m_sectionProperty = property;
306 updateWatchedRoles();
308 // TODO recreate sections
310 Q_EMIT sectionPropertyChanged();
314bool ListViewWithPageHeader::forceNoClip() const
316 return m_forceNoClip;
319void ListViewWithPageHeader::setForceNoClip(bool noClip)
321 if (noClip != m_forceNoClip) {
322 m_forceNoClip = noClip;
324 Q_EMIT forceNoClipChanged();
328void ListViewWithPageHeader::positionAtBeginning()
330 qreal headerHeight = (m_headerItem ? m_headerItem->height() : 0);
331 if (m_firstVisibleIndex != 0) {
332 // TODO This could be optimized by trying to reuse the interesection
333 // of items that may end up intersecting between the existing
334 // m_visibleItems and the items we are creating in the next loop
335 Q_FOREACH(ListItem *item, m_visibleItems)
337 m_visibleItems.clear();
338 m_firstVisibleIndex = -1;
340 // Create the item 0, it will be already correctly positioned at createItem()
342 ListItem *item = createItem(0, false);
343 // Create the subsequent items
345 qreal pos = item->y() + item->height();
346 const qreal buffer = height() * bufferRatio;
347 const qreal bufferTo = height() + buffer;
348 while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
349 if (!(item = createItem(modelIndex, false)))
351 pos += item->height();
355 m_previousContentY = m_visibleItems.first()->y() - headerHeight;
357 setContentY(m_visibleItems.first()->y() + m_clipItem->y() - headerHeight);
360void ListViewWithPageHeader::showHeader()
365 auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
366 if (to != contentY()) {
367 const bool headerShownByItsOwn = contentY() < m_headerItem->y() + m_headerItem->height();
368 if (headerShownByItsOwn && m_headerItemShownHeight == 0) {
369 // We are not clipping since we are just at the top of the viewport
370 // but because of the showHeader animation we will need to, so
371 // enable the clipping without logically moving the items
372 m_headerItemShownHeight = m_headerItem->y() + m_headerItem->height() - contentY();
373 if (!m_visibleItems.isEmpty()) {
375 ListItem *firstItem = m_visibleItems.first();
376 firstItem->setY(firstItem->y() - m_headerItemShownHeight);
380 m_contentYAnimation->setTo(to);
381 contentYAnimationType = ContentYAnimationShowHeader;
382 m_contentYAnimation->start();
386QQuickItem *ListViewWithPageHeader::item(int modelIndex) const
388 ListItem *item = itemAtIndex(modelIndex);
395bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex)
397 ListItem *listItem = itemAtIndex(modelIndex);
399 return maximizeVisibleArea(listItem, listItem->height());
405bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex, int itemHeight)
410 ListItem *listItem = itemAtIndex(modelIndex);
412 return maximizeVisibleArea(listItem, itemHeight + (listItem->m_sectionItem ? listItem->m_sectionItem->height() : 0));
418bool ListViewWithPageHeader::maximizeVisibleArea(ListItem *listItem, int listItemHeight)
422 const auto listItemY = m_clipItem->y() + listItem->y();
423 if (listItemY > contentY() && listItemY + listItemHeight > contentY() + height()) {
424 // we can scroll the list up to show more stuff
425 const auto to = qMin(listItemY, listItemY + listItemHeight - height());
426 m_contentYAnimation->setTo(to);
427 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
428 m_contentYAnimation->start();
429 } else if ((listItemY < contentY() && listItemY + listItemHeight < contentY() + height()) ||
430 (m_topSectionItem && !listItem->m_sectionItem && listItemY - m_topSectionItem->height() < contentY() && listItemY + listItemHeight < contentY() + height()))
432 // we can scroll the list down to show more stuff
433 auto realVisibleListItemY = listItemY;
434 if (m_topSectionItem) {
435 // If we are showing the top section sticky item and this item doesn't have a section
436 // item we have to make sure to scroll it a bit more so that it is not underlapping
437 // the top section sticky item
438 bool topSectionShown = !QQuickItemPrivate::get(m_topSectionItem)->culled;
439 if (topSectionShown && !listItem->m_sectionItem) {
440 realVisibleListItemY -= m_topSectionItem->height();
443 const auto to = qMax(realVisibleListItemY, listItemY + listItemHeight - height());
444 m_contentYAnimation->setTo(to);
445 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
446 m_contentYAnimation->start();
453qreal ListViewWithPageHeader::minYExtent() const
455// qDebug() << "ListViewWithPageHeader::minYExtent" << m_minYExtent;
459void ListViewWithPageHeader::componentComplete()
462 m_delegateModel->componentComplete();
464 QQuickFlickable::componentComplete();
469void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
471 QQuickFlickable::viewportMoved(orient);
472// qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
473 qreal diff = m_previousContentY - contentY();
474 const bool showHeaderAnimationRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationShowHeader;
476 auto oldHeaderItemShownHeight = m_headerItemShownHeight;
477 if (contentY() < -m_minYExtent) {
478 // Stick the header item to the top when dragging down
479 m_headerItem->setY(contentY());
480 m_headerItem->setHeight(m_headerItem->implicitHeight() + (-m_minYExtent - contentY()));
482 m_headerItem->setHeight(m_headerItem->implicitHeight());
483 // We are going down (but it's not because of the rebound at the end)
484 // (but the header was not shown by it's own position)
485 // or the header is partially shown and we are not doing a maximizeVisibleArea either
486 const bool scrolledUp = m_previousContentY > contentY();
487 const bool notRebounding = contentY() + height() < contentHeight();
488 const bool notShownByItsOwn = contentY() + diff >= m_headerItem->y() + m_headerItem->height();
489 const bool maximizeVisibleAreaRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationMaximizeVisibleArea;
491 if (!scrolledUp && contentY() == -m_minYExtent) {
492 m_headerItemShownHeight = 0;
493 m_headerItem->setY(contentY());
494 } else if ((scrolledUp && notRebounding && notShownByItsOwn && !maximizeVisibleAreaRunning) || (m_headerItemShownHeight > 0)) {
495 if (maximizeVisibleAreaRunning && diff > 0) // If we are maximizing and the header was shown, make sure we hide it
496 m_headerItemShownHeight -= diff;
498 m_headerItemShownHeight += diff;
499 if (contentY() == -m_minYExtent) {
500 m_headerItemShownHeight = 0;
502 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
504 if (m_headerItemShownHeight > 0) {
505 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
507 m_headerItem->setY(-m_minYExtent);
511 // We will be changing the clip item, need to accomadate for it
512 // otherwise we move the firstItem down/up twice (unless the
513 // show header animation is running, where we want to keep the viewport stable)
514 if (!showHeaderAnimationRunning) {
515 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
520 if (!m_visibleItems.isEmpty()) {
522 ListItem *firstItem = m_visibleItems.first();
523 firstItem->setY(firstItem->y() + diff);
524 if (showHeaderAnimationRunning) {
529 m_previousContentY = contentY();
534void ListViewWithPageHeader::createDelegateModel()
536#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
537 m_delegateModel = new QQuickVisualDataModel(qmlContext(this), this);
538 connect(m_delegateModel, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(itemCreated(int,QQuickItem*)));
540 m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
541 connect(m_delegateModel, SIGNAL(createdItem(int,QObject*)), this, SLOT(itemCreated(int,QObject*)));
543 if (isComponentComplete())
544 m_delegateModel->componentComplete();
545 updateWatchedRoles();
548void ListViewWithPageHeader::refill()
553 if (!isComponentComplete()) {
557 const qreal buffer = height() * bufferRatio;
558 const qreal from = contentY();
559 const qreal to = from + height();
560 const qreal bufferFrom = from - buffer;
561 const qreal bufferTo = to + buffer;
563 bool added = addVisibleItems(from, to, false);
564 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
565 added |= addVisibleItems(bufferFrom, bufferTo, true);
567 if (added || removed) {
568 m_contentHeightDirty = true;
572bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
577 if (m_delegateModel->count() == 0)
581// qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
585 if (!m_visibleItems.isEmpty()) {
586 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
587 item = m_visibleItems.last();
588 pos = item->y() + item->height() + m_clipItem->y();
590 bool changed = false;
591// qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
592 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
593// qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
594 if (!(item = createItem(modelIndex, asynchronous)))
596 pos += item->height();
603 if (!m_visibleItems.isEmpty()) {
604 modelIndex = m_firstVisibleIndex - 1;
605 item = m_visibleItems.first();
606 pos = item->y() + m_clipItem->y();
608 while (modelIndex >= 0 && pos > fillFrom) {
609// qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
610 if (!(item = createItem(modelIndex, asynchronous)))
612 pos -= item->height();
620void ListViewWithPageHeader::reallyReleaseItem(ListItem *listItem)
622 QQuickItem *item = listItem->m_item;
623#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
624 QQuickVisualModel::ReleaseFlags flags = m_delegateModel->release(item);
625 if (flags & QQuickVisualModel::Destroyed) {
627 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
628 if (flags & QQmlDelegateModel::Destroyed) {
630 item->setParentItem(nullptr);
632 delete listItem->m_sectionItem;
636void ListViewWithPageHeader::releaseItem(ListItem *listItem)
638 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(listItem->m_item);
639 itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
640 m_itemsToRelease << listItem;
643void ListViewWithPageHeader::updateWatchedRoles()
645 if (m_delegateModel) {
646 QList<QByteArray> roles;
647 if (!m_sectionProperty.isEmpty())
648 roles << m_sectionProperty.toUtf8();
649 m_delegateModel->setWatchedRoles(roles);
653QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
655 if (!m_sectionDelegate)
658 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
659 if (modelIndex > 0) {
660 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
661 if (section == prevSection)
664 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
665 // Already inserted items can't steal next section header
666 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
667 if (section == nextSection) {
668 // Steal the section header
669 ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
671 QQuickItem *sectionItem = nextItem->m_sectionItem;
672 nextItem->m_sectionItem = nullptr;
678 return getSectionItem(section);
681QQuickItem *ListViewWithPageHeader::getSectionItem(const QString §ionText)
683 QQuickItem *sectionItem = nullptr;
685 QQmlContext *creationContext = m_sectionDelegate->creationContext();
686 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
687 context->setContextProperty(QLatin1String("section"), sectionText);
688 context->setContextProperty(QLatin1String("delegateIndex"), -1);
689 QObject *nobj = m_sectionDelegate->beginCreate(context);
691 QQml_setParent_noEvent(context, nobj);
692 sectionItem = qobject_cast<QQuickItem *>(nobj);
696 sectionItem->setZ(2);
697 QQml_setParent_noEvent(sectionItem, m_clipItem);
698 sectionItem->setParentItem(m_clipItem);
703 m_sectionDelegate->completeCreate();
705 // TODO attach to sectionItem so we can accomodate to it changing its height
710bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
712// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
713 // Do not remove items if we are overshooting up or down, since we'll come back
714 // to the "stable" position and delete/create items without any reason
715 if (contentY() < -m_minYExtent) {
717 } else if (contentY() + height() > contentHeight()) {
720 bool changed = false;
722 bool foundVisible = false;
724 int removedItems = 0;
725 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
726 while (i < m_visibleItems.count()) {
727 ListItem *item = m_visibleItems[i];
728 const qreal pos = item->y() + m_clipItem->y();
729// qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
730 if (pos + item->height() < bufferFrom || pos > bufferTo) {
731// qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
733 m_visibleItems.removeAt(i);
739 const int itemIndex = m_firstVisibleIndex + removedItems + i;
740 m_firstVisibleIndex = itemIndex;
745 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
752ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
754// qDebug() << "CREATE ITEM" << modelIndex;
755 if (asynchronous && m_asyncRequestedIndex != -1)
758 m_asyncRequestedIndex = -1;
759#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
760 QQuickItem *item = m_delegateModel->item(modelIndex, asynchronous);
762 QObject* object = m_delegateModel->object(modelIndex, asynchronous);
763 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
766#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
767 m_asyncRequestedIndex = modelIndex;
770 m_delegateModel->release(object);
771 if (!m_delegateValidated) {
772 m_delegateValidated = true;
773 QObject* delegateObj = delegate();
774 qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
777 m_asyncRequestedIndex = modelIndex;
782// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
783 ListItem *listItem = new ListItem;
784 listItem->m_item = item;
785 listItem->m_sectionItem = getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/);
786 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
787 ListItem *prevItem = itemAtIndex(modelIndex - 1);
788 bool lostItem = false; // Is an item that we requested async but because of model changes
789 // it is no longer attached to any of the existing items (has no prev nor next item)
790 // nor is the first item
792 listItem->setY(prevItem->y() + prevItem->height());
794 ListItem *currItem = itemAtIndex(modelIndex);
796 // There's something already in m_visibleItems at out index, meaning this is an insert, so attach to its top
797 listItem->setY(currItem->y() - listItem->height());
799 ListItem *nextItem = itemAtIndex(modelIndex + 1);
801 listItem->setY(nextItem->y() - listItem->height());
802 } else if (modelIndex == 0 && m_headerItem) {
803 listItem->setY(m_headerItem->height());
804 } else if (!m_visibleItems.isEmpty()) {
810 releaseItem(listItem);
813 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() <= contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
814 if (m_visibleItems.isEmpty()) {
815 m_visibleItems << listItem;
817 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
819 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
820 m_firstVisibleIndex = modelIndex;
823 if (listItem->m_sectionItem) {
824 QQmlContext *context = QQmlEngine::contextForObject(listItem->m_sectionItem)->parentContext();
825 context->setContextProperty(QLatin1String("delegateIndex"), modelIndex);
828 m_contentHeightDirty = true;
834#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
835void ListViewWithPageHeader::itemCreated(int modelIndex, QQuickItem *item)
838void ListViewWithPageHeader::itemCreated(int modelIndex, QObject *object)
840 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
842 qWarning() << "ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
846// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
848 item->setParentItem(m_clipItem);
849 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
850 context->setContextProperty(QLatin1String("ListViewWithPageHeader"), this);
851 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
852 if (modelIndex == m_asyncRequestedIndex) {
853 createItem(modelIndex, false);
858void ListViewWithPageHeader::updateClipItem()
860 m_clipItem->setHeight(height() - m_headerItemShownHeight);
861 m_clipItem->setY(contentY() + m_headerItemShownHeight);
862 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
865void ListViewWithPageHeader::onContentHeightChanged()
870void ListViewWithPageHeader::onContentWidthChanged()
872 m_clipItem->setWidth(contentItem()->width());
875void ListViewWithPageHeader::onHeightChanged()
881#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
882void ListViewWithPageHeader::onModelUpdated(const QQuickChangeSet &changeSet, bool /*reset*/)
884void ListViewWithPageHeader::onModelUpdated(const QQmlChangeSet &changeSet, bool /*reset*/)
887 // TODO Do something with reset
888// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
889 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
891#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
892 Q_FOREACH(const QQuickChangeSet::Remove &remove, changeSet.removes()) {
894 Q_FOREACH(const QQmlChangeSet::Remove &remove, changeSet.removes()) {
896// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove.index << remove.count;
897 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
898 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
899 // If all the items we are removing are either not created or culled
900 // we have to grow down to avoid viewport changing
901 bool growDown = true;
902 for (int i = 0; growDown && i < remove.count; ++i) {
903 const int modelIndex = remove.index + i;
904 ListItem *item = itemAtIndex(modelIndex);
905 if (item && !item->culled()) {
909 for (int i = remove.count - 1; i >= 0; --i) {
910 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
911 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
912 ListItem *item = m_visibleItems[visibleIndex];
913 // Pass the section item down if needed
914 if (item->m_sectionItem && visibleIndex + 1 < m_visibleItems.count()) {
915 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
916 if (!nextItem->m_sectionItem) {
917 nextItem->m_sectionItem = item->m_sectionItem;
918 item->m_sectionItem = nullptr;
922 m_visibleItems.removeAt(visibleIndex);
927 } else if (remove.index <= m_firstVisibleIndex) {
928 if (!m_visibleItems.isEmpty()) {
929 // We removed the first item that is the one that positions the rest
930 // position the new first item correctly
931 m_visibleItems.first()->setY(oldFirstValidIndexPos);
933 m_firstVisibleIndex = -1;
936 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
937 m_firstVisibleIndex -= remove.count;
939 for (int i = remove.count - 1; i >= 0; --i) {
940 const int modelIndex = remove.index + i;
941 if (modelIndex == m_asyncRequestedIndex) {
942 m_asyncRequestedIndex = -1;
943 } else if (modelIndex < m_asyncRequestedIndex) {
944 m_asyncRequestedIndex--;
949#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
950 Q_FOREACH(const QQuickChangeSet::Insert &insert, changeSet.inserts()) {
952 Q_FOREACH(const QQmlChangeSet::Insert &insert, changeSet.inserts()) {
954// qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
955 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
956 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
957 if (insertingInValidIndexes || firstItemWithViewOnTop)
959 // If the items we are adding won't be really visible
960 // we grow up instead of down to not change the viewport
962 if (!firstItemWithViewOnTop) {
963 for (int i = 0; i < m_visibleItems.count(); ++i) {
964 if (!m_visibleItems[i]->culled()) {
965 if (insert.index <= m_firstVisibleIndex + i) {
973 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
974 for (int i = insert.count - 1; i >= 0; --i) {
975 const int modelIndex = insert.index + i;
976 ListItem *item = createItem(modelIndex, false);
978 ListItem *firstItem = m_visibleItems.first();
979 firstItem->setY(firstItem->y() - item->height());
982 // Adding an item may break a "same section" chain, so check
983 // if we need adding a new section item
984 if (m_sectionDelegate) {
985 ListItem *nextItem = itemAtIndex(modelIndex + 1);
986 if (nextItem && !nextItem->m_sectionItem) {
987 nextItem->m_sectionItem = getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/);
988 if (growUp && nextItem->m_sectionItem) {
989 ListItem *firstItem = m_visibleItems.first();
990 firstItem->setY(firstItem->y() - nextItem->m_sectionItem->height());
996 if (firstItemWithViewOnTop) {
997 ListItem *firstItem = m_visibleItems.first();
998 firstItem->setY(oldFirstValidIndexPos);
1000 } else if (insert.index <= m_firstVisibleIndex) {
1001 m_firstVisibleIndex += insert.count;
1004 for (int i = insert.count - 1; i >= 0; --i) {
1005 const int modelIndex = insert.index + i;
1006 if (modelIndex <= m_asyncRequestedIndex) {
1007 m_asyncRequestedIndex++;
1012#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
1013 Q_FOREACH(const QQuickChangeSet::Change &change, changeSet.changes()) {
1015 Q_FOREACH(const QQmlChangeSet::Change &change, changeSet.changes()) {
1017// qDebug() << "ListViewWithPageHeader::onModelUpdated Change" << change.index << change.count;
1018 for (int i = change.index; i < change.count; ++i) {
1019 ListItem *item = itemAtIndex(i);
1020 if (item && item->m_sectionItem) {
1021 QQmlContext *context = QQmlEngine::contextForObject(item->m_sectionItem)->parentContext();
1022 const QString sectionText = m_delegateModel->stringValue(i, m_sectionProperty);
1023 context->setContextProperty(QLatin1String("section"), sectionText);
1028 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
1032 for (int i = 0; i < m_visibleItems.count(); ++i) {
1033 ListItem *item = m_visibleItems[i];
1034 if (item->m_sectionItem) {
1035 QQmlContext *context = QQmlEngine::contextForObject(item->m_sectionItem)->parentContext();
1036 context->setContextProperty(QLatin1String("delegateIndex"), m_firstVisibleIndex + i);
1042 m_contentHeightDirty = true;
1045void ListViewWithPageHeader::onShowHeaderAnimationFinished()
1047 m_contentHeightDirty = true;
1051void ListViewWithPageHeader::itemGeometryChanged(QQuickItem * /*item*/, const QRectF &newGeometry, const QRectF &oldGeometry)
1053 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
1054 if (heightDiff != 0) {
1055 if (oldGeometry.y() + oldGeometry.height() + m_clipItem->y() < contentY() && !m_visibleItems.isEmpty()) {
1056 ListItem *firstItem = m_visibleItems.first();
1057 firstItem->setY(firstItem->y() - heightDiff);
1064 m_contentHeightDirty = true;
1068void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
1070 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
1071 if (m_headerItemShownHeight > 0) {
1072 // If the header is shown because of the clip
1074 m_headerItemShownHeight += heightDiff;
1075 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
1079 if (oldHeaderY + oldHeaderHeight > contentY()) {
1080 // If the header is shown because its position
1082 ListItem *firstItem = m_visibleItems.first();
1083 firstItem->setY(firstItem->y() + heightDiff);
1086 // If the header is not on screen, just change the start of the list
1087 // so the viewport is not changed
1094void ListViewWithPageHeader::adjustMinYExtent()
1096 if (m_visibleItems.isEmpty()) {
1099 qreal nonCreatedHeight = 0;
1100 if (m_firstVisibleIndex != 0) {
1101 // Calculate the average height of items to estimate the position of the list start
1102 const int visibleItems = m_visibleItems.count();
1103 qreal visibleItemsHeight = 0;
1104 Q_FOREACH(ListItem *item, m_visibleItems) {
1105 visibleItemsHeight += item->height();
1107 nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
1108// qDebug() << m_firstVisibleIndex << visibleItemsHeight << visibleItems << nonCreatedHeight;
1110 m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + (m_headerItem ? m_headerItem->implicitHeight() : 0);
1114ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
1116 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
1117 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
1118 return m_visibleItems[visibleIndexedModelIndex];
1123void ListViewWithPageHeader::layout()
1129 if (!m_visibleItems.isEmpty()) {
1130 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
1131 const qreal visibleTo = contentY() + height() - m_clipItem->y();
1133 qreal pos = m_visibleItems.first()->y();
1135// qDebug() << "ListViewWithPageHeader::layout Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
1136 int firstReallyVisibleItem = -1;
1137 int modelIndex = m_firstVisibleIndex;
1138 Q_FOREACH(ListItem *item, m_visibleItems) {
1139 const bool cull = pos + item->height() <= visibleFrom || pos >= visibleTo;
1140 item->setCulled(cull);
1142 if (!cull && firstReallyVisibleItem == -1) {
1143 firstReallyVisibleItem = modelIndex;
1144 if (m_topSectionItem) {
1145 // Positing the top section sticky item is a two step process
1146 // First we set it either we cull it (because it doesn't need to be sticked to the top)
1147 // or stick it to the top
1148 // Then after the loop we'll make sure that if there's another section just below it
1149 // pushed the sticky section up to make it disappear
1150 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
1151 bool showStickySectionItem;
1152 // We need to show the "top section sticky item" when the position at the "top" of the
1153 // viewport is bigger than the start of the position of the first visible item
1154 // i.e. the first visible item starts before the viewport, or when the first
1155 // visible item starts just at the viewport start and it does not have its own section item
1156 if (topSectionStickPos > pos) {
1157 showStickySectionItem = true;
1158 } else if (topSectionStickPos == pos) {
1159 showStickySectionItem = !item->m_sectionItem;
1161 showStickySectionItem = false;
1163 if (!showStickySectionItem) {
1164 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
1165 if (item->m_sectionItem) {
1166 // This seems it should happen since why would we cull the top section
1167 // if the first visible item has no section header? This only happens briefly
1168 // when increasing the height of a list that is at the bottom, the m_topSectionItem
1169 // gets shown shortly in the next polish call
1170 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(false);
1173 // Update the top sticky section header
1174 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1175 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
1176 context->setContextProperty(QLatin1String("section"), section);
1178 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
1179 m_topSectionItem->setY(topSectionStickPos);
1180 int delegateIndex = modelIndex;
1181 // Look for the first index with this section text
1182 while (delegateIndex > 0) {
1183 const QString prevSection = m_delegateModel->stringValue(delegateIndex - 1, m_sectionProperty);
1184 if (prevSection != section)
1188 context->setContextProperty(QLatin1String("delegateIndex"), delegateIndex);
1189 if (item->m_sectionItem) {
1190 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(true);
1195 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
1196 const qreal clipFrom = visibleFrom + (!item->m_sectionItem && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
1197 if (!cull && pos < clipFrom) {
1198 context->setContextProperty(QLatin1String("heightToClip"), clipFrom - pos);
1200 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
1202// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
1203 pos += item->height();
1207 // Second step of section sticky item positioning
1208 // Look at the next section header, check if it's pushing up the sticky one
1209 if (m_topSectionItem) {
1210 if (firstReallyVisibleItem >= 0) {
1211 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
1212 ListItem *item = m_visibleItems[i];
1213 if (item->m_sectionItem) {
1214 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
1215 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
1226void ListViewWithPageHeader::updatePolish()
1228 Q_FOREACH(ListItem *item, m_itemsToRelease)
1229 reallyReleaseItem(item);
1230 m_itemsToRelease.clear();
1239 if (m_contentHeightDirty) {
1240 qreal contentHeight;
1241 if (m_visibleItems.isEmpty()) {
1242 contentHeight = m_headerItem ? m_headerItem->height() : 0;
1244 const int modelCount = model()->rowCount();
1245 const int visibleItems = m_visibleItems.count();
1246 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
1247 qreal nonCreatedHeight = 0;
1248 if (lastValidIndex != modelCount - 1) {
1249 const int visibleItems = m_visibleItems.count();
1250 qreal visibleItemsHeight = 0;
1251 Q_FOREACH(ListItem *item, m_visibleItems) {
1252 visibleItemsHeight += item->height();
1254 const int unknownSizes = modelCount - (m_firstVisibleIndex + visibleItems);
1255 nonCreatedHeight = unknownSizes * visibleItemsHeight / visibleItems;
1257 ListItem *item = m_visibleItems.last();
1258 contentHeight = nonCreatedHeight + item->y() + item->height() + m_clipItem->y();
1259 if (m_firstVisibleIndex != 0) {
1260 // Make sure that if we are shrinking we tell the view we still fit
1261 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
1265 m_contentHeightDirty = false;
1267 setContentHeight(contentHeight);
1271#include "moc_listviewwithpageheader.cpp"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list