on aca9b06 help sign in
unity8/plugins/ListViewWithPageHeader/listviewwithpageheader.cpp
1278 lines 51 kB history raw
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
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.
7 *
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.
12 *
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/>.
15 */
17/*
18 * Some documentation on how this thing works:
19 *
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.
29 *
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.
35 *
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.
38 * In it we:
39 * * Make sure all items are positioned correctly
40 * * Add/Remove items if needed
41 * * Update the content height if it was dirty
42 *
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
49 * in m_visibleItems
50 *
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.
57 *
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())
62 *
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.
68 *
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.
72 *
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.
79 *
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
85 *
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.
90 */
92#include "listviewwithpageheader.h"
94#include <QDebug>
95#include <qqmlinfo.h>
96#include <qqmlengine.h>
97#pragma GCC diagnostic push
98#pragma GCC diagnostic ignored "-pedantic"
99#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
100#include <private/qquickvisualdatamodel_p.h>
101#else
102#include <private/qqmldelegatemodel_p.h>
103#endif
104#include <private/qqmlglobal_p.h>
105#include <private/qquickitem_p.h>
106#include <private/qquickanimation_p.h>
107#pragma GCC diagnostic pop
108// #include <private/qquickrectangle_p.h>
110static const qreal bufferRatio = 0.5;
112qreal ListViewWithPageHeader::ListItem::height() const
114 return m_item->height() + (m_sectionItem ? m_sectionItem->height() : 0);
117qreal ListViewWithPageHeader::ListItem::y() const
119 return m_item->y() - (m_sectionItem ? m_sectionItem->height() : 0);
122void ListViewWithPageHeader::ListItem::setY(qreal newY)
124 if (m_sectionItem) {
125 m_sectionItem->setY(newY);
126 m_item->setY(newY + m_sectionItem->height());
127 } else {
128 m_item->setY(newY);
129 }
132bool ListViewWithPageHeader::ListItem::culled() const
134 return QQuickItemPrivate::get(m_item)->culled;
137void ListViewWithPageHeader::ListItem::setCulled(bool culled)
139 QQuickItemPrivate::get(m_item)->setCulled(culled);
140 if (m_sectionItem)
141 QQuickItemPrivate::get(m_sectionItem)->setCulled(culled);
143 QQmlContext *itemContext = QQmlEngine::contextForObject(m_item)->parentContext();
144 itemContext->setContextProperty(QLatin1String("culled"), QVariant::fromValue<bool>(culled));
147ListViewWithPageHeader::ListViewWithPageHeader()
148 : m_delegateModel(nullptr)
149 , m_asyncRequestedIndex(-1)
150 , m_delegateValidated(false)
151 , m_firstVisibleIndex(-1)
152 , m_minYExtent(0)
153 , m_contentHeightDirty(false)
154 , m_headerItem(nullptr)
155 , m_previousContentY(0)
156 , m_headerItemShownHeight(0)
157 , m_sectionDelegate(nullptr)
158 , m_topSectionItem(nullptr)
159 , m_forceNoClip(false)
160 , m_inLayout(false)
162 m_clipItem = new QQuickItem(contentItem());
163// m_clipItem = new QQuickRectangle(contentItem());
164// ((QQuickRectangle*)m_clipItem)->setColor(Qt::gray);
166 m_contentYAnimation = new QQuickNumberAnimation(this);
167 m_contentYAnimation->setEasing(QEasingCurve::OutQuad);
168 m_contentYAnimation->setProperty("contentY");
169 m_contentYAnimation->setDuration(200);
170 m_contentYAnimation->setTargetObject(this);
172 connect(this, SIGNAL(contentWidthChanged()), this, SLOT(onContentWidthChanged()));
173 connect(this, SIGNAL(contentHeightChanged()), this, SLOT(onContentHeightChanged()));
174 connect(this, SIGNAL(heightChanged()), this, SLOT(onHeightChanged()));
175 connect(m_contentYAnimation, SIGNAL(stopped()), this, SLOT(onShowHeaderAnimationFinished()));
177 setFlickableDirection(VerticalFlick);
180ListViewWithPageHeader::~ListViewWithPageHeader()
184QAbstractItemModel *ListViewWithPageHeader::model() const
186 return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
189void ListViewWithPageHeader::setModel(QAbstractItemModel *model)
191 if (model != this->model()) {
192 if (!m_delegateModel) {
193 createDelegateModel();
194 } else {
195#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
196 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
197 }
198 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
199 connect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
200#else
201 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
202 }
203 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
204 connect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
205#endif
206 Q_EMIT modelChanged();
207 polish();
208 // TODO?
209// Q_EMIT contentHeightChanged();
210// Q_EMIT contentYChanged();
211 }
214QQmlComponent *ListViewWithPageHeader::delegate() const
216 return m_delegateModel ? m_delegateModel->delegate() : nullptr;
219void ListViewWithPageHeader::setDelegate(QQmlComponent *delegate)
221 if (delegate != this->delegate()) {
222 if (!m_delegateModel) {
223 createDelegateModel();
224 }
226 // Cleanup the existing items
227 Q_FOREACH(ListItem *item, m_visibleItems)
228 releaseItem(item);
229 m_visibleItems.clear();
230 m_firstVisibleIndex = -1;
231 adjustMinYExtent();
232 setContentY(0);
233 m_clipItem->setY(0);
234 if (m_topSectionItem) {
235 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
236 }
238 m_delegateModel->setDelegate(delegate);
240 Q_EMIT delegateChanged();
241 m_delegateValidated = false;
242 m_contentHeightDirty = true;
243 polish();
244 }
247QQuickItem *ListViewWithPageHeader::header() const
249 return m_headerItem;
252void ListViewWithPageHeader::setHeader(QQuickItem *headerItem)
254 if (m_headerItem != headerItem) {
255 qreal oldHeaderHeight = 0;
256 qreal oldHeaderY = 0;
257 if (m_headerItem) {
258 oldHeaderHeight = m_headerItem->height();
259 oldHeaderY = m_headerItem->y();
260 m_headerItem->setParentItem(nullptr);
261 }
262 m_headerItem = headerItem;
263 if (m_headerItem) {
264 m_headerItem->setParentItem(contentItem());
265 m_headerItem->setZ(1);
266 }
267 qreal newHeaderHeight = m_headerItem ? m_headerItem->height() : 0;
268 if (!m_visibleItems.isEmpty() && newHeaderHeight != oldHeaderHeight) {
269 headerHeightChanged(newHeaderHeight, oldHeaderHeight, oldHeaderY);
270 polish();
271 m_contentHeightDirty = true;
272 }
273 Q_EMIT headerChanged();
274 }
277QQmlComponent *ListViewWithPageHeader::sectionDelegate() const
279 return m_sectionDelegate;
282void ListViewWithPageHeader::setSectionDelegate(QQmlComponent *delegate)
284 if (delegate != m_sectionDelegate) {
285 // TODO clean existing sections
287 m_sectionDelegate = delegate;
289 m_topSectionItem = getSectionItem(QString());
290 m_topSectionItem->setZ(3);
291 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
293 // TODO create sections for existing items
295 Q_EMIT sectionDelegateChanged();
296 }
299QString ListViewWithPageHeader::sectionProperty() const
301 return m_sectionProperty;
304void ListViewWithPageHeader::setSectionProperty(const QString &property)
306 if (property != m_sectionProperty) {
307 m_sectionProperty = property;
309 updateWatchedRoles();
311 // TODO recreate sections
313 Q_EMIT sectionPropertyChanged();
314 }
317bool ListViewWithPageHeader::forceNoClip() const
319 return m_forceNoClip;
322void ListViewWithPageHeader::setForceNoClip(bool noClip)
324 if (noClip != m_forceNoClip) {
325 m_forceNoClip = noClip;
326 updateClipItem();
327 Q_EMIT forceNoClipChanged();
328 }
331void ListViewWithPageHeader::positionAtBeginning()
333 if (m_delegateModel->count() <= 0)
334 return;
336 qreal headerHeight = (m_headerItem ? m_headerItem->height() : 0);
337 if (m_firstVisibleIndex != 0) {
338 // TODO This could be optimized by trying to reuse the interesection
339 // of items that may end up intersecting between the existing
340 // m_visibleItems and the items we are creating in the next loop
341 Q_FOREACH(ListItem *item, m_visibleItems)
342 releaseItem(item);
343 m_visibleItems.clear();
344 m_firstVisibleIndex = -1;
346 // Create the item 0, it will be already correctly positioned at createItem()
347 m_clipItem->setY(0);
348 ListItem *item = createItem(0, false);
349 // Create the subsequent items
350 int modelIndex = 1;
351 qreal pos = item->y() + item->height();
352 const qreal buffer = height() * bufferRatio;
353 const qreal bufferTo = height() + buffer;
354 while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
355 if (!(item = createItem(modelIndex, false)))
356 break;
357 pos += item->height();
358 ++modelIndex;
359 }
361 m_previousContentY = m_visibleItems.first()->y() - headerHeight;
362 }
363 setContentY(m_visibleItems.first()->y() + m_clipItem->y() - headerHeight);
366void ListViewWithPageHeader::showHeader()
368 if (!m_headerItem)
369 return;
371 auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
372 if (to != contentY()) {
373 const bool headerShownByItsOwn = contentY() < m_headerItem->y() + m_headerItem->height();
374 if (headerShownByItsOwn && m_headerItemShownHeight == 0) {
375 // We are not clipping since we are just at the top of the viewport
376 // but because of the showHeader animation we will need to, so
377 // enable the clipping without logically moving the items
378 m_headerItemShownHeight = m_headerItem->y() + m_headerItem->height() - contentY();
379 if (!m_visibleItems.isEmpty()) {
380 updateClipItem();
381 ListItem *firstItem = m_visibleItems.first();
382 firstItem->setY(firstItem->y() - m_headerItemShownHeight);
383 layout();
384 }
385 }
386 m_contentYAnimation->setTo(to);
387 contentYAnimationType = ContentYAnimationShowHeader;
388 m_contentYAnimation->start();
389 }
392QQuickItem *ListViewWithPageHeader::item(int modelIndex) const
394 ListItem *item = itemAtIndex(modelIndex);
395 if (item)
396 return item->m_item;
397 else
398 return nullptr;
401bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex)
403 ListItem *listItem = itemAtIndex(modelIndex);
404 if (listItem) {
405 return maximizeVisibleArea(listItem, listItem->height());
406 }
408 return false;
411bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex, int itemHeight)
413 if (itemHeight < 0)
414 return false;
416 ListItem *listItem = itemAtIndex(modelIndex);
417 if (listItem) {
418 return maximizeVisibleArea(listItem, itemHeight + (listItem->m_sectionItem ? listItem->m_sectionItem->height() : 0));
419 }
421 return false;
424bool ListViewWithPageHeader::maximizeVisibleArea(ListItem *listItem, int listItemHeight)
426 if (listItem) {
427 layout();
428 const auto listItemY = m_clipItem->y() + listItem->y();
429 if (listItemY > contentY() && listItemY + listItemHeight > contentY() + height()) {
430 // we can scroll the list up to show more stuff
431 const auto to = qMin(listItemY, listItemY + listItemHeight - height());
432 m_contentYAnimation->setTo(to);
433 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
434 m_contentYAnimation->start();
435 } else if ((listItemY < contentY() && listItemY + listItemHeight < contentY() + height()) ||
436 (m_topSectionItem && !listItem->m_sectionItem && listItemY - m_topSectionItem->height() < contentY() && listItemY + listItemHeight < contentY() + height()))
437 {
438 // we can scroll the list down to show more stuff
439 auto realVisibleListItemY = listItemY;
440 if (m_topSectionItem) {
441 // If we are showing the top section sticky item and this item doesn't have a section
442 // item we have to make sure to scroll it a bit more so that it is not underlapping
443 // the top section sticky item
444 bool topSectionShown = !QQuickItemPrivate::get(m_topSectionItem)->culled;
445 if (topSectionShown && !listItem->m_sectionItem) {
446 realVisibleListItemY -= m_topSectionItem->height();
447 }
448 }
449 const auto to = qMax(realVisibleListItemY, listItemY + listItemHeight - height());
450 m_contentYAnimation->setTo(to);
451 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
452 m_contentYAnimation->start();
453 }
454 return true;
455 }
456 return false;
459qreal ListViewWithPageHeader::minYExtent() const
461// qDebug() << "ListViewWithPageHeader::minYExtent" << m_minYExtent;
462 return m_minYExtent;
465void ListViewWithPageHeader::componentComplete()
467 if (m_delegateModel)
468 m_delegateModel->componentComplete();
470 QQuickFlickable::componentComplete();
472 polish();
475void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
477 QQuickFlickable::viewportMoved(orient);
478// qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
479 qreal diff = m_previousContentY - contentY();
480 const bool showHeaderAnimationRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationShowHeader;
481 if (m_headerItem) {
482 auto oldHeaderItemShownHeight = m_headerItemShownHeight;
483 if (contentY() < -m_minYExtent) {
484 // Stick the header item to the top when dragging down
485 m_headerItem->setY(contentY());
486 m_headerItem->setHeight(m_headerItem->implicitHeight() + (-m_minYExtent - contentY()));
487 } else {
488 m_headerItem->setHeight(m_headerItem->implicitHeight());
489 // We are going down (but it's not because of the rebound at the end)
490 // (but the header was not shown by it's own position)
491 // or the header is partially shown and we are not doing a maximizeVisibleArea either
492 const bool scrolledUp = m_previousContentY > contentY();
493 const bool notRebounding = contentY() + height() < contentHeight();
494 const bool notShownByItsOwn = contentY() + diff >= m_headerItem->y() + m_headerItem->height();
495 const bool maximizeVisibleAreaRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationMaximizeVisibleArea;
497 if (!scrolledUp && contentY() == -m_minYExtent) {
498 m_headerItemShownHeight = 0;
499 m_headerItem->setY(contentY());
500 } else if ((scrolledUp && notRebounding && notShownByItsOwn && !maximizeVisibleAreaRunning) || (m_headerItemShownHeight > 0)) {
501 if (maximizeVisibleAreaRunning && diff > 0) // If we are maximizing and the header was shown, make sure we hide it
502 m_headerItemShownHeight -= diff;
503 else
504 m_headerItemShownHeight += diff;
505 if (contentY() == -m_minYExtent) {
506 m_headerItemShownHeight = 0;
507 } else {
508 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
509 }
510 if (m_headerItemShownHeight > 0) {
511 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
512 } else {
513 m_headerItem->setY(-m_minYExtent);
514 }
515 }
516 }
517 // We will be changing the clip item, need to accomadate for it
518 // otherwise we move the firstItem down/up twice (unless the
519 // show header animation is running, where we want to keep the viewport stable)
520 if (!showHeaderAnimationRunning) {
521 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
522 } else {
523 diff = -diff;
524 }
525 }
526 if (!m_visibleItems.isEmpty()) {
527 updateClipItem();
528 ListItem *firstItem = m_visibleItems.first();
529 firstItem->setY(firstItem->y() + diff);
530 if (showHeaderAnimationRunning) {
531 adjustMinYExtent();
532 }
533 }
535 m_previousContentY = contentY();
536 layout();
537 polish();
540void ListViewWithPageHeader::createDelegateModel()
542#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
543 m_delegateModel = new QQuickVisualDataModel(qmlContext(this), this);
544 connect(m_delegateModel, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(itemCreated(int,QQuickItem*)));
545#else
546 m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
547 connect(m_delegateModel, SIGNAL(createdItem(int,QObject*)), this, SLOT(itemCreated(int,QObject*)));
548#endif
549 if (isComponentComplete())
550 m_delegateModel->componentComplete();
551 updateWatchedRoles();
554void ListViewWithPageHeader::refill()
556 if (m_inLayout) {
557 return;
558 }
559 if (!isComponentComplete()) {
560 return;
561 }
563 const qreal buffer = height() * bufferRatio;
564 const qreal from = contentY();
565 const qreal to = from + height();
566 const qreal bufferFrom = from - buffer;
567 const qreal bufferTo = to + buffer;
569 bool added = addVisibleItems(from, to, false);
570 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
571 added |= addVisibleItems(bufferFrom, bufferTo, true);
573 if (added || removed) {
574 m_contentHeightDirty = true;
575 }
578bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
580 if (!delegate())
581 return false;
583 if (m_delegateModel->count() == 0)
584 return false;
586 ListItem *item;
587// qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
589 int modelIndex = 0;
590 qreal pos = 0;
591 if (!m_visibleItems.isEmpty()) {
592 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
593 item = m_visibleItems.last();
594 pos = item->y() + item->height() + m_clipItem->y();
595 }
596 bool changed = false;
597// qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
598 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
599// qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
600 if (!(item = createItem(modelIndex, asynchronous)))
601 break;
602 pos += item->height();
603 ++modelIndex;
604 changed = true;
605 }
607 modelIndex = 0;
608 pos = 0;
609 if (!m_visibleItems.isEmpty()) {
610 modelIndex = m_firstVisibleIndex - 1;
611 item = m_visibleItems.first();
612 pos = item->y() + m_clipItem->y();
613 }
614 while (modelIndex >= 0 && pos > fillFrom) {
615// qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
616 if (!(item = createItem(modelIndex, asynchronous)))
617 break;
618 pos -= item->height();
619 --modelIndex;
620 changed = true;
621 }
623 return changed;
626void ListViewWithPageHeader::reallyReleaseItem(ListItem *listItem)
628 QQuickItem *item = listItem->m_item;
629#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
630 QQuickVisualModel::ReleaseFlags flags = m_delegateModel->release(item);
631 if (flags & QQuickVisualModel::Destroyed) {
632#else
633 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
634 if (flags & QQmlDelegateModel::Destroyed) {
635#endif
636 item->setParentItem(nullptr);
637 }
638 delete listItem->m_sectionItem;
639 delete listItem;
642void ListViewWithPageHeader::releaseItem(ListItem *listItem)
644 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(listItem->m_item);
645 itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
646 m_itemsToRelease << listItem;
649void ListViewWithPageHeader::updateWatchedRoles()
651 if (m_delegateModel) {
652 QList<QByteArray> roles;
653 if (!m_sectionProperty.isEmpty())
654 roles << m_sectionProperty.toUtf8();
655 m_delegateModel->setWatchedRoles(roles);
656 }
659QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
661 if (!m_sectionDelegate)
662 return nullptr;
664 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
665 if (modelIndex > 0) {
666 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
667 if (section == prevSection)
668 return nullptr;
669 }
670 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
671 // Already inserted items can't steal next section header
672 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
673 if (section == nextSection) {
674 // Steal the section header
675 ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
676 if (nextItem) {
677 QQuickItem *sectionItem = nextItem->m_sectionItem;
678 nextItem->m_sectionItem = nullptr;
679 return sectionItem;
680 }
681 }
682 }
684 return getSectionItem(section);
687QQuickItem *ListViewWithPageHeader::getSectionItem(const QString &sectionText)
689 QQuickItem *sectionItem = nullptr;
691 QQmlContext *creationContext = m_sectionDelegate->creationContext();
692 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
693 context->setContextProperty(QLatin1String("section"), sectionText);
694 context->setContextProperty(QLatin1String("delegateIndex"), -1);
695 QObject *nobj = m_sectionDelegate->beginCreate(context);
696 if (nobj) {
697 QQml_setParent_noEvent(context, nobj);
698 sectionItem = qobject_cast<QQuickItem *>(nobj);
699 if (!sectionItem) {
700 delete nobj;
701 } else {
702 sectionItem->setZ(2);
703 QQml_setParent_noEvent(sectionItem, m_clipItem);
704 sectionItem->setParentItem(m_clipItem);
705 }
706 } else {
707 delete context;
708 }
709 m_sectionDelegate->completeCreate();
711 // TODO attach to sectionItem so we can accomodate to it changing its height
713 return sectionItem;
716bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
718// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
719 // Do not remove items if we are overshooting up or down, since we'll come back
720 // to the "stable" position and delete/create items without any reason
721 if (contentY() < -m_minYExtent) {
722 return false;
723 } else if (contentY() + height() > contentHeight()) {
724 return false;
725 }
726 bool changed = false;
728 bool foundVisible = false;
729 int i = 0;
730 int removedItems = 0;
731 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
732 while (i < m_visibleItems.count()) {
733 ListItem *item = m_visibleItems[i];
734 const qreal pos = item->y() + m_clipItem->y();
735// qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
736 if (pos + item->height() < bufferFrom || pos > bufferTo) {
737// qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
738 releaseItem(item);
739 m_visibleItems.removeAt(i);
740 changed = true;
741 ++removedItems;
742 } else {
743 if (!foundVisible) {
744 foundVisible = true;
745 const int itemIndex = m_firstVisibleIndex + removedItems + i;
746 m_firstVisibleIndex = itemIndex;
747 }
748 ++i;
749 }
750 }
751 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
752 adjustMinYExtent();
753 }
755 return changed;
758ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
760// qDebug() << "CREATE ITEM" << modelIndex;
761 if (asynchronous && m_asyncRequestedIndex != -1)
762 return nullptr;
764 m_asyncRequestedIndex = -1;
765#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
766 QQuickItem *item = m_delegateModel->item(modelIndex, asynchronous);
767#else
768 QObject* object = m_delegateModel->object(modelIndex, asynchronous);
769 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
770#endif
771 if (!item) {
772#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
773 m_asyncRequestedIndex = modelIndex;
774#else
775 if (object) {
776 m_delegateModel->release(object);
777 if (!m_delegateValidated) {
778 m_delegateValidated = true;
779 QObject* delegateObj = delegate();
780 qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
781 }
782 } else {
783 m_asyncRequestedIndex = modelIndex;
784 }
785#endif
786 return 0;
787 } else {
788// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
789 ListItem *listItem = new ListItem;
790 listItem->m_item = item;
791 listItem->m_sectionItem = getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/);
792 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
793 ListItem *prevItem = itemAtIndex(modelIndex - 1);
794 bool lostItem = false; // Is an item that we requested async but because of model changes
795 // it is no longer attached to any of the existing items (has no prev nor next item)
796 // nor is the first item
797 if (prevItem) {
798 listItem->setY(prevItem->y() + prevItem->height());
799 } else {
800 ListItem *currItem = itemAtIndex(modelIndex);
801 if (currItem) {
802 // There's something already in m_visibleItems at out index, meaning this is an insert, so attach to its top
803 listItem->setY(currItem->y() - listItem->height());
804 } else {
805 ListItem *nextItem = itemAtIndex(modelIndex + 1);
806 if (nextItem) {
807 listItem->setY(nextItem->y() - listItem->height());
808 } else if (modelIndex == 0 && m_headerItem) {
809 listItem->setY(m_headerItem->height());
810 } else if (!m_visibleItems.isEmpty()) {
811 lostItem = true;
812 }
813 }
814 }
815 if (lostItem) {
816 releaseItem(listItem);
817 listItem = nullptr;
818 } else {
819 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() <= contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
820 if (m_visibleItems.isEmpty()) {
821 m_visibleItems << listItem;
822 } else {
823 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
824 }
825 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
826 m_firstVisibleIndex = modelIndex;
827 polish();
828 }
829 if (listItem->m_sectionItem) {
830 QQmlContext *context = QQmlEngine::contextForObject(listItem->m_sectionItem)->parentContext();
831 context->setContextProperty(QLatin1String("delegateIndex"), modelIndex);
832 }
833 adjustMinYExtent();
834 m_contentHeightDirty = true;
835 }
836 return listItem;
837 }
840#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
841void ListViewWithPageHeader::itemCreated(int modelIndex, QQuickItem *item)
843#else
844void ListViewWithPageHeader::itemCreated(int modelIndex, QObject *object)
846 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
847 if (!item) {
848 qWarning() << "ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
849 return;
850 }
851#endif
852// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
854 item->setParentItem(m_clipItem);
855 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
856 context->setContextProperty(QLatin1String("ListViewWithPageHeader"), this);
857 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
858 context->setContextProperty(QLatin1String("culled"), QVariant::fromValue<bool>(false));
859 if (modelIndex == m_asyncRequestedIndex) {
860 createItem(modelIndex, false);
861 refill();
862 }
865void ListViewWithPageHeader::updateClipItem()
867 m_clipItem->setHeight(height() - m_headerItemShownHeight);
868 m_clipItem->setY(contentY() + m_headerItemShownHeight);
869 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
872void ListViewWithPageHeader::onContentHeightChanged()
874 updateClipItem();
877void ListViewWithPageHeader::onContentWidthChanged()
879 m_clipItem->setWidth(contentItem()->width());
882void ListViewWithPageHeader::onHeightChanged()
884 polish();
888#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
889void ListViewWithPageHeader::onModelUpdated(const QQuickChangeSet &changeSet, bool /*reset*/)
890#else
891void ListViewWithPageHeader::onModelUpdated(const QQmlChangeSet &changeSet, bool /*reset*/)
892#endif
894 // TODO Do something with reset
895// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
896 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
898#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
899 Q_FOREACH(const QQuickChangeSet::Remove &remove, changeSet.removes()) {
900#else
901 Q_FOREACH(const QQmlChangeSet::Remove &remove, changeSet.removes()) {
902#endif
903// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove.index << remove.count;
904 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
905 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
906 // If all the items we are removing are either not created or culled
907 // we have to grow down to avoid viewport changing
908 bool growDown = true;
909 for (int i = 0; growDown && i < remove.count; ++i) {
910 const int modelIndex = remove.index + i;
911 ListItem *item = itemAtIndex(modelIndex);
912 if (item && !item->culled()) {
913 growDown = false;
914 }
915 }
916 for (int i = remove.count - 1; i >= 0; --i) {
917 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
918 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
919 ListItem *item = m_visibleItems[visibleIndex];
920 // Pass the section item down if needed
921 if (item->m_sectionItem && visibleIndex + 1 < m_visibleItems.count()) {
922 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
923 if (!nextItem->m_sectionItem) {
924 nextItem->m_sectionItem = item->m_sectionItem;
925 item->m_sectionItem = nullptr;
926 }
927 }
928 releaseItem(item);
929 m_visibleItems.removeAt(visibleIndex);
930 }
931 }
932 if (growDown) {
933 adjustMinYExtent();
934 } else if (remove.index <= m_firstVisibleIndex) {
935 if (!m_visibleItems.isEmpty()) {
936 // We removed the first item that is the one that positions the rest
937 // position the new first item correctly
938 m_visibleItems.first()->setY(oldFirstValidIndexPos);
939 } else {
940 m_firstVisibleIndex = -1;
941 }
942 }
943 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
944 m_firstVisibleIndex -= remove.count;
945 }
946 for (int i = remove.count - 1; i >= 0; --i) {
947 const int modelIndex = remove.index + i;
948 if (modelIndex == m_asyncRequestedIndex) {
949 m_asyncRequestedIndex = -1;
950 } else if (modelIndex < m_asyncRequestedIndex) {
951 m_asyncRequestedIndex--;
952 }
953 }
954 }
956#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
957 Q_FOREACH(const QQuickChangeSet::Insert &insert, changeSet.inserts()) {
958#else
959 Q_FOREACH(const QQmlChangeSet::Insert &insert, changeSet.inserts()) {
960#endif
961// qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
962 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
963 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
964 if (insertingInValidIndexes || firstItemWithViewOnTop)
965 {
966 // If the items we are adding won't be really visible
967 // we grow up instead of down to not change the viewport
968 bool growUp = false;
969 if (!firstItemWithViewOnTop) {
970 for (int i = 0; i < m_visibleItems.count(); ++i) {
971 if (!m_visibleItems[i]->culled()) {
972 if (insert.index <= m_firstVisibleIndex + i) {
973 growUp = true;
974 }
975 break;
976 }
977 }
978 }
980 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
981 for (int i = insert.count - 1; i >= 0; --i) {
982 const int modelIndex = insert.index + i;
983 ListItem *item = createItem(modelIndex, false);
984 if (growUp) {
985 ListItem *firstItem = m_visibleItems.first();
986 firstItem->setY(firstItem->y() - item->height());
987 adjustMinYExtent();
988 }
989 // Adding an item may break a "same section" chain, so check
990 // if we need adding a new section item
991 if (m_sectionDelegate) {
992 ListItem *nextItem = itemAtIndex(modelIndex + 1);
993 if (nextItem && !nextItem->m_sectionItem) {
994 nextItem->m_sectionItem = getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/);
995 if (growUp && nextItem->m_sectionItem) {
996 ListItem *firstItem = m_visibleItems.first();
997 firstItem->setY(firstItem->y() - nextItem->m_sectionItem->height());
998 adjustMinYExtent();
999 }
1003 if (firstItemWithViewOnTop) {
1004 ListItem *firstItem = m_visibleItems.first();
1005 firstItem->setY(oldFirstValidIndexPos);
1007 } else if (insert.index <= m_firstVisibleIndex) {
1008 m_firstVisibleIndex += insert.count;
1011 for (int i = insert.count - 1; i >= 0; --i) {
1012 const int modelIndex = insert.index + i;
1013 if (modelIndex <= m_asyncRequestedIndex) {
1014 m_asyncRequestedIndex++;
1019#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
1020 Q_FOREACH(const QQuickChangeSet::Change &change, changeSet.changes()) {
1021#else
1022 Q_FOREACH(const QQmlChangeSet::Change &change, changeSet.changes()) {
1023#endif
1024// qDebug() << "ListViewWithPageHeader::onModelUpdated Change" << change.index << change.count;
1025 for (int i = change.index; i < change.count; ++i) {
1026 ListItem *item = itemAtIndex(i);
1027 if (item && item->m_sectionItem) {
1028 QQmlContext *context = QQmlEngine::contextForObject(item->m_sectionItem)->parentContext();
1029 const QString sectionText = m_delegateModel->stringValue(i, m_sectionProperty);
1030 context->setContextProperty(QLatin1String("section"), sectionText);
1035 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
1036 adjustMinYExtent();
1039 for (int i = 0; i < m_visibleItems.count(); ++i) {
1040 ListItem *item = m_visibleItems[i];
1041 if (item->m_sectionItem) {
1042 QQmlContext *context = QQmlEngine::contextForObject(item->m_sectionItem)->parentContext();
1043 context->setContextProperty(QLatin1String("delegateIndex"), m_firstVisibleIndex + i);
1047 layout();
1048 polish();
1049 m_contentHeightDirty = true;
1052void ListViewWithPageHeader::onShowHeaderAnimationFinished()
1054 m_contentHeightDirty = true;
1055 polish();
1058void ListViewWithPageHeader::itemGeometryChanged(QQuickItem * /*item*/, const QRectF &newGeometry, const QRectF &oldGeometry)
1060 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
1061 if (heightDiff != 0) {
1062 if (oldGeometry.y() + oldGeometry.height() + m_clipItem->y() < contentY() && !m_visibleItems.isEmpty()) {
1063 ListItem *firstItem = m_visibleItems.first();
1064 firstItem->setY(firstItem->y() - heightDiff);
1065 adjustMinYExtent();
1066 layout();
1068 refill();
1069 adjustMinYExtent();
1070 polish();
1071 m_contentHeightDirty = true;
1075void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
1077 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
1078 if (m_headerItemShownHeight > 0) {
1079 // If the header is shown because of the clip
1080 // Change its size
1081 m_headerItemShownHeight += heightDiff;
1082 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
1083 updateClipItem();
1084 adjustMinYExtent();
1085 } else {
1086 if (oldHeaderY + oldHeaderHeight > contentY()) {
1087 // If the header is shown because its position
1088 // Change its size
1089 ListItem *firstItem = m_visibleItems.first();
1090 firstItem->setY(firstItem->y() + heightDiff);
1091 layout();
1092 } else {
1093 // If the header is not on screen, just change the start of the list
1094 // so the viewport is not changed
1095 adjustMinYExtent();
1101void ListViewWithPageHeader::adjustMinYExtent()
1103 if (m_visibleItems.isEmpty()) {
1104 m_minYExtent = 0;
1105 } else {
1106 qreal nonCreatedHeight = 0;
1107 if (m_firstVisibleIndex != 0) {
1108 // Calculate the average height of items to estimate the position of the list start
1109 const int visibleItems = m_visibleItems.count();
1110 qreal visibleItemsHeight = 0;
1111 Q_FOREACH(ListItem *item, m_visibleItems) {
1112 visibleItemsHeight += item->height();
1114 nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
1115// qDebug() << m_firstVisibleIndex << visibleItemsHeight << visibleItems << nonCreatedHeight;
1117 m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + (m_headerItem ? m_headerItem->implicitHeight() : 0);
1121ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
1123 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
1124 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
1125 return m_visibleItems[visibleIndexedModelIndex];
1127 return nullptr;
1130void ListViewWithPageHeader::layout()
1132 if (m_inLayout)
1133 return;
1135 m_inLayout = true;
1136 if (!m_visibleItems.isEmpty()) {
1137 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
1138 const qreal visibleTo = contentY() + height() - m_clipItem->y();
1140 qreal pos = m_visibleItems.first()->y();
1142// qDebug() << "ListViewWithPageHeader::layout Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
1143 int firstReallyVisibleItem = -1;
1144 int modelIndex = m_firstVisibleIndex;
1145 Q_FOREACH(ListItem *item, m_visibleItems) {
1146 const bool cull = pos + item->height() <= visibleFrom || pos >= visibleTo;
1147 item->setCulled(cull);
1148 item->setY(pos);
1149 if (!cull && firstReallyVisibleItem == -1) {
1150 firstReallyVisibleItem = modelIndex;
1151 if (m_topSectionItem) {
1152 // Positing the top section sticky item is a two step process
1153 // First we set it either we cull it (because it doesn't need to be sticked to the top)
1154 // or stick it to the top
1155 // Then after the loop we'll make sure that if there's another section just below it
1156 // pushed the sticky section up to make it disappear
1157 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
1158 bool showStickySectionItem;
1159 // We need to show the "top section sticky item" when the position at the "top" of the
1160 // viewport is bigger than the start of the position of the first visible item
1161 // i.e. the first visible item starts before the viewport, or when the first
1162 // visible item starts just at the viewport start and it does not have its own section item
1163 if (topSectionStickPos > pos) {
1164 showStickySectionItem = true;
1165 } else if (topSectionStickPos == pos) {
1166 showStickySectionItem = !item->m_sectionItem;
1167 } else {
1168 showStickySectionItem = false;
1170 if (!showStickySectionItem) {
1171 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
1172 if (item->m_sectionItem) {
1173 // This seems it should happen since why would we cull the top section
1174 // if the first visible item has no section header? This only happens briefly
1175 // when increasing the height of a list that is at the bottom, the m_topSectionItem
1176 // gets shown shortly in the next polish call
1177 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(false);
1179 } else {
1180 // Update the top sticky section header
1181 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1182 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
1183 context->setContextProperty(QLatin1String("section"), section);
1185 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
1186 m_topSectionItem->setY(topSectionStickPos);
1187 int delegateIndex = modelIndex;
1188 // Look for the first index with this section text
1189 while (delegateIndex > 0) {
1190 const QString prevSection = m_delegateModel->stringValue(delegateIndex - 1, m_sectionProperty);
1191 if (prevSection != section)
1192 break;
1193 delegateIndex--;
1195 context->setContextProperty(QLatin1String("delegateIndex"), delegateIndex);
1196 if (item->m_sectionItem) {
1197 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(true);
1202 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
1203 const qreal clipFrom = visibleFrom + (!item->m_sectionItem && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
1204 if (!cull && pos < clipFrom) {
1205 context->setContextProperty(QLatin1String("heightToClip"), clipFrom - pos);
1206 } else {
1207 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
1209// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
1210 pos += item->height();
1211 ++modelIndex;
1214 // Second step of section sticky item positioning
1215 // Look at the next section header, check if it's pushing up the sticky one
1216 if (m_topSectionItem) {
1217 if (firstReallyVisibleItem >= 0) {
1218 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
1219 ListItem *item = m_visibleItems[i];
1220 if (item->m_sectionItem) {
1221 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
1222 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
1224 break;
1230 m_inLayout = false;
1233void ListViewWithPageHeader::updatePolish()
1235 Q_FOREACH(ListItem *item, m_itemsToRelease)
1236 reallyReleaseItem(item);
1237 m_itemsToRelease.clear();
1239 if (!model())
1240 return;
1242 layout();
1244 refill();
1246 if (m_contentHeightDirty) {
1247 qreal contentHeight;
1248 if (m_visibleItems.isEmpty()) {
1249 contentHeight = m_headerItem ? m_headerItem->height() : 0;
1250 } else {
1251 const int modelCount = model()->rowCount();
1252 const int visibleItems = m_visibleItems.count();
1253 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
1254 qreal nonCreatedHeight = 0;
1255 if (lastValidIndex != modelCount - 1) {
1256 const int visibleItems = m_visibleItems.count();
1257 qreal visibleItemsHeight = 0;
1258 Q_FOREACH(ListItem *item, m_visibleItems) {
1259 visibleItemsHeight += item->height();
1261 const int unknownSizes = modelCount - (m_firstVisibleIndex + visibleItems);
1262 nonCreatedHeight = unknownSizes * visibleItemsHeight / visibleItems;
1264 ListItem *item = m_visibleItems.last();
1265 contentHeight = nonCreatedHeight + item->y() + item->height() + m_clipItem->y();
1266 if (m_firstVisibleIndex != 0) {
1267 // Make sure that if we are shrinking we tell the view we still fit
1268 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
1272 m_contentHeightDirty = false;
1273 adjustMinYExtent();
1274 setContentHeight(contentHeight);
1278#include "moc_listviewwithpageheader.cpp"