on d761fea help sign in
unity8/plugins/Dash/listviewwithpageheader.cpp
1305 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;
146ListViewWithPageHeader::ListViewWithPageHeader()
147 : m_delegateModel(nullptr)
148 , m_asyncRequestedIndex(-1)
149 , m_delegateValidated(false)
150 , m_firstVisibleIndex(-1)
151 , m_minYExtent(0)
152 , m_contentHeightDirty(false)
153 , m_headerItem(nullptr)
154 , m_previousContentY(0)
155 , m_headerItemShownHeight(0)
156 , m_sectionDelegate(nullptr)
157 , m_topSectionItem(nullptr)
158 , m_forceNoClip(false)
159 , m_inLayout(false)
160 , m_inContentHeightKeepHeaderShown(false)
162 m_clipItem = new QQuickItem(contentItem());
163// m_clipItem = new QQuickRectangle(contentItem());
164// ((QQuickRectangle*)m_clipItem)->setColor(Qt::gray);
166 m_contentYAnimation = new QQuickNumberAnimation(this);
167 m_contentYAnimation->setEasing(QEasingCurve::OutQuad);
168 m_contentYAnimation->setProperty("contentY");
169 m_contentYAnimation->setDuration(200);
170 m_contentYAnimation->setTargetObject(this);
172 connect(this, SIGNAL(contentWidthChanged()), this, SLOT(onContentWidthChanged()));
173 connect(this, SIGNAL(contentHeightChanged()), this, SLOT(onContentHeightChanged()));
174 connect(this, SIGNAL(heightChanged()), this, SLOT(onHeightChanged()));
175 connect(m_contentYAnimation, SIGNAL(stopped()), this, SLOT(onShowHeaderAnimationFinished()));
177 setFlickableDirection(VerticalFlick);
180ListViewWithPageHeader::~ListViewWithPageHeader()
184QAbstractItemModel *ListViewWithPageHeader::model() const
186 return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
189void ListViewWithPageHeader::setModel(QAbstractItemModel *model)
191 if (model != this->model()) {
192 if (!m_delegateModel) {
193 createDelegateModel();
194 } else {
195 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
196 }
197 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
198 connect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
199 Q_EMIT modelChanged();
200 polish();
201 // TODO?
202// Q_EMIT contentHeightChanged();
203// Q_EMIT contentYChanged();
204 }
207QQmlComponent *ListViewWithPageHeader::delegate() const
209 return m_delegateModel ? m_delegateModel->delegate() : nullptr;
212void ListViewWithPageHeader::setDelegate(QQmlComponent *delegate)
214 if (delegate != this->delegate()) {
215 if (!m_delegateModel) {
216 createDelegateModel();
217 }
219 // Cleanup the existing items
220 Q_FOREACH(ListItem *item, m_visibleItems)
221 releaseItem(item);
222 m_visibleItems.clear();
223 m_firstVisibleIndex = -1;
224 adjustMinYExtent();
225 setContentY(0);
226 m_clipItem->setY(0);
227 if (m_topSectionItem) {
228 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
229 }
231 m_delegateModel->setDelegate(delegate);
233 Q_EMIT delegateChanged();
234 m_delegateValidated = false;
235 m_contentHeightDirty = true;
236 polish();
237 }
240QQuickItem *ListViewWithPageHeader::header() const
242 return m_headerItem;
245void ListViewWithPageHeader::setHeader(QQuickItem *headerItem)
247 if (m_headerItem != headerItem) {
248 qreal oldHeaderHeight = 0;
249 qreal oldHeaderY = 0;
250 if (m_headerItem) {
251 oldHeaderHeight = m_headerItem->height();
252 oldHeaderY = m_headerItem->y();
253 m_headerItem->setParentItem(nullptr);
254 }
255 m_headerItem = headerItem;
256 if (m_headerItem) {
257 m_headerItem->setParentItem(contentItem());
258 m_headerItem->setZ(1);
259 m_previousHeaderImplicitHeight = m_headerItem->implicitHeight();
260 QQuickItemPrivate::get(m_headerItem)->addItemChangeListener(this, QQuickItemPrivate::ImplicitHeight);
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 const qreal diff = m_previousContentY - contentY();
500 adjustHeader(diff);
501 m_previousContentY = contentY();
502 layout();
503 polish();
506void ListViewWithPageHeader::adjustHeader(qreal diff)
508 const bool showHeaderAnimationRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationShowHeader;
509 if (m_headerItem) {
510 const auto oldHeaderItemShownHeight = m_headerItemShownHeight;
511 if (uFuzzyCompare(contentY(), -m_minYExtent) || contentY() > -m_minYExtent) {
512 m_headerItem->setHeight(m_headerItem->implicitHeight());
513 // We are going down (but it's not because of the rebound at the end)
514 // (but the header was not shown by it's own position)
515 // or the header is partially shown and we are not doing a maximizeVisibleArea either
516 const bool scrolledUp = m_previousContentY > contentY();
517 const bool notRebounding = contentY() + height() < contentHeight();
518 const bool notShownByItsOwn = contentY() + diff >= m_headerItem->y() + m_headerItem->height();
519 const bool maximizeVisibleAreaRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationMaximizeVisibleArea;
521 if (!scrolledUp && (contentY() == -m_minYExtent || (m_headerItemShownHeight == 0 && m_previousContentY == m_headerItem->y()))) {
522 m_headerItemShownHeight = 0;
523 m_headerItem->setY(-m_minYExtent);
524 } else if ((scrolledUp && notRebounding && notShownByItsOwn && !maximizeVisibleAreaRunning) || (m_headerItemShownHeight > 0) || m_inContentHeightKeepHeaderShown) {
525 if (maximizeVisibleAreaRunning && diff > 0) {
526 // If we are maximizing and the header was shown, make sure we hide it
527 m_headerItemShownHeight -= diff;
528 } else {
529 m_headerItemShownHeight += diff;
530 }
531 if (uFuzzyCompare(contentY(), -m_minYExtent)) {
532 m_headerItemShownHeight = 0;
533 } else {
534 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
535 }
536 if (m_headerItemShownHeight > 0) {
537 if (uFuzzyCompare(m_headerItem->height(), m_headerItemShownHeight)) {
538 m_headerItem->setY(contentY());
539 m_headerItemShownHeight = m_headerItem->height();
540 } else {
541 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
542 }
543 } else {
544 m_headerItem->setY(-m_minYExtent);
545 }
546 }
547 } else {
548 // Stick the header item to the top when dragging down
549 m_headerItem->setY(contentY());
550 m_headerItem->setHeight(m_headerItem->implicitHeight() + (-m_minYExtent - contentY()));
551 }
552 // We will be changing the clip item, need to accomadate for it
553 // otherwise we move the firstItem down/up twice (unless the
554 // show header animation is running, where we want to keep the viewport stable)
555 if (!showHeaderAnimationRunning) {
556 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
557 } else {
558 diff = -diff;
559 }
560 }
561 if (!m_visibleItems.isEmpty()) {
562 updateClipItem();
563 ListItem *firstItem = m_visibleItems.first();
564 firstItem->setY(firstItem->y() + diff);
565 if (showHeaderAnimationRunning) {
566 adjustMinYExtent();
567 }
568 }
571void ListViewWithPageHeader::createDelegateModel()
573 m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
574 connect(m_delegateModel, SIGNAL(createdItem(int,QObject*)), this, SLOT(itemCreated(int,QObject*)));
575 if (isComponentComplete())
576 m_delegateModel->componentComplete();
577 updateWatchedRoles();
580void ListViewWithPageHeader::refill()
582 if (m_inLayout) {
583 return;
584 }
585 if (!isComponentComplete()) {
586 return;
587 }
589 const qreal buffer = height() * bufferRatio;
590 const qreal from = contentY();
591 const qreal to = from + height();
592 const qreal bufferFrom = from - buffer;
593 const qreal bufferTo = to + buffer;
595 bool added = addVisibleItems(from, to, false);
596 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
597 added |= addVisibleItems(bufferFrom, bufferTo, true);
599 if (added || removed) {
600 m_contentHeightDirty = true;
601 }
604bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
606 if (!delegate())
607 return false;
609 if (m_delegateModel->count() == 0)
610 return false;
612 ListItem *item;
613// qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
615 int modelIndex = 0;
616 qreal pos = 0;
617 if (!m_visibleItems.isEmpty()) {
618 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
619 item = m_visibleItems.last();
620 pos = item->y() + item->height() + m_clipItem->y();
621 }
622 bool changed = false;
623// qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
624 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
625// qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
626 if (!(item = createItem(modelIndex, asynchronous)))
627 break;
628 pos += item->height();
629 ++modelIndex;
630 changed = true;
631 }
633 modelIndex = 0;
634 pos = 0;
635 if (!m_visibleItems.isEmpty()) {
636 modelIndex = m_firstVisibleIndex - 1;
637 item = m_visibleItems.first();
638 pos = item->y() + m_clipItem->y();
639 }
640 while (modelIndex >= 0 && pos > fillFrom) {
641// qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
642 if (!(item = createItem(modelIndex, asynchronous)))
643 break;
644 pos -= item->height();
645 --modelIndex;
646 changed = true;
647 }
649 return changed;
652void ListViewWithPageHeader::reallyReleaseItem(ListItem *listItem)
654 QQuickItem *item = listItem->m_item;
655 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
656 if (flags & QQmlDelegateModel::Destroyed) {
657 item->setParentItem(nullptr);
658 }
659 listItem->sectionItem()->deleteLater();
660 delete listItem;
663void ListViewWithPageHeader::releaseItem(ListItem *listItem)
665 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(listItem->m_item);
666 itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
667 m_itemsToRelease << listItem;
670void ListViewWithPageHeader::updateWatchedRoles()
672 if (m_delegateModel) {
673 QList<QByteArray> roles;
674 if (!m_sectionProperty.isEmpty())
675 roles << m_sectionProperty.toUtf8();
676 m_delegateModel->setWatchedRoles(roles);
677 }
680QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
682 if (!m_sectionDelegate)
683 return nullptr;
685 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
687 if (modelIndex > 0) {
688 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
689 if (section == prevSection)
690 return nullptr;
691 }
692 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
693 // Already inserted items can't steal next section header
694 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
695 if (section == nextSection) {
696 // Steal the section header
697 ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
698 if (nextItem) {
699 QQuickItem *sectionItem = nextItem->sectionItem();
700 nextItem->setSectionItem(nullptr);
701 return sectionItem;
702 }
703 }
704 }
706 return getSectionItem(section);
709QQuickItem *ListViewWithPageHeader::getSectionItem(const QString &sectionText)
711 QQuickItem *sectionItem = nullptr;
713 QQmlContext *creationContext = m_sectionDelegate->creationContext();
714 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
715 context->setContextProperty(QLatin1String("section"), sectionText);
716 context->setContextProperty(QLatin1String("delegateIndex"), -1);
717 QObject *nobj = m_sectionDelegate->beginCreate(context);
718 if (nobj) {
719 QQml_setParent_noEvent(context, nobj);
720 sectionItem = qobject_cast<QQuickItem *>(nobj);
721 if (!sectionItem) {
722 delete nobj;
723 } else {
724 sectionItem->setZ(2);
725 QQml_setParent_noEvent(sectionItem, m_clipItem);
726 sectionItem->setParentItem(m_clipItem);
727 }
728 } else {
729 delete context;
730 }
731 m_sectionDelegate->completeCreate();
733 // TODO attach to sectionItem so we can accomodate to it changing its height
735 return sectionItem;
738bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
740// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
741 // Do not remove items if we are overshooting up or down, since we'll come back
742 // to the "stable" position and delete/create items without any reason
743 if (contentY() < -m_minYExtent) {
744 return false;
745 } else if (contentY() + height() > contentHeight()) {
746 return false;
747 }
748 bool changed = false;
750 bool foundVisible = false;
751 int i = 0;
752 int removedItems = 0;
753 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
754 while (i < m_visibleItems.count()) {
755 ListItem *item = m_visibleItems[i];
756 const qreal pos = item->y() + m_clipItem->y();
757// qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
758 if (pos + item->height() < bufferFrom || pos > bufferTo) {
759// qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
760 releaseItem(item);
761 m_visibleItems.removeAt(i);
762 changed = true;
763 ++removedItems;
764 } else {
765 if (!foundVisible) {
766 foundVisible = true;
767 const int itemIndex = m_firstVisibleIndex + removedItems + i;
768 m_firstVisibleIndex = itemIndex;
769 }
770 ++i;
771 }
772 }
773 if (!foundVisible) {
774 m_firstVisibleIndex = -1;
775 }
776 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
777 adjustMinYExtent();
778 }
780 return changed;
783ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
785// qDebug() << "CREATE ITEM" << modelIndex;
786 if (asynchronous && m_asyncRequestedIndex != -1)
787 return nullptr;
789 m_asyncRequestedIndex = -1;
790 QObject* object = m_delegateModel->object(modelIndex, asynchronous);
791 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
792 if (!item) {
793 if (object) {
794 m_delegateModel->release(object);
795 if (!m_delegateValidated) {
796 m_delegateValidated = true;
797 QObject* delegateObj = delegate();
798 qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
799 }
800 } else {
801 m_asyncRequestedIndex = modelIndex;
802 }
803 return 0;
804 } else {
805// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
806 ListItem *listItem = new ListItem;
807 listItem->m_item = item;
808 listItem->setSectionItem(getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/));
809 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
810 ListItem *prevItem = itemAtIndex(modelIndex - 1);
811 bool lostItem = false; // Is an item that we requested async but because of model changes
812 // it is no longer attached to any of the existing items (has no prev nor next item)
813 // nor is the first item
814 if (prevItem) {
815 listItem->setY(prevItem->y() + prevItem->height());
816 } else {
817 ListItem *currItem = itemAtIndex(modelIndex);
818 if (currItem) {
819 // There's something already in m_visibleItems at out index, meaning this is an insert, so attach to its top
820 listItem->setY(currItem->y() - listItem->height());
821 } else {
822 ListItem *nextItem = itemAtIndex(modelIndex + 1);
823 if (nextItem) {
824 listItem->setY(nextItem->y() - listItem->height());
825 } else if (modelIndex == 0) {
826 listItem->setY(m_headerItem ? m_headerItem->height() : 0);
827 } else if (!m_visibleItems.isEmpty()) {
828 lostItem = true;
829 }
830 }
831 }
832 if (lostItem) {
833 listItem->setCulled(true);
834 releaseItem(listItem);
835 listItem = nullptr;
836 } else {
837 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() <= contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
838 if (m_visibleItems.isEmpty()) {
839 m_visibleItems << listItem;
840 } else {
841 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
842 }
843 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
844 m_firstVisibleIndex = modelIndex;
845 polish();
846 }
847 if (listItem->sectionItem()) {
848 QQmlContext *context = QQmlEngine::contextForObject(listItem->sectionItem())->parentContext();
849 context->setContextProperty(QLatin1String("delegateIndex"), modelIndex);
850 }
851 adjustMinYExtent();
852 m_contentHeightDirty = true;
853 }
854 return listItem;
855 }
858void ListViewWithPageHeader::itemCreated(int modelIndex, QObject *object)
860 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
861 if (!item) {
862 qWarning() << "ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
863 return;
864 }
865// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
866 // Check we are not being taken down and don't paint anything
867 // TODO Check if we still need this in 5.2
868 // For reproduction just inifnite loop testDash or testDashContent
869 if (!QQmlEngine::contextForObject(this)->parentContext())
870 return;
872 item->setParentItem(m_clipItem);
873 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
874 context->setContextProperty(QLatin1String("ListViewWithPageHeader"), this);
875 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
876 if (modelIndex == m_asyncRequestedIndex) {
877 createItem(modelIndex, false);
878 refill();
879 }
882void ListViewWithPageHeader::updateClipItem()
884 m_clipItem->setHeight(height() - m_headerItemShownHeight);
885 m_clipItem->setY(contentY() + m_headerItemShownHeight);
886 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
889void ListViewWithPageHeader::onContentHeightChanged()
891 updateClipItem();
894void ListViewWithPageHeader::onContentWidthChanged()
896 m_clipItem->setWidth(contentItem()->width());
899void ListViewWithPageHeader::onHeightChanged()
901 polish();
905void ListViewWithPageHeader::onModelUpdated(const QQmlChangeSet &changeSet, bool /*reset*/)
907 // TODO Do something with reset
908// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
909 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
911 Q_FOREACH(const QQmlChangeSet::Remove &remove, changeSet.removes()) {
912// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove.index << remove.count;
913 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
914 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
915 // If all the items we are removing are either not created or culled
916 // we have to grow down to avoid viewport changing
917 bool growDown = true;
918 for (int i = 0; growDown && i < remove.count; ++i) {
919 const int modelIndex = remove.index + i;
920 ListItem *item = itemAtIndex(modelIndex);
921 if (item && !item->culled()) {
922 growDown = false;
923 }
924 }
925 for (int i = remove.count - 1; i >= 0; --i) {
926 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
927 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
928 ListItem *item = m_visibleItems[visibleIndex];
929 // Pass the section item down if needed
930 if (item->sectionItem() && visibleIndex + 1 < m_visibleItems.count()) {
931 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
932 if (!nextItem->sectionItem()) {
933 nextItem->setSectionItem(item->sectionItem());
934 item->setSectionItem(nullptr);
935 }
936 }
937 releaseItem(item);
938 m_visibleItems.removeAt(visibleIndex);
939 }
940 }
941 if (growDown) {
942 adjustMinYExtent();
943 } else if (remove.index <= m_firstVisibleIndex) {
944 if (!m_visibleItems.isEmpty()) {
945 // We removed the first item that is the one that positions the rest
946 // position the new first item correctly
947 m_visibleItems.first()->setY(oldFirstValidIndexPos);
948 } else {
949 m_firstVisibleIndex = -1;
950 }
951 }
952 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
953 m_firstVisibleIndex -= remove.count;
954 }
955 for (int i = remove.count - 1; i >= 0; --i) {
956 const int modelIndex = remove.index + i;
957 if (modelIndex == m_asyncRequestedIndex) {
958 m_asyncRequestedIndex = -1;
959 } else if (modelIndex < m_asyncRequestedIndex) {
960 m_asyncRequestedIndex--;
961 }
962 }
963 }
965 Q_FOREACH(const QQmlChangeSet::Insert &insert, changeSet.inserts()) {
966// qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
967 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
968 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
969 if (insertingInValidIndexes || firstItemWithViewOnTop)
970 {
971 // If the items we are adding won't be really visible
972 // we grow up instead of down to not change the viewport
973 bool growUp = false;
974 if (!firstItemWithViewOnTop) {
975 for (int i = 0; i < m_visibleItems.count(); ++i) {
976 if (!m_visibleItems[i]->culled()) {
977 if (insert.index <= m_firstVisibleIndex + i) {
978 growUp = true;
979 }
980 break;
981 }
982 }
983 }
985 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
986 for (int i = insert.count - 1; i >= 0; --i) {
987 const int modelIndex = insert.index + i;
988 ListItem *item = createItem(modelIndex, false);
989 if (growUp) {
990 ListItem *firstItem = m_visibleItems.first();
991 firstItem->setY(firstItem->y() - item->height());
992 }
993 // Adding an item may break a "same section" chain, so check
994 // if we need adding a new section item
995 if (m_sectionDelegate) {
996 ListItem *nextItem = itemAtIndex(modelIndex + 1);
997 if (nextItem && !nextItem->sectionItem()) {
998 nextItem->setSectionItem(getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/));
999 if (growUp && nextItem->sectionItem()) {
1000 ListItem *firstItem = m_visibleItems.first();
1001 firstItem->setY(firstItem->y() - nextItem->sectionItem()->height());
1006 if (firstItemWithViewOnTop) {
1007 ListItem *firstItem = m_visibleItems.first();
1008 firstItem->setY(oldFirstValidIndexPos);
1010 adjustMinYExtent();
1011 } else if (insert.index <= m_firstVisibleIndex) {
1012 m_firstVisibleIndex += insert.count;
1015 for (int i = insert.count - 1; i >= 0; --i) {
1016 const int modelIndex = insert.index + i;
1017 if (modelIndex <= m_asyncRequestedIndex) {
1018 m_asyncRequestedIndex++;
1023 Q_FOREACH(const QQmlChangeSet::Change &change, changeSet.changes()) {
1024// qDebug() << "ListViewWithPageHeader::onModelUpdated Change" << change.index << change.count;
1025 for (int i = change.index; i < change.count; ++i) {
1026 ListItem *item = itemAtIndex(i);
1027 if (item && item->sectionItem()) {
1028 QQmlContext *context = QQmlEngine::contextForObject(item->sectionItem())->parentContext();
1029 const QString sectionText = m_delegateModel->stringValue(i, m_sectionProperty);
1030 context->setContextProperty(QLatin1String("section"), sectionText);
1035 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
1036 adjustMinYExtent();
1039 for (int i = 0; i < m_visibleItems.count(); ++i) {
1040 ListItem *item = m_visibleItems[i];
1041 if (item->sectionItem()) {
1042 QQmlContext *context = QQmlEngine::contextForObject(item->sectionItem())->parentContext();
1043 context->setContextProperty(QLatin1String("delegateIndex"), m_firstVisibleIndex + i);
1047 layout();
1048 polish();
1049 m_contentHeightDirty = true;
1052void ListViewWithPageHeader::onShowHeaderAnimationFinished()
1054 m_contentHeightDirty = true;
1055 polish();
1058void ListViewWithPageHeader::itemGeometryChanged(QQuickItem * /*item*/, const QRectF &newGeometry, const QRectF &oldGeometry)
1060 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
1061 if (heightDiff != 0) {
1062 if (!m_inContentHeightKeepHeaderShown && oldGeometry.y() + oldGeometry.height() + m_clipItem->y() <= contentY() && !m_visibleItems.isEmpty()) {
1063 ListItem *firstItem = m_visibleItems.first();
1064 firstItem->setY(firstItem->y() - heightDiff);
1065 adjustMinYExtent();
1066 layout();
1068 refill();
1069 adjustMinYExtent();
1070 polish();
1071 m_contentHeightDirty = true;
1075void ListViewWithPageHeader::itemImplicitHeightChanged(QQuickItem *item)
1077 if (item == m_headerItem) {
1078 const qreal diff = m_headerItem->implicitHeight() - m_previousHeaderImplicitHeight;
1079 if (diff != 0) {
1080 adjustHeader(diff);
1081 m_previousHeaderImplicitHeight = m_headerItem->implicitHeight();
1082 layout();
1083 polish();
1084 m_contentHeightDirty = true;
1089void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
1091 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
1092 if (m_headerItemShownHeight > 0) {
1093 // If the header is shown because of the clip
1094 // Change its size
1095 m_headerItemShownHeight += heightDiff;
1096 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
1097 updateClipItem();
1098 adjustMinYExtent();
1099 } else {
1100 if (oldHeaderY + oldHeaderHeight > contentY()) {
1101 // If the header is shown because its position
1102 // Change its size
1103 ListItem *firstItem = m_visibleItems.first();
1104 firstItem->setY(firstItem->y() + heightDiff);
1105 layout();
1106 } else {
1107 // If the header is not on screen, just change the start of the list
1108 // so the viewport is not changed
1109 adjustMinYExtent();
1115void ListViewWithPageHeader::adjustMinYExtent()
1117 if (m_visibleItems.isEmpty()) {
1118 m_minYExtent = 0;
1119 } else {
1120 qreal nonCreatedHeight = 0;
1121 if (m_firstVisibleIndex != 0) {
1122 // Calculate the average height of items to estimate the position of the list start
1123 const int visibleItems = m_visibleItems.count();
1124 qreal visibleItemsHeight = 0;
1125 Q_FOREACH(ListItem *item, m_visibleItems) {
1126 visibleItemsHeight += item->height();
1128 nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
1129// qDebug() << m_firstVisibleIndex << visibleItemsHeight << visibleItems << nonCreatedHeight;
1131 const qreal headerHeight = (m_headerItem ? m_headerItem->implicitHeight() : 0);
1132 m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + headerHeight;
1133 if (m_minYExtent != 0 && qFuzzyIsNull(m_minYExtent)) {
1134 m_minYExtent = 0;
1135 m_visibleItems.first()->setY(nonCreatedHeight - m_clipItem->y() + headerHeight);
1140ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
1142 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
1143 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
1144 return m_visibleItems[visibleIndexedModelIndex];
1146 return nullptr;
1149void ListViewWithPageHeader::layout()
1151 if (m_inLayout)
1152 return;
1154 m_inLayout = true;
1155 if (!m_visibleItems.isEmpty()) {
1156 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
1157 const qreal visibleTo = contentY() + height() - m_clipItem->y();
1159 qreal pos = m_visibleItems.first()->y();
1161// qDebug() << "ListViewWithPageHeader::layout Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
1162 int firstReallyVisibleItem = -1;
1163 int modelIndex = m_firstVisibleIndex;
1164 Q_FOREACH(ListItem *item, m_visibleItems) {
1165 const bool cull = pos + item->height() <= visibleFrom || pos >= visibleTo;
1166 item->setCulled(cull);
1167 item->setY(pos);
1168 if (!cull && firstReallyVisibleItem == -1) {
1169 firstReallyVisibleItem = modelIndex;
1170 if (m_topSectionItem) {
1171 // Positing the top section sticky item is a two step process
1172 // First we set it either we cull it (because it doesn't need to be sticked to the top)
1173 // or stick it to the top
1174 // Then after the loop we'll make sure that if there's another section just below it
1175 // pushed the sticky section up to make it disappear
1176 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
1177 bool showStickySectionItem;
1178 // We need to show the "top section sticky item" when the position at the "top" of the
1179 // viewport is bigger than the start of the position of the first visible item
1180 // i.e. the first visible item starts before the viewport, or when the first
1181 // visible item starts just at the viewport start and it does not have its own section item
1182 if (topSectionStickPos > pos) {
1183 showStickySectionItem = true;
1184 } else if (topSectionStickPos == pos) {
1185 showStickySectionItem = !item->sectionItem();
1186 } else {
1187 showStickySectionItem = false;
1189 if (!showStickySectionItem) {
1190 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
1191 if (item->sectionItem()) {
1192 // This seems it should happen since why would we cull the top section
1193 // if the first visible item has no section header? This only happens briefly
1194 // when increasing the height of a list that is at the bottom, the m_topSectionItem
1195 // gets shown shortly in the next polish call
1196 QQuickItemPrivate::get(item->sectionItem())->setCulled(false);
1198 } else {
1199 // Update the top sticky section header
1200 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1201 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
1202 context->setContextProperty(QLatin1String("section"), section);
1204 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
1205 m_topSectionItem->setY(topSectionStickPos);
1206 int delegateIndex = modelIndex;
1207 // Look for the first index with this section text
1208 while (delegateIndex > 0) {
1209 const QString prevSection = m_delegateModel->stringValue(delegateIndex - 1, m_sectionProperty);
1210 if (prevSection != section)
1211 break;
1212 delegateIndex--;
1214 context->setContextProperty(QLatin1String("delegateIndex"), delegateIndex);
1215 if (item->sectionItem()) {
1216 QQuickItemPrivate::get(item->sectionItem())->setCulled(true);
1221 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
1222 const qreal clipFrom = visibleFrom + (!item->sectionItem() && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
1223 if (!cull && pos < clipFrom) {
1224 context->setContextProperty(QLatin1String("heightToClip"), clipFrom - pos);
1225 } else {
1226 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
1228// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
1229 pos += item->height();
1230 ++modelIndex;
1233 // Second step of section sticky item positioning
1234 // Look at the next section header, check if it's pushing up the sticky one
1235 if (m_topSectionItem) {
1236 if (firstReallyVisibleItem >= 0) {
1237 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
1238 ListItem *item = m_visibleItems[i];
1239 if (item->sectionItem()) {
1240 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
1241 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
1243 break;
1249 m_inLayout = false;
1252void ListViewWithPageHeader::updatePolish()
1254 // Check we are not being taken down and don't paint anything
1255 // TODO Check if we still need this in 5.2
1256 // For reproduction just inifnite loop testDash or testDashContent
1257 if (!QQmlEngine::contextForObject(this)->parentContext())
1258 return;
1260 Q_FOREACH(ListItem *item, m_itemsToRelease)
1261 reallyReleaseItem(item);
1262 m_itemsToRelease.clear();
1264 if (!model())
1265 return;
1267 layout();
1269 refill();
1271 if (m_contentHeightDirty) {
1272 qreal contentHeight;
1273 if (m_visibleItems.isEmpty()) {
1274 contentHeight = m_headerItem ? m_headerItem->height() : 0;
1275 } else {
1276 const int modelCount = model()->rowCount();
1277 const int visibleItems = m_visibleItems.count();
1278 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
1279 qreal nonCreatedHeight = 0;
1280 if (lastValidIndex != modelCount - 1) {
1281 const int visibleItems = m_visibleItems.count();
1282 qreal visibleItemsHeight = 0;
1283 Q_FOREACH(ListItem *item, m_visibleItems) {
1284 visibleItemsHeight += item->height();
1286 const int unknownSizes = modelCount - (m_firstVisibleIndex + visibleItems);
1287 nonCreatedHeight = unknownSizes * visibleItemsHeight / visibleItems;
1289 ListItem *item = m_visibleItems.last();
1290 contentHeight = nonCreatedHeight + item->y() + item->height() + m_clipItem->y();
1291 if (m_firstVisibleIndex != 0) {
1292 // Make sure that if we are shrinking we tell the view we still fit
1293 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
1297 m_contentHeightDirty = false;
1298 adjustMinYExtent();
1299 m_inContentHeightKeepHeaderShown = m_headerItem && m_headerItem->y() == contentY();
1300 setContentHeight(contentHeight);
1301 m_inContentHeightKeepHeaderShown = false;
1305#include "moc_listviewwithpageheader.cpp"