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 * There are a few things that are not really implemented or tested properly
74 * which we don't use at the moment like changing the model, changing
75 * the section delegate, having a section delegate that changes its size, etc.
76 * The known missing features are marked with TODOs along the code.
79#include "listviewwithpageheader.h"
83#include <qqmlengine.h>
84#include <private/qquickvisualdatamodel_p.h>
85#include <private/qqmlglobal_p.h>
86#include <private/qquickitem_p.h>
87#include <private/qquickanimation_p.h>
88// #include <private/qquickrectangle_p.h>
90qreal ListViewWithPageHeader::ListItem::height() const
92 return m_item->height() + (m_sectionItem ? m_sectionItem->height() : 0);
95qreal ListViewWithPageHeader::ListItem::y() const
97 return m_item->y() - (m_sectionItem ? m_sectionItem->height() : 0);
100void ListViewWithPageHeader::ListItem::setY(qreal newY)
103 m_sectionItem->setY(newY);
104 m_item->setY(newY + m_sectionItem->height());
110bool ListViewWithPageHeader::ListItem::culled() const
112 return QQuickItemPrivate::get(m_item)->culled;
115void ListViewWithPageHeader::ListItem::setCulled(bool culled)
117 QQuickItemPrivate::get(m_item)->setCulled(culled);
119 QQuickItemPrivate::get(m_sectionItem)->setCulled(culled);
122ListViewWithPageHeader::ListViewWithPageHeader()
123 : m_delegateModel(nullptr)
124 , m_asyncRequestedIndex(-1)
125 , m_delegateValidated(false)
126 , m_firstVisibleIndex(-1)
128 , m_contentHeightDirty(false)
129 , m_headerItem(nullptr)
130 , m_previousContentY(0)
131 , m_headerItemShownHeight(0)
132 , m_sectionDelegate(nullptr)
133 , m_topSectionItem(nullptr)
134 , m_forceNoClip(false)
136 m_clipItem = new QQuickItem(contentItem());
137// m_clipItem = new QQuickRectangle(contentItem());
138// ((QQuickRectangle*)m_clipItem)->setColor(Qt::gray);
140 m_headerShowAnimation = new QQuickNumberAnimation(this);
141 m_headerShowAnimation->setEasing(QEasingCurve::OutQuad);
142 m_headerShowAnimation->setProperty("contentY");
143 m_headerShowAnimation->setDuration(200);
144 m_headerShowAnimation->setTargetObject(this);
146 connect(this, SIGNAL(contentWidthChanged()), this, SLOT(onContentWidthChanged()));
147 connect(this, SIGNAL(contentHeightChanged()), this, SLOT(onContentHeightChanged()));
148 connect(this, SIGNAL(heightChanged()), this, SLOT(onHeightChanged()));
151ListViewWithPageHeader::~ListViewWithPageHeader()
155QAbstractItemModel *ListViewWithPageHeader::model() const
157 return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
160void ListViewWithPageHeader::setModel(QAbstractItemModel *model)
162 if (model != this->model()) {
163 if (!m_delegateModel) {
164 createDelegateModel();
166 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
168 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
169 connect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
170 Q_EMIT modelChanged();
172// Q_EMIT contentHeightChanged();
173// Q_EMIT contentYChanged();
177QQmlComponent *ListViewWithPageHeader::delegate() const
179 return m_delegateModel ? m_delegateModel->delegate() : nullptr;
182void ListViewWithPageHeader::setDelegate(QQmlComponent *delegate)
184 if (delegate != this->delegate()) {
185 if (!m_delegateModel) {
186 createDelegateModel();
189 // Cleanup the existing items
190 foreach(ListItem *item, m_visibleItems)
192 m_visibleItems.clear();
193 m_firstVisibleIndex = -1;
197 m_delegateModel->setDelegate(delegate);
199 Q_EMIT delegateChanged();
200 m_delegateValidated = false;
201 m_contentHeightDirty = true;
206QQuickItem *ListViewWithPageHeader::header() const
211void ListViewWithPageHeader::setHeader(QQuickItem *headerItem)
213 if (m_headerItem != headerItem) {
214 qreal oldHeaderHeight = 0;
215 qreal oldHeaderY = 0;
217 oldHeaderHeight = m_headerItem->height();
218 oldHeaderY = m_headerItem->y();
219 QQuickItemPrivate::get(m_headerItem)->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
220 m_headerItem->setParentItem(nullptr);
222 m_headerItem = headerItem;
224 m_headerItem->setParentItem(contentItem());
225 m_headerItem->setZ(1);
226 QQuickItemPrivate::get(m_headerItem)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
228 qreal newHeaderHeight = m_headerItem ? m_headerItem->height() : 0;
229 if (!m_visibleItems.isEmpty() && newHeaderHeight != oldHeaderHeight) {
230 headerHeightChanged(newHeaderHeight, oldHeaderHeight, oldHeaderY);
232 m_contentHeightDirty = true;
234 Q_EMIT headerChanged();
238QQmlComponent *ListViewWithPageHeader::sectionDelegate() const
240 return m_sectionDelegate;
243void ListViewWithPageHeader::setSectionDelegate(QQmlComponent *delegate)
245 if (delegate != m_sectionDelegate) {
246 // TODO clean existing sections
248 m_sectionDelegate = delegate;
250 m_topSectionItem = getSectionItem(QString());
251 m_topSectionItem->setZ(3);
252 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
254 // TODO create sections for existing items
256 Q_EMIT sectionDelegateChanged();
260QString ListViewWithPageHeader::sectionProperty() const
262 return m_sectionProperty;
265void ListViewWithPageHeader::setSectionProperty(const QString &property)
267 if (property != m_sectionProperty) {
268 m_sectionProperty = property;
270 updateWatchedRoles();
272 // TODO recreate sections
274 Q_EMIT sectionPropertyChanged();
278bool ListViewWithPageHeader::forceNoClip() const
280 return m_forceNoClip;
283void ListViewWithPageHeader::setForceNoClip(bool noClip)
285 if (noClip != m_forceNoClip) {
286 m_forceNoClip = noClip;
288 Q_EMIT forceNoClipChanged();
292void ListViewWithPageHeader::positionAtBeginning()
294 qreal headerHeight = (m_headerItem ? m_headerItem->height() : 0);
295 if (m_firstVisibleIndex != 0) {
296 // This could be optimized by trying to reuse the interesection
297 // of items that may end up intersecting between the existing
298 // m_visibleItems and the items we are creating in the next loop
299 foreach(ListItem *item, m_visibleItems)
301 m_visibleItems.clear();
302 m_firstVisibleIndex = -1;
304 // Create the item 0, it will be already correctly positioned at createItem()
306 ListItem *item = createItem(0, false);
307 // Create the subsequent items
309 qreal pos = item->y() + item->height();
310 const qreal buffer = height() / 2;
311 const qreal bufferTo = height() + buffer;
312 while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
313 if (!(item = createItem(modelIndex, false)))
315 pos += item->height();
319 m_previousContentY = m_visibleItems.first()->y() - headerHeight;
321 setContentY(m_visibleItems.first()->y() + m_clipItem->y() - headerHeight);
324void ListViewWithPageHeader::showHeader()
326 auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
327 if (to != contentY()) {
328 m_headerShowAnimation->setTo(to);
329 m_headerShowAnimation->start();
333qreal ListViewWithPageHeader::minYExtent() const
335// qDebug() << "ListViewWithPageHeader::minYExtent" << m_minYExtent;
339void ListViewWithPageHeader::componentComplete()
342 m_delegateModel->componentComplete();
344 QQuickFlickable::componentComplete();
349void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
351 QQuickFlickable::viewportMoved(orient);
352// qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
353 qreal diff = m_previousContentY - contentY();
355 auto oldHeaderItemShownHeight = m_headerItemShownHeight;
356 if (contentY() < -m_minYExtent) {
357 // Stick the header item to the top when dragging down
358 m_headerItem->setY(contentY());
360 // We are going down (but it's not because of the rebound at the end)
361 // (but the header was not shown by it's own position)
362 // or the header is partially shown
363 const bool scrolledUp = m_previousContentY > contentY();
364 const bool notRebounding = contentY() + height() < contentHeight();
365 const bool notShownByItsOwn = contentY() + diff > m_headerItem->y() + m_headerItem->height();
367 if (!scrolledUp && contentY() == -m_minYExtent) {
368 m_headerItemShownHeight = 0;
369 m_headerItem->setY(contentY());
370 } else if ((scrolledUp && notRebounding && notShownByItsOwn) || (m_headerItemShownHeight > 0)) {
371 m_headerItemShownHeight += diff;
372 if (contentY() == -m_minYExtent) {
373 m_headerItemShownHeight = 0;
375 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
377 if (m_headerItemShownHeight > 0) {
378 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
380 m_headerItem->setY(-m_minYExtent);
384 // We will be changing the clip item, need to accomadate for it
385 // otherwise we move the firstItem down/up twice (unless the
386 // show header animation is running, where we want to keep the viewport stable)
387 if (!m_headerShowAnimation->isRunning())
388 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
392 if (!m_visibleItems.isEmpty()) {
394 ListItem *firstItem = m_visibleItems.first();
395 firstItem->setY(firstItem->y() + diff);
396 if (m_headerShowAnimation->isRunning()) {
401 m_previousContentY = contentY();
406void ListViewWithPageHeader::createDelegateModel()
408 m_delegateModel = new QQuickVisualDataModel(qmlContext(this), this);
409 connect(m_delegateModel, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(itemCreated(int,QQuickItem*)));
410 if (isComponentComplete())
411 m_delegateModel->componentComplete();
412 updateWatchedRoles();
415void ListViewWithPageHeader::refill()
417 if (!isComponentComplete()) {
421 const qreal buffer = height() / 2;
422 const qreal from = contentY();
423 const qreal to = from + height();
424 const qreal bufferFrom = from - buffer;
425 const qreal bufferTo = to + buffer;
427 bool added = addVisibleItems(from, to, false);
428 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
429 added |= addVisibleItems(bufferFrom, bufferTo, true);
431 if (added || removed) {
432 m_contentHeightDirty = true;
436bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
441 if (m_delegateModel->count() == 0)
445// qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
449 if (!m_visibleItems.isEmpty()) {
450 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
451 item = m_visibleItems.last();
452 pos = item->y() + item->height() + m_clipItem->y();
454 bool changed = false;
455// qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
456 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
457// qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
458 if (!(item = createItem(modelIndex, asynchronous)))
460 pos += item->height();
467 if (!m_visibleItems.isEmpty()) {
468 modelIndex = m_firstVisibleIndex - 1;
469 item = m_visibleItems.first();
470 pos = item->y() + m_clipItem->y();
472 while (modelIndex >= 0 && pos > fillFrom) {
473// qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
474 if (!(item = createItem(modelIndex, asynchronous)))
476 pos -= item->height();
484void ListViewWithPageHeader::releaseItem(ListItem *listItem)
486 QQuickItem *item = listItem->m_item;
487 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
488 itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
489 QQuickVisualModel::ReleaseFlags flags = m_delegateModel->release(item);
490 if (flags & QQuickVisualModel::Destroyed) {
491 item->setParentItem(nullptr);
493 delete listItem->m_sectionItem;
497void ListViewWithPageHeader::updateWatchedRoles()
499 if (m_delegateModel) {
500 QList<QByteArray> roles;
501 if (!m_sectionProperty.isEmpty())
502 roles << m_sectionProperty.toUtf8();
503 m_delegateModel->setWatchedRoles(roles);
507QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
509 if (!m_sectionDelegate)
512 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
513 if (modelIndex > 0) {
514 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
515 if (section == prevSection)
518 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
519 // Already inserted items can't steal next section header
520 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
521 if (section == nextSection) {
522 // Steal the section header
523 ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
525 QQuickItem *sectionItem = nextItem->m_sectionItem;
526 nextItem->m_sectionItem = nullptr;
532 return getSectionItem(section);
535QQuickItem *ListViewWithPageHeader::getSectionItem(const QString §ionText)
537 QQuickItem *sectionItem = nullptr;
539 QQmlContext *creationContext = m_sectionDelegate->creationContext();
540 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
541 context->setContextProperty(QLatin1String("section"), sectionText);
542 QObject *nobj = m_sectionDelegate->beginCreate(context);
544 QQml_setParent_noEvent(context, nobj);
545 sectionItem = qobject_cast<QQuickItem *>(nobj);
549 sectionItem->setZ(2);
550 QQml_setParent_noEvent(sectionItem, m_clipItem);
551 sectionItem->setParentItem(m_clipItem);
556 m_sectionDelegate->completeCreate();
558 // TODO attach to sectionItem so we can accomodate to it changing its height
563bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
565// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
566 bool changed = false;
568 bool foundVisible = false;
570 int removedItems = 0;
571 while (i < m_visibleItems.count()) {
572 ListItem *item = m_visibleItems[i];
573 const qreal pos = item->y() + m_clipItem->y();
574// qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
575 if (pos + item->height() < bufferFrom || pos > bufferTo) {
576// qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
578 m_visibleItems.removeAt(i);
584 const int itemIndex = m_firstVisibleIndex + removedItems + i;
585 m_firstVisibleIndex = itemIndex;
594ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
596// qDebug() << "CREATE ITEM" << modelIndex;
597 if (asynchronous && m_asyncRequestedIndex != -1)
600 m_asyncRequestedIndex = -1;
601 QQuickItem *item = m_delegateModel->item(modelIndex, asynchronous);
603 m_asyncRequestedIndex = modelIndex;
606// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
607 ListItem *listItem = new ListItem;
608 listItem->m_item = item;
609 listItem->m_sectionItem = getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/);
610 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
611 ListItem *prevItem = itemAtIndex(modelIndex - 1);
612 bool lostItem = false; // Is an item that we requested async but because of model changes
613 // it is no longer attached to any of the existing items (has no prev nor next item)
614 // nor is the first item
616 listItem->setY(prevItem->y() + prevItem->height());
618 ListItem *nextItem = itemAtIndex(modelIndex + 1);
620 listItem->setY(nextItem->y() - listItem ->height());
621 } else if (modelIndex == 0 && m_headerItem) {
622 listItem->setY(m_headerItem->height());
623 } else if (!m_visibleItems.isEmpty()) {
628 releaseItem(listItem);
631 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() < contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
632 if (m_visibleItems.isEmpty()) {
633 m_visibleItems << listItem;
635 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
637 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
638 m_firstVisibleIndex = modelIndex;
646void ListViewWithPageHeader::itemCreated(int modelIndex, QQuickItem *item)
648// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
650 item->setParentItem(m_clipItem);
651 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
652 context->setContextProperty(QLatin1String("ListViewWithPageHeader"), this);
653 if (modelIndex == m_asyncRequestedIndex) {
654 createItem(modelIndex, false);
658void ListViewWithPageHeader::updateClipItem()
660 m_clipItem->setHeight(height() - m_headerItemShownHeight);
661 m_clipItem->setY(contentY() + m_headerItemShownHeight);
662 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
665void ListViewWithPageHeader::onContentHeightChanged()
670void ListViewWithPageHeader::onContentWidthChanged()
672 m_clipItem->setWidth(contentItem()->width());
675void ListViewWithPageHeader::onHeightChanged()
677 setMaximumFlickVelocity(height() * 10);
678 setFlickDeceleration(height() * 2);
681void ListViewWithPageHeader::onModelUpdated(const QQuickChangeSet &changeSet, bool /*reset*/)
683 // TODO Do something with reset
684// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
685 foreach(const QQuickChangeSet::Remove &remove, changeSet.removes()) {
686// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove;
687 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
688 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
689 // If all the items we are removing are either not created or culled
690 // we have to grow down to avoid viewport changing
691 bool growDown = true;
692 for (int i = 0; growDown && i < remove.count; ++i) {
693 const int modelIndex = remove.index + i;
694 ListItem *item = itemAtIndex(modelIndex);
695 if (item && !item->culled()) {
699 for (int i = remove.count - 1; i >= 0; --i) {
700 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
701 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
702 ListItem *item = m_visibleItems[visibleIndex];
703 // Pass the section item down if needed
704 if (item->m_sectionItem && visibleIndex + 1 < m_visibleItems.count()) {
705 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
706 if (!nextItem->m_sectionItem) {
707 nextItem->m_sectionItem = item->m_sectionItem;
708 item->m_sectionItem = nullptr;
712 m_visibleItems.removeAt(visibleIndex);
717 } else if (remove.index <= m_firstVisibleIndex && !m_visibleItems.isEmpty()) {
718 // We removed the first item that is the one that positions the rest
719 // position the new first item correctly
720 m_visibleItems.first()->setY(oldFirstValidIndexPos);
722 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
723 m_firstVisibleIndex -= remove.count;
725 for (int i = remove.count - 1; i >= 0; --i) {
726 const int modelIndex = remove.index + i;
727 if (modelIndex == m_asyncRequestedIndex) {
728 m_asyncRequestedIndex = -1;
729 } else if (modelIndex < m_asyncRequestedIndex) {
730 m_asyncRequestedIndex--;
735 foreach(const QQuickChangeSet::Insert &insert, changeSet.inserts()) {
736// qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
737 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
738 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
739 if (insertingInValidIndexes || firstItemWithViewOnTop)
741 // If the items we are adding won't be really visible
742 // we grow up instead of down to not change the viewport
744 if (!firstItemWithViewOnTop) {
745 for (int i = 0; i < m_visibleItems.count(); ++i) {
746 if (!m_visibleItems[i]->culled()) {
747 if (insert.index <= m_firstVisibleIndex + i) {
755 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
756 for (int i = insert.count - 1; i >= 0; --i) {
757 const int modelIndex = insert.index + i;
758 ListItem *item = createItem(modelIndex, false);
760 ListItem *firstItem = m_visibleItems.first();
761 firstItem->setY(firstItem->y() - item->height());
764 // Adding an item may break a "same section" chain, so check
765 // if we need adding a new section item
766 if (m_sectionDelegate) {
767 ListItem *nextItem = itemAtIndex(modelIndex + 1);
768 if (nextItem && !nextItem->m_sectionItem) {
769 nextItem->m_sectionItem = getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/);
771 ListItem *firstItem = m_visibleItems.first();
772 firstItem->setY(firstItem->y() - nextItem->m_sectionItem->height());
778 if (firstItemWithViewOnTop) {
779 ListItem *firstItem = m_visibleItems.first();
780 firstItem->setY(oldFirstValidIndexPos);
782 } else if (insert.index <= m_firstVisibleIndex) {
783 m_firstVisibleIndex += insert.count;
786 for (int i = insert.count - 1; i >= 0; --i) {
787 const int modelIndex = insert.index + i;
788 if (modelIndex <= m_asyncRequestedIndex) {
789 m_asyncRequestedIndex++;
796 m_contentHeightDirty = true;
799void ListViewWithPageHeader::itemGeometryChanged(QQuickItem *item, const QRectF &newGeometry, const QRectF &oldGeometry)
801 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
802 if (heightDiff != 0) {
803 if (item == m_headerItem) {
804 headerHeightChanged(newGeometry.height(), oldGeometry.height(), oldGeometry.y());
806 if (oldGeometry.y() + oldGeometry.height() + m_clipItem->y() < contentY() && !m_visibleItems.isEmpty()) {
807 ListItem *firstItem = m_visibleItems.first();
808 firstItem->setY(firstItem->y() - heightDiff);
813 m_contentHeightDirty = true;
817void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
819 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
820 if (m_headerItemShownHeight > 0) {
821 // If the header is shown because of the clip
823 m_headerItemShownHeight += heightDiff;
824 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
829 if (oldHeaderY + oldHeaderHeight > contentY()) {
830 // If the header is shown because its position
832 ListItem *firstItem = m_visibleItems.first();
833 firstItem->setY(firstItem->y() + heightDiff);
835 // If the header is not on screen, just change the start of the list
836 // so the viewport is not changed
843void ListViewWithPageHeader::adjustMinYExtent()
845 if (m_visibleItems.isEmpty()) {
848 m_minYExtent = -m_visibleItems.first()->y() - m_clipItem->y() + (m_headerItem ? m_headerItem->height() : 0);
849 qDebug() << "adjustMinYExtent" << m_minYExtent << m_visibleItems.first()->y() << m_clipItem->y();
853ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
855 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
856 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
857 return m_visibleItems[visibleIndexedModelIndex];
862void ListViewWithPageHeader::layout()
864 if (!m_visibleItems.isEmpty()) {
865 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
866 const qreal visibleTo = contentY() + height() - m_clipItem->y();
868 qreal pos = m_visibleItems.first()->y();
870// qDebug() << "ListViewWithPageHeader::updatePolish Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
871 int firstReallyVisibleItem = -1;
872 int modelIndex = m_firstVisibleIndex;
873 foreach(ListItem *item, m_visibleItems) {
874 const bool cull = pos + item->height() < visibleFrom || pos >= visibleTo;
875 item->setCulled(cull);
877 if (!cull && firstReallyVisibleItem == -1) {
878 firstReallyVisibleItem = modelIndex;
879 if (m_topSectionItem) {
880 // Update the top sticky section header
881 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
882 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
883 context->setContextProperty(QLatin1String("section"), section);
885 // Positing the top section sticky item is a two step process
886 // First we set it either we cull it (because it doesn't need to be sticked to the top)
887 // or stick it to the top
888 // Then after the loop we'll make sure that if there's another section just below it
889 // pushed the sticky section up to make it disappear
890 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
891 if (topSectionStickPos <= pos) {
892 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
893 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(false);
895 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
896 m_topSectionItem->setY(topSectionStickPos);
897 if (item->m_sectionItem) {
898 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(true);
903 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
904 if (!cull && pos < visibleFrom) {
905 context->setContextProperty(QLatin1String("heightToClip"), visibleFrom - pos);
907 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
909// qDebug() << "ListViewWithPageHeader::updatePolish" << item->m_item;
910 pos += item->height();
914 // Second step of section sticky item positioning
915 // Look at the next section header, check if it's pushing up the sticky one
916 if (m_topSectionItem) {
917 if (firstReallyVisibleItem >= 0) {
918 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
919 ListItem *item = m_visibleItems[i];
920 if (item->m_sectionItem) {
921 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
922 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
932void ListViewWithPageHeader::updatePolish()
941 if (m_contentHeightDirty) {
943 const int modelCount = model()->rowCount();
944 const int visibleItems = m_visibleItems.count();
945 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
946 if (lastValidIndex == modelCount - 1) {
947 ListItem *item = m_visibleItems.last();
948 contentHeight = item->y() + item->height() + m_clipItem->y();
949 if (m_firstVisibleIndex != 0) {
950 // Make sure that if we are shrinking we tell the view we still fit
951 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
954 contentHeight = m_headerItem ? m_headerItem->height() : 0;
955 if (!m_visibleItems.isEmpty()) {
956 foreach(ListItem *item, m_visibleItems) {
957 contentHeight += item->height();
959 const int unknownSizes = modelCount - visibleItems;
960 contentHeight += unknownSizes * contentHeight / visibleItems;
964 m_contentHeightDirty = false;
965 setContentHeight(contentHeight);
969#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