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(-1, 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);
398 const auto listItemY = m_clipItem->y() + listItem->y();
399 if (listItemY > contentY() && listItemY + listItem->height() > contentY() + height()) {
400 // we can scroll the list up to show more stuff
401 const auto to = qMin(listItemY, listItemY + listItem->height() - height());
402 m_contentYAnimation->setTo(to);
403 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
404 m_contentYAnimation->start();
405 } else if ((listItemY < contentY() && listItemY + listItem->height() < contentY() + height()) ||
406 (m_topSectionItem && !listItem->m_sectionItem && listItemY - m_topSectionItem->height() < contentY() && listItemY + listItem->height() < contentY() + height()))
408 // we can scroll the list down to show more stuff
409 auto realVisibleListItemY = listItemY;
410 if (m_topSectionItem) {
411 // If we are showing the top section sticky item and this item doesn't have a section
412 // item we have to make sure to scroll it a bit more so that it is not underlapping
413 // the top section sticky item
414 bool topSectionShown = !QQuickItemPrivate::get(m_topSectionItem)->culled;
415 if (topSectionShown && !listItem->m_sectionItem) {
416 realVisibleListItemY -= m_topSectionItem->height();
419 const auto to = qMax(realVisibleListItemY, listItemY + listItem->height() - height());
420 m_contentYAnimation->setTo(to);
421 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
422 m_contentYAnimation->start();
429qreal ListViewWithPageHeader::minYExtent() const
431// qDebug() << "ListViewWithPageHeader::minYExtent" << m_minYExtent;
435void ListViewWithPageHeader::componentComplete()
438 m_delegateModel->componentComplete();
440 QQuickFlickable::componentComplete();
445void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
447 QQuickFlickable::viewportMoved(orient);
448// qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
449 qreal diff = m_previousContentY - contentY();
450 const bool showHeaderAnimationRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationShowHeader;
452 auto oldHeaderItemShownHeight = m_headerItemShownHeight;
453 if (contentY() < -m_minYExtent) {
454 // Stick the header item to the top when dragging down
455 m_headerItem->setY(contentY());
456 m_headerItem->setHeight(m_headerItem->implicitHeight() + (-m_minYExtent - contentY()));
458 m_headerItem->setHeight(m_headerItem->implicitHeight());
459 // We are going down (but it's not because of the rebound at the end)
460 // (but the header was not shown by it's own position)
461 // or the header is partially shown and we are not doing a maximizeVisibleArea either
462 const bool scrolledUp = m_previousContentY > contentY();
463 const bool notRebounding = contentY() + height() < contentHeight();
464 const bool notShownByItsOwn = contentY() + diff > m_headerItem->y() + m_headerItem->height();
465 const bool maximizeVisibleAreaRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationMaximizeVisibleArea;
467 if (!scrolledUp && contentY() == -m_minYExtent) {
468 m_headerItemShownHeight = 0;
469 m_headerItem->setY(contentY());
470 } else if ((scrolledUp && notRebounding && notShownByItsOwn && !maximizeVisibleAreaRunning) || (m_headerItemShownHeight > 0)) {
471 if (maximizeVisibleAreaRunning && diff > 0) // If we are maximizing and the header was shown, make sure we hide it
472 m_headerItemShownHeight -= diff;
474 m_headerItemShownHeight += diff;
475 if (contentY() == -m_minYExtent) {
476 m_headerItemShownHeight = 0;
478 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
480 if (m_headerItemShownHeight > 0) {
481 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
483 m_headerItem->setY(-m_minYExtent);
487 // We will be changing the clip item, need to accomadate for it
488 // otherwise we move the firstItem down/up twice (unless the
489 // show header animation is running, where we want to keep the viewport stable)
490 if (!showHeaderAnimationRunning) {
491 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
496 if (!m_visibleItems.isEmpty()) {
498 ListItem *firstItem = m_visibleItems.first();
499 firstItem->setY(firstItem->y() + diff);
500 if (showHeaderAnimationRunning) {
505 m_previousContentY = contentY();
510void ListViewWithPageHeader::createDelegateModel()
512#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
513 m_delegateModel = new QQuickVisualDataModel(qmlContext(this), this);
514 connect(m_delegateModel, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(itemCreated(int,QQuickItem*)));
516 m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
517 connect(m_delegateModel, SIGNAL(createdItem(int,QObject*)), this, SLOT(itemCreated(int,QObject*)));
519 if (isComponentComplete())
520 m_delegateModel->componentComplete();
521 updateWatchedRoles();
524void ListViewWithPageHeader::refill()
529 if (!isComponentComplete()) {
533 const qreal buffer = height() * bufferRatio;
534 const qreal from = contentY();
535 const qreal to = from + height();
536 const qreal bufferFrom = from - buffer;
537 const qreal bufferTo = to + buffer;
539 bool added = addVisibleItems(from, to, false);
540 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
541 added |= addVisibleItems(bufferFrom, bufferTo, true);
543 if (added || removed) {
544 m_contentHeightDirty = true;
548bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
553 if (m_delegateModel->count() == 0)
557// qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
561 if (!m_visibleItems.isEmpty()) {
562 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
563 item = m_visibleItems.last();
564 pos = item->y() + item->height() + m_clipItem->y();
566 bool changed = false;
567// qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
568 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
569// qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
570 if (!(item = createItem(modelIndex, asynchronous)))
572 pos += item->height();
579 if (!m_visibleItems.isEmpty()) {
580 modelIndex = m_firstVisibleIndex - 1;
581 item = m_visibleItems.first();
582 pos = item->y() + m_clipItem->y();
584 while (modelIndex >= 0 && pos > fillFrom) {
585// qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
586 if (!(item = createItem(modelIndex, asynchronous)))
588 pos -= item->height();
596void ListViewWithPageHeader::reallyReleaseItem(ListItem *listItem)
598 QQuickItem *item = listItem->m_item;
599#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
600 QQuickVisualModel::ReleaseFlags flags = m_delegateModel->release(item);
601 if (flags & QQuickVisualModel::Destroyed) {
603 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
604 if (flags & QQmlDelegateModel::Destroyed) {
606 item->setParentItem(nullptr);
608 delete listItem->m_sectionItem;
612void ListViewWithPageHeader::releaseItem(ListItem *listItem)
614 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(listItem->m_item);
615 itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
616 m_itemsToRelease << listItem;
619void ListViewWithPageHeader::updateWatchedRoles()
621 if (m_delegateModel) {
622 QList<QByteArray> roles;
623 if (!m_sectionProperty.isEmpty())
624 roles << m_sectionProperty.toUtf8();
625 m_delegateModel->setWatchedRoles(roles);
629QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
631 if (!m_sectionDelegate)
634 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
635 if (modelIndex > 0) {
636 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
637 if (section == prevSection)
640 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
641 // Already inserted items can't steal next section header
642 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
643 if (section == nextSection) {
644 // Steal the section header
645 ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
647 QQuickItem *sectionItem = nextItem->m_sectionItem;
648 nextItem->m_sectionItem = nullptr;
654 return getSectionItem(modelIndex, section);
657QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, const QString §ionText)
659 QQuickItem *sectionItem = nullptr;
661 QQmlContext *creationContext = m_sectionDelegate->creationContext();
662 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
663 context->setContextProperty(QLatin1String("section"), sectionText);
664 context->setContextProperty(QLatin1String("delegateIndex"), modelIndex);
665 QObject *nobj = m_sectionDelegate->beginCreate(context);
667 QQml_setParent_noEvent(context, nobj);
668 sectionItem = qobject_cast<QQuickItem *>(nobj);
672 sectionItem->setZ(2);
673 QQml_setParent_noEvent(sectionItem, m_clipItem);
674 sectionItem->setParentItem(m_clipItem);
679 m_sectionDelegate->completeCreate();
681 // TODO attach to sectionItem so we can accomodate to it changing its height
686bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
688// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
689 // Do not remove items if we are overshooting up or down, since we'll come back
690 // to the "stable" position and delete/create items without any reason
691 if (contentY() < -m_minYExtent) {
693 } else if (contentY() + height() > contentHeight()) {
696 bool changed = false;
698 bool foundVisible = false;
700 int removedItems = 0;
701 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
702 while (i < m_visibleItems.count()) {
703 ListItem *item = m_visibleItems[i];
704 const qreal pos = item->y() + m_clipItem->y();
705// qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
706 if (pos + item->height() < bufferFrom || pos > bufferTo) {
707// qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
709 m_visibleItems.removeAt(i);
715 const int itemIndex = m_firstVisibleIndex + removedItems + i;
716 m_firstVisibleIndex = itemIndex;
721 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
728ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
730// qDebug() << "CREATE ITEM" << modelIndex;
731 if (asynchronous && m_asyncRequestedIndex != -1)
734 m_asyncRequestedIndex = -1;
735#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
736 QQuickItem *item = m_delegateModel->item(modelIndex, asynchronous);
738 QObject* object = m_delegateModel->object(modelIndex, asynchronous);
739 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
742#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
743 m_asyncRequestedIndex = modelIndex;
746 m_delegateModel->release(object);
747 if (!m_delegateValidated) {
748 m_delegateValidated = true;
749 QObject* delegateObj = delegate();
750 qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
753 m_asyncRequestedIndex = modelIndex;
758// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
759 ListItem *listItem = new ListItem;
760 listItem->m_item = item;
761 listItem->m_sectionItem = getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/);
762 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
763 ListItem *prevItem = itemAtIndex(modelIndex - 1);
764 bool lostItem = false; // Is an item that we requested async but because of model changes
765 // it is no longer attached to any of the existing items (has no prev nor next item)
766 // nor is the first item
768 listItem->setY(prevItem->y() + prevItem->height());
770 ListItem *currItem = itemAtIndex(modelIndex);
772 // There's something already in m_visibleItems at out index, meaning this is an insert, so attach to its top
773 listItem->setY(currItem->y() - listItem->height());
775 ListItem *nextItem = itemAtIndex(modelIndex + 1);
777 listItem->setY(nextItem->y() - listItem->height());
778 } else if (modelIndex == 0 && m_headerItem) {
779 listItem->setY(m_headerItem->height());
780 } else if (!m_visibleItems.isEmpty()) {
786 releaseItem(listItem);
789 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() <= contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
790 if (m_visibleItems.isEmpty()) {
791 m_visibleItems << listItem;
793 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
795 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
796 m_firstVisibleIndex = modelIndex;
799 if (listItem->m_sectionItem) {
800 QQmlContext *context = QQmlEngine::contextForObject(listItem->m_sectionItem)->parentContext();
801 context->setContextProperty(QLatin1String("delegateIndex"), modelIndex);
804 m_contentHeightDirty = true;
810#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
811void ListViewWithPageHeader::itemCreated(int modelIndex, QQuickItem *item)
814void ListViewWithPageHeader::itemCreated(int modelIndex, QObject *object)
816 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
818 qWarning() << "ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
822// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
824 item->setParentItem(m_clipItem);
825 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
826 context->setContextProperty(QLatin1String("ListViewWithPageHeader"), this);
827 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
828 if (modelIndex == m_asyncRequestedIndex) {
829 createItem(modelIndex, false);
834void ListViewWithPageHeader::updateClipItem()
836 m_clipItem->setHeight(height() - m_headerItemShownHeight);
837 m_clipItem->setY(contentY() + m_headerItemShownHeight);
838 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
841void ListViewWithPageHeader::onContentHeightChanged()
846void ListViewWithPageHeader::onContentWidthChanged()
848 m_clipItem->setWidth(contentItem()->width());
851void ListViewWithPageHeader::onHeightChanged()
857#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
858void ListViewWithPageHeader::onModelUpdated(const QQuickChangeSet &changeSet, bool /*reset*/)
860void ListViewWithPageHeader::onModelUpdated(const QQmlChangeSet &changeSet, bool /*reset*/)
863 // TODO Do something with reset
864// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
865 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
867#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
868 Q_FOREACH(const QQuickChangeSet::Remove &remove, changeSet.removes()) {
870 Q_FOREACH(const QQmlChangeSet::Remove &remove, changeSet.removes()) {
872// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove.index << remove.count;
873 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
874 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
875 // If all the items we are removing are either not created or culled
876 // we have to grow down to avoid viewport changing
877 bool growDown = true;
878 for (int i = 0; growDown && i < remove.count; ++i) {
879 const int modelIndex = remove.index + i;
880 ListItem *item = itemAtIndex(modelIndex);
881 if (item && !item->culled()) {
885 for (int i = remove.count - 1; i >= 0; --i) {
886 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
887 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
888 ListItem *item = m_visibleItems[visibleIndex];
889 // Pass the section item down if needed
890 if (item->m_sectionItem && visibleIndex + 1 < m_visibleItems.count()) {
891 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
892 if (!nextItem->m_sectionItem) {
893 nextItem->m_sectionItem = item->m_sectionItem;
894 item->m_sectionItem = nullptr;
898 m_visibleItems.removeAt(visibleIndex);
903 } else if (remove.index <= m_firstVisibleIndex) {
904 if (!m_visibleItems.isEmpty()) {
905 // We removed the first item that is the one that positions the rest
906 // position the new first item correctly
907 m_visibleItems.first()->setY(oldFirstValidIndexPos);
909 m_firstVisibleIndex = -1;
912 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
913 m_firstVisibleIndex -= remove.count;
915 for (int i = remove.count - 1; i >= 0; --i) {
916 const int modelIndex = remove.index + i;
917 if (modelIndex == m_asyncRequestedIndex) {
918 m_asyncRequestedIndex = -1;
919 } else if (modelIndex < m_asyncRequestedIndex) {
920 m_asyncRequestedIndex--;
925#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
926 Q_FOREACH(const QQuickChangeSet::Insert &insert, changeSet.inserts()) {
928 Q_FOREACH(const QQmlChangeSet::Insert &insert, changeSet.inserts()) {
930// qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
931 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
932 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
933 if (insertingInValidIndexes || firstItemWithViewOnTop)
935 // If the items we are adding won't be really visible
936 // we grow up instead of down to not change the viewport
938 if (!firstItemWithViewOnTop) {
939 for (int i = 0; i < m_visibleItems.count(); ++i) {
940 if (!m_visibleItems[i]->culled()) {
941 if (insert.index <= m_firstVisibleIndex + i) {
949 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
950 for (int i = insert.count - 1; i >= 0; --i) {
951 const int modelIndex = insert.index + i;
952 ListItem *item = createItem(modelIndex, false);
954 ListItem *firstItem = m_visibleItems.first();
955 firstItem->setY(firstItem->y() - item->height());
958 // Adding an item may break a "same section" chain, so check
959 // if we need adding a new section item
960 if (m_sectionDelegate) {
961 ListItem *nextItem = itemAtIndex(modelIndex + 1);
962 if (nextItem && !nextItem->m_sectionItem) {
963 nextItem->m_sectionItem = getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/);
964 if (growUp && nextItem->m_sectionItem) {
965 ListItem *firstItem = m_visibleItems.first();
966 firstItem->setY(firstItem->y() - nextItem->m_sectionItem->height());
972 if (firstItemWithViewOnTop) {
973 ListItem *firstItem = m_visibleItems.first();
974 firstItem->setY(oldFirstValidIndexPos);
976 } else if (insert.index <= m_firstVisibleIndex) {
977 m_firstVisibleIndex += insert.count;
980 for (int i = insert.count - 1; i >= 0; --i) {
981 const int modelIndex = insert.index + i;
982 if (modelIndex <= m_asyncRequestedIndex) {
983 m_asyncRequestedIndex++;
988 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
992 for (int i = 0; i < m_visibleItems.count(); ++i) {
993 ListItem *item = m_visibleItems[i];
994 if (item->m_sectionItem) {
995 QQmlContext *context = QQmlEngine::contextForObject(item->m_sectionItem)->parentContext();
996 context->setContextProperty(QLatin1String("delegateIndex"), m_firstVisibleIndex + i);
1002 m_contentHeightDirty = true;
1005void ListViewWithPageHeader::onShowHeaderAnimationFinished()
1007 m_contentHeightDirty = true;
1011void ListViewWithPageHeader::itemGeometryChanged(QQuickItem * /*item*/, const QRectF &newGeometry, const QRectF &oldGeometry)
1013 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
1014 if (heightDiff != 0) {
1015 if (oldGeometry.y() + oldGeometry.height() + m_clipItem->y() < contentY() && !m_visibleItems.isEmpty()) {
1016 ListItem *firstItem = m_visibleItems.first();
1017 firstItem->setY(firstItem->y() - heightDiff);
1024 m_contentHeightDirty = true;
1028void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
1030 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
1031 if (m_headerItemShownHeight > 0) {
1032 // If the header is shown because of the clip
1034 m_headerItemShownHeight += heightDiff;
1035 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
1039 if (oldHeaderY + oldHeaderHeight > contentY()) {
1040 // If the header is shown because its position
1042 ListItem *firstItem = m_visibleItems.first();
1043 firstItem->setY(firstItem->y() + heightDiff);
1046 // If the header is not on screen, just change the start of the list
1047 // so the viewport is not changed
1054void ListViewWithPageHeader::adjustMinYExtent()
1056 if (m_visibleItems.isEmpty()) {
1059 qreal nonCreatedHeight = 0;
1060 if (m_firstVisibleIndex != 0) {
1061 // Calculate the average height of items to estimate the position of the list start
1062 const int visibleItems = m_visibleItems.count();
1063 qreal visibleItemsHeight = 0;
1064 Q_FOREACH(ListItem *item, m_visibleItems) {
1065 visibleItemsHeight += item->height();
1067 nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
1068// qDebug() << m_firstVisibleIndex << visibleItemsHeight << visibleItems << nonCreatedHeight;
1070 m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + (m_headerItem ? m_headerItem->implicitHeight() : 0);
1074ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
1076 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
1077 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
1078 return m_visibleItems[visibleIndexedModelIndex];
1083void ListViewWithPageHeader::layout()
1089 if (!m_visibleItems.isEmpty()) {
1090 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
1091 const qreal visibleTo = contentY() + height() - m_clipItem->y();
1093 qreal pos = m_visibleItems.first()->y();
1095// qDebug() << "ListViewWithPageHeader::layout Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
1096 int firstReallyVisibleItem = -1;
1097 int modelIndex = m_firstVisibleIndex;
1098 Q_FOREACH(ListItem *item, m_visibleItems) {
1099 const bool cull = pos + item->height() <= visibleFrom || pos >= visibleTo;
1100 item->setCulled(cull);
1102 if (!cull && firstReallyVisibleItem == -1) {
1103 firstReallyVisibleItem = modelIndex;
1104 if (m_topSectionItem) {
1105 // Positing the top section sticky item is a two step process
1106 // First we set it either we cull it (because it doesn't need to be sticked to the top)
1107 // or stick it to the top
1108 // Then after the loop we'll make sure that if there's another section just below it
1109 // pushed the sticky section up to make it disappear
1110 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
1111 bool showStickySectionItem;
1112 // We need to show the "top section sticky item" when the position at the "top" of the
1113 // viewport is bigger than the start of the position of the first visible item
1114 // i.e. the first visible item starts before the viewport, or when the first
1115 // visible item starts just at the viewport start and it does not have its own section item
1116 if (topSectionStickPos > pos) {
1117 showStickySectionItem = true;
1118 } else if (topSectionStickPos == pos) {
1119 showStickySectionItem = !item->m_sectionItem;
1121 showStickySectionItem = false;
1123 if (!showStickySectionItem) {
1124 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
1125 if (item->m_sectionItem) {
1126 // This seems it should happen since why would we cull the top section
1127 // if the first visible item has no section header? This only happens briefly
1128 // when increasing the height of a list that is at the bottom, the m_topSectionItem
1129 // gets shown shortly in the next polish call
1130 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(false);
1133 // Update the top sticky section header
1134 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1135 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
1136 context->setContextProperty(QLatin1String("section"), section);
1138 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
1139 m_topSectionItem->setY(topSectionStickPos);
1140 int delegateIndex = modelIndex;
1141 // Look for the first index with this section text
1142 while (delegateIndex > 0) {
1143 const QString prevSection = m_delegateModel->stringValue(delegateIndex - 1, m_sectionProperty);
1144 if (prevSection != section)
1148 context->setContextProperty(QLatin1String("delegateIndex"), delegateIndex);
1149 if (item->m_sectionItem) {
1150 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(true);
1155 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
1156 const qreal clipFrom = visibleFrom + (!item->m_sectionItem && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
1157 if (!cull && pos < clipFrom) {
1158 context->setContextProperty(QLatin1String("heightToClip"), clipFrom - pos);
1160 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
1162// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
1163 pos += item->height();
1167 // Second step of section sticky item positioning
1168 // Look at the next section header, check if it's pushing up the sticky one
1169 if (m_topSectionItem) {
1170 if (firstReallyVisibleItem >= 0) {
1171 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
1172 ListItem *item = m_visibleItems[i];
1173 if (item->m_sectionItem) {
1174 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
1175 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
1186void ListViewWithPageHeader::updatePolish()
1188 Q_FOREACH(ListItem *item, m_itemsToRelease)
1189 reallyReleaseItem(item);
1190 m_itemsToRelease.clear();
1199 if (m_contentHeightDirty) {
1200 qreal contentHeight;
1201 if (m_visibleItems.isEmpty()) {
1202 contentHeight = m_headerItem ? m_headerItem->height() : 0;
1204 const int modelCount = model()->rowCount();
1205 const int visibleItems = m_visibleItems.count();
1206 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
1207 qreal nonCreatedHeight = 0;
1208 if (lastValidIndex != modelCount - 1) {
1209 const int visibleItems = m_visibleItems.count();
1210 qreal visibleItemsHeight = 0;
1211 Q_FOREACH(ListItem *item, m_visibleItems) {
1212 visibleItemsHeight += item->height();
1214 const int unknownSizes = modelCount - (m_firstVisibleIndex + visibleItems);
1215 nonCreatedHeight = unknownSizes * visibleItemsHeight / visibleItems;
1217 ListItem *item = m_visibleItems.last();
1218 contentHeight = nonCreatedHeight + item->y() + item->height() + m_clipItem->y();
1219 if (m_firstVisibleIndex != 0) {
1220 // Make sure that if we are shrinking we tell the view we still fit
1221 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
1225 m_contentHeightDirty = false;
1226 setContentHeight(contentHeight);
1230#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