on a22847a help sign in
unity8/plugins/Dash/listviewwithpageheader.cpp
1391 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;
517void ListViewWithPageHeader::componentComplete()
519 if (m_delegateModel)
520 m_delegateModel->componentComplete();
522 QQuickFlickable::componentComplete();
524 polish();
527void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
529 // Check we are not being taken down and don't paint anything
530 // TODO Check if we still need this in 5.2
531 // For reproduction just inifnite loop testDash or testDashContent
532 if (!QQmlEngine::contextForObject(this)->parentContext())
533 return;
535 QQuickFlickable::viewportMoved(orient);
536// qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
537 const qreal diff = m_previousContentY - contentY();
538 adjustHeader(diff);
539 m_previousContentY = contentY();
540 layout();
541 polish();
544void ListViewWithPageHeader::adjustHeader(qreal diff)
546 const bool showHeaderAnimationRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationShowHeader;
547 if (m_headerItem) {
548 const auto oldHeaderItemShownHeight = m_headerItemShownHeight;
549 if (uFuzzyCompare(contentY(), -m_minYExtent) || contentY() > -m_minYExtent) {
550 m_headerItem->setHeight(m_headerItem->implicitHeight());
551 // We are going down (but it's not because of the rebound at the end)
552 // (but the header was not shown by it's own position)
553 // or the header is partially shown and we are not doing a maximizeVisibleArea either
554 const bool scrolledUp = m_previousContentY > contentY();
555 const bool notRebounding = qRound(contentY() + height()) < qRound(contentHeight());
556 const bool notShownByItsOwn = contentY() + diff >= m_headerItem->y() + m_headerItem->height();
557 const bool maximizeVisibleAreaRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationMaximizeVisibleArea;
559 if (!scrolledUp && (contentY() == -m_minYExtent || (m_headerItemShownHeight == 0 && m_previousContentY == m_headerItem->y()))) {
560 m_headerItemShownHeight = 0;
561 m_headerItem->setY(-m_minYExtent);
562 } else if ((scrolledUp && notRebounding && notShownByItsOwn && !maximizeVisibleAreaRunning) || (m_headerItemShownHeight > 0) || m_inContentHeightKeepHeaderShown) {
563 if (maximizeVisibleAreaRunning && diff > 0) {
564 // If we are maximizing and the header was shown, make sure we hide it
565 m_headerItemShownHeight -= diff;
566 } else {
567 m_headerItemShownHeight += diff;
568 }
569 if (uFuzzyCompare(contentY(), -m_minYExtent)) {
570 m_headerItemShownHeight = 0;
571 } else {
572 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
573 }
574 if (m_headerItemShownHeight > 0) {
575 if (uFuzzyCompare(m_headerItem->height(), m_headerItemShownHeight)) {
576 m_headerItem->setY(contentY());
577 m_headerItemShownHeight = m_headerItem->height();
578 } else {
579 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
580 }
581 } else {
582 m_headerItem->setY(-m_minYExtent);
583 }
584 }
585 Q_EMIT headerItemShownHeightChanged();
586 } else {
587 // Stick the header item to the top when dragging down
588 m_headerItem->setY(contentY());
589 m_headerItem->setHeight(m_headerItem->implicitHeight() + (-m_minYExtent - contentY()));
590 }
591 // We will be changing the clip item, need to accomadate for it
592 // otherwise we move the firstItem down/up twice (unless the
593 // show header animation is running, where we want to keep the viewport stable)
594 if (!showHeaderAnimationRunning) {
595 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
596 } else {
597 diff = -diff;
598 }
599 }
600 if (!m_visibleItems.isEmpty()) {
601 updateClipItem();
602 ListItem *firstItem = m_visibleItems.first();
603 firstItem->setY(firstItem->y() + diff);
604 if (showHeaderAnimationRunning) {
605 adjustMinYExtent();
606 }
607 }
610void ListViewWithPageHeader::createDelegateModel()
612 m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
613 connect(m_delegateModel, &QQmlDelegateModel::createdItem, this, &ListViewWithPageHeader::itemCreated);
614 if (isComponentComplete())
615 m_delegateModel->componentComplete();
616 updateWatchedRoles();
619void ListViewWithPageHeader::refill()
621 if (m_inLayout) {
622 return;
623 }
624 if (!isComponentComplete()) {
625 return;
626 }
628 const qreal from = contentY();
629 const qreal to = from + height();
630 const qreal bufferFrom = from - m_cacheBuffer;
631 const qreal bufferTo = to + m_cacheBuffer;
633 bool added = addVisibleItems(from, to, false);
634 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
635 added |= addVisibleItems(bufferFrom, bufferTo, true);
637 if (added || removed) {
638 m_contentHeightDirty = true;
639 }
642bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
644 if (!delegate())
645 return false;
647 if (m_delegateModel->count() == 0)
648 return false;
650 ListItem *item;
651// qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
653 int modelIndex = 0;
654 qreal pos = 0;
655 if (!m_visibleItems.isEmpty()) {
656 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
657 item = m_visibleItems.last();
658 pos = item->y() + item->height() + m_clipItem->y();
659 }
660 bool changed = false;
661// qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
662 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
663// qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
664 if (!(item = createItem(modelIndex, asynchronous)))
665 break;
666 pos += item->height();
667 ++modelIndex;
668 changed = true;
669 }
671 modelIndex = 0;
672 pos = 0;
673 if (!m_visibleItems.isEmpty()) {
674 modelIndex = m_firstVisibleIndex - 1;
675 item = m_visibleItems.first();
676 pos = item->y() + m_clipItem->y();
677 }
678 while (modelIndex >= 0 && pos > fillFrom) {
679// qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
680 if (!(item = createItem(modelIndex, asynchronous)))
681 break;
682 pos -= item->height();
683 --modelIndex;
684 changed = true;
685 }
687 return changed;
690void ListViewWithPageHeader::reallyReleaseItem(ListItem *listItem)
692 QQuickItem *item = listItem->m_item;
693 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
694 if (flags & QQmlDelegateModel::Destroyed) {
695 item->setParentItem(nullptr);
696 }
697 listItem->sectionItem()->deleteLater();
698 delete listItem;
701void ListViewWithPageHeader::releaseItem(ListItem *listItem)
703 QQuickItemPrivate::get(listItem->m_item)->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
704 if (listItem->sectionItem()) {
705 QQuickItemPrivate::get(listItem->sectionItem())->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
706 }
707 m_itemsToRelease << listItem;
710void ListViewWithPageHeader::updateWatchedRoles()
712 if (m_delegateModel) {
713 QList<QByteArray> roles;
714 if (!m_sectionProperty.isEmpty())
715 roles << m_sectionProperty.toUtf8();
716 m_delegateModel->setWatchedRoles(roles);
717 }
720QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
722 if (!m_sectionDelegate)
723 return nullptr;
725 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
727 if (modelIndex > 0) {
728 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
729 if (section == prevSection)
730 return nullptr;
731 }
732 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
733 // Already inserted items can't steal next section header
734 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
735 if (section == nextSection) {
736 // Steal the section header
737 ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
738 if (nextItem) {
739 QQuickItem *sectionItem = nextItem->sectionItem();
740 nextItem->setSectionItem(nullptr);
741 return sectionItem;
742 }
743 }
744 }
746 return getSectionItem(section);
749QQuickItem *ListViewWithPageHeader::getSectionItem(const QString &sectionText)
751 QQuickItem *sectionItem = nullptr;
753 QQmlContext *creationContext = m_sectionDelegate->creationContext();
754 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
755 context->setContextProperty(QStringLiteral("section"), sectionText);
756 context->setContextProperty(QStringLiteral("delegateIndex"), -1);
757 QObject *nobj = m_sectionDelegate->beginCreate(context);
758 if (nobj) {
759 QQml_setParent_noEvent(context, nobj);
760 sectionItem = qobject_cast<QQuickItem *>(nobj);
761 if (!sectionItem) {
762 delete nobj;
763 } else {
764 sectionItem->setZ(2);
765 QQml_setParent_noEvent(sectionItem, m_clipItem);
766 sectionItem->setParentItem(m_clipItem);
767 }
768 } else {
769 delete context;
770 }
771 m_sectionDelegate->completeCreate();
773 QQuickItemPrivate::get(sectionItem)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
775 return sectionItem;
778void ListViewWithPageHeader::updateSectionItem(int modelIndex)
780 ListItem *item = itemAtIndex(modelIndex);
781 if (item) {
782 const QString sectionText = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
784 bool needSectionHeader = true;
785 // if it is the same section as the previous item need to drop the section
786 if (modelIndex > 0) {
787 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
788 if (sectionText == prevSection) {
789 needSectionHeader = false;
790 }
791 }
793 if (needSectionHeader) {
794 if (!item->sectionItem()) {
795 item->setSectionItem(getSectionItem(sectionText));
796 } else {
797 QQmlContext *context = QQmlEngine::contextForObject(item->sectionItem())->parentContext();
798 context->setContextProperty(QStringLiteral("section"), sectionText);
799 }
800 } else {
801 if (item->sectionItem()) {
802 item->sectionItem()->deleteLater();
803 item->setSectionItem(nullptr);
804 }
805 }
806 }
809bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
811// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
812 // Do not remove items if we are overshooting up or down, since we'll come back
813 // to the "stable" position and delete/create items without any reason
814 if (contentY() < -m_minYExtent) {
815 return false;
816 } else if (contentY() + height() > contentHeight()) {
817 return false;
818 }
819 bool changed = false;
821 bool foundVisible = false;
822 int i = 0;
823 int removedItems = 0;
824 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
825 while (i < m_visibleItems.count()) {
826 ListItem *item = m_visibleItems[i];
827 const qreal pos = item->y() + m_clipItem->y();
828// qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
829 if (pos + item->height() < bufferFrom || pos > bufferTo) {
830// qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
831 releaseItem(item);
832 m_visibleItems.removeAt(i);
833 changed = true;
834 ++removedItems;
835 } else {
836 if (!foundVisible) {
837 foundVisible = true;
838 const int itemIndex = m_firstVisibleIndex + removedItems + i;
839 m_firstVisibleIndex = itemIndex;
840 }
841 ++i;
842 }
843 }
844 if (!foundVisible) {
845 initializeValuesForEmptyList();
846 }
847 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
848 adjustMinYExtent();
849 }
851 return changed;
854ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
856// qDebug() << "CREATE ITEM" << modelIndex;
857 if (asynchronous && m_asyncRequestedIndex != -1)
858 return nullptr;
860 m_asyncRequestedIndex = -1;
861 QObject* object = m_delegateModel->object(modelIndex, asynchronous);
862 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
863 if (!item) {
864 if (object) {
865 m_delegateModel->release(object);
866 if (!m_delegateValidated) {
867 m_delegateValidated = true;
868 QObject* delegateObj = delegate();
869 qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
870 }
871 } else {
872 m_asyncRequestedIndex = modelIndex;
873 }
874 return 0;
875 } else {
876// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
877 ListItem *listItem = new ListItem;
878 listItem->m_item = item;
879 listItem->setSectionItem(getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/));
880 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
881 ListItem *prevItem = itemAtIndex(modelIndex - 1);
882 bool lostItem = false; // Is an item that we requested async but because of model changes
883 // it is no longer attached to any of the existing items (has no prev nor next item)
884 // nor is the first item
885 if (prevItem) {
886 listItem->setY(prevItem->y() + prevItem->height());
887 } else {
888 ListItem *currItem = itemAtIndex(modelIndex);
889 if (currItem) {
890 // There's something already in m_visibleItems at out index, meaning this is an insert, so attach to its top
891 listItem->setY(currItem->y() - listItem->height());
892 } else {
893 ListItem *nextItem = itemAtIndex(modelIndex + 1);
894 if (nextItem) {
895 listItem->setY(nextItem->y() - listItem->height());
896 } else if (modelIndex == 0) {
897 listItem->setY(-m_clipItem->y() + (m_headerItem ? m_headerItem->height() : 0));
898 } else if (!m_visibleItems.isEmpty()) {
899 lostItem = true;
900 }
901 }
902 }
903 if (lostItem) {
904 listItem->setCulled(true);
905 releaseItem(listItem);
906 listItem = nullptr;
907 } else {
908 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() <= contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
909 if (m_visibleItems.isEmpty()) {
910 m_visibleItems << listItem;
911 } else {
912 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
913 }
914 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
915 m_firstVisibleIndex = modelIndex;
916 polish();
917 }
918 if (listItem->sectionItem()) {
919 QQmlContext *context = QQmlEngine::contextForObject(listItem->sectionItem())->parentContext();
920 context->setContextProperty(QStringLiteral("delegateIndex"), modelIndex);
921 }
922 adjustMinYExtent();
923 m_contentHeightDirty = true;
924 }
925 return listItem;
926 }
929void ListViewWithPageHeader::itemCreated(int modelIndex, QObject *object)
931 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
932 if (!item) {
933 qWarning() << "ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
934 return;
935 }
936// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
937 // Check we are not being taken down and don't paint anything
938 // TODO Check if we still need this in 5.2
939 // For reproduction just inifnite loop testDash or testDashContent
940 if (!QQmlEngine::contextForObject(this)->parentContext())
941 return;
943 item->setParentItem(m_clipItem);
944 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
945 context->setContextProperty(QStringLiteral("ListViewWithPageHeader"), this);
946 context->setContextProperty(QStringLiteral("heightToClip"), QVariant::fromValue<int>(0));
947 if (modelIndex == m_asyncRequestedIndex) {
948 createItem(modelIndex, false);
949 refill();
950 }
953void ListViewWithPageHeader::updateClipItem()
955 m_clipItem->setHeight(height() - m_headerItemShownHeight);
956 m_clipItem->setY(contentY() + m_headerItemShownHeight);
957 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
960void ListViewWithPageHeader::onContentHeightChanged()
962 updateClipItem();
965void ListViewWithPageHeader::onContentWidthChanged()
967 m_clipItem->setWidth(contentItem()->width());
970void ListViewWithPageHeader::onHeightChanged()
972 polish();
976void ListViewWithPageHeader::onModelUpdated(const QQmlChangeSet &changeSet, bool /*reset*/)
978 // TODO Do something with reset
979// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
980 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
982 Q_FOREACH(const QQmlChangeSet::Change remove, changeSet.removes()) {
983// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove.index << remove.count;
984 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
985 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
986 // If all the items we are removing are either not created or culled
987 // we have to grow down to avoid viewport changing
988 bool growDown = true;
989 for (int i = 0; growDown && i < remove.count; ++i) {
990 const int modelIndex = remove.index + i;
991 ListItem *item = itemAtIndex(modelIndex);
992 if (item && !item->culled()) {
993 growDown = false;
994 }
995 }
996 for (int i = remove.count - 1; i >= 0; --i) {
997 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
998 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
999 ListItem *item = m_visibleItems[visibleIndex];
1000 // Pass the section item down if needed
1001 if (item->sectionItem() && visibleIndex + 1 < m_visibleItems.count()) {
1002 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
1003 if (!nextItem->sectionItem()) {
1004 nextItem->setSectionItem(item->sectionItem());
1005 item->setSectionItem(nullptr);
1008 releaseItem(item);
1009 m_visibleItems.removeAt(visibleIndex);
1012 if (growDown) {
1013 adjustMinYExtent();
1014 } else if (remove.index <= m_firstVisibleIndex && !m_visibleItems.isEmpty()) {
1015 m_visibleItems.first()->setY(oldFirstValidIndexPos);
1017 if (m_visibleItems.isEmpty()) {
1018 m_firstVisibleIndex = -1;
1019 } else {
1020 m_firstVisibleIndex -= qMax(0, m_firstVisibleIndex - remove.index);
1022 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
1023 m_firstVisibleIndex -= remove.count;
1025 for (int i = remove.count - 1; i >= 0; --i) {
1026 const int modelIndex = remove.index + i;
1027 if (modelIndex == m_asyncRequestedIndex) {
1028 m_asyncRequestedIndex = -1;
1029 } else if (modelIndex < m_asyncRequestedIndex) {
1030 m_asyncRequestedIndex--;
1035 Q_FOREACH(const QQmlChangeSet::Change insert, changeSet.inserts()) {
1036// qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
1037 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
1038 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
1039 if (insertingInValidIndexes || firstItemWithViewOnTop)
1041 // If the items we are adding won't be really visible
1042 // we grow up instead of down to not change the viewport
1043 bool growUp = false;
1044 if (!firstItemWithViewOnTop) {
1045 for (int i = 0; i < m_visibleItems.count(); ++i) {
1046 if (!m_visibleItems[i]->culled()) {
1047 if (insert.index <= m_firstVisibleIndex + i) {
1048 growUp = true;
1050 break;
1055 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
1056 for (int i = insert.count - 1; i >= 0; --i) {
1057 const int modelIndex = insert.index + i;
1058 ListItem *item = createItem(modelIndex, false);
1059 if (growUp) {
1060 ListItem *firstItem = m_visibleItems.first();
1061 firstItem->setY(firstItem->y() - item->height());
1063 // Adding an item may break a "same section" chain, so check
1064 // if we need adding a new section item
1065 if (m_sectionDelegate) {
1066 ListItem *nextItem = itemAtIndex(modelIndex + 1);
1067 if (nextItem && !nextItem->sectionItem()) {
1068 nextItem->setSectionItem(getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/));
1069 if (growUp && nextItem->sectionItem()) {
1070 ListItem *firstItem = m_visibleItems.first();
1071 firstItem->setY(firstItem->y() - nextItem->sectionItem()->height());
1076 if (firstItemWithViewOnTop) {
1077 ListItem *firstItem = m_visibleItems.first();
1078 firstItem->setY(oldFirstValidIndexPos);
1080 adjustMinYExtent();
1081 } else if (insert.index <= m_firstVisibleIndex) {
1082 m_firstVisibleIndex += insert.count;
1085 for (int i = insert.count - 1; i >= 0; --i) {
1086 const int modelIndex = insert.index + i;
1087 if (modelIndex <= m_asyncRequestedIndex) {
1088 m_asyncRequestedIndex++;
1093 Q_FOREACH(const QQmlChangeSet::Change change, changeSet.changes()) {
1094 for (int i = change.start(); i < change.end(); ++i) {
1095 updateSectionItem(i);
1097 // Also update the section header for the next item after the change since it may be influenced
1098 updateSectionItem(change.end());
1101 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
1102 if (m_visibleItems.isEmpty()) {
1103 initializeValuesForEmptyList();
1104 } else {
1105 adjustMinYExtent();
1109 for (int i = 0; i < m_visibleItems.count(); ++i) {
1110 ListItem *item = m_visibleItems[i];
1111 if (item->sectionItem()) {
1112 QQmlContext *context = QQmlEngine::contextForObject(item->sectionItem())->parentContext();
1113 context->setContextProperty(QStringLiteral("delegateIndex"), m_firstVisibleIndex + i);
1117 layout();
1118 polish();
1119 m_contentHeightDirty = true;
1122void ListViewWithPageHeader::contentYAnimationRunningChanged(bool running)
1124 setInteractive(!running);
1125 if (!running) {
1126 m_contentHeightDirty = true;
1127 polish();
1131void ListViewWithPageHeader::itemGeometryChanged(QQuickItem *item, const QRectF &newGeometry, const QRectF &oldGeometry)
1133 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
1134 if (heightDiff != 0) {
1135 if (!m_visibleItems.isEmpty()) {
1136 ListItem *firstItem = m_visibleItems.first();
1137 const auto prevFirstItemY = firstItem->y();
1138 if (!m_inContentHeightKeepHeaderShown && oldGeometry.y() + oldGeometry.height() + m_clipItem->y() <= contentY()) {
1139 firstItem->setY(firstItem->y() - heightDiff);
1140 } else if (item == firstItem->sectionItem()) {
1141 firstItem->setY(firstItem->y() + heightDiff);
1144 if (firstItem->y() != prevFirstItemY) {
1145 adjustMinYExtent();
1146 layout();
1149 refill();
1150 adjustMinYExtent();
1151 polish();
1152 m_contentHeightDirty = true;
1156void ListViewWithPageHeader::itemImplicitHeightChanged(QQuickItem *item)
1158 if (item == m_headerItem) {
1159 const qreal diff = m_headerItem->implicitHeight() - m_previousHeaderImplicitHeight;
1160 if (diff != 0) {
1161 adjustHeader(diff);
1162 m_previousHeaderImplicitHeight = m_headerItem->implicitHeight();
1163 layout();
1164 polish();
1165 m_contentHeightDirty = true;
1170void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
1172 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
1173 if (m_headerItemShownHeight > 0) {
1174 // If the header is shown because of the clip
1175 // Change its size
1176 m_headerItemShownHeight += heightDiff;
1177 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
1178 updateClipItem();
1179 adjustMinYExtent();
1180 Q_EMIT headerItemShownHeightChanged();
1181 } else {
1182 if (oldHeaderY + oldHeaderHeight > contentY()) {
1183 // If the header is shown because its position
1184 // Change its size
1185 ListItem *firstItem = m_visibleItems.first();
1186 firstItem->setY(firstItem->y() + heightDiff);
1187 layout();
1188 } else {
1189 // If the header is not on screen, just change the start of the list
1190 // so the viewport is not changed
1191 adjustMinYExtent();
1197void ListViewWithPageHeader::adjustMinYExtent()
1199 if (m_visibleItems.isEmpty() || contentHeight() < height()) {
1200 m_minYExtent = 0;
1201 } else {
1202 qreal nonCreatedHeight = 0;
1203 if (m_firstVisibleIndex != 0) {
1204 // Calculate the average height of items to estimate the position of the list start
1205 const int visibleItems = m_visibleItems.count();
1206 qreal visibleItemsHeight = 0;
1207 Q_FOREACH(ListItem *item, m_visibleItems) {
1208 visibleItemsHeight += item->height();
1210 nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
1211// qDebug() << m_firstVisibleIndex << visibleItemsHeight << visibleItems << nonCreatedHeight;
1213 const qreal headerHeight = (m_headerItem ? m_headerItem->implicitHeight() : 0);
1214 m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + headerHeight;
1215 if (m_minYExtent != 0 && qFuzzyIsNull(m_minYExtent)) {
1216 m_minYExtent = 0;
1217 m_visibleItems.first()->setY(nonCreatedHeight - m_clipItem->y() + headerHeight);
1222ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
1224 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
1225 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
1226 return m_visibleItems[visibleIndexedModelIndex];
1228 return nullptr;
1231void ListViewWithPageHeader::layout()
1233 if (m_inLayout)
1234 return;
1236 m_inLayout = true;
1237 if (!m_visibleItems.isEmpty()) {
1238 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
1239 const qreal visibleTo = contentY() + height() - m_clipItem->y();
1241 qreal pos = m_visibleItems.first()->y();
1243// qDebug() << "ListViewWithPageHeader::layout Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
1244 int firstReallyVisibleItem = -1;
1245 int modelIndex = m_firstVisibleIndex;
1246 Q_FOREACH(ListItem *item, m_visibleItems) {
1247 const bool cull = pos + item->height() <= visibleFrom || pos >= visibleTo;
1248 item->setCulled(cull);
1249 item->setY(pos);
1250 if (!cull && firstReallyVisibleItem == -1) {
1251 firstReallyVisibleItem = modelIndex;
1252 if (m_topSectionItem) {
1253 // Positing the top section sticky item is a two step process
1254 // First we set it either we cull it (because it doesn't need to be sticked to the top)
1255 // or stick it to the top
1256 // Then after the loop we'll make sure that if there's another section just below it
1257 // pushed the sticky section up to make it disappear
1258 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
1259 bool showStickySectionItem;
1260 // We need to show the "top section sticky item" when the position at the "top" of the
1261 // viewport is bigger than the start of the position of the first visible item
1262 // i.e. the first visible item starts before the viewport, or when the first
1263 // visible item starts just at the viewport start and it does not have its own section item
1264 if (topSectionStickPos > pos) {
1265 showStickySectionItem = true;
1266 } else if (topSectionStickPos == pos) {
1267 showStickySectionItem = !item->sectionItem();
1268 } else {
1269 showStickySectionItem = false;
1271 if (!showStickySectionItem) {
1272 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
1273 if (item->sectionItem()) {
1274 // This seems it should happen since why would we cull the top section
1275 // if the first visible item has no section header? This only happens briefly
1276 // when increasing the height of a list that is at the bottom, the m_topSectionItem
1277 // gets shown shortly in the next polish call
1278 QQuickItemPrivate::get(item->sectionItem())->setCulled(false);
1280 } else {
1281 // Update the top sticky section header
1282 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1283 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
1284 context->setContextProperty(QStringLiteral("section"), 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 context->setContextProperty(QStringLiteral("delegateIndex"), delegateIndex);
1297 if (item->sectionItem()) {
1298 QQuickItemPrivate::get(item->sectionItem())->setCulled(true);
1303 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
1304 const qreal clipFrom = visibleFrom + (!item->sectionItem() && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
1305 if (!cull && pos < clipFrom) {
1306 context->setContextProperty(QStringLiteral("heightToClip"), clipFrom - pos);
1307 } else {
1308 context->setContextProperty(QStringLiteral("heightToClip"), QVariant::fromValue<int>(0));
1310// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
1311 pos += item->height();
1312 ++modelIndex;
1315 // Second step of section sticky item positioning
1316 // Look at the next section header, check if it's pushing up the sticky one
1317 if (m_topSectionItem) {
1318 if (firstReallyVisibleItem >= 0) {
1319 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
1320 ListItem *item = m_visibleItems[i];
1321 if (item->sectionItem()) {
1322 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
1323 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
1325 break;
1331 m_inLayout = false;
1334void ListViewWithPageHeader::updatePolish()
1336 // Check we are not being taken down and don't paint anything
1337 // TODO Check if we still need this in 5.2
1338 // For reproduction just inifnite loop testDash or testDashContent
1339 if (!QQmlEngine::contextForObject(this)->parentContext())
1340 return;
1342 Q_FOREACH(ListItem *item, m_itemsToRelease)
1343 reallyReleaseItem(item);
1344 m_itemsToRelease.clear();
1346 if (!model())
1347 return;
1349 layout();
1351 refill();
1353 if (m_contentHeightDirty) {
1354 qreal contentHeight;
1355 if (m_visibleItems.isEmpty()) {
1356 contentHeight = m_headerItem ? m_headerItem->height() : 0;
1357 } else {
1358 const int modelCount = model()->rowCount();
1359 const int visibleItems = m_visibleItems.count();
1360 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
1361 qreal nonCreatedHeight = 0;
1362 if (lastValidIndex != modelCount - 1) {
1363 const int visibleItems = m_visibleItems.count();
1364 qreal visibleItemsHeight = 0;
1365 Q_FOREACH(ListItem *item, m_visibleItems) {
1366 visibleItemsHeight += item->height();
1368 const int unknownSizes = modelCount - (m_firstVisibleIndex + visibleItems);
1369 nonCreatedHeight = unknownSizes * visibleItemsHeight / visibleItems;
1371 ListItem *item = m_visibleItems.last();
1372 contentHeight = nonCreatedHeight + item->y() + item->height() + m_clipItem->y();
1373 if (m_firstVisibleIndex != 0) {
1374 // Make sure that if we are shrinking we tell the view we still fit
1375 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
1379 m_contentHeightDirty = false;
1380 adjustMinYExtent();
1381 if (contentHeight < height()) {
1382 // need this since in the previous call to adjustMinYExtent contentHeight is not set yet
1383 m_minYExtent = 0;
1385 m_inContentHeightKeepHeaderShown = m_headerItem && m_headerItem->y() == contentY();
1386 setContentHeight(contentHeight);
1387 m_inContentHeightKeepHeaderShown = false;
1391#include "moc_listviewwithpageheader.cpp"