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>
107static const qreal bufferRatio = 0.5;
109qreal ListViewWithPageHeader::ListItem::height() const
111 return m_item->height() + (m_sectionItem ? m_sectionItem->height() : 0);
114qreal ListViewWithPageHeader::ListItem::y() const
116 return m_item->y() - (m_sectionItem ? m_sectionItem->height() : 0);
119void ListViewWithPageHeader::ListItem::setY(qreal newY)
122 m_sectionItem->setY(newY);
123 m_item->setY(newY + m_sectionItem->height());
129bool ListViewWithPageHeader::ListItem::culled() const
131 return QQuickItemPrivate::get(m_item)->culled;
134void ListViewWithPageHeader::ListItem::setCulled(bool culled)
136 QQuickItemPrivate::get(m_item)->setCulled(culled);
138 QQuickItemPrivate::get(m_sectionItem)->setCulled(culled);
141void ListViewWithPageHeader::ListItem::setSectionItem(QQuickItem *sectionItem)
143 m_sectionItem = sectionItem;
146ListViewWithPageHeader::ListViewWithPageHeader()
147 : m_delegateModel(nullptr)
148 , m_asyncRequestedIndex(-1)
149 , m_delegateValidated(false)
150 , m_firstVisibleIndex(-1)
152 , m_contentHeightDirty(false)
153 , m_headerItem(nullptr)
154 , m_previousContentY(0)
155 , m_headerItemShownHeight(0)
156 , m_sectionDelegate(nullptr)
157 , m_topSectionItem(nullptr)
158 , m_forceNoClip(false)
160 , m_inContentHeightKeepHeaderShown(false)
162 m_clipItem = new QQuickItem(contentItem());
163// m_clipItem = new QQuickRectangle(contentItem());
164// ((QQuickRectangle*)m_clipItem)->setColor(Qt::gray);
166 m_contentYAnimation = new QQuickNumberAnimation(this);
167 m_contentYAnimation->setEasing(QEasingCurve::OutQuad);
168 m_contentYAnimation->setProperty("contentY");
169 m_contentYAnimation->setDuration(200);
170 m_contentYAnimation->setTargetObject(this);
172 connect(this, SIGNAL(contentWidthChanged()), this, SLOT(onContentWidthChanged()));
173 connect(this, SIGNAL(contentHeightChanged()), this, SLOT(onContentHeightChanged()));
174 connect(this, SIGNAL(heightChanged()), this, SLOT(onHeightChanged()));
175 connect(m_contentYAnimation, SIGNAL(stopped()), this, SLOT(onShowHeaderAnimationFinished()));
177 setFlickableDirection(VerticalFlick);
180ListViewWithPageHeader::~ListViewWithPageHeader()
184QAbstractItemModel *ListViewWithPageHeader::model() const
186 return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
189void ListViewWithPageHeader::setModel(QAbstractItemModel *model)
191 if (model != this->model()) {
192 if (!m_delegateModel) {
193 createDelegateModel();
195 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
197 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
198 connect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
199 Q_EMIT modelChanged();
202// Q_EMIT contentHeightChanged();
203// Q_EMIT contentYChanged();
207QQmlComponent *ListViewWithPageHeader::delegate() const
209 return m_delegateModel ? m_delegateModel->delegate() : nullptr;
212void ListViewWithPageHeader::setDelegate(QQmlComponent *delegate)
214 if (delegate != this->delegate()) {
215 if (!m_delegateModel) {
216 createDelegateModel();
219 // Cleanup the existing items
220 Q_FOREACH(ListItem *item, m_visibleItems)
222 m_visibleItems.clear();
223 m_firstVisibleIndex = -1;
227 if (m_topSectionItem) {
228 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
231 m_delegateModel->setDelegate(delegate);
233 Q_EMIT delegateChanged();
234 m_delegateValidated = false;
235 m_contentHeightDirty = true;
240QQuickItem *ListViewWithPageHeader::header() const
245void ListViewWithPageHeader::setHeader(QQuickItem *headerItem)
247 if (m_headerItem != headerItem) {
248 qreal oldHeaderHeight = 0;
249 qreal oldHeaderY = 0;
251 oldHeaderHeight = m_headerItem->height();
252 oldHeaderY = m_headerItem->y();
253 m_headerItem->setParentItem(nullptr);
255 m_headerItem = headerItem;
257 m_headerItem->setParentItem(contentItem());
258 m_headerItem->setZ(1);
259 m_previousHeaderImplicitHeight = m_headerItem->implicitHeight();
260 QQuickItemPrivate::get(m_headerItem)->addItemChangeListener(this, QQuickItemPrivate::ImplicitHeight);
262 qreal newHeaderHeight = m_headerItem ? m_headerItem->height() : 0;
263 if (!m_visibleItems.isEmpty() && newHeaderHeight != oldHeaderHeight) {
264 headerHeightChanged(newHeaderHeight, oldHeaderHeight, oldHeaderY);
266 m_contentHeightDirty = true;
268 Q_EMIT headerChanged();
272QQmlComponent *ListViewWithPageHeader::sectionDelegate() const
274 return m_sectionDelegate;
277void ListViewWithPageHeader::setSectionDelegate(QQmlComponent *delegate)
279 if (delegate != m_sectionDelegate) {
280 // TODO clean existing sections
282 m_sectionDelegate = delegate;
284 m_topSectionItem = getSectionItem(QString());
285 m_topSectionItem->setZ(3);
286 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
287 connect(m_topSectionItem, SIGNAL(heightChanged()), SIGNAL(stickyHeaderHeightChanged()));
289 // TODO create sections for existing items
291 Q_EMIT sectionDelegateChanged();
292 Q_EMIT stickyHeaderHeightChanged();
296QString ListViewWithPageHeader::sectionProperty() const
298 return m_sectionProperty;
301void ListViewWithPageHeader::setSectionProperty(const QString &property)
303 if (property != m_sectionProperty) {
304 m_sectionProperty = property;
306 updateWatchedRoles();
308 // TODO recreate sections
310 Q_EMIT sectionPropertyChanged();
314bool ListViewWithPageHeader::forceNoClip() const
316 return m_forceNoClip;
319void ListViewWithPageHeader::setForceNoClip(bool noClip)
321 if (noClip != m_forceNoClip) {
322 m_forceNoClip = noClip;
324 Q_EMIT forceNoClipChanged();
328int ListViewWithPageHeader::stickyHeaderHeight() const
330 return m_topSectionItem ? m_topSectionItem->height() : 0;
333qreal ListViewWithPageHeader::headerItemShownHeight() const
335 return m_headerItemShownHeight;
338void ListViewWithPageHeader::positionAtBeginning()
340 if (m_delegateModel->count() <= 0)
343 qreal headerHeight = (m_headerItem ? m_headerItem->height() : 0);
344 if (m_firstVisibleIndex != 0) {
345 // TODO This could be optimized by trying to reuse the interesection
346 // of items that may end up intersecting between the existing
347 // m_visibleItems and the items we are creating in the next loop
348 Q_FOREACH(ListItem *item, m_visibleItems)
350 m_visibleItems.clear();
351 m_firstVisibleIndex = -1;
353 // Create the item 0, it will be already correctly positioned at createItem()
355 ListItem *item = createItem(0, false);
356 // Create the subsequent items
358 qreal pos = item->y() + item->height();
359 const qreal buffer = height() * bufferRatio;
360 const qreal bufferTo = height() + buffer;
361 while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
362 if (!(item = createItem(modelIndex, false)))
364 pos += item->height();
368 m_previousContentY = m_visibleItems.first()->y() - headerHeight;
370 setContentY(m_visibleItems.first()->y() + m_clipItem->y() - headerHeight);
372 // TODO This should not be needed and the code that adjust the m_headerItem position
373 // in viewportMoved() should be enough but in some cases we have not found a way to reproduce
374 // yet the code of viewportMoved() fails so here we make sure that at least if we are calling
375 // positionAtBeginning the header item will be correctly positioned
376 m_headerItem->setY(-m_minYExtent);
380static inline bool uFuzzyCompare(qreal r1, qreal r2)
382 return qFuzzyCompare(r1, r2) || (qFuzzyIsNull(r1) && qFuzzyIsNull(r2));
385void ListViewWithPageHeader::showHeader()
390 const auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
391 if (!uFuzzyCompare(to, contentY())) {
392 const bool headerShownByItsOwn = contentY() < m_headerItem->y() + m_headerItem->height();
393 if (headerShownByItsOwn && m_headerItemShownHeight == 0) {
394 // We are not clipping since we are just at the top of the viewport
395 // but because of the showHeader animation we will need to, so
396 // enable the clipping without logically moving the items
397 m_headerItemShownHeight = m_headerItem->y() + m_headerItem->height() - contentY();
398 if (!m_visibleItems.isEmpty()) {
400 ListItem *firstItem = m_visibleItems.first();
401 firstItem->setY(firstItem->y() - m_headerItemShownHeight);
404 Q_EMIT headerItemShownHeightChanged();
406 m_contentYAnimation->setTo(to);
407 contentYAnimationType = ContentYAnimationShowHeader;
408 m_contentYAnimation->start();
412QQuickItem *ListViewWithPageHeader::item(int modelIndex) const
414 ListItem *item = itemAtIndex(modelIndex);
421bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex)
423 ListItem *listItem = itemAtIndex(modelIndex);
425 return maximizeVisibleArea(listItem, listItem->height());
431bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex, int itemHeight)
436 ListItem *listItem = itemAtIndex(modelIndex);
438 return maximizeVisibleArea(listItem, itemHeight + (listItem->sectionItem() ? listItem->sectionItem()->height() : 0));
444bool ListViewWithPageHeader::maximizeVisibleArea(ListItem *listItem, int listItemHeight)
448 const auto listItemY = m_clipItem->y() + listItem->y();
449 if (listItemY > contentY() && listItemY + listItemHeight > contentY() + height()) {
450 // we can scroll the list up to show more stuff
451 const auto to = qMin(listItemY, listItemY + listItemHeight - height());
452 m_contentYAnimation->setTo(to);
453 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
454 m_contentYAnimation->start();
455 } else if ((listItemY < contentY() && listItemY + listItemHeight < contentY() + height()) ||
456 (m_topSectionItem && !listItem->sectionItem() && listItemY - m_topSectionItem->height() < contentY() && listItemY + listItemHeight < contentY() + height()))
458 // we can scroll the list down to show more stuff
459 auto realVisibleListItemY = listItemY;
460 if (m_topSectionItem) {
461 // If we are showing the top section sticky item and this item doesn't have a section
462 // item we have to make sure to scroll it a bit more so that it is not underlapping
463 // the top section sticky item
464 bool topSectionShown = !QQuickItemPrivate::get(m_topSectionItem)->culled;
465 if (topSectionShown && !listItem->sectionItem()) {
466 realVisibleListItemY -= m_topSectionItem->height();
469 const auto to = qMax(realVisibleListItemY, listItemY + listItemHeight - height());
470 m_contentYAnimation->setTo(to);
471 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
472 m_contentYAnimation->start();
479qreal ListViewWithPageHeader::minYExtent() const
481// qDebug() << "ListViewWithPageHeader::minYExtent" << m_minYExtent;
485void ListViewWithPageHeader::componentComplete()
488 m_delegateModel->componentComplete();
490 QQuickFlickable::componentComplete();
495void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
497 // Check we are not being taken down and don't paint anything
498 // TODO Check if we still need this in 5.2
499 // For reproduction just inifnite loop testDash or testDashContent
500 if (!QQmlEngine::contextForObject(this)->parentContext())
503 QQuickFlickable::viewportMoved(orient);
504// qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
505 const qreal diff = m_previousContentY - contentY();
507 m_previousContentY = contentY();
512void ListViewWithPageHeader::adjustHeader(qreal diff)
514 const bool showHeaderAnimationRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationShowHeader;
516 const auto oldHeaderItemShownHeight = m_headerItemShownHeight;
517 if (uFuzzyCompare(contentY(), -m_minYExtent) || contentY() > -m_minYExtent) {
518 m_headerItem->setHeight(m_headerItem->implicitHeight());
519 // We are going down (but it's not because of the rebound at the end)
520 // (but the header was not shown by it's own position)
521 // or the header is partially shown and we are not doing a maximizeVisibleArea either
522 const bool scrolledUp = m_previousContentY > contentY();
523 const bool notRebounding = contentY() + height() < contentHeight();
524 const bool notShownByItsOwn = contentY() + diff >= m_headerItem->y() + m_headerItem->height();
525 const bool maximizeVisibleAreaRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationMaximizeVisibleArea;
527 if (!scrolledUp && (contentY() == -m_minYExtent || (m_headerItemShownHeight == 0 && m_previousContentY == m_headerItem->y()))) {
528 m_headerItemShownHeight = 0;
529 m_headerItem->setY(-m_minYExtent);
530 } else if ((scrolledUp && notRebounding && notShownByItsOwn && !maximizeVisibleAreaRunning) || (m_headerItemShownHeight > 0) || m_inContentHeightKeepHeaderShown) {
531 if (maximizeVisibleAreaRunning && diff > 0) {
532 // If we are maximizing and the header was shown, make sure we hide it
533 m_headerItemShownHeight -= diff;
535 m_headerItemShownHeight += diff;
537 if (uFuzzyCompare(contentY(), -m_minYExtent)) {
538 m_headerItemShownHeight = 0;
540 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
542 if (m_headerItemShownHeight > 0) {
543 if (uFuzzyCompare(m_headerItem->height(), m_headerItemShownHeight)) {
544 m_headerItem->setY(contentY());
545 m_headerItemShownHeight = m_headerItem->height();
547 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
550 m_headerItem->setY(-m_minYExtent);
553 Q_EMIT headerItemShownHeightChanged();
555 // Stick the header item to the top when dragging down
556 m_headerItem->setY(contentY());
557 m_headerItem->setHeight(m_headerItem->implicitHeight() + (-m_minYExtent - contentY()));
559 // We will be changing the clip item, need to accomadate for it
560 // otherwise we move the firstItem down/up twice (unless the
561 // show header animation is running, where we want to keep the viewport stable)
562 if (!showHeaderAnimationRunning) {
563 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
568 if (!m_visibleItems.isEmpty()) {
570 ListItem *firstItem = m_visibleItems.first();
571 firstItem->setY(firstItem->y() + diff);
572 if (showHeaderAnimationRunning) {
578void ListViewWithPageHeader::createDelegateModel()
580 m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
581 connect(m_delegateModel, SIGNAL(createdItem(int,QObject*)), this, SLOT(itemCreated(int,QObject*)));
582 if (isComponentComplete())
583 m_delegateModel->componentComplete();
584 updateWatchedRoles();
587void ListViewWithPageHeader::refill()
592 if (!isComponentComplete()) {
596 const qreal buffer = height() * bufferRatio;
597 const qreal from = contentY();
598 const qreal to = from + height();
599 const qreal bufferFrom = from - buffer;
600 const qreal bufferTo = to + buffer;
602 bool added = addVisibleItems(from, to, false);
603 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
604 added |= addVisibleItems(bufferFrom, bufferTo, true);
606 if (added || removed) {
607 m_contentHeightDirty = true;
611bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
616 if (m_delegateModel->count() == 0)
620// qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
624 if (!m_visibleItems.isEmpty()) {
625 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
626 item = m_visibleItems.last();
627 pos = item->y() + item->height() + m_clipItem->y();
629 bool changed = false;
630// qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
631 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
632// qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
633 if (!(item = createItem(modelIndex, asynchronous)))
635 pos += item->height();
642 if (!m_visibleItems.isEmpty()) {
643 modelIndex = m_firstVisibleIndex - 1;
644 item = m_visibleItems.first();
645 pos = item->y() + m_clipItem->y();
647 while (modelIndex >= 0 && pos > fillFrom) {
648// qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
649 if (!(item = createItem(modelIndex, asynchronous)))
651 pos -= item->height();
659void ListViewWithPageHeader::reallyReleaseItem(ListItem *listItem)
661 QQuickItem *item = listItem->m_item;
662 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
663 if (flags & QQmlDelegateModel::Destroyed) {
664 item->setParentItem(nullptr);
666 listItem->sectionItem()->deleteLater();
670void ListViewWithPageHeader::releaseItem(ListItem *listItem)
672 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(listItem->m_item);
673 itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
674 m_itemsToRelease << listItem;
677void ListViewWithPageHeader::updateWatchedRoles()
679 if (m_delegateModel) {
680 QList<QByteArray> roles;
681 if (!m_sectionProperty.isEmpty())
682 roles << m_sectionProperty.toUtf8();
683 m_delegateModel->setWatchedRoles(roles);
687QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
689 if (!m_sectionDelegate)
692 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
694 if (modelIndex > 0) {
695 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
696 if (section == prevSection)
699 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
700 // Already inserted items can't steal next section header
701 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
702 if (section == nextSection) {
703 // Steal the section header
704 ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
706 QQuickItem *sectionItem = nextItem->sectionItem();
707 nextItem->setSectionItem(nullptr);
713 return getSectionItem(section);
716QQuickItem *ListViewWithPageHeader::getSectionItem(const QString §ionText)
718 QQuickItem *sectionItem = nullptr;
720 QQmlContext *creationContext = m_sectionDelegate->creationContext();
721 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
722 context->setContextProperty(QLatin1String("section"), sectionText);
723 context->setContextProperty(QLatin1String("delegateIndex"), -1);
724 QObject *nobj = m_sectionDelegate->beginCreate(context);
726 QQml_setParent_noEvent(context, nobj);
727 sectionItem = qobject_cast<QQuickItem *>(nobj);
731 sectionItem->setZ(2);
732 QQml_setParent_noEvent(sectionItem, m_clipItem);
733 sectionItem->setParentItem(m_clipItem);
738 m_sectionDelegate->completeCreate();
740 // TODO attach to sectionItem so we can accomodate to it changing its height
745bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
747// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
748 // Do not remove items if we are overshooting up or down, since we'll come back
749 // to the "stable" position and delete/create items without any reason
750 if (contentY() < -m_minYExtent) {
752 } else if (contentY() + height() > contentHeight()) {
755 bool changed = false;
757 bool foundVisible = false;
759 int removedItems = 0;
760 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
761 while (i < m_visibleItems.count()) {
762 ListItem *item = m_visibleItems[i];
763 const qreal pos = item->y() + m_clipItem->y();
764// qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
765 if (pos + item->height() < bufferFrom || pos > bufferTo) {
766// qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
768 m_visibleItems.removeAt(i);
774 const int itemIndex = m_firstVisibleIndex + removedItems + i;
775 m_firstVisibleIndex = itemIndex;
781 m_firstVisibleIndex = -1;
783 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
790ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
792// qDebug() << "CREATE ITEM" << modelIndex;
793 if (asynchronous && m_asyncRequestedIndex != -1)
796 m_asyncRequestedIndex = -1;
797 QObject* object = m_delegateModel->object(modelIndex, asynchronous);
798 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
801 m_delegateModel->release(object);
802 if (!m_delegateValidated) {
803 m_delegateValidated = true;
804 QObject* delegateObj = delegate();
805 qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
808 m_asyncRequestedIndex = modelIndex;
812// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
813 ListItem *listItem = new ListItem;
814 listItem->m_item = item;
815 listItem->setSectionItem(getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/));
816 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
817 ListItem *prevItem = itemAtIndex(modelIndex - 1);
818 bool lostItem = false; // Is an item that we requested async but because of model changes
819 // it is no longer attached to any of the existing items (has no prev nor next item)
820 // nor is the first item
822 listItem->setY(prevItem->y() + prevItem->height());
824 ListItem *currItem = itemAtIndex(modelIndex);
826 // There's something already in m_visibleItems at out index, meaning this is an insert, so attach to its top
827 listItem->setY(currItem->y() - listItem->height());
829 ListItem *nextItem = itemAtIndex(modelIndex + 1);
831 listItem->setY(nextItem->y() - listItem->height());
832 } else if (modelIndex == 0) {
833 listItem->setY(m_headerItem ? m_headerItem->height() : 0);
834 } else if (!m_visibleItems.isEmpty()) {
840 listItem->setCulled(true);
841 releaseItem(listItem);
844 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() <= contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
845 if (m_visibleItems.isEmpty()) {
846 m_visibleItems << listItem;
848 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
850 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
851 m_firstVisibleIndex = modelIndex;
854 if (listItem->sectionItem()) {
855 QQmlContext *context = QQmlEngine::contextForObject(listItem->sectionItem())->parentContext();
856 context->setContextProperty(QLatin1String("delegateIndex"), modelIndex);
859 m_contentHeightDirty = true;
865void ListViewWithPageHeader::itemCreated(int modelIndex, QObject *object)
867 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
869 qWarning() << "ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
872// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
873 // Check we are not being taken down and don't paint anything
874 // TODO Check if we still need this in 5.2
875 // For reproduction just inifnite loop testDash or testDashContent
876 if (!QQmlEngine::contextForObject(this)->parentContext())
879 item->setParentItem(m_clipItem);
880 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
881 context->setContextProperty(QLatin1String("ListViewWithPageHeader"), this);
882 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
883 if (modelIndex == m_asyncRequestedIndex) {
884 createItem(modelIndex, false);
889void ListViewWithPageHeader::updateClipItem()
891 m_clipItem->setHeight(height() - m_headerItemShownHeight);
892 m_clipItem->setY(contentY() + m_headerItemShownHeight);
893 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
896void ListViewWithPageHeader::onContentHeightChanged()
901void ListViewWithPageHeader::onContentWidthChanged()
903 m_clipItem->setWidth(contentItem()->width());
906void ListViewWithPageHeader::onHeightChanged()
912void ListViewWithPageHeader::onModelUpdated(const QQmlChangeSet &changeSet, bool /*reset*/)
914 // TODO Do something with reset
915// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
916 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
918 Q_FOREACH(const QQmlChangeSet::Remove &remove, changeSet.removes()) {
919// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove.index << remove.count;
920 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
921 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
922 // If all the items we are removing are either not created or culled
923 // we have to grow down to avoid viewport changing
924 bool growDown = true;
925 for (int i = 0; growDown && i < remove.count; ++i) {
926 const int modelIndex = remove.index + i;
927 ListItem *item = itemAtIndex(modelIndex);
928 if (item && !item->culled()) {
932 for (int i = remove.count - 1; i >= 0; --i) {
933 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
934 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
935 ListItem *item = m_visibleItems[visibleIndex];
936 // Pass the section item down if needed
937 if (item->sectionItem() && visibleIndex + 1 < m_visibleItems.count()) {
938 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
939 if (!nextItem->sectionItem()) {
940 nextItem->setSectionItem(item->sectionItem());
941 item->setSectionItem(nullptr);
945 m_visibleItems.removeAt(visibleIndex);
950 } else if (remove.index <= m_firstVisibleIndex) {
951 if (!m_visibleItems.isEmpty()) {
952 // We removed the first item that is the one that positions the rest
953 // position the new first item correctly
954 m_visibleItems.first()->setY(oldFirstValidIndexPos);
956 m_firstVisibleIndex = -1;
959 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
960 m_firstVisibleIndex -= remove.count;
962 for (int i = remove.count - 1; i >= 0; --i) {
963 const int modelIndex = remove.index + i;
964 if (modelIndex == m_asyncRequestedIndex) {
965 m_asyncRequestedIndex = -1;
966 } else if (modelIndex < m_asyncRequestedIndex) {
967 m_asyncRequestedIndex--;
972 Q_FOREACH(const QQmlChangeSet::Insert &insert, changeSet.inserts()) {
973// qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
974 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
975 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
976 if (insertingInValidIndexes || firstItemWithViewOnTop)
978 // If the items we are adding won't be really visible
979 // we grow up instead of down to not change the viewport
981 if (!firstItemWithViewOnTop) {
982 for (int i = 0; i < m_visibleItems.count(); ++i) {
983 if (!m_visibleItems[i]->culled()) {
984 if (insert.index <= m_firstVisibleIndex + i) {
992 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
993 for (int i = insert.count - 1; i >= 0; --i) {
994 const int modelIndex = insert.index + i;
995 ListItem *item = createItem(modelIndex, false);
997 ListItem *firstItem = m_visibleItems.first();
998 firstItem->setY(firstItem->y() - item->height());
1000 // Adding an item may break a "same section" chain, so check
1001 // if we need adding a new section item
1002 if (m_sectionDelegate) {
1003 ListItem *nextItem = itemAtIndex(modelIndex + 1);
1004 if (nextItem && !nextItem->sectionItem()) {
1005 nextItem->setSectionItem(getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/));
1006 if (growUp && nextItem->sectionItem()) {
1007 ListItem *firstItem = m_visibleItems.first();
1008 firstItem->setY(firstItem->y() - nextItem->sectionItem()->height());
1013 if (firstItemWithViewOnTop) {
1014 ListItem *firstItem = m_visibleItems.first();
1015 firstItem->setY(oldFirstValidIndexPos);
1018 } else if (insert.index <= m_firstVisibleIndex) {
1019 m_firstVisibleIndex += insert.count;
1022 for (int i = insert.count - 1; i >= 0; --i) {
1023 const int modelIndex = insert.index + i;
1024 if (modelIndex <= m_asyncRequestedIndex) {
1025 m_asyncRequestedIndex++;
1030 Q_FOREACH(const QQmlChangeSet::Change &change, changeSet.changes()) {
1031// qDebug() << "ListViewWithPageHeader::onModelUpdated Change" << change.index << change.count;
1032 for (int i = change.index; i < change.count; ++i) {
1033 ListItem *item = itemAtIndex(i);
1034 if (item && item->sectionItem()) {
1035 QQmlContext *context = QQmlEngine::contextForObject(item->sectionItem())->parentContext();
1036 const QString sectionText = m_delegateModel->stringValue(i, m_sectionProperty);
1037 context->setContextProperty(QLatin1String("section"), sectionText);
1042 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
1046 for (int i = 0; i < m_visibleItems.count(); ++i) {
1047 ListItem *item = m_visibleItems[i];
1048 if (item->sectionItem()) {
1049 QQmlContext *context = QQmlEngine::contextForObject(item->sectionItem())->parentContext();
1050 context->setContextProperty(QLatin1String("delegateIndex"), m_firstVisibleIndex + i);
1056 m_contentHeightDirty = true;
1059void ListViewWithPageHeader::onShowHeaderAnimationFinished()
1061 m_contentHeightDirty = true;
1065void ListViewWithPageHeader::itemGeometryChanged(QQuickItem * /*item*/, const QRectF &newGeometry, const QRectF &oldGeometry)
1067 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
1068 if (heightDiff != 0) {
1069 if (!m_inContentHeightKeepHeaderShown && oldGeometry.y() + oldGeometry.height() + m_clipItem->y() <= contentY() && !m_visibleItems.isEmpty()) {
1070 ListItem *firstItem = m_visibleItems.first();
1071 firstItem->setY(firstItem->y() - heightDiff);
1078 m_contentHeightDirty = true;
1082void ListViewWithPageHeader::itemImplicitHeightChanged(QQuickItem *item)
1084 if (item == m_headerItem) {
1085 const qreal diff = m_headerItem->implicitHeight() - m_previousHeaderImplicitHeight;
1088 m_previousHeaderImplicitHeight = m_headerItem->implicitHeight();
1091 m_contentHeightDirty = true;
1096void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
1098 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
1099 if (m_headerItemShownHeight > 0) {
1100 // If the header is shown because of the clip
1102 m_headerItemShownHeight += heightDiff;
1103 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
1106 Q_EMIT headerItemShownHeightChanged();
1108 if (oldHeaderY + oldHeaderHeight > contentY()) {
1109 // If the header is shown because its position
1111 ListItem *firstItem = m_visibleItems.first();
1112 firstItem->setY(firstItem->y() + heightDiff);
1115 // If the header is not on screen, just change the start of the list
1116 // so the viewport is not changed
1123void ListViewWithPageHeader::adjustMinYExtent()
1125 if (m_visibleItems.isEmpty()) {
1128 qreal nonCreatedHeight = 0;
1129 if (m_firstVisibleIndex != 0) {
1130 // Calculate the average height of items to estimate the position of the list start
1131 const int visibleItems = m_visibleItems.count();
1132 qreal visibleItemsHeight = 0;
1133 Q_FOREACH(ListItem *item, m_visibleItems) {
1134 visibleItemsHeight += item->height();
1136 nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
1137// qDebug() << m_firstVisibleIndex << visibleItemsHeight << visibleItems << nonCreatedHeight;
1139 const qreal headerHeight = (m_headerItem ? m_headerItem->implicitHeight() : 0);
1140 m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + headerHeight;
1141 if (m_minYExtent != 0 && qFuzzyIsNull(m_minYExtent)) {
1143 m_visibleItems.first()->setY(nonCreatedHeight - m_clipItem->y() + headerHeight);
1148ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
1150 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
1151 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
1152 return m_visibleItems[visibleIndexedModelIndex];
1157void ListViewWithPageHeader::layout()
1163 if (!m_visibleItems.isEmpty()) {
1164 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
1165 const qreal visibleTo = contentY() + height() - m_clipItem->y();
1167 qreal pos = m_visibleItems.first()->y();
1169// qDebug() << "ListViewWithPageHeader::layout Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
1170 int firstReallyVisibleItem = -1;
1171 int modelIndex = m_firstVisibleIndex;
1172 Q_FOREACH(ListItem *item, m_visibleItems) {
1173 const bool cull = pos + item->height() <= visibleFrom || pos >= visibleTo;
1174 item->setCulled(cull);
1176 if (!cull && firstReallyVisibleItem == -1) {
1177 firstReallyVisibleItem = modelIndex;
1178 if (m_topSectionItem) {
1179 // Positing the top section sticky item is a two step process
1180 // First we set it either we cull it (because it doesn't need to be sticked to the top)
1181 // or stick it to the top
1182 // Then after the loop we'll make sure that if there's another section just below it
1183 // pushed the sticky section up to make it disappear
1184 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
1185 bool showStickySectionItem;
1186 // We need to show the "top section sticky item" when the position at the "top" of the
1187 // viewport is bigger than the start of the position of the first visible item
1188 // i.e. the first visible item starts before the viewport, or when the first
1189 // visible item starts just at the viewport start and it does not have its own section item
1190 if (topSectionStickPos > pos) {
1191 showStickySectionItem = true;
1192 } else if (topSectionStickPos == pos) {
1193 showStickySectionItem = !item->sectionItem();
1195 showStickySectionItem = false;
1197 if (!showStickySectionItem) {
1198 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
1199 if (item->sectionItem()) {
1200 // This seems it should happen since why would we cull the top section
1201 // if the first visible item has no section header? This only happens briefly
1202 // when increasing the height of a list that is at the bottom, the m_topSectionItem
1203 // gets shown shortly in the next polish call
1204 QQuickItemPrivate::get(item->sectionItem())->setCulled(false);
1207 // Update the top sticky section header
1208 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1209 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
1210 context->setContextProperty(QLatin1String("section"), section);
1212 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
1213 m_topSectionItem->setY(topSectionStickPos);
1214 int delegateIndex = modelIndex;
1215 // Look for the first index with this section text
1216 while (delegateIndex > 0) {
1217 const QString prevSection = m_delegateModel->stringValue(delegateIndex - 1, m_sectionProperty);
1218 if (prevSection != section)
1222 context->setContextProperty(QLatin1String("delegateIndex"), delegateIndex);
1223 if (item->sectionItem()) {
1224 QQuickItemPrivate::get(item->sectionItem())->setCulled(true);
1229 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
1230 const qreal clipFrom = visibleFrom + (!item->sectionItem() && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
1231 if (!cull && pos < clipFrom) {
1232 context->setContextProperty(QLatin1String("heightToClip"), clipFrom - pos);
1234 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
1236// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
1237 pos += item->height();
1241 // Second step of section sticky item positioning
1242 // Look at the next section header, check if it's pushing up the sticky one
1243 if (m_topSectionItem) {
1244 if (firstReallyVisibleItem >= 0) {
1245 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
1246 ListItem *item = m_visibleItems[i];
1247 if (item->sectionItem()) {
1248 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
1249 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
1260void ListViewWithPageHeader::updatePolish()
1262 // Check we are not being taken down and don't paint anything
1263 // TODO Check if we still need this in 5.2
1264 // For reproduction just inifnite loop testDash or testDashContent
1265 if (!QQmlEngine::contextForObject(this)->parentContext())
1268 Q_FOREACH(ListItem *item, m_itemsToRelease)
1269 reallyReleaseItem(item);
1270 m_itemsToRelease.clear();
1279 if (m_contentHeightDirty) {
1280 qreal contentHeight;
1281 if (m_visibleItems.isEmpty()) {
1282 contentHeight = m_headerItem ? m_headerItem->height() : 0;
1284 const int modelCount = model()->rowCount();
1285 const int visibleItems = m_visibleItems.count();
1286 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
1287 qreal nonCreatedHeight = 0;
1288 if (lastValidIndex != modelCount - 1) {
1289 const int visibleItems = m_visibleItems.count();
1290 qreal visibleItemsHeight = 0;
1291 Q_FOREACH(ListItem *item, m_visibleItems) {
1292 visibleItemsHeight += item->height();
1294 const int unknownSizes = modelCount - (m_firstVisibleIndex + visibleItems);
1295 nonCreatedHeight = unknownSizes * visibleItemsHeight / visibleItems;
1297 ListItem *item = m_visibleItems.last();
1298 contentHeight = nonCreatedHeight + item->y() + item->height() + m_clipItem->y();
1299 if (m_firstVisibleIndex != 0) {
1300 // Make sure that if we are shrinking we tell the view we still fit
1301 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
1305 m_contentHeightDirty = false;
1307 m_inContentHeightKeepHeaderShown = m_headerItem && m_headerItem->y() == contentY();
1308 setContentHeight(contentHeight);
1309 m_inContentHeightKeepHeaderShown = false;
1313#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