on 82a456d help sign in
unity8/plugins/Dash/listviewwithpageheader.cpp
1390 lines 54 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/qqmlcontext_p.h>
101#include <private/qqmldelegatemodel_p.h>
102#include <private/qqmlglobal_p.h>
103#include <private/qquickitem_p.h>
104#include <private/qquickanimation_p.h>
105#pragma GCC diagnostic pop
106// #include <private/qquickrectangle_p.h>
108qreal ListViewWithPageHeader::ListItem::height() const
110 return m_item->height() + (m_sectionItem ? m_sectionItem->height() : 0);
113qreal ListViewWithPageHeader::ListItem::y() const
115 return m_item->y() - (m_sectionItem ? m_sectionItem->height() : 0);
118void ListViewWithPageHeader::ListItem::setY(qreal newY)
120 if (m_sectionItem) {
121 m_sectionItem->setY(newY);
122 m_item->setY(newY + m_sectionItem->height());
123 } else {
124 m_item->setY(newY);
125 }
128bool ListViewWithPageHeader::ListItem::culled() const
130 return QQuickItemPrivate::get(m_item)->culled;
133void ListViewWithPageHeader::ListItem::setCulled(bool culled)
135 QQuickItemPrivate::get(m_item)->setCulled(culled);
136 if (m_sectionItem)
137 QQuickItemPrivate::get(m_sectionItem)->setCulled(culled);
140void ListViewWithPageHeader::ListItem::setSectionItem(QQuickItem *sectionItem)
142 m_sectionItem = sectionItem;
145ListViewWithPageHeader::ListViewWithPageHeader()
146 : m_delegateModel(nullptr)
147 , m_asyncRequestedIndex(-1)
148 , m_delegateValidated(false)
149 , m_firstVisibleIndex(-1)
150 , m_minYExtent(0)
151 , m_contentHeightDirty(false)
152 , m_headerItem(nullptr)
153 , m_previousContentY(0)
154 , m_headerItemShownHeight(0)
155 , m_sectionDelegate(nullptr)
156 , m_topSectionItem(nullptr)
157 , m_forceNoClip(false)
158 , m_inLayout(false)
159 , m_inContentHeightKeepHeaderShown(false)
160 , m_cacheBuffer(0)
162 m_clipItem = new QQuickItem(contentItem());
163// m_clipItem = new QQuickRectangle(contentItem());
164// ((QQuickRectangle*)m_clipItem)->setColor(Qt::gray);
166 m_contentYAnimation = new QQuickNumberAnimation(this);
167 m_contentYAnimation->setEasing(QEasingCurve::OutQuad);
168 m_contentYAnimation->setProperty(QStringLiteral("contentY"));
169 m_contentYAnimation->setDuration(200);
170 m_contentYAnimation->setTargetObject(this);
172 connect(this, &ListViewWithPageHeader::contentWidthChanged, this, &ListViewWithPageHeader::onContentWidthChanged);
173 connect(this, &ListViewWithPageHeader::contentHeightChanged, this, &ListViewWithPageHeader::onContentHeightChanged);
174 connect(this, &ListViewWithPageHeader::heightChanged, this, &ListViewWithPageHeader::onHeightChanged);
175 connect(m_contentYAnimation, &QQuickNumberAnimation::runningChanged, this, &ListViewWithPageHeader::contentYAnimationRunningChanged);
177 setFlickableDirection(VerticalFlick);
180ListViewWithPageHeader::~ListViewWithPageHeader()
184QAbstractItemModel *ListViewWithPageHeader::model() const
186 return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
189void ListViewWithPageHeader::setModel(QAbstractItemModel *model)
191 if (model != this->model()) {
192 if (!m_delegateModel) {
193 createDelegateModel();
194 } else {
195 disconnect(m_delegateModel, &QQmlDelegateModel::modelUpdated, this, &ListViewWithPageHeader::onModelUpdated);
196 }
197 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
198 connect(m_delegateModel, &QQmlDelegateModel::modelUpdated, this, &ListViewWithPageHeader::onModelUpdated);
199 Q_EMIT modelChanged();
200 polish();
201 // TODO?
202// Q_EMIT contentHeightChanged();
203// Q_EMIT contentYChanged();
204 }
207QQmlComponent *ListViewWithPageHeader::delegate() const
209 return m_delegateModel ? m_delegateModel->delegate() : nullptr;
212void ListViewWithPageHeader::setDelegate(QQmlComponent *delegate)
214 if (delegate != this->delegate()) {
215 if (!m_delegateModel) {
216 createDelegateModel();
217 }
219 // Cleanup the existing items
220 Q_FOREACH(ListItem *item, m_visibleItems)
221 releaseItem(item);
222 m_visibleItems.clear();
223 initializeValuesForEmptyList();
225 m_delegateModel->setDelegate(delegate);
227 Q_EMIT delegateChanged();
228 m_delegateValidated = false;
229 m_contentHeightDirty = true;
230 polish();
231 }
234void ListViewWithPageHeader::initializeValuesForEmptyList()
236 m_firstVisibleIndex = -1;
237 adjustMinYExtent();
238 setContentY(0);
239 m_clipItem->setY(0);
240 if (m_topSectionItem) {
241 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
242 }
245QQuickItem *ListViewWithPageHeader::header() const
247 return m_headerItem;
250void ListViewWithPageHeader::setHeader(QQuickItem *headerItem)
252 if (m_headerItem != headerItem) {
253 qreal oldHeaderHeight = 0;
254 qreal oldHeaderY = 0;
255 if (m_headerItem) {
256 oldHeaderHeight = m_headerItem->height();
257 oldHeaderY = m_headerItem->y();
258 m_headerItem->setParentItem(nullptr);
259 }
260 m_headerItem = headerItem;
261 if (m_headerItem) {
262 m_headerItem->setParentItem(contentItem());
263 m_headerItem->setZ(1);
264 m_previousHeaderImplicitHeight = m_headerItem->implicitHeight();
265 QQuickItemPrivate::get(m_headerItem)->addItemChangeListener(this, QQuickItemPrivate::ImplicitHeight);
266 }
267 qreal newHeaderHeight = m_headerItem ? m_headerItem->height() : 0;
268 if (!m_visibleItems.isEmpty() && newHeaderHeight != oldHeaderHeight) {
269 headerHeightChanged(newHeaderHeight, oldHeaderHeight, oldHeaderY);
270 polish();
271 m_contentHeightDirty = true;
272 }
273 Q_EMIT headerChanged();
274 }
277QQmlComponent *ListViewWithPageHeader::sectionDelegate() const
279 return m_sectionDelegate;
282void ListViewWithPageHeader::setSectionDelegate(QQmlComponent *delegate)
284 if (delegate != m_sectionDelegate) {
285 // TODO clean existing sections
287 m_sectionDelegate = delegate;
289 m_topSectionItem = getSectionItem(QString(), false /*watchGeometry*/);
290 m_topSectionItem->setZ(3);
291 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
292 connect(m_topSectionItem, &QQuickItem::heightChanged, this, &ListViewWithPageHeader::stickyHeaderHeightChanged);
294 // TODO create sections for existing items
296 Q_EMIT sectionDelegateChanged();
297 Q_EMIT stickyHeaderHeightChanged();
298 }
301QString ListViewWithPageHeader::sectionProperty() const
303 return m_sectionProperty;
306void ListViewWithPageHeader::setSectionProperty(const QString &property)
308 if (property != m_sectionProperty) {
309 m_sectionProperty = property;
311 updateWatchedRoles();
313 // TODO recreate sections
315 Q_EMIT sectionPropertyChanged();
316 }
319bool ListViewWithPageHeader::forceNoClip() const
321 return m_forceNoClip;
324void ListViewWithPageHeader::setForceNoClip(bool noClip)
326 if (noClip != m_forceNoClip) {
327 m_forceNoClip = noClip;
328 updateClipItem();
329 Q_EMIT forceNoClipChanged();
330 }
333int ListViewWithPageHeader::stickyHeaderHeight() const
335 return m_topSectionItem ? m_topSectionItem->height() : 0;
338qreal ListViewWithPageHeader::headerItemShownHeight() const
340 return m_headerItemShownHeight;
343int ListViewWithPageHeader::cacheBuffer() const
345 return m_cacheBuffer;
348void ListViewWithPageHeader::setCacheBuffer(int cacheBuffer)
350 if (cacheBuffer < 0) {
351 qmlInfo(this) << "Cannot set a negative cache buffer";
352 return;
353 }
355 if (cacheBuffer != m_cacheBuffer) {
356 m_cacheBuffer = cacheBuffer;
357 Q_EMIT cacheBufferChanged();
358 polish();
359 }
362void ListViewWithPageHeader::positionAtBeginning()
364 if (m_delegateModel->count() <= 0)
365 return;
367 qreal headerHeight = (m_headerItem ? m_headerItem->height() : 0);
368 if (m_firstVisibleIndex != 0) {
369 // TODO This could be optimized by trying to reuse the interesection
370 // of items that may end up intersecting between the existing
371 // m_visibleItems and the items we are creating in the next loop
372 Q_FOREACH(ListItem *item, m_visibleItems)
373 releaseItem(item);
374 m_visibleItems.clear();
375 m_firstVisibleIndex = -1;
377 // Create the item 0, it will be already correctly positioned at createItem()
378 m_clipItem->setY(0);
379 ListItem *item = createItem(0, false);
380 // Create the subsequent items
381 int modelIndex = 1;
382 qreal pos = item->y() + item->height();
383 const qreal bufferTo = height() + m_cacheBuffer;
384 while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
385 if (!(item = createItem(modelIndex, false)))
386 break;
387 pos += item->height();
388 ++modelIndex;
389 }
391 m_previousContentY = m_visibleItems.first()->y() - headerHeight;
392 }
393 setContentY(m_visibleItems.first()->y() + m_clipItem->y() - headerHeight);
394 if (m_headerItem) {
395 // TODO This should not be needed and the code that adjust the m_headerItem position
396 // in viewportMoved() should be enough but in some cases we have not found a way to reproduce
397 // yet the code of viewportMoved() fails so here we make sure that at least if we are calling
398 // positionAtBeginning the header item will be correctly positioned
399 m_headerItem->setY(-m_minYExtent);
400 }
403static inline bool uFuzzyCompare(qreal r1, qreal r2)
405 return qFuzzyCompare(r1, r2) || (qFuzzyIsNull(r1) && qFuzzyIsNull(r2));
408void ListViewWithPageHeader::showHeader()
410 if (!m_headerItem)
411 return;
413 const auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
414 if (!uFuzzyCompare(to, contentY())) {
415 const bool headerShownByItsOwn = contentY() < m_headerItem->y() + m_headerItem->height();
416 if (headerShownByItsOwn && m_headerItemShownHeight == 0) {
417 // We are not clipping since we are just at the top of the viewport
418 // but because of the showHeader animation we will need to, so
419 // enable the clipping without logically moving the items
420 m_headerItemShownHeight = m_headerItem->y() + m_headerItem->height() - contentY();
421 if (!m_visibleItems.isEmpty()) {
422 updateClipItem();
423 ListItem *firstItem = m_visibleItems.first();
424 firstItem->setY(firstItem->y() - m_headerItemShownHeight);
425 layout();
426 }
427 Q_EMIT headerItemShownHeightChanged();
428 }
429 m_contentYAnimation->setTo(to);
430 contentYAnimationType = ContentYAnimationShowHeader;
431 m_contentYAnimation->start();
432 }
435int ListViewWithPageHeader::firstCreatedIndex() const
437 return m_firstVisibleIndex;
440int ListViewWithPageHeader::createdItemCount() const
442 return m_visibleItems.count();
445QQuickItem *ListViewWithPageHeader::item(int modelIndex) const
447 ListItem *item = itemAtIndex(modelIndex);
448 if (item)
449 return item->m_item;
450 else
451 return nullptr;
454bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex)
456 ListItem *listItem = itemAtIndex(modelIndex);
457 if (listItem) {
458 return maximizeVisibleArea(listItem, listItem->height());
459 }
461 return false;
464bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex, int itemHeight)
466 if (itemHeight < 0)
467 return false;
469 ListItem *listItem = itemAtIndex(modelIndex);
470 if (listItem) {
471 return maximizeVisibleArea(listItem, itemHeight + (listItem->sectionItem() ? listItem->sectionItem()->height() : 0));
472 }
474 return false;
477bool ListViewWithPageHeader::maximizeVisibleArea(ListItem *listItem, int listItemHeight)
479 if (listItem) {
480 layout();
481 const auto listItemY = m_clipItem->y() + listItem->y();
482 if (listItemY > contentY() && listItemY + listItemHeight > contentY() + height()) {
483 // we can scroll the list up to show more stuff
484 const auto to = qMin(listItemY, listItemY + listItemHeight - height());
485 m_contentYAnimation->setTo(to);
486 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
487 m_contentYAnimation->start();
488 } else if ((listItemY < contentY() && listItemY + listItemHeight < contentY() + height()) ||
489 (m_topSectionItem && !listItem->sectionItem() && listItemY - m_topSectionItem->height() < contentY() && listItemY + listItemHeight < contentY() + height()))
490 {
491 // we can scroll the list down to show more stuff
492 auto realVisibleListItemY = listItemY;
493 if (m_topSectionItem) {
494 // If we are showing the top section sticky item and this item doesn't have a section
495 // item we have to make sure to scroll it a bit more so that it is not underlapping
496 // the top section sticky item
497 bool topSectionShown = !QQuickItemPrivate::get(m_topSectionItem)->culled;
498 if (topSectionShown && !listItem->sectionItem()) {
499 realVisibleListItemY -= m_topSectionItem->height();
500 }
501 }
502 const auto to = qMax(realVisibleListItemY, listItemY + listItemHeight - height());
503 m_contentYAnimation->setTo(to);
504 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
505 m_contentYAnimation->start();
506 }
507 return true;
508 }
509 return false;
512qreal ListViewWithPageHeader::minYExtent() const
514// qDebug() << "ListViewWithPageHeader::minYExtent" << m_minYExtent;
515 return m_minYExtent;
518void ListViewWithPageHeader::componentComplete()
520 if (m_delegateModel)
521 m_delegateModel->componentComplete();
523 QQuickFlickable::componentComplete();
525 polish();
528void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
530 // Check we are not being taken down and don't paint anything
531 // TODO Check if we still need this in 5.2
532 // For reproduction just inifnite loop testDash or testDashContent
533 if (!QQmlEngine::contextForObject(this)->parentContext())
534 return;
536 QQuickFlickable::viewportMoved(orient);
537// qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
538 const qreal diff = m_previousContentY - contentY();
539 adjustHeader(diff);
540 m_previousContentY = contentY();
541 layout();
542 polish();
545void ListViewWithPageHeader::adjustHeader(qreal diff)
547 const bool showHeaderAnimationRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationShowHeader;
548 if (m_headerItem) {
549 const auto oldHeaderItemShownHeight = m_headerItemShownHeight;
550 if (uFuzzyCompare(contentY(), -m_minYExtent) || contentY() > -m_minYExtent) {
551 m_headerItem->setHeight(m_headerItem->implicitHeight());
552 // We are going down (but it's not because of the rebound at the end)
553 // (but the header was not shown by it's own position)
554 // or the header is partially shown and we are not doing a maximizeVisibleArea either
555 const bool scrolledUp = m_previousContentY > contentY();
556 const bool notRebounding = qRound(contentY() + height()) < qRound(contentHeight());
557 const bool notShownByItsOwn = contentY() + diff >= m_headerItem->y() + m_headerItem->height();
558 const bool maximizeVisibleAreaRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationMaximizeVisibleArea;
560 if (!scrolledUp && (contentY() == -m_minYExtent || (m_headerItemShownHeight == 0 && m_previousContentY == m_headerItem->y()))) {
561 m_headerItemShownHeight = 0;
562 m_headerItem->setY(-m_minYExtent);
563 } else if ((scrolledUp && notRebounding && notShownByItsOwn && !maximizeVisibleAreaRunning) || (m_headerItemShownHeight > 0) || m_inContentHeightKeepHeaderShown) {
564 if (maximizeVisibleAreaRunning && diff > 0) {
565 // If we are maximizing and the header was shown, make sure we hide it
566 m_headerItemShownHeight -= diff;
567 } else {
568 m_headerItemShownHeight += diff;
569 }
570 if (uFuzzyCompare(contentY(), -m_minYExtent)) {
571 m_headerItemShownHeight = 0;
572 } else {
573 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
574 }
575 if (m_headerItemShownHeight > 0) {
576 if (uFuzzyCompare(m_headerItem->height(), m_headerItemShownHeight)) {
577 m_headerItem->setY(contentY());
578 m_headerItemShownHeight = m_headerItem->height();
579 } else {
580 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
581 }
582 } else {
583 m_headerItem->setY(-m_minYExtent);
584 }
585 }
586 Q_EMIT headerItemShownHeightChanged();
587 } else {
588 // Stick the header item to the top when dragging down
589 m_headerItem->setY(contentY());
590 m_headerItem->setHeight(m_headerItem->implicitHeight() + (-m_minYExtent - contentY()));
591 }
592 // We will be changing the clip item, need to accomadate for it
593 // otherwise we move the firstItem down/up twice (unless the
594 // show header animation is running, where we want to keep the viewport stable)
595 if (!showHeaderAnimationRunning) {
596 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
597 } else {
598 diff = -diff;
599 }
600 }
601 if (!m_visibleItems.isEmpty()) {
602 updateClipItem();
603 ListItem *firstItem = m_visibleItems.first();
604 firstItem->setY(firstItem->y() + diff);
605 if (showHeaderAnimationRunning) {
606 adjustMinYExtent();
607 }
608 }
611void ListViewWithPageHeader::createDelegateModel()
613 m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
614 connect(m_delegateModel, &QQmlDelegateModel::createdItem, this, &ListViewWithPageHeader::itemCreated);
615 if (isComponentComplete())
616 m_delegateModel->componentComplete();
617 updateWatchedRoles();
620void ListViewWithPageHeader::refill()
622 if (m_inLayout) {
623 return;
624 }
625 if (!isComponentComplete()) {
626 return;
627 }
629 const qreal from = contentY();
630 const qreal to = from + height();
631 const qreal bufferFrom = from - m_cacheBuffer;
632 const qreal bufferTo = to + m_cacheBuffer;
634 bool added = addVisibleItems(from, to, false);
635 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
636 added |= addVisibleItems(bufferFrom, bufferTo, true);
638 if (added || removed) {
639 m_contentHeightDirty = true;
640 }
643bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
645 if (!delegate())
646 return false;
648 if (m_delegateModel->count() == 0)
649 return false;
651 ListItem *item;
652// qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
654 int modelIndex = 0;
655 qreal pos = 0;
656 if (!m_visibleItems.isEmpty()) {
657 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
658 item = m_visibleItems.last();
659 pos = item->y() + item->height() + m_clipItem->y();
660 }
661 bool changed = false;
662// qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
663 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
664// qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
665 if (!(item = createItem(modelIndex, asynchronous)))
666 break;
667 pos += item->height();
668 ++modelIndex;
669 changed = true;
670 }
672 modelIndex = 0;
673 pos = 0;
674 if (!m_visibleItems.isEmpty()) {
675 modelIndex = m_firstVisibleIndex - 1;
676 item = m_visibleItems.first();
677 pos = item->y() + m_clipItem->y();
678 }
679 while (modelIndex >= 0 && pos > fillFrom) {
680// qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
681 if (!(item = createItem(modelIndex, asynchronous)))
682 break;
683 pos -= item->height();
684 --modelIndex;
685 changed = true;
686 }
688 return changed;
691void ListViewWithPageHeader::reallyReleaseItem(ListItem *listItem)
693 QQuickItem *item = listItem->m_item;
694 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
695 if (flags & QQmlDelegateModel::Destroyed) {
696 item->setParentItem(nullptr);
697 }
698 listItem->sectionItem()->deleteLater();
699 delete listItem;
702void ListViewWithPageHeader::releaseItem(ListItem *listItem)
704 QQuickItemPrivate::get(listItem->m_item)->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
705 if (listItem->sectionItem()) {
706 QQuickItemPrivate::get(listItem->sectionItem())->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
707 }
708 m_itemsToRelease << listItem;
711void ListViewWithPageHeader::updateWatchedRoles()
713 if (m_delegateModel) {
714 QList<QByteArray> roles;
715 if (!m_sectionProperty.isEmpty())
716 roles << m_sectionProperty.toUtf8();
717 m_delegateModel->setWatchedRoles(roles);
718 }
721QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
723 if (!m_sectionDelegate)
724 return nullptr;
726 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
728 if (modelIndex > 0) {
729 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
730 if (section == prevSection)
731 return nullptr;
732 }
733 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
734 // Already inserted items can't steal next section header
735 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
736 if (section == nextSection) {
737 // Steal the section header
738 ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
739 if (nextItem) {
740 QQuickItem *sectionItem = nextItem->sectionItem();
741 nextItem->setSectionItem(nullptr);
742 return sectionItem;
743 }
744 }
745 }
747 return getSectionItem(section);
750QQuickItem *ListViewWithPageHeader::getSectionItem(const QString &sectionText, bool watchGeometry)
752 QQuickItem *sectionItem = nullptr;
754 QQmlContext *creationContext = m_sectionDelegate->creationContext();
755 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
756 QObject *nobj = m_sectionDelegate->beginCreate(context);
757 if (nobj) {
758 QQml_setParent_noEvent(context, nobj);
759 sectionItem = qobject_cast<QQuickItem *>(nobj);
760 if (!sectionItem) {
761 delete nobj;
762 } else {
763 sectionItem->setProperty("text", sectionText);
764 sectionItem->setProperty("delegateIndex", -1);
765 sectionItem->setZ(2);
766 QQml_setParent_noEvent(sectionItem, m_clipItem);
767 sectionItem->setParentItem(m_clipItem);
768 }
769 } else {
770 delete context;
771 }
772 m_sectionDelegate->completeCreate();
774 if (watchGeometry) {
775 QQuickItemPrivate::get(sectionItem)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
776 }
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 item->sectionItem()->setProperty("text", sectionText);
801 }
802 } else {
803 if (item->sectionItem()) {
804 item->sectionItem()->deleteLater();
805 item->setSectionItem(nullptr);
806 }
807 }
808 }
811bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
813// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
814 // Do not remove items if we are overshooting up or down, since we'll come back
815 // to the "stable" position and delete/create items without any reason
816 if (contentY() < -m_minYExtent) {
817 return false;
818 } else if (contentY() + height() > contentHeight()) {
819 return false;
820 }
821 bool changed = false;
823 bool foundVisible = false;
824 int i = 0;
825 int removedItems = 0;
826 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
827 while (i < m_visibleItems.count()) {
828 ListItem *item = m_visibleItems[i];
829 const qreal pos = item->y() + m_clipItem->y();
830// qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
831 if (pos + item->height() < bufferFrom || pos > bufferTo) {
832// qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
833 releaseItem(item);
834 m_visibleItems.removeAt(i);
835 changed = true;
836 ++removedItems;
837 } else {
838 if (!foundVisible) {
839 foundVisible = true;
840 const int itemIndex = m_firstVisibleIndex + removedItems + i;
841 m_firstVisibleIndex = itemIndex;
842 }
843 ++i;
844 }
845 }
846 if (!foundVisible) {
847 initializeValuesForEmptyList();
848 }
849 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
850 adjustMinYExtent();
851 }
853 return changed;
856ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
858// qDebug() << "CREATE ITEM" << modelIndex;
859 if (asynchronous && m_asyncRequestedIndex != -1)
860 return nullptr;
862 m_asyncRequestedIndex = -1;
863 QObject* object = m_delegateModel->object(modelIndex, asynchronous);
864 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
865 if (!item) {
866 if (object) {
867 m_delegateModel->release(object);
868 if (!m_delegateValidated) {
869 m_delegateValidated = true;
870 QObject* delegateObj = delegate();
871 qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
872 }
873 } else {
874 m_asyncRequestedIndex = modelIndex;
875 }
876 return 0;
877 } else {
878// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
879 ListItem *listItem = new ListItem;
880 listItem->m_item = item;
881 listItem->setSectionItem(getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/));
882 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
883 ListItem *prevItem = itemAtIndex(modelIndex - 1);
884 bool lostItem = false; // Is an item that we requested async but because of model changes
885 // it is no longer attached to any of the existing items (has no prev nor next item)
886 // nor is the first item
887 if (prevItem) {
888 listItem->setY(prevItem->y() + prevItem->height());
889 } else {
890 ListItem *currItem = itemAtIndex(modelIndex);
891 if (currItem) {
892 // There's something already in m_visibleItems at out index, meaning this is an insert, so attach to its top
893 listItem->setY(currItem->y() - listItem->height());
894 } else {
895 ListItem *nextItem = itemAtIndex(modelIndex + 1);
896 if (nextItem) {
897 listItem->setY(nextItem->y() - listItem->height());
898 } else if (modelIndex == 0) {
899 listItem->setY(-m_clipItem->y() + (m_headerItem ? m_headerItem->height() : 0));
900 } else if (!m_visibleItems.isEmpty()) {
901 lostItem = true;
902 }
903 }
904 }
905 if (lostItem) {
906 listItem->setCulled(true);
907 releaseItem(listItem);
908 listItem = nullptr;
909 } else {
910 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() <= contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
911 if (m_visibleItems.isEmpty()) {
912 m_visibleItems << listItem;
913 } else {
914 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
915 }
916 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
917 m_firstVisibleIndex = modelIndex;
918 polish();
919 }
920 if (listItem->sectionItem()) {
921 listItem->sectionItem()->setProperty("delegateIndex", modelIndex);
922 }
923 adjustMinYExtent();
924 m_contentHeightDirty = true;
925 }
926 return listItem;
927 }
930void ListViewWithPageHeader::itemCreated(int modelIndex, QObject *object)
932 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
933 if (!item) {
934 qWarning() << "ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
935 return;
936 }
937// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
938 // Check we are not being taken down and don't paint anything
939 // TODO Check if we still need this in 5.2
940 // For reproduction just inifnite loop testDash or testDashContent
941 if (!QQmlEngine::contextForObject(this)->parentContext())
942 return;
944 item->setParentItem(m_clipItem);
945 // FIXME Why do we need the refreshExpressions call?
946 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
947 QQmlContextPrivate::get(context)->data->refreshExpressions();
948 item->setProperty("heightToClip", QVariant::fromValue<int>(0));
949 if (modelIndex == m_asyncRequestedIndex) {
950 createItem(modelIndex, false);
951 refill();
952 }
955void ListViewWithPageHeader::updateClipItem()
957 m_clipItem->setHeight(height() - m_headerItemShownHeight);
958 m_clipItem->setY(contentY() + m_headerItemShownHeight);
959 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
962void ListViewWithPageHeader::onContentHeightChanged()
964 updateClipItem();
967void ListViewWithPageHeader::onContentWidthChanged()
969 m_clipItem->setWidth(contentItem()->width());
972void ListViewWithPageHeader::onHeightChanged()
974 polish();
978void ListViewWithPageHeader::onModelUpdated(const QQmlChangeSet &changeSet, bool /*reset*/)
980 // TODO Do something with reset
981// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
982 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
984 Q_FOREACH(const QQmlChangeSet::Change remove, changeSet.removes()) {
985// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove.index << remove.count;
986 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
987 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
988 // If all the items we are removing are either not created or culled
989 // we have to grow down to avoid viewport changing
990 bool growDown = true;
991 for (int i = 0; growDown && i < remove.count; ++i) {
992 const int modelIndex = remove.index + i;
993 ListItem *item = itemAtIndex(modelIndex);
994 if (item && !item->culled()) {
995 growDown = false;
996 }
997 }
998 for (int i = remove.count - 1; i >= 0; --i) {
999 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
1000 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
1001 ListItem *item = m_visibleItems[visibleIndex];
1002 // Pass the section item down if needed
1003 if (item->sectionItem() && visibleIndex + 1 < m_visibleItems.count()) {
1004 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
1005 if (!nextItem->sectionItem()) {
1006 nextItem->setSectionItem(item->sectionItem());
1007 item->setSectionItem(nullptr);
1010 releaseItem(item);
1011 m_visibleItems.removeAt(visibleIndex);
1014 if (growDown) {
1015 adjustMinYExtent();
1016 } else if (remove.index <= m_firstVisibleIndex && !m_visibleItems.isEmpty()) {
1017 m_visibleItems.first()->setY(oldFirstValidIndexPos);
1019 if (m_visibleItems.isEmpty()) {
1020 m_firstVisibleIndex = -1;
1021 } else {
1022 m_firstVisibleIndex -= qMax(0, m_firstVisibleIndex - remove.index);
1024 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
1025 m_firstVisibleIndex -= remove.count;
1027 for (int i = remove.count - 1; i >= 0; --i) {
1028 const int modelIndex = remove.index + i;
1029 if (modelIndex == m_asyncRequestedIndex) {
1030 m_asyncRequestedIndex = -1;
1031 } else if (modelIndex < m_asyncRequestedIndex) {
1032 m_asyncRequestedIndex--;
1037 Q_FOREACH(const QQmlChangeSet::Change insert, changeSet.inserts()) {
1038// qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
1039 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
1040 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
1041 if (insertingInValidIndexes || firstItemWithViewOnTop)
1043 // If the items we are adding won't be really visible
1044 // we grow up instead of down to not change the viewport
1045 bool growUp = false;
1046 if (!firstItemWithViewOnTop) {
1047 for (int i = 0; i < m_visibleItems.count(); ++i) {
1048 if (!m_visibleItems[i]->culled()) {
1049 if (insert.index <= m_firstVisibleIndex + i) {
1050 growUp = true;
1052 break;
1057 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
1058 for (int i = insert.count - 1; i >= 0; --i) {
1059 const int modelIndex = insert.index + i;
1060 ListItem *item = createItem(modelIndex, false);
1061 if (growUp) {
1062 ListItem *firstItem = m_visibleItems.first();
1063 firstItem->setY(firstItem->y() - item->height());
1065 // Adding an item may break a "same section" chain, so check
1066 // if we need adding a new section item
1067 if (m_sectionDelegate) {
1068 ListItem *nextItem = itemAtIndex(modelIndex + 1);
1069 if (nextItem && !nextItem->sectionItem()) {
1070 nextItem->setSectionItem(getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/));
1071 if (growUp && nextItem->sectionItem()) {
1072 ListItem *firstItem = m_visibleItems.first();
1073 firstItem->setY(firstItem->y() - nextItem->sectionItem()->height());
1078 if (firstItemWithViewOnTop) {
1079 ListItem *firstItem = m_visibleItems.first();
1080 firstItem->setY(oldFirstValidIndexPos);
1082 adjustMinYExtent();
1083 } else if (insert.index <= m_firstVisibleIndex) {
1084 m_firstVisibleIndex += insert.count;
1087 for (int i = insert.count - 1; i >= 0; --i) {
1088 const int modelIndex = insert.index + i;
1089 if (modelIndex <= m_asyncRequestedIndex) {
1090 m_asyncRequestedIndex++;
1095 Q_FOREACH(const QQmlChangeSet::Change change, changeSet.changes()) {
1096 for (int i = change.start(); i < change.end(); ++i) {
1097 updateSectionItem(i);
1099 // Also update the section header for the next item after the change since it may be influenced
1100 updateSectionItem(change.end());
1103 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
1104 if (m_visibleItems.isEmpty()) {
1105 initializeValuesForEmptyList();
1106 } else {
1107 adjustMinYExtent();
1111 for (int i = 0; i < m_visibleItems.count(); ++i) {
1112 ListItem *item = m_visibleItems[i];
1113 if (item->sectionItem()) {
1114 item->sectionItem()->setProperty("delegateIndex", m_firstVisibleIndex + i);
1118 layout();
1119 polish();
1120 m_contentHeightDirty = true;
1123void ListViewWithPageHeader::contentYAnimationRunningChanged(bool running)
1125 setInteractive(!running);
1126 if (!running) {
1127 m_contentHeightDirty = true;
1128 polish();
1132void ListViewWithPageHeader::itemGeometryChanged(QQuickItem *item, const QRectF &newGeometry, const QRectF &oldGeometry)
1134 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
1135 if (heightDiff != 0) {
1136 if (!m_visibleItems.isEmpty()) {
1137 ListItem *firstItem = m_visibleItems.first();
1138 const auto prevFirstItemY = firstItem->y();
1139 if (!m_inContentHeightKeepHeaderShown && oldGeometry.y() + oldGeometry.height() + m_clipItem->y() <= contentY()) {
1140 firstItem->setY(firstItem->y() - heightDiff);
1141 } else if (item == firstItem->sectionItem()) {
1142 firstItem->setY(firstItem->y() + heightDiff);
1145 if (firstItem->y() != prevFirstItemY) {
1146 adjustMinYExtent();
1147 layout();
1150 refill();
1151 adjustMinYExtent();
1152 polish();
1153 m_contentHeightDirty = true;
1157void ListViewWithPageHeader::itemImplicitHeightChanged(QQuickItem *item)
1159 if (item == m_headerItem) {
1160 const qreal diff = m_headerItem->implicitHeight() - m_previousHeaderImplicitHeight;
1161 if (diff != 0) {
1162 adjustHeader(diff);
1163 m_previousHeaderImplicitHeight = m_headerItem->implicitHeight();
1164 layout();
1165 polish();
1166 m_contentHeightDirty = true;
1171void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
1173 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
1174 if (m_headerItemShownHeight > 0) {
1175 // If the header is shown because of the clip
1176 // Change its size
1177 m_headerItemShownHeight += heightDiff;
1178 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
1179 updateClipItem();
1180 adjustMinYExtent();
1181 Q_EMIT headerItemShownHeightChanged();
1182 } else {
1183 if (oldHeaderY + oldHeaderHeight > contentY()) {
1184 // If the header is shown because its position
1185 // Change its size
1186 ListItem *firstItem = m_visibleItems.first();
1187 firstItem->setY(firstItem->y() + heightDiff);
1188 layout();
1189 } else {
1190 // If the header is not on screen, just change the start of the list
1191 // so the viewport is not changed
1192 adjustMinYExtent();
1198void ListViewWithPageHeader::adjustMinYExtent()
1200 if (m_visibleItems.isEmpty() || contentHeight() < height()) {
1201 m_minYExtent = 0;
1202 } else {
1203 qreal nonCreatedHeight = 0;
1204 if (m_firstVisibleIndex != 0) {
1205 // Calculate the average height of items to estimate the position of the list start
1206 const int visibleItems = m_visibleItems.count();
1207 qreal visibleItemsHeight = 0;
1208 Q_FOREACH(ListItem *item, m_visibleItems) {
1209 visibleItemsHeight += item->height();
1211 nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
1212// qDebug() << m_firstVisibleIndex << visibleItemsHeight << visibleItems << nonCreatedHeight;
1214 const qreal headerHeight = (m_headerItem ? m_headerItem->implicitHeight() : 0);
1215 m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + headerHeight;
1216 if (m_minYExtent != 0 && qFuzzyIsNull(m_minYExtent)) {
1217 m_minYExtent = 0;
1218 m_visibleItems.first()->setY(nonCreatedHeight - m_clipItem->y() + headerHeight);
1223ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
1225 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
1226 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
1227 return m_visibleItems[visibleIndexedModelIndex];
1229 return nullptr;
1232void ListViewWithPageHeader::layout()
1234 if (m_inLayout)
1235 return;
1237 m_inLayout = true;
1238 if (!m_visibleItems.isEmpty()) {
1239 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
1240 const qreal visibleTo = contentY() + height() - m_clipItem->y();
1242 qreal pos = m_visibleItems.first()->y();
1244// qDebug() << "ListViewWithPageHeader::layout Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
1245 int firstReallyVisibleItem = -1;
1246 int modelIndex = m_firstVisibleIndex;
1247 Q_FOREACH(ListItem *item, m_visibleItems) {
1248 const bool cull = pos + item->height() <= visibleFrom || pos >= visibleTo;
1249 item->setCulled(cull);
1250 item->setY(pos);
1251 if (!cull && firstReallyVisibleItem == -1) {
1252 firstReallyVisibleItem = modelIndex;
1253 if (m_topSectionItem) {
1254 // Positing the top section sticky item is a two step process
1255 // First we set it either we cull it (because it doesn't need to be sticked to the top)
1256 // or stick it to the top
1257 // Then after the loop we'll make sure that if there's another section just below it
1258 // pushed the sticky section up to make it disappear
1259 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
1260 bool showStickySectionItem;
1261 // We need to show the "top section sticky item" when the position at the "top" of the
1262 // viewport is bigger than the start of the position of the first visible item
1263 // i.e. the first visible item starts before the viewport, or when the first
1264 // visible item starts just at the viewport start and it does not have its own section item
1265 if (topSectionStickPos > pos) {
1266 showStickySectionItem = true;
1267 } else if (topSectionStickPos == pos) {
1268 showStickySectionItem = !item->sectionItem();
1269 } else {
1270 showStickySectionItem = false;
1272 if (!showStickySectionItem) {
1273 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
1274 if (item->sectionItem()) {
1275 // This seems it should happen since why would we cull the top section
1276 // if the first visible item has no section header? This only happens briefly
1277 // when increasing the height of a list that is at the bottom, the m_topSectionItem
1278 // gets shown shortly in the next polish call
1279 QQuickItemPrivate::get(item->sectionItem())->setCulled(false);
1281 } else {
1282 // Update the top sticky section header
1283 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1284 m_topSectionItem->setProperty("text", section);
1286 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
1287 m_topSectionItem->setY(topSectionStickPos);
1288 int delegateIndex = modelIndex;
1289 // Look for the first index with this section text
1290 while (delegateIndex > 0) {
1291 const QString prevSection = m_delegateModel->stringValue(delegateIndex - 1, m_sectionProperty);
1292 if (prevSection != section)
1293 break;
1294 delegateIndex--;
1296 m_topSectionItem->setProperty("delegateIndex", delegateIndex);
1297 if (item->sectionItem()) {
1298 QQuickItemPrivate::get(item->sectionItem())->setCulled(true);
1303 const qreal clipFrom = visibleFrom + (!item->sectionItem() && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
1304 if (!cull && pos < clipFrom) {
1305 item->m_item->setProperty("heightToClip", clipFrom - pos);
1306 } else {
1307 item->m_item->setProperty("heightToClip", QVariant::fromValue<int>(0));
1309// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
1310 pos += item->height();
1311 ++modelIndex;
1314 // Second step of section sticky item positioning
1315 // Look at the next section header, check if it's pushing up the sticky one
1316 if (m_topSectionItem) {
1317 if (firstReallyVisibleItem >= 0) {
1318 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
1319 ListItem *item = m_visibleItems[i];
1320 if (item->sectionItem()) {
1321 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
1322 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
1324 break;
1330 m_inLayout = false;
1333void ListViewWithPageHeader::updatePolish()
1335 // Check we are not being taken down and don't paint anything
1336 // TODO Check if we still need this in 5.2
1337 // For reproduction just inifnite loop testDash or testDashContent
1338 if (!QQmlEngine::contextForObject(this)->parentContext())
1339 return;
1341 Q_FOREACH(ListItem *item, m_itemsToRelease)
1342 reallyReleaseItem(item);
1343 m_itemsToRelease.clear();
1345 if (!model())
1346 return;
1348 layout();
1350 refill();
1352 if (m_contentHeightDirty) {
1353 qreal contentHeight;
1354 if (m_visibleItems.isEmpty()) {
1355 contentHeight = m_headerItem ? m_headerItem->height() : 0;
1356 } else {
1357 const int modelCount = model()->rowCount();
1358 const int visibleItems = m_visibleItems.count();
1359 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
1360 qreal nonCreatedHeight = 0;
1361 if (lastValidIndex != modelCount - 1) {
1362 const int visibleItems = m_visibleItems.count();
1363 qreal visibleItemsHeight = 0;
1364 Q_FOREACH(ListItem *item, m_visibleItems) {
1365 visibleItemsHeight += item->height();
1367 const int unknownSizes = modelCount - (m_firstVisibleIndex + visibleItems);
1368 nonCreatedHeight = unknownSizes * visibleItemsHeight / visibleItems;
1370 ListItem *item = m_visibleItems.last();
1371 contentHeight = nonCreatedHeight + item->y() + item->height() + m_clipItem->y();
1372 if (m_firstVisibleIndex != 0) {
1373 // Make sure that if we are shrinking we tell the view we still fit
1374 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
1378 m_contentHeightDirty = false;
1379 adjustMinYExtent();
1380 if (contentHeight < height()) {
1381 // need this since in the previous call to adjustMinYExtent contentHeight is not set yet
1382 m_minYExtent = 0;
1384 m_inContentHeightKeepHeaderShown = m_headerItem && m_headerItem->y() == contentY();
1385 setContentHeight(contentHeight);
1386 m_inContentHeightKeepHeaderShown = false;
1390#include "moc_listviewwithpageheader.cpp"