2 * Copyright (C) 2013 Canonical, Ltd.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * Some documentation on how this thing works:
20 * A flickable has two very important concepts that define the top and
21 * height of the flickable area.
22 * The top is returned in minYExtent()
23 * The height is set using setContentHeight()
24 * By changing those two values we can make the list grow up or down
25 * as needed. e.g. if we are in the middle of the list
26 * and something that is above the viewport grows, since we do not
27 * want to change the viewport because of that we just adjust the
28 * minYExtent so that the list grows up.
30 * The implementation on the list relies on the delegateModel doing
31 * most of the instantiation work. You call createItem() when you
32 * need to create an item asking for it async or not. If returns null
33 * it means the item will be created async and the model will call the
34 * itemCreated slot with the item.
36 * updatePolish is the central point of dispatch for the work of the
37 * class. It is called by the scene graph just before drawing the class.
39 * * Make sure all items are positioned correctly
40 * * Add/Remove items if needed
41 * * Update the content height if it was dirty
43 * m_visibleItems contains all the items we have created at the moment.
44 * Actually not all of them are visible since it includes the ones
45 * in the cache area we create asynchronously to help performance.
46 * The first item in m_visibleItems has the m_firstVisibleIndex in
47 * the model. If you actually want to know what is the first
48 * item in the viewport you have to find the first non culled element
51 * All the items (except the header) are childs of m_clipItem which
52 * is a child of the contentItem() of the flickable (The contentItem()
53 * is what actually 'moves' in a a flickable). This way
54 * we can implement the clipping needed so we can have the header
55 * shown in the middle of the list over the items without the items
56 * leaking under the header in case it is transparent.
58 * The first item of m_visibleItems is the one that defines the
59 * positions of all the rest of items (see updatePolish()) and
60 * this is why sometimes we move it even if it's not the item
61 * that has triggered the function (i.e. in itemGeometryChanged())
63 * m_visibleItems is a list of ListItem. Each ListItem
64 * will contain a item and potentially a sectionItem. The sectionItem
65 * is only there when the list is using sectionDelegate+sectionProperty
66 * and this is the first item of the section. Each ListItem is vertically
67 * layouted with the sectionItem first and then the item.
69 * For sectioning we also have a section item alone (m_topSectionItem)
70 * that is used for the cases we need to show the sticky section item at
71 * the top of the view.
73 * Each delegate item has a context property called heightToClip that is
74 * used to communicate to the delegate implementation in case it has to
75 * clip itself because of overlapping with the top sticky section item.
76 * This is an implementation decision since it has been agreed it
77 * is easier to implement the clipping in QML with this info than to
78 * do it at the C++ level.
80 * Note that minYExtent and height are not always totally accurate, since
81 * we don't have the items created we can't guess their heights
82 * so we can only guarantee the values are correct when the first/last
83 * items of the list are visible, otherwise we just live with good enough
84 * values that make the list scrollable
86 * There are a few things that are not really implemented or tested properly
87 * which we don't use at the moment like changing the model, changing
88 * the section delegate, having a section delegate that changes its size, etc.
89 * The known missing features are marked with TODOs along the code.
92#include "listviewwithpageheader.h"
96#include <qqmlengine.h>
97#include <private/qquickvisualdatamodel_p.h>
98#include <private/qqmlglobal_p.h>
99#include <private/qquickitem_p.h>
100#include <private/qquickanimation_p.h>
101// #include <private/qquickrectangle_p.h>
103qreal ListViewWithPageHeader::ListItem::height() const
105 return m_item->height() + (m_sectionItem ? m_sectionItem->height() : 0);
108qreal ListViewWithPageHeader::ListItem::y() const
110 return m_item->y() - (m_sectionItem ? m_sectionItem->height() : 0);
113void ListViewWithPageHeader::ListItem::setY(qreal newY)
116 m_sectionItem->setY(newY);
117 m_item->setY(newY + m_sectionItem->height());
123bool ListViewWithPageHeader::ListItem::culled() const
125 return QQuickItemPrivate::get(m_item)->culled;
128void ListViewWithPageHeader::ListItem::setCulled(bool culled)
130 QQuickItemPrivate::get(m_item)->setCulled(culled);
132 QQuickItemPrivate::get(m_sectionItem)->setCulled(culled);
135ListViewWithPageHeader::ListViewWithPageHeader()
136 : m_delegateModel(nullptr)
137 , m_asyncRequestedIndex(-1)
138 , m_delegateValidated(false)
139 , m_firstVisibleIndex(-1)
141 , m_contentHeightDirty(false)
142 , m_headerItem(nullptr)
143 , m_previousContentY(0)
144 , m_headerItemShownHeight(0)
145 , m_sectionDelegate(nullptr)
146 , m_topSectionItem(nullptr)
147 , m_forceNoClip(false)
149 m_clipItem = new QQuickItem(contentItem());
150// m_clipItem = new QQuickRectangle(contentItem());
151// ((QQuickRectangle*)m_clipItem)->setColor(Qt::gray);
153 m_headerShowAnimation = new QQuickNumberAnimation(this);
154 m_headerShowAnimation->setEasing(QEasingCurve::OutQuad);
155 m_headerShowAnimation->setProperty("contentY");
156 m_headerShowAnimation->setDuration(200);
157 m_headerShowAnimation->setTargetObject(this);
159 connect(this, SIGNAL(contentWidthChanged()), this, SLOT(onContentWidthChanged()));
160 connect(this, SIGNAL(contentHeightChanged()), this, SLOT(onContentHeightChanged()));
161 connect(this, SIGNAL(heightChanged()), this, SLOT(onHeightChanged()));
164ListViewWithPageHeader::~ListViewWithPageHeader()
168QAbstractItemModel *ListViewWithPageHeader::model() const
170 return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
173void ListViewWithPageHeader::setModel(QAbstractItemModel *model)
175 if (model != this->model()) {
176 if (!m_delegateModel) {
177 createDelegateModel();
179 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
181 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
182 connect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
183 Q_EMIT modelChanged();
185// Q_EMIT contentHeightChanged();
186// Q_EMIT contentYChanged();
190QQmlComponent *ListViewWithPageHeader::delegate() const
192 return m_delegateModel ? m_delegateModel->delegate() : nullptr;
195void ListViewWithPageHeader::setDelegate(QQmlComponent *delegate)
197 if (delegate != this->delegate()) {
198 if (!m_delegateModel) {
199 createDelegateModel();
202 // Cleanup the existing items
203 foreach(ListItem *item, m_visibleItems)
205 m_visibleItems.clear();
206 m_firstVisibleIndex = -1;
210 if (m_topSectionItem) {
211 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
214 m_delegateModel->setDelegate(delegate);
216 Q_EMIT delegateChanged();
217 m_delegateValidated = false;
218 m_contentHeightDirty = true;
223QQuickItem *ListViewWithPageHeader::header() const
228void ListViewWithPageHeader::setHeader(QQuickItem *headerItem)
230 if (m_headerItem != headerItem) {
231 qreal oldHeaderHeight = 0;
232 qreal oldHeaderY = 0;
234 oldHeaderHeight = m_headerItem->height();
235 oldHeaderY = m_headerItem->y();
236 m_headerItem->setParentItem(nullptr);
238 m_headerItem = headerItem;
240 m_headerItem->setParentItem(contentItem());
241 m_headerItem->setZ(1);
243 qreal newHeaderHeight = m_headerItem ? m_headerItem->height() : 0;
244 if (!m_visibleItems.isEmpty() && newHeaderHeight != oldHeaderHeight) {
245 headerHeightChanged(newHeaderHeight, oldHeaderHeight, oldHeaderY);
247 m_contentHeightDirty = true;
249 Q_EMIT headerChanged();
253QQmlComponent *ListViewWithPageHeader::sectionDelegate() const
255 return m_sectionDelegate;
258void ListViewWithPageHeader::setSectionDelegate(QQmlComponent *delegate)
260 if (delegate != m_sectionDelegate) {
261 // TODO clean existing sections
263 m_sectionDelegate = delegate;
265 m_topSectionItem = getSectionItem(QString());
266 m_topSectionItem->setZ(3);
267 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
269 // TODO create sections for existing items
271 Q_EMIT sectionDelegateChanged();
275QString ListViewWithPageHeader::sectionProperty() const
277 return m_sectionProperty;
280void ListViewWithPageHeader::setSectionProperty(const QString &property)
282 if (property != m_sectionProperty) {
283 m_sectionProperty = property;
285 updateWatchedRoles();
287 // TODO recreate sections
289 Q_EMIT sectionPropertyChanged();
293bool ListViewWithPageHeader::forceNoClip() const
295 return m_forceNoClip;
298void ListViewWithPageHeader::setForceNoClip(bool noClip)
300 if (noClip != m_forceNoClip) {
301 m_forceNoClip = noClip;
303 Q_EMIT forceNoClipChanged();
307void ListViewWithPageHeader::positionAtBeginning()
309 qreal headerHeight = (m_headerItem ? m_headerItem->height() : 0);
310 if (m_firstVisibleIndex != 0) {
311 // This could be optimized by trying to reuse the interesection
312 // of items that may end up intersecting between the existing
313 // m_visibleItems and the items we are creating in the next loop
314 foreach(ListItem *item, m_visibleItems)
316 m_visibleItems.clear();
317 m_firstVisibleIndex = -1;
319 // Create the item 0, it will be already correctly positioned at createItem()
321 ListItem *item = createItem(0, false);
322 // Create the subsequent items
324 qreal pos = item->y() + item->height();
325 const qreal buffer = height() / 2;
326 const qreal bufferTo = height() + buffer;
327 while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
328 if (!(item = createItem(modelIndex, false)))
330 pos += item->height();
334 m_previousContentY = m_visibleItems.first()->y() - headerHeight;
336 setContentY(m_visibleItems.first()->y() + m_clipItem->y() - headerHeight);
339void ListViewWithPageHeader::showHeader()
341 auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
342 if (to != contentY()) {
343 m_headerShowAnimation->setTo(to);
344 m_headerShowAnimation->start();
348qreal ListViewWithPageHeader::minYExtent() const
350// qDebug() << "ListViewWithPageHeader::minYExtent" << m_minYExtent;
354void ListViewWithPageHeader::componentComplete()
357 m_delegateModel->componentComplete();
359 QQuickFlickable::componentComplete();
364void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
366 QQuickFlickable::viewportMoved(orient);
367// qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
368 qreal diff = m_previousContentY - contentY();
370 auto oldHeaderItemShownHeight = m_headerItemShownHeight;
371 if (contentY() < -m_minYExtent) {
372 // Stick the header item to the top when dragging down
373 m_headerItem->setY(contentY());
374 m_headerItem->setHeight(m_headerItem->implicitHeight() + (-m_minYExtent - contentY()));
376 m_headerItem->setHeight(m_headerItem->implicitHeight());
377 // We are going down (but it's not because of the rebound at the end)
378 // (but the header was not shown by it's own position)
379 // or the header is partially shown
380 const bool scrolledUp = m_previousContentY > contentY();
381 const bool notRebounding = contentY() + height() < contentHeight();
382 const bool notShownByItsOwn = contentY() + diff > m_headerItem->y() + m_headerItem->height();
384 if (!scrolledUp && contentY() == -m_minYExtent) {
385 m_headerItemShownHeight = 0;
386 m_headerItem->setY(contentY());
387 } else if ((scrolledUp && notRebounding && notShownByItsOwn) || (m_headerItemShownHeight > 0)) {
388 m_headerItemShownHeight += diff;
389 if (contentY() == -m_minYExtent) {
390 m_headerItemShownHeight = 0;
392 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
394 if (m_headerItemShownHeight > 0) {
395 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
397 m_headerItem->setY(-m_minYExtent);
401 // We will be changing the clip item, need to accomadate for it
402 // otherwise we move the firstItem down/up twice (unless the
403 // show header animation is running, where we want to keep the viewport stable)
404 if (!m_headerShowAnimation->isRunning()) {
405 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
408 m_contentHeightDirty = true;
411 if (!m_visibleItems.isEmpty()) {
413 ListItem *firstItem = m_visibleItems.first();
414 firstItem->setY(firstItem->y() + diff);
415 if (m_headerShowAnimation->isRunning()) {
420 m_previousContentY = contentY();
425void ListViewWithPageHeader::createDelegateModel()
427 m_delegateModel = new QQuickVisualDataModel(qmlContext(this), this);
428 connect(m_delegateModel, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(itemCreated(int,QQuickItem*)));
429 if (isComponentComplete())
430 m_delegateModel->componentComplete();
431 updateWatchedRoles();
434void ListViewWithPageHeader::refill()
436 if (!isComponentComplete()) {
440 const qreal buffer = height() / 2;
441 const qreal from = contentY();
442 const qreal to = from + height();
443 const qreal bufferFrom = from - buffer;
444 const qreal bufferTo = to + buffer;
446 bool added = addVisibleItems(from, to, false);
447 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
448 added |= addVisibleItems(bufferFrom, bufferTo, true);
450 if (added || removed) {
451 m_contentHeightDirty = true;
455bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
460 if (m_delegateModel->count() == 0)
464// qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
468 if (!m_visibleItems.isEmpty()) {
469 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
470 item = m_visibleItems.last();
471 pos = item->y() + item->height() + m_clipItem->y();
473 bool changed = false;
474// qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
475 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
476// qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
477 if (!(item = createItem(modelIndex, asynchronous)))
479 pos += item->height();
486 if (!m_visibleItems.isEmpty()) {
487 modelIndex = m_firstVisibleIndex - 1;
488 item = m_visibleItems.first();
489 pos = item->y() + m_clipItem->y();
491 while (modelIndex >= 0 && pos > fillFrom) {
492// qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
493 if (!(item = createItem(modelIndex, asynchronous)))
495 pos -= item->height();
503void ListViewWithPageHeader::reallyReleaseItem(ListItem *listItem)
505 QQuickItem *item = listItem->m_item;
506 QQuickVisualModel::ReleaseFlags flags = m_delegateModel->release(item);
507 if (flags & QQuickVisualModel::Destroyed) {
508 item->setParentItem(nullptr);
510 delete listItem->m_sectionItem;
514void ListViewWithPageHeader::releaseItem(ListItem *listItem)
516 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(listItem->m_item);
517 itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
518 m_itemsToRelease << listItem;
521void ListViewWithPageHeader::updateWatchedRoles()
523 if (m_delegateModel) {
524 QList<QByteArray> roles;
525 if (!m_sectionProperty.isEmpty())
526 roles << m_sectionProperty.toUtf8();
527 m_delegateModel->setWatchedRoles(roles);
531QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
533 if (!m_sectionDelegate)
536 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
537 if (modelIndex > 0) {
538 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
539 if (section == prevSection)
542 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
543 // Already inserted items can't steal next section header
544 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
545 if (section == nextSection) {
546 // Steal the section header
547 ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
549 QQuickItem *sectionItem = nextItem->m_sectionItem;
550 nextItem->m_sectionItem = nullptr;
556 return getSectionItem(section);
559QQuickItem *ListViewWithPageHeader::getSectionItem(const QString §ionText)
561 QQuickItem *sectionItem = nullptr;
563 QQmlContext *creationContext = m_sectionDelegate->creationContext();
564 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
565 context->setContextProperty(QLatin1String("section"), sectionText);
566 QObject *nobj = m_sectionDelegate->beginCreate(context);
568 QQml_setParent_noEvent(context, nobj);
569 sectionItem = qobject_cast<QQuickItem *>(nobj);
573 sectionItem->setZ(2);
574 QQml_setParent_noEvent(sectionItem, m_clipItem);
575 sectionItem->setParentItem(m_clipItem);
580 m_sectionDelegate->completeCreate();
582 // TODO attach to sectionItem so we can accomodate to it changing its height
587bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
589// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
590 // Do not remove items if we are overshooting up or down, since we'll come back
591 // to the "stable" position and delete/create items without any reason
592 if (contentY() < -m_minYExtent) {
594 } else if (contentY() + height() > contentHeight()) {
597 bool changed = false;
599 bool foundVisible = false;
601 int removedItems = 0;
602 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
603 while (i < m_visibleItems.count()) {
604 ListItem *item = m_visibleItems[i];
605 const qreal pos = item->y() + m_clipItem->y();
606// qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
607 if (pos + item->height() < bufferFrom || pos > bufferTo) {
608// qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
610 m_visibleItems.removeAt(i);
616 const int itemIndex = m_firstVisibleIndex + removedItems + i;
617 m_firstVisibleIndex = itemIndex;
622 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
629ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
631// qDebug() << "CREATE ITEM" << modelIndex;
632 if (asynchronous && m_asyncRequestedIndex != -1)
635 m_asyncRequestedIndex = -1;
636 QQuickItem *item = m_delegateModel->item(modelIndex, asynchronous);
638 m_asyncRequestedIndex = modelIndex;
641// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
642 ListItem *listItem = new ListItem;
643 listItem->m_item = item;
644 listItem->m_sectionItem = getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/);
645 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
646 ListItem *prevItem = itemAtIndex(modelIndex - 1);
647 bool lostItem = false; // Is an item that we requested async but because of model changes
648 // it is no longer attached to any of the existing items (has no prev nor next item)
649 // nor is the first item
651 listItem->setY(prevItem->y() + prevItem->height());
653 ListItem *currItem = itemAtIndex(modelIndex);
655 // There's something already in m_visibleItems at out index, meaning this is an insert, so attach to its top
656 listItem->setY(currItem->y() - listItem->height());
658 ListItem *nextItem = itemAtIndex(modelIndex + 1);
660 listItem->setY(nextItem->y() - listItem->height());
661 } else if (modelIndex == 0 && m_headerItem) {
662 listItem->setY(m_headerItem->height());
663 } else if (!m_visibleItems.isEmpty()) {
669 releaseItem(listItem);
672 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() < contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
673 if (m_visibleItems.isEmpty()) {
674 m_visibleItems << listItem;
676 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
678 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
679 m_firstVisibleIndex = modelIndex;
688void ListViewWithPageHeader::itemCreated(int modelIndex, QQuickItem *item)
690// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
692 item->setParentItem(m_clipItem);
693 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
694 context->setContextProperty(QLatin1String("ListViewWithPageHeader"), this);
695 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
696 if (modelIndex == m_asyncRequestedIndex) {
697 createItem(modelIndex, false);
702void ListViewWithPageHeader::updateClipItem()
704 m_clipItem->setHeight(height() - m_headerItemShownHeight);
705 m_clipItem->setY(contentY() + m_headerItemShownHeight);
706 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
709void ListViewWithPageHeader::onContentHeightChanged()
714void ListViewWithPageHeader::onContentWidthChanged()
716 m_clipItem->setWidth(contentItem()->width());
719void ListViewWithPageHeader::onHeightChanged()
721 setMaximumFlickVelocity(height() * 10);
722 setFlickDeceleration(height() * 2);
726void ListViewWithPageHeader::onModelUpdated(const QQuickChangeSet &changeSet, bool /*reset*/)
728 // TODO Do something with reset
729// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
730 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
732 foreach(const QQuickChangeSet::Remove &remove, changeSet.removes()) {
733// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove.index << remove.count;
734 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
735 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
736 // If all the items we are removing are either not created or culled
737 // we have to grow down to avoid viewport changing
738 bool growDown = true;
739 for (int i = 0; growDown && i < remove.count; ++i) {
740 const int modelIndex = remove.index + i;
741 ListItem *item = itemAtIndex(modelIndex);
742 if (item && !item->culled()) {
746 for (int i = remove.count - 1; i >= 0; --i) {
747 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
748 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
749 ListItem *item = m_visibleItems[visibleIndex];
750 // Pass the section item down if needed
751 if (item->m_sectionItem && visibleIndex + 1 < m_visibleItems.count()) {
752 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
753 if (!nextItem->m_sectionItem) {
754 nextItem->m_sectionItem = item->m_sectionItem;
755 item->m_sectionItem = nullptr;
759 m_visibleItems.removeAt(visibleIndex);
764 } else if (remove.index <= m_firstVisibleIndex && !m_visibleItems.isEmpty()) {
765 // We removed the first item that is the one that positions the rest
766 // position the new first item correctly
767 m_visibleItems.first()->setY(oldFirstValidIndexPos);
769 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
770 m_firstVisibleIndex -= remove.count;
772 for (int i = remove.count - 1; i >= 0; --i) {
773 const int modelIndex = remove.index + i;
774 if (modelIndex == m_asyncRequestedIndex) {
775 m_asyncRequestedIndex = -1;
776 } else if (modelIndex < m_asyncRequestedIndex) {
777 m_asyncRequestedIndex--;
782 foreach(const QQuickChangeSet::Insert &insert, changeSet.inserts()) {
783// qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
784 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
785 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
786 if (insertingInValidIndexes || firstItemWithViewOnTop)
788 // If the items we are adding won't be really visible
789 // we grow up instead of down to not change the viewport
791 if (!firstItemWithViewOnTop) {
792 for (int i = 0; i < m_visibleItems.count(); ++i) {
793 if (!m_visibleItems[i]->culled()) {
794 if (insert.index <= m_firstVisibleIndex + i) {
802 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
803 for (int i = insert.count - 1; i >= 0; --i) {
804 const int modelIndex = insert.index + i;
805 ListItem *item = createItem(modelIndex, false);
807 ListItem *firstItem = m_visibleItems.first();
808 firstItem->setY(firstItem->y() - item->height());
811 // Adding an item may break a "same section" chain, so check
812 // if we need adding a new section item
813 if (m_sectionDelegate) {
814 ListItem *nextItem = itemAtIndex(modelIndex + 1);
815 if (nextItem && !nextItem->m_sectionItem) {
816 nextItem->m_sectionItem = getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/);
817 if (growUp && nextItem->m_sectionItem) {
818 ListItem *firstItem = m_visibleItems.first();
819 firstItem->setY(firstItem->y() - nextItem->m_sectionItem->height());
825 if (firstItemWithViewOnTop) {
826 ListItem *firstItem = m_visibleItems.first();
827 firstItem->setY(oldFirstValidIndexPos);
829 } else if (insert.index <= m_firstVisibleIndex) {
830 m_firstVisibleIndex += insert.count;
833 for (int i = insert.count - 1; i >= 0; --i) {
834 const int modelIndex = insert.index + i;
835 if (modelIndex <= m_asyncRequestedIndex) {
836 m_asyncRequestedIndex++;
841 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
847 m_contentHeightDirty = true;
850void ListViewWithPageHeader::itemGeometryChanged(QQuickItem * /*item*/, const QRectF &newGeometry, const QRectF &oldGeometry)
852 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
853 if (heightDiff != 0) {
854 if (oldGeometry.y() + oldGeometry.height() + m_clipItem->y() < contentY() && !m_visibleItems.isEmpty()) {
855 ListItem *firstItem = m_visibleItems.first();
856 firstItem->setY(firstItem->y() - heightDiff);
863 m_contentHeightDirty = true;
867void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
869 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
870 if (m_headerItemShownHeight > 0) {
871 // If the header is shown because of the clip
873 m_headerItemShownHeight += heightDiff;
874 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
878 if (oldHeaderY + oldHeaderHeight > contentY()) {
879 // If the header is shown because its position
881 ListItem *firstItem = m_visibleItems.first();
882 firstItem->setY(firstItem->y() + heightDiff);
885 // If the header is not on screen, just change the start of the list
886 // so the viewport is not changed
893void ListViewWithPageHeader::adjustMinYExtent()
895 if (m_visibleItems.isEmpty()) {
898 qreal nonCreatedHeight = 0;
899 if (m_firstVisibleIndex != 0) {
900 // Calculate the average height of items to estimate the position of the list start
901 const int visibleItems = m_visibleItems.count();
902 qreal visibleItemsHeight = 0;
903 foreach(ListItem *item, m_visibleItems) {
904 visibleItemsHeight += item->height();
906 nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
907// qDebug() << m_firstVisibleIndex << visibleItemsHeight << visibleItems << nonCreatedHeight;
909 m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + (m_headerItem ? m_headerItem->implicitHeight() : 0);
913ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
915 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
916 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
917 return m_visibleItems[visibleIndexedModelIndex];
922void ListViewWithPageHeader::layout()
924 if (!m_visibleItems.isEmpty()) {
925 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
926 const qreal visibleTo = contentY() + height() - m_clipItem->y();
928 qreal pos = m_visibleItems.first()->y();
930// qDebug() << "ListViewWithPageHeader::layout Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
931 int firstReallyVisibleItem = -1;
932 int modelIndex = m_firstVisibleIndex;
933 foreach(ListItem *item, m_visibleItems) {
934 const bool cull = pos + item->height() < visibleFrom || pos >= visibleTo;
935 item->setCulled(cull);
937 if (!cull && firstReallyVisibleItem == -1) {
938 firstReallyVisibleItem = modelIndex;
939 if (m_topSectionItem) {
940 // Update the top sticky section header
941 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
942 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
943 context->setContextProperty(QLatin1String("section"), section);
945 // Positing the top section sticky item is a two step process
946 // First we set it either we cull it (because it doesn't need to be sticked to the top)
947 // or stick it to the top
948 // Then after the loop we'll make sure that if there's another section just below it
949 // pushed the sticky section up to make it disappear
950 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
951 if (topSectionStickPos <= pos) {
952 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
953 if (item->m_sectionItem) {
954 // This seems it should happen since why would we cull the top section
955 // if the first visible item has no section header? This only happens briefly
956 // when increasing the height of a list that is at the bottom, the m_topSectionItem
957 // gets shown shortly in the next polish call
958 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(false);
961 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
962 m_topSectionItem->setY(topSectionStickPos);
963 if (item->m_sectionItem) {
964 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(true);
969 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
970 if (!cull && pos < visibleFrom) {
971 context->setContextProperty(QLatin1String("heightToClip"), visibleFrom - pos);
973 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
975// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
976 pos += item->height();
980 // Second step of section sticky item positioning
981 // Look at the next section header, check if it's pushing up the sticky one
982 if (m_topSectionItem) {
983 if (firstReallyVisibleItem >= 0) {
984 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
985 ListItem *item = m_visibleItems[i];
986 if (item->m_sectionItem) {
987 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
988 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
998void ListViewWithPageHeader::updatePolish()
1000 Q_FOREACH(ListItem *item, m_itemsToRelease)
1001 reallyReleaseItem(item);
1002 m_itemsToRelease.clear();
1011 if (m_contentHeightDirty) {
1012 qreal contentHeight;
1013 if (m_visibleItems.isEmpty()) {
1014 contentHeight = m_headerItem ? m_headerItem->height() : 0;
1016 const int modelCount = model()->rowCount();
1017 const int visibleItems = m_visibleItems.count();
1018 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
1019 qreal nonCreatedHeight = 0;
1020 if (lastValidIndex != modelCount - 1) {
1021 const int visibleItems = m_visibleItems.count();
1022 qreal visibleItemsHeight = 0;
1023 foreach(ListItem *item, m_visibleItems) {
1024 visibleItemsHeight += item->height();
1026 const int unknownSizes = modelCount - (m_firstVisibleIndex + visibleItems);
1027 nonCreatedHeight = unknownSizes * visibleItemsHeight / visibleItems;
1029 ListItem *item = m_visibleItems.last();
1030 contentHeight = nonCreatedHeight + item->y() + item->height() + m_clipItem->y();
1031 if (m_firstVisibleIndex != 0) {
1032 // Make sure that if we are shrinking we tell the view we still fit
1033 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
1037 m_contentHeightDirty = false;
1038 setContentHeight(contentHeight);
1042#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