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 * Note that minYExtent and height are not always totally accurate, since
74 * we don't have the items created we can't guess their heights
75 * so we can only guarantee the values are correct when the first/last
76 * items of the list are visible, otherwise we just live with good enough
77 * values that make the list scrollable
79 * There are a few things that are not really implemented or tested properly
80 * which we don't use at the moment like changing the model, changing
81 * the section delegate, having a section delegate that changes its size, etc.
82 * The known missing features are marked with TODOs along the code.
85#include "listviewwithpageheader.h"
89#include <qqmlengine.h>
90#include <private/qquickvisualdatamodel_p.h>
91#include <private/qqmlglobal_p.h>
92#include <private/qquickitem_p.h>
93#include <private/qquickanimation_p.h>
94// #include <private/qquickrectangle_p.h>
96qreal ListViewWithPageHeader::ListItem::height() const
98 return m_item->height() + (m_sectionItem ? m_sectionItem->height() : 0);
101qreal ListViewWithPageHeader::ListItem::y() const
103 return m_item->y() - (m_sectionItem ? m_sectionItem->height() : 0);
106void ListViewWithPageHeader::ListItem::setY(qreal newY)
109 m_sectionItem->setY(newY);
110 m_item->setY(newY + m_sectionItem->height());
116bool ListViewWithPageHeader::ListItem::culled() const
118 return QQuickItemPrivate::get(m_item)->culled;
121void ListViewWithPageHeader::ListItem::setCulled(bool culled)
123 QQuickItemPrivate::get(m_item)->setCulled(culled);
125 QQuickItemPrivate::get(m_sectionItem)->setCulled(culled);
128ListViewWithPageHeader::ListViewWithPageHeader()
129 : m_delegateModel(nullptr)
130 , m_asyncRequestedIndex(-1)
131 , m_delegateValidated(false)
132 , m_firstVisibleIndex(-1)
134 , m_contentHeightDirty(false)
135 , m_headerItem(nullptr)
136 , m_previousContentY(0)
137 , m_headerItemShownHeight(0)
138 , m_sectionDelegate(nullptr)
139 , m_topSectionItem(nullptr)
140 , m_forceNoClip(false)
142 m_clipItem = new QQuickItem(contentItem());
143// m_clipItem = new QQuickRectangle(contentItem());
144// ((QQuickRectangle*)m_clipItem)->setColor(Qt::gray);
146 m_headerShowAnimation = new QQuickNumberAnimation(this);
147 m_headerShowAnimation->setEasing(QEasingCurve::OutQuad);
148 m_headerShowAnimation->setProperty("contentY");
149 m_headerShowAnimation->setDuration(200);
150 m_headerShowAnimation->setTargetObject(this);
152 connect(this, SIGNAL(contentWidthChanged()), this, SLOT(onContentWidthChanged()));
153 connect(this, SIGNAL(contentHeightChanged()), this, SLOT(onContentHeightChanged()));
154 connect(this, SIGNAL(heightChanged()), this, SLOT(onHeightChanged()));
157ListViewWithPageHeader::~ListViewWithPageHeader()
161QAbstractItemModel *ListViewWithPageHeader::model() const
163 return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
166void ListViewWithPageHeader::setModel(QAbstractItemModel *model)
168 if (model != this->model()) {
169 if (!m_delegateModel) {
170 createDelegateModel();
172 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
174 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
175 connect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
176 Q_EMIT modelChanged();
178// Q_EMIT contentHeightChanged();
179// Q_EMIT contentYChanged();
183QQmlComponent *ListViewWithPageHeader::delegate() const
185 return m_delegateModel ? m_delegateModel->delegate() : nullptr;
188void ListViewWithPageHeader::setDelegate(QQmlComponent *delegate)
190 if (delegate != this->delegate()) {
191 if (!m_delegateModel) {
192 createDelegateModel();
195 // Cleanup the existing items
196 foreach(ListItem *item, m_visibleItems)
198 m_visibleItems.clear();
199 m_firstVisibleIndex = -1;
203 if (m_topSectionItem) {
204 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
207 m_delegateModel->setDelegate(delegate);
209 Q_EMIT delegateChanged();
210 m_delegateValidated = false;
211 m_contentHeightDirty = true;
216QQuickItem *ListViewWithPageHeader::header() const
221void ListViewWithPageHeader::setHeader(QQuickItem *headerItem)
223 if (m_headerItem != headerItem) {
224 qreal oldHeaderHeight = 0;
225 qreal oldHeaderY = 0;
227 oldHeaderHeight = m_headerItem->height();
228 oldHeaderY = m_headerItem->y();
229 QQuickItemPrivate::get(m_headerItem)->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
230 m_headerItem->setParentItem(nullptr);
232 m_headerItem = headerItem;
234 m_headerItem->setParentItem(contentItem());
235 m_headerItem->setZ(1);
236 QQuickItemPrivate::get(m_headerItem)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
238 qreal newHeaderHeight = m_headerItem ? m_headerItem->height() : 0;
239 if (!m_visibleItems.isEmpty() && newHeaderHeight != oldHeaderHeight) {
240 headerHeightChanged(newHeaderHeight, oldHeaderHeight, oldHeaderY);
242 m_contentHeightDirty = true;
244 Q_EMIT headerChanged();
248QQmlComponent *ListViewWithPageHeader::sectionDelegate() const
250 return m_sectionDelegate;
253void ListViewWithPageHeader::setSectionDelegate(QQmlComponent *delegate)
255 if (delegate != m_sectionDelegate) {
256 // TODO clean existing sections
258 m_sectionDelegate = delegate;
260 m_topSectionItem = getSectionItem(QString());
261 m_topSectionItem->setZ(3);
262 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
264 // TODO create sections for existing items
266 Q_EMIT sectionDelegateChanged();
270QString ListViewWithPageHeader::sectionProperty() const
272 return m_sectionProperty;
275void ListViewWithPageHeader::setSectionProperty(const QString &property)
277 if (property != m_sectionProperty) {
278 m_sectionProperty = property;
280 updateWatchedRoles();
282 // TODO recreate sections
284 Q_EMIT sectionPropertyChanged();
288bool ListViewWithPageHeader::forceNoClip() const
290 return m_forceNoClip;
293void ListViewWithPageHeader::setForceNoClip(bool noClip)
295 if (noClip != m_forceNoClip) {
296 m_forceNoClip = noClip;
298 Q_EMIT forceNoClipChanged();
302void ListViewWithPageHeader::positionAtBeginning()
304 qreal headerHeight = (m_headerItem ? m_headerItem->height() : 0);
305 if (m_firstVisibleIndex != 0) {
306 // This could be optimized by trying to reuse the interesection
307 // of items that may end up intersecting between the existing
308 // m_visibleItems and the items we are creating in the next loop
309 foreach(ListItem *item, m_visibleItems)
311 m_visibleItems.clear();
312 m_firstVisibleIndex = -1;
314 // Create the item 0, it will be already correctly positioned at createItem()
316 ListItem *item = createItem(0, false);
317 // Create the subsequent items
319 qreal pos = item->y() + item->height();
320 const qreal buffer = height() / 2;
321 const qreal bufferTo = height() + buffer;
322 while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
323 if (!(item = createItem(modelIndex, false)))
325 pos += item->height();
329 m_previousContentY = m_visibleItems.first()->y() - headerHeight;
331 setContentY(m_visibleItems.first()->y() + m_clipItem->y() - headerHeight);
334void ListViewWithPageHeader::showHeader()
336 auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
337 if (to != contentY()) {
338 m_headerShowAnimation->setTo(to);
339 m_headerShowAnimation->start();
343qreal ListViewWithPageHeader::minYExtent() const
345// qDebug() << "ListViewWithPageHeader::minYExtent" << m_minYExtent;
349void ListViewWithPageHeader::componentComplete()
352 m_delegateModel->componentComplete();
354 QQuickFlickable::componentComplete();
359void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
361 QQuickFlickable::viewportMoved(orient);
362// qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
363 qreal diff = m_previousContentY - contentY();
365 auto oldHeaderItemShownHeight = m_headerItemShownHeight;
366 if (contentY() < -m_minYExtent) {
367 // Stick the header item to the top when dragging down
368 m_headerItem->setY(contentY());
370 // We are going down (but it's not because of the rebound at the end)
371 // (but the header was not shown by it's own position)
372 // or the header is partially shown
373 const bool scrolledUp = m_previousContentY > contentY();
374 const bool notRebounding = contentY() + height() < contentHeight();
375 const bool notShownByItsOwn = contentY() + diff > m_headerItem->y() + m_headerItem->height();
377 if (!scrolledUp && contentY() == -m_minYExtent) {
378 m_headerItemShownHeight = 0;
379 m_headerItem->setY(contentY());
380 } else if ((scrolledUp && notRebounding && notShownByItsOwn) || (m_headerItemShownHeight > 0)) {
381 m_headerItemShownHeight += diff;
382 if (contentY() == -m_minYExtent) {
383 m_headerItemShownHeight = 0;
385 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
387 if (m_headerItemShownHeight > 0) {
388 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
390 m_headerItem->setY(-m_minYExtent);
394 // We will be changing the clip item, need to accomadate for it
395 // otherwise we move the firstItem down/up twice (unless the
396 // show header animation is running, where we want to keep the viewport stable)
397 if (!m_headerShowAnimation->isRunning()) {
398 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
401 m_contentHeightDirty = true;
404 if (!m_visibleItems.isEmpty()) {
406 ListItem *firstItem = m_visibleItems.first();
407 firstItem->setY(firstItem->y() + diff);
408 if (m_headerShowAnimation->isRunning()) {
413 m_previousContentY = contentY();
418void ListViewWithPageHeader::createDelegateModel()
420 m_delegateModel = new QQuickVisualDataModel(qmlContext(this), this);
421 connect(m_delegateModel, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(itemCreated(int,QQuickItem*)));
422 if (isComponentComplete())
423 m_delegateModel->componentComplete();
424 updateWatchedRoles();
427void ListViewWithPageHeader::refill()
429 if (!isComponentComplete()) {
433 const qreal buffer = height() / 2;
434 const qreal from = contentY();
435 const qreal to = from + height();
436 const qreal bufferFrom = from - buffer;
437 const qreal bufferTo = to + buffer;
439 bool added = addVisibleItems(from, to, false);
440 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
441 added |= addVisibleItems(bufferFrom, bufferTo, true);
443 if (added || removed) {
444 m_contentHeightDirty = true;
448bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
453 if (m_delegateModel->count() == 0)
457// qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
461 if (!m_visibleItems.isEmpty()) {
462 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
463 item = m_visibleItems.last();
464 pos = item->y() + item->height() + m_clipItem->y();
466 bool changed = false;
467// qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
468 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
469// qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
470 if (!(item = createItem(modelIndex, asynchronous)))
472 pos += item->height();
479 if (!m_visibleItems.isEmpty()) {
480 modelIndex = m_firstVisibleIndex - 1;
481 item = m_visibleItems.first();
482 pos = item->y() + m_clipItem->y();
484 while (modelIndex >= 0 && pos > fillFrom) {
485// qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
486 if (!(item = createItem(modelIndex, asynchronous)))
488 pos -= item->height();
496void ListViewWithPageHeader::releaseItem(ListItem *listItem)
498 QQuickItem *item = listItem->m_item;
499 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
500 itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
501 QQuickVisualModel::ReleaseFlags flags = m_delegateModel->release(item);
502 if (flags & QQuickVisualModel::Destroyed) {
503 item->setParentItem(nullptr);
505 delete listItem->m_sectionItem;
509void ListViewWithPageHeader::updateWatchedRoles()
511 if (m_delegateModel) {
512 QList<QByteArray> roles;
513 if (!m_sectionProperty.isEmpty())
514 roles << m_sectionProperty.toUtf8();
515 m_delegateModel->setWatchedRoles(roles);
519QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
521 if (!m_sectionDelegate)
524 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
525 if (modelIndex > 0) {
526 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
527 if (section == prevSection)
530 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
531 // Already inserted items can't steal next section header
532 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
533 if (section == nextSection) {
534 // Steal the section header
535 ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
537 QQuickItem *sectionItem = nextItem->m_sectionItem;
538 nextItem->m_sectionItem = nullptr;
544 return getSectionItem(section);
547QQuickItem *ListViewWithPageHeader::getSectionItem(const QString §ionText)
549 QQuickItem *sectionItem = nullptr;
551 QQmlContext *creationContext = m_sectionDelegate->creationContext();
552 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
553 context->setContextProperty(QLatin1String("section"), sectionText);
554 QObject *nobj = m_sectionDelegate->beginCreate(context);
556 QQml_setParent_noEvent(context, nobj);
557 sectionItem = qobject_cast<QQuickItem *>(nobj);
561 sectionItem->setZ(2);
562 QQml_setParent_noEvent(sectionItem, m_clipItem);
563 sectionItem->setParentItem(m_clipItem);
568 m_sectionDelegate->completeCreate();
570 // TODO attach to sectionItem so we can accomodate to it changing its height
575bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
577// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
578 bool changed = false;
580 bool foundVisible = false;
582 int removedItems = 0;
583 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
584 while (i < m_visibleItems.count()) {
585 ListItem *item = m_visibleItems[i];
586 const qreal pos = item->y() + m_clipItem->y();
587// qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
588 if (pos + item->height() < bufferFrom || pos > bufferTo) {
589// qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
591 m_visibleItems.removeAt(i);
597 const int itemIndex = m_firstVisibleIndex + removedItems + i;
598 m_firstVisibleIndex = itemIndex;
603 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
610ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
612// qDebug() << "CREATE ITEM" << modelIndex;
613 if (asynchronous && m_asyncRequestedIndex != -1)
616 m_asyncRequestedIndex = -1;
617 QQuickItem *item = m_delegateModel->item(modelIndex, asynchronous);
619 m_asyncRequestedIndex = modelIndex;
622// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
623 ListItem *listItem = new ListItem;
624 listItem->m_item = item;
625 listItem->m_sectionItem = getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/);
626 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
627 ListItem *prevItem = itemAtIndex(modelIndex - 1);
628 bool lostItem = false; // Is an item that we requested async but because of model changes
629 // it is no longer attached to any of the existing items (has no prev nor next item)
630 // nor is the first item
632 listItem->setY(prevItem->y() + prevItem->height());
634 ListItem *nextItem = itemAtIndex(modelIndex + 1);
636 listItem->setY(nextItem->y() - listItem ->height());
637 } else if (modelIndex == 0 && m_headerItem) {
638 listItem->setY(m_headerItem->height());
639 } else if (!m_visibleItems.isEmpty()) {
644 releaseItem(listItem);
647 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() < contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
648 if (m_visibleItems.isEmpty()) {
649 m_visibleItems << listItem;
651 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
653 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
654 m_firstVisibleIndex = modelIndex;
662void ListViewWithPageHeader::itemCreated(int modelIndex, QQuickItem *item)
664// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
666 item->setParentItem(m_clipItem);
667 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
668 context->setContextProperty(QLatin1String("ListViewWithPageHeader"), this);
669 if (modelIndex == m_asyncRequestedIndex) {
670 createItem(modelIndex, false);
674void ListViewWithPageHeader::updateClipItem()
676 m_clipItem->setHeight(height() - m_headerItemShownHeight);
677 m_clipItem->setY(contentY() + m_headerItemShownHeight);
678 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
681void ListViewWithPageHeader::onContentHeightChanged()
686void ListViewWithPageHeader::onContentWidthChanged()
688 m_clipItem->setWidth(contentItem()->width());
691void ListViewWithPageHeader::onHeightChanged()
693 setMaximumFlickVelocity(height() * 10);
694 setFlickDeceleration(height() * 2);
697void ListViewWithPageHeader::onModelUpdated(const QQuickChangeSet &changeSet, bool /*reset*/)
699 // TODO Do something with reset
700// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
701 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
703 foreach(const QQuickChangeSet::Remove &remove, changeSet.removes()) {
704// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove;
705 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
706 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
707 // If all the items we are removing are either not created or culled
708 // we have to grow down to avoid viewport changing
709 bool growDown = true;
710 for (int i = 0; growDown && i < remove.count; ++i) {
711 const int modelIndex = remove.index + i;
712 ListItem *item = itemAtIndex(modelIndex);
713 if (item && !item->culled()) {
717 for (int i = remove.count - 1; i >= 0; --i) {
718 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
719 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
720 ListItem *item = m_visibleItems[visibleIndex];
721 // Pass the section item down if needed
722 if (item->m_sectionItem && visibleIndex + 1 < m_visibleItems.count()) {
723 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
724 if (!nextItem->m_sectionItem) {
725 nextItem->m_sectionItem = item->m_sectionItem;
726 item->m_sectionItem = nullptr;
730 m_visibleItems.removeAt(visibleIndex);
735 } else if (remove.index <= m_firstVisibleIndex && !m_visibleItems.isEmpty()) {
736 // We removed the first item that is the one that positions the rest
737 // position the new first item correctly
738 m_visibleItems.first()->setY(oldFirstValidIndexPos);
740 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
741 m_firstVisibleIndex -= remove.count;
743 for (int i = remove.count - 1; i >= 0; --i) {
744 const int modelIndex = remove.index + i;
745 if (modelIndex == m_asyncRequestedIndex) {
746 m_asyncRequestedIndex = -1;
747 } else if (modelIndex < m_asyncRequestedIndex) {
748 m_asyncRequestedIndex--;
753 foreach(const QQuickChangeSet::Insert &insert, changeSet.inserts()) {
754// qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
755 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
756 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
757 if (insertingInValidIndexes || firstItemWithViewOnTop)
759 // If the items we are adding won't be really visible
760 // we grow up instead of down to not change the viewport
762 if (!firstItemWithViewOnTop) {
763 for (int i = 0; i < m_visibleItems.count(); ++i) {
764 if (!m_visibleItems[i]->culled()) {
765 if (insert.index <= m_firstVisibleIndex + i) {
773 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
774 for (int i = insert.count - 1; i >= 0; --i) {
775 const int modelIndex = insert.index + i;
776 ListItem *item = createItem(modelIndex, false);
778 ListItem *firstItem = m_visibleItems.first();
779 firstItem->setY(firstItem->y() - item->height());
782 // Adding an item may break a "same section" chain, so check
783 // if we need adding a new section item
784 if (m_sectionDelegate) {
785 ListItem *nextItem = itemAtIndex(modelIndex + 1);
786 if (nextItem && !nextItem->m_sectionItem) {
787 nextItem->m_sectionItem = getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/);
789 ListItem *firstItem = m_visibleItems.first();
790 firstItem->setY(firstItem->y() - nextItem->m_sectionItem->height());
796 if (firstItemWithViewOnTop) {
797 ListItem *firstItem = m_visibleItems.first();
798 firstItem->setY(oldFirstValidIndexPos);
800 } else if (insert.index <= m_firstVisibleIndex) {
801 m_firstVisibleIndex += insert.count;
804 for (int i = insert.count - 1; i >= 0; --i) {
805 const int modelIndex = insert.index + i;
806 if (modelIndex <= m_asyncRequestedIndex) {
807 m_asyncRequestedIndex++;
812 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
818 m_contentHeightDirty = true;
821void ListViewWithPageHeader::itemGeometryChanged(QQuickItem *item, const QRectF &newGeometry, const QRectF &oldGeometry)
823 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
824 if (heightDiff != 0) {
825 if (item == m_headerItem) {
826 headerHeightChanged(newGeometry.height(), oldGeometry.height(), oldGeometry.y());
828 if (oldGeometry.y() + oldGeometry.height() + m_clipItem->y() < contentY() && !m_visibleItems.isEmpty()) {
829 ListItem *firstItem = m_visibleItems.first();
830 firstItem->setY(firstItem->y() - heightDiff);
838 m_contentHeightDirty = true;
842void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
844 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
845 if (m_headerItemShownHeight > 0) {
846 // If the header is shown because of the clip
848 m_headerItemShownHeight += heightDiff;
849 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
853 if (oldHeaderY + oldHeaderHeight > contentY()) {
854 // If the header is shown because its position
856 ListItem *firstItem = m_visibleItems.first();
857 firstItem->setY(firstItem->y() + heightDiff);
860 // If the header is not on screen, just change the start of the list
861 // so the viewport is not changed
868void ListViewWithPageHeader::adjustMinYExtent()
870 if (m_visibleItems.isEmpty()) {
873 qreal nonCreatedHeight = 0;
874 if (m_firstVisibleIndex != 0) {
875 // Calculate the average height of items to estimate the position of the list start
876 const int visibleItems = m_visibleItems.count();
877 qreal visibleItemsHeight = 0;
878 foreach(ListItem *item, m_visibleItems) {
879 visibleItemsHeight += item->height();
881 nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
882// qDebug() << m_firstVisibleIndex << visibleItemsHeight << visibleItems << nonCreatedHeight;
884 m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + (m_headerItem ? m_headerItem->height() : 0);
888ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
890 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
891 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
892 return m_visibleItems[visibleIndexedModelIndex];
897void ListViewWithPageHeader::layout()
899 if (!m_visibleItems.isEmpty()) {
900 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
901 const qreal visibleTo = contentY() + height() - m_clipItem->y();
903 qreal pos = m_visibleItems.first()->y();
905// qDebug() << "ListViewWithPageHeader::layout Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
906 int firstReallyVisibleItem = -1;
907 int modelIndex = m_firstVisibleIndex;
908 foreach(ListItem *item, m_visibleItems) {
909 const bool cull = pos + item->height() < visibleFrom || pos >= visibleTo;
910 item->setCulled(cull);
912 if (!cull && firstReallyVisibleItem == -1) {
913 firstReallyVisibleItem = modelIndex;
914 if (m_topSectionItem) {
915 // Update the top sticky section header
916 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
917 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
918 context->setContextProperty(QLatin1String("section"), section);
920 // Positing the top section sticky item is a two step process
921 // First we set it either we cull it (because it doesn't need to be sticked to the top)
922 // or stick it to the top
923 // Then after the loop we'll make sure that if there's another section just below it
924 // pushed the sticky section up to make it disappear
925 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
926 if (topSectionStickPos <= pos) {
927 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
928 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(false);
930 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
931 m_topSectionItem->setY(topSectionStickPos);
932 if (item->m_sectionItem) {
933 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(true);
938 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
939 if (!cull && pos < visibleFrom) {
940 context->setContextProperty(QLatin1String("heightToClip"), visibleFrom - pos);
942 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
944// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
945 pos += item->height();
949 // Second step of section sticky item positioning
950 // Look at the next section header, check if it's pushing up the sticky one
951 if (m_topSectionItem) {
952 if (firstReallyVisibleItem >= 0) {
953 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
954 ListItem *item = m_visibleItems[i];
955 if (item->m_sectionItem) {
956 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
957 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
967void ListViewWithPageHeader::updatePolish()
976 if (m_contentHeightDirty) {
978 const int modelCount = model()->rowCount();
979 const int visibleItems = m_visibleItems.count();
980 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
981 if (lastValidIndex == modelCount - 1) {
982 ListItem *item = m_visibleItems.last();
983 contentHeight = item->y() + item->height() + m_clipItem->y();
984 if (m_firstVisibleIndex != 0) {
985 // Make sure that if we are shrinking we tell the view we still fit
986 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
989 contentHeight = m_headerItem ? m_headerItem->height() : 0;
990 if (!m_visibleItems.isEmpty()) {
991 foreach(ListItem *item, m_visibleItems) {
992 contentHeight += item->height();
994 const int unknownSizes = modelCount - visibleItems;
995 contentHeight += unknownSizes * contentHeight / visibleItems;
999 m_contentHeightDirty = false;
1000 setContentHeight(contentHeight);
1004#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