on dda4fe1 help sign in
unity8/plugins/DashViews/listviewwithpageheader.cpp
1323 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);
372void ListViewWithPageHeader::showHeader()
374 if (!m_headerItem)
375 return;
377 auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
378 if (to != contentY()) {
379 const bool headerShownByItsOwn = contentY() < m_headerItem->y() + m_headerItem->height();
380 if (headerShownByItsOwn && m_headerItemShownHeight == 0) {
381 // We are not clipping since we are just at the top of the viewport
382 // but because of the showHeader animation we will need to, so
383 // enable the clipping without logically moving the items
384 m_headerItemShownHeight = m_headerItem->y() + m_headerItem->height() - contentY();
385 if (!m_visibleItems.isEmpty()) {
386 updateClipItem();
387 ListItem *firstItem = m_visibleItems.first();
388 firstItem->setY(firstItem->y() - m_headerItemShownHeight);
389 layout();
390 }
391 }
392 m_contentYAnimation->setTo(to);
393 contentYAnimationType = ContentYAnimationShowHeader;
394 m_contentYAnimation->start();
395 }
398QQuickItem *ListViewWithPageHeader::item(int modelIndex) const
400 ListItem *item = itemAtIndex(modelIndex);
401 if (item)
402 return item->m_item;
403 else
404 return nullptr;
407bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex)
409 ListItem *listItem = itemAtIndex(modelIndex);
410 if (listItem) {
411 return maximizeVisibleArea(listItem, listItem->height());
412 }
414 return false;
417bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex, int itemHeight)
419 if (itemHeight < 0)
420 return false;
422 ListItem *listItem = itemAtIndex(modelIndex);
423 if (listItem) {
424 return maximizeVisibleArea(listItem, itemHeight + (listItem->m_sectionItem ? listItem->m_sectionItem->height() : 0));
425 }
427 return false;
430bool ListViewWithPageHeader::maximizeVisibleArea(ListItem *listItem, int listItemHeight)
432 if (listItem) {
433 layout();
434 const auto listItemY = m_clipItem->y() + listItem->y();
435 if (listItemY > contentY() && listItemY + listItemHeight > contentY() + height()) {
436 // we can scroll the list up to show more stuff
437 const auto to = qMin(listItemY, listItemY + listItemHeight - height());
438 m_contentYAnimation->setTo(to);
439 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
440 m_contentYAnimation->start();
441 } else if ((listItemY < contentY() && listItemY + listItemHeight < contentY() + height()) ||
442 (m_topSectionItem && !listItem->m_sectionItem && listItemY - m_topSectionItem->height() < contentY() && listItemY + listItemHeight < contentY() + height()))
443 {
444 // we can scroll the list down to show more stuff
445 auto realVisibleListItemY = listItemY;
446 if (m_topSectionItem) {
447 // If we are showing the top section sticky item and this item doesn't have a section
448 // item we have to make sure to scroll it a bit more so that it is not underlapping
449 // the top section sticky item
450 bool topSectionShown = !QQuickItemPrivate::get(m_topSectionItem)->culled;
451 if (topSectionShown && !listItem->m_sectionItem) {
452 realVisibleListItemY -= m_topSectionItem->height();
453 }
454 }
455 const auto to = qMax(realVisibleListItemY, listItemY + listItemHeight - height());
456 m_contentYAnimation->setTo(to);
457 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
458 m_contentYAnimation->start();
459 }
460 return true;
461 }
462 return false;
465qreal ListViewWithPageHeader::minYExtent() const
467// qDebug() << "ListViewWithPageHeader::minYExtent" << m_minYExtent;
468 return m_minYExtent;
471void ListViewWithPageHeader::componentComplete()
473 if (m_delegateModel)
474 m_delegateModel->componentComplete();
476 QQuickFlickable::componentComplete();
478 polish();
481static inline bool uFuzzyCompare(qreal r1, qreal r2)
483 return qFuzzyCompare(r1, r2) || (qFuzzyIsNull(r1) && qFuzzyIsNull(r2));
486void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
488 // Check we are not being taken down and don't paint anything
489 // TODO Check if we still need this in 5.2
490 // For reproduction just inifnite loop testDash or testDashContent
491 if (!QQmlEngine::contextForObject(this)->parentContext())
492 return;
494 QQuickFlickable::viewportMoved(orient);
495// qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
496 qreal diff = m_previousContentY - contentY();
497 const bool showHeaderAnimationRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationShowHeader;
498 if (m_headerItem) {
499 const auto oldHeaderItemShownHeight = m_headerItemShownHeight;
500 if (uFuzzyCompare(contentY(), -m_minYExtent) || contentY() > -m_minYExtent) {
501 m_headerItem->setHeight(m_headerItem->implicitHeight());
502 // We are going down (but it's not because of the rebound at the end)
503 // (but the header was not shown by it's own position)
504 // or the header is partially shown and we are not doing a maximizeVisibleArea either
505 const bool scrolledUp = m_previousContentY > contentY();
506 const bool notRebounding = contentY() + height() < contentHeight();
507 const bool notShownByItsOwn = contentY() + diff >= m_headerItem->y() + m_headerItem->height();
508 const bool maximizeVisibleAreaRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationMaximizeVisibleArea;
510 if (!scrolledUp && (contentY() == -m_minYExtent || (m_headerItemShownHeight == 0 && m_previousContentY == m_headerItem->y()))) {
511 m_headerItemShownHeight = 0;
512 m_headerItem->setY(-m_minYExtent);
513 } else if ((scrolledUp && notRebounding && notShownByItsOwn && !maximizeVisibleAreaRunning) || (m_headerItemShownHeight > 0) || m_inContentHeightKeepHeaderShown) {
514 if (maximizeVisibleAreaRunning && diff > 0) {
515 // If we are maximizing and the header was shown, make sure we hide it
516 m_headerItemShownHeight -= diff;
517 } else {
518 m_headerItemShownHeight += diff;
519 }
520 if (uFuzzyCompare(contentY(), -m_minYExtent)) {
521 m_headerItemShownHeight = 0;
522 } else {
523 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
524 }
525 if (m_headerItemShownHeight > 0) {
526 if (uFuzzyCompare(m_headerItem->height(), m_headerItemShownHeight)) {
527 m_headerItem->setY(contentY());
528 m_headerItemShownHeight = m_headerItem->height();
529 } else {
530 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
531 }
532 } else {
533 m_headerItem->setY(-m_minYExtent);
534 }
535 }
536 } else {
537 // Stick the header item to the top when dragging down
538 m_headerItem->setY(contentY());
539 m_headerItem->setHeight(m_headerItem->implicitHeight() + (-m_minYExtent - contentY()));
540 }
541 // We will be changing the clip item, need to accomadate for it
542 // otherwise we move the firstItem down/up twice (unless the
543 // show header animation is running, where we want to keep the viewport stable)
544 if (!showHeaderAnimationRunning) {
545 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
546 } else {
547 diff = -diff;
548 }
549 }
550 if (!m_visibleItems.isEmpty()) {
551 updateClipItem();
552 ListItem *firstItem = m_visibleItems.first();
553 firstItem->setY(firstItem->y() + diff);
554 if (showHeaderAnimationRunning) {
555 adjustMinYExtent();
556 }
557 }
559 m_previousContentY = contentY();
560 layout();
561 polish();
564void ListViewWithPageHeader::createDelegateModel()
566#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
567 m_delegateModel = new QQuickVisualDataModel(qmlContext(this), this);
568 connect(m_delegateModel, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(itemCreated(int,QQuickItem*)));
569#else
570 m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
571 connect(m_delegateModel, SIGNAL(createdItem(int,QObject*)), this, SLOT(itemCreated(int,QObject*)));
572#endif
573 if (isComponentComplete())
574 m_delegateModel->componentComplete();
575 updateWatchedRoles();
578void ListViewWithPageHeader::refill()
580 if (m_inLayout) {
581 return;
582 }
583 if (!isComponentComplete()) {
584 return;
585 }
587 const qreal buffer = height() * bufferRatio;
588 const qreal from = contentY();
589 const qreal to = from + height();
590 const qreal bufferFrom = from - buffer;
591 const qreal bufferTo = to + buffer;
593 bool added = addVisibleItems(from, to, false);
594 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
595 added |= addVisibleItems(bufferFrom, bufferTo, true);
597 if (added || removed) {
598 m_contentHeightDirty = true;
599 }
602bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
604 if (!delegate())
605 return false;
607 if (m_delegateModel->count() == 0)
608 return false;
610 ListItem *item;
611// qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
613 int modelIndex = 0;
614 qreal pos = 0;
615 if (!m_visibleItems.isEmpty()) {
616 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
617 item = m_visibleItems.last();
618 pos = item->y() + item->height() + m_clipItem->y();
619 }
620 bool changed = false;
621// qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
622 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
623// qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
624 if (!(item = createItem(modelIndex, asynchronous)))
625 break;
626 pos += item->height();
627 ++modelIndex;
628 changed = true;
629 }
631 modelIndex = 0;
632 pos = 0;
633 if (!m_visibleItems.isEmpty()) {
634 modelIndex = m_firstVisibleIndex - 1;
635 item = m_visibleItems.first();
636 pos = item->y() + m_clipItem->y();
637 }
638 while (modelIndex >= 0 && pos > fillFrom) {
639// qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
640 if (!(item = createItem(modelIndex, asynchronous)))
641 break;
642 pos -= item->height();
643 --modelIndex;
644 changed = true;
645 }
647 return changed;
650void ListViewWithPageHeader::reallyReleaseItem(ListItem *listItem)
652 QQuickItem *item = listItem->m_item;
653#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
654 QQuickVisualModel::ReleaseFlags flags = m_delegateModel->release(item);
655 if (flags & QQuickVisualModel::Destroyed) {
656#else
657 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
658 if (flags & QQmlDelegateModel::Destroyed) {
659#endif
660 item->setParentItem(nullptr);
661 }
662 delete listItem->m_sectionItem;
663 delete listItem;
666void ListViewWithPageHeader::releaseItem(ListItem *listItem)
668 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(listItem->m_item);
669 itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
670 m_itemsToRelease << listItem;
673void ListViewWithPageHeader::updateWatchedRoles()
675 if (m_delegateModel) {
676 QList<QByteArray> roles;
677 if (!m_sectionProperty.isEmpty())
678 roles << m_sectionProperty.toUtf8();
679 m_delegateModel->setWatchedRoles(roles);
680 }
683QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
685 if (!m_sectionDelegate)
686 return nullptr;
688 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
689 if (modelIndex > 0) {
690 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
691 if (section == prevSection)
692 return nullptr;
693 }
694 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
695 // Already inserted items can't steal next section header
696 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
697 if (section == nextSection) {
698 // Steal the section header
699 ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
700 if (nextItem) {
701 QQuickItem *sectionItem = nextItem->m_sectionItem;
702 nextItem->m_sectionItem = nullptr;
703 return sectionItem;
704 }
705 }
706 }
708 return getSectionItem(section);
711QQuickItem *ListViewWithPageHeader::getSectionItem(const QString &sectionText)
713 QQuickItem *sectionItem = nullptr;
715 QQmlContext *creationContext = m_sectionDelegate->creationContext();
716 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
717 context->setContextProperty(QLatin1String("section"), sectionText);
718 context->setContextProperty(QLatin1String("delegateIndex"), -1);
719 QObject *nobj = m_sectionDelegate->beginCreate(context);
720 if (nobj) {
721 QQml_setParent_noEvent(context, nobj);
722 sectionItem = qobject_cast<QQuickItem *>(nobj);
723 if (!sectionItem) {
724 delete nobj;
725 } else {
726 sectionItem->setZ(2);
727 QQml_setParent_noEvent(sectionItem, m_clipItem);
728 sectionItem->setParentItem(m_clipItem);
729 }
730 } else {
731 delete context;
732 }
733 m_sectionDelegate->completeCreate();
735 // TODO attach to sectionItem so we can accomodate to it changing its height
737 return sectionItem;
740bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
742// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
743 // Do not remove items if we are overshooting up or down, since we'll come back
744 // to the "stable" position and delete/create items without any reason
745 if (contentY() < -m_minYExtent) {
746 return false;
747 } else if (contentY() + height() > contentHeight()) {
748 return false;
749 }
750 bool changed = false;
752 bool foundVisible = false;
753 int i = 0;
754 int removedItems = 0;
755 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
756 while (i < m_visibleItems.count()) {
757 ListItem *item = m_visibleItems[i];
758 const qreal pos = item->y() + m_clipItem->y();
759// qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
760 if (pos + item->height() < bufferFrom || pos > bufferTo) {
761// qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
762 releaseItem(item);
763 m_visibleItems.removeAt(i);
764 changed = true;
765 ++removedItems;
766 } else {
767 if (!foundVisible) {
768 foundVisible = true;
769 const int itemIndex = m_firstVisibleIndex + removedItems + i;
770 m_firstVisibleIndex = itemIndex;
771 }
772 ++i;
773 }
774 }
775 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
776 adjustMinYExtent();
777 }
779 return changed;
782ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
784// qDebug() << "CREATE ITEM" << modelIndex;
785 if (asynchronous && m_asyncRequestedIndex != -1)
786 return nullptr;
788 m_asyncRequestedIndex = -1;
789#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
790 QQuickItem *item = m_delegateModel->item(modelIndex, asynchronous);
791#else
792 QObject* object = m_delegateModel->object(modelIndex, asynchronous);
793 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
794#endif
795 if (!item) {
796#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
797 m_asyncRequestedIndex = modelIndex;
798#else
799 if (object) {
800 m_delegateModel->release(object);
801 if (!m_delegateValidated) {
802 m_delegateValidated = true;
803 QObject* delegateObj = delegate();
804 qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
805 }
806 } else {
807 m_asyncRequestedIndex = modelIndex;
808 }
809#endif
810 return 0;
811 } else {
812// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
813 ListItem *listItem = new ListItem;
814 listItem->m_item = item;
815 listItem->m_sectionItem = getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/);
816 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
817 ListItem *prevItem = itemAtIndex(modelIndex - 1);
818 bool lostItem = false; // Is an item that we requested async but because of model changes
819 // it is no longer attached to any of the existing items (has no prev nor next item)
820 // nor is the first item
821 if (prevItem) {
822 listItem->setY(prevItem->y() + prevItem->height());
823 } else {
824 ListItem *currItem = itemAtIndex(modelIndex);
825 if (currItem) {
826 // There's something already in m_visibleItems at out index, meaning this is an insert, so attach to its top
827 listItem->setY(currItem->y() - listItem->height());
828 } else {
829 ListItem *nextItem = itemAtIndex(modelIndex + 1);
830 if (nextItem) {
831 listItem->setY(nextItem->y() - listItem->height());
832 } else if (modelIndex == 0 && m_headerItem) {
833 listItem->setY(m_headerItem->height());
834 } else if (!m_visibleItems.isEmpty()) {
835 lostItem = true;
836 }
837 }
838 }
839 if (lostItem) {
840 releaseItem(listItem);
841 listItem = nullptr;
842 } else {
843 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() <= contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
844 if (m_visibleItems.isEmpty()) {
845 m_visibleItems << listItem;
846 } else {
847 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
848 }
849 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
850 m_firstVisibleIndex = modelIndex;
851 polish();
852 }
853 if (listItem->m_sectionItem) {
854 QQmlContext *context = QQmlEngine::contextForObject(listItem->m_sectionItem)->parentContext();
855 context->setContextProperty(QLatin1String("delegateIndex"), modelIndex);
856 }
857 adjustMinYExtent();
858 m_contentHeightDirty = true;
859 }
860 return listItem;
861 }
864#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
865void ListViewWithPageHeader::itemCreated(int modelIndex, QQuickItem *item)
867#else
868void ListViewWithPageHeader::itemCreated(int modelIndex, QObject *object)
870 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
871 if (!item) {
872 qWarning() << "ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
873 return;
874 }
875#endif
876// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
877 // Check we are not being taken down and don't paint anything
878 // TODO Check if we still need this in 5.2
879 // For reproduction just inifnite loop testDash or testDashContent
880 if (!QQmlEngine::contextForObject(this)->parentContext())
881 return;
883 item->setParentItem(m_clipItem);
884 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
885 context->setContextProperty(QLatin1String("ListViewWithPageHeader"), this);
886 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
887 if (modelIndex == m_asyncRequestedIndex) {
888 createItem(modelIndex, false);
889 refill();
890 }
893void ListViewWithPageHeader::updateClipItem()
895 m_clipItem->setHeight(height() - m_headerItemShownHeight);
896 m_clipItem->setY(contentY() + m_headerItemShownHeight);
897 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
900void ListViewWithPageHeader::onContentHeightChanged()
902 updateClipItem();
905void ListViewWithPageHeader::onContentWidthChanged()
907 m_clipItem->setWidth(contentItem()->width());
910void ListViewWithPageHeader::onHeightChanged()
912 polish();
916#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
917void ListViewWithPageHeader::onModelUpdated(const QQuickChangeSet &changeSet, bool /*reset*/)
918#else
919void ListViewWithPageHeader::onModelUpdated(const QQmlChangeSet &changeSet, bool /*reset*/)
920#endif
922 // TODO Do something with reset
923// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
924 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
926#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
927 Q_FOREACH(const QQuickChangeSet::Remove &remove, changeSet.removes()) {
928#else
929 Q_FOREACH(const QQmlChangeSet::Remove &remove, changeSet.removes()) {
930#endif
931// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove.index << remove.count;
932 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
933 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
934 // If all the items we are removing are either not created or culled
935 // we have to grow down to avoid viewport changing
936 bool growDown = true;
937 for (int i = 0; growDown && i < remove.count; ++i) {
938 const int modelIndex = remove.index + i;
939 ListItem *item = itemAtIndex(modelIndex);
940 if (item && !item->culled()) {
941 growDown = false;
942 }
943 }
944 for (int i = remove.count - 1; i >= 0; --i) {
945 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
946 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
947 ListItem *item = m_visibleItems[visibleIndex];
948 // Pass the section item down if needed
949 if (item->m_sectionItem && visibleIndex + 1 < m_visibleItems.count()) {
950 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
951 if (!nextItem->m_sectionItem) {
952 nextItem->m_sectionItem = item->m_sectionItem;
953 item->m_sectionItem = nullptr;
954 }
955 }
956 releaseItem(item);
957 m_visibleItems.removeAt(visibleIndex);
958 }
959 }
960 if (growDown) {
961 adjustMinYExtent();
962 } else if (remove.index <= m_firstVisibleIndex) {
963 if (!m_visibleItems.isEmpty()) {
964 // We removed the first item that is the one that positions the rest
965 // position the new first item correctly
966 m_visibleItems.first()->setY(oldFirstValidIndexPos);
967 } else {
968 m_firstVisibleIndex = -1;
969 }
970 }
971 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
972 m_firstVisibleIndex -= remove.count;
973 }
974 for (int i = remove.count - 1; i >= 0; --i) {
975 const int modelIndex = remove.index + i;
976 if (modelIndex == m_asyncRequestedIndex) {
977 m_asyncRequestedIndex = -1;
978 } else if (modelIndex < m_asyncRequestedIndex) {
979 m_asyncRequestedIndex--;
980 }
981 }
982 }
984#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
985 Q_FOREACH(const QQuickChangeSet::Insert &insert, changeSet.inserts()) {
986#else
987 Q_FOREACH(const QQmlChangeSet::Insert &insert, changeSet.inserts()) {
988#endif
989// qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
990 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
991 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
992 if (insertingInValidIndexes || firstItemWithViewOnTop)
993 {
994 // If the items we are adding won't be really visible
995 // we grow up instead of down to not change the viewport
996 bool growUp = false;
997 if (!firstItemWithViewOnTop) {
998 for (int i = 0; i < m_visibleItems.count(); ++i) {
999 if (!m_visibleItems[i]->culled()) {
1000 if (insert.index <= m_firstVisibleIndex + i) {
1001 growUp = true;
1003 break;
1008 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
1009 for (int i = insert.count - 1; i >= 0; --i) {
1010 const int modelIndex = insert.index + i;
1011 ListItem *item = createItem(modelIndex, false);
1012 if (growUp) {
1013 ListItem *firstItem = m_visibleItems.first();
1014 firstItem->setY(firstItem->y() - item->height());
1016 // Adding an item may break a "same section" chain, so check
1017 // if we need adding a new section item
1018 if (m_sectionDelegate) {
1019 ListItem *nextItem = itemAtIndex(modelIndex + 1);
1020 if (nextItem && !nextItem->m_sectionItem) {
1021 nextItem->m_sectionItem = getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/);
1022 if (growUp && nextItem->m_sectionItem) {
1023 ListItem *firstItem = m_visibleItems.first();
1024 firstItem->setY(firstItem->y() - nextItem->m_sectionItem->height());
1029 if (firstItemWithViewOnTop) {
1030 ListItem *firstItem = m_visibleItems.first();
1031 firstItem->setY(oldFirstValidIndexPos);
1033 adjustMinYExtent();
1034 } else if (insert.index <= m_firstVisibleIndex) {
1035 m_firstVisibleIndex += insert.count;
1038 for (int i = insert.count - 1; i >= 0; --i) {
1039 const int modelIndex = insert.index + i;
1040 if (modelIndex <= m_asyncRequestedIndex) {
1041 m_asyncRequestedIndex++;
1046#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
1047 Q_FOREACH(const QQuickChangeSet::Change &change, changeSet.changes()) {
1048#else
1049 Q_FOREACH(const QQmlChangeSet::Change &change, changeSet.changes()) {
1050#endif
1051// qDebug() << "ListViewWithPageHeader::onModelUpdated Change" << change.index << change.count;
1052 for (int i = change.index; i < change.count; ++i) {
1053 ListItem *item = itemAtIndex(i);
1054 if (item && item->m_sectionItem) {
1055 QQmlContext *context = QQmlEngine::contextForObject(item->m_sectionItem)->parentContext();
1056 const QString sectionText = m_delegateModel->stringValue(i, m_sectionProperty);
1057 context->setContextProperty(QLatin1String("section"), sectionText);
1062 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
1063 adjustMinYExtent();
1066 for (int i = 0; i < m_visibleItems.count(); ++i) {
1067 ListItem *item = m_visibleItems[i];
1068 if (item->m_sectionItem) {
1069 QQmlContext *context = QQmlEngine::contextForObject(item->m_sectionItem)->parentContext();
1070 context->setContextProperty(QLatin1String("delegateIndex"), m_firstVisibleIndex + i);
1074 layout();
1075 polish();
1076 m_contentHeightDirty = true;
1079void ListViewWithPageHeader::onShowHeaderAnimationFinished()
1081 m_contentHeightDirty = true;
1082 polish();
1085void ListViewWithPageHeader::itemGeometryChanged(QQuickItem * /*item*/, const QRectF &newGeometry, const QRectF &oldGeometry)
1087 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
1088 if (heightDiff != 0) {
1089 if (oldGeometry.y() + oldGeometry.height() + m_clipItem->y() <= contentY() && !m_visibleItems.isEmpty()) {
1090 ListItem *firstItem = m_visibleItems.first();
1091 firstItem->setY(firstItem->y() - heightDiff);
1092 adjustMinYExtent();
1093 layout();
1095 refill();
1096 adjustMinYExtent();
1097 polish();
1098 m_contentHeightDirty = true;
1102void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
1104 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
1105 if (m_headerItemShownHeight > 0) {
1106 // If the header is shown because of the clip
1107 // Change its size
1108 m_headerItemShownHeight += heightDiff;
1109 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
1110 updateClipItem();
1111 adjustMinYExtent();
1112 } else {
1113 if (oldHeaderY + oldHeaderHeight > contentY()) {
1114 // If the header is shown because its position
1115 // Change its size
1116 ListItem *firstItem = m_visibleItems.first();
1117 firstItem->setY(firstItem->y() + heightDiff);
1118 layout();
1119 } else {
1120 // If the header is not on screen, just change the start of the list
1121 // so the viewport is not changed
1122 adjustMinYExtent();
1128void ListViewWithPageHeader::adjustMinYExtent()
1130 if (m_visibleItems.isEmpty()) {
1131 m_minYExtent = 0;
1132 } else {
1133 qreal nonCreatedHeight = 0;
1134 if (m_firstVisibleIndex != 0) {
1135 // Calculate the average height of items to estimate the position of the list start
1136 const int visibleItems = m_visibleItems.count();
1137 qreal visibleItemsHeight = 0;
1138 Q_FOREACH(ListItem *item, m_visibleItems) {
1139 visibleItemsHeight += item->height();
1141 nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
1142// qDebug() << m_firstVisibleIndex << visibleItemsHeight << visibleItems << nonCreatedHeight;
1144 const qreal headerHeight = (m_headerItem ? m_headerItem->implicitHeight() : 0);
1145 m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + headerHeight;
1146 if (m_minYExtent != 0 && qFuzzyIsNull(m_minYExtent)) {
1147 m_minYExtent = 0;
1148 m_visibleItems.first()->setY(nonCreatedHeight - m_clipItem->y() + headerHeight);
1153ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
1155 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
1156 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
1157 return m_visibleItems[visibleIndexedModelIndex];
1159 return nullptr;
1162void ListViewWithPageHeader::layout()
1164 if (m_inLayout)
1165 return;
1167 m_inLayout = true;
1168 if (!m_visibleItems.isEmpty()) {
1169 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
1170 const qreal visibleTo = contentY() + height() - m_clipItem->y();
1172 qreal pos = m_visibleItems.first()->y();
1174// qDebug() << "ListViewWithPageHeader::layout Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
1175 int firstReallyVisibleItem = -1;
1176 int modelIndex = m_firstVisibleIndex;
1177 Q_FOREACH(ListItem *item, m_visibleItems) {
1178 const bool cull = pos + item->height() <= visibleFrom || pos >= visibleTo;
1179 item->setCulled(cull);
1180 item->setY(pos);
1181 if (!cull && firstReallyVisibleItem == -1) {
1182 firstReallyVisibleItem = modelIndex;
1183 if (m_topSectionItem) {
1184 // Positing the top section sticky item is a two step process
1185 // First we set it either we cull it (because it doesn't need to be sticked to the top)
1186 // or stick it to the top
1187 // Then after the loop we'll make sure that if there's another section just below it
1188 // pushed the sticky section up to make it disappear
1189 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
1190 bool showStickySectionItem;
1191 // We need to show the "top section sticky item" when the position at the "top" of the
1192 // viewport is bigger than the start of the position of the first visible item
1193 // i.e. the first visible item starts before the viewport, or when the first
1194 // visible item starts just at the viewport start and it does not have its own section item
1195 if (topSectionStickPos > pos) {
1196 showStickySectionItem = true;
1197 } else if (topSectionStickPos == pos) {
1198 showStickySectionItem = !item->m_sectionItem;
1199 } else {
1200 showStickySectionItem = false;
1202 if (!showStickySectionItem) {
1203 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
1204 if (item->m_sectionItem) {
1205 // This seems it should happen since why would we cull the top section
1206 // if the first visible item has no section header? This only happens briefly
1207 // when increasing the height of a list that is at the bottom, the m_topSectionItem
1208 // gets shown shortly in the next polish call
1209 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(false);
1211 } else {
1212 // Update the top sticky section header
1213 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1214 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
1215 context->setContextProperty(QLatin1String("section"), section);
1217 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
1218 m_topSectionItem->setY(topSectionStickPos);
1219 int delegateIndex = modelIndex;
1220 // Look for the first index with this section text
1221 while (delegateIndex > 0) {
1222 const QString prevSection = m_delegateModel->stringValue(delegateIndex - 1, m_sectionProperty);
1223 if (prevSection != section)
1224 break;
1225 delegateIndex--;
1227 context->setContextProperty(QLatin1String("delegateIndex"), delegateIndex);
1228 if (item->m_sectionItem) {
1229 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(true);
1234 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
1235 const qreal clipFrom = visibleFrom + (!item->m_sectionItem && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
1236 if (!cull && pos < clipFrom) {
1237 context->setContextProperty(QLatin1String("heightToClip"), clipFrom - pos);
1238 } else {
1239 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
1241// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
1242 pos += item->height();
1243 ++modelIndex;
1246 // Second step of section sticky item positioning
1247 // Look at the next section header, check if it's pushing up the sticky one
1248 if (m_topSectionItem) {
1249 if (firstReallyVisibleItem >= 0) {
1250 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
1251 ListItem *item = m_visibleItems[i];
1252 if (item->m_sectionItem) {
1253 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
1254 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
1256 break;
1262 m_inLayout = false;
1265void ListViewWithPageHeader::updatePolish()
1267 // Check we are not being taken down and don't paint anything
1268 // TODO Check if we still need this in 5.2
1269 // For reproduction just inifnite loop testDash or testDashContent
1270 if (!QQmlEngine::contextForObject(this)->parentContext())
1271 return;
1273 Q_FOREACH(ListItem *item, m_itemsToRelease)
1274 reallyReleaseItem(item);
1275 m_itemsToRelease.clear();
1277 if (!model())
1278 return;
1280 layout();
1282 refill();
1284 if (m_contentHeightDirty) {
1285 // We need to make sure all bindings have updated otherwise
1286 // we may end up with bindings updating when we call setContentHeight
1287 // and then everything gets out of sync, i.e. testHeaderPositionBug1240118
1288 QCoreApplication::instance()->processEvents();
1290 qreal contentHeight;
1291 if (m_visibleItems.isEmpty()) {
1292 contentHeight = m_headerItem ? m_headerItem->height() : 0;
1293 } else {
1294 const int modelCount = model()->rowCount();
1295 const int visibleItems = m_visibleItems.count();
1296 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
1297 qreal nonCreatedHeight = 0;
1298 if (lastValidIndex != modelCount - 1) {
1299 const int visibleItems = m_visibleItems.count();
1300 qreal visibleItemsHeight = 0;
1301 Q_FOREACH(ListItem *item, m_visibleItems) {
1302 visibleItemsHeight += item->height();
1304 const int unknownSizes = modelCount - (m_firstVisibleIndex + visibleItems);
1305 nonCreatedHeight = unknownSizes * visibleItemsHeight / visibleItems;
1307 ListItem *item = m_visibleItems.last();
1308 contentHeight = nonCreatedHeight + item->y() + item->height() + m_clipItem->y();
1309 if (m_firstVisibleIndex != 0) {
1310 // Make sure that if we are shrinking we tell the view we still fit
1311 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
1315 m_contentHeightDirty = false;
1316 adjustMinYExtent();
1317 m_inContentHeightKeepHeaderShown = m_headerItem && m_headerItem->y() == contentY();
1318 setContentHeight(contentHeight);
1319 m_inContentHeightKeepHeaderShown = false;
1323#include "moc_listviewwithpageheader.cpp"