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);
395 return maximizeVisibleArea(listItem, listItem->height());
398bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex, int itemHeight)
403 ListItem *listItem = itemAtIndex(modelIndex);
405 return maximizeVisibleArea(listItem, itemHeight + (listItem->m_sectionItem ? listItem->m_sectionItem->height() : 0));
411bool ListViewWithPageHeader::maximizeVisibleArea(ListItem *listItem, int listItemHeight)
415 const auto listItemY = m_clipItem->y() + listItem->y();
416 if (listItemY > contentY() && listItemY + listItemHeight > contentY() + height()) {
417 // we can scroll the list up to show more stuff
418 const auto to = qMin(listItemY, listItemY + listItemHeight - height());
419 m_contentYAnimation->setTo(to);
420 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
421 m_contentYAnimation->start();
422 } else if ((listItemY < contentY() && listItemY + listItemHeight < contentY() + height()) ||
423 (m_topSectionItem && !listItem->m_sectionItem && listItemY - m_topSectionItem->height() < contentY() && listItemY + listItemHeight < contentY() + height()))
425 // we can scroll the list down to show more stuff
426 auto realVisibleListItemY = listItemY;
427 if (m_topSectionItem) {
428 // If we are showing the top section sticky item and this item doesn't have a section
429 // item we have to make sure to scroll it a bit more so that it is not underlapping
430 // the top section sticky item
431 bool topSectionShown = !QQuickItemPrivate::get(m_topSectionItem)->culled;
432 if (topSectionShown && !listItem->m_sectionItem) {
433 realVisibleListItemY -= m_topSectionItem->height();
436 const auto to = qMax(realVisibleListItemY, listItemY + listItemHeight - height());
437 m_contentYAnimation->setTo(to);
438 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
439 m_contentYAnimation->start();
446qreal ListViewWithPageHeader::minYExtent() const
448// qDebug() << "ListViewWithPageHeader::minYExtent" << m_minYExtent;
452void ListViewWithPageHeader::componentComplete()
455 m_delegateModel->componentComplete();
457 QQuickFlickable::componentComplete();
462void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
464 QQuickFlickable::viewportMoved(orient);
465// qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
466 qreal diff = m_previousContentY - contentY();
467 const bool showHeaderAnimationRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationShowHeader;
469 auto oldHeaderItemShownHeight = m_headerItemShownHeight;
470 if (contentY() < -m_minYExtent) {
471 // Stick the header item to the top when dragging down
472 m_headerItem->setY(contentY());
473 m_headerItem->setHeight(m_headerItem->implicitHeight() + (-m_minYExtent - contentY()));
475 m_headerItem->setHeight(m_headerItem->implicitHeight());
476 // We are going down (but it's not because of the rebound at the end)
477 // (but the header was not shown by it's own position)
478 // or the header is partially shown and we are not doing a maximizeVisibleArea either
479 const bool scrolledUp = m_previousContentY > contentY();
480 const bool notRebounding = contentY() + height() < contentHeight();
481 const bool notShownByItsOwn = contentY() + diff > m_headerItem->y() + m_headerItem->height();
482 const bool maximizeVisibleAreaRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationMaximizeVisibleArea;
484 if (!scrolledUp && contentY() == -m_minYExtent) {
485 m_headerItemShownHeight = 0;
486 m_headerItem->setY(contentY());
487 } else if ((scrolledUp && notRebounding && notShownByItsOwn && !maximizeVisibleAreaRunning) || (m_headerItemShownHeight > 0)) {
488 if (maximizeVisibleAreaRunning && diff > 0) // If we are maximizing and the header was shown, make sure we hide it
489 m_headerItemShownHeight -= diff;
491 m_headerItemShownHeight += diff;
492 if (contentY() == -m_minYExtent) {
493 m_headerItemShownHeight = 0;
495 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
497 if (m_headerItemShownHeight > 0) {
498 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
500 m_headerItem->setY(-m_minYExtent);
504 // We will be changing the clip item, need to accomadate for it
505 // otherwise we move the firstItem down/up twice (unless the
506 // show header animation is running, where we want to keep the viewport stable)
507 if (!showHeaderAnimationRunning) {
508 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
513 if (!m_visibleItems.isEmpty()) {
515 ListItem *firstItem = m_visibleItems.first();
516 firstItem->setY(firstItem->y() + diff);
517 if (showHeaderAnimationRunning) {
522 m_previousContentY = contentY();
527void ListViewWithPageHeader::createDelegateModel()
529#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
530 m_delegateModel = new QQuickVisualDataModel(qmlContext(this), this);
531 connect(m_delegateModel, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(itemCreated(int,QQuickItem*)));
533 m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
534 connect(m_delegateModel, SIGNAL(createdItem(int,QObject*)), this, SLOT(itemCreated(int,QObject*)));
536 if (isComponentComplete())
537 m_delegateModel->componentComplete();
538 updateWatchedRoles();
541void ListViewWithPageHeader::refill()
546 if (!isComponentComplete()) {
550 const qreal buffer = height() * bufferRatio;
551 const qreal from = contentY();
552 const qreal to = from + height();
553 const qreal bufferFrom = from - buffer;
554 const qreal bufferTo = to + buffer;
556 bool added = addVisibleItems(from, to, false);
557 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
558 added |= addVisibleItems(bufferFrom, bufferTo, true);
560 if (added || removed) {
561 m_contentHeightDirty = true;
565bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
570 if (m_delegateModel->count() == 0)
574// qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
578 if (!m_visibleItems.isEmpty()) {
579 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
580 item = m_visibleItems.last();
581 pos = item->y() + item->height() + m_clipItem->y();
583 bool changed = false;
584// qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
585 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
586// qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
587 if (!(item = createItem(modelIndex, asynchronous)))
589 pos += item->height();
596 if (!m_visibleItems.isEmpty()) {
597 modelIndex = m_firstVisibleIndex - 1;
598 item = m_visibleItems.first();
599 pos = item->y() + m_clipItem->y();
601 while (modelIndex >= 0 && pos > fillFrom) {
602// qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
603 if (!(item = createItem(modelIndex, asynchronous)))
605 pos -= item->height();
613void ListViewWithPageHeader::reallyReleaseItem(ListItem *listItem)
615 QQuickItem *item = listItem->m_item;
616#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
617 QQuickVisualModel::ReleaseFlags flags = m_delegateModel->release(item);
618 if (flags & QQuickVisualModel::Destroyed) {
620 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
621 if (flags & QQmlDelegateModel::Destroyed) {
623 item->setParentItem(nullptr);
625 delete listItem->m_sectionItem;
629void ListViewWithPageHeader::releaseItem(ListItem *listItem)
631 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(listItem->m_item);
632 itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
633 m_itemsToRelease << listItem;
636void ListViewWithPageHeader::updateWatchedRoles()
638 if (m_delegateModel) {
639 QList<QByteArray> roles;
640 if (!m_sectionProperty.isEmpty())
641 roles << m_sectionProperty.toUtf8();
642 m_delegateModel->setWatchedRoles(roles);
646QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
648 if (!m_sectionDelegate)
651 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
652 if (modelIndex > 0) {
653 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
654 if (section == prevSection)
657 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
658 // Already inserted items can't steal next section header
659 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
660 if (section == nextSection) {
661 // Steal the section header
662 ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
664 QQuickItem *sectionItem = nextItem->m_sectionItem;
665 nextItem->m_sectionItem = nullptr;
671 return getSectionItem(section);
674QQuickItem *ListViewWithPageHeader::getSectionItem(const QString §ionText)
676 QQuickItem *sectionItem = nullptr;
678 QQmlContext *creationContext = m_sectionDelegate->creationContext();
679 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
680 context->setContextProperty(QLatin1String("section"), sectionText);
681 context->setContextProperty(QLatin1String("delegateIndex"), -1);
682 QObject *nobj = m_sectionDelegate->beginCreate(context);
684 QQml_setParent_noEvent(context, nobj);
685 sectionItem = qobject_cast<QQuickItem *>(nobj);
689 sectionItem->setZ(2);
690 QQml_setParent_noEvent(sectionItem, m_clipItem);
691 sectionItem->setParentItem(m_clipItem);
696 m_sectionDelegate->completeCreate();
698 // TODO attach to sectionItem so we can accomodate to it changing its height
703bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
705// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
706 // Do not remove items if we are overshooting up or down, since we'll come back
707 // to the "stable" position and delete/create items without any reason
708 if (contentY() < -m_minYExtent) {
710 } else if (contentY() + height() > contentHeight()) {
713 bool changed = false;
715 bool foundVisible = false;
717 int removedItems = 0;
718 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
719 while (i < m_visibleItems.count()) {
720 ListItem *item = m_visibleItems[i];
721 const qreal pos = item->y() + m_clipItem->y();
722// qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
723 if (pos + item->height() < bufferFrom || pos > bufferTo) {
724// qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
726 m_visibleItems.removeAt(i);
732 const int itemIndex = m_firstVisibleIndex + removedItems + i;
733 m_firstVisibleIndex = itemIndex;
738 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
745ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
747// qDebug() << "CREATE ITEM" << modelIndex;
748 if (asynchronous && m_asyncRequestedIndex != -1)
751 m_asyncRequestedIndex = -1;
752#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
753 QQuickItem *item = m_delegateModel->item(modelIndex, asynchronous);
755 QObject* object = m_delegateModel->object(modelIndex, asynchronous);
756 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
759#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
760 m_asyncRequestedIndex = modelIndex;
763 m_delegateModel->release(object);
764 if (!m_delegateValidated) {
765 m_delegateValidated = true;
766 QObject* delegateObj = delegate();
767 qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
770 m_asyncRequestedIndex = modelIndex;
775// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
776 ListItem *listItem = new ListItem;
777 listItem->m_item = item;
778 listItem->m_sectionItem = getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/);
779 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
780 ListItem *prevItem = itemAtIndex(modelIndex - 1);
781 bool lostItem = false; // Is an item that we requested async but because of model changes
782 // it is no longer attached to any of the existing items (has no prev nor next item)
783 // nor is the first item
785 listItem->setY(prevItem->y() + prevItem->height());
787 ListItem *currItem = itemAtIndex(modelIndex);
789 // There's something already in m_visibleItems at out index, meaning this is an insert, so attach to its top
790 listItem->setY(currItem->y() - listItem->height());
792 ListItem *nextItem = itemAtIndex(modelIndex + 1);
794 listItem->setY(nextItem->y() - listItem->height());
795 } else if (modelIndex == 0 && m_headerItem) {
796 listItem->setY(m_headerItem->height());
797 } else if (!m_visibleItems.isEmpty()) {
803 releaseItem(listItem);
806 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() <= contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
807 if (m_visibleItems.isEmpty()) {
808 m_visibleItems << listItem;
810 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
812 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
813 m_firstVisibleIndex = modelIndex;
816 if (listItem->m_sectionItem) {
817 QQmlContext *context = QQmlEngine::contextForObject(listItem->m_sectionItem)->parentContext();
818 context->setContextProperty(QLatin1String("delegateIndex"), modelIndex);
821 m_contentHeightDirty = true;
827#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
828void ListViewWithPageHeader::itemCreated(int modelIndex, QQuickItem *item)
831void ListViewWithPageHeader::itemCreated(int modelIndex, QObject *object)
833 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
835 qWarning() << "ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
839// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
841 item->setParentItem(m_clipItem);
842 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
843 context->setContextProperty(QLatin1String("ListViewWithPageHeader"), this);
844 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
845 if (modelIndex == m_asyncRequestedIndex) {
846 createItem(modelIndex, false);
851void ListViewWithPageHeader::updateClipItem()
853 m_clipItem->setHeight(height() - m_headerItemShownHeight);
854 m_clipItem->setY(contentY() + m_headerItemShownHeight);
855 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
858void ListViewWithPageHeader::onContentHeightChanged()
863void ListViewWithPageHeader::onContentWidthChanged()
865 m_clipItem->setWidth(contentItem()->width());
868void ListViewWithPageHeader::onHeightChanged()
874#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
875void ListViewWithPageHeader::onModelUpdated(const QQuickChangeSet &changeSet, bool /*reset*/)
877void ListViewWithPageHeader::onModelUpdated(const QQmlChangeSet &changeSet, bool /*reset*/)
880 // TODO Do something with reset
881// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
882 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
884#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
885 Q_FOREACH(const QQuickChangeSet::Remove &remove, changeSet.removes()) {
887 Q_FOREACH(const QQmlChangeSet::Remove &remove, changeSet.removes()) {
889// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove.index << remove.count;
890 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
891 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
892 // If all the items we are removing are either not created or culled
893 // we have to grow down to avoid viewport changing
894 bool growDown = true;
895 for (int i = 0; growDown && i < remove.count; ++i) {
896 const int modelIndex = remove.index + i;
897 ListItem *item = itemAtIndex(modelIndex);
898 if (item && !item->culled()) {
902 for (int i = remove.count - 1; i >= 0; --i) {
903 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
904 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
905 ListItem *item = m_visibleItems[visibleIndex];
906 // Pass the section item down if needed
907 if (item->m_sectionItem && visibleIndex + 1 < m_visibleItems.count()) {
908 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
909 if (!nextItem->m_sectionItem) {
910 nextItem->m_sectionItem = item->m_sectionItem;
911 item->m_sectionItem = nullptr;
915 m_visibleItems.removeAt(visibleIndex);
920 } else if (remove.index <= m_firstVisibleIndex) {
921 if (!m_visibleItems.isEmpty()) {
922 // We removed the first item that is the one that positions the rest
923 // position the new first item correctly
924 m_visibleItems.first()->setY(oldFirstValidIndexPos);
926 m_firstVisibleIndex = -1;
929 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
930 m_firstVisibleIndex -= remove.count;
932 for (int i = remove.count - 1; i >= 0; --i) {
933 const int modelIndex = remove.index + i;
934 if (modelIndex == m_asyncRequestedIndex) {
935 m_asyncRequestedIndex = -1;
936 } else if (modelIndex < m_asyncRequestedIndex) {
937 m_asyncRequestedIndex--;
942#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
943 Q_FOREACH(const QQuickChangeSet::Insert &insert, changeSet.inserts()) {
945 Q_FOREACH(const QQmlChangeSet::Insert &insert, changeSet.inserts()) {
947// qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
948 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
949 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
950 if (insertingInValidIndexes || firstItemWithViewOnTop)
952 // If the items we are adding won't be really visible
953 // we grow up instead of down to not change the viewport
955 if (!firstItemWithViewOnTop) {
956 for (int i = 0; i < m_visibleItems.count(); ++i) {
957 if (!m_visibleItems[i]->culled()) {
958 if (insert.index <= m_firstVisibleIndex + i) {
966 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
967 for (int i = insert.count - 1; i >= 0; --i) {
968 const int modelIndex = insert.index + i;
969 ListItem *item = createItem(modelIndex, false);
971 ListItem *firstItem = m_visibleItems.first();
972 firstItem->setY(firstItem->y() - item->height());
975 // Adding an item may break a "same section" chain, so check
976 // if we need adding a new section item
977 if (m_sectionDelegate) {
978 ListItem *nextItem = itemAtIndex(modelIndex + 1);
979 if (nextItem && !nextItem->m_sectionItem) {
980 nextItem->m_sectionItem = getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/);
981 if (growUp && nextItem->m_sectionItem) {
982 ListItem *firstItem = m_visibleItems.first();
983 firstItem->setY(firstItem->y() - nextItem->m_sectionItem->height());
989 if (firstItemWithViewOnTop) {
990 ListItem *firstItem = m_visibleItems.first();
991 firstItem->setY(oldFirstValidIndexPos);
993 } else if (insert.index <= m_firstVisibleIndex) {
994 m_firstVisibleIndex += insert.count;
997 for (int i = insert.count - 1; i >= 0; --i) {
998 const int modelIndex = insert.index + i;
999 if (modelIndex <= m_asyncRequestedIndex) {
1000 m_asyncRequestedIndex++;
1005 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
1009 for (int i = 0; i < m_visibleItems.count(); ++i) {
1010 ListItem *item = m_visibleItems[i];
1011 if (item->m_sectionItem) {
1012 QQmlContext *context = QQmlEngine::contextForObject(item->m_sectionItem)->parentContext();
1013 context->setContextProperty(QLatin1String("delegateIndex"), m_firstVisibleIndex + i);
1019 m_contentHeightDirty = true;
1022void ListViewWithPageHeader::onShowHeaderAnimationFinished()
1024 m_contentHeightDirty = true;
1028void ListViewWithPageHeader::itemGeometryChanged(QQuickItem * /*item*/, const QRectF &newGeometry, const QRectF &oldGeometry)
1030 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
1031 if (heightDiff != 0) {
1032 if (oldGeometry.y() + oldGeometry.height() + m_clipItem->y() < contentY() && !m_visibleItems.isEmpty()) {
1033 ListItem *firstItem = m_visibleItems.first();
1034 firstItem->setY(firstItem->y() - heightDiff);
1041 m_contentHeightDirty = true;
1045void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
1047 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
1048 if (m_headerItemShownHeight > 0) {
1049 // If the header is shown because of the clip
1051 m_headerItemShownHeight += heightDiff;
1052 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
1056 if (oldHeaderY + oldHeaderHeight > contentY()) {
1057 // If the header is shown because its position
1059 ListItem *firstItem = m_visibleItems.first();
1060 firstItem->setY(firstItem->y() + heightDiff);
1063 // If the header is not on screen, just change the start of the list
1064 // so the viewport is not changed
1071void ListViewWithPageHeader::adjustMinYExtent()
1073 if (m_visibleItems.isEmpty()) {
1076 qreal nonCreatedHeight = 0;
1077 if (m_firstVisibleIndex != 0) {
1078 // Calculate the average height of items to estimate the position of the list start
1079 const int visibleItems = m_visibleItems.count();
1080 qreal visibleItemsHeight = 0;
1081 Q_FOREACH(ListItem *item, m_visibleItems) {
1082 visibleItemsHeight += item->height();
1084 nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
1085// qDebug() << m_firstVisibleIndex << visibleItemsHeight << visibleItems << nonCreatedHeight;
1087 m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + (m_headerItem ? m_headerItem->implicitHeight() : 0);
1091ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
1093 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
1094 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
1095 return m_visibleItems[visibleIndexedModelIndex];
1100void ListViewWithPageHeader::layout()
1106 if (!m_visibleItems.isEmpty()) {
1107 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
1108 const qreal visibleTo = contentY() + height() - m_clipItem->y();
1110 qreal pos = m_visibleItems.first()->y();
1112// qDebug() << "ListViewWithPageHeader::layout Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
1113 int firstReallyVisibleItem = -1;
1114 int modelIndex = m_firstVisibleIndex;
1115 Q_FOREACH(ListItem *item, m_visibleItems) {
1116 const bool cull = pos + item->height() <= visibleFrom || pos >= visibleTo;
1117 item->setCulled(cull);
1119 if (!cull && firstReallyVisibleItem == -1) {
1120 firstReallyVisibleItem = modelIndex;
1121 if (m_topSectionItem) {
1122 // Positing the top section sticky item is a two step process
1123 // First we set it either we cull it (because it doesn't need to be sticked to the top)
1124 // or stick it to the top
1125 // Then after the loop we'll make sure that if there's another section just below it
1126 // pushed the sticky section up to make it disappear
1127 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
1128 bool showStickySectionItem;
1129 // We need to show the "top section sticky item" when the position at the "top" of the
1130 // viewport is bigger than the start of the position of the first visible item
1131 // i.e. the first visible item starts before the viewport, or when the first
1132 // visible item starts just at the viewport start and it does not have its own section item
1133 if (topSectionStickPos > pos) {
1134 showStickySectionItem = true;
1135 } else if (topSectionStickPos == pos) {
1136 showStickySectionItem = !item->m_sectionItem;
1138 showStickySectionItem = false;
1140 if (!showStickySectionItem) {
1141 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
1142 if (item->m_sectionItem) {
1143 // This seems it should happen since why would we cull the top section
1144 // if the first visible item has no section header? This only happens briefly
1145 // when increasing the height of a list that is at the bottom, the m_topSectionItem
1146 // gets shown shortly in the next polish call
1147 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(false);
1150 // Update the top sticky section header
1151 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1152 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
1153 context->setContextProperty(QLatin1String("section"), section);
1155 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
1156 m_topSectionItem->setY(topSectionStickPos);
1157 int delegateIndex = modelIndex;
1158 // Look for the first index with this section text
1159 while (delegateIndex > 0) {
1160 const QString prevSection = m_delegateModel->stringValue(delegateIndex - 1, m_sectionProperty);
1161 if (prevSection != section)
1165 context->setContextProperty(QLatin1String("delegateIndex"), delegateIndex);
1166 if (item->m_sectionItem) {
1167 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(true);
1172 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
1173 const qreal clipFrom = visibleFrom + (!item->m_sectionItem && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
1174 if (!cull && pos < clipFrom) {
1175 context->setContextProperty(QLatin1String("heightToClip"), clipFrom - pos);
1177 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
1179// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
1180 pos += item->height();
1184 // Second step of section sticky item positioning
1185 // Look at the next section header, check if it's pushing up the sticky one
1186 if (m_topSectionItem) {
1187 if (firstReallyVisibleItem >= 0) {
1188 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
1189 ListItem *item = m_visibleItems[i];
1190 if (item->m_sectionItem) {
1191 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
1192 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
1203void ListViewWithPageHeader::updatePolish()
1205 Q_FOREACH(ListItem *item, m_itemsToRelease)
1206 reallyReleaseItem(item);
1207 m_itemsToRelease.clear();
1216 if (m_contentHeightDirty) {
1217 qreal contentHeight;
1218 if (m_visibleItems.isEmpty()) {
1219 contentHeight = m_headerItem ? m_headerItem->height() : 0;
1221 const int modelCount = model()->rowCount();
1222 const int visibleItems = m_visibleItems.count();
1223 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
1224 qreal nonCreatedHeight = 0;
1225 if (lastValidIndex != modelCount - 1) {
1226 const int visibleItems = m_visibleItems.count();
1227 qreal visibleItemsHeight = 0;
1228 Q_FOREACH(ListItem *item, m_visibleItems) {
1229 visibleItemsHeight += item->height();
1231 const int unknownSizes = modelCount - (m_firstVisibleIndex + visibleItems);
1232 nonCreatedHeight = unknownSizes * visibleItemsHeight / visibleItems;
1234 ListItem *item = m_visibleItems.last();
1235 contentHeight = nonCreatedHeight + item->y() + item->height() + m_clipItem->y();
1236 if (m_firstVisibleIndex != 0) {
1237 // Make sure that if we are shrinking we tell the view we still fit
1238 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
1242 m_contentHeightDirty = false;
1243 setContentHeight(contentHeight);
1247#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