on 79a23b2 help sign in
unity8/plugins/ListViewWithPageHeader/listviewwithpageheader.cpp
1277 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);
144ListViewWithPageHeader::ListViewWithPageHeader()
145 : m_delegateModel(nullptr)
146 , m_asyncRequestedIndex(-1)
147 , m_delegateValidated(false)
148 , m_firstVisibleIndex(-1)
149 , m_minYExtent(0)
150 , m_contentHeightDirty(false)
151 , m_headerItem(nullptr)
152 , m_previousContentY(0)
153 , m_headerItemShownHeight(0)
154 , m_sectionDelegate(nullptr)
155 , m_topSectionItem(nullptr)
156 , m_forceNoClip(false)
157 , m_inLayout(false)
158 , m_inContentHeightKeepHeaderShown(false)
160 m_clipItem = new QQuickItem(contentItem());
161// m_clipItem = new QQuickRectangle(contentItem());
162// ((QQuickRectangle*)m_clipItem)->setColor(Qt::gray);
164 m_contentYAnimation = new QQuickNumberAnimation(this);
165 m_contentYAnimation->setEasing(QEasingCurve::OutQuad);
166 m_contentYAnimation->setProperty("contentY");
167 m_contentYAnimation->setDuration(200);
168 m_contentYAnimation->setTargetObject(this);
170 connect(this, SIGNAL(contentWidthChanged()), this, SLOT(onContentWidthChanged()));
171 connect(this, SIGNAL(contentHeightChanged()), this, SLOT(onContentHeightChanged()));
172 connect(this, SIGNAL(heightChanged()), this, SLOT(onHeightChanged()));
173 connect(m_contentYAnimation, SIGNAL(stopped()), this, SLOT(onShowHeaderAnimationFinished()));
175 setFlickableDirection(VerticalFlick);
178ListViewWithPageHeader::~ListViewWithPageHeader()
182QAbstractItemModel *ListViewWithPageHeader::model() const
184 return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
187void ListViewWithPageHeader::setModel(QAbstractItemModel *model)
189 if (model != this->model()) {
190 if (!m_delegateModel) {
191 createDelegateModel();
192 } else {
193#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
194 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
195 }
196 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
197 connect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
198#else
199 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
200 }
201 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
202 connect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
203#endif
204 Q_EMIT modelChanged();
205 polish();
206 // TODO?
207// Q_EMIT contentHeightChanged();
208// Q_EMIT contentYChanged();
209 }
212QQmlComponent *ListViewWithPageHeader::delegate() const
214 return m_delegateModel ? m_delegateModel->delegate() : nullptr;
217void ListViewWithPageHeader::setDelegate(QQmlComponent *delegate)
219 if (delegate != this->delegate()) {
220 if (!m_delegateModel) {
221 createDelegateModel();
222 }
224 // Cleanup the existing items
225 Q_FOREACH(ListItem *item, m_visibleItems)
226 releaseItem(item);
227 m_visibleItems.clear();
228 m_firstVisibleIndex = -1;
229 adjustMinYExtent();
230 setContentY(0);
231 m_clipItem->setY(0);
232 if (m_topSectionItem) {
233 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
234 }
236 m_delegateModel->setDelegate(delegate);
238 Q_EMIT delegateChanged();
239 m_delegateValidated = false;
240 m_contentHeightDirty = true;
241 polish();
242 }
245QQuickItem *ListViewWithPageHeader::header() const
247 return m_headerItem;
250void ListViewWithPageHeader::setHeader(QQuickItem *headerItem)
252 if (m_headerItem != headerItem) {
253 qreal oldHeaderHeight = 0;
254 qreal oldHeaderY = 0;
255 if (m_headerItem) {
256 oldHeaderHeight = m_headerItem->height();
257 oldHeaderY = m_headerItem->y();
258 m_headerItem->setParentItem(nullptr);
259 }
260 m_headerItem = headerItem;
261 if (m_headerItem) {
262 m_headerItem->setParentItem(contentItem());
263 m_headerItem->setZ(1);
264 }
265 qreal newHeaderHeight = m_headerItem ? m_headerItem->height() : 0;
266 if (!m_visibleItems.isEmpty() && newHeaderHeight != oldHeaderHeight) {
267 headerHeightChanged(newHeaderHeight, oldHeaderHeight, oldHeaderY);
268 polish();
269 m_contentHeightDirty = true;
270 }
271 Q_EMIT headerChanged();
272 }
275QQmlComponent *ListViewWithPageHeader::sectionDelegate() const
277 return m_sectionDelegate;
280void ListViewWithPageHeader::setSectionDelegate(QQmlComponent *delegate)
282 if (delegate != m_sectionDelegate) {
283 // TODO clean existing sections
285 m_sectionDelegate = delegate;
287 m_topSectionItem = getSectionItem(QString());
288 m_topSectionItem->setZ(3);
289 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
291 // TODO create sections for existing items
293 Q_EMIT sectionDelegateChanged();
294 }
297QString ListViewWithPageHeader::sectionProperty() const
299 return m_sectionProperty;
302void ListViewWithPageHeader::setSectionProperty(const QString &property)
304 if (property != m_sectionProperty) {
305 m_sectionProperty = property;
307 updateWatchedRoles();
309 // TODO recreate sections
311 Q_EMIT sectionPropertyChanged();
312 }
315bool ListViewWithPageHeader::forceNoClip() const
317 return m_forceNoClip;
320void ListViewWithPageHeader::setForceNoClip(bool noClip)
322 if (noClip != m_forceNoClip) {
323 m_forceNoClip = noClip;
324 updateClipItem();
325 Q_EMIT forceNoClipChanged();
326 }
329void ListViewWithPageHeader::positionAtBeginning()
331 if (m_delegateModel->count() <= 0)
332 return;
334 qreal headerHeight = (m_headerItem ? m_headerItem->height() : 0);
335 if (m_firstVisibleIndex != 0) {
336 // TODO This could be optimized by trying to reuse the interesection
337 // of items that may end up intersecting between the existing
338 // m_visibleItems and the items we are creating in the next loop
339 Q_FOREACH(ListItem *item, m_visibleItems)
340 releaseItem(item);
341 m_visibleItems.clear();
342 m_firstVisibleIndex = -1;
344 // Create the item 0, it will be already correctly positioned at createItem()
345 m_clipItem->setY(0);
346 ListItem *item = createItem(0, false);
347 // Create the subsequent items
348 int modelIndex = 1;
349 qreal pos = item->y() + item->height();
350 const qreal buffer = height() * bufferRatio;
351 const qreal bufferTo = height() + buffer;
352 while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
353 if (!(item = createItem(modelIndex, false)))
354 break;
355 pos += item->height();
356 ++modelIndex;
357 }
359 m_previousContentY = m_visibleItems.first()->y() - headerHeight;
360 }
361 setContentY(m_visibleItems.first()->y() + m_clipItem->y() - headerHeight);
364void ListViewWithPageHeader::showHeader()
366 if (!m_headerItem)
367 return;
369 auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
370 if (to != contentY()) {
371 const bool headerShownByItsOwn = contentY() < m_headerItem->y() + m_headerItem->height();
372 if (headerShownByItsOwn && m_headerItemShownHeight == 0) {
373 // We are not clipping since we are just at the top of the viewport
374 // but because of the showHeader animation we will need to, so
375 // enable the clipping without logically moving the items
376 m_headerItemShownHeight = m_headerItem->y() + m_headerItem->height() - contentY();
377 if (!m_visibleItems.isEmpty()) {
378 updateClipItem();
379 ListItem *firstItem = m_visibleItems.first();
380 firstItem->setY(firstItem->y() - m_headerItemShownHeight);
381 layout();
382 }
383 }
384 m_contentYAnimation->setTo(to);
385 contentYAnimationType = ContentYAnimationShowHeader;
386 m_contentYAnimation->start();
387 }
390QQuickItem *ListViewWithPageHeader::item(int modelIndex) const
392 ListItem *item = itemAtIndex(modelIndex);
393 if (item)
394 return item->m_item;
395 else
396 return nullptr;
399bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex)
401 ListItem *listItem = itemAtIndex(modelIndex);
402 if (listItem) {
403 return maximizeVisibleArea(listItem, listItem->height());
404 }
406 return false;
409bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex, int itemHeight)
411 if (itemHeight < 0)
412 return false;
414 ListItem *listItem = itemAtIndex(modelIndex);
415 if (listItem) {
416 return maximizeVisibleArea(listItem, itemHeight + (listItem->m_sectionItem ? listItem->m_sectionItem->height() : 0));
417 }
419 return false;
422bool ListViewWithPageHeader::maximizeVisibleArea(ListItem *listItem, int listItemHeight)
424 if (listItem) {
425 layout();
426 const auto listItemY = m_clipItem->y() + listItem->y();
427 if (listItemY > contentY() && listItemY + listItemHeight > contentY() + height()) {
428 // we can scroll the list up to show more stuff
429 const auto to = qMin(listItemY, listItemY + listItemHeight - height());
430 m_contentYAnimation->setTo(to);
431 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
432 m_contentYAnimation->start();
433 } else if ((listItemY < contentY() && listItemY + listItemHeight < contentY() + height()) ||
434 (m_topSectionItem && !listItem->m_sectionItem && listItemY - m_topSectionItem->height() < contentY() && listItemY + listItemHeight < contentY() + height()))
435 {
436 // we can scroll the list down to show more stuff
437 auto realVisibleListItemY = listItemY;
438 if (m_topSectionItem) {
439 // If we are showing the top section sticky item and this item doesn't have a section
440 // item we have to make sure to scroll it a bit more so that it is not underlapping
441 // the top section sticky item
442 bool topSectionShown = !QQuickItemPrivate::get(m_topSectionItem)->culled;
443 if (topSectionShown && !listItem->m_sectionItem) {
444 realVisibleListItemY -= m_topSectionItem->height();
445 }
446 }
447 const auto to = qMax(realVisibleListItemY, listItemY + listItemHeight - height());
448 m_contentYAnimation->setTo(to);
449 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
450 m_contentYAnimation->start();
451 }
452 return true;
453 }
454 return false;
457qreal ListViewWithPageHeader::minYExtent() const
459// qDebug() << "ListViewWithPageHeader::minYExtent" << m_minYExtent;
460 return m_minYExtent;
463void ListViewWithPageHeader::componentComplete()
465 if (m_delegateModel)
466 m_delegateModel->componentComplete();
468 QQuickFlickable::componentComplete();
470 polish();
473void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
475 QQuickFlickable::viewportMoved(orient);
476// qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
477 qreal diff = m_previousContentY - contentY();
478 const bool showHeaderAnimationRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationShowHeader;
479 if (m_headerItem) {
480 auto oldHeaderItemShownHeight = m_headerItemShownHeight;
481 if (contentY() < -m_minYExtent) {
482 // Stick the header item to the top when dragging down
483 m_headerItem->setY(contentY());
484 m_headerItem->setHeight(m_headerItem->implicitHeight() + (-m_minYExtent - contentY()));
485 } else {
486 m_headerItem->setHeight(m_headerItem->implicitHeight());
487 // We are going down (but it's not because of the rebound at the end)
488 // (but the header was not shown by it's own position)
489 // or the header is partially shown and we are not doing a maximizeVisibleArea either
490 const bool scrolledUp = m_previousContentY > contentY();
491 const bool notRebounding = contentY() + height() < contentHeight();
492 const bool notShownByItsOwn = contentY() + diff >= m_headerItem->y() + m_headerItem->height();
493 const bool maximizeVisibleAreaRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationMaximizeVisibleArea;
495 if (!scrolledUp && contentY() == -m_minYExtent) {
496 m_headerItemShownHeight = 0;
497 m_headerItem->setY(contentY());
498 } else if ((scrolledUp && notRebounding && notShownByItsOwn && !maximizeVisibleAreaRunning) || (m_headerItemShownHeight > 0) || m_inContentHeightKeepHeaderShown) {
499 if (maximizeVisibleAreaRunning && diff > 0) // If we are maximizing and the header was shown, make sure we hide it
500 m_headerItemShownHeight -= diff;
501 else
502 m_headerItemShownHeight += diff;
503 if (contentY() == -m_minYExtent) {
504 m_headerItemShownHeight = 0;
505 } else {
506 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
507 }
508 if (m_headerItemShownHeight > 0) {
509 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
510 } else {
511 m_headerItem->setY(-m_minYExtent);
512 }
513 }
514 }
515 // We will be changing the clip item, need to accomadate for it
516 // otherwise we move the firstItem down/up twice (unless the
517 // show header animation is running, where we want to keep the viewport stable)
518 if (!showHeaderAnimationRunning) {
519 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
520 } else {
521 diff = -diff;
522 }
523 }
524 if (!m_visibleItems.isEmpty()) {
525 updateClipItem();
526 ListItem *firstItem = m_visibleItems.first();
527 firstItem->setY(firstItem->y() + diff);
528 if (showHeaderAnimationRunning) {
529 adjustMinYExtent();
530 }
531 }
533 m_previousContentY = contentY();
534 layout();
535 polish();
538void ListViewWithPageHeader::createDelegateModel()
540#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
541 m_delegateModel = new QQuickVisualDataModel(qmlContext(this), this);
542 connect(m_delegateModel, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(itemCreated(int,QQuickItem*)));
543#else
544 m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
545 connect(m_delegateModel, SIGNAL(createdItem(int,QObject*)), this, SLOT(itemCreated(int,QObject*)));
546#endif
547 if (isComponentComplete())
548 m_delegateModel->componentComplete();
549 updateWatchedRoles();
552void ListViewWithPageHeader::refill()
554 if (m_inLayout) {
555 return;
556 }
557 if (!isComponentComplete()) {
558 return;
559 }
561 const qreal buffer = height() * bufferRatio;
562 const qreal from = contentY();
563 const qreal to = from + height();
564 const qreal bufferFrom = from - buffer;
565 const qreal bufferTo = to + buffer;
567 bool added = addVisibleItems(from, to, false);
568 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
569 added |= addVisibleItems(bufferFrom, bufferTo, true);
571 if (added || removed) {
572 m_contentHeightDirty = true;
573 }
576bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
578 if (!delegate())
579 return false;
581 if (m_delegateModel->count() == 0)
582 return false;
584 ListItem *item;
585// qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
587 int modelIndex = 0;
588 qreal pos = 0;
589 if (!m_visibleItems.isEmpty()) {
590 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
591 item = m_visibleItems.last();
592 pos = item->y() + item->height() + m_clipItem->y();
593 }
594 bool changed = false;
595// qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
596 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
597// qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
598 if (!(item = createItem(modelIndex, asynchronous)))
599 break;
600 pos += item->height();
601 ++modelIndex;
602 changed = true;
603 }
605 modelIndex = 0;
606 pos = 0;
607 if (!m_visibleItems.isEmpty()) {
608 modelIndex = m_firstVisibleIndex - 1;
609 item = m_visibleItems.first();
610 pos = item->y() + m_clipItem->y();
611 }
612 while (modelIndex >= 0 && pos > fillFrom) {
613// qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
614 if (!(item = createItem(modelIndex, asynchronous)))
615 break;
616 pos -= item->height();
617 --modelIndex;
618 changed = true;
619 }
621 return changed;
624void ListViewWithPageHeader::reallyReleaseItem(ListItem *listItem)
626 QQuickItem *item = listItem->m_item;
627#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
628 QQuickVisualModel::ReleaseFlags flags = m_delegateModel->release(item);
629 if (flags & QQuickVisualModel::Destroyed) {
630#else
631 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
632 if (flags & QQmlDelegateModel::Destroyed) {
633#endif
634 item->setParentItem(nullptr);
635 }
636 delete listItem->m_sectionItem;
637 delete listItem;
640void ListViewWithPageHeader::releaseItem(ListItem *listItem)
642 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(listItem->m_item);
643 itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
644 m_itemsToRelease << listItem;
647void ListViewWithPageHeader::updateWatchedRoles()
649 if (m_delegateModel) {
650 QList<QByteArray> roles;
651 if (!m_sectionProperty.isEmpty())
652 roles << m_sectionProperty.toUtf8();
653 m_delegateModel->setWatchedRoles(roles);
654 }
657QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
659 if (!m_sectionDelegate)
660 return nullptr;
662 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
663 if (modelIndex > 0) {
664 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
665 if (section == prevSection)
666 return nullptr;
667 }
668 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
669 // Already inserted items can't steal next section header
670 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
671 if (section == nextSection) {
672 // Steal the section header
673 ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
674 if (nextItem) {
675 QQuickItem *sectionItem = nextItem->m_sectionItem;
676 nextItem->m_sectionItem = nullptr;
677 return sectionItem;
678 }
679 }
680 }
682 return getSectionItem(section);
685QQuickItem *ListViewWithPageHeader::getSectionItem(const QString &sectionText)
687 QQuickItem *sectionItem = nullptr;
689 QQmlContext *creationContext = m_sectionDelegate->creationContext();
690 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
691 context->setContextProperty(QLatin1String("section"), sectionText);
692 context->setContextProperty(QLatin1String("delegateIndex"), -1);
693 QObject *nobj = m_sectionDelegate->beginCreate(context);
694 if (nobj) {
695 QQml_setParent_noEvent(context, nobj);
696 sectionItem = qobject_cast<QQuickItem *>(nobj);
697 if (!sectionItem) {
698 delete nobj;
699 } else {
700 sectionItem->setZ(2);
701 QQml_setParent_noEvent(sectionItem, m_clipItem);
702 sectionItem->setParentItem(m_clipItem);
703 }
704 } else {
705 delete context;
706 }
707 m_sectionDelegate->completeCreate();
709 // TODO attach to sectionItem so we can accomodate to it changing its height
711 return sectionItem;
714bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
716// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
717 // Do not remove items if we are overshooting up or down, since we'll come back
718 // to the "stable" position and delete/create items without any reason
719 if (contentY() < -m_minYExtent) {
720 return false;
721 } else if (contentY() + height() > contentHeight()) {
722 return false;
723 }
724 bool changed = false;
726 bool foundVisible = false;
727 int i = 0;
728 int removedItems = 0;
729 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
730 while (i < m_visibleItems.count()) {
731 ListItem *item = m_visibleItems[i];
732 const qreal pos = item->y() + m_clipItem->y();
733// qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
734 if (pos + item->height() < bufferFrom || pos > bufferTo) {
735// qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
736 releaseItem(item);
737 m_visibleItems.removeAt(i);
738 changed = true;
739 ++removedItems;
740 } else {
741 if (!foundVisible) {
742 foundVisible = true;
743 const int itemIndex = m_firstVisibleIndex + removedItems + i;
744 m_firstVisibleIndex = itemIndex;
745 }
746 ++i;
747 }
748 }
749 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
750 adjustMinYExtent();
751 }
753 return changed;
756ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
758// qDebug() << "CREATE ITEM" << modelIndex;
759 if (asynchronous && m_asyncRequestedIndex != -1)
760 return nullptr;
762 m_asyncRequestedIndex = -1;
763#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
764 QQuickItem *item = m_delegateModel->item(modelIndex, asynchronous);
765#else
766 QObject* object = m_delegateModel->object(modelIndex, asynchronous);
767 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
768#endif
769 if (!item) {
770#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
771 m_asyncRequestedIndex = modelIndex;
772#else
773 if (object) {
774 m_delegateModel->release(object);
775 if (!m_delegateValidated) {
776 m_delegateValidated = true;
777 QObject* delegateObj = delegate();
778 qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
779 }
780 } else {
781 m_asyncRequestedIndex = modelIndex;
782 }
783#endif
784 return 0;
785 } else {
786// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
787 ListItem *listItem = new ListItem;
788 listItem->m_item = item;
789 listItem->m_sectionItem = getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/);
790 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
791 ListItem *prevItem = itemAtIndex(modelIndex - 1);
792 bool lostItem = false; // Is an item that we requested async but because of model changes
793 // it is no longer attached to any of the existing items (has no prev nor next item)
794 // nor is the first item
795 if (prevItem) {
796 listItem->setY(prevItem->y() + prevItem->height());
797 } else {
798 ListItem *currItem = itemAtIndex(modelIndex);
799 if (currItem) {
800 // There's something already in m_visibleItems at out index, meaning this is an insert, so attach to its top
801 listItem->setY(currItem->y() - listItem->height());
802 } else {
803 ListItem *nextItem = itemAtIndex(modelIndex + 1);
804 if (nextItem) {
805 listItem->setY(nextItem->y() - listItem->height());
806 } else if (modelIndex == 0 && m_headerItem) {
807 listItem->setY(m_headerItem->height());
808 } else if (!m_visibleItems.isEmpty()) {
809 lostItem = true;
810 }
811 }
812 }
813 if (lostItem) {
814 releaseItem(listItem);
815 listItem = nullptr;
816 } else {
817 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() <= contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
818 if (m_visibleItems.isEmpty()) {
819 m_visibleItems << listItem;
820 } else {
821 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
822 }
823 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
824 m_firstVisibleIndex = modelIndex;
825 polish();
826 }
827 if (listItem->m_sectionItem) {
828 QQmlContext *context = QQmlEngine::contextForObject(listItem->m_sectionItem)->parentContext();
829 context->setContextProperty(QLatin1String("delegateIndex"), modelIndex);
830 }
831 adjustMinYExtent();
832 m_contentHeightDirty = true;
833 }
834 return listItem;
835 }
838#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
839void ListViewWithPageHeader::itemCreated(int modelIndex, QQuickItem *item)
841#else
842void ListViewWithPageHeader::itemCreated(int modelIndex, QObject *object)
844 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
845 if (!item) {
846 qWarning() << "ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
847 return;
848 }
849#endif
850// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
852 item->setParentItem(m_clipItem);
853 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
854 context->setContextProperty(QLatin1String("ListViewWithPageHeader"), this);
855 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
856 if (modelIndex == m_asyncRequestedIndex) {
857 createItem(modelIndex, false);
858 refill();
859 }
862void ListViewWithPageHeader::updateClipItem()
864 m_clipItem->setHeight(height() - m_headerItemShownHeight);
865 m_clipItem->setY(contentY() + m_headerItemShownHeight);
866 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
869void ListViewWithPageHeader::onContentHeightChanged()
871 updateClipItem();
874void ListViewWithPageHeader::onContentWidthChanged()
876 m_clipItem->setWidth(contentItem()->width());
879void ListViewWithPageHeader::onHeightChanged()
881 polish();
885#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
886void ListViewWithPageHeader::onModelUpdated(const QQuickChangeSet &changeSet, bool /*reset*/)
887#else
888void ListViewWithPageHeader::onModelUpdated(const QQmlChangeSet &changeSet, bool /*reset*/)
889#endif
891 // TODO Do something with reset
892// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
893 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
895#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
896 Q_FOREACH(const QQuickChangeSet::Remove &remove, changeSet.removes()) {
897#else
898 Q_FOREACH(const QQmlChangeSet::Remove &remove, changeSet.removes()) {
899#endif
900// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove.index << remove.count;
901 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
902 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
903 // If all the items we are removing are either not created or culled
904 // we have to grow down to avoid viewport changing
905 bool growDown = true;
906 for (int i = 0; growDown && i < remove.count; ++i) {
907 const int modelIndex = remove.index + i;
908 ListItem *item = itemAtIndex(modelIndex);
909 if (item && !item->culled()) {
910 growDown = false;
911 }
912 }
913 for (int i = remove.count - 1; i >= 0; --i) {
914 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
915 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
916 ListItem *item = m_visibleItems[visibleIndex];
917 // Pass the section item down if needed
918 if (item->m_sectionItem && visibleIndex + 1 < m_visibleItems.count()) {
919 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
920 if (!nextItem->m_sectionItem) {
921 nextItem->m_sectionItem = item->m_sectionItem;
922 item->m_sectionItem = nullptr;
923 }
924 }
925 releaseItem(item);
926 m_visibleItems.removeAt(visibleIndex);
927 }
928 }
929 if (growDown) {
930 adjustMinYExtent();
931 } else if (remove.index <= m_firstVisibleIndex) {
932 if (!m_visibleItems.isEmpty()) {
933 // We removed the first item that is the one that positions the rest
934 // position the new first item correctly
935 m_visibleItems.first()->setY(oldFirstValidIndexPos);
936 } else {
937 m_firstVisibleIndex = -1;
938 }
939 }
940 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
941 m_firstVisibleIndex -= remove.count;
942 }
943 for (int i = remove.count - 1; i >= 0; --i) {
944 const int modelIndex = remove.index + i;
945 if (modelIndex == m_asyncRequestedIndex) {
946 m_asyncRequestedIndex = -1;
947 } else if (modelIndex < m_asyncRequestedIndex) {
948 m_asyncRequestedIndex--;
949 }
950 }
951 }
953#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
954 Q_FOREACH(const QQuickChangeSet::Insert &insert, changeSet.inserts()) {
955#else
956 Q_FOREACH(const QQmlChangeSet::Insert &insert, changeSet.inserts()) {
957#endif
958// qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
959 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
960 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
961 if (insertingInValidIndexes || firstItemWithViewOnTop)
962 {
963 // If the items we are adding won't be really visible
964 // we grow up instead of down to not change the viewport
965 bool growUp = false;
966 if (!firstItemWithViewOnTop) {
967 for (int i = 0; i < m_visibleItems.count(); ++i) {
968 if (!m_visibleItems[i]->culled()) {
969 if (insert.index <= m_firstVisibleIndex + i) {
970 growUp = true;
971 }
972 break;
973 }
974 }
975 }
977 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
978 for (int i = insert.count - 1; i >= 0; --i) {
979 const int modelIndex = insert.index + i;
980 ListItem *item = createItem(modelIndex, false);
981 if (growUp) {
982 ListItem *firstItem = m_visibleItems.first();
983 firstItem->setY(firstItem->y() - item->height());
984 adjustMinYExtent();
985 }
986 // Adding an item may break a "same section" chain, so check
987 // if we need adding a new section item
988 if (m_sectionDelegate) {
989 ListItem *nextItem = itemAtIndex(modelIndex + 1);
990 if (nextItem && !nextItem->m_sectionItem) {
991 nextItem->m_sectionItem = getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/);
992 if (growUp && nextItem->m_sectionItem) {
993 ListItem *firstItem = m_visibleItems.first();
994 firstItem->setY(firstItem->y() - nextItem->m_sectionItem->height());
995 adjustMinYExtent();
996 }
997 }
998 }
999 }
1000 if (firstItemWithViewOnTop) {
1001 ListItem *firstItem = m_visibleItems.first();
1002 firstItem->setY(oldFirstValidIndexPos);
1004 } else if (insert.index <= m_firstVisibleIndex) {
1005 m_firstVisibleIndex += insert.count;
1008 for (int i = insert.count - 1; i >= 0; --i) {
1009 const int modelIndex = insert.index + i;
1010 if (modelIndex <= m_asyncRequestedIndex) {
1011 m_asyncRequestedIndex++;
1016#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
1017 Q_FOREACH(const QQuickChangeSet::Change &change, changeSet.changes()) {
1018#else
1019 Q_FOREACH(const QQmlChangeSet::Change &change, changeSet.changes()) {
1020#endif
1021// qDebug() << "ListViewWithPageHeader::onModelUpdated Change" << change.index << change.count;
1022 for (int i = change.index; i < change.count; ++i) {
1023 ListItem *item = itemAtIndex(i);
1024 if (item && item->m_sectionItem) {
1025 QQmlContext *context = QQmlEngine::contextForObject(item->m_sectionItem)->parentContext();
1026 const QString sectionText = m_delegateModel->stringValue(i, m_sectionProperty);
1027 context->setContextProperty(QLatin1String("section"), sectionText);
1032 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
1033 adjustMinYExtent();
1036 for (int i = 0; i < m_visibleItems.count(); ++i) {
1037 ListItem *item = m_visibleItems[i];
1038 if (item->m_sectionItem) {
1039 QQmlContext *context = QQmlEngine::contextForObject(item->m_sectionItem)->parentContext();
1040 context->setContextProperty(QLatin1String("delegateIndex"), m_firstVisibleIndex + i);
1044 layout();
1045 polish();
1046 m_contentHeightDirty = true;
1049void ListViewWithPageHeader::onShowHeaderAnimationFinished()
1051 m_contentHeightDirty = true;
1052 polish();
1055void ListViewWithPageHeader::itemGeometryChanged(QQuickItem * /*item*/, const QRectF &newGeometry, const QRectF &oldGeometry)
1057 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
1058 if (heightDiff != 0) {
1059 if (oldGeometry.y() + oldGeometry.height() + m_clipItem->y() < contentY() && !m_visibleItems.isEmpty()) {
1060 ListItem *firstItem = m_visibleItems.first();
1061 firstItem->setY(firstItem->y() - heightDiff);
1062 adjustMinYExtent();
1063 layout();
1065 refill();
1066 adjustMinYExtent();
1067 polish();
1068 m_contentHeightDirty = true;
1072void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
1074 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
1075 if (m_headerItemShownHeight > 0) {
1076 // If the header is shown because of the clip
1077 // Change its size
1078 m_headerItemShownHeight += heightDiff;
1079 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
1080 updateClipItem();
1081 adjustMinYExtent();
1082 } else {
1083 if (oldHeaderY + oldHeaderHeight > contentY()) {
1084 // If the header is shown because its position
1085 // Change its size
1086 ListItem *firstItem = m_visibleItems.first();
1087 firstItem->setY(firstItem->y() + heightDiff);
1088 layout();
1089 } else {
1090 // If the header is not on screen, just change the start of the list
1091 // so the viewport is not changed
1092 adjustMinYExtent();
1098void ListViewWithPageHeader::adjustMinYExtent()
1100 if (m_visibleItems.isEmpty()) {
1101 m_minYExtent = 0;
1102 } else {
1103 qreal nonCreatedHeight = 0;
1104 if (m_firstVisibleIndex != 0) {
1105 // Calculate the average height of items to estimate the position of the list start
1106 const int visibleItems = m_visibleItems.count();
1107 qreal visibleItemsHeight = 0;
1108 Q_FOREACH(ListItem *item, m_visibleItems) {
1109 visibleItemsHeight += item->height();
1111 nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
1112// qDebug() << m_firstVisibleIndex << visibleItemsHeight << visibleItems << nonCreatedHeight;
1114 m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + (m_headerItem ? m_headerItem->implicitHeight() : 0);
1118ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
1120 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
1121 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
1122 return m_visibleItems[visibleIndexedModelIndex];
1124 return nullptr;
1127void ListViewWithPageHeader::layout()
1129 if (m_inLayout)
1130 return;
1132 m_inLayout = true;
1133 if (!m_visibleItems.isEmpty()) {
1134 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
1135 const qreal visibleTo = contentY() + height() - m_clipItem->y();
1137 qreal pos = m_visibleItems.first()->y();
1139// qDebug() << "ListViewWithPageHeader::layout Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
1140 int firstReallyVisibleItem = -1;
1141 int modelIndex = m_firstVisibleIndex;
1142 Q_FOREACH(ListItem *item, m_visibleItems) {
1143 const bool cull = pos + item->height() <= visibleFrom || pos >= visibleTo;
1144 item->setCulled(cull);
1145 item->setY(pos);
1146 if (!cull && firstReallyVisibleItem == -1) {
1147 firstReallyVisibleItem = modelIndex;
1148 if (m_topSectionItem) {
1149 // Positing the top section sticky item is a two step process
1150 // First we set it either we cull it (because it doesn't need to be sticked to the top)
1151 // or stick it to the top
1152 // Then after the loop we'll make sure that if there's another section just below it
1153 // pushed the sticky section up to make it disappear
1154 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
1155 bool showStickySectionItem;
1156 // We need to show the "top section sticky item" when the position at the "top" of the
1157 // viewport is bigger than the start of the position of the first visible item
1158 // i.e. the first visible item starts before the viewport, or when the first
1159 // visible item starts just at the viewport start and it does not have its own section item
1160 if (topSectionStickPos > pos) {
1161 showStickySectionItem = true;
1162 } else if (topSectionStickPos == pos) {
1163 showStickySectionItem = !item->m_sectionItem;
1164 } else {
1165 showStickySectionItem = false;
1167 if (!showStickySectionItem) {
1168 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
1169 if (item->m_sectionItem) {
1170 // This seems it should happen since why would we cull the top section
1171 // if the first visible item has no section header? This only happens briefly
1172 // when increasing the height of a list that is at the bottom, the m_topSectionItem
1173 // gets shown shortly in the next polish call
1174 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(false);
1176 } else {
1177 // Update the top sticky section header
1178 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1179 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
1180 context->setContextProperty(QLatin1String("section"), section);
1182 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
1183 m_topSectionItem->setY(topSectionStickPos);
1184 int delegateIndex = modelIndex;
1185 // Look for the first index with this section text
1186 while (delegateIndex > 0) {
1187 const QString prevSection = m_delegateModel->stringValue(delegateIndex - 1, m_sectionProperty);
1188 if (prevSection != section)
1189 break;
1190 delegateIndex--;
1192 context->setContextProperty(QLatin1String("delegateIndex"), delegateIndex);
1193 if (item->m_sectionItem) {
1194 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(true);
1199 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
1200 const qreal clipFrom = visibleFrom + (!item->m_sectionItem && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
1201 if (!cull && pos < clipFrom) {
1202 context->setContextProperty(QLatin1String("heightToClip"), clipFrom - pos);
1203 } else {
1204 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
1206// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
1207 pos += item->height();
1208 ++modelIndex;
1211 // Second step of section sticky item positioning
1212 // Look at the next section header, check if it's pushing up the sticky one
1213 if (m_topSectionItem) {
1214 if (firstReallyVisibleItem >= 0) {
1215 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
1216 ListItem *item = m_visibleItems[i];
1217 if (item->m_sectionItem) {
1218 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
1219 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
1221 break;
1227 m_inLayout = false;
1230void ListViewWithPageHeader::updatePolish()
1232 Q_FOREACH(ListItem *item, m_itemsToRelease)
1233 reallyReleaseItem(item);
1234 m_itemsToRelease.clear();
1236 if (!model())
1237 return;
1239 layout();
1241 refill();
1243 if (m_contentHeightDirty) {
1244 qreal contentHeight;
1245 if (m_visibleItems.isEmpty()) {
1246 contentHeight = m_headerItem ? m_headerItem->height() : 0;
1247 } else {
1248 const int modelCount = model()->rowCount();
1249 const int visibleItems = m_visibleItems.count();
1250 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
1251 qreal nonCreatedHeight = 0;
1252 if (lastValidIndex != modelCount - 1) {
1253 const int visibleItems = m_visibleItems.count();
1254 qreal visibleItemsHeight = 0;
1255 Q_FOREACH(ListItem *item, m_visibleItems) {
1256 visibleItemsHeight += item->height();
1258 const int unknownSizes = modelCount - (m_firstVisibleIndex + visibleItems);
1259 nonCreatedHeight = unknownSizes * visibleItemsHeight / visibleItems;
1261 ListItem *item = m_visibleItems.last();
1262 contentHeight = nonCreatedHeight + item->y() + item->height() + m_clipItem->y();
1263 if (m_firstVisibleIndex != 0) {
1264 // Make sure that if we are shrinking we tell the view we still fit
1265 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
1269 m_contentHeightDirty = false;
1270 adjustMinYExtent();
1271 m_inContentHeightKeepHeaderShown = m_headerItem && m_headerItem->y() == contentY();
1272 setContentHeight(contentHeight);
1273 m_inContentHeightKeepHeaderShown = false;
1277#include "moc_listviewwithpageheader.cpp"