on 90de529 help sign in
unity8/plugins/Dash/listviewwithpageheader.cpp
1290 lines 52 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 <QCoreApplication>
95#include <QDebug>
96#include <qqmlinfo.h>
97#include <qqmlengine.h>
98#pragma GCC diagnostic push
99#pragma GCC diagnostic ignored "-pedantic"
100#include <private/qqmldelegatemodel_p.h>
101#include <private/qqmlglobal_p.h>
102#include <private/qquickitem_p.h>
103#include <private/qquickanimation_p.h>
104#pragma GCC diagnostic pop
105// #include <private/qquickrectangle_p.h>
107static const qreal bufferRatio = 0.5;
109qreal ListViewWithPageHeader::ListItem::height() const
111 return m_item->height() + (m_sectionItem ? m_sectionItem->height() : 0);
114qreal ListViewWithPageHeader::ListItem::y() const
116 return m_item->y() - (m_sectionItem ? m_sectionItem->height() : 0);
119void ListViewWithPageHeader::ListItem::setY(qreal newY)
121 if (m_sectionItem) {
122 m_sectionItem->setY(newY);
123 m_item->setY(newY + m_sectionItem->height());
124 } else {
125 m_item->setY(newY);
126 }
129bool ListViewWithPageHeader::ListItem::culled() const
131 return QQuickItemPrivate::get(m_item)->culled;
134void ListViewWithPageHeader::ListItem::setCulled(bool culled)
136 QQuickItemPrivate::get(m_item)->setCulled(culled);
137 if (m_sectionItem)
138 QQuickItemPrivate::get(m_sectionItem)->setCulled(culled);
141void ListViewWithPageHeader::ListItem::setSectionItem(QQuickItem *sectionItem)
143 m_sectionItem = sectionItem;
144 QQmlContext *context = QQmlEngine::contextForObject(m_item)->parentContext();
145 context->setContextProperty(QLatin1String("hasSectionHeader"), QVariant::fromValue<bool>(sectionItem != nullptr));
148ListViewWithPageHeader::ListViewWithPageHeader()
149 : m_delegateModel(nullptr)
150 , m_asyncRequestedIndex(-1)
151 , m_delegateValidated(false)
152 , m_firstVisibleIndex(-1)
153 , m_minYExtent(0)
154 , m_contentHeightDirty(false)
155 , m_headerItem(nullptr)
156 , m_previousContentY(0)
157 , m_headerItemShownHeight(0)
158 , m_sectionDelegate(nullptr)
159 , m_topSectionItem(nullptr)
160 , m_forceNoClip(false)
161 , m_inLayout(false)
162 , m_inContentHeightKeepHeaderShown(false)
164 m_clipItem = new QQuickItem(contentItem());
165// m_clipItem = new QQuickRectangle(contentItem());
166// ((QQuickRectangle*)m_clipItem)->setColor(Qt::gray);
168 m_contentYAnimation = new QQuickNumberAnimation(this);
169 m_contentYAnimation->setEasing(QEasingCurve::OutQuad);
170 m_contentYAnimation->setProperty("contentY");
171 m_contentYAnimation->setDuration(200);
172 m_contentYAnimation->setTargetObject(this);
174 connect(this, SIGNAL(contentWidthChanged()), this, SLOT(onContentWidthChanged()));
175 connect(this, SIGNAL(contentHeightChanged()), this, SLOT(onContentHeightChanged()));
176 connect(this, SIGNAL(heightChanged()), this, SLOT(onHeightChanged()));
177 connect(m_contentYAnimation, SIGNAL(stopped()), this, SLOT(onShowHeaderAnimationFinished()));
179 setFlickableDirection(VerticalFlick);
182ListViewWithPageHeader::~ListViewWithPageHeader()
186QAbstractItemModel *ListViewWithPageHeader::model() const
188 return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
191void ListViewWithPageHeader::setModel(QAbstractItemModel *model)
193 if (model != this->model()) {
194 if (!m_delegateModel) {
195 createDelegateModel();
196 } else {
197 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
198 }
199 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
200 connect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
201 Q_EMIT modelChanged();
202 polish();
203 // TODO?
204// Q_EMIT contentHeightChanged();
205// Q_EMIT contentYChanged();
206 }
209QQmlComponent *ListViewWithPageHeader::delegate() const
211 return m_delegateModel ? m_delegateModel->delegate() : nullptr;
214void ListViewWithPageHeader::setDelegate(QQmlComponent *delegate)
216 if (delegate != this->delegate()) {
217 if (!m_delegateModel) {
218 createDelegateModel();
219 }
221 // Cleanup the existing items
222 Q_FOREACH(ListItem *item, m_visibleItems)
223 releaseItem(item);
224 m_visibleItems.clear();
225 m_firstVisibleIndex = -1;
226 adjustMinYExtent();
227 setContentY(0);
228 m_clipItem->setY(0);
229 if (m_topSectionItem) {
230 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
231 }
233 m_delegateModel->setDelegate(delegate);
235 Q_EMIT delegateChanged();
236 m_delegateValidated = false;
237 m_contentHeightDirty = true;
238 polish();
239 }
242QQuickItem *ListViewWithPageHeader::header() const
244 return m_headerItem;
247void ListViewWithPageHeader::setHeader(QQuickItem *headerItem)
249 if (m_headerItem != headerItem) {
250 qreal oldHeaderHeight = 0;
251 qreal oldHeaderY = 0;
252 if (m_headerItem) {
253 oldHeaderHeight = m_headerItem->height();
254 oldHeaderY = m_headerItem->y();
255 m_headerItem->setParentItem(nullptr);
256 }
257 m_headerItem = headerItem;
258 if (m_headerItem) {
259 m_headerItem->setParentItem(contentItem());
260 m_headerItem->setZ(1);
261 }
262 qreal newHeaderHeight = m_headerItem ? m_headerItem->height() : 0;
263 if (!m_visibleItems.isEmpty() && newHeaderHeight != oldHeaderHeight) {
264 headerHeightChanged(newHeaderHeight, oldHeaderHeight, oldHeaderY);
265 polish();
266 m_contentHeightDirty = true;
267 }
268 Q_EMIT headerChanged();
269 }
272QQmlComponent *ListViewWithPageHeader::sectionDelegate() const
274 return m_sectionDelegate;
277void ListViewWithPageHeader::setSectionDelegate(QQmlComponent *delegate)
279 if (delegate != m_sectionDelegate) {
280 // TODO clean existing sections
282 m_sectionDelegate = delegate;
284 m_topSectionItem = getSectionItem(QString());
285 m_topSectionItem->setZ(3);
286 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
287 connect(m_topSectionItem, SIGNAL(heightChanged()), SIGNAL(stickyHeaderHeightChanged()));
289 // TODO create sections for existing items
291 Q_EMIT sectionDelegateChanged();
292 Q_EMIT stickyHeaderHeightChanged();
293 }
296QString ListViewWithPageHeader::sectionProperty() const
298 return m_sectionProperty;
301void ListViewWithPageHeader::setSectionProperty(const QString &property)
303 if (property != m_sectionProperty) {
304 m_sectionProperty = property;
306 updateWatchedRoles();
308 // TODO recreate sections
310 Q_EMIT sectionPropertyChanged();
311 }
314bool ListViewWithPageHeader::forceNoClip() const
316 return m_forceNoClip;
319void ListViewWithPageHeader::setForceNoClip(bool noClip)
321 if (noClip != m_forceNoClip) {
322 m_forceNoClip = noClip;
323 updateClipItem();
324 Q_EMIT forceNoClipChanged();
325 }
328int ListViewWithPageHeader::stickyHeaderHeight() const
330 return m_topSectionItem ? m_topSectionItem->height() : 0;
333void ListViewWithPageHeader::positionAtBeginning()
335 if (m_delegateModel->count() <= 0)
336 return;
338 qreal headerHeight = (m_headerItem ? m_headerItem->height() : 0);
339 if (m_firstVisibleIndex != 0) {
340 // TODO This could be optimized by trying to reuse the interesection
341 // of items that may end up intersecting between the existing
342 // m_visibleItems and the items we are creating in the next loop
343 Q_FOREACH(ListItem *item, m_visibleItems)
344 releaseItem(item);
345 m_visibleItems.clear();
346 m_firstVisibleIndex = -1;
348 // Create the item 0, it will be already correctly positioned at createItem()
349 m_clipItem->setY(0);
350 ListItem *item = createItem(0, false);
351 // Create the subsequent items
352 int modelIndex = 1;
353 qreal pos = item->y() + item->height();
354 const qreal buffer = height() * bufferRatio;
355 const qreal bufferTo = height() + buffer;
356 while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
357 if (!(item = createItem(modelIndex, false)))
358 break;
359 pos += item->height();
360 ++modelIndex;
361 }
363 m_previousContentY = m_visibleItems.first()->y() - headerHeight;
364 }
365 setContentY(m_visibleItems.first()->y() + m_clipItem->y() - headerHeight);
366 if (m_headerItem) {
367 // TODO This should not be needed and the code that adjust the m_headerItem position
368 // in viewportMoved() should be enough but in some cases we have not found a way to reproduce
369 // yet the code of viewportMoved() fails so here we make sure that at least if we are calling
370 // positionAtBeginning the header item will be correctly positioned
371 m_headerItem->setY(-m_minYExtent);
372 }
375static inline bool uFuzzyCompare(qreal r1, qreal r2)
377 return qFuzzyCompare(r1, r2) || (qFuzzyIsNull(r1) && qFuzzyIsNull(r2));
380void ListViewWithPageHeader::showHeader()
382 if (!m_headerItem)
383 return;
385 const auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
386 if (!uFuzzyCompare(to, contentY())) {
387 const bool headerShownByItsOwn = contentY() < m_headerItem->y() + m_headerItem->height();
388 if (headerShownByItsOwn && m_headerItemShownHeight == 0) {
389 // We are not clipping since we are just at the top of the viewport
390 // but because of the showHeader animation we will need to, so
391 // enable the clipping without logically moving the items
392 m_headerItemShownHeight = m_headerItem->y() + m_headerItem->height() - contentY();
393 if (!m_visibleItems.isEmpty()) {
394 updateClipItem();
395 ListItem *firstItem = m_visibleItems.first();
396 firstItem->setY(firstItem->y() - m_headerItemShownHeight);
397 layout();
398 }
399 }
400 m_contentYAnimation->setTo(to);
401 contentYAnimationType = ContentYAnimationShowHeader;
402 m_contentYAnimation->start();
403 }
406QQuickItem *ListViewWithPageHeader::item(int modelIndex) const
408 ListItem *item = itemAtIndex(modelIndex);
409 if (item)
410 return item->m_item;
411 else
412 return nullptr;
415bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex)
417 ListItem *listItem = itemAtIndex(modelIndex);
418 if (listItem) {
419 return maximizeVisibleArea(listItem, listItem->height());
420 }
422 return false;
425bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex, int itemHeight)
427 if (itemHeight < 0)
428 return false;
430 ListItem *listItem = itemAtIndex(modelIndex);
431 if (listItem) {
432 return maximizeVisibleArea(listItem, itemHeight + (listItem->sectionItem() ? listItem->sectionItem()->height() : 0));
433 }
435 return false;
438bool ListViewWithPageHeader::maximizeVisibleArea(ListItem *listItem, int listItemHeight)
440 if (listItem) {
441 layout();
442 const auto listItemY = m_clipItem->y() + listItem->y();
443 if (listItemY > contentY() && listItemY + listItemHeight > contentY() + height()) {
444 // we can scroll the list up to show more stuff
445 const auto to = qMin(listItemY, listItemY + listItemHeight - height());
446 m_contentYAnimation->setTo(to);
447 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
448 m_contentYAnimation->start();
449 } else if ((listItemY < contentY() && listItemY + listItemHeight < contentY() + height()) ||
450 (m_topSectionItem && !listItem->sectionItem() && listItemY - m_topSectionItem->height() < contentY() && listItemY + listItemHeight < contentY() + height()))
451 {
452 // we can scroll the list down to show more stuff
453 auto realVisibleListItemY = listItemY;
454 if (m_topSectionItem) {
455 // If we are showing the top section sticky item and this item doesn't have a section
456 // item we have to make sure to scroll it a bit more so that it is not underlapping
457 // the top section sticky item
458 bool topSectionShown = !QQuickItemPrivate::get(m_topSectionItem)->culled;
459 if (topSectionShown && !listItem->sectionItem()) {
460 realVisibleListItemY -= m_topSectionItem->height();
461 }
462 }
463 const auto to = qMax(realVisibleListItemY, listItemY + listItemHeight - height());
464 m_contentYAnimation->setTo(to);
465 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
466 m_contentYAnimation->start();
467 }
468 return true;
469 }
470 return false;
473qreal ListViewWithPageHeader::minYExtent() const
475// qDebug() << "ListViewWithPageHeader::minYExtent" << m_minYExtent;
476 return m_minYExtent;
479void ListViewWithPageHeader::componentComplete()
481 if (m_delegateModel)
482 m_delegateModel->componentComplete();
484 QQuickFlickable::componentComplete();
486 polish();
489void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
491 // Check we are not being taken down and don't paint anything
492 // TODO Check if we still need this in 5.2
493 // For reproduction just inifnite loop testDash or testDashContent
494 if (!QQmlEngine::contextForObject(this)->parentContext())
495 return;
497 QQuickFlickable::viewportMoved(orient);
498// qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
499 qreal diff = m_previousContentY - contentY();
500 const bool showHeaderAnimationRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationShowHeader;
501 if (m_headerItem) {
502 const auto oldHeaderItemShownHeight = m_headerItemShownHeight;
503 if (uFuzzyCompare(contentY(), -m_minYExtent) || contentY() > -m_minYExtent) {
504 m_headerItem->setHeight(m_headerItem->implicitHeight());
505 // We are going down (but it's not because of the rebound at the end)
506 // (but the header was not shown by it's own position)
507 // or the header is partially shown and we are not doing a maximizeVisibleArea either
508 const bool scrolledUp = m_previousContentY > contentY();
509 const bool notRebounding = contentY() + height() < contentHeight();
510 const bool notShownByItsOwn = contentY() + diff >= m_headerItem->y() + m_headerItem->height();
511 const bool maximizeVisibleAreaRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationMaximizeVisibleArea;
513 if (!scrolledUp && (contentY() == -m_minYExtent || (m_headerItemShownHeight == 0 && m_previousContentY == m_headerItem->y()))) {
514 m_headerItemShownHeight = 0;
515 m_headerItem->setY(-m_minYExtent);
516 } else if ((scrolledUp && notRebounding && notShownByItsOwn && !maximizeVisibleAreaRunning) || (m_headerItemShownHeight > 0) || m_inContentHeightKeepHeaderShown) {
517 if (maximizeVisibleAreaRunning && diff > 0) {
518 // If we are maximizing and the header was shown, make sure we hide it
519 m_headerItemShownHeight -= diff;
520 } else {
521 m_headerItemShownHeight += diff;
522 }
523 if (uFuzzyCompare(contentY(), -m_minYExtent)) {
524 m_headerItemShownHeight = 0;
525 } else {
526 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
527 }
528 if (m_headerItemShownHeight > 0) {
529 if (uFuzzyCompare(m_headerItem->height(), m_headerItemShownHeight)) {
530 m_headerItem->setY(contentY());
531 m_headerItemShownHeight = m_headerItem->height();
532 } else {
533 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
534 }
535 } else {
536 m_headerItem->setY(-m_minYExtent);
537 }
538 }
539 } else {
540 // Stick the header item to the top when dragging down
541 m_headerItem->setY(contentY());
542 m_headerItem->setHeight(m_headerItem->implicitHeight() + (-m_minYExtent - contentY()));
543 }
544 // We will be changing the clip item, need to accomadate for it
545 // otherwise we move the firstItem down/up twice (unless the
546 // show header animation is running, where we want to keep the viewport stable)
547 if (!showHeaderAnimationRunning) {
548 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
549 } else {
550 diff = -diff;
551 }
552 }
553 if (!m_visibleItems.isEmpty()) {
554 updateClipItem();
555 ListItem *firstItem = m_visibleItems.first();
556 firstItem->setY(firstItem->y() + diff);
557 if (showHeaderAnimationRunning) {
558 adjustMinYExtent();
559 }
560 }
562 m_previousContentY = contentY();
563 layout();
564 polish();
567void ListViewWithPageHeader::createDelegateModel()
569 m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
570 connect(m_delegateModel, SIGNAL(createdItem(int,QObject*)), this, SLOT(itemCreated(int,QObject*)));
571 if (isComponentComplete())
572 m_delegateModel->componentComplete();
573 updateWatchedRoles();
576void ListViewWithPageHeader::refill()
578 if (m_inLayout) {
579 return;
580 }
581 if (!isComponentComplete()) {
582 return;
583 }
585 const qreal buffer = height() * bufferRatio;
586 const qreal from = contentY();
587 const qreal to = from + height();
588 const qreal bufferFrom = from - buffer;
589 const qreal bufferTo = to + buffer;
591 bool added = addVisibleItems(from, to, false);
592 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
593 added |= addVisibleItems(bufferFrom, bufferTo, true);
595 if (added || removed) {
596 m_contentHeightDirty = true;
597 }
600bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
602 if (!delegate())
603 return false;
605 if (m_delegateModel->count() == 0)
606 return false;
608 ListItem *item;
609// qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
611 int modelIndex = 0;
612 qreal pos = 0;
613 if (!m_visibleItems.isEmpty()) {
614 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
615 item = m_visibleItems.last();
616 pos = item->y() + item->height() + m_clipItem->y();
617 }
618 bool changed = false;
619// qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
620 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
621// qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
622 if (!(item = createItem(modelIndex, asynchronous)))
623 break;
624 pos += item->height();
625 ++modelIndex;
626 changed = true;
627 }
629 modelIndex = 0;
630 pos = 0;
631 if (!m_visibleItems.isEmpty()) {
632 modelIndex = m_firstVisibleIndex - 1;
633 item = m_visibleItems.first();
634 pos = item->y() + m_clipItem->y();
635 }
636 while (modelIndex >= 0 && pos > fillFrom) {
637// qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
638 if (!(item = createItem(modelIndex, asynchronous)))
639 break;
640 pos -= item->height();
641 --modelIndex;
642 changed = true;
643 }
645 return changed;
648void ListViewWithPageHeader::reallyReleaseItem(ListItem *listItem)
650 QQuickItem *item = listItem->m_item;
651 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
652 if (flags & QQmlDelegateModel::Destroyed) {
653 item->setParentItem(nullptr);
654 }
655 delete listItem->sectionItem();
656 delete listItem;
659void ListViewWithPageHeader::releaseItem(ListItem *listItem)
661 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(listItem->m_item);
662 itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
663 m_itemsToRelease << listItem;
666void ListViewWithPageHeader::updateWatchedRoles()
668 if (m_delegateModel) {
669 QList<QByteArray> roles;
670 if (!m_sectionProperty.isEmpty())
671 roles << m_sectionProperty.toUtf8();
672 m_delegateModel->setWatchedRoles(roles);
673 }
676QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
678 if (!m_sectionDelegate)
679 return nullptr;
681 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
682 if (section.isEmpty())
683 return nullptr;
685 if (modelIndex > 0) {
686 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
687 if (section == prevSection)
688 return nullptr;
689 }
690 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
691 // Already inserted items can't steal next section header
692 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
693 if (section == nextSection) {
694 // Steal the section header
695 ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
696 if (nextItem) {
697 QQuickItem *sectionItem = nextItem->sectionItem();
698 nextItem->setSectionItem(nullptr);
699 return sectionItem;
700 }
701 }
702 }
704 return getSectionItem(section);
707QQuickItem *ListViewWithPageHeader::getSectionItem(const QString &sectionText)
709 QQuickItem *sectionItem = nullptr;
711 QQmlContext *creationContext = m_sectionDelegate->creationContext();
712 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
713 context->setContextProperty(QLatin1String("section"), sectionText);
714 context->setContextProperty(QLatin1String("delegateIndex"), -1);
715 QObject *nobj = m_sectionDelegate->beginCreate(context);
716 if (nobj) {
717 QQml_setParent_noEvent(context, nobj);
718 sectionItem = qobject_cast<QQuickItem *>(nobj);
719 if (!sectionItem) {
720 delete nobj;
721 } else {
722 sectionItem->setZ(2);
723 QQml_setParent_noEvent(sectionItem, m_clipItem);
724 sectionItem->setParentItem(m_clipItem);
725 }
726 } else {
727 delete context;
728 }
729 m_sectionDelegate->completeCreate();
731 // TODO attach to sectionItem so we can accomodate to it changing its height
733 return sectionItem;
736bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
738// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
739 // Do not remove items if we are overshooting up or down, since we'll come back
740 // to the "stable" position and delete/create items without any reason
741 if (contentY() < -m_minYExtent) {
742 return false;
743 } else if (contentY() + height() > contentHeight()) {
744 return false;
745 }
746 bool changed = false;
748 bool foundVisible = false;
749 int i = 0;
750 int removedItems = 0;
751 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
752 while (i < m_visibleItems.count()) {
753 ListItem *item = m_visibleItems[i];
754 const qreal pos = item->y() + m_clipItem->y();
755// qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
756 if (pos + item->height() < bufferFrom || pos > bufferTo) {
757// qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
758 releaseItem(item);
759 m_visibleItems.removeAt(i);
760 changed = true;
761 ++removedItems;
762 } else {
763 if (!foundVisible) {
764 foundVisible = true;
765 const int itemIndex = m_firstVisibleIndex + removedItems + i;
766 m_firstVisibleIndex = itemIndex;
767 }
768 ++i;
769 }
770 }
771 if (!foundVisible) {
772 m_firstVisibleIndex = -1;
773 }
774 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
775 adjustMinYExtent();
776 }
778 return changed;
781ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
783// qDebug() << "CREATE ITEM" << modelIndex;
784 if (asynchronous && m_asyncRequestedIndex != -1)
785 return nullptr;
787 m_asyncRequestedIndex = -1;
788 QObject* object = m_delegateModel->object(modelIndex, asynchronous);
789 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
790 if (!item) {
791 if (object) {
792 m_delegateModel->release(object);
793 if (!m_delegateValidated) {
794 m_delegateValidated = true;
795 QObject* delegateObj = delegate();
796 qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
797 }
798 } else {
799 m_asyncRequestedIndex = modelIndex;
800 }
801 return 0;
802 } else {
803// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
804 ListItem *listItem = new ListItem;
805 listItem->m_item = item;
806 listItem->setSectionItem(getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/));
807 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
808 ListItem *prevItem = itemAtIndex(modelIndex - 1);
809 bool lostItem = false; // Is an item that we requested async but because of model changes
810 // it is no longer attached to any of the existing items (has no prev nor next item)
811 // nor is the first item
812 if (prevItem) {
813 listItem->setY(prevItem->y() + prevItem->height());
814 } else {
815 ListItem *currItem = itemAtIndex(modelIndex);
816 if (currItem) {
817 // There's something already in m_visibleItems at out index, meaning this is an insert, so attach to its top
818 listItem->setY(currItem->y() - listItem->height());
819 } else {
820 ListItem *nextItem = itemAtIndex(modelIndex + 1);
821 if (nextItem) {
822 listItem->setY(nextItem->y() - listItem->height());
823 } else if (modelIndex == 0 && m_headerItem) {
824 listItem->setY(m_headerItem->height());
825 } else if (!m_visibleItems.isEmpty()) {
826 lostItem = true;
827 }
828 }
829 }
830 if (lostItem) {
831 listItem->setCulled(true);
832 releaseItem(listItem);
833 listItem = nullptr;
834 } else {
835 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() <= contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
836 if (m_visibleItems.isEmpty()) {
837 m_visibleItems << listItem;
838 } else {
839 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
840 }
841 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
842 m_firstVisibleIndex = modelIndex;
843 polish();
844 }
845 if (listItem->sectionItem()) {
846 QQmlContext *context = QQmlEngine::contextForObject(listItem->sectionItem())->parentContext();
847 context->setContextProperty(QLatin1String("delegateIndex"), modelIndex);
848 }
849 adjustMinYExtent();
850 m_contentHeightDirty = true;
851 }
852 return listItem;
853 }
856void ListViewWithPageHeader::itemCreated(int modelIndex, QObject *object)
858 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
859 if (!item) {
860 qWarning() << "ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
861 return;
862 }
863// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
864 // Check we are not being taken down and don't paint anything
865 // TODO Check if we still need this in 5.2
866 // For reproduction just inifnite loop testDash or testDashContent
867 if (!QQmlEngine::contextForObject(this)->parentContext())
868 return;
870 item->setParentItem(m_clipItem);
871 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
872 context->setContextProperty(QLatin1String("ListViewWithPageHeader"), this);
873 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
874 context->setContextProperty(QLatin1String("hasSectionHeader"), QVariant::fromValue<bool>(false));
875 if (modelIndex == m_asyncRequestedIndex) {
876 createItem(modelIndex, false);
877 refill();
878 }
881void ListViewWithPageHeader::updateClipItem()
883 m_clipItem->setHeight(height() - m_headerItemShownHeight);
884 m_clipItem->setY(contentY() + m_headerItemShownHeight);
885 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
888void ListViewWithPageHeader::onContentHeightChanged()
890 updateClipItem();
893void ListViewWithPageHeader::onContentWidthChanged()
895 m_clipItem->setWidth(contentItem()->width());
898void ListViewWithPageHeader::onHeightChanged()
900 polish();
904void ListViewWithPageHeader::onModelUpdated(const QQmlChangeSet &changeSet, bool /*reset*/)
906 // TODO Do something with reset
907// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
908 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
910 Q_FOREACH(const QQmlChangeSet::Remove &remove, changeSet.removes()) {
911// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove.index << remove.count;
912 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
913 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
914 // If all the items we are removing are either not created or culled
915 // we have to grow down to avoid viewport changing
916 bool growDown = true;
917 for (int i = 0; growDown && i < remove.count; ++i) {
918 const int modelIndex = remove.index + i;
919 ListItem *item = itemAtIndex(modelIndex);
920 if (item && !item->culled()) {
921 growDown = false;
922 }
923 }
924 for (int i = remove.count - 1; i >= 0; --i) {
925 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
926 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
927 ListItem *item = m_visibleItems[visibleIndex];
928 // Pass the section item down if needed
929 if (item->sectionItem() && visibleIndex + 1 < m_visibleItems.count()) {
930 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
931 if (!nextItem->sectionItem()) {
932 nextItem->setSectionItem(item->sectionItem());
933 item->setSectionItem(nullptr);
934 }
935 }
936 releaseItem(item);
937 m_visibleItems.removeAt(visibleIndex);
938 }
939 }
940 if (growDown) {
941 adjustMinYExtent();
942 } else if (remove.index <= m_firstVisibleIndex) {
943 if (!m_visibleItems.isEmpty()) {
944 // We removed the first item that is the one that positions the rest
945 // position the new first item correctly
946 m_visibleItems.first()->setY(oldFirstValidIndexPos);
947 } else {
948 m_firstVisibleIndex = -1;
949 }
950 }
951 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
952 m_firstVisibleIndex -= remove.count;
953 }
954 for (int i = remove.count - 1; i >= 0; --i) {
955 const int modelIndex = remove.index + i;
956 if (modelIndex == m_asyncRequestedIndex) {
957 m_asyncRequestedIndex = -1;
958 } else if (modelIndex < m_asyncRequestedIndex) {
959 m_asyncRequestedIndex--;
960 }
961 }
962 }
964 Q_FOREACH(const QQmlChangeSet::Insert &insert, changeSet.inserts()) {
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->sectionItem()) {
997 nextItem->setSectionItem(getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/));
998 if (growUp && nextItem->sectionItem()) {
999 ListItem *firstItem = m_visibleItems.first();
1000 firstItem->setY(firstItem->y() - nextItem->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 Q_FOREACH(const QQmlChangeSet::Change &change, changeSet.changes()) {
1023// qDebug() << "ListViewWithPageHeader::onModelUpdated Change" << change.index << change.count;
1024 for (int i = change.index; i < change.count; ++i) {
1025 ListItem *item = itemAtIndex(i);
1026 if (item && item->sectionItem()) {
1027 QQmlContext *context = QQmlEngine::contextForObject(item->sectionItem())->parentContext();
1028 const QString sectionText = m_delegateModel->stringValue(i, m_sectionProperty);
1029 context->setContextProperty(QLatin1String("section"), sectionText);
1034 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
1035 adjustMinYExtent();
1038 for (int i = 0; i < m_visibleItems.count(); ++i) {
1039 ListItem *item = m_visibleItems[i];
1040 if (item->sectionItem()) {
1041 QQmlContext *context = QQmlEngine::contextForObject(item->sectionItem())->parentContext();
1042 context->setContextProperty(QLatin1String("delegateIndex"), m_firstVisibleIndex + i);
1046 layout();
1047 polish();
1048 m_contentHeightDirty = true;
1051void ListViewWithPageHeader::onShowHeaderAnimationFinished()
1053 m_contentHeightDirty = true;
1054 polish();
1057void ListViewWithPageHeader::itemGeometryChanged(QQuickItem * /*item*/, const QRectF &newGeometry, const QRectF &oldGeometry)
1059 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
1060 if (heightDiff != 0) {
1061 if (!m_inContentHeightKeepHeaderShown && oldGeometry.y() + oldGeometry.height() + m_clipItem->y() <= contentY() && !m_visibleItems.isEmpty()) {
1062 ListItem *firstItem = m_visibleItems.first();
1063 firstItem->setY(firstItem->y() - heightDiff);
1064 adjustMinYExtent();
1065 layout();
1067 refill();
1068 adjustMinYExtent();
1069 polish();
1070 m_contentHeightDirty = true;
1074void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
1076 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
1077 if (m_headerItemShownHeight > 0) {
1078 // If the header is shown because of the clip
1079 // Change its size
1080 m_headerItemShownHeight += heightDiff;
1081 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
1082 updateClipItem();
1083 adjustMinYExtent();
1084 } else {
1085 if (oldHeaderY + oldHeaderHeight > contentY()) {
1086 // If the header is shown because its position
1087 // Change its size
1088 ListItem *firstItem = m_visibleItems.first();
1089 firstItem->setY(firstItem->y() + heightDiff);
1090 layout();
1091 } else {
1092 // If the header is not on screen, just change the start of the list
1093 // so the viewport is not changed
1094 adjustMinYExtent();
1100void ListViewWithPageHeader::adjustMinYExtent()
1102 if (m_visibleItems.isEmpty()) {
1103 m_minYExtent = 0;
1104 } else {
1105 qreal nonCreatedHeight = 0;
1106 if (m_firstVisibleIndex != 0) {
1107 // Calculate the average height of items to estimate the position of the list start
1108 const int visibleItems = m_visibleItems.count();
1109 qreal visibleItemsHeight = 0;
1110 Q_FOREACH(ListItem *item, m_visibleItems) {
1111 visibleItemsHeight += item->height();
1113 nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
1114// qDebug() << m_firstVisibleIndex << visibleItemsHeight << visibleItems << nonCreatedHeight;
1116 const qreal headerHeight = (m_headerItem ? m_headerItem->implicitHeight() : 0);
1117 m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + headerHeight;
1118 if (m_minYExtent != 0 && qFuzzyIsNull(m_minYExtent)) {
1119 m_minYExtent = 0;
1120 m_visibleItems.first()->setY(nonCreatedHeight - m_clipItem->y() + headerHeight);
1125ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
1127 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
1128 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
1129 return m_visibleItems[visibleIndexedModelIndex];
1131 return nullptr;
1134void ListViewWithPageHeader::layout()
1136 if (m_inLayout)
1137 return;
1139 m_inLayout = true;
1140 if (!m_visibleItems.isEmpty()) {
1141 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
1142 const qreal visibleTo = contentY() + height() - m_clipItem->y();
1144 qreal pos = m_visibleItems.first()->y();
1146// qDebug() << "ListViewWithPageHeader::layout Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
1147 int firstReallyVisibleItem = -1;
1148 int modelIndex = m_firstVisibleIndex;
1149 Q_FOREACH(ListItem *item, m_visibleItems) {
1150 const bool cull = pos + item->height() <= visibleFrom || pos >= visibleTo;
1151 item->setCulled(cull);
1152 item->setY(pos);
1153 if (!cull && firstReallyVisibleItem == -1) {
1154 firstReallyVisibleItem = modelIndex;
1155 if (m_topSectionItem) {
1156 // Positing the top section sticky item is a two step process
1157 // First we set it either we cull it (because it doesn't need to be sticked to the top)
1158 // or stick it to the top
1159 // Then after the loop we'll make sure that if there's another section just below it
1160 // pushed the sticky section up to make it disappear
1161 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
1162 bool showStickySectionItem;
1163 // We need to show the "top section sticky item" when the position at the "top" of the
1164 // viewport is bigger than the start of the position of the first visible item
1165 // i.e. the first visible item starts before the viewport, or when the first
1166 // visible item starts just at the viewport start and it does not have its own section item
1167 if (topSectionStickPos > pos) {
1168 showStickySectionItem = true;
1169 } else if (topSectionStickPos == pos) {
1170 showStickySectionItem = !item->sectionItem();
1171 } else {
1172 showStickySectionItem = false;
1174 if (!showStickySectionItem) {
1175 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
1176 if (item->sectionItem()) {
1177 // This seems it should happen since why would we cull the top section
1178 // if the first visible item has no section header? This only happens briefly
1179 // when increasing the height of a list that is at the bottom, the m_topSectionItem
1180 // gets shown shortly in the next polish call
1181 QQuickItemPrivate::get(item->sectionItem())->setCulled(false);
1183 } else {
1184 // Update the top sticky section header
1185 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1186 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
1187 context->setContextProperty(QLatin1String("section"), section);
1189 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
1190 m_topSectionItem->setY(topSectionStickPos);
1191 int delegateIndex = modelIndex;
1192 // Look for the first index with this section text
1193 while (delegateIndex > 0) {
1194 const QString prevSection = m_delegateModel->stringValue(delegateIndex - 1, m_sectionProperty);
1195 if (prevSection != section)
1196 break;
1197 delegateIndex--;
1199 context->setContextProperty(QLatin1String("delegateIndex"), delegateIndex);
1200 if (item->sectionItem()) {
1201 QQuickItemPrivate::get(item->sectionItem())->setCulled(true);
1206 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
1207 const qreal clipFrom = visibleFrom + (!item->sectionItem() && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
1208 if (!cull && pos < clipFrom) {
1209 context->setContextProperty(QLatin1String("heightToClip"), clipFrom - pos);
1210 } else {
1211 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
1213// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
1214 pos += item->height();
1215 ++modelIndex;
1218 // Second step of section sticky item positioning
1219 // Look at the next section header, check if it's pushing up the sticky one
1220 if (m_topSectionItem) {
1221 if (firstReallyVisibleItem >= 0) {
1222 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
1223 ListItem *item = m_visibleItems[i];
1224 if (item->sectionItem()) {
1225 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
1226 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
1228 break;
1234 m_inLayout = false;
1237void ListViewWithPageHeader::updatePolish()
1239 // Check we are not being taken down and don't paint anything
1240 // TODO Check if we still need this in 5.2
1241 // For reproduction just inifnite loop testDash or testDashContent
1242 if (!QQmlEngine::contextForObject(this)->parentContext())
1243 return;
1245 Q_FOREACH(ListItem *item, m_itemsToRelease)
1246 reallyReleaseItem(item);
1247 m_itemsToRelease.clear();
1249 if (!model())
1250 return;
1252 layout();
1254 refill();
1256 if (m_contentHeightDirty) {
1257 qreal contentHeight;
1258 if (m_visibleItems.isEmpty()) {
1259 contentHeight = m_headerItem ? m_headerItem->height() : 0;
1260 } else {
1261 const int modelCount = model()->rowCount();
1262 const int visibleItems = m_visibleItems.count();
1263 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
1264 qreal nonCreatedHeight = 0;
1265 if (lastValidIndex != modelCount - 1) {
1266 const int visibleItems = m_visibleItems.count();
1267 qreal visibleItemsHeight = 0;
1268 Q_FOREACH(ListItem *item, m_visibleItems) {
1269 visibleItemsHeight += item->height();
1271 const int unknownSizes = modelCount - (m_firstVisibleIndex + visibleItems);
1272 nonCreatedHeight = unknownSizes * visibleItemsHeight / visibleItems;
1274 ListItem *item = m_visibleItems.last();
1275 contentHeight = nonCreatedHeight + item->y() + item->height() + m_clipItem->y();
1276 if (m_firstVisibleIndex != 0) {
1277 // Make sure that if we are shrinking we tell the view we still fit
1278 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
1282 m_contentHeightDirty = false;
1283 adjustMinYExtent();
1284 m_inContentHeightKeepHeaderShown = m_headerItem && m_headerItem->y() == contentY();
1285 setContentHeight(contentHeight);
1286 m_inContentHeightKeepHeaderShown = false;
1290#include "moc_listviewwithpageheader.cpp"