on 3ee3c50 help sign in
unity8/plugins/ListViewWithPageHeader/listviewwithpageheader.cpp
1283 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);
290 connect(m_topSectionItem, SIGNAL(heightChanged()), SIGNAL(stickyHeaderHeightChanged()));
292 // TODO create sections for existing items
294 Q_EMIT sectionDelegateChanged();
295 Q_EMIT stickyHeaderHeightChanged();
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 }
331int ListViewWithPageHeader::stickyHeaderHeight() const
333 return m_topSectionItem ? m_topSectionItem->height() : 0;
336void ListViewWithPageHeader::positionAtBeginning()
338 if (m_delegateModel->count() <= 0)
339 return;
341 qreal headerHeight = (m_headerItem ? m_headerItem->height() : 0);
342 if (m_firstVisibleIndex != 0) {
343 // TODO This could be optimized by trying to reuse the interesection
344 // of items that may end up intersecting between the existing
345 // m_visibleItems and the items we are creating in the next loop
346 Q_FOREACH(ListItem *item, m_visibleItems)
347 releaseItem(item);
348 m_visibleItems.clear();
349 m_firstVisibleIndex = -1;
351 // Create the item 0, it will be already correctly positioned at createItem()
352 m_clipItem->setY(0);
353 ListItem *item = createItem(0, false);
354 // Create the subsequent items
355 int modelIndex = 1;
356 qreal pos = item->y() + item->height();
357 const qreal buffer = height() * bufferRatio;
358 const qreal bufferTo = height() + buffer;
359 while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
360 if (!(item = createItem(modelIndex, false)))
361 break;
362 pos += item->height();
363 ++modelIndex;
364 }
366 m_previousContentY = m_visibleItems.first()->y() - headerHeight;
367 }
368 setContentY(m_visibleItems.first()->y() + m_clipItem->y() - headerHeight);
371void ListViewWithPageHeader::showHeader()
373 if (!m_headerItem)
374 return;
376 auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
377 if (to != contentY()) {
378 const bool headerShownByItsOwn = contentY() < m_headerItem->y() + m_headerItem->height();
379 if (headerShownByItsOwn && m_headerItemShownHeight == 0) {
380 // We are not clipping since we are just at the top of the viewport
381 // but because of the showHeader animation we will need to, so
382 // enable the clipping without logically moving the items
383 m_headerItemShownHeight = m_headerItem->y() + m_headerItem->height() - contentY();
384 if (!m_visibleItems.isEmpty()) {
385 updateClipItem();
386 ListItem *firstItem = m_visibleItems.first();
387 firstItem->setY(firstItem->y() - m_headerItemShownHeight);
388 layout();
389 }
390 }
391 m_contentYAnimation->setTo(to);
392 contentYAnimationType = ContentYAnimationShowHeader;
393 m_contentYAnimation->start();
394 }
397QQuickItem *ListViewWithPageHeader::item(int modelIndex) const
399 ListItem *item = itemAtIndex(modelIndex);
400 if (item)
401 return item->m_item;
402 else
403 return nullptr;
406bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex)
408 ListItem *listItem = itemAtIndex(modelIndex);
409 if (listItem) {
410 return maximizeVisibleArea(listItem, listItem->height());
411 }
413 return false;
416bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex, int itemHeight)
418 if (itemHeight < 0)
419 return false;
421 ListItem *listItem = itemAtIndex(modelIndex);
422 if (listItem) {
423 return maximizeVisibleArea(listItem, itemHeight + (listItem->m_sectionItem ? listItem->m_sectionItem->height() : 0));
424 }
426 return false;
429bool ListViewWithPageHeader::maximizeVisibleArea(ListItem *listItem, int listItemHeight)
431 if (listItem) {
432 layout();
433 const auto listItemY = m_clipItem->y() + listItem->y();
434 if (listItemY > contentY() && listItemY + listItemHeight > contentY() + height()) {
435 // we can scroll the list up to show more stuff
436 const auto to = qMin(listItemY, listItemY + listItemHeight - height());
437 m_contentYAnimation->setTo(to);
438 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
439 m_contentYAnimation->start();
440 } else if ((listItemY < contentY() && listItemY + listItemHeight < contentY() + height()) ||
441 (m_topSectionItem && !listItem->m_sectionItem && listItemY - m_topSectionItem->height() < contentY() && listItemY + listItemHeight < contentY() + height()))
442 {
443 // we can scroll the list down to show more stuff
444 auto realVisibleListItemY = listItemY;
445 if (m_topSectionItem) {
446 // If we are showing the top section sticky item and this item doesn't have a section
447 // item we have to make sure to scroll it a bit more so that it is not underlapping
448 // the top section sticky item
449 bool topSectionShown = !QQuickItemPrivate::get(m_topSectionItem)->culled;
450 if (topSectionShown && !listItem->m_sectionItem) {
451 realVisibleListItemY -= m_topSectionItem->height();
452 }
453 }
454 const auto to = qMax(realVisibleListItemY, listItemY + listItemHeight - height());
455 m_contentYAnimation->setTo(to);
456 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
457 m_contentYAnimation->start();
458 }
459 return true;
460 }
461 return false;
464qreal ListViewWithPageHeader::minYExtent() const
466// qDebug() << "ListViewWithPageHeader::minYExtent" << m_minYExtent;
467 return m_minYExtent;
470void ListViewWithPageHeader::componentComplete()
472 if (m_delegateModel)
473 m_delegateModel->componentComplete();
475 QQuickFlickable::componentComplete();
477 polish();
480void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
482 QQuickFlickable::viewportMoved(orient);
483// qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
484 qreal diff = m_previousContentY - contentY();
485 const bool showHeaderAnimationRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationShowHeader;
486 if (m_headerItem) {
487 const auto oldHeaderItemShownHeight = m_headerItemShownHeight;
488 if (contentY() < -m_minYExtent) {
489 // Stick the header item to the top when dragging down
490 m_headerItem->setY(contentY());
491 m_headerItem->setHeight(m_headerItem->implicitHeight() + (-m_minYExtent - contentY()));
492 } else {
493 m_headerItem->setHeight(m_headerItem->implicitHeight());
494 // We are going down (but it's not because of the rebound at the end)
495 // (but the header was not shown by it's own position)
496 // or the header is partially shown and we are not doing a maximizeVisibleArea either
497 const bool scrolledUp = m_previousContentY > contentY();
498 const bool notRebounding = contentY() + height() < contentHeight();
499 const bool notShownByItsOwn = contentY() + diff >= m_headerItem->y() + m_headerItem->height();
500 const bool maximizeVisibleAreaRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationMaximizeVisibleArea;
502 if (!scrolledUp && (contentY() == -m_minYExtent || (m_headerItemShownHeight == 0 && m_previousContentY == m_headerItem->y()))) {
503 m_headerItemShownHeight = 0;
504 m_headerItem->setY(-m_minYExtent);
505 } else if ((scrolledUp && notRebounding && notShownByItsOwn && !maximizeVisibleAreaRunning) || (m_headerItemShownHeight > 0) || m_inContentHeightKeepHeaderShown) {
506 if (maximizeVisibleAreaRunning && diff > 0) // If we are maximizing and the header was shown, make sure we hide it
507 m_headerItemShownHeight -= diff;
508 else
509 m_headerItemShownHeight += diff;
510 if (contentY() == -m_minYExtent) {
511 m_headerItemShownHeight = 0;
512 } else {
513 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
514 }
515 if (m_headerItemShownHeight > 0) {
516 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
517 } else {
518 m_headerItem->setY(-m_minYExtent);
519 }
520 }
521 }
522 // We will be changing the clip item, need to accomadate for it
523 // otherwise we move the firstItem down/up twice (unless the
524 // show header animation is running, where we want to keep the viewport stable)
525 if (!showHeaderAnimationRunning) {
526 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
527 } else {
528 diff = -diff;
529 }
530 }
531 if (!m_visibleItems.isEmpty()) {
532 updateClipItem();
533 ListItem *firstItem = m_visibleItems.first();
534 firstItem->setY(firstItem->y() + diff);
535 if (showHeaderAnimationRunning) {
536 adjustMinYExtent();
537 }
538 }
540 m_previousContentY = contentY();
541 layout();
542 polish();
545void ListViewWithPageHeader::createDelegateModel()
547#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
548 m_delegateModel = new QQuickVisualDataModel(qmlContext(this), this);
549 connect(m_delegateModel, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(itemCreated(int,QQuickItem*)));
550#else
551 m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
552 connect(m_delegateModel, SIGNAL(createdItem(int,QObject*)), this, SLOT(itemCreated(int,QObject*)));
553#endif
554 if (isComponentComplete())
555 m_delegateModel->componentComplete();
556 updateWatchedRoles();
559void ListViewWithPageHeader::refill()
561 if (m_inLayout) {
562 return;
563 }
564 if (!isComponentComplete()) {
565 return;
566 }
568 const qreal buffer = height() * bufferRatio;
569 const qreal from = contentY();
570 const qreal to = from + height();
571 const qreal bufferFrom = from - buffer;
572 const qreal bufferTo = to + buffer;
574 bool added = addVisibleItems(from, to, false);
575 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
576 added |= addVisibleItems(bufferFrom, bufferTo, true);
578 if (added || removed) {
579 m_contentHeightDirty = true;
580 }
583bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
585 if (!delegate())
586 return false;
588 if (m_delegateModel->count() == 0)
589 return false;
591 ListItem *item;
592// qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
594 int modelIndex = 0;
595 qreal pos = 0;
596 if (!m_visibleItems.isEmpty()) {
597 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
598 item = m_visibleItems.last();
599 pos = item->y() + item->height() + m_clipItem->y();
600 }
601 bool changed = false;
602// qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
603 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
604// qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
605 if (!(item = createItem(modelIndex, asynchronous)))
606 break;
607 pos += item->height();
608 ++modelIndex;
609 changed = true;
610 }
612 modelIndex = 0;
613 pos = 0;
614 if (!m_visibleItems.isEmpty()) {
615 modelIndex = m_firstVisibleIndex - 1;
616 item = m_visibleItems.first();
617 pos = item->y() + m_clipItem->y();
618 }
619 while (modelIndex >= 0 && pos > fillFrom) {
620// qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
621 if (!(item = createItem(modelIndex, asynchronous)))
622 break;
623 pos -= item->height();
624 --modelIndex;
625 changed = true;
626 }
628 return changed;
631void ListViewWithPageHeader::reallyReleaseItem(ListItem *listItem)
633 QQuickItem *item = listItem->m_item;
634#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
635 QQuickVisualModel::ReleaseFlags flags = m_delegateModel->release(item);
636 if (flags & QQuickVisualModel::Destroyed) {
637#else
638 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
639 if (flags & QQmlDelegateModel::Destroyed) {
640#endif
641 item->setParentItem(nullptr);
642 }
643 delete listItem->m_sectionItem;
644 delete listItem;
647void ListViewWithPageHeader::releaseItem(ListItem *listItem)
649 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(listItem->m_item);
650 itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
651 m_itemsToRelease << listItem;
654void ListViewWithPageHeader::updateWatchedRoles()
656 if (m_delegateModel) {
657 QList<QByteArray> roles;
658 if (!m_sectionProperty.isEmpty())
659 roles << m_sectionProperty.toUtf8();
660 m_delegateModel->setWatchedRoles(roles);
661 }
664QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
666 if (!m_sectionDelegate)
667 return nullptr;
669 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
670 if (modelIndex > 0) {
671 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
672 if (section == prevSection)
673 return nullptr;
674 }
675 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
676 // Already inserted items can't steal next section header
677 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
678 if (section == nextSection) {
679 // Steal the section header
680 ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
681 if (nextItem) {
682 QQuickItem *sectionItem = nextItem->m_sectionItem;
683 nextItem->m_sectionItem = nullptr;
684 return sectionItem;
685 }
686 }
687 }
689 return getSectionItem(section);
692QQuickItem *ListViewWithPageHeader::getSectionItem(const QString &sectionText)
694 QQuickItem *sectionItem = nullptr;
696 QQmlContext *creationContext = m_sectionDelegate->creationContext();
697 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
698 context->setContextProperty(QLatin1String("section"), sectionText);
699 context->setContextProperty(QLatin1String("delegateIndex"), -1);
700 QObject *nobj = m_sectionDelegate->beginCreate(context);
701 if (nobj) {
702 QQml_setParent_noEvent(context, nobj);
703 sectionItem = qobject_cast<QQuickItem *>(nobj);
704 if (!sectionItem) {
705 delete nobj;
706 } else {
707 sectionItem->setZ(2);
708 QQml_setParent_noEvent(sectionItem, m_clipItem);
709 sectionItem->setParentItem(m_clipItem);
710 }
711 } else {
712 delete context;
713 }
714 m_sectionDelegate->completeCreate();
716 // TODO attach to sectionItem so we can accomodate to it changing its height
718 return sectionItem;
721bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
723// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
724 // Do not remove items if we are overshooting up or down, since we'll come back
725 // to the "stable" position and delete/create items without any reason
726 if (contentY() < -m_minYExtent) {
727 return false;
728 } else if (contentY() + height() > contentHeight()) {
729 return false;
730 }
731 bool changed = false;
733 bool foundVisible = false;
734 int i = 0;
735 int removedItems = 0;
736 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
737 while (i < m_visibleItems.count()) {
738 ListItem *item = m_visibleItems[i];
739 const qreal pos = item->y() + m_clipItem->y();
740// qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
741 if (pos + item->height() < bufferFrom || pos > bufferTo) {
742// qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
743 releaseItem(item);
744 m_visibleItems.removeAt(i);
745 changed = true;
746 ++removedItems;
747 } else {
748 if (!foundVisible) {
749 foundVisible = true;
750 const int itemIndex = m_firstVisibleIndex + removedItems + i;
751 m_firstVisibleIndex = itemIndex;
752 }
753 ++i;
754 }
755 }
756 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
757 adjustMinYExtent();
758 }
760 return changed;
763ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
765// qDebug() << "CREATE ITEM" << modelIndex;
766 if (asynchronous && m_asyncRequestedIndex != -1)
767 return nullptr;
769 m_asyncRequestedIndex = -1;
770#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
771 QQuickItem *item = m_delegateModel->item(modelIndex, asynchronous);
772#else
773 QObject* object = m_delegateModel->object(modelIndex, asynchronous);
774 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
775#endif
776 if (!item) {
777#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
778 m_asyncRequestedIndex = modelIndex;
779#else
780 if (object) {
781 m_delegateModel->release(object);
782 if (!m_delegateValidated) {
783 m_delegateValidated = true;
784 QObject* delegateObj = delegate();
785 qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
786 }
787 } else {
788 m_asyncRequestedIndex = modelIndex;
789 }
790#endif
791 return 0;
792 } else {
793// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
794 ListItem *listItem = new ListItem;
795 listItem->m_item = item;
796 listItem->m_sectionItem = getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/);
797 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
798 ListItem *prevItem = itemAtIndex(modelIndex - 1);
799 bool lostItem = false; // Is an item that we requested async but because of model changes
800 // it is no longer attached to any of the existing items (has no prev nor next item)
801 // nor is the first item
802 if (prevItem) {
803 listItem->setY(prevItem->y() + prevItem->height());
804 } else {
805 ListItem *currItem = itemAtIndex(modelIndex);
806 if (currItem) {
807 // There's something already in m_visibleItems at out index, meaning this is an insert, so attach to its top
808 listItem->setY(currItem->y() - listItem->height());
809 } else {
810 ListItem *nextItem = itemAtIndex(modelIndex + 1);
811 if (nextItem) {
812 listItem->setY(nextItem->y() - listItem->height());
813 } else if (modelIndex == 0 && m_headerItem) {
814 listItem->setY(m_headerItem->height());
815 } else if (!m_visibleItems.isEmpty()) {
816 lostItem = true;
817 }
818 }
819 }
820 if (lostItem) {
821 releaseItem(listItem);
822 listItem = nullptr;
823 } else {
824 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() <= contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
825 if (m_visibleItems.isEmpty()) {
826 m_visibleItems << listItem;
827 } else {
828 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
829 }
830 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
831 m_firstVisibleIndex = modelIndex;
832 polish();
833 }
834 if (listItem->m_sectionItem) {
835 QQmlContext *context = QQmlEngine::contextForObject(listItem->m_sectionItem)->parentContext();
836 context->setContextProperty(QLatin1String("delegateIndex"), modelIndex);
837 }
838 adjustMinYExtent();
839 m_contentHeightDirty = true;
840 }
841 return listItem;
842 }
845#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
846void ListViewWithPageHeader::itemCreated(int modelIndex, QQuickItem *item)
848#else
849void ListViewWithPageHeader::itemCreated(int modelIndex, QObject *object)
851 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
852 if (!item) {
853 qWarning() << "ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
854 return;
855 }
856#endif
857// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
859 item->setParentItem(m_clipItem);
860 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
861 context->setContextProperty(QLatin1String("ListViewWithPageHeader"), this);
862 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
863 if (modelIndex == m_asyncRequestedIndex) {
864 createItem(modelIndex, false);
865 refill();
866 }
869void ListViewWithPageHeader::updateClipItem()
871 m_clipItem->setHeight(height() - m_headerItemShownHeight);
872 m_clipItem->setY(contentY() + m_headerItemShownHeight);
873 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
876void ListViewWithPageHeader::onContentHeightChanged()
878 updateClipItem();
881void ListViewWithPageHeader::onContentWidthChanged()
883 m_clipItem->setWidth(contentItem()->width());
886void ListViewWithPageHeader::onHeightChanged()
888 polish();
892#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
893void ListViewWithPageHeader::onModelUpdated(const QQuickChangeSet &changeSet, bool /*reset*/)
894#else
895void ListViewWithPageHeader::onModelUpdated(const QQmlChangeSet &changeSet, bool /*reset*/)
896#endif
898 // TODO Do something with reset
899// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
900 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
902#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
903 Q_FOREACH(const QQuickChangeSet::Remove &remove, changeSet.removes()) {
904#else
905 Q_FOREACH(const QQmlChangeSet::Remove &remove, changeSet.removes()) {
906#endif
907// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove.index << remove.count;
908 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
909 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
910 // If all the items we are removing are either not created or culled
911 // we have to grow down to avoid viewport changing
912 bool growDown = true;
913 for (int i = 0; growDown && i < remove.count; ++i) {
914 const int modelIndex = remove.index + i;
915 ListItem *item = itemAtIndex(modelIndex);
916 if (item && !item->culled()) {
917 growDown = false;
918 }
919 }
920 for (int i = remove.count - 1; i >= 0; --i) {
921 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
922 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
923 ListItem *item = m_visibleItems[visibleIndex];
924 // Pass the section item down if needed
925 if (item->m_sectionItem && visibleIndex + 1 < m_visibleItems.count()) {
926 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
927 if (!nextItem->m_sectionItem) {
928 nextItem->m_sectionItem = item->m_sectionItem;
929 item->m_sectionItem = nullptr;
930 }
931 }
932 releaseItem(item);
933 m_visibleItems.removeAt(visibleIndex);
934 }
935 }
936 if (growDown) {
937 adjustMinYExtent();
938 } else if (remove.index <= m_firstVisibleIndex) {
939 if (!m_visibleItems.isEmpty()) {
940 // We removed the first item that is the one that positions the rest
941 // position the new first item correctly
942 m_visibleItems.first()->setY(oldFirstValidIndexPos);
943 } else {
944 m_firstVisibleIndex = -1;
945 }
946 }
947 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
948 m_firstVisibleIndex -= remove.count;
949 }
950 for (int i = remove.count - 1; i >= 0; --i) {
951 const int modelIndex = remove.index + i;
952 if (modelIndex == m_asyncRequestedIndex) {
953 m_asyncRequestedIndex = -1;
954 } else if (modelIndex < m_asyncRequestedIndex) {
955 m_asyncRequestedIndex--;
956 }
957 }
958 }
960#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
961 Q_FOREACH(const QQuickChangeSet::Insert &insert, changeSet.inserts()) {
962#else
963 Q_FOREACH(const QQmlChangeSet::Insert &insert, changeSet.inserts()) {
964#endif
965// qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
966 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
967 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
968 if (insertingInValidIndexes || firstItemWithViewOnTop)
969 {
970 // If the items we are adding won't be really visible
971 // we grow up instead of down to not change the viewport
972 bool growUp = false;
973 if (!firstItemWithViewOnTop) {
974 for (int i = 0; i < m_visibleItems.count(); ++i) {
975 if (!m_visibleItems[i]->culled()) {
976 if (insert.index <= m_firstVisibleIndex + i) {
977 growUp = true;
978 }
979 break;
980 }
981 }
982 }
984 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
985 for (int i = insert.count - 1; i >= 0; --i) {
986 const int modelIndex = insert.index + i;
987 ListItem *item = createItem(modelIndex, false);
988 if (growUp) {
989 ListItem *firstItem = m_visibleItems.first();
990 firstItem->setY(firstItem->y() - item->height());
991 }
992 // Adding an item may break a "same section" chain, so check
993 // if we need adding a new section item
994 if (m_sectionDelegate) {
995 ListItem *nextItem = itemAtIndex(modelIndex + 1);
996 if (nextItem && !nextItem->m_sectionItem) {
997 nextItem->m_sectionItem = getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/);
998 if (growUp && nextItem->m_sectionItem) {
999 ListItem *firstItem = m_visibleItems.first();
1000 firstItem->setY(firstItem->y() - nextItem->m_sectionItem->height());
1005 if (firstItemWithViewOnTop) {
1006 ListItem *firstItem = m_visibleItems.first();
1007 firstItem->setY(oldFirstValidIndexPos);
1009 adjustMinYExtent();
1010 } else if (insert.index <= m_firstVisibleIndex) {
1011 m_firstVisibleIndex += insert.count;
1014 for (int i = insert.count - 1; i >= 0; --i) {
1015 const int modelIndex = insert.index + i;
1016 if (modelIndex <= m_asyncRequestedIndex) {
1017 m_asyncRequestedIndex++;
1022#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
1023 Q_FOREACH(const QQuickChangeSet::Change &change, changeSet.changes()) {
1024#else
1025 Q_FOREACH(const QQmlChangeSet::Change &change, changeSet.changes()) {
1026#endif
1027// qDebug() << "ListViewWithPageHeader::onModelUpdated Change" << change.index << change.count;
1028 for (int i = change.index; i < change.count; ++i) {
1029 ListItem *item = itemAtIndex(i);
1030 if (item && item->m_sectionItem) {
1031 QQmlContext *context = QQmlEngine::contextForObject(item->m_sectionItem)->parentContext();
1032 const QString sectionText = m_delegateModel->stringValue(i, m_sectionProperty);
1033 context->setContextProperty(QLatin1String("section"), sectionText);
1038 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
1039 adjustMinYExtent();
1042 for (int i = 0; i < m_visibleItems.count(); ++i) {
1043 ListItem *item = m_visibleItems[i];
1044 if (item->m_sectionItem) {
1045 QQmlContext *context = QQmlEngine::contextForObject(item->m_sectionItem)->parentContext();
1046 context->setContextProperty(QLatin1String("delegateIndex"), m_firstVisibleIndex + i);
1050 layout();
1051 polish();
1052 m_contentHeightDirty = true;
1055void ListViewWithPageHeader::onShowHeaderAnimationFinished()
1057 m_contentHeightDirty = true;
1058 polish();
1061void ListViewWithPageHeader::itemGeometryChanged(QQuickItem * /*item*/, const QRectF &newGeometry, const QRectF &oldGeometry)
1063 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
1064 if (heightDiff != 0) {
1065 if (oldGeometry.y() + oldGeometry.height() + m_clipItem->y() < contentY() && !m_visibleItems.isEmpty()) {
1066 ListItem *firstItem = m_visibleItems.first();
1067 firstItem->setY(firstItem->y() - heightDiff);
1068 adjustMinYExtent();
1069 layout();
1071 refill();
1072 adjustMinYExtent();
1073 polish();
1074 m_contentHeightDirty = true;
1078void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
1080 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
1081 if (m_headerItemShownHeight > 0) {
1082 // If the header is shown because of the clip
1083 // Change its size
1084 m_headerItemShownHeight += heightDiff;
1085 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
1086 updateClipItem();
1087 adjustMinYExtent();
1088 } else {
1089 if (oldHeaderY + oldHeaderHeight > contentY()) {
1090 // If the header is shown because its position
1091 // Change its size
1092 ListItem *firstItem = m_visibleItems.first();
1093 firstItem->setY(firstItem->y() + heightDiff);
1094 layout();
1095 } else {
1096 // If the header is not on screen, just change the start of the list
1097 // so the viewport is not changed
1098 adjustMinYExtent();
1104void ListViewWithPageHeader::adjustMinYExtent()
1106 if (m_visibleItems.isEmpty()) {
1107 m_minYExtent = 0;
1108 } else {
1109 qreal nonCreatedHeight = 0;
1110 if (m_firstVisibleIndex != 0) {
1111 // Calculate the average height of items to estimate the position of the list start
1112 const int visibleItems = m_visibleItems.count();
1113 qreal visibleItemsHeight = 0;
1114 Q_FOREACH(ListItem *item, m_visibleItems) {
1115 visibleItemsHeight += item->height();
1117 nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
1118// qDebug() << m_firstVisibleIndex << visibleItemsHeight << visibleItems << nonCreatedHeight;
1120 m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + (m_headerItem ? m_headerItem->implicitHeight() : 0);
1124ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
1126 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
1127 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
1128 return m_visibleItems[visibleIndexedModelIndex];
1130 return nullptr;
1133void ListViewWithPageHeader::layout()
1135 if (m_inLayout)
1136 return;
1138 m_inLayout = true;
1139 if (!m_visibleItems.isEmpty()) {
1140 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
1141 const qreal visibleTo = contentY() + height() - m_clipItem->y();
1143 qreal pos = m_visibleItems.first()->y();
1145// qDebug() << "ListViewWithPageHeader::layout Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
1146 int firstReallyVisibleItem = -1;
1147 int modelIndex = m_firstVisibleIndex;
1148 Q_FOREACH(ListItem *item, m_visibleItems) {
1149 const bool cull = pos + item->height() <= visibleFrom || pos >= visibleTo;
1150 item->setCulled(cull);
1151 item->setY(pos);
1152 if (!cull && firstReallyVisibleItem == -1) {
1153 firstReallyVisibleItem = modelIndex;
1154 if (m_topSectionItem) {
1155 // Positing the top section sticky item is a two step process
1156 // First we set it either we cull it (because it doesn't need to be sticked to the top)
1157 // or stick it to the top
1158 // Then after the loop we'll make sure that if there's another section just below it
1159 // pushed the sticky section up to make it disappear
1160 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
1161 bool showStickySectionItem;
1162 // We need to show the "top section sticky item" when the position at the "top" of the
1163 // viewport is bigger than the start of the position of the first visible item
1164 // i.e. the first visible item starts before the viewport, or when the first
1165 // visible item starts just at the viewport start and it does not have its own section item
1166 if (topSectionStickPos > pos) {
1167 showStickySectionItem = true;
1168 } else if (topSectionStickPos == pos) {
1169 showStickySectionItem = !item->m_sectionItem;
1170 } else {
1171 showStickySectionItem = false;
1173 if (!showStickySectionItem) {
1174 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
1175 if (item->m_sectionItem) {
1176 // This seems it should happen since why would we cull the top section
1177 // if the first visible item has no section header? This only happens briefly
1178 // when increasing the height of a list that is at the bottom, the m_topSectionItem
1179 // gets shown shortly in the next polish call
1180 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(false);
1182 } else {
1183 // Update the top sticky section header
1184 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1185 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
1186 context->setContextProperty(QLatin1String("section"), section);
1188 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
1189 m_topSectionItem->setY(topSectionStickPos);
1190 int delegateIndex = modelIndex;
1191 // Look for the first index with this section text
1192 while (delegateIndex > 0) {
1193 const QString prevSection = m_delegateModel->stringValue(delegateIndex - 1, m_sectionProperty);
1194 if (prevSection != section)
1195 break;
1196 delegateIndex--;
1198 context->setContextProperty(QLatin1String("delegateIndex"), delegateIndex);
1199 if (item->m_sectionItem) {
1200 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(true);
1205 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
1206 const qreal clipFrom = visibleFrom + (!item->m_sectionItem && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
1207 if (!cull && pos < clipFrom) {
1208 context->setContextProperty(QLatin1String("heightToClip"), clipFrom - pos);
1209 } else {
1210 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
1212// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
1213 pos += item->height();
1214 ++modelIndex;
1217 // Second step of section sticky item positioning
1218 // Look at the next section header, check if it's pushing up the sticky one
1219 if (m_topSectionItem) {
1220 if (firstReallyVisibleItem >= 0) {
1221 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
1222 ListItem *item = m_visibleItems[i];
1223 if (item->m_sectionItem) {
1224 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
1225 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
1227 break;
1233 m_inLayout = false;
1236void ListViewWithPageHeader::updatePolish()
1238 Q_FOREACH(ListItem *item, m_itemsToRelease)
1239 reallyReleaseItem(item);
1240 m_itemsToRelease.clear();
1242 if (!model())
1243 return;
1245 layout();
1247 refill();
1249 if (m_contentHeightDirty) {
1250 qreal contentHeight;
1251 if (m_visibleItems.isEmpty()) {
1252 contentHeight = m_headerItem ? m_headerItem->height() : 0;
1253 } else {
1254 const int modelCount = model()->rowCount();
1255 const int visibleItems = m_visibleItems.count();
1256 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
1257 qreal nonCreatedHeight = 0;
1258 if (lastValidIndex != modelCount - 1) {
1259 const int visibleItems = m_visibleItems.count();
1260 qreal visibleItemsHeight = 0;
1261 Q_FOREACH(ListItem *item, m_visibleItems) {
1262 visibleItemsHeight += item->height();
1264 const int unknownSizes = modelCount - (m_firstVisibleIndex + visibleItems);
1265 nonCreatedHeight = unknownSizes * visibleItemsHeight / visibleItems;
1267 ListItem *item = m_visibleItems.last();
1268 contentHeight = nonCreatedHeight + item->y() + item->height() + m_clipItem->y();
1269 if (m_firstVisibleIndex != 0) {
1270 // Make sure that if we are shrinking we tell the view we still fit
1271 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
1275 m_contentHeightDirty = false;
1276 adjustMinYExtent();
1277 m_inContentHeightKeepHeaderShown = m_headerItem && m_headerItem->y() == contentY();
1278 setContentHeight(contentHeight);
1279 m_inContentHeightKeepHeaderShown = false;
1283#include "moc_listviewwithpageheader.cpp"