on e804c19 help sign in
unity8/plugins/Dash/listviewwithpageheader.cpp
1386 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());
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 *itemPrivate = QQuickItemPrivate::get(listItem->m_item);
709 itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
710 m_itemsToRelease << listItem;
713void ListViewWithPageHeader::updateWatchedRoles()
715 if (m_delegateModel) {
716 QList<QByteArray> roles;
717 if (!m_sectionProperty.isEmpty())
718 roles << m_sectionProperty.toUtf8();
719 m_delegateModel->setWatchedRoles(roles);
720 }
723QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
725 if (!m_sectionDelegate)
726 return nullptr;
728 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
730 if (modelIndex > 0) {
731 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
732 if (section == prevSection)
733 return nullptr;
734 }
735 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
736 // Already inserted items can't steal next section header
737 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
738 if (section == nextSection) {
739 // Steal the section header
740 ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
741 if (nextItem) {
742 QQuickItem *sectionItem = nextItem->sectionItem();
743 nextItem->setSectionItem(nullptr);
744 return sectionItem;
745 }
746 }
747 }
749 return getSectionItem(section);
752QQuickItem *ListViewWithPageHeader::getSectionItem(const QString &sectionText)
754 QQuickItem *sectionItem = nullptr;
756 QQmlContext *creationContext = m_sectionDelegate->creationContext();
757 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
758 context->setContextProperty(QStringLiteral("section"), sectionText);
759 context->setContextProperty(QStringLiteral("delegateIndex"), -1);
760 QObject *nobj = m_sectionDelegate->beginCreate(context);
761 if (nobj) {
762 QQml_setParent_noEvent(context, nobj);
763 sectionItem = qobject_cast<QQuickItem *>(nobj);
764 if (!sectionItem) {
765 delete nobj;
766 } else {
767 sectionItem->setZ(2);
768 QQml_setParent_noEvent(sectionItem, m_clipItem);
769 sectionItem->setParentItem(m_clipItem);
770 }
771 } else {
772 delete context;
773 }
774 m_sectionDelegate->completeCreate();
776 // TODO attach to sectionItem so we can accomodate to it changing its height
778 return sectionItem;
781void ListViewWithPageHeader::updateSectionItem(int modelIndex)
783 ListItem *item = itemAtIndex(modelIndex);
784 if (item) {
785 const QString sectionText = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
787 bool needSectionHeader = true;
788 // if it is the same section as the previous item need to drop the section
789 if (modelIndex > 0) {
790 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
791 if (sectionText == prevSection) {
792 needSectionHeader = false;
793 }
794 }
796 if (needSectionHeader) {
797 if (!item->sectionItem()) {
798 item->setSectionItem(getSectionItem(sectionText));
799 } else {
800 QQmlContext *context = QQmlEngine::contextForObject(item->sectionItem())->parentContext();
801 context->setContextProperty(QStringLiteral("section"), sectionText);
802 }
803 } else {
804 if (item->sectionItem()) {
805 item->sectionItem()->deleteLater();
806 item->setSectionItem(nullptr);
807 }
808 }
809 }
812bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
814// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
815 // Do not remove items if we are overshooting up or down, since we'll come back
816 // to the "stable" position and delete/create items without any reason
817 if (contentY() < -m_minYExtent) {
818 return false;
819 } else if (contentY() + height() > contentHeight()) {
820 return false;
821 }
822 bool changed = false;
824 bool foundVisible = false;
825 int i = 0;
826 int removedItems = 0;
827 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
828 while (i < m_visibleItems.count()) {
829 ListItem *item = m_visibleItems[i];
830 const qreal pos = item->y() + m_clipItem->y();
831// qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
832 if (pos + item->height() < bufferFrom || pos > bufferTo) {
833// qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
834 releaseItem(item);
835 m_visibleItems.removeAt(i);
836 changed = true;
837 ++removedItems;
838 } else {
839 if (!foundVisible) {
840 foundVisible = true;
841 const int itemIndex = m_firstVisibleIndex + removedItems + i;
842 m_firstVisibleIndex = itemIndex;
843 }
844 ++i;
845 }
846 }
847 if (!foundVisible) {
848 initializeValuesForEmptyList();
849 }
850 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
851 adjustMinYExtent();
852 }
854 return changed;
857ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
859// qDebug() << "CREATE ITEM" << modelIndex;
860 if (asynchronous && m_asyncRequestedIndex != -1)
861 return nullptr;
863 m_asyncRequestedIndex = -1;
864 QObject* object = m_delegateModel->object(modelIndex, asynchronous);
865 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
866 if (!item) {
867 if (object) {
868 m_delegateModel->release(object);
869 if (!m_delegateValidated) {
870 m_delegateValidated = true;
871 QObject* delegateObj = delegate();
872 qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
873 }
874 } else {
875 m_asyncRequestedIndex = modelIndex;
876 }
877 return 0;
878 } else {
879// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
880 ListItem *listItem = new ListItem;
881 listItem->m_item = item;
882 listItem->setSectionItem(getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/));
883 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
884 ListItem *prevItem = itemAtIndex(modelIndex - 1);
885 bool lostItem = false; // Is an item that we requested async but because of model changes
886 // it is no longer attached to any of the existing items (has no prev nor next item)
887 // nor is the first item
888 if (prevItem) {
889 listItem->setY(prevItem->y() + prevItem->height());
890 } else {
891 ListItem *currItem = itemAtIndex(modelIndex);
892 if (currItem) {
893 // There's something already in m_visibleItems at out index, meaning this is an insert, so attach to its top
894 listItem->setY(currItem->y() - listItem->height());
895 } else {
896 ListItem *nextItem = itemAtIndex(modelIndex + 1);
897 if (nextItem) {
898 listItem->setY(nextItem->y() - listItem->height());
899 } else if (modelIndex == 0) {
900 listItem->setY(-m_clipItem->y() + (m_headerItem ? m_headerItem->height() : 0));
901 } else if (!m_visibleItems.isEmpty()) {
902 lostItem = true;
903 }
904 }
905 }
906 if (lostItem) {
907 listItem->setCulled(true);
908 releaseItem(listItem);
909 listItem = nullptr;
910 } else {
911 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() <= contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
912 if (m_visibleItems.isEmpty()) {
913 m_visibleItems << listItem;
914 } else {
915 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
916 }
917 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
918 m_firstVisibleIndex = modelIndex;
919 polish();
920 }
921 if (listItem->sectionItem()) {
922 QQmlContext *context = QQmlEngine::contextForObject(listItem->sectionItem())->parentContext();
923 context->setContextProperty(QStringLiteral("delegateIndex"), modelIndex);
924 }
925 adjustMinYExtent();
926 m_contentHeightDirty = true;
927 }
928 return listItem;
929 }
932void ListViewWithPageHeader::itemCreated(int modelIndex, QObject *object)
934 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
935 if (!item) {
936 qWarning() << "ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
937 return;
938 }
939// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
940 // Check we are not being taken down and don't paint anything
941 // TODO Check if we still need this in 5.2
942 // For reproduction just inifnite loop testDash or testDashContent
943 if (!QQmlEngine::contextForObject(this)->parentContext())
944 return;
946 item->setParentItem(m_clipItem);
947 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
948 context->setContextProperty(QStringLiteral("ListViewWithPageHeader"), this);
949 context->setContextProperty(QStringLiteral("heightToClip"), QVariant::fromValue<int>(0));
950 if (modelIndex == m_asyncRequestedIndex) {
951 createItem(modelIndex, false);
952 refill();
953 }
956void ListViewWithPageHeader::updateClipItem()
958 m_clipItem->setHeight(height() - m_headerItemShownHeight);
959 m_clipItem->setY(contentY() + m_headerItemShownHeight);
960 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
963void ListViewWithPageHeader::onContentHeightChanged()
965 updateClipItem();
968void ListViewWithPageHeader::onContentWidthChanged()
970 m_clipItem->setWidth(contentItem()->width());
973void ListViewWithPageHeader::onHeightChanged()
975 polish();
979void ListViewWithPageHeader::onModelUpdated(const QQmlChangeSet &changeSet, bool /*reset*/)
981 // TODO Do something with reset
982// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
983 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
985 Q_FOREACH(const QQmlChangeSet::Change remove, changeSet.removes()) {
986// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove.index << remove.count;
987 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
988 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
989 // If all the items we are removing are either not created or culled
990 // we have to grow down to avoid viewport changing
991 bool growDown = true;
992 for (int i = 0; growDown && i < remove.count; ++i) {
993 const int modelIndex = remove.index + i;
994 ListItem *item = itemAtIndex(modelIndex);
995 if (item && !item->culled()) {
996 growDown = false;
997 }
998 }
999 for (int i = remove.count - 1; i >= 0; --i) {
1000 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
1001 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
1002 ListItem *item = m_visibleItems[visibleIndex];
1003 // Pass the section item down if needed
1004 if (item->sectionItem() && visibleIndex + 1 < m_visibleItems.count()) {
1005 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
1006 if (!nextItem->sectionItem()) {
1007 nextItem->setSectionItem(item->sectionItem());
1008 item->setSectionItem(nullptr);
1011 releaseItem(item);
1012 m_visibleItems.removeAt(visibleIndex);
1015 if (growDown) {
1016 adjustMinYExtent();
1017 } else if (remove.index <= m_firstVisibleIndex && !m_visibleItems.isEmpty()) {
1018 m_visibleItems.first()->setY(oldFirstValidIndexPos);
1020 if (m_visibleItems.isEmpty()) {
1021 m_firstVisibleIndex = -1;
1022 } else {
1023 m_firstVisibleIndex -= qMax(0, m_firstVisibleIndex - remove.index);
1025 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
1026 m_firstVisibleIndex -= remove.count;
1028 for (int i = remove.count - 1; i >= 0; --i) {
1029 const int modelIndex = remove.index + i;
1030 if (modelIndex == m_asyncRequestedIndex) {
1031 m_asyncRequestedIndex = -1;
1032 } else if (modelIndex < m_asyncRequestedIndex) {
1033 m_asyncRequestedIndex--;
1038 Q_FOREACH(const QQmlChangeSet::Change insert, changeSet.inserts()) {
1039// qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
1040 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
1041 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
1042 if (insertingInValidIndexes || firstItemWithViewOnTop)
1044 // If the items we are adding won't be really visible
1045 // we grow up instead of down to not change the viewport
1046 bool growUp = false;
1047 if (!firstItemWithViewOnTop) {
1048 for (int i = 0; i < m_visibleItems.count(); ++i) {
1049 if (!m_visibleItems[i]->culled()) {
1050 if (insert.index <= m_firstVisibleIndex + i) {
1051 growUp = true;
1053 break;
1058 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
1059 for (int i = insert.count - 1; i >= 0; --i) {
1060 const int modelIndex = insert.index + i;
1061 ListItem *item = createItem(modelIndex, false);
1062 if (growUp) {
1063 ListItem *firstItem = m_visibleItems.first();
1064 firstItem->setY(firstItem->y() - item->height());
1066 // Adding an item may break a "same section" chain, so check
1067 // if we need adding a new section item
1068 if (m_sectionDelegate) {
1069 ListItem *nextItem = itemAtIndex(modelIndex + 1);
1070 if (nextItem && !nextItem->sectionItem()) {
1071 nextItem->setSectionItem(getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/));
1072 if (growUp && nextItem->sectionItem()) {
1073 ListItem *firstItem = m_visibleItems.first();
1074 firstItem->setY(firstItem->y() - nextItem->sectionItem()->height());
1079 if (firstItemWithViewOnTop) {
1080 ListItem *firstItem = m_visibleItems.first();
1081 firstItem->setY(oldFirstValidIndexPos);
1083 adjustMinYExtent();
1084 } else if (insert.index <= m_firstVisibleIndex) {
1085 m_firstVisibleIndex += insert.count;
1088 for (int i = insert.count - 1; i >= 0; --i) {
1089 const int modelIndex = insert.index + i;
1090 if (modelIndex <= m_asyncRequestedIndex) {
1091 m_asyncRequestedIndex++;
1096 Q_FOREACH(const QQmlChangeSet::Change change, changeSet.changes()) {
1097 for (int i = change.start(); i < change.end(); ++i) {
1098 updateSectionItem(i);
1100 // Also update the section header for the next item after the change since it may be influenced
1101 updateSectionItem(change.end());
1104 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
1105 if (m_visibleItems.isEmpty()) {
1106 initializeValuesForEmptyList();
1107 } else {
1108 adjustMinYExtent();
1112 for (int i = 0; i < m_visibleItems.count(); ++i) {
1113 ListItem *item = m_visibleItems[i];
1114 if (item->sectionItem()) {
1115 QQmlContext *context = QQmlEngine::contextForObject(item->sectionItem())->parentContext();
1116 context->setContextProperty(QStringLiteral("delegateIndex"), m_firstVisibleIndex + i);
1120 layout();
1121 polish();
1122 m_contentHeightDirty = true;
1125void ListViewWithPageHeader::contentYAnimationRunningChanged(bool running)
1127 setInteractive(!running);
1128 if (!running) {
1129 m_contentHeightDirty = true;
1130 polish();
1134void ListViewWithPageHeader::itemGeometryChanged(QQuickItem * /*item*/, const QRectF &newGeometry, const QRectF &oldGeometry)
1136 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
1137 if (heightDiff != 0) {
1138 if (!m_inContentHeightKeepHeaderShown && oldGeometry.y() + oldGeometry.height() + m_clipItem->y() <= contentY() && !m_visibleItems.isEmpty()) {
1139 ListItem *firstItem = m_visibleItems.first();
1140 firstItem->setY(firstItem->y() - heightDiff);
1141 adjustMinYExtent();
1142 layout();
1144 refill();
1145 adjustMinYExtent();
1146 polish();
1147 m_contentHeightDirty = true;
1151void ListViewWithPageHeader::itemImplicitHeightChanged(QQuickItem *item)
1153 if (item == m_headerItem) {
1154 const qreal diff = m_headerItem->implicitHeight() - m_previousHeaderImplicitHeight;
1155 if (diff != 0) {
1156 adjustHeader(diff);
1157 m_previousHeaderImplicitHeight = m_headerItem->implicitHeight();
1158 layout();
1159 polish();
1160 m_contentHeightDirty = true;
1165void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
1167 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
1168 if (m_headerItemShownHeight > 0) {
1169 // If the header is shown because of the clip
1170 // Change its size
1171 m_headerItemShownHeight += heightDiff;
1172 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
1173 updateClipItem();
1174 adjustMinYExtent();
1175 Q_EMIT headerItemShownHeightChanged();
1176 } else {
1177 if (oldHeaderY + oldHeaderHeight > contentY()) {
1178 // If the header is shown because its position
1179 // Change its size
1180 ListItem *firstItem = m_visibleItems.first();
1181 firstItem->setY(firstItem->y() + heightDiff);
1182 layout();
1183 } else {
1184 // If the header is not on screen, just change the start of the list
1185 // so the viewport is not changed
1186 adjustMinYExtent();
1192void ListViewWithPageHeader::adjustMinYExtent()
1194 if (m_visibleItems.isEmpty() || (contentHeight() + m_minYExtent < height())) {
1195 m_minYExtent = 0;
1196 } else {
1197 qreal nonCreatedHeight = 0;
1198 if (m_firstVisibleIndex != 0) {
1199 // Calculate the average height of items to estimate the position of the list start
1200 const int visibleItems = m_visibleItems.count();
1201 qreal visibleItemsHeight = 0;
1202 Q_FOREACH(ListItem *item, m_visibleItems) {
1203 visibleItemsHeight += item->height();
1205 nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
1206// qDebug() << m_firstVisibleIndex << visibleItemsHeight << visibleItems << nonCreatedHeight;
1208 const qreal headerHeight = (m_headerItem ? m_headerItem->implicitHeight() : 0);
1209 m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + headerHeight;
1210 if (m_minYExtent != 0 && qFuzzyIsNull(m_minYExtent)) {
1211 m_minYExtent = 0;
1212 m_visibleItems.first()->setY(nonCreatedHeight - m_clipItem->y() + headerHeight);
1217ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
1219 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
1220 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
1221 return m_visibleItems[visibleIndexedModelIndex];
1223 return nullptr;
1226void ListViewWithPageHeader::layout()
1228 if (m_inLayout)
1229 return;
1231 m_inLayout = true;
1232 if (!m_visibleItems.isEmpty()) {
1233 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
1234 const qreal visibleTo = contentY() + height() - m_clipItem->y();
1236 qreal pos = m_visibleItems.first()->y();
1238// qDebug() << "ListViewWithPageHeader::layout Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
1239 int firstReallyVisibleItem = -1;
1240 int modelIndex = m_firstVisibleIndex;
1241 Q_FOREACH(ListItem *item, m_visibleItems) {
1242 const bool cull = pos + item->height() <= visibleFrom || pos >= visibleTo;
1243 item->setCulled(cull);
1244 item->setY(pos);
1245 if (!cull && firstReallyVisibleItem == -1) {
1246 firstReallyVisibleItem = modelIndex;
1247 if (m_topSectionItem) {
1248 // Positing the top section sticky item is a two step process
1249 // First we set it either we cull it (because it doesn't need to be sticked to the top)
1250 // or stick it to the top
1251 // Then after the loop we'll make sure that if there's another section just below it
1252 // pushed the sticky section up to make it disappear
1253 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
1254 bool showStickySectionItem;
1255 // We need to show the "top section sticky item" when the position at the "top" of the
1256 // viewport is bigger than the start of the position of the first visible item
1257 // i.e. the first visible item starts before the viewport, or when the first
1258 // visible item starts just at the viewport start and it does not have its own section item
1259 if (topSectionStickPos > pos) {
1260 showStickySectionItem = true;
1261 } else if (topSectionStickPos == pos) {
1262 showStickySectionItem = !item->sectionItem();
1263 } else {
1264 showStickySectionItem = false;
1266 if (!showStickySectionItem) {
1267 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
1268 if (item->sectionItem()) {
1269 // This seems it should happen since why would we cull the top section
1270 // if the first visible item has no section header? This only happens briefly
1271 // when increasing the height of a list that is at the bottom, the m_topSectionItem
1272 // gets shown shortly in the next polish call
1273 QQuickItemPrivate::get(item->sectionItem())->setCulled(false);
1275 } else {
1276 // Update the top sticky section header
1277 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1278 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
1279 context->setContextProperty(QStringLiteral("section"), section);
1281 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
1282 m_topSectionItem->setY(topSectionStickPos);
1283 int delegateIndex = modelIndex;
1284 // Look for the first index with this section text
1285 while (delegateIndex > 0) {
1286 const QString prevSection = m_delegateModel->stringValue(delegateIndex - 1, m_sectionProperty);
1287 if (prevSection != section)
1288 break;
1289 delegateIndex--;
1291 context->setContextProperty(QStringLiteral("delegateIndex"), delegateIndex);
1292 if (item->sectionItem()) {
1293 QQuickItemPrivate::get(item->sectionItem())->setCulled(true);
1298 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
1299 const qreal clipFrom = visibleFrom + (!item->sectionItem() && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
1300 if (!cull && pos < clipFrom) {
1301 context->setContextProperty(QStringLiteral("heightToClip"), clipFrom - pos);
1302 } else {
1303 context->setContextProperty(QStringLiteral("heightToClip"), QVariant::fromValue<int>(0));
1305// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
1306 pos += item->height();
1307 ++modelIndex;
1310 // Second step of section sticky item positioning
1311 // Look at the next section header, check if it's pushing up the sticky one
1312 if (m_topSectionItem) {
1313 if (firstReallyVisibleItem >= 0) {
1314 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
1315 ListItem *item = m_visibleItems[i];
1316 if (item->sectionItem()) {
1317 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
1318 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
1320 break;
1326 m_inLayout = false;
1329void ListViewWithPageHeader::updatePolish()
1331 // Check we are not being taken down and don't paint anything
1332 // TODO Check if we still need this in 5.2
1333 // For reproduction just inifnite loop testDash or testDashContent
1334 if (!QQmlEngine::contextForObject(this)->parentContext())
1335 return;
1337 Q_FOREACH(ListItem *item, m_itemsToRelease)
1338 reallyReleaseItem(item);
1339 m_itemsToRelease.clear();
1341 if (!model())
1342 return;
1344 layout();
1346 refill();
1348 if (m_contentHeightDirty) {
1349 qreal contentHeight;
1350 if (m_visibleItems.isEmpty()) {
1351 contentHeight = m_headerItem ? m_headerItem->height() : 0;
1352 } else {
1353 const int modelCount = model()->rowCount();
1354 const int visibleItems = m_visibleItems.count();
1355 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
1356 qreal nonCreatedHeight = 0;
1357 if (lastValidIndex != modelCount - 1) {
1358 const int visibleItems = m_visibleItems.count();
1359 qreal visibleItemsHeight = 0;
1360 Q_FOREACH(ListItem *item, m_visibleItems) {
1361 visibleItemsHeight += item->height();
1363 const int unknownSizes = modelCount - (m_firstVisibleIndex + visibleItems);
1364 nonCreatedHeight = unknownSizes * visibleItemsHeight / visibleItems;
1366 ListItem *item = m_visibleItems.last();
1367 contentHeight = nonCreatedHeight + item->y() + item->height() + m_clipItem->y();
1368 if (m_firstVisibleIndex != 0) {
1369 // Make sure that if we are shrinking we tell the view we still fit
1370 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
1374 m_contentHeightDirty = false;
1375 adjustMinYExtent();
1376 if (contentHeight + m_minYExtent < height()) {
1377 // need this since in the previous call to adjustMinYExtent contentHeight is not set yet
1378 m_minYExtent = 0;
1380 m_inContentHeightKeepHeaderShown = m_headerItem && m_headerItem->y() == contentY();
1381 setContentHeight(contentHeight);
1382 m_inContentHeightKeepHeaderShown = false;
1386#include "moc_listviewwithpageheader.cpp"