on 918d8ff help sign in
unity8/plugins/Dash/listviewwithpageheader.cpp
1398 lines 55 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>
107qreal ListViewWithPageHeader::ListItem::height() const
109 return m_item->height() + (m_sectionItem ? m_sectionItem->height() : 0);
112qreal ListViewWithPageHeader::ListItem::y() const
114 return m_item->y() - (m_sectionItem ? m_sectionItem->height() : 0);
117void ListViewWithPageHeader::ListItem::setY(qreal newY)
119 if (m_sectionItem) {
120 m_sectionItem->setY(newY);
121 m_item->setY(newY + m_sectionItem->height());
122 } else {
123 m_item->setY(newY);
124 }
127bool ListViewWithPageHeader::ListItem::culled() const
129 return QQuickItemPrivate::get(m_item)->culled;
132void ListViewWithPageHeader::ListItem::setCulled(bool culled)
134 QQuickItemPrivate::get(m_item)->setCulled(culled);
135 if (m_sectionItem)
136 QQuickItemPrivate::get(m_sectionItem)->setCulled(culled);
139void ListViewWithPageHeader::ListItem::setSectionItem(QQuickItem *sectionItem)
141 m_sectionItem = sectionItem;
144ListViewWithPageHeader::ListViewWithPageHeader()
145 : m_delegateModel(nullptr)
146 , m_asyncRequestedIndex(-1)
147 , m_delegateValidated(false)
148 , m_firstVisibleIndex(-1)
149 , m_minYExtent(0)
150 , m_contentHeightDirty(false)
151 , m_headerItem(nullptr)
152 , m_previousContentY(0)
153 , m_headerItemShownHeight(0)
154 , m_sectionDelegate(nullptr)
155 , m_topSectionItem(nullptr)
156 , m_forceNoClip(false)
157 , m_inLayout(false)
158 , m_inContentHeightKeepHeaderShown(false)
159 , m_cacheBuffer(0)
161 m_clipItem = new QQuickItem(contentItem());
162// m_clipItem = new QQuickRectangle(contentItem());
163// ((QQuickRectangle*)m_clipItem)->setColor(Qt::gray);
165 m_contentYAnimation = new QQuickNumberAnimation(this);
166 m_contentYAnimation->setEasing(QEasingCurve::OutQuad);
167 m_contentYAnimation->setProperty(QStringLiteral("contentY"));
168 m_contentYAnimation->setDuration(200);
169 m_contentYAnimation->setTargetObject(this);
171 connect(this, &ListViewWithPageHeader::contentWidthChanged, this, &ListViewWithPageHeader::onContentWidthChanged);
172 connect(this, &ListViewWithPageHeader::contentHeightChanged, this, &ListViewWithPageHeader::onContentHeightChanged);
173 connect(this, &ListViewWithPageHeader::heightChanged, this, &ListViewWithPageHeader::onHeightChanged);
174 connect(m_contentYAnimation, &QQuickNumberAnimation::runningChanged, this, &ListViewWithPageHeader::contentYAnimationRunningChanged);
176 setFlickableDirection(VerticalFlick);
179ListViewWithPageHeader::~ListViewWithPageHeader()
183QAbstractItemModel *ListViewWithPageHeader::model() const
185 return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
188void ListViewWithPageHeader::setModel(QAbstractItemModel *model)
190 if (model != this->model()) {
191 if (!m_delegateModel) {
192 createDelegateModel();
193 } else {
194 disconnect(m_delegateModel, &QQmlDelegateModel::modelUpdated, this, &ListViewWithPageHeader::onModelUpdated);
195 }
196 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
197 connect(m_delegateModel, &QQmlDelegateModel::modelUpdated, this, &ListViewWithPageHeader::onModelUpdated);
198 Q_EMIT modelChanged();
199 polish();
200 // TODO?
201// Q_EMIT contentHeightChanged();
202// Q_EMIT contentYChanged();
203 }
206QQmlComponent *ListViewWithPageHeader::delegate() const
208 return m_delegateModel ? m_delegateModel->delegate() : nullptr;
211void ListViewWithPageHeader::setDelegate(QQmlComponent *delegate)
213 if (delegate != this->delegate()) {
214 if (!m_delegateModel) {
215 createDelegateModel();
216 }
218 // Cleanup the existing items
219 Q_FOREACH(ListItem *item, m_visibleItems)
220 releaseItem(item);
221 m_visibleItems.clear();
222 initializeValuesForEmptyList();
224 m_delegateModel->setDelegate(delegate);
226 Q_EMIT delegateChanged();
227 m_delegateValidated = false;
228 m_contentHeightDirty = true;
229 polish();
230 }
233void ListViewWithPageHeader::initializeValuesForEmptyList()
235 m_firstVisibleIndex = -1;
236 adjustMinYExtent();
237 setContentY(0);
238 m_clipItem->setY(0);
239 if (m_topSectionItem) {
240 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
241 }
244QQuickItem *ListViewWithPageHeader::header() const
246 return m_headerItem;
249void ListViewWithPageHeader::setHeader(QQuickItem *headerItem)
251 if (m_headerItem != headerItem) {
252 qreal oldHeaderHeight = 0;
253 qreal oldHeaderY = 0;
254 if (m_headerItem) {
255 oldHeaderHeight = m_headerItem->height();
256 oldHeaderY = m_headerItem->y();
257 m_headerItem->setParentItem(nullptr);
258 }
259 m_headerItem = headerItem;
260 if (m_headerItem) {
261 m_headerItem->setParentItem(contentItem());
262 m_headerItem->setZ(1);
263 m_previousHeaderImplicitHeight = m_headerItem->implicitHeight();
264 QQuickItemPrivate::get(m_headerItem)->addItemChangeListener(this, QQuickItemPrivate::ImplicitHeight);
265 }
266 qreal newHeaderHeight = m_headerItem ? m_headerItem->height() : 0;
267 if (!m_visibleItems.isEmpty() && newHeaderHeight != oldHeaderHeight) {
268 headerHeightChanged(newHeaderHeight, oldHeaderHeight, oldHeaderY);
269 polish();
270 m_contentHeightDirty = true;
271 }
272 Q_EMIT headerChanged();
273 }
276QQmlComponent *ListViewWithPageHeader::sectionDelegate() const
278 return m_sectionDelegate;
281void ListViewWithPageHeader::setSectionDelegate(QQmlComponent *delegate)
283 if (delegate != m_sectionDelegate) {
284 // TODO clean existing sections
286 m_sectionDelegate = delegate;
288 m_topSectionItem = getSectionItem(QString(), false /*watchGeometry*/);
289 m_topSectionItem->setZ(3);
290 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
291 connect(m_topSectionItem, &QQuickItem::heightChanged, this, &ListViewWithPageHeader::stickyHeaderHeightChanged);
293 // TODO create sections for existing items
295 Q_EMIT sectionDelegateChanged();
296 Q_EMIT stickyHeaderHeightChanged();
297 }
300QString ListViewWithPageHeader::sectionProperty() const
302 return m_sectionProperty;
305void ListViewWithPageHeader::setSectionProperty(const QString &property)
307 if (property != m_sectionProperty) {
308 m_sectionProperty = property;
310 updateWatchedRoles();
312 // TODO recreate sections
314 Q_EMIT sectionPropertyChanged();
315 }
318bool ListViewWithPageHeader::forceNoClip() const
320 return m_forceNoClip;
323void ListViewWithPageHeader::setForceNoClip(bool noClip)
325 if (noClip != m_forceNoClip) {
326 m_forceNoClip = noClip;
327 updateClipItem();
328 Q_EMIT forceNoClipChanged();
329 }
332int ListViewWithPageHeader::stickyHeaderHeight() const
334 return m_topSectionItem ? m_topSectionItem->height() : 0;
337qreal ListViewWithPageHeader::headerItemShownHeight() const
339 return m_headerItemShownHeight;
342int ListViewWithPageHeader::cacheBuffer() const
344 return m_cacheBuffer;
347void ListViewWithPageHeader::setCacheBuffer(int cacheBuffer)
349 if (cacheBuffer < 0) {
350 qmlInfo(this) << "Cannot set a negative cache buffer";
351 return;
352 }
354 if (cacheBuffer != m_cacheBuffer) {
355 m_cacheBuffer = cacheBuffer;
356 Q_EMIT cacheBufferChanged();
357 polish();
358 }
361void ListViewWithPageHeader::positionAtBeginning()
363 if (m_delegateModel->count() <= 0)
364 return;
366 qreal headerHeight = (m_headerItem ? m_headerItem->height() : 0);
367 if (m_firstVisibleIndex != 0) {
368 // TODO This could be optimized by trying to reuse the interesection
369 // of items that may end up intersecting between the existing
370 // m_visibleItems and the items we are creating in the next loop
371 Q_FOREACH(ListItem *item, m_visibleItems)
372 releaseItem(item);
373 m_visibleItems.clear();
374 m_firstVisibleIndex = -1;
376 // Create the item 0, it will be already correctly positioned at createItem()
377 m_clipItem->setY(0);
378 ListItem *item = createItem(0, false);
379 // Create the subsequent items
380 int modelIndex = 1;
381 qreal pos = item->y() + item->height();
382 const qreal bufferTo = height() + m_cacheBuffer;
383 while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
384 if (!(item = createItem(modelIndex, false)))
385 break;
386 pos += item->height();
387 ++modelIndex;
388 }
390 m_previousContentY = m_visibleItems.first()->y() - headerHeight;
391 }
392 setContentY(m_visibleItems.first()->y() + m_clipItem->y() - headerHeight);
393 if (m_headerItem) {
394 // TODO This should not be needed and the code that adjust the m_headerItem position
395 // in viewportMoved() should be enough but in some cases we have not found a way to reproduce
396 // yet the code of viewportMoved() fails so here we make sure that at least if we are calling
397 // positionAtBeginning the header item will be correctly positioned
398 m_headerItem->setY(-m_minYExtent);
399 }
402static inline bool uFuzzyCompare(qreal r1, qreal r2)
404 return qFuzzyCompare(r1, r2) || (qFuzzyIsNull(r1) && qFuzzyIsNull(r2));
407void ListViewWithPageHeader::showHeader()
409 if (!m_headerItem)
410 return;
412 const auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
413 if (!uFuzzyCompare(to, contentY())) {
414 const bool headerShownByItsOwn = contentY() < m_headerItem->y() + m_headerItem->height();
415 if (headerShownByItsOwn && m_headerItemShownHeight == 0) {
416 // We are not clipping since we are just at the top of the viewport
417 // but because of the showHeader animation we will need to, so
418 // enable the clipping without logically moving the items
419 m_headerItemShownHeight = m_headerItem->y() + m_headerItem->height() - contentY();
420 if (!m_visibleItems.isEmpty()) {
421 updateClipItem();
422 ListItem *firstItem = m_visibleItems.first();
423 firstItem->setY(firstItem->y() - m_headerItemShownHeight);
424 layout();
425 }
426 Q_EMIT headerItemShownHeightChanged();
427 }
428 m_contentYAnimation->setTo(to);
429 contentYAnimationType = ContentYAnimationShowHeader;
430 m_contentYAnimation->start();
431 }
434int ListViewWithPageHeader::firstCreatedIndex() const
436 return m_firstVisibleIndex;
439int ListViewWithPageHeader::createdItemCount() const
441 return m_visibleItems.count();
444QQuickItem *ListViewWithPageHeader::item(int modelIndex) const
446 ListItem *item = itemAtIndex(modelIndex);
447 if (item)
448 return item->m_item;
449 else
450 return nullptr;
453bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex)
455 ListItem *listItem = itemAtIndex(modelIndex);
456 if (listItem) {
457 return maximizeVisibleArea(listItem, listItem->height());
458 }
460 return false;
463bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex, int itemHeight)
465 if (itemHeight < 0)
466 return false;
468 ListItem *listItem = itemAtIndex(modelIndex);
469 if (listItem) {
470 return maximizeVisibleArea(listItem, itemHeight + (listItem->sectionItem() ? listItem->sectionItem()->height() : 0));
471 }
473 return false;
476bool ListViewWithPageHeader::maximizeVisibleArea(ListItem *listItem, int listItemHeight)
478 if (listItem) {
479 layout();
480 const auto listItemY = m_clipItem->y() + listItem->y();
481 if (listItemY > contentY() && listItemY + listItemHeight > contentY() + height()) {
482 // we can scroll the list up to show more stuff
483 const auto to = qMin(listItemY, listItemY + listItemHeight - height());
484 m_contentYAnimation->setTo(to);
485 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
486 m_contentYAnimation->start();
487 } else if ((listItemY < contentY() && listItemY + listItemHeight < contentY() + height()) ||
488 (m_topSectionItem && !listItem->sectionItem() && listItemY - m_topSectionItem->height() < contentY() && listItemY + listItemHeight < contentY() + height()))
489 {
490 // we can scroll the list down to show more stuff
491 auto realVisibleListItemY = listItemY;
492 if (m_topSectionItem) {
493 // If we are showing the top section sticky item and this item doesn't have a section
494 // item we have to make sure to scroll it a bit more so that it is not underlapping
495 // the top section sticky item
496 bool topSectionShown = !QQuickItemPrivate::get(m_topSectionItem)->culled;
497 if (topSectionShown && !listItem->sectionItem()) {
498 realVisibleListItemY -= m_topSectionItem->height();
499 }
500 }
501 const auto to = qMax(realVisibleListItemY, listItemY + listItemHeight - height());
502 m_contentYAnimation->setTo(to);
503 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
504 m_contentYAnimation->start();
505 }
506 return true;
507 }
508 return false;
511qreal ListViewWithPageHeader::minYExtent() const
513// qDebug() << "ListViewWithPageHeader::minYExtent" << m_minYExtent;
514 return m_minYExtent;
517qreal ListViewWithPageHeader::maxYExtent() const
519 return height() - contentHeight();
522void ListViewWithPageHeader::componentComplete()
524 if (m_delegateModel)
525 m_delegateModel->componentComplete();
527 QQuickFlickable::componentComplete();
529 polish();
532void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
534 // Check we are not being taken down and don't paint anything
535 // TODO Check if we still need this in 5.2
536 // For reproduction just inifnite loop testDash or testDashContent
537 if (!QQmlEngine::contextForObject(this)->parentContext())
538 return;
540 QQuickFlickable::viewportMoved(orient);
541// qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
542 const qreal diff = m_previousContentY - contentY();
543 adjustHeader(diff);
544 m_previousContentY = contentY();
545 layout();
546 polish();
549void ListViewWithPageHeader::adjustHeader(qreal diff)
551 const bool showHeaderAnimationRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationShowHeader;
552 if (m_headerItem) {
553 const auto oldHeaderItemShownHeight = m_headerItemShownHeight;
554 if (uFuzzyCompare(contentY(), -m_minYExtent) || contentY() > -m_minYExtent) {
555 m_headerItem->setHeight(m_headerItem->implicitHeight());
556 // We are going down (but it's not because of the rebound at the end)
557 // (but the header was not shown by it's own position)
558 // or the header is partially shown and we are not doing a maximizeVisibleArea either
559 const bool scrolledUp = m_previousContentY > contentY();
560 const bool notRebounding = qRound(contentY() + height()) < qRound(contentHeight());
561 const bool notShownByItsOwn = contentY() + diff >= m_headerItem->y() + m_headerItem->height();
562 const bool maximizeVisibleAreaRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationMaximizeVisibleArea;
564 if (!scrolledUp && (contentY() == -m_minYExtent || (m_headerItemShownHeight == 0 && m_previousContentY == m_headerItem->y()))) {
565 m_headerItemShownHeight = 0;
566 m_headerItem->setY(-m_minYExtent);
567 } else if ((scrolledUp && notRebounding && notShownByItsOwn && !maximizeVisibleAreaRunning) || (m_headerItemShownHeight > 0) || m_inContentHeightKeepHeaderShown) {
568 if (maximizeVisibleAreaRunning && diff > 0) {
569 // If we are maximizing and the header was shown, make sure we hide it
570 m_headerItemShownHeight -= diff;
571 } else {
572 m_headerItemShownHeight += diff;
573 }
574 if (uFuzzyCompare(contentY(), -m_minYExtent)) {
575 m_headerItemShownHeight = 0;
576 } else {
577 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
578 }
579 if (m_headerItemShownHeight > 0) {
580 if (uFuzzyCompare(m_headerItem->height(), m_headerItemShownHeight)) {
581 m_headerItem->setY(contentY());
582 m_headerItemShownHeight = m_headerItem->height();
583 } else {
584 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
585 }
586 } else {
587 m_headerItem->setY(-m_minYExtent);
588 }
589 }
590 Q_EMIT headerItemShownHeightChanged();
591 } else {
592 // Stick the header item to the top when dragging down
593 m_headerItem->setY(contentY());
594 m_headerItem->setHeight(m_headerItem->implicitHeight() + (-m_minYExtent - contentY()));
595 }
596 // We will be changing the clip item, need to accomadate for it
597 // otherwise we move the firstItem down/up twice (unless the
598 // show header animation is running, where we want to keep the viewport stable)
599 if (!showHeaderAnimationRunning) {
600 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
601 } else {
602 diff = -diff;
603 }
604 }
605 if (!m_visibleItems.isEmpty()) {
606 updateClipItem();
607 ListItem *firstItem = m_visibleItems.first();
608 firstItem->setY(firstItem->y() + diff);
609 if (showHeaderAnimationRunning) {
610 adjustMinYExtent();
611 }
612 }
615void ListViewWithPageHeader::createDelegateModel()
617 m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
618 connect(m_delegateModel, &QQmlDelegateModel::createdItem, this, &ListViewWithPageHeader::itemCreated);
619 if (isComponentComplete())
620 m_delegateModel->componentComplete();
621 updateWatchedRoles();
624void ListViewWithPageHeader::refill()
626 if (m_inLayout) {
627 return;
628 }
629 if (!isComponentComplete()) {
630 return;
631 }
633 const qreal from = contentY();
634 const qreal to = from + height();
635 const qreal bufferFrom = from - m_cacheBuffer;
636 const qreal bufferTo = to + m_cacheBuffer;
638 bool added = addVisibleItems(from, to, false);
639 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
640 added |= addVisibleItems(bufferFrom, bufferTo, true);
642 if (added || removed) {
643 m_contentHeightDirty = true;
644 }
647bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
649 if (!delegate())
650 return false;
652 if (m_delegateModel->count() == 0)
653 return false;
655 ListItem *item;
656// qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
658 int modelIndex = 0;
659 qreal pos = 0;
660 if (!m_visibleItems.isEmpty()) {
661 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
662 item = m_visibleItems.last();
663 pos = item->y() + item->height() + m_clipItem->y();
664 }
665 bool changed = false;
666// qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
667 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
668// qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
669 if (!(item = createItem(modelIndex, asynchronous)))
670 break;
671 pos += item->height();
672 ++modelIndex;
673 changed = true;
674 }
676 modelIndex = 0;
677 pos = 0;
678 if (!m_visibleItems.isEmpty()) {
679 modelIndex = m_firstVisibleIndex - 1;
680 item = m_visibleItems.first();
681 pos = item->y() + m_clipItem->y();
682 }
683 while (modelIndex >= 0 && pos > fillFrom) {
684// qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
685 if (!(item = createItem(modelIndex, asynchronous)))
686 break;
687 pos -= item->height();
688 --modelIndex;
689 changed = true;
690 }
692 return changed;
695void ListViewWithPageHeader::reallyReleaseItem(ListItem *listItem)
697 QQuickItem *item = listItem->m_item;
698 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
699 if (flags & QQmlDelegateModel::Destroyed) {
700 item->setParentItem(nullptr);
701 }
702 listItem->sectionItem()->deleteLater();
703 delete listItem;
706void ListViewWithPageHeader::releaseItem(ListItem *listItem)
708 QQuickItemPrivate::get(listItem->m_item)->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
709 if (listItem->sectionItem()) {
710 QQuickItemPrivate::get(listItem->sectionItem())->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
711 }
712 m_itemsToRelease << listItem;
715void ListViewWithPageHeader::updateWatchedRoles()
717 if (m_delegateModel) {
718 QList<QByteArray> roles;
719 if (!m_sectionProperty.isEmpty())
720 roles << m_sectionProperty.toUtf8();
721 m_delegateModel->setWatchedRoles(roles);
722 }
725QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
727 if (!m_sectionDelegate)
728 return nullptr;
730 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
732 if (modelIndex > 0) {
733 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
734 if (section == prevSection)
735 return nullptr;
736 }
737 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
738 // Already inserted items can't steal next section header
739 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
740 if (section == nextSection) {
741 // Steal the section header
742 ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
743 if (nextItem) {
744 QQuickItem *sectionItem = nextItem->sectionItem();
745 nextItem->setSectionItem(nullptr);
746 return sectionItem;
747 }
748 }
749 }
751 return getSectionItem(section);
754QQuickItem *ListViewWithPageHeader::getSectionItem(const QString &sectionText, bool watchGeometry)
756 QQuickItem *sectionItem = nullptr;
758 QQmlContext *creationContext = m_sectionDelegate->creationContext();
759 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
760 context->setContextProperty(QStringLiteral("section"), sectionText);
761 context->setContextProperty(QStringLiteral("delegateIndex"), -1);
762 QObject *nobj = m_sectionDelegate->beginCreate(context);
763 if (nobj) {
764 QQml_setParent_noEvent(context, nobj);
765 sectionItem = qobject_cast<QQuickItem *>(nobj);
766 if (!sectionItem) {
767 delete nobj;
768 } else {
769 sectionItem->setZ(2);
770 QQml_setParent_noEvent(sectionItem, m_clipItem);
771 sectionItem->setParentItem(m_clipItem);
772 }
773 } else {
774 delete context;
775 }
776 m_sectionDelegate->completeCreate();
778 if (watchGeometry) {
779 QQuickItemPrivate::get(sectionItem)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
780 }
782 return sectionItem;
785void ListViewWithPageHeader::updateSectionItem(int modelIndex)
787 ListItem *item = itemAtIndex(modelIndex);
788 if (item) {
789 const QString sectionText = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
791 bool needSectionHeader = true;
792 // if it is the same section as the previous item need to drop the section
793 if (modelIndex > 0) {
794 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
795 if (sectionText == prevSection) {
796 needSectionHeader = false;
797 }
798 }
800 if (needSectionHeader) {
801 if (!item->sectionItem()) {
802 item->setSectionItem(getSectionItem(sectionText));
803 } else {
804 QQmlContext *context = QQmlEngine::contextForObject(item->sectionItem())->parentContext();
805 context->setContextProperty(QStringLiteral("section"), sectionText);
806 }
807 } else {
808 if (item->sectionItem()) {
809 item->sectionItem()->deleteLater();
810 item->setSectionItem(nullptr);
811 }
812 }
813 }
816bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
818// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
819 // Do not remove items if we are overshooting up or down, since we'll come back
820 // to the "stable" position and delete/create items without any reason
821 if (contentY() < -m_minYExtent) {
822 return false;
823 } else if (contentY() + height() > contentHeight()) {
824 return false;
825 }
826 bool changed = false;
828 bool foundVisible = false;
829 int i = 0;
830 int removedItems = 0;
831 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
832 while (i < m_visibleItems.count()) {
833 ListItem *item = m_visibleItems[i];
834 const qreal pos = item->y() + m_clipItem->y();
835// qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
836 if (pos + item->height() < bufferFrom || pos > bufferTo) {
837// qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
838 releaseItem(item);
839 m_visibleItems.removeAt(i);
840 changed = true;
841 ++removedItems;
842 } else {
843 if (!foundVisible) {
844 foundVisible = true;
845 const int itemIndex = m_firstVisibleIndex + removedItems + i;
846 m_firstVisibleIndex = itemIndex;
847 }
848 ++i;
849 }
850 }
851 if (!foundVisible) {
852 initializeValuesForEmptyList();
853 }
854 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
855 adjustMinYExtent();
856 }
858 return changed;
861ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
863// qDebug() << "CREATE ITEM" << modelIndex;
864 if (asynchronous && m_asyncRequestedIndex != -1)
865 return nullptr;
867 m_asyncRequestedIndex = -1;
868 QObject* object = m_delegateModel->object(modelIndex, asynchronous);
869 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
870 if (!item) {
871 if (object) {
872 m_delegateModel->release(object);
873 if (!m_delegateValidated) {
874 m_delegateValidated = true;
875 QObject* delegateObj = delegate();
876 qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
877 }
878 } else {
879 m_asyncRequestedIndex = modelIndex;
880 }
881 return 0;
882 } else {
883// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
884 ListItem *listItem = new ListItem;
885 listItem->m_item = item;
886 listItem->setSectionItem(getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/));
887 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
888 ListItem *prevItem = itemAtIndex(modelIndex - 1);
889 bool lostItem = false; // Is an item that we requested async but because of model changes
890 // it is no longer attached to any of the existing items (has no prev nor next item)
891 // nor is the first item
892 if (prevItem) {
893 listItem->setY(prevItem->y() + prevItem->height());
894 } else {
895 ListItem *currItem = itemAtIndex(modelIndex);
896 if (currItem) {
897 // There's something already in m_visibleItems at out index, meaning this is an insert, so attach to its top
898 listItem->setY(currItem->y() - listItem->height());
899 } else {
900 ListItem *nextItem = itemAtIndex(modelIndex + 1);
901 if (nextItem) {
902 listItem->setY(nextItem->y() - listItem->height());
903 } else if (modelIndex == 0) {
904 listItem->setY(-m_clipItem->y() + (m_headerItem ? m_headerItem->height() : 0));
905 } else if (!m_visibleItems.isEmpty()) {
906 lostItem = true;
907 }
908 }
909 }
910 if (lostItem) {
911 listItem->setCulled(true);
912 releaseItem(listItem);
913 listItem = nullptr;
914 } else {
915 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() <= contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
916 if (m_visibleItems.isEmpty()) {
917 m_visibleItems << listItem;
918 } else {
919 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
920 }
921 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
922 m_firstVisibleIndex = modelIndex;
923 polish();
924 }
925 if (listItem->sectionItem()) {
926 QQmlContext *context = QQmlEngine::contextForObject(listItem->sectionItem())->parentContext();
927 context->setContextProperty(QStringLiteral("delegateIndex"), modelIndex);
928 }
929 adjustMinYExtent();
930 m_contentHeightDirty = true;
931 }
932 return listItem;
933 }
936void ListViewWithPageHeader::itemCreated(int modelIndex, QObject *object)
938 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
939 if (!item) {
940 qWarning() << "ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
941 return;
942 }
943// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
944 // Check we are not being taken down and don't paint anything
945 // TODO Check if we still need this in 5.2
946 // For reproduction just inifnite loop testDash or testDashContent
947 if (!QQmlEngine::contextForObject(this)->parentContext())
948 return;
950 item->setParentItem(m_clipItem);
951 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
952 context->setContextProperty(QStringLiteral("ListViewWithPageHeader"), this);
953 context->setContextProperty(QStringLiteral("heightToClip"), QVariant::fromValue<int>(0));
954 if (modelIndex == m_asyncRequestedIndex) {
955 createItem(modelIndex, false);
956 refill();
957 }
960void ListViewWithPageHeader::updateClipItem()
962 m_clipItem->setHeight(height() - m_headerItemShownHeight);
963 m_clipItem->setY(contentY() + m_headerItemShownHeight);
964 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
967void ListViewWithPageHeader::onContentHeightChanged()
969 updateClipItem();
972void ListViewWithPageHeader::onContentWidthChanged()
974 m_clipItem->setWidth(contentItem()->width());
977void ListViewWithPageHeader::onHeightChanged()
979 polish();
983void ListViewWithPageHeader::onModelUpdated(const QQmlChangeSet &changeSet, bool /*reset*/)
985 // TODO Do something with reset
986// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
987 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
989 Q_FOREACH(const QQmlChangeSet::Change remove, changeSet.removes()) {
990// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove.index << remove.count;
991 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
992 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
993 // If all the items we are removing are either not created or culled
994 // we have to grow down to avoid viewport changing
995 bool growDown = true;
996 for (int i = 0; growDown && i < remove.count; ++i) {
997 const int modelIndex = remove.index + i;
998 ListItem *item = itemAtIndex(modelIndex);
999 if (item && !item->culled()) {
1000 growDown = false;
1003 for (int i = remove.count - 1; i >= 0; --i) {
1004 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
1005 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
1006 ListItem *item = m_visibleItems[visibleIndex];
1007 // Pass the section item down if needed
1008 if (item->sectionItem() && visibleIndex + 1 < m_visibleItems.count()) {
1009 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
1010 if (!nextItem->sectionItem()) {
1011 nextItem->setSectionItem(item->sectionItem());
1012 item->setSectionItem(nullptr);
1015 releaseItem(item);
1016 m_visibleItems.removeAt(visibleIndex);
1019 if (growDown) {
1020 adjustMinYExtent();
1021 } else if (remove.index <= m_firstVisibleIndex && !m_visibleItems.isEmpty()) {
1022 m_visibleItems.first()->setY(oldFirstValidIndexPos);
1024 if (m_visibleItems.isEmpty()) {
1025 m_firstVisibleIndex = -1;
1026 } else {
1027 m_firstVisibleIndex -= qMax(0, m_firstVisibleIndex - remove.index);
1029 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
1030 m_firstVisibleIndex -= remove.count;
1032 for (int i = remove.count - 1; i >= 0; --i) {
1033 const int modelIndex = remove.index + i;
1034 if (modelIndex == m_asyncRequestedIndex) {
1035 m_asyncRequestedIndex = -1;
1036 } else if (modelIndex < m_asyncRequestedIndex) {
1037 m_asyncRequestedIndex--;
1042 Q_FOREACH(const QQmlChangeSet::Change insert, changeSet.inserts()) {
1043// qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
1044 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
1045 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
1046 if (insertingInValidIndexes || firstItemWithViewOnTop)
1048 // If the items we are adding won't be really visible
1049 // we grow up instead of down to not change the viewport
1050 bool growUp = false;
1051 if (!firstItemWithViewOnTop) {
1052 for (int i = 0; i < m_visibleItems.count(); ++i) {
1053 if (!m_visibleItems[i]->culled()) {
1054 if (insert.index <= m_firstVisibleIndex + i) {
1055 growUp = true;
1057 break;
1062 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
1063 for (int i = insert.count - 1; i >= 0; --i) {
1064 const int modelIndex = insert.index + i;
1065 ListItem *item = createItem(modelIndex, false);
1066 if (growUp) {
1067 ListItem *firstItem = m_visibleItems.first();
1068 firstItem->setY(firstItem->y() - item->height());
1070 // Adding an item may break a "same section" chain, so check
1071 // if we need adding a new section item
1072 if (m_sectionDelegate) {
1073 ListItem *nextItem = itemAtIndex(modelIndex + 1);
1074 if (nextItem && !nextItem->sectionItem()) {
1075 nextItem->setSectionItem(getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/));
1076 if (growUp && nextItem->sectionItem()) {
1077 ListItem *firstItem = m_visibleItems.first();
1078 firstItem->setY(firstItem->y() - nextItem->sectionItem()->height());
1083 if (firstItemWithViewOnTop) {
1084 ListItem *firstItem = m_visibleItems.first();
1085 firstItem->setY(oldFirstValidIndexPos);
1087 adjustMinYExtent();
1088 } else if (insert.index <= m_firstVisibleIndex) {
1089 m_firstVisibleIndex += insert.count;
1092 for (int i = insert.count - 1; i >= 0; --i) {
1093 const int modelIndex = insert.index + i;
1094 if (modelIndex <= m_asyncRequestedIndex) {
1095 m_asyncRequestedIndex++;
1100 Q_FOREACH(const QQmlChangeSet::Change change, changeSet.changes()) {
1101 for (int i = change.start(); i < change.end(); ++i) {
1102 updateSectionItem(i);
1104 // Also update the section header for the next item after the change since it may be influenced
1105 updateSectionItem(change.end());
1108 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
1109 if (m_visibleItems.isEmpty()) {
1110 initializeValuesForEmptyList();
1111 } else {
1112 adjustMinYExtent();
1116 for (int i = 0; i < m_visibleItems.count(); ++i) {
1117 ListItem *item = m_visibleItems[i];
1118 if (item->sectionItem()) {
1119 QQmlContext *context = QQmlEngine::contextForObject(item->sectionItem())->parentContext();
1120 context->setContextProperty(QStringLiteral("delegateIndex"), m_firstVisibleIndex + i);
1124 layout();
1125 polish();
1126 m_contentHeightDirty = true;
1129void ListViewWithPageHeader::contentYAnimationRunningChanged(bool running)
1131 setInteractive(!running);
1132 if (!running) {
1133 m_contentHeightDirty = true;
1134 polish();
1138void ListViewWithPageHeader::itemGeometryChanged(QQuickItem *item, const QRectF &newGeometry, const QRectF &oldGeometry)
1140 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
1141 if (heightDiff != 0) {
1142 if (!m_visibleItems.isEmpty()) {
1143 ListItem *firstItem = m_visibleItems.first();
1144 const auto prevFirstItemY = firstItem->y();
1145 if (!m_inContentHeightKeepHeaderShown && oldGeometry.y() + oldGeometry.height() + m_clipItem->y() <= contentY()) {
1146 firstItem->setY(firstItem->y() - heightDiff);
1147 } else if (item == firstItem->sectionItem()) {
1148 firstItem->setY(firstItem->y() + heightDiff);
1151 if (firstItem->y() != prevFirstItemY) {
1152 adjustMinYExtent();
1153 layout();
1156 refill();
1157 adjustMinYExtent();
1158 polish();
1159 m_contentHeightDirty = true;
1163void ListViewWithPageHeader::itemImplicitHeightChanged(QQuickItem *item)
1165 if (item == m_headerItem) {
1166 const qreal diff = m_headerItem->implicitHeight() - m_previousHeaderImplicitHeight;
1167 if (diff != 0) {
1168 adjustHeader(diff);
1169 m_previousHeaderImplicitHeight = m_headerItem->implicitHeight();
1170 layout();
1171 polish();
1172 m_contentHeightDirty = true;
1177void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
1179 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
1180 if (m_headerItemShownHeight > 0) {
1181 // If the header is shown because of the clip
1182 // Change its size
1183 m_headerItemShownHeight += heightDiff;
1184 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
1185 updateClipItem();
1186 adjustMinYExtent();
1187 Q_EMIT headerItemShownHeightChanged();
1188 } else {
1189 if (oldHeaderY + oldHeaderHeight > contentY()) {
1190 // If the header is shown because its position
1191 // Change its size
1192 ListItem *firstItem = m_visibleItems.first();
1193 firstItem->setY(firstItem->y() + heightDiff);
1194 layout();
1195 } else {
1196 // If the header is not on screen, just change the start of the list
1197 // so the viewport is not changed
1198 adjustMinYExtent();
1204void ListViewWithPageHeader::adjustMinYExtent()
1206 if (m_visibleItems.isEmpty() || (contentHeight() + m_minYExtent < height())) {
1207 m_minYExtent = 0;
1208 } else {
1209 qreal nonCreatedHeight = 0;
1210 if (m_firstVisibleIndex != 0) {
1211 // Calculate the average height of items to estimate the position of the list start
1212 const int visibleItems = m_visibleItems.count();
1213 qreal visibleItemsHeight = 0;
1214 Q_FOREACH(ListItem *item, m_visibleItems) {
1215 visibleItemsHeight += item->height();
1217 nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
1218// qDebug() << m_firstVisibleIndex << visibleItemsHeight << visibleItems << nonCreatedHeight;
1220 const qreal headerHeight = (m_headerItem ? m_headerItem->implicitHeight() : 0);
1221 m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + headerHeight;
1222 if (m_minYExtent != 0 && qFuzzyIsNull(m_minYExtent)) {
1223 m_minYExtent = 0;
1224 m_visibleItems.first()->setY(nonCreatedHeight - m_clipItem->y() + headerHeight);
1229ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
1231 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
1232 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
1233 return m_visibleItems[visibleIndexedModelIndex];
1235 return nullptr;
1238void ListViewWithPageHeader::layout()
1240 if (m_inLayout)
1241 return;
1243 m_inLayout = true;
1244 if (!m_visibleItems.isEmpty()) {
1245 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
1246 const qreal visibleTo = contentY() + height() - m_clipItem->y();
1248 qreal pos = m_visibleItems.first()->y();
1250// qDebug() << "ListViewWithPageHeader::layout Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
1251 int firstReallyVisibleItem = -1;
1252 int modelIndex = m_firstVisibleIndex;
1253 Q_FOREACH(ListItem *item, m_visibleItems) {
1254 const bool cull = pos + item->height() <= visibleFrom || pos >= visibleTo;
1255 item->setCulled(cull);
1256 item->setY(pos);
1257 if (!cull && firstReallyVisibleItem == -1) {
1258 firstReallyVisibleItem = modelIndex;
1259 if (m_topSectionItem) {
1260 // Positing the top section sticky item is a two step process
1261 // First we set it either we cull it (because it doesn't need to be sticked to the top)
1262 // or stick it to the top
1263 // Then after the loop we'll make sure that if there's another section just below it
1264 // pushed the sticky section up to make it disappear
1265 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
1266 bool showStickySectionItem;
1267 // We need to show the "top section sticky item" when the position at the "top" of the
1268 // viewport is bigger than the start of the position of the first visible item
1269 // i.e. the first visible item starts before the viewport, or when the first
1270 // visible item starts just at the viewport start and it does not have its own section item
1271 if (topSectionStickPos > pos) {
1272 showStickySectionItem = true;
1273 } else if (topSectionStickPos == pos) {
1274 showStickySectionItem = !item->sectionItem();
1275 } else {
1276 showStickySectionItem = false;
1278 if (!showStickySectionItem) {
1279 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
1280 if (item->sectionItem()) {
1281 // This seems it should happen since why would we cull the top section
1282 // if the first visible item has no section header? This only happens briefly
1283 // when increasing the height of a list that is at the bottom, the m_topSectionItem
1284 // gets shown shortly in the next polish call
1285 QQuickItemPrivate::get(item->sectionItem())->setCulled(false);
1287 } else {
1288 // Update the top sticky section header
1289 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1290 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
1291 context->setContextProperty(QStringLiteral("section"), section);
1293 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
1294 m_topSectionItem->setY(topSectionStickPos);
1295 int delegateIndex = modelIndex;
1296 // Look for the first index with this section text
1297 while (delegateIndex > 0) {
1298 const QString prevSection = m_delegateModel->stringValue(delegateIndex - 1, m_sectionProperty);
1299 if (prevSection != section)
1300 break;
1301 delegateIndex--;
1303 context->setContextProperty(QStringLiteral("delegateIndex"), delegateIndex);
1304 if (item->sectionItem()) {
1305 QQuickItemPrivate::get(item->sectionItem())->setCulled(true);
1310 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
1311 const qreal clipFrom = visibleFrom + (!item->sectionItem() && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
1312 if (!cull && pos < clipFrom) {
1313 context->setContextProperty(QStringLiteral("heightToClip"), clipFrom - pos);
1314 } else {
1315 context->setContextProperty(QStringLiteral("heightToClip"), QVariant::fromValue<int>(0));
1317// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
1318 pos += item->height();
1319 ++modelIndex;
1322 // Second step of section sticky item positioning
1323 // Look at the next section header, check if it's pushing up the sticky one
1324 if (m_topSectionItem) {
1325 if (firstReallyVisibleItem >= 0) {
1326 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
1327 ListItem *item = m_visibleItems[i];
1328 if (item->sectionItem()) {
1329 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
1330 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
1332 break;
1338 m_inLayout = false;
1341void ListViewWithPageHeader::updatePolish()
1343 // Check we are not being taken down and don't paint anything
1344 // TODO Check if we still need this in 5.2
1345 // For reproduction just inifnite loop testDash or testDashContent
1346 if (!QQmlEngine::contextForObject(this)->parentContext())
1347 return;
1349 Q_FOREACH(ListItem *item, m_itemsToRelease)
1350 reallyReleaseItem(item);
1351 m_itemsToRelease.clear();
1353 if (!model())
1354 return;
1356 layout();
1358 refill();
1360 if (m_contentHeightDirty) {
1361 qreal contentHeight;
1362 if (m_visibleItems.isEmpty()) {
1363 contentHeight = m_headerItem ? m_headerItem->height() : 0;
1364 } else {
1365 const int modelCount = model()->rowCount();
1366 const int visibleItems = m_visibleItems.count();
1367 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
1368 qreal nonCreatedHeight = 0;
1369 if (lastValidIndex != modelCount - 1) {
1370 const int visibleItems = m_visibleItems.count();
1371 qreal visibleItemsHeight = 0;
1372 Q_FOREACH(ListItem *item, m_visibleItems) {
1373 visibleItemsHeight += item->height();
1375 const int unknownSizes = modelCount - (m_firstVisibleIndex + visibleItems);
1376 nonCreatedHeight = unknownSizes * visibleItemsHeight / visibleItems;
1378 ListItem *item = m_visibleItems.last();
1379 contentHeight = nonCreatedHeight + item->y() + item->height() + m_clipItem->y();
1380 if (m_firstVisibleIndex != 0) {
1381 // Make sure that if we are shrinking we tell the view we still fit
1382 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
1386 m_contentHeightDirty = false;
1387 adjustMinYExtent();
1388 if (contentHeight + m_minYExtent < height()) {
1389 // need this since in the previous call to adjustMinYExtent contentHeight is not set yet
1390 m_minYExtent = 0;
1392 m_inContentHeightKeepHeaderShown = m_headerItem && m_headerItem->y() == contentY();
1393 setContentHeight(contentHeight);
1394 m_inContentHeightKeepHeaderShown = false;
1398#include "moc_listviewwithpageheader.cpp"