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 if (m_delegateModel->count() <= 0)
333 qreal headerHeight = (m_headerItem ? m_headerItem->height() : 0);
334 if (m_firstVisibleIndex != 0) {
335 // TODO This could be optimized by trying to reuse the interesection
336 // of items that may end up intersecting between the existing
337 // m_visibleItems and the items we are creating in the next loop
338 Q_FOREACH(ListItem *item, m_visibleItems)
340 m_visibleItems.clear();
341 m_firstVisibleIndex = -1;
343 // Create the item 0, it will be already correctly positioned at createItem()
345 ListItem *item = createItem(0, false);
346 // Create the subsequent items
348 qreal pos = item->y() + item->height();
349 const qreal buffer = height() * bufferRatio;
350 const qreal bufferTo = height() + buffer;
351 while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
352 if (!(item = createItem(modelIndex, false)))
354 pos += item->height();
358 m_previousContentY = m_visibleItems.first()->y() - headerHeight;
360 setContentY(m_visibleItems.first()->y() + m_clipItem->y() - headerHeight);
363void ListViewWithPageHeader::showHeader()
368 auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
369 if (to != contentY()) {
370 const bool headerShownByItsOwn = contentY() < m_headerItem->y() + m_headerItem->height();
371 if (headerShownByItsOwn && m_headerItemShownHeight == 0) {
372 // We are not clipping since we are just at the top of the viewport
373 // but because of the showHeader animation we will need to, so
374 // enable the clipping without logically moving the items
375 m_headerItemShownHeight = m_headerItem->y() + m_headerItem->height() - contentY();
376 if (!m_visibleItems.isEmpty()) {
378 ListItem *firstItem = m_visibleItems.first();
379 firstItem->setY(firstItem->y() - m_headerItemShownHeight);
383 m_contentYAnimation->setTo(to);
384 contentYAnimationType = ContentYAnimationShowHeader;
385 m_contentYAnimation->start();
389QQuickItem *ListViewWithPageHeader::item(int modelIndex) const
391 ListItem *item = itemAtIndex(modelIndex);
398bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex)
400 ListItem *listItem = itemAtIndex(modelIndex);
402 return maximizeVisibleArea(listItem, listItem->height());
408bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex, int itemHeight)
413 ListItem *listItem = itemAtIndex(modelIndex);
415 return maximizeVisibleArea(listItem, itemHeight + (listItem->m_sectionItem ? listItem->m_sectionItem->height() : 0));
421bool ListViewWithPageHeader::maximizeVisibleArea(ListItem *listItem, int listItemHeight)
425 const auto listItemY = m_clipItem->y() + listItem->y();
426 if (listItemY > contentY() && listItemY + listItemHeight > contentY() + height()) {
427 // we can scroll the list up to show more stuff
428 const auto to = qMin(listItemY, listItemY + listItemHeight - height());
429 m_contentYAnimation->setTo(to);
430 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
431 m_contentYAnimation->start();
432 } else if ((listItemY < contentY() && listItemY + listItemHeight < contentY() + height()) ||
433 (m_topSectionItem && !listItem->m_sectionItem && listItemY - m_topSectionItem->height() < contentY() && listItemY + listItemHeight < contentY() + height()))
435 // we can scroll the list down to show more stuff
436 auto realVisibleListItemY = listItemY;
437 if (m_topSectionItem) {
438 // If we are showing the top section sticky item and this item doesn't have a section
439 // item we have to make sure to scroll it a bit more so that it is not underlapping
440 // the top section sticky item
441 bool topSectionShown = !QQuickItemPrivate::get(m_topSectionItem)->culled;
442 if (topSectionShown && !listItem->m_sectionItem) {
443 realVisibleListItemY -= m_topSectionItem->height();
446 const auto to = qMax(realVisibleListItemY, listItemY + listItemHeight - height());
447 m_contentYAnimation->setTo(to);
448 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
449 m_contentYAnimation->start();
456qreal ListViewWithPageHeader::minYExtent() const
458// qDebug() << "ListViewWithPageHeader::minYExtent" << m_minYExtent;
462void ListViewWithPageHeader::componentComplete()
465 m_delegateModel->componentComplete();
467 QQuickFlickable::componentComplete();
472void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
474 QQuickFlickable::viewportMoved(orient);
475// qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
476 qreal diff = m_previousContentY - contentY();
477 const bool showHeaderAnimationRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationShowHeader;
479 auto oldHeaderItemShownHeight = m_headerItemShownHeight;
480 if (contentY() < -m_minYExtent) {
481 // Stick the header item to the top when dragging down
482 m_headerItem->setY(contentY());
483 m_headerItem->setHeight(m_headerItem->implicitHeight() + (-m_minYExtent - contentY()));
485 m_headerItem->setHeight(m_headerItem->implicitHeight());
486 // We are going down (but it's not because of the rebound at the end)
487 // (but the header was not shown by it's own position)
488 // or the header is partially shown and we are not doing a maximizeVisibleArea either
489 const bool scrolledUp = m_previousContentY > contentY();
490 const bool notRebounding = contentY() + height() < contentHeight();
491 const bool notShownByItsOwn = contentY() + diff >= m_headerItem->y() + m_headerItem->height();
492 const bool maximizeVisibleAreaRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationMaximizeVisibleArea;
494 if (!scrolledUp && contentY() == -m_minYExtent) {
495 m_headerItemShownHeight = 0;
496 m_headerItem->setY(contentY());
497 } else if ((scrolledUp && notRebounding && notShownByItsOwn && !maximizeVisibleAreaRunning) || (m_headerItemShownHeight > 0)) {
498 if (maximizeVisibleAreaRunning && diff > 0) // If we are maximizing and the header was shown, make sure we hide it
499 m_headerItemShownHeight -= diff;
501 m_headerItemShownHeight += diff;
502 if (contentY() == -m_minYExtent) {
503 m_headerItemShownHeight = 0;
505 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
507 if (m_headerItemShownHeight > 0) {
508 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
510 m_headerItem->setY(-m_minYExtent);
514 // We will be changing the clip item, need to accomadate for it
515 // otherwise we move the firstItem down/up twice (unless the
516 // show header animation is running, where we want to keep the viewport stable)
517 if (!showHeaderAnimationRunning) {
518 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
523 if (!m_visibleItems.isEmpty()) {
525 ListItem *firstItem = m_visibleItems.first();
526 firstItem->setY(firstItem->y() + diff);
527 if (showHeaderAnimationRunning) {
532 m_previousContentY = contentY();
537void ListViewWithPageHeader::createDelegateModel()
539#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
540 m_delegateModel = new QQuickVisualDataModel(qmlContext(this), this);
541 connect(m_delegateModel, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(itemCreated(int,QQuickItem*)));
543 m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
544 connect(m_delegateModel, SIGNAL(createdItem(int,QObject*)), this, SLOT(itemCreated(int,QObject*)));
546 if (isComponentComplete())
547 m_delegateModel->componentComplete();
548 updateWatchedRoles();
551void ListViewWithPageHeader::refill()
556 if (!isComponentComplete()) {
560 const qreal buffer = height() * bufferRatio;
561 const qreal from = contentY();
562 const qreal to = from + height();
563 const qreal bufferFrom = from - buffer;
564 const qreal bufferTo = to + buffer;
566 bool added = addVisibleItems(from, to, false);
567 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
568 added |= addVisibleItems(bufferFrom, bufferTo, true);
570 if (added || removed) {
571 m_contentHeightDirty = true;
575bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
580 if (m_delegateModel->count() == 0)
584// qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
588 if (!m_visibleItems.isEmpty()) {
589 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
590 item = m_visibleItems.last();
591 pos = item->y() + item->height() + m_clipItem->y();
593 bool changed = false;
594// qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
595 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
596// qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
597 if (!(item = createItem(modelIndex, asynchronous)))
599 pos += item->height();
606 if (!m_visibleItems.isEmpty()) {
607 modelIndex = m_firstVisibleIndex - 1;
608 item = m_visibleItems.first();
609 pos = item->y() + m_clipItem->y();
611 while (modelIndex >= 0 && pos > fillFrom) {
612// qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
613 if (!(item = createItem(modelIndex, asynchronous)))
615 pos -= item->height();
623void ListViewWithPageHeader::reallyReleaseItem(ListItem *listItem)
625 QQuickItem *item = listItem->m_item;
626#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
627 QQuickVisualModel::ReleaseFlags flags = m_delegateModel->release(item);
628 if (flags & QQuickVisualModel::Destroyed) {
630 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
631 if (flags & QQmlDelegateModel::Destroyed) {
633 item->setParentItem(nullptr);
635 delete listItem->m_sectionItem;
639void ListViewWithPageHeader::releaseItem(ListItem *listItem)
641 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(listItem->m_item);
642 itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
643 m_itemsToRelease << listItem;
646void ListViewWithPageHeader::updateWatchedRoles()
648 if (m_delegateModel) {
649 QList<QByteArray> roles;
650 if (!m_sectionProperty.isEmpty())
651 roles << m_sectionProperty.toUtf8();
652 m_delegateModel->setWatchedRoles(roles);
656QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
658 if (!m_sectionDelegate)
661 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
662 if (modelIndex > 0) {
663 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
664 if (section == prevSection)
667 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
668 // Already inserted items can't steal next section header
669 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
670 if (section == nextSection) {
671 // Steal the section header
672 ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
674 QQuickItem *sectionItem = nextItem->m_sectionItem;
675 nextItem->m_sectionItem = nullptr;
681 return getSectionItem(section);
684QQuickItem *ListViewWithPageHeader::getSectionItem(const QString §ionText)
686 QQuickItem *sectionItem = nullptr;
688 QQmlContext *creationContext = m_sectionDelegate->creationContext();
689 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
690 context->setContextProperty(QLatin1String("section"), sectionText);
691 context->setContextProperty(QLatin1String("delegateIndex"), -1);
692 QObject *nobj = m_sectionDelegate->beginCreate(context);
694 QQml_setParent_noEvent(context, nobj);
695 sectionItem = qobject_cast<QQuickItem *>(nobj);
699 sectionItem->setZ(2);
700 QQml_setParent_noEvent(sectionItem, m_clipItem);
701 sectionItem->setParentItem(m_clipItem);
706 m_sectionDelegate->completeCreate();
708 // TODO attach to sectionItem so we can accomodate to it changing its height
713bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
715// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
716 // Do not remove items if we are overshooting up or down, since we'll come back
717 // to the "stable" position and delete/create items without any reason
718 if (contentY() < -m_minYExtent) {
720 } else if (contentY() + height() > contentHeight()) {
723 bool changed = false;
725 bool foundVisible = false;
727 int removedItems = 0;
728 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
729 while (i < m_visibleItems.count()) {
730 ListItem *item = m_visibleItems[i];
731 const qreal pos = item->y() + m_clipItem->y();
732// qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
733 if (pos + item->height() < bufferFrom || pos > bufferTo) {
734// qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
736 m_visibleItems.removeAt(i);
742 const int itemIndex = m_firstVisibleIndex + removedItems + i;
743 m_firstVisibleIndex = itemIndex;
748 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
755ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
757// qDebug() << "CREATE ITEM" << modelIndex;
758 if (asynchronous && m_asyncRequestedIndex != -1)
761 m_asyncRequestedIndex = -1;
762#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
763 QQuickItem *item = m_delegateModel->item(modelIndex, asynchronous);
765 QObject* object = m_delegateModel->object(modelIndex, asynchronous);
766 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
769#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
770 m_asyncRequestedIndex = modelIndex;
773 m_delegateModel->release(object);
774 if (!m_delegateValidated) {
775 m_delegateValidated = true;
776 QObject* delegateObj = delegate();
777 qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
780 m_asyncRequestedIndex = modelIndex;
785// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
786 ListItem *listItem = new ListItem;
787 listItem->m_item = item;
788 listItem->m_sectionItem = getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/);
789 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
790 ListItem *prevItem = itemAtIndex(modelIndex - 1);
791 bool lostItem = false; // Is an item that we requested async but because of model changes
792 // it is no longer attached to any of the existing items (has no prev nor next item)
793 // nor is the first item
795 listItem->setY(prevItem->y() + prevItem->height());
797 ListItem *currItem = itemAtIndex(modelIndex);
799 // There's something already in m_visibleItems at out index, meaning this is an insert, so attach to its top
800 listItem->setY(currItem->y() - listItem->height());
802 ListItem *nextItem = itemAtIndex(modelIndex + 1);
804 listItem->setY(nextItem->y() - listItem->height());
805 } else if (modelIndex == 0 && m_headerItem) {
806 listItem->setY(m_headerItem->height());
807 } else if (!m_visibleItems.isEmpty()) {
813 releaseItem(listItem);
816 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() <= contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
817 if (m_visibleItems.isEmpty()) {
818 m_visibleItems << listItem;
820 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
822 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
823 m_firstVisibleIndex = modelIndex;
826 if (listItem->m_sectionItem) {
827 QQmlContext *context = QQmlEngine::contextForObject(listItem->m_sectionItem)->parentContext();
828 context->setContextProperty(QLatin1String("delegateIndex"), modelIndex);
831 m_contentHeightDirty = true;
837#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
838void ListViewWithPageHeader::itemCreated(int modelIndex, QQuickItem *item)
841void ListViewWithPageHeader::itemCreated(int modelIndex, QObject *object)
843 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
845 qWarning() << "ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
849// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
851 item->setParentItem(m_clipItem);
852 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
853 context->setContextProperty(QLatin1String("ListViewWithPageHeader"), this);
854 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
855 if (modelIndex == m_asyncRequestedIndex) {
856 createItem(modelIndex, false);
861void ListViewWithPageHeader::updateClipItem()
863 m_clipItem->setHeight(height() - m_headerItemShownHeight);
864 m_clipItem->setY(contentY() + m_headerItemShownHeight);
865 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
868void ListViewWithPageHeader::onContentHeightChanged()
873void ListViewWithPageHeader::onContentWidthChanged()
875 m_clipItem->setWidth(contentItem()->width());
878void ListViewWithPageHeader::onHeightChanged()
884#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
885void ListViewWithPageHeader::onModelUpdated(const QQuickChangeSet &changeSet, bool /*reset*/)
887void ListViewWithPageHeader::onModelUpdated(const QQmlChangeSet &changeSet, bool /*reset*/)
890 // TODO Do something with reset
891// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
892 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
894#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
895 Q_FOREACH(const QQuickChangeSet::Remove &remove, changeSet.removes()) {
897 Q_FOREACH(const QQmlChangeSet::Remove &remove, changeSet.removes()) {
899// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove.index << remove.count;
900 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
901 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
902 // If all the items we are removing are either not created or culled
903 // we have to grow down to avoid viewport changing
904 bool growDown = true;
905 for (int i = 0; growDown && i < remove.count; ++i) {
906 const int modelIndex = remove.index + i;
907 ListItem *item = itemAtIndex(modelIndex);
908 if (item && !item->culled()) {
912 for (int i = remove.count - 1; i >= 0; --i) {
913 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
914 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
915 ListItem *item = m_visibleItems[visibleIndex];
916 // Pass the section item down if needed
917 if (item->m_sectionItem && visibleIndex + 1 < m_visibleItems.count()) {
918 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
919 if (!nextItem->m_sectionItem) {
920 nextItem->m_sectionItem = item->m_sectionItem;
921 item->m_sectionItem = nullptr;
925 m_visibleItems.removeAt(visibleIndex);
930 } else if (remove.index <= m_firstVisibleIndex) {
931 if (!m_visibleItems.isEmpty()) {
932 // We removed the first item that is the one that positions the rest
933 // position the new first item correctly
934 m_visibleItems.first()->setY(oldFirstValidIndexPos);
936 m_firstVisibleIndex = -1;
939 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
940 m_firstVisibleIndex -= remove.count;
942 for (int i = remove.count - 1; i >= 0; --i) {
943 const int modelIndex = remove.index + i;
944 if (modelIndex == m_asyncRequestedIndex) {
945 m_asyncRequestedIndex = -1;
946 } else if (modelIndex < m_asyncRequestedIndex) {
947 m_asyncRequestedIndex--;
952#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
953 Q_FOREACH(const QQuickChangeSet::Insert &insert, changeSet.inserts()) {
955 Q_FOREACH(const QQmlChangeSet::Insert &insert, changeSet.inserts()) {
957// qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
958 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
959 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
960 if (insertingInValidIndexes || firstItemWithViewOnTop)
962 // If the items we are adding won't be really visible
963 // we grow up instead of down to not change the viewport
965 if (!firstItemWithViewOnTop) {
966 for (int i = 0; i < m_visibleItems.count(); ++i) {
967 if (!m_visibleItems[i]->culled()) {
968 if (insert.index <= m_firstVisibleIndex + i) {
976 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
977 for (int i = insert.count - 1; i >= 0; --i) {
978 const int modelIndex = insert.index + i;
979 ListItem *item = createItem(modelIndex, false);
981 ListItem *firstItem = m_visibleItems.first();
982 firstItem->setY(firstItem->y() - item->height());
985 // Adding an item may break a "same section" chain, so check
986 // if we need adding a new section item
987 if (m_sectionDelegate) {
988 ListItem *nextItem = itemAtIndex(modelIndex + 1);
989 if (nextItem && !nextItem->m_sectionItem) {
990 nextItem->m_sectionItem = getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/);
991 if (growUp && nextItem->m_sectionItem) {
992 ListItem *firstItem = m_visibleItems.first();
993 firstItem->setY(firstItem->y() - nextItem->m_sectionItem->height());
999 if (firstItemWithViewOnTop) {
1000 ListItem *firstItem = m_visibleItems.first();
1001 firstItem->setY(oldFirstValidIndexPos);
1003 } else if (insert.index <= m_firstVisibleIndex) {
1004 m_firstVisibleIndex += insert.count;
1007 for (int i = insert.count - 1; i >= 0; --i) {
1008 const int modelIndex = insert.index + i;
1009 if (modelIndex <= m_asyncRequestedIndex) {
1010 m_asyncRequestedIndex++;
1015#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
1016 Q_FOREACH(const QQuickChangeSet::Change &change, changeSet.changes()) {
1018 Q_FOREACH(const QQmlChangeSet::Change &change, changeSet.changes()) {
1020// qDebug() << "ListViewWithPageHeader::onModelUpdated Change" << change.index << change.count;
1021 for (int i = change.index; i < change.count; ++i) {
1022 ListItem *item = itemAtIndex(i);
1023 if (item && item->m_sectionItem) {
1024 QQmlContext *context = QQmlEngine::contextForObject(item->m_sectionItem)->parentContext();
1025 const QString sectionText = m_delegateModel->stringValue(i, m_sectionProperty);
1026 context->setContextProperty(QLatin1String("section"), sectionText);
1031 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
1035 for (int i = 0; i < m_visibleItems.count(); ++i) {
1036 ListItem *item = m_visibleItems[i];
1037 if (item->m_sectionItem) {
1038 QQmlContext *context = QQmlEngine::contextForObject(item->m_sectionItem)->parentContext();
1039 context->setContextProperty(QLatin1String("delegateIndex"), m_firstVisibleIndex + i);
1045 m_contentHeightDirty = true;
1048void ListViewWithPageHeader::onShowHeaderAnimationFinished()
1050 m_contentHeightDirty = true;
1054void ListViewWithPageHeader::itemGeometryChanged(QQuickItem * /*item*/, const QRectF &newGeometry, const QRectF &oldGeometry)
1056 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
1057 if (heightDiff != 0) {
1058 if (oldGeometry.y() + oldGeometry.height() + m_clipItem->y() < contentY() && !m_visibleItems.isEmpty()) {
1059 ListItem *firstItem = m_visibleItems.first();
1060 firstItem->setY(firstItem->y() - heightDiff);
1067 m_contentHeightDirty = true;
1071void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
1073 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
1074 if (m_headerItemShownHeight > 0) {
1075 // If the header is shown because of the clip
1077 m_headerItemShownHeight += heightDiff;
1078 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
1082 if (oldHeaderY + oldHeaderHeight > contentY()) {
1083 // If the header is shown because its position
1085 ListItem *firstItem = m_visibleItems.first();
1086 firstItem->setY(firstItem->y() + heightDiff);
1089 // If the header is not on screen, just change the start of the list
1090 // so the viewport is not changed
1097void ListViewWithPageHeader::adjustMinYExtent()
1099 if (m_visibleItems.isEmpty()) {
1102 qreal nonCreatedHeight = 0;
1103 if (m_firstVisibleIndex != 0) {
1104 // Calculate the average height of items to estimate the position of the list start
1105 const int visibleItems = m_visibleItems.count();
1106 qreal visibleItemsHeight = 0;
1107 Q_FOREACH(ListItem *item, m_visibleItems) {
1108 visibleItemsHeight += item->height();
1110 nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
1111// qDebug() << m_firstVisibleIndex << visibleItemsHeight << visibleItems << nonCreatedHeight;
1113 m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + (m_headerItem ? m_headerItem->implicitHeight() : 0);
1117ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
1119 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
1120 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
1121 return m_visibleItems[visibleIndexedModelIndex];
1126void ListViewWithPageHeader::layout()
1132 if (!m_visibleItems.isEmpty()) {
1133 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
1134 const qreal visibleTo = contentY() + height() - m_clipItem->y();
1136 qreal pos = m_visibleItems.first()->y();
1138// qDebug() << "ListViewWithPageHeader::layout Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
1139 int firstReallyVisibleItem = -1;
1140 int modelIndex = m_firstVisibleIndex;
1141 Q_FOREACH(ListItem *item, m_visibleItems) {
1142 const bool cull = pos + item->height() <= visibleFrom || pos >= visibleTo;
1143 item->setCulled(cull);
1145 if (!cull && firstReallyVisibleItem == -1) {
1146 firstReallyVisibleItem = modelIndex;
1147 if (m_topSectionItem) {
1148 // Positing the top section sticky item is a two step process
1149 // First we set it either we cull it (because it doesn't need to be sticked to the top)
1150 // or stick it to the top
1151 // Then after the loop we'll make sure that if there's another section just below it
1152 // pushed the sticky section up to make it disappear
1153 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
1154 bool showStickySectionItem;
1155 // We need to show the "top section sticky item" when the position at the "top" of the
1156 // viewport is bigger than the start of the position of the first visible item
1157 // i.e. the first visible item starts before the viewport, or when the first
1158 // visible item starts just at the viewport start and it does not have its own section item
1159 if (topSectionStickPos > pos) {
1160 showStickySectionItem = true;
1161 } else if (topSectionStickPos == pos) {
1162 showStickySectionItem = !item->m_sectionItem;
1164 showStickySectionItem = false;
1166 if (!showStickySectionItem) {
1167 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
1168 if (item->m_sectionItem) {
1169 // This seems it should happen since why would we cull the top section
1170 // if the first visible item has no section header? This only happens briefly
1171 // when increasing the height of a list that is at the bottom, the m_topSectionItem
1172 // gets shown shortly in the next polish call
1173 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(false);
1176 // Update the top sticky section header
1177 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1178 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
1179 context->setContextProperty(QLatin1String("section"), section);
1181 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
1182 m_topSectionItem->setY(topSectionStickPos);
1183 int delegateIndex = modelIndex;
1184 // Look for the first index with this section text
1185 while (delegateIndex > 0) {
1186 const QString prevSection = m_delegateModel->stringValue(delegateIndex - 1, m_sectionProperty);
1187 if (prevSection != section)
1191 context->setContextProperty(QLatin1String("delegateIndex"), delegateIndex);
1192 if (item->m_sectionItem) {
1193 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(true);
1198 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
1199 const qreal clipFrom = visibleFrom + (!item->m_sectionItem && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
1200 if (!cull && pos < clipFrom) {
1201 context->setContextProperty(QLatin1String("heightToClip"), clipFrom - pos);
1203 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
1205// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
1206 pos += item->height();
1210 // Second step of section sticky item positioning
1211 // Look at the next section header, check if it's pushing up the sticky one
1212 if (m_topSectionItem) {
1213 if (firstReallyVisibleItem >= 0) {
1214 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
1215 ListItem *item = m_visibleItems[i];
1216 if (item->m_sectionItem) {
1217 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
1218 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
1229void ListViewWithPageHeader::updatePolish()
1231 Q_FOREACH(ListItem *item, m_itemsToRelease)
1232 reallyReleaseItem(item);
1233 m_itemsToRelease.clear();
1242 if (m_contentHeightDirty) {
1243 qreal contentHeight;
1244 if (m_visibleItems.isEmpty()) {
1245 contentHeight = m_headerItem ? m_headerItem->height() : 0;
1247 const int modelCount = model()->rowCount();
1248 const int visibleItems = m_visibleItems.count();
1249 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
1250 qreal nonCreatedHeight = 0;
1251 if (lastValidIndex != modelCount - 1) {
1252 const int visibleItems = m_visibleItems.count();
1253 qreal visibleItemsHeight = 0;
1254 Q_FOREACH(ListItem *item, m_visibleItems) {
1255 visibleItemsHeight += item->height();
1257 const int unknownSizes = modelCount - (m_firstVisibleIndex + visibleItems);
1258 nonCreatedHeight = unknownSizes * visibleItemsHeight / visibleItems;
1260 ListItem *item = m_visibleItems.last();
1261 contentHeight = nonCreatedHeight + item->y() + item->height() + m_clipItem->y();
1262 if (m_firstVisibleIndex != 0) {
1263 // Make sure that if we are shrinking we tell the view we still fit
1264 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
1268 m_contentHeightDirty = false;
1270 setContentHeight(contentHeight);
1274#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