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#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
101#include <private/qquickvisualdatamodel_p.h>
103#include <private/qqmldelegatemodel_p.h>
105#include <private/qqmlglobal_p.h>
106#include <private/qquickitem_p.h>
107#include <private/qquickanimation_p.h>
108#pragma GCC diagnostic pop
109// #include <private/qquickrectangle_p.h>
111static const qreal bufferRatio = 0.5;
113qreal ListViewWithPageHeader::ListItem::height() const
115 return m_item->height() + (m_sectionItem ? m_sectionItem->height() : 0);
118qreal ListViewWithPageHeader::ListItem::y() const
120 return m_item->y() - (m_sectionItem ? m_sectionItem->height() : 0);
123void ListViewWithPageHeader::ListItem::setY(qreal newY)
126 m_sectionItem->setY(newY);
127 m_item->setY(newY + m_sectionItem->height());
133bool ListViewWithPageHeader::ListItem::culled() const
135 return QQuickItemPrivate::get(m_item)->culled;
138void ListViewWithPageHeader::ListItem::setCulled(bool culled)
140 QQuickItemPrivate::get(m_item)->setCulled(culled);
142 QQuickItemPrivate::get(m_sectionItem)->setCulled(culled);
145ListViewWithPageHeader::ListViewWithPageHeader()
146 : m_delegateModel(nullptr)
147 , m_asyncRequestedIndex(-1)
148 , m_delegateValidated(false)
149 , m_firstVisibleIndex(-1)
151 , m_contentHeightDirty(false)
152 , m_headerItem(nullptr)
153 , m_previousContentY(0)
154 , m_headerItemShownHeight(0)
155 , m_sectionDelegate(nullptr)
156 , m_topSectionItem(nullptr)
157 , m_forceNoClip(false)
159 , 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#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
195 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
197 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
198 connect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
200 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
202 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
203 connect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
205 Q_EMIT modelChanged();
208// Q_EMIT contentHeightChanged();
209// Q_EMIT contentYChanged();
213QQmlComponent *ListViewWithPageHeader::delegate() const
215 return m_delegateModel ? m_delegateModel->delegate() : nullptr;
218void ListViewWithPageHeader::setDelegate(QQmlComponent *delegate)
220 if (delegate != this->delegate()) {
221 if (!m_delegateModel) {
222 createDelegateModel();
225 // Cleanup the existing items
226 Q_FOREACH(ListItem *item, m_visibleItems)
228 m_visibleItems.clear();
229 m_firstVisibleIndex = -1;
233 if (m_topSectionItem) {
234 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
237 m_delegateModel->setDelegate(delegate);
239 Q_EMIT delegateChanged();
240 m_delegateValidated = false;
241 m_contentHeightDirty = true;
246QQuickItem *ListViewWithPageHeader::header() const
251void ListViewWithPageHeader::setHeader(QQuickItem *headerItem)
253 if (m_headerItem != headerItem) {
254 qreal oldHeaderHeight = 0;
255 qreal oldHeaderY = 0;
257 oldHeaderHeight = m_headerItem->height();
258 oldHeaderY = m_headerItem->y();
259 m_headerItem->setParentItem(nullptr);
261 m_headerItem = headerItem;
263 m_headerItem->setParentItem(contentItem());
264 m_headerItem->setZ(1);
266 qreal newHeaderHeight = m_headerItem ? m_headerItem->height() : 0;
267 if (!m_visibleItems.isEmpty() && newHeaderHeight != oldHeaderHeight) {
268 headerHeightChanged(newHeaderHeight, oldHeaderHeight, oldHeaderY);
270 m_contentHeightDirty = true;
272 Q_EMIT headerChanged();
276QQmlComponent *ListViewWithPageHeader::sectionDelegate() const
278 return m_sectionDelegate;
281void ListViewWithPageHeader::setSectionDelegate(QQmlComponent *delegate)
283 if (delegate != m_sectionDelegate) {
284 // TODO clean existing sections
286 m_sectionDelegate = delegate;
288 m_topSectionItem = getSectionItem(QString());
289 m_topSectionItem->setZ(3);
290 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
291 connect(m_topSectionItem, SIGNAL(heightChanged()), SIGNAL(stickyHeaderHeightChanged()));
293 // TODO create sections for existing items
295 Q_EMIT sectionDelegateChanged();
296 Q_EMIT stickyHeaderHeightChanged();
300QString ListViewWithPageHeader::sectionProperty() const
302 return m_sectionProperty;
305void ListViewWithPageHeader::setSectionProperty(const QString &property)
307 if (property != m_sectionProperty) {
308 m_sectionProperty = property;
310 updateWatchedRoles();
312 // TODO recreate sections
314 Q_EMIT sectionPropertyChanged();
318bool ListViewWithPageHeader::forceNoClip() const
320 return m_forceNoClip;
323void ListViewWithPageHeader::setForceNoClip(bool noClip)
325 if (noClip != m_forceNoClip) {
326 m_forceNoClip = noClip;
328 Q_EMIT forceNoClipChanged();
332int ListViewWithPageHeader::stickyHeaderHeight() const
334 return m_topSectionItem ? m_topSectionItem->height() : 0;
337void ListViewWithPageHeader::positionAtBeginning()
339 if (m_delegateModel->count() <= 0)
342 qreal headerHeight = (m_headerItem ? m_headerItem->height() : 0);
343 if (m_firstVisibleIndex != 0) {
344 // TODO This could be optimized by trying to reuse the interesection
345 // of items that may end up intersecting between the existing
346 // m_visibleItems and the items we are creating in the next loop
347 Q_FOREACH(ListItem *item, m_visibleItems)
349 m_visibleItems.clear();
350 m_firstVisibleIndex = -1;
352 // Create the item 0, it will be already correctly positioned at createItem()
354 ListItem *item = createItem(0, false);
355 // Create the subsequent items
357 qreal pos = item->y() + item->height();
358 const qreal buffer = height() * bufferRatio;
359 const qreal bufferTo = height() + buffer;
360 while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
361 if (!(item = createItem(modelIndex, false)))
363 pos += item->height();
367 m_previousContentY = m_visibleItems.first()->y() - headerHeight;
369 setContentY(m_visibleItems.first()->y() + m_clipItem->y() - headerHeight);
371 // TODO This should not be needed and the code that adjust the m_headerItem position
372 // in viewportMoved() should be enough but in some cases we have not found a way to reproduce
373 // yet the code of viewportMoved() fails so here we make sure that at least if we are calling
374 // positionAtBeginning the header item will be correctly positioned
375 m_headerItem->setY(-m_minYExtent);
379static inline bool uFuzzyCompare(qreal r1, qreal r2)
381 return qFuzzyCompare(r1, r2) || (qFuzzyIsNull(r1) && qFuzzyIsNull(r2));
384void ListViewWithPageHeader::showHeader()
389 const auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
390 if (!uFuzzyCompare(to, contentY())) {
391 const bool headerShownByItsOwn = contentY() < m_headerItem->y() + m_headerItem->height();
392 if (headerShownByItsOwn && m_headerItemShownHeight == 0) {
393 // We are not clipping since we are just at the top of the viewport
394 // but because of the showHeader animation we will need to, so
395 // enable the clipping without logically moving the items
396 m_headerItemShownHeight = m_headerItem->y() + m_headerItem->height() - contentY();
397 if (!m_visibleItems.isEmpty()) {
399 ListItem *firstItem = m_visibleItems.first();
400 firstItem->setY(firstItem->y() - m_headerItemShownHeight);
404 m_contentYAnimation->setTo(to);
405 contentYAnimationType = ContentYAnimationShowHeader;
406 m_contentYAnimation->start();
410QQuickItem *ListViewWithPageHeader::item(int modelIndex) const
412 ListItem *item = itemAtIndex(modelIndex);
419bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex)
421 ListItem *listItem = itemAtIndex(modelIndex);
423 return maximizeVisibleArea(listItem, listItem->height());
429bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex, int itemHeight)
434 ListItem *listItem = itemAtIndex(modelIndex);
436 return maximizeVisibleArea(listItem, itemHeight + (listItem->m_sectionItem ? listItem->m_sectionItem->height() : 0));
442bool ListViewWithPageHeader::maximizeVisibleArea(ListItem *listItem, int listItemHeight)
446 const auto listItemY = m_clipItem->y() + listItem->y();
447 if (listItemY > contentY() && listItemY + listItemHeight > contentY() + height()) {
448 // we can scroll the list up to show more stuff
449 const auto to = qMin(listItemY, listItemY + listItemHeight - height());
450 m_contentYAnimation->setTo(to);
451 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
452 m_contentYAnimation->start();
453 } else if ((listItemY < contentY() && listItemY + listItemHeight < contentY() + height()) ||
454 (m_topSectionItem && !listItem->m_sectionItem && listItemY - m_topSectionItem->height() < contentY() && listItemY + listItemHeight < contentY() + height()))
456 // we can scroll the list down to show more stuff
457 auto realVisibleListItemY = listItemY;
458 if (m_topSectionItem) {
459 // If we are showing the top section sticky item and this item doesn't have a section
460 // item we have to make sure to scroll it a bit more so that it is not underlapping
461 // the top section sticky item
462 bool topSectionShown = !QQuickItemPrivate::get(m_topSectionItem)->culled;
463 if (topSectionShown && !listItem->m_sectionItem) {
464 realVisibleListItemY -= m_topSectionItem->height();
467 const auto to = qMax(realVisibleListItemY, listItemY + listItemHeight - height());
468 m_contentYAnimation->setTo(to);
469 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
470 m_contentYAnimation->start();
477qreal ListViewWithPageHeader::minYExtent() const
479// qDebug() << "ListViewWithPageHeader::minYExtent" << m_minYExtent;
483void ListViewWithPageHeader::componentComplete()
486 m_delegateModel->componentComplete();
488 QQuickFlickable::componentComplete();
493void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
495 // Check we are not being taken down and don't paint anything
496 // TODO Check if we still need this in 5.2
497 // For reproduction just inifnite loop testDash or testDashContent
498 if (!QQmlEngine::contextForObject(this)->parentContext())
501 QQuickFlickable::viewportMoved(orient);
502// qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
503 qreal diff = m_previousContentY - contentY();
504 const bool showHeaderAnimationRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationShowHeader;
506 const auto oldHeaderItemShownHeight = m_headerItemShownHeight;
507 if (uFuzzyCompare(contentY(), -m_minYExtent) || contentY() > -m_minYExtent) {
508 m_headerItem->setHeight(m_headerItem->implicitHeight());
509 // We are going down (but it's not because of the rebound at the end)
510 // (but the header was not shown by it's own position)
511 // or the header is partially shown and we are not doing a maximizeVisibleArea either
512 const bool scrolledUp = m_previousContentY > contentY();
513 const bool notRebounding = contentY() + height() < contentHeight();
514 const bool notShownByItsOwn = contentY() + diff >= m_headerItem->y() + m_headerItem->height();
515 const bool maximizeVisibleAreaRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationMaximizeVisibleArea;
517 if (!scrolledUp && (contentY() == -m_minYExtent || (m_headerItemShownHeight == 0 && m_previousContentY == m_headerItem->y()))) {
518 m_headerItemShownHeight = 0;
519 m_headerItem->setY(-m_minYExtent);
520 } else if ((scrolledUp && notRebounding && notShownByItsOwn && !maximizeVisibleAreaRunning) || (m_headerItemShownHeight > 0) || m_inContentHeightKeepHeaderShown) {
521 if (maximizeVisibleAreaRunning && diff > 0) {
522 // If we are maximizing and the header was shown, make sure we hide it
523 m_headerItemShownHeight -= diff;
525 m_headerItemShownHeight += diff;
527 if (uFuzzyCompare(contentY(), -m_minYExtent)) {
528 m_headerItemShownHeight = 0;
530 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
532 if (m_headerItemShownHeight > 0) {
533 if (uFuzzyCompare(m_headerItem->height(), m_headerItemShownHeight)) {
534 m_headerItem->setY(contentY());
535 m_headerItemShownHeight = m_headerItem->height();
537 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
540 m_headerItem->setY(-m_minYExtent);
544 // Stick the header item to the top when dragging down
545 m_headerItem->setY(contentY());
546 m_headerItem->setHeight(m_headerItem->implicitHeight() + (-m_minYExtent - contentY()));
548 // We will be changing the clip item, need to accomadate for it
549 // otherwise we move the firstItem down/up twice (unless the
550 // show header animation is running, where we want to keep the viewport stable)
551 if (!showHeaderAnimationRunning) {
552 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
557 if (!m_visibleItems.isEmpty()) {
559 ListItem *firstItem = m_visibleItems.first();
560 firstItem->setY(firstItem->y() + diff);
561 if (showHeaderAnimationRunning) {
566 m_previousContentY = contentY();
571void ListViewWithPageHeader::createDelegateModel()
573#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
574 m_delegateModel = new QQuickVisualDataModel(qmlContext(this), this);
575 connect(m_delegateModel, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(itemCreated(int,QQuickItem*)));
577 m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
578 connect(m_delegateModel, SIGNAL(createdItem(int,QObject*)), this, SLOT(itemCreated(int,QObject*)));
580 if (isComponentComplete())
581 m_delegateModel->componentComplete();
582 updateWatchedRoles();
585void ListViewWithPageHeader::refill()
590 if (!isComponentComplete()) {
594 const qreal buffer = height() * bufferRatio;
595 const qreal from = contentY();
596 const qreal to = from + height();
597 const qreal bufferFrom = from - buffer;
598 const qreal bufferTo = to + buffer;
600 bool added = addVisibleItems(from, to, false);
601 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
602 added |= addVisibleItems(bufferFrom, bufferTo, true);
604 if (added || removed) {
605 m_contentHeightDirty = true;
609bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
614 if (m_delegateModel->count() == 0)
618// qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
622 if (!m_visibleItems.isEmpty()) {
623 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
624 item = m_visibleItems.last();
625 pos = item->y() + item->height() + m_clipItem->y();
627 bool changed = false;
628// qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
629 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
630// qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
631 if (!(item = createItem(modelIndex, asynchronous)))
633 pos += item->height();
640 if (!m_visibleItems.isEmpty()) {
641 modelIndex = m_firstVisibleIndex - 1;
642 item = m_visibleItems.first();
643 pos = item->y() + m_clipItem->y();
645 while (modelIndex >= 0 && pos > fillFrom) {
646// qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
647 if (!(item = createItem(modelIndex, asynchronous)))
649 pos -= item->height();
657void ListViewWithPageHeader::reallyReleaseItem(ListItem *listItem)
659 QQuickItem *item = listItem->m_item;
660#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
661 QQuickVisualModel::ReleaseFlags flags = m_delegateModel->release(item);
662 if (flags & QQuickVisualModel::Destroyed) {
664 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
665 if (flags & QQmlDelegateModel::Destroyed) {
667 item->setParentItem(nullptr);
669 delete listItem->m_sectionItem;
673void ListViewWithPageHeader::releaseItem(ListItem *listItem)
675 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(listItem->m_item);
676 itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
677 m_itemsToRelease << listItem;
680void ListViewWithPageHeader::updateWatchedRoles()
682 if (m_delegateModel) {
683 QList<QByteArray> roles;
684 if (!m_sectionProperty.isEmpty())
685 roles << m_sectionProperty.toUtf8();
686 m_delegateModel->setWatchedRoles(roles);
690QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
692 if (!m_sectionDelegate)
695 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
696 if (modelIndex > 0) {
697 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
698 if (section == prevSection)
701 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
702 // Already inserted items can't steal next section header
703 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
704 if (section == nextSection) {
705 // Steal the section header
706 ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
708 QQuickItem *sectionItem = nextItem->m_sectionItem;
709 nextItem->m_sectionItem = nullptr;
715 return getSectionItem(section);
718QQuickItem *ListViewWithPageHeader::getSectionItem(const QString §ionText)
720 QQuickItem *sectionItem = nullptr;
722 QQmlContext *creationContext = m_sectionDelegate->creationContext();
723 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
724 context->setContextProperty(QLatin1String("section"), sectionText);
725 context->setContextProperty(QLatin1String("delegateIndex"), -1);
726 QObject *nobj = m_sectionDelegate->beginCreate(context);
728 QQml_setParent_noEvent(context, nobj);
729 sectionItem = qobject_cast<QQuickItem *>(nobj);
733 sectionItem->setZ(2);
734 QQml_setParent_noEvent(sectionItem, m_clipItem);
735 sectionItem->setParentItem(m_clipItem);
740 m_sectionDelegate->completeCreate();
742 // TODO attach to sectionItem so we can accomodate to it changing its height
747bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
749// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
750 // Do not remove items if we are overshooting up or down, since we'll come back
751 // to the "stable" position and delete/create items without any reason
752 if (contentY() < -m_minYExtent) {
754 } else if (contentY() + height() > contentHeight()) {
757 bool changed = false;
759 bool foundVisible = false;
761 int removedItems = 0;
762 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
763 while (i < m_visibleItems.count()) {
764 ListItem *item = m_visibleItems[i];
765 const qreal pos = item->y() + m_clipItem->y();
766// qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
767 if (pos + item->height() < bufferFrom || pos > bufferTo) {
768// qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
770 m_visibleItems.removeAt(i);
776 const int itemIndex = m_firstVisibleIndex + removedItems + i;
777 m_firstVisibleIndex = itemIndex;
783 m_firstVisibleIndex = -1;
785 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
792ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
794// qDebug() << "CREATE ITEM" << modelIndex;
795 if (asynchronous && m_asyncRequestedIndex != -1)
798 m_asyncRequestedIndex = -1;
799#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
800 QQuickItem *item = m_delegateModel->item(modelIndex, asynchronous);
802 QObject* object = m_delegateModel->object(modelIndex, asynchronous);
803 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
806#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
807 m_asyncRequestedIndex = modelIndex;
810 m_delegateModel->release(object);
811 if (!m_delegateValidated) {
812 m_delegateValidated = true;
813 QObject* delegateObj = delegate();
814 qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
817 m_asyncRequestedIndex = modelIndex;
822// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
823 ListItem *listItem = new ListItem;
824 listItem->m_item = item;
825 listItem->m_sectionItem = getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/);
826 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
827 ListItem *prevItem = itemAtIndex(modelIndex - 1);
828 bool lostItem = false; // Is an item that we requested async but because of model changes
829 // it is no longer attached to any of the existing items (has no prev nor next item)
830 // nor is the first item
832 listItem->setY(prevItem->y() + prevItem->height());
834 ListItem *currItem = itemAtIndex(modelIndex);
836 // There's something already in m_visibleItems at out index, meaning this is an insert, so attach to its top
837 listItem->setY(currItem->y() - listItem->height());
839 ListItem *nextItem = itemAtIndex(modelIndex + 1);
841 listItem->setY(nextItem->y() - listItem->height());
842 } else if (modelIndex == 0 && m_headerItem) {
843 listItem->setY(m_headerItem->height());
844 } else if (!m_visibleItems.isEmpty()) {
850 listItem->setCulled(true);
851 releaseItem(listItem);
854 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() <= contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
855 if (m_visibleItems.isEmpty()) {
856 m_visibleItems << listItem;
858 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
860 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
861 m_firstVisibleIndex = modelIndex;
864 if (listItem->m_sectionItem) {
865 QQmlContext *context = QQmlEngine::contextForObject(listItem->m_sectionItem)->parentContext();
866 context->setContextProperty(QLatin1String("delegateIndex"), modelIndex);
869 m_contentHeightDirty = true;
875#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
876void ListViewWithPageHeader::itemCreated(int modelIndex, QQuickItem *item)
879void ListViewWithPageHeader::itemCreated(int modelIndex, QObject *object)
881 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
883 qWarning() << "ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
887// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
888 // Check we are not being taken down and don't paint anything
889 // TODO Check if we still need this in 5.2
890 // For reproduction just inifnite loop testDash or testDashContent
891 if (!QQmlEngine::contextForObject(this)->parentContext())
894 item->setParentItem(m_clipItem);
895 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
896 context->setContextProperty(QLatin1String("ListViewWithPageHeader"), this);
897 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
898 if (modelIndex == m_asyncRequestedIndex) {
899 createItem(modelIndex, false);
904void ListViewWithPageHeader::updateClipItem()
906 m_clipItem->setHeight(height() - m_headerItemShownHeight);
907 m_clipItem->setY(contentY() + m_headerItemShownHeight);
908 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
911void ListViewWithPageHeader::onContentHeightChanged()
916void ListViewWithPageHeader::onContentWidthChanged()
918 m_clipItem->setWidth(contentItem()->width());
921void ListViewWithPageHeader::onHeightChanged()
927#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
928void ListViewWithPageHeader::onModelUpdated(const QQuickChangeSet &changeSet, bool /*reset*/)
930void ListViewWithPageHeader::onModelUpdated(const QQmlChangeSet &changeSet, bool /*reset*/)
933 // TODO Do something with reset
934// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
935 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
937#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
938 Q_FOREACH(const QQuickChangeSet::Remove &remove, changeSet.removes()) {
940 Q_FOREACH(const QQmlChangeSet::Remove &remove, changeSet.removes()) {
942// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove.index << remove.count;
943 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
944 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
945 // If all the items we are removing are either not created or culled
946 // we have to grow down to avoid viewport changing
947 bool growDown = true;
948 for (int i = 0; growDown && i < remove.count; ++i) {
949 const int modelIndex = remove.index + i;
950 ListItem *item = itemAtIndex(modelIndex);
951 if (item && !item->culled()) {
955 for (int i = remove.count - 1; i >= 0; --i) {
956 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
957 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
958 ListItem *item = m_visibleItems[visibleIndex];
959 // Pass the section item down if needed
960 if (item->m_sectionItem && visibleIndex + 1 < m_visibleItems.count()) {
961 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
962 if (!nextItem->m_sectionItem) {
963 nextItem->m_sectionItem = item->m_sectionItem;
964 item->m_sectionItem = nullptr;
968 m_visibleItems.removeAt(visibleIndex);
973 } else if (remove.index <= m_firstVisibleIndex) {
974 if (!m_visibleItems.isEmpty()) {
975 // We removed the first item that is the one that positions the rest
976 // position the new first item correctly
977 m_visibleItems.first()->setY(oldFirstValidIndexPos);
979 m_firstVisibleIndex = -1;
982 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
983 m_firstVisibleIndex -= remove.count;
985 for (int i = remove.count - 1; i >= 0; --i) {
986 const int modelIndex = remove.index + i;
987 if (modelIndex == m_asyncRequestedIndex) {
988 m_asyncRequestedIndex = -1;
989 } else if (modelIndex < m_asyncRequestedIndex) {
990 m_asyncRequestedIndex--;
995#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
996 Q_FOREACH(const QQuickChangeSet::Insert &insert, changeSet.inserts()) {
998 Q_FOREACH(const QQmlChangeSet::Insert &insert, changeSet.inserts()) {
1000// qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
1001 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
1002 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
1003 if (insertingInValidIndexes || firstItemWithViewOnTop)
1005 // If the items we are adding won't be really visible
1006 // we grow up instead of down to not change the viewport
1007 bool growUp = false;
1008 if (!firstItemWithViewOnTop) {
1009 for (int i = 0; i < m_visibleItems.count(); ++i) {
1010 if (!m_visibleItems[i]->culled()) {
1011 if (insert.index <= m_firstVisibleIndex + i) {
1019 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
1020 for (int i = insert.count - 1; i >= 0; --i) {
1021 const int modelIndex = insert.index + i;
1022 ListItem *item = createItem(modelIndex, false);
1024 ListItem *firstItem = m_visibleItems.first();
1025 firstItem->setY(firstItem->y() - item->height());
1027 // Adding an item may break a "same section" chain, so check
1028 // if we need adding a new section item
1029 if (m_sectionDelegate) {
1030 ListItem *nextItem = itemAtIndex(modelIndex + 1);
1031 if (nextItem && !nextItem->m_sectionItem) {
1032 nextItem->m_sectionItem = getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/);
1033 if (growUp && nextItem->m_sectionItem) {
1034 ListItem *firstItem = m_visibleItems.first();
1035 firstItem->setY(firstItem->y() - nextItem->m_sectionItem->height());
1040 if (firstItemWithViewOnTop) {
1041 ListItem *firstItem = m_visibleItems.first();
1042 firstItem->setY(oldFirstValidIndexPos);
1045 } else if (insert.index <= m_firstVisibleIndex) {
1046 m_firstVisibleIndex += insert.count;
1049 for (int i = insert.count - 1; i >= 0; --i) {
1050 const int modelIndex = insert.index + i;
1051 if (modelIndex <= m_asyncRequestedIndex) {
1052 m_asyncRequestedIndex++;
1057#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
1058 Q_FOREACH(const QQuickChangeSet::Change &change, changeSet.changes()) {
1060 Q_FOREACH(const QQmlChangeSet::Change &change, changeSet.changes()) {
1062// qDebug() << "ListViewWithPageHeader::onModelUpdated Change" << change.index << change.count;
1063 for (int i = change.index; i < change.count; ++i) {
1064 ListItem *item = itemAtIndex(i);
1065 if (item && item->m_sectionItem) {
1066 QQmlContext *context = QQmlEngine::contextForObject(item->m_sectionItem)->parentContext();
1067 const QString sectionText = m_delegateModel->stringValue(i, m_sectionProperty);
1068 context->setContextProperty(QLatin1String("section"), sectionText);
1073 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
1077 for (int i = 0; i < m_visibleItems.count(); ++i) {
1078 ListItem *item = m_visibleItems[i];
1079 if (item->m_sectionItem) {
1080 QQmlContext *context = QQmlEngine::contextForObject(item->m_sectionItem)->parentContext();
1081 context->setContextProperty(QLatin1String("delegateIndex"), m_firstVisibleIndex + i);
1087 m_contentHeightDirty = true;
1090void ListViewWithPageHeader::onShowHeaderAnimationFinished()
1092 m_contentHeightDirty = true;
1096void ListViewWithPageHeader::itemGeometryChanged(QQuickItem * /*item*/, const QRectF &newGeometry, const QRectF &oldGeometry)
1098 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
1099 if (heightDiff != 0) {
1100 if (!m_inContentHeightKeepHeaderShown && oldGeometry.y() + oldGeometry.height() + m_clipItem->y() <= contentY() && !m_visibleItems.isEmpty()) {
1101 ListItem *firstItem = m_visibleItems.first();
1102 firstItem->setY(firstItem->y() - heightDiff);
1109 m_contentHeightDirty = true;
1113void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
1115 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
1116 if (m_headerItemShownHeight > 0) {
1117 // If the header is shown because of the clip
1119 m_headerItemShownHeight += heightDiff;
1120 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
1124 if (oldHeaderY + oldHeaderHeight > contentY()) {
1125 // If the header is shown because its position
1127 ListItem *firstItem = m_visibleItems.first();
1128 firstItem->setY(firstItem->y() + heightDiff);
1131 // If the header is not on screen, just change the start of the list
1132 // so the viewport is not changed
1139void ListViewWithPageHeader::adjustMinYExtent()
1141 if (m_visibleItems.isEmpty()) {
1144 qreal nonCreatedHeight = 0;
1145 if (m_firstVisibleIndex != 0) {
1146 // Calculate the average height of items to estimate the position of the list start
1147 const int visibleItems = m_visibleItems.count();
1148 qreal visibleItemsHeight = 0;
1149 Q_FOREACH(ListItem *item, m_visibleItems) {
1150 visibleItemsHeight += item->height();
1152 nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
1153// qDebug() << m_firstVisibleIndex << visibleItemsHeight << visibleItems << nonCreatedHeight;
1155 const qreal headerHeight = (m_headerItem ? m_headerItem->implicitHeight() : 0);
1156 m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + headerHeight;
1157 if (m_minYExtent != 0 && qFuzzyIsNull(m_minYExtent)) {
1159 m_visibleItems.first()->setY(nonCreatedHeight - m_clipItem->y() + headerHeight);
1164ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
1166 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
1167 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
1168 return m_visibleItems[visibleIndexedModelIndex];
1173void ListViewWithPageHeader::layout()
1179 if (!m_visibleItems.isEmpty()) {
1180 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
1181 const qreal visibleTo = contentY() + height() - m_clipItem->y();
1183 qreal pos = m_visibleItems.first()->y();
1185// qDebug() << "ListViewWithPageHeader::layout Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
1186 int firstReallyVisibleItem = -1;
1187 int modelIndex = m_firstVisibleIndex;
1188 Q_FOREACH(ListItem *item, m_visibleItems) {
1189 const bool cull = pos + item->height() <= visibleFrom || pos >= visibleTo;
1190 item->setCulled(cull);
1192 if (!cull && firstReallyVisibleItem == -1) {
1193 firstReallyVisibleItem = modelIndex;
1194 if (m_topSectionItem) {
1195 // Positing the top section sticky item is a two step process
1196 // First we set it either we cull it (because it doesn't need to be sticked to the top)
1197 // or stick it to the top
1198 // Then after the loop we'll make sure that if there's another section just below it
1199 // pushed the sticky section up to make it disappear
1200 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
1201 bool showStickySectionItem;
1202 // We need to show the "top section sticky item" when the position at the "top" of the
1203 // viewport is bigger than the start of the position of the first visible item
1204 // i.e. the first visible item starts before the viewport, or when the first
1205 // visible item starts just at the viewport start and it does not have its own section item
1206 if (topSectionStickPos > pos) {
1207 showStickySectionItem = true;
1208 } else if (topSectionStickPos == pos) {
1209 showStickySectionItem = !item->m_sectionItem;
1211 showStickySectionItem = false;
1213 if (!showStickySectionItem) {
1214 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
1215 if (item->m_sectionItem) {
1216 // This seems it should happen since why would we cull the top section
1217 // if the first visible item has no section header? This only happens briefly
1218 // when increasing the height of a list that is at the bottom, the m_topSectionItem
1219 // gets shown shortly in the next polish call
1220 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(false);
1223 // Update the top sticky section header
1224 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1225 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
1226 context->setContextProperty(QLatin1String("section"), section);
1228 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
1229 m_topSectionItem->setY(topSectionStickPos);
1230 int delegateIndex = modelIndex;
1231 // Look for the first index with this section text
1232 while (delegateIndex > 0) {
1233 const QString prevSection = m_delegateModel->stringValue(delegateIndex - 1, m_sectionProperty);
1234 if (prevSection != section)
1238 context->setContextProperty(QLatin1String("delegateIndex"), delegateIndex);
1239 if (item->m_sectionItem) {
1240 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(true);
1245 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
1246 const qreal clipFrom = visibleFrom + (!item->m_sectionItem && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
1247 if (!cull && pos < clipFrom) {
1248 context->setContextProperty(QLatin1String("heightToClip"), clipFrom - pos);
1250 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
1252// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
1253 pos += item->height();
1257 // Second step of section sticky item positioning
1258 // Look at the next section header, check if it's pushing up the sticky one
1259 if (m_topSectionItem) {
1260 if (firstReallyVisibleItem >= 0) {
1261 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
1262 ListItem *item = m_visibleItems[i];
1263 if (item->m_sectionItem) {
1264 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
1265 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
1276void ListViewWithPageHeader::updatePolish()
1278 // Check we are not being taken down and don't paint anything
1279 // TODO Check if we still need this in 5.2
1280 // For reproduction just inifnite loop testDash or testDashContent
1281 if (!QQmlEngine::contextForObject(this)->parentContext())
1284 Q_FOREACH(ListItem *item, m_itemsToRelease)
1285 reallyReleaseItem(item);
1286 m_itemsToRelease.clear();
1295 if (m_contentHeightDirty) {
1296 qreal contentHeight;
1297 if (m_visibleItems.isEmpty()) {
1298 contentHeight = m_headerItem ? m_headerItem->height() : 0;
1300 const int modelCount = model()->rowCount();
1301 const int visibleItems = m_visibleItems.count();
1302 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
1303 qreal nonCreatedHeight = 0;
1304 if (lastValidIndex != modelCount - 1) {
1305 const int visibleItems = m_visibleItems.count();
1306 qreal visibleItemsHeight = 0;
1307 Q_FOREACH(ListItem *item, m_visibleItems) {
1308 visibleItemsHeight += item->height();
1310 const int unknownSizes = modelCount - (m_firstVisibleIndex + visibleItems);
1311 nonCreatedHeight = unknownSizes * visibleItemsHeight / visibleItems;
1313 ListItem *item = m_visibleItems.last();
1314 contentHeight = nonCreatedHeight + item->y() + item->height() + m_clipItem->y();
1315 if (m_firstVisibleIndex != 0) {
1316 // Make sure that if we are shrinking we tell the view we still fit
1317 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
1321 m_contentHeightDirty = false;
1323 m_inContentHeightKeepHeaderShown = m_headerItem && m_headerItem->y() == contentY();
1324 setContentHeight(contentHeight);
1325 m_inContentHeightKeepHeaderShown = false;
1329#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