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()));
175ListViewWithPageHeader::~ListViewWithPageHeader()
179QAbstractItemModel *ListViewWithPageHeader::model() const
181 return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
184void ListViewWithPageHeader::setModel(QAbstractItemModel *model)
186 if (model != this->model()) {
187 if (!m_delegateModel) {
188 createDelegateModel();
190#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
191 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
193 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
194 connect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
196 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
198 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
199 connect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
201 Q_EMIT modelChanged();
203// Q_EMIT contentHeightChanged();
204// Q_EMIT contentYChanged();
208QQmlComponent *ListViewWithPageHeader::delegate() const
210 return m_delegateModel ? m_delegateModel->delegate() : nullptr;
213void ListViewWithPageHeader::setDelegate(QQmlComponent *delegate)
215 if (delegate != this->delegate()) {
216 if (!m_delegateModel) {
217 createDelegateModel();
220 // Cleanup the existing items
221 Q_FOREACH(ListItem *item, m_visibleItems)
223 m_visibleItems.clear();
224 m_firstVisibleIndex = -1;
228 if (m_topSectionItem) {
229 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
232 m_delegateModel->setDelegate(delegate);
234 Q_EMIT delegateChanged();
235 m_delegateValidated = false;
236 m_contentHeightDirty = true;
241QQuickItem *ListViewWithPageHeader::header() const
246void ListViewWithPageHeader::setHeader(QQuickItem *headerItem)
248 if (m_headerItem != headerItem) {
249 qreal oldHeaderHeight = 0;
250 qreal oldHeaderY = 0;
252 oldHeaderHeight = m_headerItem->height();
253 oldHeaderY = m_headerItem->y();
254 m_headerItem->setParentItem(nullptr);
256 m_headerItem = headerItem;
258 m_headerItem->setParentItem(contentItem());
259 m_headerItem->setZ(1);
261 qreal newHeaderHeight = m_headerItem ? m_headerItem->height() : 0;
262 if (!m_visibleItems.isEmpty() && newHeaderHeight != oldHeaderHeight) {
263 headerHeightChanged(newHeaderHeight, oldHeaderHeight, oldHeaderY);
265 m_contentHeightDirty = true;
267 Q_EMIT headerChanged();
271QQmlComponent *ListViewWithPageHeader::sectionDelegate() const
273 return m_sectionDelegate;
276void ListViewWithPageHeader::setSectionDelegate(QQmlComponent *delegate)
278 if (delegate != m_sectionDelegate) {
279 // TODO clean existing sections
281 m_sectionDelegate = delegate;
283 m_topSectionItem = getSectionItem(QString());
284 m_topSectionItem->setZ(3);
285 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
287 // TODO create sections for existing items
289 Q_EMIT sectionDelegateChanged();
293QString ListViewWithPageHeader::sectionProperty() const
295 return m_sectionProperty;
298void ListViewWithPageHeader::setSectionProperty(const QString &property)
300 if (property != m_sectionProperty) {
301 m_sectionProperty = property;
303 updateWatchedRoles();
305 // TODO recreate sections
307 Q_EMIT sectionPropertyChanged();
311bool ListViewWithPageHeader::forceNoClip() const
313 return m_forceNoClip;
316void ListViewWithPageHeader::setForceNoClip(bool noClip)
318 if (noClip != m_forceNoClip) {
319 m_forceNoClip = noClip;
321 Q_EMIT forceNoClipChanged();
325void ListViewWithPageHeader::positionAtBeginning()
327 qreal headerHeight = (m_headerItem ? m_headerItem->height() : 0);
328 if (m_firstVisibleIndex != 0) {
329 // TODO This could be optimized by trying to reuse the interesection
330 // of items that may end up intersecting between the existing
331 // m_visibleItems and the items we are creating in the next loop
332 Q_FOREACH(ListItem *item, m_visibleItems)
334 m_visibleItems.clear();
335 m_firstVisibleIndex = -1;
337 // Create the item 0, it will be already correctly positioned at createItem()
339 ListItem *item = createItem(0, false);
340 // Create the subsequent items
342 qreal pos = item->y() + item->height();
343 const qreal buffer = height() * bufferRatio;
344 const qreal bufferTo = height() + buffer;
345 while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
346 if (!(item = createItem(modelIndex, false)))
348 pos += item->height();
352 m_previousContentY = m_visibleItems.first()->y() - headerHeight;
354 setContentY(m_visibleItems.first()->y() + m_clipItem->y() - headerHeight);
357void ListViewWithPageHeader::showHeader()
362 auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
363 if (to != contentY()) {
364 const bool headerShownByItsOwn = contentY() < m_headerItem->y() + m_headerItem->height();
365 if (headerShownByItsOwn && m_headerItemShownHeight == 0) {
366 // We are not clipping since we are just at the top of the viewport
367 // but because of the showHeader animation we will need to, so
368 // enable the clipping without logically moving the items
369 m_headerItemShownHeight = m_headerItem->y() + m_headerItem->height() - contentY();
370 if (!m_visibleItems.isEmpty()) {
372 ListItem *firstItem = m_visibleItems.first();
373 firstItem->setY(firstItem->y() - m_headerItemShownHeight);
377 m_contentYAnimation->setTo(to);
378 contentYAnimationType = ContentYAnimationShowHeader;
379 m_contentYAnimation->start();
383QQuickItem *ListViewWithPageHeader::item(int modelIndex) const
385 ListItem *item = itemAtIndex(modelIndex);
392bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex)
394 ListItem *listItem = itemAtIndex(modelIndex);
396 return maximizeVisibleArea(listItem, listItem->height());
402bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex, int itemHeight)
407 ListItem *listItem = itemAtIndex(modelIndex);
409 return maximizeVisibleArea(listItem, itemHeight + (listItem->m_sectionItem ? listItem->m_sectionItem->height() : 0));
415bool ListViewWithPageHeader::maximizeVisibleArea(ListItem *listItem, int listItemHeight)
419 const auto listItemY = m_clipItem->y() + listItem->y();
420 if (listItemY > contentY() && listItemY + listItemHeight > contentY() + height()) {
421 // we can scroll the list up to show more stuff
422 const auto to = qMin(listItemY, listItemY + listItemHeight - height());
423 m_contentYAnimation->setTo(to);
424 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
425 m_contentYAnimation->start();
426 } else if ((listItemY < contentY() && listItemY + listItemHeight < contentY() + height()) ||
427 (m_topSectionItem && !listItem->m_sectionItem && listItemY - m_topSectionItem->height() < contentY() && listItemY + listItemHeight < contentY() + height()))
429 // we can scroll the list down to show more stuff
430 auto realVisibleListItemY = listItemY;
431 if (m_topSectionItem) {
432 // If we are showing the top section sticky item and this item doesn't have a section
433 // item we have to make sure to scroll it a bit more so that it is not underlapping
434 // the top section sticky item
435 bool topSectionShown = !QQuickItemPrivate::get(m_topSectionItem)->culled;
436 if (topSectionShown && !listItem->m_sectionItem) {
437 realVisibleListItemY -= m_topSectionItem->height();
440 const auto to = qMax(realVisibleListItemY, listItemY + listItemHeight - height());
441 m_contentYAnimation->setTo(to);
442 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
443 m_contentYAnimation->start();
450qreal ListViewWithPageHeader::minYExtent() const
452// qDebug() << "ListViewWithPageHeader::minYExtent" << m_minYExtent;
456void ListViewWithPageHeader::componentComplete()
459 m_delegateModel->componentComplete();
461 QQuickFlickable::componentComplete();
466void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
468 QQuickFlickable::viewportMoved(orient);
469// qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
470 qreal diff = m_previousContentY - contentY();
471 const bool showHeaderAnimationRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationShowHeader;
473 auto oldHeaderItemShownHeight = m_headerItemShownHeight;
474 if (contentY() < -m_minYExtent) {
475 // Stick the header item to the top when dragging down
476 m_headerItem->setY(contentY());
477 m_headerItem->setHeight(m_headerItem->implicitHeight() + (-m_minYExtent - contentY()));
479 m_headerItem->setHeight(m_headerItem->implicitHeight());
480 // We are going down (but it's not because of the rebound at the end)
481 // (but the header was not shown by it's own position)
482 // or the header is partially shown and we are not doing a maximizeVisibleArea either
483 const bool scrolledUp = m_previousContentY > contentY();
484 const bool notRebounding = contentY() + height() < contentHeight();
485 const bool notShownByItsOwn = contentY() + diff > m_headerItem->y() + m_headerItem->height();
486 const bool maximizeVisibleAreaRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationMaximizeVisibleArea;
488 if (!scrolledUp && contentY() == -m_minYExtent) {
489 m_headerItemShownHeight = 0;
490 m_headerItem->setY(contentY());
491 } else if ((scrolledUp && notRebounding && notShownByItsOwn && !maximizeVisibleAreaRunning) || (m_headerItemShownHeight > 0)) {
492 if (maximizeVisibleAreaRunning && diff > 0) // If we are maximizing and the header was shown, make sure we hide it
493 m_headerItemShownHeight -= diff;
495 m_headerItemShownHeight += diff;
496 if (contentY() == -m_minYExtent) {
497 m_headerItemShownHeight = 0;
499 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
501 if (m_headerItemShownHeight > 0) {
502 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
504 m_headerItem->setY(-m_minYExtent);
508 // We will be changing the clip item, need to accomadate for it
509 // otherwise we move the firstItem down/up twice (unless the
510 // show header animation is running, where we want to keep the viewport stable)
511 if (!showHeaderAnimationRunning) {
512 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
517 if (!m_visibleItems.isEmpty()) {
519 ListItem *firstItem = m_visibleItems.first();
520 firstItem->setY(firstItem->y() + diff);
521 if (showHeaderAnimationRunning) {
526 m_previousContentY = contentY();
531void ListViewWithPageHeader::createDelegateModel()
533#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
534 m_delegateModel = new QQuickVisualDataModel(qmlContext(this), this);
535 connect(m_delegateModel, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(itemCreated(int,QQuickItem*)));
537 m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
538 connect(m_delegateModel, SIGNAL(createdItem(int,QObject*)), this, SLOT(itemCreated(int,QObject*)));
540 if (isComponentComplete())
541 m_delegateModel->componentComplete();
542 updateWatchedRoles();
545void ListViewWithPageHeader::refill()
550 if (!isComponentComplete()) {
554 const qreal buffer = height() * bufferRatio;
555 const qreal from = contentY();
556 const qreal to = from + height();
557 const qreal bufferFrom = from - buffer;
558 const qreal bufferTo = to + buffer;
560 bool added = addVisibleItems(from, to, false);
561 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
562 added |= addVisibleItems(bufferFrom, bufferTo, true);
564 if (added || removed) {
565 m_contentHeightDirty = true;
569bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
574 if (m_delegateModel->count() == 0)
578// qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
582 if (!m_visibleItems.isEmpty()) {
583 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
584 item = m_visibleItems.last();
585 pos = item->y() + item->height() + m_clipItem->y();
587 bool changed = false;
588// qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
589 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
590// qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
591 if (!(item = createItem(modelIndex, asynchronous)))
593 pos += item->height();
600 if (!m_visibleItems.isEmpty()) {
601 modelIndex = m_firstVisibleIndex - 1;
602 item = m_visibleItems.first();
603 pos = item->y() + m_clipItem->y();
605 while (modelIndex >= 0 && pos > fillFrom) {
606// qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
607 if (!(item = createItem(modelIndex, asynchronous)))
609 pos -= item->height();
617void ListViewWithPageHeader::reallyReleaseItem(ListItem *listItem)
619 QQuickItem *item = listItem->m_item;
620#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
621 QQuickVisualModel::ReleaseFlags flags = m_delegateModel->release(item);
622 if (flags & QQuickVisualModel::Destroyed) {
624 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
625 if (flags & QQmlDelegateModel::Destroyed) {
627 item->setParentItem(nullptr);
629 delete listItem->m_sectionItem;
633void ListViewWithPageHeader::releaseItem(ListItem *listItem)
635 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(listItem->m_item);
636 itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
637 m_itemsToRelease << listItem;
640void ListViewWithPageHeader::updateWatchedRoles()
642 if (m_delegateModel) {
643 QList<QByteArray> roles;
644 if (!m_sectionProperty.isEmpty())
645 roles << m_sectionProperty.toUtf8();
646 m_delegateModel->setWatchedRoles(roles);
650QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
652 if (!m_sectionDelegate)
655 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
656 if (modelIndex > 0) {
657 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
658 if (section == prevSection)
661 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
662 // Already inserted items can't steal next section header
663 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
664 if (section == nextSection) {
665 // Steal the section header
666 ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
668 QQuickItem *sectionItem = nextItem->m_sectionItem;
669 nextItem->m_sectionItem = nullptr;
675 return getSectionItem(section);
678QQuickItem *ListViewWithPageHeader::getSectionItem(const QString §ionText)
680 QQuickItem *sectionItem = nullptr;
682 QQmlContext *creationContext = m_sectionDelegate->creationContext();
683 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
684 context->setContextProperty(QLatin1String("section"), sectionText);
685 context->setContextProperty(QLatin1String("delegateIndex"), -1);
686 QObject *nobj = m_sectionDelegate->beginCreate(context);
688 QQml_setParent_noEvent(context, nobj);
689 sectionItem = qobject_cast<QQuickItem *>(nobj);
693 sectionItem->setZ(2);
694 QQml_setParent_noEvent(sectionItem, m_clipItem);
695 sectionItem->setParentItem(m_clipItem);
700 m_sectionDelegate->completeCreate();
702 // TODO attach to sectionItem so we can accomodate to it changing its height
707bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
709// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
710 // Do not remove items if we are overshooting up or down, since we'll come back
711 // to the "stable" position and delete/create items without any reason
712 if (contentY() < -m_minYExtent) {
714 } else if (contentY() + height() > contentHeight()) {
717 bool changed = false;
719 bool foundVisible = false;
721 int removedItems = 0;
722 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
723 while (i < m_visibleItems.count()) {
724 ListItem *item = m_visibleItems[i];
725 const qreal pos = item->y() + m_clipItem->y();
726// qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
727 if (pos + item->height() < bufferFrom || pos > bufferTo) {
728// qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
730 m_visibleItems.removeAt(i);
736 const int itemIndex = m_firstVisibleIndex + removedItems + i;
737 m_firstVisibleIndex = itemIndex;
742 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
749ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
751// qDebug() << "CREATE ITEM" << modelIndex;
752 if (asynchronous && m_asyncRequestedIndex != -1)
755 m_asyncRequestedIndex = -1;
756#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
757 QQuickItem *item = m_delegateModel->item(modelIndex, asynchronous);
759 QObject* object = m_delegateModel->object(modelIndex, asynchronous);
760 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
763#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
764 m_asyncRequestedIndex = modelIndex;
767 m_delegateModel->release(object);
768 if (!m_delegateValidated) {
769 m_delegateValidated = true;
770 QObject* delegateObj = delegate();
771 qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
774 m_asyncRequestedIndex = modelIndex;
779// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
780 ListItem *listItem = new ListItem;
781 listItem->m_item = item;
782 listItem->m_sectionItem = getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/);
783 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
784 ListItem *prevItem = itemAtIndex(modelIndex - 1);
785 bool lostItem = false; // Is an item that we requested async but because of model changes
786 // it is no longer attached to any of the existing items (has no prev nor next item)
787 // nor is the first item
789 listItem->setY(prevItem->y() + prevItem->height());
791 ListItem *currItem = itemAtIndex(modelIndex);
793 // There's something already in m_visibleItems at out index, meaning this is an insert, so attach to its top
794 listItem->setY(currItem->y() - listItem->height());
796 ListItem *nextItem = itemAtIndex(modelIndex + 1);
798 listItem->setY(nextItem->y() - listItem->height());
799 } else if (modelIndex == 0 && m_headerItem) {
800 listItem->setY(m_headerItem->height());
801 } else if (!m_visibleItems.isEmpty()) {
807 releaseItem(listItem);
810 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() <= contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
811 if (m_visibleItems.isEmpty()) {
812 m_visibleItems << listItem;
814 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
816 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
817 m_firstVisibleIndex = modelIndex;
820 if (listItem->m_sectionItem) {
821 QQmlContext *context = QQmlEngine::contextForObject(listItem->m_sectionItem)->parentContext();
822 context->setContextProperty(QLatin1String("delegateIndex"), modelIndex);
825 m_contentHeightDirty = true;
831#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
832void ListViewWithPageHeader::itemCreated(int modelIndex, QQuickItem *item)
835void ListViewWithPageHeader::itemCreated(int modelIndex, QObject *object)
837 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
839 qWarning() << "ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
843// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
845 item->setParentItem(m_clipItem);
846 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
847 context->setContextProperty(QLatin1String("ListViewWithPageHeader"), this);
848 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
849 if (modelIndex == m_asyncRequestedIndex) {
850 createItem(modelIndex, false);
855void ListViewWithPageHeader::updateClipItem()
857 m_clipItem->setHeight(height() - m_headerItemShownHeight);
858 m_clipItem->setY(contentY() + m_headerItemShownHeight);
859 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
862void ListViewWithPageHeader::onContentHeightChanged()
867void ListViewWithPageHeader::onContentWidthChanged()
869 m_clipItem->setWidth(contentItem()->width());
872void ListViewWithPageHeader::onHeightChanged()
878#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
879void ListViewWithPageHeader::onModelUpdated(const QQuickChangeSet &changeSet, bool /*reset*/)
881void ListViewWithPageHeader::onModelUpdated(const QQmlChangeSet &changeSet, bool /*reset*/)
884 // TODO Do something with reset
885// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
886 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
888#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
889 Q_FOREACH(const QQuickChangeSet::Remove &remove, changeSet.removes()) {
891 Q_FOREACH(const QQmlChangeSet::Remove &remove, changeSet.removes()) {
893// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove.index << remove.count;
894 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
895 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
896 // If all the items we are removing are either not created or culled
897 // we have to grow down to avoid viewport changing
898 bool growDown = true;
899 for (int i = 0; growDown && i < remove.count; ++i) {
900 const int modelIndex = remove.index + i;
901 ListItem *item = itemAtIndex(modelIndex);
902 if (item && !item->culled()) {
906 for (int i = remove.count - 1; i >= 0; --i) {
907 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
908 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
909 ListItem *item = m_visibleItems[visibleIndex];
910 // Pass the section item down if needed
911 if (item->m_sectionItem && visibleIndex + 1 < m_visibleItems.count()) {
912 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
913 if (!nextItem->m_sectionItem) {
914 nextItem->m_sectionItem = item->m_sectionItem;
915 item->m_sectionItem = nullptr;
919 m_visibleItems.removeAt(visibleIndex);
924 } else if (remove.index <= m_firstVisibleIndex) {
925 if (!m_visibleItems.isEmpty()) {
926 // We removed the first item that is the one that positions the rest
927 // position the new first item correctly
928 m_visibleItems.first()->setY(oldFirstValidIndexPos);
930 m_firstVisibleIndex = -1;
933 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
934 m_firstVisibleIndex -= remove.count;
936 for (int i = remove.count - 1; i >= 0; --i) {
937 const int modelIndex = remove.index + i;
938 if (modelIndex == m_asyncRequestedIndex) {
939 m_asyncRequestedIndex = -1;
940 } else if (modelIndex < m_asyncRequestedIndex) {
941 m_asyncRequestedIndex--;
946#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
947 Q_FOREACH(const QQuickChangeSet::Insert &insert, changeSet.inserts()) {
949 Q_FOREACH(const QQmlChangeSet::Insert &insert, changeSet.inserts()) {
951// qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
952 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
953 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
954 if (insertingInValidIndexes || firstItemWithViewOnTop)
956 // If the items we are adding won't be really visible
957 // we grow up instead of down to not change the viewport
959 if (!firstItemWithViewOnTop) {
960 for (int i = 0; i < m_visibleItems.count(); ++i) {
961 if (!m_visibleItems[i]->culled()) {
962 if (insert.index <= m_firstVisibleIndex + i) {
970 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
971 for (int i = insert.count - 1; i >= 0; --i) {
972 const int modelIndex = insert.index + i;
973 ListItem *item = createItem(modelIndex, false);
975 ListItem *firstItem = m_visibleItems.first();
976 firstItem->setY(firstItem->y() - item->height());
979 // Adding an item may break a "same section" chain, so check
980 // if we need adding a new section item
981 if (m_sectionDelegate) {
982 ListItem *nextItem = itemAtIndex(modelIndex + 1);
983 if (nextItem && !nextItem->m_sectionItem) {
984 nextItem->m_sectionItem = getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/);
985 if (growUp && nextItem->m_sectionItem) {
986 ListItem *firstItem = m_visibleItems.first();
987 firstItem->setY(firstItem->y() - nextItem->m_sectionItem->height());
993 if (firstItemWithViewOnTop) {
994 ListItem *firstItem = m_visibleItems.first();
995 firstItem->setY(oldFirstValidIndexPos);
997 } else if (insert.index <= m_firstVisibleIndex) {
998 m_firstVisibleIndex += insert.count;
1001 for (int i = insert.count - 1; i >= 0; --i) {
1002 const int modelIndex = insert.index + i;
1003 if (modelIndex <= m_asyncRequestedIndex) {
1004 m_asyncRequestedIndex++;
1009 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
1013 for (int i = 0; i < m_visibleItems.count(); ++i) {
1014 ListItem *item = m_visibleItems[i];
1015 if (item->m_sectionItem) {
1016 QQmlContext *context = QQmlEngine::contextForObject(item->m_sectionItem)->parentContext();
1017 context->setContextProperty(QLatin1String("delegateIndex"), m_firstVisibleIndex + i);
1023 m_contentHeightDirty = true;
1026void ListViewWithPageHeader::onShowHeaderAnimationFinished()
1028 m_contentHeightDirty = true;
1032void ListViewWithPageHeader::itemGeometryChanged(QQuickItem * /*item*/, const QRectF &newGeometry, const QRectF &oldGeometry)
1034 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
1035 if (heightDiff != 0) {
1036 if (oldGeometry.y() + oldGeometry.height() + m_clipItem->y() < contentY() && !m_visibleItems.isEmpty()) {
1037 ListItem *firstItem = m_visibleItems.first();
1038 firstItem->setY(firstItem->y() - heightDiff);
1045 m_contentHeightDirty = true;
1049void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
1051 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
1052 if (m_headerItemShownHeight > 0) {
1053 // If the header is shown because of the clip
1055 m_headerItemShownHeight += heightDiff;
1056 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
1060 if (oldHeaderY + oldHeaderHeight > contentY()) {
1061 // If the header is shown because its position
1063 ListItem *firstItem = m_visibleItems.first();
1064 firstItem->setY(firstItem->y() + heightDiff);
1067 // If the header is not on screen, just change the start of the list
1068 // so the viewport is not changed
1075void ListViewWithPageHeader::adjustMinYExtent()
1077 if (m_visibleItems.isEmpty()) {
1080 qreal nonCreatedHeight = 0;
1081 if (m_firstVisibleIndex != 0) {
1082 // Calculate the average height of items to estimate the position of the list start
1083 const int visibleItems = m_visibleItems.count();
1084 qreal visibleItemsHeight = 0;
1085 Q_FOREACH(ListItem *item, m_visibleItems) {
1086 visibleItemsHeight += item->height();
1088 nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
1089// qDebug() << m_firstVisibleIndex << visibleItemsHeight << visibleItems << nonCreatedHeight;
1091 m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + (m_headerItem ? m_headerItem->implicitHeight() : 0);
1095ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
1097 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
1098 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
1099 return m_visibleItems[visibleIndexedModelIndex];
1104void ListViewWithPageHeader::layout()
1110 if (!m_visibleItems.isEmpty()) {
1111 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
1112 const qreal visibleTo = contentY() + height() - m_clipItem->y();
1114 qreal pos = m_visibleItems.first()->y();
1116// qDebug() << "ListViewWithPageHeader::layout Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
1117 int firstReallyVisibleItem = -1;
1118 int modelIndex = m_firstVisibleIndex;
1119 Q_FOREACH(ListItem *item, m_visibleItems) {
1120 const bool cull = pos + item->height() <= visibleFrom || pos >= visibleTo;
1121 item->setCulled(cull);
1123 if (!cull && firstReallyVisibleItem == -1) {
1124 firstReallyVisibleItem = modelIndex;
1125 if (m_topSectionItem) {
1126 // Positing the top section sticky item is a two step process
1127 // First we set it either we cull it (because it doesn't need to be sticked to the top)
1128 // or stick it to the top
1129 // Then after the loop we'll make sure that if there's another section just below it
1130 // pushed the sticky section up to make it disappear
1131 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
1132 bool showStickySectionItem;
1133 // We need to show the "top section sticky item" when the position at the "top" of the
1134 // viewport is bigger than the start of the position of the first visible item
1135 // i.e. the first visible item starts before the viewport, or when the first
1136 // visible item starts just at the viewport start and it does not have its own section item
1137 if (topSectionStickPos > pos) {
1138 showStickySectionItem = true;
1139 } else if (topSectionStickPos == pos) {
1140 showStickySectionItem = !item->m_sectionItem;
1142 showStickySectionItem = false;
1144 if (!showStickySectionItem) {
1145 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
1146 if (item->m_sectionItem) {
1147 // This seems it should happen since why would we cull the top section
1148 // if the first visible item has no section header? This only happens briefly
1149 // when increasing the height of a list that is at the bottom, the m_topSectionItem
1150 // gets shown shortly in the next polish call
1151 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(false);
1154 // Update the top sticky section header
1155 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1156 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
1157 context->setContextProperty(QLatin1String("section"), section);
1159 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
1160 m_topSectionItem->setY(topSectionStickPos);
1161 int delegateIndex = modelIndex;
1162 // Look for the first index with this section text
1163 while (delegateIndex > 0) {
1164 const QString prevSection = m_delegateModel->stringValue(delegateIndex - 1, m_sectionProperty);
1165 if (prevSection != section)
1169 context->setContextProperty(QLatin1String("delegateIndex"), delegateIndex);
1170 if (item->m_sectionItem) {
1171 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(true);
1176 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
1177 const qreal clipFrom = visibleFrom + (!item->m_sectionItem && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
1178 if (!cull && pos < clipFrom) {
1179 context->setContextProperty(QLatin1String("heightToClip"), clipFrom - pos);
1181 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
1183// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
1184 pos += item->height();
1188 // Second step of section sticky item positioning
1189 // Look at the next section header, check if it's pushing up the sticky one
1190 if (m_topSectionItem) {
1191 if (firstReallyVisibleItem >= 0) {
1192 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
1193 ListItem *item = m_visibleItems[i];
1194 if (item->m_sectionItem) {
1195 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
1196 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
1207void ListViewWithPageHeader::updatePolish()
1209 Q_FOREACH(ListItem *item, m_itemsToRelease)
1210 reallyReleaseItem(item);
1211 m_itemsToRelease.clear();
1220 if (m_contentHeightDirty) {
1221 qreal contentHeight;
1222 if (m_visibleItems.isEmpty()) {
1223 contentHeight = m_headerItem ? m_headerItem->height() : 0;
1225 const int modelCount = model()->rowCount();
1226 const int visibleItems = m_visibleItems.count();
1227 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
1228 qreal nonCreatedHeight = 0;
1229 if (lastValidIndex != modelCount - 1) {
1230 const int visibleItems = m_visibleItems.count();
1231 qreal visibleItemsHeight = 0;
1232 Q_FOREACH(ListItem *item, m_visibleItems) {
1233 visibleItemsHeight += item->height();
1235 const int unknownSizes = modelCount - (m_firstVisibleIndex + visibleItems);
1236 nonCreatedHeight = unknownSizes * visibleItemsHeight / visibleItems;
1238 ListItem *item = m_visibleItems.last();
1239 contentHeight = nonCreatedHeight + item->y() + item->height() + m_clipItem->y();
1240 if (m_firstVisibleIndex != 0) {
1241 // Make sure that if we are shrinking we tell the view we still fit
1242 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
1246 m_contentHeightDirty = false;
1248 setContentHeight(contentHeight);
1252#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