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"
94#include <QCoreApplication>
97#include <qqmlengine.h>
98#pragma GCC diagnostic push
99#pragma GCC diagnostic ignored "-pedantic"
100#include <private/qqmldelegatemodel_p.h>
101#include <private/qqmlglobal_p.h>
102#include <private/qquickitem_p.h>
103#include <private/qquickanimation_p.h>
104#pragma GCC diagnostic pop
105// #include <private/qquickrectangle_p.h>
107qreal ListViewWithPageHeader::ListItem::height() const
109 return m_item->height() + (m_sectionItem ? m_sectionItem->height() : 0);
112qreal ListViewWithPageHeader::ListItem::y() const
114 return m_item->y() - (m_sectionItem ? m_sectionItem->height() : 0);
117void ListViewWithPageHeader::ListItem::setY(qreal newY)
120 m_sectionItem->setY(newY);
121 m_item->setY(newY + m_sectionItem->height());
127bool ListViewWithPageHeader::ListItem::culled() const
129 return QQuickItemPrivate::get(m_item)->culled;
132void ListViewWithPageHeader::ListItem::setCulled(bool culled)
134 QQuickItemPrivate::get(m_item)->setCulled(culled);
136 QQuickItemPrivate::get(m_sectionItem)->setCulled(culled);
139void ListViewWithPageHeader::ListItem::setSectionItem(QQuickItem *sectionItem)
141 m_sectionItem = sectionItem;
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)
158 , m_inContentHeightKeepHeaderShown(false)
161 m_clipItem = new QQuickItem(contentItem());
162// m_clipItem = new QQuickRectangle(contentItem());
163// ((QQuickRectangle*)m_clipItem)->setColor(Qt::gray);
165 m_contentYAnimation = new QQuickNumberAnimation(this);
166 m_contentYAnimation->setEasing(QEasingCurve::OutQuad);
167 m_contentYAnimation->setProperty("contentY");
168 m_contentYAnimation->setDuration(200);
169 m_contentYAnimation->setTargetObject(this);
171 connect(this, SIGNAL(contentWidthChanged()), this, SLOT(onContentWidthChanged()));
172 connect(this, SIGNAL(contentHeightChanged()), this, SLOT(onContentHeightChanged()));
173 connect(this, SIGNAL(heightChanged()), this, SLOT(onHeightChanged()));
174 connect(m_contentYAnimation, SIGNAL(stopped()), this, SLOT(onShowHeaderAnimationFinished()));
176 setFlickableDirection(VerticalFlick);
179ListViewWithPageHeader::~ListViewWithPageHeader()
183QAbstractItemModel *ListViewWithPageHeader::model() const
185 return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
188void ListViewWithPageHeader::setModel(QAbstractItemModel *model)
190 if (model != this->model()) {
191 if (!m_delegateModel) {
192 createDelegateModel();
194 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
196 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
197 connect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
198 Q_EMIT modelChanged();
201// Q_EMIT contentHeightChanged();
202// Q_EMIT contentYChanged();
206QQmlComponent *ListViewWithPageHeader::delegate() const
208 return m_delegateModel ? m_delegateModel->delegate() : nullptr;
211void ListViewWithPageHeader::setDelegate(QQmlComponent *delegate)
213 if (delegate != this->delegate()) {
214 if (!m_delegateModel) {
215 createDelegateModel();
218 // Cleanup the existing items
219 Q_FOREACH(ListItem *item, m_visibleItems)
221 m_visibleItems.clear();
222 m_firstVisibleIndex = -1;
226 if (m_topSectionItem) {
227 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
230 m_delegateModel->setDelegate(delegate);
232 Q_EMIT delegateChanged();
233 m_delegateValidated = false;
234 m_contentHeightDirty = true;
239QQuickItem *ListViewWithPageHeader::header() const
244void ListViewWithPageHeader::setHeader(QQuickItem *headerItem)
246 if (m_headerItem != headerItem) {
247 qreal oldHeaderHeight = 0;
248 qreal oldHeaderY = 0;
250 oldHeaderHeight = m_headerItem->height();
251 oldHeaderY = m_headerItem->y();
252 m_headerItem->setParentItem(nullptr);
254 m_headerItem = headerItem;
256 m_headerItem->setParentItem(contentItem());
257 m_headerItem->setZ(1);
258 m_previousHeaderImplicitHeight = m_headerItem->implicitHeight();
259 QQuickItemPrivate::get(m_headerItem)->addItemChangeListener(this, QQuickItemPrivate::ImplicitHeight);
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);
286 connect(m_topSectionItem, SIGNAL(heightChanged()), SIGNAL(stickyHeaderHeightChanged()));
288 // TODO create sections for existing items
290 Q_EMIT sectionDelegateChanged();
291 Q_EMIT stickyHeaderHeightChanged();
295QString ListViewWithPageHeader::sectionProperty() const
297 return m_sectionProperty;
300void ListViewWithPageHeader::setSectionProperty(const QString &property)
302 if (property != m_sectionProperty) {
303 m_sectionProperty = property;
305 updateWatchedRoles();
307 // TODO recreate sections
309 Q_EMIT sectionPropertyChanged();
313bool ListViewWithPageHeader::forceNoClip() const
315 return m_forceNoClip;
318void ListViewWithPageHeader::setForceNoClip(bool noClip)
320 if (noClip != m_forceNoClip) {
321 m_forceNoClip = noClip;
323 Q_EMIT forceNoClipChanged();
327int ListViewWithPageHeader::stickyHeaderHeight() const
329 return m_topSectionItem ? m_topSectionItem->height() : 0;
332qreal ListViewWithPageHeader::headerItemShownHeight() const
334 return m_headerItemShownHeight;
337qreal ListViewWithPageHeader::cacheBuffer() const
339 return m_cacheBuffer;
342void ListViewWithPageHeader::setCacheBuffer(qreal cacheBuffer)
344 if (cacheBuffer != m_cacheBuffer) {
345 m_cacheBuffer = cacheBuffer;
346 Q_EMIT cacheBufferChanged();
351void ListViewWithPageHeader::positionAtBeginning()
353 if (m_delegateModel->count() <= 0)
356 qreal headerHeight = (m_headerItem ? m_headerItem->height() : 0);
357 if (m_firstVisibleIndex != 0) {
358 // TODO This could be optimized by trying to reuse the interesection
359 // of items that may end up intersecting between the existing
360 // m_visibleItems and the items we are creating in the next loop
361 Q_FOREACH(ListItem *item, m_visibleItems)
363 m_visibleItems.clear();
364 m_firstVisibleIndex = -1;
366 // Create the item 0, it will be already correctly positioned at createItem()
368 ListItem *item = createItem(0, false);
369 // Create the subsequent items
371 qreal pos = item->y() + item->height();
372 const qreal bufferTo = height() + m_cacheBuffer;
373 while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
374 if (!(item = createItem(modelIndex, false)))
376 pos += item->height();
380 m_previousContentY = m_visibleItems.first()->y() - headerHeight;
382 setContentY(m_visibleItems.first()->y() + m_clipItem->y() - headerHeight);
384 // TODO This should not be needed and the code that adjust the m_headerItem position
385 // in viewportMoved() should be enough but in some cases we have not found a way to reproduce
386 // yet the code of viewportMoved() fails so here we make sure that at least if we are calling
387 // positionAtBeginning the header item will be correctly positioned
388 m_headerItem->setY(-m_minYExtent);
392static inline bool uFuzzyCompare(qreal r1, qreal r2)
394 return qFuzzyCompare(r1, r2) || (qFuzzyIsNull(r1) && qFuzzyIsNull(r2));
397void ListViewWithPageHeader::showHeader()
402 const auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
403 if (!uFuzzyCompare(to, contentY())) {
404 const bool headerShownByItsOwn = contentY() < m_headerItem->y() + m_headerItem->height();
405 if (headerShownByItsOwn && m_headerItemShownHeight == 0) {
406 // We are not clipping since we are just at the top of the viewport
407 // but because of the showHeader animation we will need to, so
408 // enable the clipping without logically moving the items
409 m_headerItemShownHeight = m_headerItem->y() + m_headerItem->height() - contentY();
410 if (!m_visibleItems.isEmpty()) {
412 ListItem *firstItem = m_visibleItems.first();
413 firstItem->setY(firstItem->y() - m_headerItemShownHeight);
416 Q_EMIT headerItemShownHeightChanged();
418 m_contentYAnimation->setTo(to);
419 contentYAnimationType = ContentYAnimationShowHeader;
420 m_contentYAnimation->start();
424QQuickItem *ListViewWithPageHeader::item(int modelIndex) const
426 ListItem *item = itemAtIndex(modelIndex);
433bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex)
435 ListItem *listItem = itemAtIndex(modelIndex);
437 return maximizeVisibleArea(listItem, listItem->height());
443bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex, int itemHeight)
448 ListItem *listItem = itemAtIndex(modelIndex);
450 return maximizeVisibleArea(listItem, itemHeight + (listItem->sectionItem() ? listItem->sectionItem()->height() : 0));
456bool ListViewWithPageHeader::maximizeVisibleArea(ListItem *listItem, int listItemHeight)
460 const auto listItemY = m_clipItem->y() + listItem->y();
461 if (listItemY > contentY() && listItemY + listItemHeight > contentY() + height()) {
462 // we can scroll the list up to show more stuff
463 const auto to = qMin(listItemY, listItemY + listItemHeight - height());
464 m_contentYAnimation->setTo(to);
465 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
466 m_contentYAnimation->start();
467 } else if ((listItemY < contentY() && listItemY + listItemHeight < contentY() + height()) ||
468 (m_topSectionItem && !listItem->sectionItem() && listItemY - m_topSectionItem->height() < contentY() && listItemY + listItemHeight < contentY() + height()))
470 // we can scroll the list down to show more stuff
471 auto realVisibleListItemY = listItemY;
472 if (m_topSectionItem) {
473 // If we are showing the top section sticky item and this item doesn't have a section
474 // item we have to make sure to scroll it a bit more so that it is not underlapping
475 // the top section sticky item
476 bool topSectionShown = !QQuickItemPrivate::get(m_topSectionItem)->culled;
477 if (topSectionShown && !listItem->sectionItem()) {
478 realVisibleListItemY -= m_topSectionItem->height();
481 const auto to = qMax(realVisibleListItemY, listItemY + listItemHeight - height());
482 m_contentYAnimation->setTo(to);
483 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
484 m_contentYAnimation->start();
491qreal ListViewWithPageHeader::minYExtent() const
493// qDebug() << "ListViewWithPageHeader::minYExtent" << m_minYExtent;
497void ListViewWithPageHeader::componentComplete()
500 m_delegateModel->componentComplete();
502 QQuickFlickable::componentComplete();
507void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
509 // Check we are not being taken down and don't paint anything
510 // TODO Check if we still need this in 5.2
511 // For reproduction just inifnite loop testDash or testDashContent
512 if (!QQmlEngine::contextForObject(this)->parentContext())
515 QQuickFlickable::viewportMoved(orient);
516// qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
517 const qreal diff = m_previousContentY - contentY();
519 m_previousContentY = contentY();
524void ListViewWithPageHeader::adjustHeader(qreal diff)
526 const bool showHeaderAnimationRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationShowHeader;
528 const auto oldHeaderItemShownHeight = m_headerItemShownHeight;
529 if (uFuzzyCompare(contentY(), -m_minYExtent) || contentY() > -m_minYExtent) {
530 m_headerItem->setHeight(m_headerItem->implicitHeight());
531 // We are going down (but it's not because of the rebound at the end)
532 // (but the header was not shown by it's own position)
533 // or the header is partially shown and we are not doing a maximizeVisibleArea either
534 const bool scrolledUp = m_previousContentY > contentY();
535 const bool notRebounding = contentY() + height() < contentHeight();
536 const bool notShownByItsOwn = contentY() + diff >= m_headerItem->y() + m_headerItem->height();
537 const bool maximizeVisibleAreaRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationMaximizeVisibleArea;
539 if (!scrolledUp && (contentY() == -m_minYExtent || (m_headerItemShownHeight == 0 && m_previousContentY == m_headerItem->y()))) {
540 m_headerItemShownHeight = 0;
541 m_headerItem->setY(-m_minYExtent);
542 } else if ((scrolledUp && notRebounding && notShownByItsOwn && !maximizeVisibleAreaRunning) || (m_headerItemShownHeight > 0) || m_inContentHeightKeepHeaderShown) {
543 if (maximizeVisibleAreaRunning && diff > 0) {
544 // If we are maximizing and the header was shown, make sure we hide it
545 m_headerItemShownHeight -= diff;
547 m_headerItemShownHeight += diff;
549 if (uFuzzyCompare(contentY(), -m_minYExtent)) {
550 m_headerItemShownHeight = 0;
552 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
554 if (m_headerItemShownHeight > 0) {
555 if (uFuzzyCompare(m_headerItem->height(), m_headerItemShownHeight)) {
556 m_headerItem->setY(contentY());
557 m_headerItemShownHeight = m_headerItem->height();
559 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
562 m_headerItem->setY(-m_minYExtent);
565 Q_EMIT headerItemShownHeightChanged();
567 // Stick the header item to the top when dragging down
568 m_headerItem->setY(contentY());
569 m_headerItem->setHeight(m_headerItem->implicitHeight() + (-m_minYExtent - contentY()));
571 // We will be changing the clip item, need to accomadate for it
572 // otherwise we move the firstItem down/up twice (unless the
573 // show header animation is running, where we want to keep the viewport stable)
574 if (!showHeaderAnimationRunning) {
575 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
580 if (!m_visibleItems.isEmpty()) {
582 ListItem *firstItem = m_visibleItems.first();
583 firstItem->setY(firstItem->y() + diff);
584 if (showHeaderAnimationRunning) {
590void ListViewWithPageHeader::createDelegateModel()
592 m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
593 connect(m_delegateModel, SIGNAL(createdItem(int,QObject*)), this, SLOT(itemCreated(int,QObject*)));
594 if (isComponentComplete())
595 m_delegateModel->componentComplete();
596 updateWatchedRoles();
599void ListViewWithPageHeader::refill()
604 if (!isComponentComplete()) {
608 const qreal from = contentY();
609 const qreal to = from + height();
610 const qreal bufferFrom = from - m_cacheBuffer;
611 const qreal bufferTo = to + m_cacheBuffer;
613 bool added = addVisibleItems(from, to, false);
614 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
615 added |= addVisibleItems(bufferFrom, bufferTo, true);
617 if (added || removed) {
618 m_contentHeightDirty = true;
622bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
627 if (m_delegateModel->count() == 0)
631// qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
635 if (!m_visibleItems.isEmpty()) {
636 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
637 item = m_visibleItems.last();
638 pos = item->y() + item->height() + m_clipItem->y();
640 bool changed = false;
641// qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
642 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
643// qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
644 if (!(item = createItem(modelIndex, asynchronous)))
646 pos += item->height();
653 if (!m_visibleItems.isEmpty()) {
654 modelIndex = m_firstVisibleIndex - 1;
655 item = m_visibleItems.first();
656 pos = item->y() + m_clipItem->y();
658 while (modelIndex >= 0 && pos > fillFrom) {
659// qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
660 if (!(item = createItem(modelIndex, asynchronous)))
662 pos -= item->height();
670void ListViewWithPageHeader::reallyReleaseItem(ListItem *listItem)
672 QQuickItem *item = listItem->m_item;
673 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
674 if (flags & QQmlDelegateModel::Destroyed) {
675 item->setParentItem(nullptr);
677 listItem->sectionItem()->deleteLater();
681void ListViewWithPageHeader::releaseItem(ListItem *listItem)
683 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(listItem->m_item);
684 itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
685 m_itemsToRelease << listItem;
688void ListViewWithPageHeader::updateWatchedRoles()
690 if (m_delegateModel) {
691 QList<QByteArray> roles;
692 if (!m_sectionProperty.isEmpty())
693 roles << m_sectionProperty.toUtf8();
694 m_delegateModel->setWatchedRoles(roles);
698QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
700 if (!m_sectionDelegate)
703 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
705 if (modelIndex > 0) {
706 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
707 if (section == prevSection)
710 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
711 // Already inserted items can't steal next section header
712 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
713 if (section == nextSection) {
714 // Steal the section header
715 ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
717 QQuickItem *sectionItem = nextItem->sectionItem();
718 nextItem->setSectionItem(nullptr);
724 return getSectionItem(section);
727QQuickItem *ListViewWithPageHeader::getSectionItem(const QString §ionText)
729 QQuickItem *sectionItem = nullptr;
731 QQmlContext *creationContext = m_sectionDelegate->creationContext();
732 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
733 context->setContextProperty(QLatin1String("section"), sectionText);
734 context->setContextProperty(QLatin1String("delegateIndex"), -1);
735 QObject *nobj = m_sectionDelegate->beginCreate(context);
737 QQml_setParent_noEvent(context, nobj);
738 sectionItem = qobject_cast<QQuickItem *>(nobj);
742 sectionItem->setZ(2);
743 QQml_setParent_noEvent(sectionItem, m_clipItem);
744 sectionItem->setParentItem(m_clipItem);
749 m_sectionDelegate->completeCreate();
751 // TODO attach to sectionItem so we can accomodate to it changing its height
756bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
758// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
759 // Do not remove items if we are overshooting up or down, since we'll come back
760 // to the "stable" position and delete/create items without any reason
761 if (contentY() < -m_minYExtent) {
763 } else if (contentY() + height() > contentHeight()) {
766 bool changed = false;
768 bool foundVisible = false;
770 int removedItems = 0;
771 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
772 while (i < m_visibleItems.count()) {
773 ListItem *item = m_visibleItems[i];
774 const qreal pos = item->y() + m_clipItem->y();
775// qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
776 if (pos + item->height() < bufferFrom || pos > bufferTo) {
777// qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
779 m_visibleItems.removeAt(i);
785 const int itemIndex = m_firstVisibleIndex + removedItems + i;
786 m_firstVisibleIndex = itemIndex;
792 m_firstVisibleIndex = -1;
794 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
801ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
803// qDebug() << "CREATE ITEM" << modelIndex;
804 if (asynchronous && m_asyncRequestedIndex != -1)
807 m_asyncRequestedIndex = -1;
808 QObject* object = m_delegateModel->object(modelIndex, asynchronous);
809 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
812 m_delegateModel->release(object);
813 if (!m_delegateValidated) {
814 m_delegateValidated = true;
815 QObject* delegateObj = delegate();
816 qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
819 m_asyncRequestedIndex = modelIndex;
823// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
824 ListItem *listItem = new ListItem;
825 listItem->m_item = item;
826 listItem->setSectionItem(getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/));
827 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
828 ListItem *prevItem = itemAtIndex(modelIndex - 1);
829 bool lostItem = false; // Is an item that we requested async but because of model changes
830 // it is no longer attached to any of the existing items (has no prev nor next item)
831 // nor is the first item
833 listItem->setY(prevItem->y() + prevItem->height());
835 ListItem *currItem = itemAtIndex(modelIndex);
837 // There's something already in m_visibleItems at out index, meaning this is an insert, so attach to its top
838 listItem->setY(currItem->y() - listItem->height());
840 ListItem *nextItem = itemAtIndex(modelIndex + 1);
842 listItem->setY(nextItem->y() - listItem->height());
843 } else if (modelIndex == 0) {
844 listItem->setY(m_headerItem ? m_headerItem->height() : 0);
845 } else if (!m_visibleItems.isEmpty()) {
851 listItem->setCulled(true);
852 releaseItem(listItem);
855 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() <= contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
856 if (m_visibleItems.isEmpty()) {
857 m_visibleItems << listItem;
859 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
861 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
862 m_firstVisibleIndex = modelIndex;
865 if (listItem->sectionItem()) {
866 QQmlContext *context = QQmlEngine::contextForObject(listItem->sectionItem())->parentContext();
867 context->setContextProperty(QLatin1String("delegateIndex"), modelIndex);
870 m_contentHeightDirty = true;
876void ListViewWithPageHeader::itemCreated(int modelIndex, QObject *object)
878 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
880 qWarning() << "ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
883// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
884 // Check we are not being taken down and don't paint anything
885 // TODO Check if we still need this in 5.2
886 // For reproduction just inifnite loop testDash or testDashContent
887 if (!QQmlEngine::contextForObject(this)->parentContext())
890 item->setParentItem(m_clipItem);
891 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
892 context->setContextProperty(QLatin1String("ListViewWithPageHeader"), this);
893 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
894 if (modelIndex == m_asyncRequestedIndex) {
895 createItem(modelIndex, false);
900void ListViewWithPageHeader::updateClipItem()
902 m_clipItem->setHeight(height() - m_headerItemShownHeight);
903 m_clipItem->setY(contentY() + m_headerItemShownHeight);
904 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
907void ListViewWithPageHeader::onContentHeightChanged()
912void ListViewWithPageHeader::onContentWidthChanged()
914 m_clipItem->setWidth(contentItem()->width());
917void ListViewWithPageHeader::onHeightChanged()
923void ListViewWithPageHeader::onModelUpdated(const QQmlChangeSet &changeSet, bool /*reset*/)
925 // TODO Do something with reset
926// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
927 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
929 Q_FOREACH(const QQmlChangeSet::Remove &remove, changeSet.removes()) {
930// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove.index << remove.count;
931 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
932 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
933 // If all the items we are removing are either not created or culled
934 // we have to grow down to avoid viewport changing
935 bool growDown = true;
936 for (int i = 0; growDown && i < remove.count; ++i) {
937 const int modelIndex = remove.index + i;
938 ListItem *item = itemAtIndex(modelIndex);
939 if (item && !item->culled()) {
943 for (int i = remove.count - 1; i >= 0; --i) {
944 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
945 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
946 ListItem *item = m_visibleItems[visibleIndex];
947 // Pass the section item down if needed
948 if (item->sectionItem() && visibleIndex + 1 < m_visibleItems.count()) {
949 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
950 if (!nextItem->sectionItem()) {
951 nextItem->setSectionItem(item->sectionItem());
952 item->setSectionItem(nullptr);
956 m_visibleItems.removeAt(visibleIndex);
961 } else if (remove.index <= m_firstVisibleIndex) {
962 if (!m_visibleItems.isEmpty()) {
963 // We removed the first item that is the one that positions the rest
964 // position the new first item correctly
965 m_visibleItems.first()->setY(oldFirstValidIndexPos);
967 m_firstVisibleIndex = -1;
970 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
971 m_firstVisibleIndex -= remove.count;
973 for (int i = remove.count - 1; i >= 0; --i) {
974 const int modelIndex = remove.index + i;
975 if (modelIndex == m_asyncRequestedIndex) {
976 m_asyncRequestedIndex = -1;
977 } else if (modelIndex < m_asyncRequestedIndex) {
978 m_asyncRequestedIndex--;
983 Q_FOREACH(const QQmlChangeSet::Insert &insert, changeSet.inserts()) {
984// qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
985 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
986 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
987 if (insertingInValidIndexes || firstItemWithViewOnTop)
989 // If the items we are adding won't be really visible
990 // we grow up instead of down to not change the viewport
992 if (!firstItemWithViewOnTop) {
993 for (int i = 0; i < m_visibleItems.count(); ++i) {
994 if (!m_visibleItems[i]->culled()) {
995 if (insert.index <= m_firstVisibleIndex + i) {
1003 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
1004 for (int i = insert.count - 1; i >= 0; --i) {
1005 const int modelIndex = insert.index + i;
1006 ListItem *item = createItem(modelIndex, false);
1008 ListItem *firstItem = m_visibleItems.first();
1009 firstItem->setY(firstItem->y() - item->height());
1011 // Adding an item may break a "same section" chain, so check
1012 // if we need adding a new section item
1013 if (m_sectionDelegate) {
1014 ListItem *nextItem = itemAtIndex(modelIndex + 1);
1015 if (nextItem && !nextItem->sectionItem()) {
1016 nextItem->setSectionItem(getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/));
1017 if (growUp && nextItem->sectionItem()) {
1018 ListItem *firstItem = m_visibleItems.first();
1019 firstItem->setY(firstItem->y() - nextItem->sectionItem()->height());
1024 if (firstItemWithViewOnTop) {
1025 ListItem *firstItem = m_visibleItems.first();
1026 firstItem->setY(oldFirstValidIndexPos);
1029 } else if (insert.index <= m_firstVisibleIndex) {
1030 m_firstVisibleIndex += insert.count;
1033 for (int i = insert.count - 1; i >= 0; --i) {
1034 const int modelIndex = insert.index + i;
1035 if (modelIndex <= m_asyncRequestedIndex) {
1036 m_asyncRequestedIndex++;
1041 Q_FOREACH(const QQmlChangeSet::Change &change, changeSet.changes()) {
1042// qDebug() << "ListViewWithPageHeader::onModelUpdated Change" << change.index << change.count;
1043 for (int i = change.index; i < change.count; ++i) {
1044 ListItem *item = itemAtIndex(i);
1045 if (item && item->sectionItem()) {
1046 QQmlContext *context = QQmlEngine::contextForObject(item->sectionItem())->parentContext();
1047 const QString sectionText = m_delegateModel->stringValue(i, m_sectionProperty);
1048 context->setContextProperty(QLatin1String("section"), sectionText);
1053 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
1057 for (int i = 0; i < m_visibleItems.count(); ++i) {
1058 ListItem *item = m_visibleItems[i];
1059 if (item->sectionItem()) {
1060 QQmlContext *context = QQmlEngine::contextForObject(item->sectionItem())->parentContext();
1061 context->setContextProperty(QLatin1String("delegateIndex"), m_firstVisibleIndex + i);
1067 m_contentHeightDirty = true;
1070void ListViewWithPageHeader::onShowHeaderAnimationFinished()
1072 m_contentHeightDirty = true;
1076void ListViewWithPageHeader::itemGeometryChanged(QQuickItem * /*item*/, const QRectF &newGeometry, const QRectF &oldGeometry)
1078 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
1079 if (heightDiff != 0) {
1080 if (!m_inContentHeightKeepHeaderShown && oldGeometry.y() + oldGeometry.height() + m_clipItem->y() <= contentY() && !m_visibleItems.isEmpty()) {
1081 ListItem *firstItem = m_visibleItems.first();
1082 firstItem->setY(firstItem->y() - heightDiff);
1089 m_contentHeightDirty = true;
1093void ListViewWithPageHeader::itemImplicitHeightChanged(QQuickItem *item)
1095 if (item == m_headerItem) {
1096 const qreal diff = m_headerItem->implicitHeight() - m_previousHeaderImplicitHeight;
1099 m_previousHeaderImplicitHeight = m_headerItem->implicitHeight();
1102 m_contentHeightDirty = true;
1107void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
1109 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
1110 if (m_headerItemShownHeight > 0) {
1111 // If the header is shown because of the clip
1113 m_headerItemShownHeight += heightDiff;
1114 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
1117 Q_EMIT headerItemShownHeightChanged();
1119 if (oldHeaderY + oldHeaderHeight > contentY()) {
1120 // If the header is shown because its position
1122 ListItem *firstItem = m_visibleItems.first();
1123 firstItem->setY(firstItem->y() + heightDiff);
1126 // If the header is not on screen, just change the start of the list
1127 // so the viewport is not changed
1134void ListViewWithPageHeader::adjustMinYExtent()
1136 if (m_visibleItems.isEmpty()) {
1139 qreal nonCreatedHeight = 0;
1140 if (m_firstVisibleIndex != 0) {
1141 // Calculate the average height of items to estimate the position of the list start
1142 const int visibleItems = m_visibleItems.count();
1143 qreal visibleItemsHeight = 0;
1144 Q_FOREACH(ListItem *item, m_visibleItems) {
1145 visibleItemsHeight += item->height();
1147 nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
1148// qDebug() << m_firstVisibleIndex << visibleItemsHeight << visibleItems << nonCreatedHeight;
1150 const qreal headerHeight = (m_headerItem ? m_headerItem->implicitHeight() : 0);
1151 m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + headerHeight;
1152 if (m_minYExtent != 0 && qFuzzyIsNull(m_minYExtent)) {
1154 m_visibleItems.first()->setY(nonCreatedHeight - m_clipItem->y() + headerHeight);
1159ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
1161 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
1162 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
1163 return m_visibleItems[visibleIndexedModelIndex];
1168void ListViewWithPageHeader::layout()
1174 if (!m_visibleItems.isEmpty()) {
1175 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
1176 const qreal visibleTo = contentY() + height() - m_clipItem->y();
1178 qreal pos = m_visibleItems.first()->y();
1180// qDebug() << "ListViewWithPageHeader::layout Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
1181 int firstReallyVisibleItem = -1;
1182 int modelIndex = m_firstVisibleIndex;
1183 Q_FOREACH(ListItem *item, m_visibleItems) {
1184 const bool cull = pos + item->height() <= visibleFrom || pos >= visibleTo;
1185 item->setCulled(cull);
1187 if (!cull && firstReallyVisibleItem == -1) {
1188 firstReallyVisibleItem = modelIndex;
1189 if (m_topSectionItem) {
1190 // Positing the top section sticky item is a two step process
1191 // First we set it either we cull it (because it doesn't need to be sticked to the top)
1192 // or stick it to the top
1193 // Then after the loop we'll make sure that if there's another section just below it
1194 // pushed the sticky section up to make it disappear
1195 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
1196 bool showStickySectionItem;
1197 // We need to show the "top section sticky item" when the position at the "top" of the
1198 // viewport is bigger than the start of the position of the first visible item
1199 // i.e. the first visible item starts before the viewport, or when the first
1200 // visible item starts just at the viewport start and it does not have its own section item
1201 if (topSectionStickPos > pos) {
1202 showStickySectionItem = true;
1203 } else if (topSectionStickPos == pos) {
1204 showStickySectionItem = !item->sectionItem();
1206 showStickySectionItem = false;
1208 if (!showStickySectionItem) {
1209 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
1210 if (item->sectionItem()) {
1211 // This seems it should happen since why would we cull the top section
1212 // if the first visible item has no section header? This only happens briefly
1213 // when increasing the height of a list that is at the bottom, the m_topSectionItem
1214 // gets shown shortly in the next polish call
1215 QQuickItemPrivate::get(item->sectionItem())->setCulled(false);
1218 // Update the top sticky section header
1219 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1220 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
1221 context->setContextProperty(QLatin1String("section"), section);
1223 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
1224 m_topSectionItem->setY(topSectionStickPos);
1225 int delegateIndex = modelIndex;
1226 // Look for the first index with this section text
1227 while (delegateIndex > 0) {
1228 const QString prevSection = m_delegateModel->stringValue(delegateIndex - 1, m_sectionProperty);
1229 if (prevSection != section)
1233 context->setContextProperty(QLatin1String("delegateIndex"), delegateIndex);
1234 if (item->sectionItem()) {
1235 QQuickItemPrivate::get(item->sectionItem())->setCulled(true);
1240 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
1241 const qreal clipFrom = visibleFrom + (!item->sectionItem() && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
1242 if (!cull && pos < clipFrom) {
1243 context->setContextProperty(QLatin1String("heightToClip"), clipFrom - pos);
1245 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
1247// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
1248 pos += item->height();
1252 // Second step of section sticky item positioning
1253 // Look at the next section header, check if it's pushing up the sticky one
1254 if (m_topSectionItem) {
1255 if (firstReallyVisibleItem >= 0) {
1256 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
1257 ListItem *item = m_visibleItems[i];
1258 if (item->sectionItem()) {
1259 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
1260 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
1271void ListViewWithPageHeader::updatePolish()
1273 // Check we are not being taken down and don't paint anything
1274 // TODO Check if we still need this in 5.2
1275 // For reproduction just inifnite loop testDash or testDashContent
1276 if (!QQmlEngine::contextForObject(this)->parentContext())
1279 Q_FOREACH(ListItem *item, m_itemsToRelease)
1280 reallyReleaseItem(item);
1281 m_itemsToRelease.clear();
1290 if (m_contentHeightDirty) {
1291 qreal contentHeight;
1292 if (m_visibleItems.isEmpty()) {
1293 contentHeight = m_headerItem ? m_headerItem->height() : 0;
1295 const int modelCount = model()->rowCount();
1296 const int visibleItems = m_visibleItems.count();
1297 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
1298 qreal nonCreatedHeight = 0;
1299 if (lastValidIndex != modelCount - 1) {
1300 const int visibleItems = m_visibleItems.count();
1301 qreal visibleItemsHeight = 0;
1302 Q_FOREACH(ListItem *item, m_visibleItems) {
1303 visibleItemsHeight += item->height();
1305 const int unknownSizes = modelCount - (m_firstVisibleIndex + visibleItems);
1306 nonCreatedHeight = unknownSizes * visibleItemsHeight / visibleItems;
1308 ListItem *item = m_visibleItems.last();
1309 contentHeight = nonCreatedHeight + item->y() + item->height() + m_clipItem->y();
1310 if (m_firstVisibleIndex != 0) {
1311 // Make sure that if we are shrinking we tell the view we still fit
1312 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
1316 m_contentHeightDirty = false;
1318 m_inContentHeightKeepHeaderShown = m_headerItem && m_headerItem->y() == contentY();
1319 setContentHeight(contentHeight);
1320 m_inContentHeightKeepHeaderShown = false;
1324#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