on 60b2e49 help sign in
unity8/plugins/ListViewWithPageHeader/listviewwithpageheader.cpp
1281 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)
159 m_clipItem = new QQuickItem(contentItem());
160// m_clipItem = new QQuickRectangle(contentItem());
161// ((QQuickRectangle*)m_clipItem)->setColor(Qt::gray);
163 m_contentYAnimation = new QQuickNumberAnimation(this);
164 m_contentYAnimation->setEasing(QEasingCurve::OutQuad);
165 m_contentYAnimation->setProperty("contentY");
166 m_contentYAnimation->setDuration(200);
167 m_contentYAnimation->setTargetObject(this);
169 connect(this, SIGNAL(contentWidthChanged()), this, SLOT(onContentWidthChanged()));
170 connect(this, SIGNAL(contentHeightChanged()), this, SLOT(onContentHeightChanged()));
171 connect(this, SIGNAL(heightChanged()), this, SLOT(onHeightChanged()));
172 connect(m_contentYAnimation, SIGNAL(stopped()), this, SLOT(onShowHeaderAnimationFinished()));
174 setFlickableDirection(VerticalFlick);
177ListViewWithPageHeader::~ListViewWithPageHeader()
181QAbstractItemModel *ListViewWithPageHeader::model() const
183 return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
186void ListViewWithPageHeader::setModel(QAbstractItemModel *model)
188 if (model != this->model()) {
189 if (!m_delegateModel) {
190 createDelegateModel();
191 } else {
192#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
193 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
194 }
195 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
196 connect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
197#else
198 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
199 }
200 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
201 connect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
202#endif
203 Q_EMIT modelChanged();
204 polish();
205 // TODO?
206// Q_EMIT contentHeightChanged();
207// Q_EMIT contentYChanged();
208 }
211QQmlComponent *ListViewWithPageHeader::delegate() const
213 return m_delegateModel ? m_delegateModel->delegate() : nullptr;
216void ListViewWithPageHeader::setDelegate(QQmlComponent *delegate)
218 if (delegate != this->delegate()) {
219 if (!m_delegateModel) {
220 createDelegateModel();
221 }
223 // Cleanup the existing items
224 Q_FOREACH(ListItem *item, m_visibleItems)
225 releaseItem(item);
226 m_visibleItems.clear();
227 m_firstVisibleIndex = -1;
228 adjustMinYExtent();
229 setContentY(0);
230 m_clipItem->setY(0);
231 if (m_topSectionItem) {
232 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
233 }
235 m_delegateModel->setDelegate(delegate);
237 Q_EMIT delegateChanged();
238 m_delegateValidated = false;
239 m_contentHeightDirty = true;
240 polish();
241 }
244QQuickItem *ListViewWithPageHeader::header() const
246 return m_headerItem;
249void ListViewWithPageHeader::setHeader(QQuickItem *headerItem)
251 if (m_headerItem != headerItem) {
252 qreal oldHeaderHeight = 0;
253 qreal oldHeaderY = 0;
254 if (m_headerItem) {
255 oldHeaderHeight = m_headerItem->height();
256 oldHeaderY = m_headerItem->y();
257 m_headerItem->setParentItem(nullptr);
258 }
259 m_headerItem = headerItem;
260 if (m_headerItem) {
261 m_headerItem->setParentItem(contentItem());
262 m_headerItem->setZ(1);
263 }
264 qreal newHeaderHeight = m_headerItem ? m_headerItem->height() : 0;
265 if (!m_visibleItems.isEmpty() && newHeaderHeight != oldHeaderHeight) {
266 headerHeightChanged(newHeaderHeight, oldHeaderHeight, oldHeaderY);
267 polish();
268 m_contentHeightDirty = true;
269 }
270 Q_EMIT headerChanged();
271 }
274QQmlComponent *ListViewWithPageHeader::sectionDelegate() const
276 return m_sectionDelegate;
279void ListViewWithPageHeader::setSectionDelegate(QQmlComponent *delegate)
281 if (delegate != m_sectionDelegate) {
282 // TODO clean existing sections
284 m_sectionDelegate = delegate;
286 m_topSectionItem = getSectionItem(QString());
287 m_topSectionItem->setZ(3);
288 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
289 connect(m_topSectionItem, SIGNAL(heightChanged()), SIGNAL(stickyHeaderHeightChanged()));
291 // TODO create sections for existing items
293 Q_EMIT sectionDelegateChanged();
294 Q_EMIT stickyHeaderHeightChanged();
295 }
298QString ListViewWithPageHeader::sectionProperty() const
300 return m_sectionProperty;
303void ListViewWithPageHeader::setSectionProperty(const QString &property)
305 if (property != m_sectionProperty) {
306 m_sectionProperty = property;
308 updateWatchedRoles();
310 // TODO recreate sections
312 Q_EMIT sectionPropertyChanged();
313 }
316bool ListViewWithPageHeader::forceNoClip() const
318 return m_forceNoClip;
321void ListViewWithPageHeader::setForceNoClip(bool noClip)
323 if (noClip != m_forceNoClip) {
324 m_forceNoClip = noClip;
325 updateClipItem();
326 Q_EMIT forceNoClipChanged();
327 }
330int ListViewWithPageHeader::stickyHeaderHeight() const
332 return m_topSectionItem ? m_topSectionItem->height() : 0;
335void ListViewWithPageHeader::positionAtBeginning()
337 if (m_delegateModel->count() <= 0)
338 return;
340 qreal headerHeight = (m_headerItem ? m_headerItem->height() : 0);
341 if (m_firstVisibleIndex != 0) {
342 // TODO This could be optimized by trying to reuse the interesection
343 // of items that may end up intersecting between the existing
344 // m_visibleItems and the items we are creating in the next loop
345 Q_FOREACH(ListItem *item, m_visibleItems)
346 releaseItem(item);
347 m_visibleItems.clear();
348 m_firstVisibleIndex = -1;
350 // Create the item 0, it will be already correctly positioned at createItem()
351 m_clipItem->setY(0);
352 ListItem *item = createItem(0, false);
353 // Create the subsequent items
354 int modelIndex = 1;
355 qreal pos = item->y() + item->height();
356 const qreal buffer = height() * bufferRatio;
357 const qreal bufferTo = height() + buffer;
358 while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
359 if (!(item = createItem(modelIndex, false)))
360 break;
361 pos += item->height();
362 ++modelIndex;
363 }
365 m_previousContentY = m_visibleItems.first()->y() - headerHeight;
366 }
367 setContentY(m_visibleItems.first()->y() + m_clipItem->y() - headerHeight);
370void ListViewWithPageHeader::showHeader()
372 if (!m_headerItem)
373 return;
375 auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
376 if (to != contentY()) {
377 const bool headerShownByItsOwn = contentY() < m_headerItem->y() + m_headerItem->height();
378 if (headerShownByItsOwn && m_headerItemShownHeight == 0) {
379 // We are not clipping since we are just at the top of the viewport
380 // but because of the showHeader animation we will need to, so
381 // enable the clipping without logically moving the items
382 m_headerItemShownHeight = m_headerItem->y() + m_headerItem->height() - contentY();
383 if (!m_visibleItems.isEmpty()) {
384 updateClipItem();
385 ListItem *firstItem = m_visibleItems.first();
386 firstItem->setY(firstItem->y() - m_headerItemShownHeight);
387 layout();
388 }
389 }
390 m_contentYAnimation->setTo(to);
391 contentYAnimationType = ContentYAnimationShowHeader;
392 m_contentYAnimation->start();
393 }
396QQuickItem *ListViewWithPageHeader::item(int modelIndex) const
398 ListItem *item = itemAtIndex(modelIndex);
399 if (item)
400 return item->m_item;
401 else
402 return nullptr;
405bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex)
407 ListItem *listItem = itemAtIndex(modelIndex);
408 if (listItem) {
409 return maximizeVisibleArea(listItem, listItem->height());
410 }
412 return false;
415bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex, int itemHeight)
417 if (itemHeight < 0)
418 return false;
420 ListItem *listItem = itemAtIndex(modelIndex);
421 if (listItem) {
422 return maximizeVisibleArea(listItem, itemHeight + (listItem->m_sectionItem ? listItem->m_sectionItem->height() : 0));
423 }
425 return false;
428bool ListViewWithPageHeader::maximizeVisibleArea(ListItem *listItem, int listItemHeight)
430 if (listItem) {
431 layout();
432 const auto listItemY = m_clipItem->y() + listItem->y();
433 if (listItemY > contentY() && listItemY + listItemHeight > contentY() + height()) {
434 // we can scroll the list up to show more stuff
435 const auto to = qMin(listItemY, listItemY + listItemHeight - height());
436 m_contentYAnimation->setTo(to);
437 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
438 m_contentYAnimation->start();
439 } else if ((listItemY < contentY() && listItemY + listItemHeight < contentY() + height()) ||
440 (m_topSectionItem && !listItem->m_sectionItem && listItemY - m_topSectionItem->height() < contentY() && listItemY + listItemHeight < contentY() + height()))
441 {
442 // we can scroll the list down to show more stuff
443 auto realVisibleListItemY = listItemY;
444 if (m_topSectionItem) {
445 // If we are showing the top section sticky item and this item doesn't have a section
446 // item we have to make sure to scroll it a bit more so that it is not underlapping
447 // the top section sticky item
448 bool topSectionShown = !QQuickItemPrivate::get(m_topSectionItem)->culled;
449 if (topSectionShown && !listItem->m_sectionItem) {
450 realVisibleListItemY -= m_topSectionItem->height();
451 }
452 }
453 const auto to = qMax(realVisibleListItemY, listItemY + listItemHeight - height());
454 m_contentYAnimation->setTo(to);
455 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
456 m_contentYAnimation->start();
457 }
458 return true;
459 }
460 return false;
463qreal ListViewWithPageHeader::minYExtent() const
465// qDebug() << "ListViewWithPageHeader::minYExtent" << m_minYExtent;
466 return m_minYExtent;
469void ListViewWithPageHeader::componentComplete()
471 if (m_delegateModel)
472 m_delegateModel->componentComplete();
474 QQuickFlickable::componentComplete();
476 polish();
479void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
481 QQuickFlickable::viewportMoved(orient);
482// qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
483 qreal diff = m_previousContentY - contentY();
484 const bool showHeaderAnimationRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationShowHeader;
485 if (m_headerItem) {
486 auto oldHeaderItemShownHeight = m_headerItemShownHeight;
487 if (contentY() < -m_minYExtent) {
488 // Stick the header item to the top when dragging down
489 m_headerItem->setY(contentY());
490 m_headerItem->setHeight(m_headerItem->implicitHeight() + (-m_minYExtent - contentY()));
491 } else {
492 m_headerItem->setHeight(m_headerItem->implicitHeight());
493 // We are going down (but it's not because of the rebound at the end)
494 // (but the header was not shown by it's own position)
495 // or the header is partially shown and we are not doing a maximizeVisibleArea either
496 const bool scrolledUp = m_previousContentY > contentY();
497 const bool notRebounding = contentY() + height() < contentHeight();
498 const bool notShownByItsOwn = contentY() + diff >= m_headerItem->y() + m_headerItem->height();
499 const bool maximizeVisibleAreaRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationMaximizeVisibleArea;
501 if (!scrolledUp && contentY() == -m_minYExtent) {
502 m_headerItemShownHeight = 0;
503 m_headerItem->setY(contentY());
504 } else if ((scrolledUp && notRebounding && notShownByItsOwn && !maximizeVisibleAreaRunning) || (m_headerItemShownHeight > 0)) {
505 if (maximizeVisibleAreaRunning && diff > 0) // If we are maximizing and the header was shown, make sure we hide it
506 m_headerItemShownHeight -= diff;
507 else
508 m_headerItemShownHeight += diff;
509 if (contentY() == -m_minYExtent) {
510 m_headerItemShownHeight = 0;
511 } else {
512 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
513 }
514 if (m_headerItemShownHeight > 0) {
515 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
516 } else {
517 m_headerItem->setY(-m_minYExtent);
518 }
519 }
520 }
521 // We will be changing the clip item, need to accomadate for it
522 // otherwise we move the firstItem down/up twice (unless the
523 // show header animation is running, where we want to keep the viewport stable)
524 if (!showHeaderAnimationRunning) {
525 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
526 } else {
527 diff = -diff;
528 }
529 }
530 if (!m_visibleItems.isEmpty()) {
531 updateClipItem();
532 ListItem *firstItem = m_visibleItems.first();
533 firstItem->setY(firstItem->y() + diff);
534 if (showHeaderAnimationRunning) {
535 adjustMinYExtent();
536 }
537 }
539 m_previousContentY = contentY();
540 layout();
541 polish();
544void ListViewWithPageHeader::createDelegateModel()
546#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
547 m_delegateModel = new QQuickVisualDataModel(qmlContext(this), this);
548 connect(m_delegateModel, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(itemCreated(int,QQuickItem*)));
549#else
550 m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
551 connect(m_delegateModel, SIGNAL(createdItem(int,QObject*)), this, SLOT(itemCreated(int,QObject*)));
552#endif
553 if (isComponentComplete())
554 m_delegateModel->componentComplete();
555 updateWatchedRoles();
558void ListViewWithPageHeader::refill()
560 if (m_inLayout) {
561 return;
562 }
563 if (!isComponentComplete()) {
564 return;
565 }
567 const qreal buffer = height() * bufferRatio;
568 const qreal from = contentY();
569 const qreal to = from + height();
570 const qreal bufferFrom = from - buffer;
571 const qreal bufferTo = to + buffer;
573 bool added = addVisibleItems(from, to, false);
574 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
575 added |= addVisibleItems(bufferFrom, bufferTo, true);
577 if (added || removed) {
578 m_contentHeightDirty = true;
579 }
582bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
584 if (!delegate())
585 return false;
587 if (m_delegateModel->count() == 0)
588 return false;
590 ListItem *item;
591// qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
593 int modelIndex = 0;
594 qreal pos = 0;
595 if (!m_visibleItems.isEmpty()) {
596 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
597 item = m_visibleItems.last();
598 pos = item->y() + item->height() + m_clipItem->y();
599 }
600 bool changed = false;
601// qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
602 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
603// qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
604 if (!(item = createItem(modelIndex, asynchronous)))
605 break;
606 pos += item->height();
607 ++modelIndex;
608 changed = true;
609 }
611 modelIndex = 0;
612 pos = 0;
613 if (!m_visibleItems.isEmpty()) {
614 modelIndex = m_firstVisibleIndex - 1;
615 item = m_visibleItems.first();
616 pos = item->y() + m_clipItem->y();
617 }
618 while (modelIndex >= 0 && pos > fillFrom) {
619// qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
620 if (!(item = createItem(modelIndex, asynchronous)))
621 break;
622 pos -= item->height();
623 --modelIndex;
624 changed = true;
625 }
627 return changed;
630void ListViewWithPageHeader::reallyReleaseItem(ListItem *listItem)
632 QQuickItem *item = listItem->m_item;
633#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
634 QQuickVisualModel::ReleaseFlags flags = m_delegateModel->release(item);
635 if (flags & QQuickVisualModel::Destroyed) {
636#else
637 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
638 if (flags & QQmlDelegateModel::Destroyed) {
639#endif
640 item->setParentItem(nullptr);
641 }
642 delete listItem->m_sectionItem;
643 delete listItem;
646void ListViewWithPageHeader::releaseItem(ListItem *listItem)
648 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(listItem->m_item);
649 itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
650 m_itemsToRelease << listItem;
653void ListViewWithPageHeader::updateWatchedRoles()
655 if (m_delegateModel) {
656 QList<QByteArray> roles;
657 if (!m_sectionProperty.isEmpty())
658 roles << m_sectionProperty.toUtf8();
659 m_delegateModel->setWatchedRoles(roles);
660 }
663QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
665 if (!m_sectionDelegate)
666 return nullptr;
668 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
669 if (modelIndex > 0) {
670 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
671 if (section == prevSection)
672 return nullptr;
673 }
674 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
675 // Already inserted items can't steal next section header
676 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
677 if (section == nextSection) {
678 // Steal the section header
679 ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
680 if (nextItem) {
681 QQuickItem *sectionItem = nextItem->m_sectionItem;
682 nextItem->m_sectionItem = nullptr;
683 return sectionItem;
684 }
685 }
686 }
688 return getSectionItem(section);
691QQuickItem *ListViewWithPageHeader::getSectionItem(const QString &sectionText)
693 QQuickItem *sectionItem = nullptr;
695 QQmlContext *creationContext = m_sectionDelegate->creationContext();
696 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
697 context->setContextProperty(QLatin1String("section"), sectionText);
698 context->setContextProperty(QLatin1String("delegateIndex"), -1);
699 QObject *nobj = m_sectionDelegate->beginCreate(context);
700 if (nobj) {
701 QQml_setParent_noEvent(context, nobj);
702 sectionItem = qobject_cast<QQuickItem *>(nobj);
703 if (!sectionItem) {
704 delete nobj;
705 } else {
706 sectionItem->setZ(2);
707 QQml_setParent_noEvent(sectionItem, m_clipItem);
708 sectionItem->setParentItem(m_clipItem);
709 }
710 } else {
711 delete context;
712 }
713 m_sectionDelegate->completeCreate();
715 // TODO attach to sectionItem so we can accomodate to it changing its height
717 return sectionItem;
720bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
722// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
723 // Do not remove items if we are overshooting up or down, since we'll come back
724 // to the "stable" position and delete/create items without any reason
725 if (contentY() < -m_minYExtent) {
726 return false;
727 } else if (contentY() + height() > contentHeight()) {
728 return false;
729 }
730 bool changed = false;
732 bool foundVisible = false;
733 int i = 0;
734 int removedItems = 0;
735 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
736 while (i < m_visibleItems.count()) {
737 ListItem *item = m_visibleItems[i];
738 const qreal pos = item->y() + m_clipItem->y();
739// qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
740 if (pos + item->height() < bufferFrom || pos > bufferTo) {
741// qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
742 releaseItem(item);
743 m_visibleItems.removeAt(i);
744 changed = true;
745 ++removedItems;
746 } else {
747 if (!foundVisible) {
748 foundVisible = true;
749 const int itemIndex = m_firstVisibleIndex + removedItems + i;
750 m_firstVisibleIndex = itemIndex;
751 }
752 ++i;
753 }
754 }
755 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
756 adjustMinYExtent();
757 }
759 return changed;
762ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
764// qDebug() << "CREATE ITEM" << modelIndex;
765 if (asynchronous && m_asyncRequestedIndex != -1)
766 return nullptr;
768 m_asyncRequestedIndex = -1;
769#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
770 QQuickItem *item = m_delegateModel->item(modelIndex, asynchronous);
771#else
772 QObject* object = m_delegateModel->object(modelIndex, asynchronous);
773 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
774#endif
775 if (!item) {
776#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
777 m_asyncRequestedIndex = modelIndex;
778#else
779 if (object) {
780 m_delegateModel->release(object);
781 if (!m_delegateValidated) {
782 m_delegateValidated = true;
783 QObject* delegateObj = delegate();
784 qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
785 }
786 } else {
787 m_asyncRequestedIndex = modelIndex;
788 }
789#endif
790 return 0;
791 } else {
792// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
793 ListItem *listItem = new ListItem;
794 listItem->m_item = item;
795 listItem->m_sectionItem = getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/);
796 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
797 ListItem *prevItem = itemAtIndex(modelIndex - 1);
798 bool lostItem = false; // Is an item that we requested async but because of model changes
799 // it is no longer attached to any of the existing items (has no prev nor next item)
800 // nor is the first item
801 if (prevItem) {
802 listItem->setY(prevItem->y() + prevItem->height());
803 } else {
804 ListItem *currItem = itemAtIndex(modelIndex);
805 if (currItem) {
806 // There's something already in m_visibleItems at out index, meaning this is an insert, so attach to its top
807 listItem->setY(currItem->y() - listItem->height());
808 } else {
809 ListItem *nextItem = itemAtIndex(modelIndex + 1);
810 if (nextItem) {
811 listItem->setY(nextItem->y() - listItem->height());
812 } else if (modelIndex == 0 && m_headerItem) {
813 listItem->setY(m_headerItem->height());
814 } else if (!m_visibleItems.isEmpty()) {
815 lostItem = true;
816 }
817 }
818 }
819 if (lostItem) {
820 releaseItem(listItem);
821 listItem = nullptr;
822 } else {
823 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() <= contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
824 if (m_visibleItems.isEmpty()) {
825 m_visibleItems << listItem;
826 } else {
827 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
828 }
829 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
830 m_firstVisibleIndex = modelIndex;
831 polish();
832 }
833 if (listItem->m_sectionItem) {
834 QQmlContext *context = QQmlEngine::contextForObject(listItem->m_sectionItem)->parentContext();
835 context->setContextProperty(QLatin1String("delegateIndex"), modelIndex);
836 }
837 adjustMinYExtent();
838 m_contentHeightDirty = true;
839 }
840 return listItem;
841 }
844#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
845void ListViewWithPageHeader::itemCreated(int modelIndex, QQuickItem *item)
847#else
848void ListViewWithPageHeader::itemCreated(int modelIndex, QObject *object)
850 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
851 if (!item) {
852 qWarning() << "ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
853 return;
854 }
855#endif
856// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
858 item->setParentItem(m_clipItem);
859 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
860 context->setContextProperty(QLatin1String("ListViewWithPageHeader"), this);
861 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
862 if (modelIndex == m_asyncRequestedIndex) {
863 createItem(modelIndex, false);
864 refill();
865 }
868void ListViewWithPageHeader::updateClipItem()
870 m_clipItem->setHeight(height() - m_headerItemShownHeight);
871 m_clipItem->setY(contentY() + m_headerItemShownHeight);
872 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
875void ListViewWithPageHeader::onContentHeightChanged()
877 updateClipItem();
880void ListViewWithPageHeader::onContentWidthChanged()
882 m_clipItem->setWidth(contentItem()->width());
885void ListViewWithPageHeader::onHeightChanged()
887 polish();
891#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
892void ListViewWithPageHeader::onModelUpdated(const QQuickChangeSet &changeSet, bool /*reset*/)
893#else
894void ListViewWithPageHeader::onModelUpdated(const QQmlChangeSet &changeSet, bool /*reset*/)
895#endif
897 // TODO Do something with reset
898// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
899 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
901#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
902 Q_FOREACH(const QQuickChangeSet::Remove &remove, changeSet.removes()) {
903#else
904 Q_FOREACH(const QQmlChangeSet::Remove &remove, changeSet.removes()) {
905#endif
906// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove.index << remove.count;
907 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
908 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
909 // If all the items we are removing are either not created or culled
910 // we have to grow down to avoid viewport changing
911 bool growDown = true;
912 for (int i = 0; growDown && i < remove.count; ++i) {
913 const int modelIndex = remove.index + i;
914 ListItem *item = itemAtIndex(modelIndex);
915 if (item && !item->culled()) {
916 growDown = false;
917 }
918 }
919 for (int i = remove.count - 1; i >= 0; --i) {
920 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
921 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
922 ListItem *item = m_visibleItems[visibleIndex];
923 // Pass the section item down if needed
924 if (item->m_sectionItem && visibleIndex + 1 < m_visibleItems.count()) {
925 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
926 if (!nextItem->m_sectionItem) {
927 nextItem->m_sectionItem = item->m_sectionItem;
928 item->m_sectionItem = nullptr;
929 }
930 }
931 releaseItem(item);
932 m_visibleItems.removeAt(visibleIndex);
933 }
934 }
935 if (growDown) {
936 adjustMinYExtent();
937 } else if (remove.index <= m_firstVisibleIndex) {
938 if (!m_visibleItems.isEmpty()) {
939 // We removed the first item that is the one that positions the rest
940 // position the new first item correctly
941 m_visibleItems.first()->setY(oldFirstValidIndexPos);
942 } else {
943 m_firstVisibleIndex = -1;
944 }
945 }
946 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
947 m_firstVisibleIndex -= remove.count;
948 }
949 for (int i = remove.count - 1; i >= 0; --i) {
950 const int modelIndex = remove.index + i;
951 if (modelIndex == m_asyncRequestedIndex) {
952 m_asyncRequestedIndex = -1;
953 } else if (modelIndex < m_asyncRequestedIndex) {
954 m_asyncRequestedIndex--;
955 }
956 }
957 }
959#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
960 Q_FOREACH(const QQuickChangeSet::Insert &insert, changeSet.inserts()) {
961#else
962 Q_FOREACH(const QQmlChangeSet::Insert &insert, changeSet.inserts()) {
963#endif
964// qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
965 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
966 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
967 if (insertingInValidIndexes || firstItemWithViewOnTop)
968 {
969 // If the items we are adding won't be really visible
970 // we grow up instead of down to not change the viewport
971 bool growUp = false;
972 if (!firstItemWithViewOnTop) {
973 for (int i = 0; i < m_visibleItems.count(); ++i) {
974 if (!m_visibleItems[i]->culled()) {
975 if (insert.index <= m_firstVisibleIndex + i) {
976 growUp = true;
977 }
978 break;
979 }
980 }
981 }
983 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
984 for (int i = insert.count - 1; i >= 0; --i) {
985 const int modelIndex = insert.index + i;
986 ListItem *item = createItem(modelIndex, false);
987 if (growUp) {
988 ListItem *firstItem = m_visibleItems.first();
989 firstItem->setY(firstItem->y() - item->height());
990 adjustMinYExtent();
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());
1001 adjustMinYExtent();
1006 if (firstItemWithViewOnTop) {
1007 ListItem *firstItem = m_visibleItems.first();
1008 firstItem->setY(oldFirstValidIndexPos);
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 setContentHeight(contentHeight);
1281#include "moc_listviewwithpageheader.cpp"