on 2492425 help sign in
unity8/plugins/DashViews/listviewwithpageheader.cpp
1329 lines 53 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#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
101#include <private/qquickvisualdatamodel_p.h>
102#else
103#include <private/qqmldelegatemodel_p.h>
104#endif
105#include <private/qqmlglobal_p.h>
106#include <private/qquickitem_p.h>
107#include <private/qquickanimation_p.h>
108#pragma GCC diagnostic pop
109// #include <private/qquickrectangle_p.h>
111static const qreal bufferRatio = 0.5;
113qreal ListViewWithPageHeader::ListItem::height() const
115 return m_item->height() + (m_sectionItem ? m_sectionItem->height() : 0);
118qreal ListViewWithPageHeader::ListItem::y() const
120 return m_item->y() - (m_sectionItem ? m_sectionItem->height() : 0);
123void ListViewWithPageHeader::ListItem::setY(qreal newY)
125 if (m_sectionItem) {
126 m_sectionItem->setY(newY);
127 m_item->setY(newY + m_sectionItem->height());
128 } else {
129 m_item->setY(newY);
130 }
133bool ListViewWithPageHeader::ListItem::culled() const
135 return QQuickItemPrivate::get(m_item)->culled;
138void ListViewWithPageHeader::ListItem::setCulled(bool culled)
140 QQuickItemPrivate::get(m_item)->setCulled(culled);
141 if (m_sectionItem)
142 QQuickItemPrivate::get(m_sectionItem)->setCulled(culled);
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)
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("contentY");
168 m_contentYAnimation->setDuration(200);
169 m_contentYAnimation->setTargetObject(this);
171 connect(this, SIGNAL(contentWidthChanged()), this, SLOT(onContentWidthChanged()));
172 connect(this, SIGNAL(contentHeightChanged()), this, SLOT(onContentHeightChanged()));
173 connect(this, SIGNAL(heightChanged()), this, SLOT(onHeightChanged()));
174 connect(m_contentYAnimation, SIGNAL(stopped()), this, SLOT(onShowHeaderAnimationFinished()));
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#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
195 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
196 }
197 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
198 connect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
199#else
200 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
201 }
202 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
203 connect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
204#endif
205 Q_EMIT modelChanged();
206 polish();
207 // TODO?
208// Q_EMIT contentHeightChanged();
209// Q_EMIT contentYChanged();
210 }
213QQmlComponent *ListViewWithPageHeader::delegate() const
215 return m_delegateModel ? m_delegateModel->delegate() : nullptr;
218void ListViewWithPageHeader::setDelegate(QQmlComponent *delegate)
220 if (delegate != this->delegate()) {
221 if (!m_delegateModel) {
222 createDelegateModel();
223 }
225 // Cleanup the existing items
226 Q_FOREACH(ListItem *item, m_visibleItems)
227 releaseItem(item);
228 m_visibleItems.clear();
229 m_firstVisibleIndex = -1;
230 adjustMinYExtent();
231 setContentY(0);
232 m_clipItem->setY(0);
233 if (m_topSectionItem) {
234 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
235 }
237 m_delegateModel->setDelegate(delegate);
239 Q_EMIT delegateChanged();
240 m_delegateValidated = false;
241 m_contentHeightDirty = true;
242 polish();
243 }
246QQuickItem *ListViewWithPageHeader::header() const
248 return m_headerItem;
251void ListViewWithPageHeader::setHeader(QQuickItem *headerItem)
253 if (m_headerItem != headerItem) {
254 qreal oldHeaderHeight = 0;
255 qreal oldHeaderY = 0;
256 if (m_headerItem) {
257 oldHeaderHeight = m_headerItem->height();
258 oldHeaderY = m_headerItem->y();
259 m_headerItem->setParentItem(nullptr);
260 }
261 m_headerItem = headerItem;
262 if (m_headerItem) {
263 m_headerItem->setParentItem(contentItem());
264 m_headerItem->setZ(1);
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, SIGNAL(heightChanged()), SIGNAL(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;
337void ListViewWithPageHeader::positionAtBeginning()
339 if (m_delegateModel->count() <= 0)
340 return;
342 qreal headerHeight = (m_headerItem ? m_headerItem->height() : 0);
343 if (m_firstVisibleIndex != 0) {
344 // TODO This could be optimized by trying to reuse the interesection
345 // of items that may end up intersecting between the existing
346 // m_visibleItems and the items we are creating in the next loop
347 Q_FOREACH(ListItem *item, m_visibleItems)
348 releaseItem(item);
349 m_visibleItems.clear();
350 m_firstVisibleIndex = -1;
352 // Create the item 0, it will be already correctly positioned at createItem()
353 m_clipItem->setY(0);
354 ListItem *item = createItem(0, false);
355 // Create the subsequent items
356 int modelIndex = 1;
357 qreal pos = item->y() + item->height();
358 const qreal buffer = height() * bufferRatio;
359 const qreal bufferTo = height() + buffer;
360 while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
361 if (!(item = createItem(modelIndex, false)))
362 break;
363 pos += item->height();
364 ++modelIndex;
365 }
367 m_previousContentY = m_visibleItems.first()->y() - headerHeight;
368 }
369 setContentY(m_visibleItems.first()->y() + m_clipItem->y() - headerHeight);
370 if (m_headerItem) {
371 // TODO This should not be needed and the code that adjust the m_headerItem position
372 // in viewportMoved() should be enough but in some cases we have not found a way to reproduce
373 // yet the code of viewportMoved() fails so here we make sure that at least if we are calling
374 // positionAtBeginning the header item will be correctly positioned
375 m_headerItem->setY(-m_minYExtent);
376 }
379static inline bool uFuzzyCompare(qreal r1, qreal r2)
381 return qFuzzyCompare(r1, r2) || (qFuzzyIsNull(r1) && qFuzzyIsNull(r2));
384void ListViewWithPageHeader::showHeader()
386 if (!m_headerItem)
387 return;
389 const auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
390 if (!uFuzzyCompare(to, contentY())) {
391 const bool headerShownByItsOwn = contentY() < m_headerItem->y() + m_headerItem->height();
392 if (headerShownByItsOwn && m_headerItemShownHeight == 0) {
393 // We are not clipping since we are just at the top of the viewport
394 // but because of the showHeader animation we will need to, so
395 // enable the clipping without logically moving the items
396 m_headerItemShownHeight = m_headerItem->y() + m_headerItem->height() - contentY();
397 if (!m_visibleItems.isEmpty()) {
398 updateClipItem();
399 ListItem *firstItem = m_visibleItems.first();
400 firstItem->setY(firstItem->y() - m_headerItemShownHeight);
401 layout();
402 }
403 }
404 m_contentYAnimation->setTo(to);
405 contentYAnimationType = ContentYAnimationShowHeader;
406 m_contentYAnimation->start();
407 }
410QQuickItem *ListViewWithPageHeader::item(int modelIndex) const
412 ListItem *item = itemAtIndex(modelIndex);
413 if (item)
414 return item->m_item;
415 else
416 return nullptr;
419bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex)
421 ListItem *listItem = itemAtIndex(modelIndex);
422 if (listItem) {
423 return maximizeVisibleArea(listItem, listItem->height());
424 }
426 return false;
429bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex, int itemHeight)
431 if (itemHeight < 0)
432 return false;
434 ListItem *listItem = itemAtIndex(modelIndex);
435 if (listItem) {
436 return maximizeVisibleArea(listItem, itemHeight + (listItem->m_sectionItem ? listItem->m_sectionItem->height() : 0));
437 }
439 return false;
442bool ListViewWithPageHeader::maximizeVisibleArea(ListItem *listItem, int listItemHeight)
444 if (listItem) {
445 layout();
446 const auto listItemY = m_clipItem->y() + listItem->y();
447 if (listItemY > contentY() && listItemY + listItemHeight > contentY() + height()) {
448 // we can scroll the list up to show more stuff
449 const auto to = qMin(listItemY, listItemY + listItemHeight - height());
450 m_contentYAnimation->setTo(to);
451 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
452 m_contentYAnimation->start();
453 } else if ((listItemY < contentY() && listItemY + listItemHeight < contentY() + height()) ||
454 (m_topSectionItem && !listItem->m_sectionItem && listItemY - m_topSectionItem->height() < contentY() && listItemY + listItemHeight < contentY() + height()))
455 {
456 // we can scroll the list down to show more stuff
457 auto realVisibleListItemY = listItemY;
458 if (m_topSectionItem) {
459 // If we are showing the top section sticky item and this item doesn't have a section
460 // item we have to make sure to scroll it a bit more so that it is not underlapping
461 // the top section sticky item
462 bool topSectionShown = !QQuickItemPrivate::get(m_topSectionItem)->culled;
463 if (topSectionShown && !listItem->m_sectionItem) {
464 realVisibleListItemY -= m_topSectionItem->height();
465 }
466 }
467 const auto to = qMax(realVisibleListItemY, listItemY + listItemHeight - height());
468 m_contentYAnimation->setTo(to);
469 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
470 m_contentYAnimation->start();
471 }
472 return true;
473 }
474 return false;
477qreal ListViewWithPageHeader::minYExtent() const
479// qDebug() << "ListViewWithPageHeader::minYExtent" << m_minYExtent;
480 return m_minYExtent;
483void ListViewWithPageHeader::componentComplete()
485 if (m_delegateModel)
486 m_delegateModel->componentComplete();
488 QQuickFlickable::componentComplete();
490 polish();
493void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
495 // Check we are not being taken down and don't paint anything
496 // TODO Check if we still need this in 5.2
497 // For reproduction just inifnite loop testDash or testDashContent
498 if (!QQmlEngine::contextForObject(this)->parentContext())
499 return;
501 QQuickFlickable::viewportMoved(orient);
502// qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
503 qreal diff = m_previousContentY - contentY();
504 const bool showHeaderAnimationRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationShowHeader;
505 if (m_headerItem) {
506 const auto oldHeaderItemShownHeight = m_headerItemShownHeight;
507 if (uFuzzyCompare(contentY(), -m_minYExtent) || contentY() > -m_minYExtent) {
508 m_headerItem->setHeight(m_headerItem->implicitHeight());
509 // We are going down (but it's not because of the rebound at the end)
510 // (but the header was not shown by it's own position)
511 // or the header is partially shown and we are not doing a maximizeVisibleArea either
512 const bool scrolledUp = m_previousContentY > contentY();
513 const bool notRebounding = contentY() + height() < contentHeight();
514 const bool notShownByItsOwn = contentY() + diff >= m_headerItem->y() + m_headerItem->height();
515 const bool maximizeVisibleAreaRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationMaximizeVisibleArea;
517 if (!scrolledUp && (contentY() == -m_minYExtent || (m_headerItemShownHeight == 0 && m_previousContentY == m_headerItem->y()))) {
518 m_headerItemShownHeight = 0;
519 m_headerItem->setY(-m_minYExtent);
520 } else if ((scrolledUp && notRebounding && notShownByItsOwn && !maximizeVisibleAreaRunning) || (m_headerItemShownHeight > 0) || m_inContentHeightKeepHeaderShown) {
521 if (maximizeVisibleAreaRunning && diff > 0) {
522 // If we are maximizing and the header was shown, make sure we hide it
523 m_headerItemShownHeight -= diff;
524 } else {
525 m_headerItemShownHeight += diff;
526 }
527 if (uFuzzyCompare(contentY(), -m_minYExtent)) {
528 m_headerItemShownHeight = 0;
529 } else {
530 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
531 }
532 if (m_headerItemShownHeight > 0) {
533 if (uFuzzyCompare(m_headerItem->height(), m_headerItemShownHeight)) {
534 m_headerItem->setY(contentY());
535 m_headerItemShownHeight = m_headerItem->height();
536 } else {
537 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
538 }
539 } else {
540 m_headerItem->setY(-m_minYExtent);
541 }
542 }
543 } else {
544 // Stick the header item to the top when dragging down
545 m_headerItem->setY(contentY());
546 m_headerItem->setHeight(m_headerItem->implicitHeight() + (-m_minYExtent - contentY()));
547 }
548 // We will be changing the clip item, need to accomadate for it
549 // otherwise we move the firstItem down/up twice (unless the
550 // show header animation is running, where we want to keep the viewport stable)
551 if (!showHeaderAnimationRunning) {
552 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
553 } else {
554 diff = -diff;
555 }
556 }
557 if (!m_visibleItems.isEmpty()) {
558 updateClipItem();
559 ListItem *firstItem = m_visibleItems.first();
560 firstItem->setY(firstItem->y() + diff);
561 if (showHeaderAnimationRunning) {
562 adjustMinYExtent();
563 }
564 }
566 m_previousContentY = contentY();
567 layout();
568 polish();
571void ListViewWithPageHeader::createDelegateModel()
573#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
574 m_delegateModel = new QQuickVisualDataModel(qmlContext(this), this);
575 connect(m_delegateModel, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(itemCreated(int,QQuickItem*)));
576#else
577 m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
578 connect(m_delegateModel, SIGNAL(createdItem(int,QObject*)), this, SLOT(itemCreated(int,QObject*)));
579#endif
580 if (isComponentComplete())
581 m_delegateModel->componentComplete();
582 updateWatchedRoles();
585void ListViewWithPageHeader::refill()
587 if (m_inLayout) {
588 return;
589 }
590 if (!isComponentComplete()) {
591 return;
592 }
594 const qreal buffer = height() * bufferRatio;
595 const qreal from = contentY();
596 const qreal to = from + height();
597 const qreal bufferFrom = from - buffer;
598 const qreal bufferTo = to + buffer;
600 bool added = addVisibleItems(from, to, false);
601 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
602 added |= addVisibleItems(bufferFrom, bufferTo, true);
604 if (added || removed) {
605 m_contentHeightDirty = true;
606 }
609bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
611 if (!delegate())
612 return false;
614 if (m_delegateModel->count() == 0)
615 return false;
617 ListItem *item;
618// qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
620 int modelIndex = 0;
621 qreal pos = 0;
622 if (!m_visibleItems.isEmpty()) {
623 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
624 item = m_visibleItems.last();
625 pos = item->y() + item->height() + m_clipItem->y();
626 }
627 bool changed = false;
628// qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
629 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
630// qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
631 if (!(item = createItem(modelIndex, asynchronous)))
632 break;
633 pos += item->height();
634 ++modelIndex;
635 changed = true;
636 }
638 modelIndex = 0;
639 pos = 0;
640 if (!m_visibleItems.isEmpty()) {
641 modelIndex = m_firstVisibleIndex - 1;
642 item = m_visibleItems.first();
643 pos = item->y() + m_clipItem->y();
644 }
645 while (modelIndex >= 0 && pos > fillFrom) {
646// qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
647 if (!(item = createItem(modelIndex, asynchronous)))
648 break;
649 pos -= item->height();
650 --modelIndex;
651 changed = true;
652 }
654 return changed;
657void ListViewWithPageHeader::reallyReleaseItem(ListItem *listItem)
659 QQuickItem *item = listItem->m_item;
660#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
661 QQuickVisualModel::ReleaseFlags flags = m_delegateModel->release(item);
662 if (flags & QQuickVisualModel::Destroyed) {
663#else
664 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
665 if (flags & QQmlDelegateModel::Destroyed) {
666#endif
667 item->setParentItem(nullptr);
668 }
669 delete listItem->m_sectionItem;
670 delete listItem;
673void ListViewWithPageHeader::releaseItem(ListItem *listItem)
675 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(listItem->m_item);
676 itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
677 m_itemsToRelease << listItem;
680void ListViewWithPageHeader::updateWatchedRoles()
682 if (m_delegateModel) {
683 QList<QByteArray> roles;
684 if (!m_sectionProperty.isEmpty())
685 roles << m_sectionProperty.toUtf8();
686 m_delegateModel->setWatchedRoles(roles);
687 }
690QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
692 if (!m_sectionDelegate)
693 return nullptr;
695 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
696 if (modelIndex > 0) {
697 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
698 if (section == prevSection)
699 return nullptr;
700 }
701 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
702 // Already inserted items can't steal next section header
703 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
704 if (section == nextSection) {
705 // Steal the section header
706 ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
707 if (nextItem) {
708 QQuickItem *sectionItem = nextItem->m_sectionItem;
709 nextItem->m_sectionItem = nullptr;
710 return sectionItem;
711 }
712 }
713 }
715 return getSectionItem(section);
718QQuickItem *ListViewWithPageHeader::getSectionItem(const QString &sectionText)
720 QQuickItem *sectionItem = nullptr;
722 QQmlContext *creationContext = m_sectionDelegate->creationContext();
723 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
724 context->setContextProperty(QLatin1String("section"), sectionText);
725 context->setContextProperty(QLatin1String("delegateIndex"), -1);
726 QObject *nobj = m_sectionDelegate->beginCreate(context);
727 if (nobj) {
728 QQml_setParent_noEvent(context, nobj);
729 sectionItem = qobject_cast<QQuickItem *>(nobj);
730 if (!sectionItem) {
731 delete nobj;
732 } else {
733 sectionItem->setZ(2);
734 QQml_setParent_noEvent(sectionItem, m_clipItem);
735 sectionItem->setParentItem(m_clipItem);
736 }
737 } else {
738 delete context;
739 }
740 m_sectionDelegate->completeCreate();
742 // TODO attach to sectionItem so we can accomodate to it changing its height
744 return sectionItem;
747bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
749// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
750 // Do not remove items if we are overshooting up or down, since we'll come back
751 // to the "stable" position and delete/create items without any reason
752 if (contentY() < -m_minYExtent) {
753 return false;
754 } else if (contentY() + height() > contentHeight()) {
755 return false;
756 }
757 bool changed = false;
759 bool foundVisible = false;
760 int i = 0;
761 int removedItems = 0;
762 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
763 while (i < m_visibleItems.count()) {
764 ListItem *item = m_visibleItems[i];
765 const qreal pos = item->y() + m_clipItem->y();
766// qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
767 if (pos + item->height() < bufferFrom || pos > bufferTo) {
768// qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
769 releaseItem(item);
770 m_visibleItems.removeAt(i);
771 changed = true;
772 ++removedItems;
773 } else {
774 if (!foundVisible) {
775 foundVisible = true;
776 const int itemIndex = m_firstVisibleIndex + removedItems + i;
777 m_firstVisibleIndex = itemIndex;
778 }
779 ++i;
780 }
781 }
782 if (!foundVisible) {
783 m_firstVisibleIndex = -1;
784 }
785 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
786 adjustMinYExtent();
787 }
789 return changed;
792ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
794// qDebug() << "CREATE ITEM" << modelIndex;
795 if (asynchronous && m_asyncRequestedIndex != -1)
796 return nullptr;
798 m_asyncRequestedIndex = -1;
799#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
800 QQuickItem *item = m_delegateModel->item(modelIndex, asynchronous);
801#else
802 QObject* object = m_delegateModel->object(modelIndex, asynchronous);
803 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
804#endif
805 if (!item) {
806#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
807 m_asyncRequestedIndex = modelIndex;
808#else
809 if (object) {
810 m_delegateModel->release(object);
811 if (!m_delegateValidated) {
812 m_delegateValidated = true;
813 QObject* delegateObj = delegate();
814 qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
815 }
816 } else {
817 m_asyncRequestedIndex = modelIndex;
818 }
819#endif
820 return 0;
821 } else {
822// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
823 ListItem *listItem = new ListItem;
824 listItem->m_item = item;
825 listItem->m_sectionItem = getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/);
826 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
827 ListItem *prevItem = itemAtIndex(modelIndex - 1);
828 bool lostItem = false; // Is an item that we requested async but because of model changes
829 // it is no longer attached to any of the existing items (has no prev nor next item)
830 // nor is the first item
831 if (prevItem) {
832 listItem->setY(prevItem->y() + prevItem->height());
833 } else {
834 ListItem *currItem = itemAtIndex(modelIndex);
835 if (currItem) {
836 // There's something already in m_visibleItems at out index, meaning this is an insert, so attach to its top
837 listItem->setY(currItem->y() - listItem->height());
838 } else {
839 ListItem *nextItem = itemAtIndex(modelIndex + 1);
840 if (nextItem) {
841 listItem->setY(nextItem->y() - listItem->height());
842 } else if (modelIndex == 0 && m_headerItem) {
843 listItem->setY(m_headerItem->height());
844 } else if (!m_visibleItems.isEmpty()) {
845 lostItem = true;
846 }
847 }
848 }
849 if (lostItem) {
850 listItem->setCulled(true);
851 releaseItem(listItem);
852 listItem = nullptr;
853 } else {
854 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() <= contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
855 if (m_visibleItems.isEmpty()) {
856 m_visibleItems << listItem;
857 } else {
858 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
859 }
860 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
861 m_firstVisibleIndex = modelIndex;
862 polish();
863 }
864 if (listItem->m_sectionItem) {
865 QQmlContext *context = QQmlEngine::contextForObject(listItem->m_sectionItem)->parentContext();
866 context->setContextProperty(QLatin1String("delegateIndex"), modelIndex);
867 }
868 adjustMinYExtent();
869 m_contentHeightDirty = true;
870 }
871 return listItem;
872 }
875#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
876void ListViewWithPageHeader::itemCreated(int modelIndex, QQuickItem *item)
878#else
879void ListViewWithPageHeader::itemCreated(int modelIndex, QObject *object)
881 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
882 if (!item) {
883 qWarning() << "ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
884 return;
885 }
886#endif
887// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
888 // Check we are not being taken down and don't paint anything
889 // TODO Check if we still need this in 5.2
890 // For reproduction just inifnite loop testDash or testDashContent
891 if (!QQmlEngine::contextForObject(this)->parentContext())
892 return;
894 item->setParentItem(m_clipItem);
895 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
896 context->setContextProperty(QLatin1String("ListViewWithPageHeader"), this);
897 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
898 if (modelIndex == m_asyncRequestedIndex) {
899 createItem(modelIndex, false);
900 refill();
901 }
904void ListViewWithPageHeader::updateClipItem()
906 m_clipItem->setHeight(height() - m_headerItemShownHeight);
907 m_clipItem->setY(contentY() + m_headerItemShownHeight);
908 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
911void ListViewWithPageHeader::onContentHeightChanged()
913 updateClipItem();
916void ListViewWithPageHeader::onContentWidthChanged()
918 m_clipItem->setWidth(contentItem()->width());
921void ListViewWithPageHeader::onHeightChanged()
923 polish();
927#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
928void ListViewWithPageHeader::onModelUpdated(const QQuickChangeSet &changeSet, bool /*reset*/)
929#else
930void ListViewWithPageHeader::onModelUpdated(const QQmlChangeSet &changeSet, bool /*reset*/)
931#endif
933 // TODO Do something with reset
934// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
935 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
937#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
938 Q_FOREACH(const QQuickChangeSet::Remove &remove, changeSet.removes()) {
939#else
940 Q_FOREACH(const QQmlChangeSet::Remove &remove, changeSet.removes()) {
941#endif
942// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove.index << remove.count;
943 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
944 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
945 // If all the items we are removing are either not created or culled
946 // we have to grow down to avoid viewport changing
947 bool growDown = true;
948 for (int i = 0; growDown && i < remove.count; ++i) {
949 const int modelIndex = remove.index + i;
950 ListItem *item = itemAtIndex(modelIndex);
951 if (item && !item->culled()) {
952 growDown = false;
953 }
954 }
955 for (int i = remove.count - 1; i >= 0; --i) {
956 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
957 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
958 ListItem *item = m_visibleItems[visibleIndex];
959 // Pass the section item down if needed
960 if (item->m_sectionItem && visibleIndex + 1 < m_visibleItems.count()) {
961 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
962 if (!nextItem->m_sectionItem) {
963 nextItem->m_sectionItem = item->m_sectionItem;
964 item->m_sectionItem = nullptr;
965 }
966 }
967 releaseItem(item);
968 m_visibleItems.removeAt(visibleIndex);
969 }
970 }
971 if (growDown) {
972 adjustMinYExtent();
973 } else if (remove.index <= m_firstVisibleIndex) {
974 if (!m_visibleItems.isEmpty()) {
975 // We removed the first item that is the one that positions the rest
976 // position the new first item correctly
977 m_visibleItems.first()->setY(oldFirstValidIndexPos);
978 } else {
979 m_firstVisibleIndex = -1;
980 }
981 }
982 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
983 m_firstVisibleIndex -= remove.count;
984 }
985 for (int i = remove.count - 1; i >= 0; --i) {
986 const int modelIndex = remove.index + i;
987 if (modelIndex == m_asyncRequestedIndex) {
988 m_asyncRequestedIndex = -1;
989 } else if (modelIndex < m_asyncRequestedIndex) {
990 m_asyncRequestedIndex--;
991 }
992 }
993 }
995#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
996 Q_FOREACH(const QQuickChangeSet::Insert &insert, changeSet.inserts()) {
997#else
998 Q_FOREACH(const QQmlChangeSet::Insert &insert, changeSet.inserts()) {
999#endif
1000// qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
1001 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
1002 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
1003 if (insertingInValidIndexes || firstItemWithViewOnTop)
1005 // If the items we are adding won't be really visible
1006 // we grow up instead of down to not change the viewport
1007 bool growUp = false;
1008 if (!firstItemWithViewOnTop) {
1009 for (int i = 0; i < m_visibleItems.count(); ++i) {
1010 if (!m_visibleItems[i]->culled()) {
1011 if (insert.index <= m_firstVisibleIndex + i) {
1012 growUp = true;
1014 break;
1019 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
1020 for (int i = insert.count - 1; i >= 0; --i) {
1021 const int modelIndex = insert.index + i;
1022 ListItem *item = createItem(modelIndex, false);
1023 if (growUp) {
1024 ListItem *firstItem = m_visibleItems.first();
1025 firstItem->setY(firstItem->y() - item->height());
1027 // Adding an item may break a "same section" chain, so check
1028 // if we need adding a new section item
1029 if (m_sectionDelegate) {
1030 ListItem *nextItem = itemAtIndex(modelIndex + 1);
1031 if (nextItem && !nextItem->m_sectionItem) {
1032 nextItem->m_sectionItem = getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/);
1033 if (growUp && nextItem->m_sectionItem) {
1034 ListItem *firstItem = m_visibleItems.first();
1035 firstItem->setY(firstItem->y() - nextItem->m_sectionItem->height());
1040 if (firstItemWithViewOnTop) {
1041 ListItem *firstItem = m_visibleItems.first();
1042 firstItem->setY(oldFirstValidIndexPos);
1044 adjustMinYExtent();
1045 } else if (insert.index <= m_firstVisibleIndex) {
1046 m_firstVisibleIndex += insert.count;
1049 for (int i = insert.count - 1; i >= 0; --i) {
1050 const int modelIndex = insert.index + i;
1051 if (modelIndex <= m_asyncRequestedIndex) {
1052 m_asyncRequestedIndex++;
1057#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
1058 Q_FOREACH(const QQuickChangeSet::Change &change, changeSet.changes()) {
1059#else
1060 Q_FOREACH(const QQmlChangeSet::Change &change, changeSet.changes()) {
1061#endif
1062// qDebug() << "ListViewWithPageHeader::onModelUpdated Change" << change.index << change.count;
1063 for (int i = change.index; i < change.count; ++i) {
1064 ListItem *item = itemAtIndex(i);
1065 if (item && item->m_sectionItem) {
1066 QQmlContext *context = QQmlEngine::contextForObject(item->m_sectionItem)->parentContext();
1067 const QString sectionText = m_delegateModel->stringValue(i, m_sectionProperty);
1068 context->setContextProperty(QLatin1String("section"), sectionText);
1073 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
1074 adjustMinYExtent();
1077 for (int i = 0; i < m_visibleItems.count(); ++i) {
1078 ListItem *item = m_visibleItems[i];
1079 if (item->m_sectionItem) {
1080 QQmlContext *context = QQmlEngine::contextForObject(item->m_sectionItem)->parentContext();
1081 context->setContextProperty(QLatin1String("delegateIndex"), m_firstVisibleIndex + i);
1085 layout();
1086 polish();
1087 m_contentHeightDirty = true;
1090void ListViewWithPageHeader::onShowHeaderAnimationFinished()
1092 m_contentHeightDirty = true;
1093 polish();
1096void ListViewWithPageHeader::itemGeometryChanged(QQuickItem * /*item*/, const QRectF &newGeometry, const QRectF &oldGeometry)
1098 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
1099 if (heightDiff != 0) {
1100 if (!m_inContentHeightKeepHeaderShown && oldGeometry.y() + oldGeometry.height() + m_clipItem->y() <= contentY() && !m_visibleItems.isEmpty()) {
1101 ListItem *firstItem = m_visibleItems.first();
1102 firstItem->setY(firstItem->y() - heightDiff);
1103 adjustMinYExtent();
1104 layout();
1106 refill();
1107 adjustMinYExtent();
1108 polish();
1109 m_contentHeightDirty = true;
1113void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
1115 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
1116 if (m_headerItemShownHeight > 0) {
1117 // If the header is shown because of the clip
1118 // Change its size
1119 m_headerItemShownHeight += heightDiff;
1120 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
1121 updateClipItem();
1122 adjustMinYExtent();
1123 } else {
1124 if (oldHeaderY + oldHeaderHeight > contentY()) {
1125 // If the header is shown because its position
1126 // Change its size
1127 ListItem *firstItem = m_visibleItems.first();
1128 firstItem->setY(firstItem->y() + heightDiff);
1129 layout();
1130 } else {
1131 // If the header is not on screen, just change the start of the list
1132 // so the viewport is not changed
1133 adjustMinYExtent();
1139void ListViewWithPageHeader::adjustMinYExtent()
1141 if (m_visibleItems.isEmpty()) {
1142 m_minYExtent = 0;
1143 } else {
1144 qreal nonCreatedHeight = 0;
1145 if (m_firstVisibleIndex != 0) {
1146 // Calculate the average height of items to estimate the position of the list start
1147 const int visibleItems = m_visibleItems.count();
1148 qreal visibleItemsHeight = 0;
1149 Q_FOREACH(ListItem *item, m_visibleItems) {
1150 visibleItemsHeight += item->height();
1152 nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
1153// qDebug() << m_firstVisibleIndex << visibleItemsHeight << visibleItems << nonCreatedHeight;
1155 const qreal headerHeight = (m_headerItem ? m_headerItem->implicitHeight() : 0);
1156 m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + headerHeight;
1157 if (m_minYExtent != 0 && qFuzzyIsNull(m_minYExtent)) {
1158 m_minYExtent = 0;
1159 m_visibleItems.first()->setY(nonCreatedHeight - m_clipItem->y() + headerHeight);
1164ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
1166 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
1167 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
1168 return m_visibleItems[visibleIndexedModelIndex];
1170 return nullptr;
1173void ListViewWithPageHeader::layout()
1175 if (m_inLayout)
1176 return;
1178 m_inLayout = true;
1179 if (!m_visibleItems.isEmpty()) {
1180 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
1181 const qreal visibleTo = contentY() + height() - m_clipItem->y();
1183 qreal pos = m_visibleItems.first()->y();
1185// qDebug() << "ListViewWithPageHeader::layout Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
1186 int firstReallyVisibleItem = -1;
1187 int modelIndex = m_firstVisibleIndex;
1188 Q_FOREACH(ListItem *item, m_visibleItems) {
1189 const bool cull = pos + item->height() <= visibleFrom || pos >= visibleTo;
1190 item->setCulled(cull);
1191 item->setY(pos);
1192 if (!cull && firstReallyVisibleItem == -1) {
1193 firstReallyVisibleItem = modelIndex;
1194 if (m_topSectionItem) {
1195 // Positing the top section sticky item is a two step process
1196 // First we set it either we cull it (because it doesn't need to be sticked to the top)
1197 // or stick it to the top
1198 // Then after the loop we'll make sure that if there's another section just below it
1199 // pushed the sticky section up to make it disappear
1200 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
1201 bool showStickySectionItem;
1202 // We need to show the "top section sticky item" when the position at the "top" of the
1203 // viewport is bigger than the start of the position of the first visible item
1204 // i.e. the first visible item starts before the viewport, or when the first
1205 // visible item starts just at the viewport start and it does not have its own section item
1206 if (topSectionStickPos > pos) {
1207 showStickySectionItem = true;
1208 } else if (topSectionStickPos == pos) {
1209 showStickySectionItem = !item->m_sectionItem;
1210 } else {
1211 showStickySectionItem = false;
1213 if (!showStickySectionItem) {
1214 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
1215 if (item->m_sectionItem) {
1216 // This seems it should happen since why would we cull the top section
1217 // if the first visible item has no section header? This only happens briefly
1218 // when increasing the height of a list that is at the bottom, the m_topSectionItem
1219 // gets shown shortly in the next polish call
1220 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(false);
1222 } else {
1223 // Update the top sticky section header
1224 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1225 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
1226 context->setContextProperty(QLatin1String("section"), section);
1228 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
1229 m_topSectionItem->setY(topSectionStickPos);
1230 int delegateIndex = modelIndex;
1231 // Look for the first index with this section text
1232 while (delegateIndex > 0) {
1233 const QString prevSection = m_delegateModel->stringValue(delegateIndex - 1, m_sectionProperty);
1234 if (prevSection != section)
1235 break;
1236 delegateIndex--;
1238 context->setContextProperty(QLatin1String("delegateIndex"), delegateIndex);
1239 if (item->m_sectionItem) {
1240 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(true);
1245 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
1246 const qreal clipFrom = visibleFrom + (!item->m_sectionItem && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
1247 if (!cull && pos < clipFrom) {
1248 context->setContextProperty(QLatin1String("heightToClip"), clipFrom - pos);
1249 } else {
1250 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
1252// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
1253 pos += item->height();
1254 ++modelIndex;
1257 // Second step of section sticky item positioning
1258 // Look at the next section header, check if it's pushing up the sticky one
1259 if (m_topSectionItem) {
1260 if (firstReallyVisibleItem >= 0) {
1261 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
1262 ListItem *item = m_visibleItems[i];
1263 if (item->m_sectionItem) {
1264 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
1265 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
1267 break;
1273 m_inLayout = false;
1276void ListViewWithPageHeader::updatePolish()
1278 // Check we are not being taken down and don't paint anything
1279 // TODO Check if we still need this in 5.2
1280 // For reproduction just inifnite loop testDash or testDashContent
1281 if (!QQmlEngine::contextForObject(this)->parentContext())
1282 return;
1284 Q_FOREACH(ListItem *item, m_itemsToRelease)
1285 reallyReleaseItem(item);
1286 m_itemsToRelease.clear();
1288 if (!model())
1289 return;
1291 layout();
1293 refill();
1295 if (m_contentHeightDirty) {
1296 qreal contentHeight;
1297 if (m_visibleItems.isEmpty()) {
1298 contentHeight = m_headerItem ? m_headerItem->height() : 0;
1299 } else {
1300 const int modelCount = model()->rowCount();
1301 const int visibleItems = m_visibleItems.count();
1302 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
1303 qreal nonCreatedHeight = 0;
1304 if (lastValidIndex != modelCount - 1) {
1305 const int visibleItems = m_visibleItems.count();
1306 qreal visibleItemsHeight = 0;
1307 Q_FOREACH(ListItem *item, m_visibleItems) {
1308 visibleItemsHeight += item->height();
1310 const int unknownSizes = modelCount - (m_firstVisibleIndex + visibleItems);
1311 nonCreatedHeight = unknownSizes * visibleItemsHeight / visibleItems;
1313 ListItem *item = m_visibleItems.last();
1314 contentHeight = nonCreatedHeight + item->y() + item->height() + m_clipItem->y();
1315 if (m_firstVisibleIndex != 0) {
1316 // Make sure that if we are shrinking we tell the view we still fit
1317 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
1321 m_contentHeightDirty = false;
1322 adjustMinYExtent();
1323 m_inContentHeightKeepHeaderShown = m_headerItem && m_headerItem->y() == contentY();
1324 setContentHeight(contentHeight);
1325 m_inContentHeightKeepHeaderShown = false;
1329#include "moc_listviewwithpageheader.cpp"