on beb9977 help sign in
unity8/plugins/Dash/listviewwithpageheader.cpp
1299 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);
141ListViewWithPageHeader::ListViewWithPageHeader()
142 : m_delegateModel(nullptr)
143 , m_asyncRequestedIndex(-1)
144 , m_delegateValidated(false)
145 , m_firstVisibleIndex(-1)
146 , m_minYExtent(0)
147 , m_contentHeightDirty(false)
148 , m_headerItem(nullptr)
149 , m_previousContentY(0)
150 , m_headerItemShownHeight(0)
151 , m_sectionDelegate(nullptr)
152 , m_topSectionItem(nullptr)
153 , m_forceNoClip(false)
154 , m_inLayout(false)
155 , m_inContentHeightKeepHeaderShown(false)
157 m_clipItem = new QQuickItem(contentItem());
158// m_clipItem = new QQuickRectangle(contentItem());
159// ((QQuickRectangle*)m_clipItem)->setColor(Qt::gray);
161 m_contentYAnimation = new QQuickNumberAnimation(this);
162 m_contentYAnimation->setEasing(QEasingCurve::OutQuad);
163 m_contentYAnimation->setProperty("contentY");
164 m_contentYAnimation->setDuration(200);
165 m_contentYAnimation->setTargetObject(this);
167 connect(this, SIGNAL(contentWidthChanged()), this, SLOT(onContentWidthChanged()));
168 connect(this, SIGNAL(contentHeightChanged()), this, SLOT(onContentHeightChanged()));
169 connect(this, SIGNAL(heightChanged()), this, SLOT(onHeightChanged()));
170 connect(m_contentYAnimation, SIGNAL(stopped()), this, SLOT(onShowHeaderAnimationFinished()));
172 setFlickableDirection(VerticalFlick);
175ListViewWithPageHeader::~ListViewWithPageHeader()
179QAbstractItemModel *ListViewWithPageHeader::model() const
181 return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
184void ListViewWithPageHeader::setModel(QAbstractItemModel *model)
186 if (model != this->model()) {
187 if (!m_delegateModel) {
188 createDelegateModel();
189 } else {
190 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
191 }
192 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
193 connect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
194 Q_EMIT modelChanged();
195 polish();
196 // TODO?
197// Q_EMIT contentHeightChanged();
198// Q_EMIT contentYChanged();
199 }
202QQmlComponent *ListViewWithPageHeader::delegate() const
204 return m_delegateModel ? m_delegateModel->delegate() : nullptr;
207void ListViewWithPageHeader::setDelegate(QQmlComponent *delegate)
209 if (delegate != this->delegate()) {
210 if (!m_delegateModel) {
211 createDelegateModel();
212 }
214 // Cleanup the existing items
215 Q_FOREACH(ListItem *item, m_visibleItems)
216 releaseItem(item);
217 m_visibleItems.clear();
218 m_firstVisibleIndex = -1;
219 adjustMinYExtent();
220 setContentY(0);
221 m_clipItem->setY(0);
222 if (m_topSectionItem) {
223 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
224 }
226 m_delegateModel->setDelegate(delegate);
228 Q_EMIT delegateChanged();
229 m_delegateValidated = false;
230 m_contentHeightDirty = true;
231 polish();
232 }
235QQuickItem *ListViewWithPageHeader::header() const
237 return m_headerItem;
240void ListViewWithPageHeader::setHeader(QQuickItem *headerItem)
242 if (m_headerItem != headerItem) {
243 qreal oldHeaderHeight = 0;
244 qreal oldHeaderY = 0;
245 if (m_headerItem) {
246 oldHeaderHeight = m_headerItem->height();
247 oldHeaderY = m_headerItem->y();
248 m_headerItem->setParentItem(nullptr);
249 }
250 m_headerItem = headerItem;
251 if (m_headerItem) {
252 m_headerItem->setParentItem(contentItem());
253 m_headerItem->setZ(1);
254 m_previousHeaderImplicitHeight = m_headerItem->implicitHeight();
255 QQuickItemPrivate::get(m_headerItem)->addItemChangeListener(this, QQuickItemPrivate::ImplicitHeight);
256 }
257 qreal newHeaderHeight = m_headerItem ? m_headerItem->height() : 0;
258 if (!m_visibleItems.isEmpty() && newHeaderHeight != oldHeaderHeight) {
259 headerHeightChanged(newHeaderHeight, oldHeaderHeight, oldHeaderY);
260 polish();
261 m_contentHeightDirty = true;
262 }
263 Q_EMIT headerChanged();
264 }
267QQmlComponent *ListViewWithPageHeader::sectionDelegate() const
269 return m_sectionDelegate;
272void ListViewWithPageHeader::setSectionDelegate(QQmlComponent *delegate)
274 if (delegate != m_sectionDelegate) {
275 // TODO clean existing sections
277 m_sectionDelegate = delegate;
279 m_topSectionItem = getSectionItem(QString());
280 m_topSectionItem->setZ(3);
281 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
282 connect(m_topSectionItem, SIGNAL(heightChanged()), SIGNAL(stickyHeaderHeightChanged()));
284 // TODO create sections for existing items
286 Q_EMIT sectionDelegateChanged();
287 Q_EMIT stickyHeaderHeightChanged();
288 }
291QString ListViewWithPageHeader::sectionProperty() const
293 return m_sectionProperty;
296void ListViewWithPageHeader::setSectionProperty(const QString &property)
298 if (property != m_sectionProperty) {
299 m_sectionProperty = property;
301 updateWatchedRoles();
303 // TODO recreate sections
305 Q_EMIT sectionPropertyChanged();
306 }
309bool ListViewWithPageHeader::forceNoClip() const
311 return m_forceNoClip;
314void ListViewWithPageHeader::setForceNoClip(bool noClip)
316 if (noClip != m_forceNoClip) {
317 m_forceNoClip = noClip;
318 updateClipItem();
319 Q_EMIT forceNoClipChanged();
320 }
323int ListViewWithPageHeader::stickyHeaderHeight() const
325 return m_topSectionItem ? m_topSectionItem->height() : 0;
328void ListViewWithPageHeader::positionAtBeginning()
330 if (m_delegateModel->count() <= 0)
331 return;
333 qreal headerHeight = (m_headerItem ? m_headerItem->height() : 0);
334 if (m_firstVisibleIndex != 0) {
335 // TODO This could be optimized by trying to reuse the interesection
336 // of items that may end up intersecting between the existing
337 // m_visibleItems and the items we are creating in the next loop
338 Q_FOREACH(ListItem *item, m_visibleItems)
339 releaseItem(item);
340 m_visibleItems.clear();
341 m_firstVisibleIndex = -1;
343 // Create the item 0, it will be already correctly positioned at createItem()
344 m_clipItem->setY(0);
345 ListItem *item = createItem(0, false);
346 // Create the subsequent items
347 int modelIndex = 1;
348 qreal pos = item->y() + item->height();
349 const qreal buffer = height() * bufferRatio;
350 const qreal bufferTo = height() + buffer;
351 while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
352 if (!(item = createItem(modelIndex, false)))
353 break;
354 pos += item->height();
355 ++modelIndex;
356 }
358 m_previousContentY = m_visibleItems.first()->y() - headerHeight;
359 }
360 setContentY(m_visibleItems.first()->y() + m_clipItem->y() - headerHeight);
361 if (m_headerItem) {
362 // TODO This should not be needed and the code that adjust the m_headerItem position
363 // in viewportMoved() should be enough but in some cases we have not found a way to reproduce
364 // yet the code of viewportMoved() fails so here we make sure that at least if we are calling
365 // positionAtBeginning the header item will be correctly positioned
366 m_headerItem->setY(-m_minYExtent);
367 }
370static inline bool uFuzzyCompare(qreal r1, qreal r2)
372 return qFuzzyCompare(r1, r2) || (qFuzzyIsNull(r1) && qFuzzyIsNull(r2));
375void ListViewWithPageHeader::showHeader()
377 if (!m_headerItem)
378 return;
380 const auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
381 if (!uFuzzyCompare(to, contentY())) {
382 const bool headerShownByItsOwn = contentY() < m_headerItem->y() + m_headerItem->height();
383 if (headerShownByItsOwn && m_headerItemShownHeight == 0) {
384 // We are not clipping since we are just at the top of the viewport
385 // but because of the showHeader animation we will need to, so
386 // enable the clipping without logically moving the items
387 m_headerItemShownHeight = m_headerItem->y() + m_headerItem->height() - contentY();
388 if (!m_visibleItems.isEmpty()) {
389 updateClipItem();
390 ListItem *firstItem = m_visibleItems.first();
391 firstItem->setY(firstItem->y() - m_headerItemShownHeight);
392 layout();
393 }
394 }
395 m_contentYAnimation->setTo(to);
396 contentYAnimationType = ContentYAnimationShowHeader;
397 m_contentYAnimation->start();
398 }
401QQuickItem *ListViewWithPageHeader::item(int modelIndex) const
403 ListItem *item = itemAtIndex(modelIndex);
404 if (item)
405 return item->m_item;
406 else
407 return nullptr;
410bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex)
412 ListItem *listItem = itemAtIndex(modelIndex);
413 if (listItem) {
414 return maximizeVisibleArea(listItem, listItem->height());
415 }
417 return false;
420bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex, int itemHeight)
422 if (itemHeight < 0)
423 return false;
425 ListItem *listItem = itemAtIndex(modelIndex);
426 if (listItem) {
427 return maximizeVisibleArea(listItem, itemHeight + (listItem->m_sectionItem ? listItem->m_sectionItem->height() : 0));
428 }
430 return false;
433bool ListViewWithPageHeader::maximizeVisibleArea(ListItem *listItem, int listItemHeight)
435 if (listItem) {
436 layout();
437 const auto listItemY = m_clipItem->y() + listItem->y();
438 if (listItemY > contentY() && listItemY + listItemHeight > contentY() + height()) {
439 // we can scroll the list up to show more stuff
440 const auto to = qMin(listItemY, listItemY + listItemHeight - height());
441 m_contentYAnimation->setTo(to);
442 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
443 m_contentYAnimation->start();
444 } else if ((listItemY < contentY() && listItemY + listItemHeight < contentY() + height()) ||
445 (m_topSectionItem && !listItem->m_sectionItem && listItemY - m_topSectionItem->height() < contentY() && listItemY + listItemHeight < contentY() + height()))
446 {
447 // we can scroll the list down to show more stuff
448 auto realVisibleListItemY = listItemY;
449 if (m_topSectionItem) {
450 // If we are showing the top section sticky item and this item doesn't have a section
451 // item we have to make sure to scroll it a bit more so that it is not underlapping
452 // the top section sticky item
453 bool topSectionShown = !QQuickItemPrivate::get(m_topSectionItem)->culled;
454 if (topSectionShown && !listItem->m_sectionItem) {
455 realVisibleListItemY -= m_topSectionItem->height();
456 }
457 }
458 const auto to = qMax(realVisibleListItemY, listItemY + listItemHeight - height());
459 m_contentYAnimation->setTo(to);
460 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
461 m_contentYAnimation->start();
462 }
463 return true;
464 }
465 return false;
468qreal ListViewWithPageHeader::minYExtent() const
470// qDebug() << "ListViewWithPageHeader::minYExtent" << m_minYExtent;
471 return m_minYExtent;
474void ListViewWithPageHeader::componentComplete()
476 if (m_delegateModel)
477 m_delegateModel->componentComplete();
479 QQuickFlickable::componentComplete();
481 polish();
484void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
486 // Check we are not being taken down and don't paint anything
487 // TODO Check if we still need this in 5.2
488 // For reproduction just inifnite loop testDash or testDashContent
489 if (!QQmlEngine::contextForObject(this)->parentContext())
490 return;
492 QQuickFlickable::viewportMoved(orient);
493// qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
494 const qreal diff = m_previousContentY - contentY();
495 adjustHeader(diff);
496 m_previousContentY = contentY();
497 layout();
498 polish();
501void ListViewWithPageHeader::adjustHeader(qreal diff)
503 const bool showHeaderAnimationRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationShowHeader;
504 if (m_headerItem) {
505 const auto oldHeaderItemShownHeight = m_headerItemShownHeight;
506 if (uFuzzyCompare(contentY(), -m_minYExtent) || contentY() > -m_minYExtent) {
507 m_headerItem->setHeight(m_headerItem->implicitHeight());
508 // We are going down (but it's not because of the rebound at the end)
509 // (but the header was not shown by it's own position)
510 // or the header is partially shown and we are not doing a maximizeVisibleArea either
511 const bool scrolledUp = m_previousContentY > contentY();
512 const bool notRebounding = contentY() + height() < contentHeight();
513 const bool notShownByItsOwn = contentY() + diff >= m_headerItem->y() + m_headerItem->height();
514 const bool maximizeVisibleAreaRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationMaximizeVisibleArea;
516 if (!scrolledUp && (contentY() == -m_minYExtent || (m_headerItemShownHeight == 0 && m_previousContentY == m_headerItem->y()))) {
517 m_headerItemShownHeight = 0;
518 m_headerItem->setY(-m_minYExtent);
519 } else if ((scrolledUp && notRebounding && notShownByItsOwn && !maximizeVisibleAreaRunning) || (m_headerItemShownHeight > 0) || m_inContentHeightKeepHeaderShown) {
520 if (maximizeVisibleAreaRunning && diff > 0) {
521 // If we are maximizing and the header was shown, make sure we hide it
522 m_headerItemShownHeight -= diff;
523 } else {
524 m_headerItemShownHeight += diff;
525 }
526 if (uFuzzyCompare(contentY(), -m_minYExtent)) {
527 m_headerItemShownHeight = 0;
528 } else {
529 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
530 }
531 if (m_headerItemShownHeight > 0) {
532 if (uFuzzyCompare(m_headerItem->height(), m_headerItemShownHeight)) {
533 m_headerItem->setY(contentY());
534 m_headerItemShownHeight = m_headerItem->height();
535 } else {
536 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
537 }
538 } else {
539 m_headerItem->setY(-m_minYExtent);
540 }
541 }
542 } else {
543 // Stick the header item to the top when dragging down
544 m_headerItem->setY(contentY());
545 m_headerItem->setHeight(m_headerItem->implicitHeight() + (-m_minYExtent - contentY()));
546 }
547 // We will be changing the clip item, need to accomadate for it
548 // otherwise we move the firstItem down/up twice (unless the
549 // show header animation is running, where we want to keep the viewport stable)
550 if (!showHeaderAnimationRunning) {
551 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
552 } else {
553 diff = -diff;
554 }
555 }
556 if (!m_visibleItems.isEmpty()) {
557 updateClipItem();
558 ListItem *firstItem = m_visibleItems.first();
559 firstItem->setY(firstItem->y() + diff);
560 if (showHeaderAnimationRunning) {
561 adjustMinYExtent();
562 }
563 }
566void ListViewWithPageHeader::createDelegateModel()
568 m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
569 connect(m_delegateModel, SIGNAL(createdItem(int,QObject*)), this, SLOT(itemCreated(int,QObject*)));
570 if (isComponentComplete())
571 m_delegateModel->componentComplete();
572 updateWatchedRoles();
575void ListViewWithPageHeader::refill()
577 if (m_inLayout) {
578 return;
579 }
580 if (!isComponentComplete()) {
581 return;
582 }
584 const qreal buffer = height() * bufferRatio;
585 const qreal from = contentY();
586 const qreal to = from + height();
587 const qreal bufferFrom = from - buffer;
588 const qreal bufferTo = to + buffer;
590 bool added = addVisibleItems(from, to, false);
591 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
592 added |= addVisibleItems(bufferFrom, bufferTo, true);
594 if (added || removed) {
595 m_contentHeightDirty = true;
596 }
599bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
601 if (!delegate())
602 return false;
604 if (m_delegateModel->count() == 0)
605 return false;
607 ListItem *item;
608// qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
610 int modelIndex = 0;
611 qreal pos = 0;
612 if (!m_visibleItems.isEmpty()) {
613 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
614 item = m_visibleItems.last();
615 pos = item->y() + item->height() + m_clipItem->y();
616 }
617 bool changed = false;
618// qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
619 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
620// qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
621 if (!(item = createItem(modelIndex, asynchronous)))
622 break;
623 pos += item->height();
624 ++modelIndex;
625 changed = true;
626 }
628 modelIndex = 0;
629 pos = 0;
630 if (!m_visibleItems.isEmpty()) {
631 modelIndex = m_firstVisibleIndex - 1;
632 item = m_visibleItems.first();
633 pos = item->y() + m_clipItem->y();
634 }
635 while (modelIndex >= 0 && pos > fillFrom) {
636// qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
637 if (!(item = createItem(modelIndex, asynchronous)))
638 break;
639 pos -= item->height();
640 --modelIndex;
641 changed = true;
642 }
644 return changed;
647void ListViewWithPageHeader::reallyReleaseItem(ListItem *listItem)
649 QQuickItem *item = listItem->m_item;
650 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
651 if (flags & QQmlDelegateModel::Destroyed) {
652 item->setParentItem(nullptr);
653 }
654 delete listItem->m_sectionItem;
655 delete listItem;
658void ListViewWithPageHeader::releaseItem(ListItem *listItem)
660 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(listItem->m_item);
661 itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
662 m_itemsToRelease << listItem;
665void ListViewWithPageHeader::updateWatchedRoles()
667 if (m_delegateModel) {
668 QList<QByteArray> roles;
669 if (!m_sectionProperty.isEmpty())
670 roles << m_sectionProperty.toUtf8();
671 m_delegateModel->setWatchedRoles(roles);
672 }
675QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
677 if (!m_sectionDelegate)
678 return nullptr;
680 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
681 if (modelIndex > 0) {
682 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
683 if (section == prevSection)
684 return nullptr;
685 }
686 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
687 // Already inserted items can't steal next section header
688 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
689 if (section == nextSection) {
690 // Steal the section header
691 ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
692 if (nextItem) {
693 QQuickItem *sectionItem = nextItem->m_sectionItem;
694 nextItem->m_sectionItem = nullptr;
695 return sectionItem;
696 }
697 }
698 }
700 return getSectionItem(section);
703QQuickItem *ListViewWithPageHeader::getSectionItem(const QString &sectionText)
705 QQuickItem *sectionItem = nullptr;
707 QQmlContext *creationContext = m_sectionDelegate->creationContext();
708 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
709 context->setContextProperty(QLatin1String("section"), sectionText);
710 context->setContextProperty(QLatin1String("delegateIndex"), -1);
711 QObject *nobj = m_sectionDelegate->beginCreate(context);
712 if (nobj) {
713 QQml_setParent_noEvent(context, nobj);
714 sectionItem = qobject_cast<QQuickItem *>(nobj);
715 if (!sectionItem) {
716 delete nobj;
717 } else {
718 sectionItem->setZ(2);
719 QQml_setParent_noEvent(sectionItem, m_clipItem);
720 sectionItem->setParentItem(m_clipItem);
721 }
722 } else {
723 delete context;
724 }
725 m_sectionDelegate->completeCreate();
727 // TODO attach to sectionItem so we can accomodate to it changing its height
729 return sectionItem;
732bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
734// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
735 // Do not remove items if we are overshooting up or down, since we'll come back
736 // to the "stable" position and delete/create items without any reason
737 if (contentY() < -m_minYExtent) {
738 return false;
739 } else if (contentY() + height() > contentHeight()) {
740 return false;
741 }
742 bool changed = false;
744 bool foundVisible = false;
745 int i = 0;
746 int removedItems = 0;
747 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
748 while (i < m_visibleItems.count()) {
749 ListItem *item = m_visibleItems[i];
750 const qreal pos = item->y() + m_clipItem->y();
751// qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
752 if (pos + item->height() < bufferFrom || pos > bufferTo) {
753// qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
754 releaseItem(item);
755 m_visibleItems.removeAt(i);
756 changed = true;
757 ++removedItems;
758 } else {
759 if (!foundVisible) {
760 foundVisible = true;
761 const int itemIndex = m_firstVisibleIndex + removedItems + i;
762 m_firstVisibleIndex = itemIndex;
763 }
764 ++i;
765 }
766 }
767 if (!foundVisible) {
768 m_firstVisibleIndex = -1;
769 }
770 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
771 adjustMinYExtent();
772 }
774 return changed;
777ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
779// qDebug() << "CREATE ITEM" << modelIndex;
780 if (asynchronous && m_asyncRequestedIndex != -1)
781 return nullptr;
783 m_asyncRequestedIndex = -1;
784 QObject* object = m_delegateModel->object(modelIndex, asynchronous);
785 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
786 if (!item) {
787 if (object) {
788 m_delegateModel->release(object);
789 if (!m_delegateValidated) {
790 m_delegateValidated = true;
791 QObject* delegateObj = delegate();
792 qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
793 }
794 } else {
795 m_asyncRequestedIndex = modelIndex;
796 }
797 return 0;
798 } else {
799// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
800 ListItem *listItem = new ListItem;
801 listItem->m_item = item;
802 listItem->m_sectionItem = getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/);
803 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
804 ListItem *prevItem = itemAtIndex(modelIndex - 1);
805 bool lostItem = false; // Is an item that we requested async but because of model changes
806 // it is no longer attached to any of the existing items (has no prev nor next item)
807 // nor is the first item
808 if (prevItem) {
809 listItem->setY(prevItem->y() + prevItem->height());
810 } else {
811 ListItem *currItem = itemAtIndex(modelIndex);
812 if (currItem) {
813 // There's something already in m_visibleItems at out index, meaning this is an insert, so attach to its top
814 listItem->setY(currItem->y() - listItem->height());
815 } else {
816 ListItem *nextItem = itemAtIndex(modelIndex + 1);
817 if (nextItem) {
818 listItem->setY(nextItem->y() - listItem->height());
819 } else if (modelIndex == 0 && m_headerItem) {
820 listItem->setY(m_headerItem->height());
821 } else if (!m_visibleItems.isEmpty()) {
822 lostItem = true;
823 }
824 }
825 }
826 if (lostItem) {
827 listItem->setCulled(true);
828 releaseItem(listItem);
829 listItem = nullptr;
830 } else {
831 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() <= contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
832 if (m_visibleItems.isEmpty()) {
833 m_visibleItems << listItem;
834 } else {
835 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
836 }
837 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
838 m_firstVisibleIndex = modelIndex;
839 polish();
840 }
841 if (listItem->m_sectionItem) {
842 QQmlContext *context = QQmlEngine::contextForObject(listItem->m_sectionItem)->parentContext();
843 context->setContextProperty(QLatin1String("delegateIndex"), modelIndex);
844 }
845 adjustMinYExtent();
846 m_contentHeightDirty = true;
847 }
848 return listItem;
849 }
852void ListViewWithPageHeader::itemCreated(int modelIndex, QObject *object)
854 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
855 if (!item) {
856 qWarning() << "ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
857 return;
858 }
859// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
860 // Check we are not being taken down and don't paint anything
861 // TODO Check if we still need this in 5.2
862 // For reproduction just inifnite loop testDash or testDashContent
863 if (!QQmlEngine::contextForObject(this)->parentContext())
864 return;
866 item->setParentItem(m_clipItem);
867 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
868 context->setContextProperty(QLatin1String("ListViewWithPageHeader"), this);
869 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
870 if (modelIndex == m_asyncRequestedIndex) {
871 createItem(modelIndex, false);
872 refill();
873 }
876void ListViewWithPageHeader::updateClipItem()
878 m_clipItem->setHeight(height() - m_headerItemShownHeight);
879 m_clipItem->setY(contentY() + m_headerItemShownHeight);
880 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
883void ListViewWithPageHeader::onContentHeightChanged()
885 updateClipItem();
888void ListViewWithPageHeader::onContentWidthChanged()
890 m_clipItem->setWidth(contentItem()->width());
893void ListViewWithPageHeader::onHeightChanged()
895 polish();
899void ListViewWithPageHeader::onModelUpdated(const QQmlChangeSet &changeSet, bool /*reset*/)
901 // TODO Do something with reset
902// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
903 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
905 Q_FOREACH(const QQmlChangeSet::Remove &remove, changeSet.removes()) {
906// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove.index << remove.count;
907 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
908 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
909 // If all the items we are removing are either not created or culled
910 // we have to grow down to avoid viewport changing
911 bool growDown = true;
912 for (int i = 0; growDown && i < remove.count; ++i) {
913 const int modelIndex = remove.index + i;
914 ListItem *item = itemAtIndex(modelIndex);
915 if (item && !item->culled()) {
916 growDown = false;
917 }
918 }
919 for (int i = remove.count - 1; i >= 0; --i) {
920 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
921 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
922 ListItem *item = m_visibleItems[visibleIndex];
923 // Pass the section item down if needed
924 if (item->m_sectionItem && visibleIndex + 1 < m_visibleItems.count()) {
925 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
926 if (!nextItem->m_sectionItem) {
927 nextItem->m_sectionItem = item->m_sectionItem;
928 item->m_sectionItem = nullptr;
929 }
930 }
931 releaseItem(item);
932 m_visibleItems.removeAt(visibleIndex);
933 }
934 }
935 if (growDown) {
936 adjustMinYExtent();
937 } else if (remove.index <= m_firstVisibleIndex) {
938 if (!m_visibleItems.isEmpty()) {
939 // We removed the first item that is the one that positions the rest
940 // position the new first item correctly
941 m_visibleItems.first()->setY(oldFirstValidIndexPos);
942 } else {
943 m_firstVisibleIndex = -1;
944 }
945 }
946 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
947 m_firstVisibleIndex -= remove.count;
948 }
949 for (int i = remove.count - 1; i >= 0; --i) {
950 const int modelIndex = remove.index + i;
951 if (modelIndex == m_asyncRequestedIndex) {
952 m_asyncRequestedIndex = -1;
953 } else if (modelIndex < m_asyncRequestedIndex) {
954 m_asyncRequestedIndex--;
955 }
956 }
957 }
959 Q_FOREACH(const QQmlChangeSet::Insert &insert, changeSet.inserts()) {
960// qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
961 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
962 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
963 if (insertingInValidIndexes || firstItemWithViewOnTop)
964 {
965 // If the items we are adding won't be really visible
966 // we grow up instead of down to not change the viewport
967 bool growUp = false;
968 if (!firstItemWithViewOnTop) {
969 for (int i = 0; i < m_visibleItems.count(); ++i) {
970 if (!m_visibleItems[i]->culled()) {
971 if (insert.index <= m_firstVisibleIndex + i) {
972 growUp = true;
973 }
974 break;
975 }
976 }
977 }
979 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
980 for (int i = insert.count - 1; i >= 0; --i) {
981 const int modelIndex = insert.index + i;
982 ListItem *item = createItem(modelIndex, false);
983 if (growUp) {
984 ListItem *firstItem = m_visibleItems.first();
985 firstItem->setY(firstItem->y() - item->height());
986 }
987 // Adding an item may break a "same section" chain, so check
988 // if we need adding a new section item
989 if (m_sectionDelegate) {
990 ListItem *nextItem = itemAtIndex(modelIndex + 1);
991 if (nextItem && !nextItem->m_sectionItem) {
992 nextItem->m_sectionItem = getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/);
993 if (growUp && nextItem->m_sectionItem) {
994 ListItem *firstItem = m_visibleItems.first();
995 firstItem->setY(firstItem->y() - nextItem->m_sectionItem->height());
996 }
997 }
998 }
999 }
1000 if (firstItemWithViewOnTop) {
1001 ListItem *firstItem = m_visibleItems.first();
1002 firstItem->setY(oldFirstValidIndexPos);
1004 adjustMinYExtent();
1005 } else if (insert.index <= m_firstVisibleIndex) {
1006 m_firstVisibleIndex += insert.count;
1009 for (int i = insert.count - 1; i >= 0; --i) {
1010 const int modelIndex = insert.index + i;
1011 if (modelIndex <= m_asyncRequestedIndex) {
1012 m_asyncRequestedIndex++;
1017 Q_FOREACH(const QQmlChangeSet::Change &change, changeSet.changes()) {
1018// qDebug() << "ListViewWithPageHeader::onModelUpdated Change" << change.index << change.count;
1019 for (int i = change.index; i < change.count; ++i) {
1020 ListItem *item = itemAtIndex(i);
1021 if (item && item->m_sectionItem) {
1022 QQmlContext *context = QQmlEngine::contextForObject(item->m_sectionItem)->parentContext();
1023 const QString sectionText = m_delegateModel->stringValue(i, m_sectionProperty);
1024 context->setContextProperty(QLatin1String("section"), sectionText);
1029 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
1030 adjustMinYExtent();
1033 for (int i = 0; i < m_visibleItems.count(); ++i) {
1034 ListItem *item = m_visibleItems[i];
1035 if (item->m_sectionItem) {
1036 QQmlContext *context = QQmlEngine::contextForObject(item->m_sectionItem)->parentContext();
1037 context->setContextProperty(QLatin1String("delegateIndex"), m_firstVisibleIndex + i);
1041 layout();
1042 polish();
1043 m_contentHeightDirty = true;
1046void ListViewWithPageHeader::onShowHeaderAnimationFinished()
1048 m_contentHeightDirty = true;
1049 polish();
1052void ListViewWithPageHeader::itemGeometryChanged(QQuickItem * /*item*/, const QRectF &newGeometry, const QRectF &oldGeometry)
1054 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
1055 if (heightDiff != 0) {
1056 if (!m_inContentHeightKeepHeaderShown && oldGeometry.y() + oldGeometry.height() + m_clipItem->y() <= contentY() && !m_visibleItems.isEmpty()) {
1057 ListItem *firstItem = m_visibleItems.first();
1058 firstItem->setY(firstItem->y() - heightDiff);
1059 adjustMinYExtent();
1060 layout();
1062 refill();
1063 adjustMinYExtent();
1064 polish();
1065 m_contentHeightDirty = true;
1069void ListViewWithPageHeader::itemImplicitHeightChanged(QQuickItem *item)
1071 if (item == m_headerItem) {
1072 const qreal diff = m_headerItem->implicitHeight() - m_previousHeaderImplicitHeight;
1073 if (diff != 0) {
1074 adjustHeader(diff);
1075 m_previousHeaderImplicitHeight = m_headerItem->implicitHeight();
1076 layout();
1077 polish();
1078 m_contentHeightDirty = true;
1083void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
1085 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
1086 if (m_headerItemShownHeight > 0) {
1087 // If the header is shown because of the clip
1088 // Change its size
1089 m_headerItemShownHeight += heightDiff;
1090 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
1091 updateClipItem();
1092 adjustMinYExtent();
1093 } else {
1094 if (oldHeaderY + oldHeaderHeight > contentY()) {
1095 // If the header is shown because its position
1096 // Change its size
1097 ListItem *firstItem = m_visibleItems.first();
1098 firstItem->setY(firstItem->y() + heightDiff);
1099 layout();
1100 } else {
1101 // If the header is not on screen, just change the start of the list
1102 // so the viewport is not changed
1103 adjustMinYExtent();
1109void ListViewWithPageHeader::adjustMinYExtent()
1111 if (m_visibleItems.isEmpty()) {
1112 m_minYExtent = 0;
1113 } else {
1114 qreal nonCreatedHeight = 0;
1115 if (m_firstVisibleIndex != 0) {
1116 // Calculate the average height of items to estimate the position of the list start
1117 const int visibleItems = m_visibleItems.count();
1118 qreal visibleItemsHeight = 0;
1119 Q_FOREACH(ListItem *item, m_visibleItems) {
1120 visibleItemsHeight += item->height();
1122 nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
1123// qDebug() << m_firstVisibleIndex << visibleItemsHeight << visibleItems << nonCreatedHeight;
1125 const qreal headerHeight = (m_headerItem ? m_headerItem->implicitHeight() : 0);
1126 m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + headerHeight;
1127 if (m_minYExtent != 0 && qFuzzyIsNull(m_minYExtent)) {
1128 m_minYExtent = 0;
1129 m_visibleItems.first()->setY(nonCreatedHeight - m_clipItem->y() + headerHeight);
1134ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
1136 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
1137 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
1138 return m_visibleItems[visibleIndexedModelIndex];
1140 return nullptr;
1143void ListViewWithPageHeader::layout()
1145 if (m_inLayout)
1146 return;
1148 m_inLayout = true;
1149 if (!m_visibleItems.isEmpty()) {
1150 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
1151 const qreal visibleTo = contentY() + height() - m_clipItem->y();
1153 qreal pos = m_visibleItems.first()->y();
1155// qDebug() << "ListViewWithPageHeader::layout Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
1156 int firstReallyVisibleItem = -1;
1157 int modelIndex = m_firstVisibleIndex;
1158 Q_FOREACH(ListItem *item, m_visibleItems) {
1159 const bool cull = pos + item->height() <= visibleFrom || pos >= visibleTo;
1160 item->setCulled(cull);
1161 item->setY(pos);
1162 if (!cull && firstReallyVisibleItem == -1) {
1163 firstReallyVisibleItem = modelIndex;
1164 if (m_topSectionItem) {
1165 // Positing the top section sticky item is a two step process
1166 // First we set it either we cull it (because it doesn't need to be sticked to the top)
1167 // or stick it to the top
1168 // Then after the loop we'll make sure that if there's another section just below it
1169 // pushed the sticky section up to make it disappear
1170 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
1171 bool showStickySectionItem;
1172 // We need to show the "top section sticky item" when the position at the "top" of the
1173 // viewport is bigger than the start of the position of the first visible item
1174 // i.e. the first visible item starts before the viewport, or when the first
1175 // visible item starts just at the viewport start and it does not have its own section item
1176 if (topSectionStickPos > pos) {
1177 showStickySectionItem = true;
1178 } else if (topSectionStickPos == pos) {
1179 showStickySectionItem = !item->m_sectionItem;
1180 } else {
1181 showStickySectionItem = false;
1183 if (!showStickySectionItem) {
1184 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
1185 if (item->m_sectionItem) {
1186 // This seems it should happen since why would we cull the top section
1187 // if the first visible item has no section header? This only happens briefly
1188 // when increasing the height of a list that is at the bottom, the m_topSectionItem
1189 // gets shown shortly in the next polish call
1190 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(false);
1192 } else {
1193 // Update the top sticky section header
1194 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1195 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
1196 context->setContextProperty(QLatin1String("section"), section);
1198 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
1199 m_topSectionItem->setY(topSectionStickPos);
1200 int delegateIndex = modelIndex;
1201 // Look for the first index with this section text
1202 while (delegateIndex > 0) {
1203 const QString prevSection = m_delegateModel->stringValue(delegateIndex - 1, m_sectionProperty);
1204 if (prevSection != section)
1205 break;
1206 delegateIndex--;
1208 context->setContextProperty(QLatin1String("delegateIndex"), delegateIndex);
1209 if (item->m_sectionItem) {
1210 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(true);
1215 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
1216 const qreal clipFrom = visibleFrom + (!item->m_sectionItem && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
1217 if (!cull && pos < clipFrom) {
1218 context->setContextProperty(QLatin1String("heightToClip"), clipFrom - pos);
1219 } else {
1220 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
1222// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
1223 pos += item->height();
1224 ++modelIndex;
1227 // Second step of section sticky item positioning
1228 // Look at the next section header, check if it's pushing up the sticky one
1229 if (m_topSectionItem) {
1230 if (firstReallyVisibleItem >= 0) {
1231 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
1232 ListItem *item = m_visibleItems[i];
1233 if (item->m_sectionItem) {
1234 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
1235 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
1237 break;
1243 m_inLayout = false;
1246void ListViewWithPageHeader::updatePolish()
1248 // Check we are not being taken down and don't paint anything
1249 // TODO Check if we still need this in 5.2
1250 // For reproduction just inifnite loop testDash or testDashContent
1251 if (!QQmlEngine::contextForObject(this)->parentContext())
1252 return;
1254 Q_FOREACH(ListItem *item, m_itemsToRelease)
1255 reallyReleaseItem(item);
1256 m_itemsToRelease.clear();
1258 if (!model())
1259 return;
1261 layout();
1263 refill();
1265 if (m_contentHeightDirty) {
1266 qreal contentHeight;
1267 if (m_visibleItems.isEmpty()) {
1268 contentHeight = m_headerItem ? m_headerItem->height() : 0;
1269 } else {
1270 const int modelCount = model()->rowCount();
1271 const int visibleItems = m_visibleItems.count();
1272 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
1273 qreal nonCreatedHeight = 0;
1274 if (lastValidIndex != modelCount - 1) {
1275 const int visibleItems = m_visibleItems.count();
1276 qreal visibleItemsHeight = 0;
1277 Q_FOREACH(ListItem *item, m_visibleItems) {
1278 visibleItemsHeight += item->height();
1280 const int unknownSizes = modelCount - (m_firstVisibleIndex + visibleItems);
1281 nonCreatedHeight = unknownSizes * visibleItemsHeight / visibleItems;
1283 ListItem *item = m_visibleItems.last();
1284 contentHeight = nonCreatedHeight + item->y() + item->height() + m_clipItem->y();
1285 if (m_firstVisibleIndex != 0) {
1286 // Make sure that if we are shrinking we tell the view we still fit
1287 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
1291 m_contentHeightDirty = false;
1292 adjustMinYExtent();
1293 m_inContentHeightKeepHeaderShown = m_headerItem && m_headerItem->y() == contentY();
1294 setContentHeight(contentHeight);
1295 m_inContentHeightKeepHeaderShown = false;
1299#include "moc_listviewwithpageheader.cpp"