on ed4d40c help sign in
unity8/plugins/DashViews/listviewwithpageheader.cpp
1326 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 (!foundVisible) {
776 m_firstVisibleIndex = -1;
777 }
778 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
779 adjustMinYExtent();
780 }
782 return changed;
785ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
787// qDebug() << "CREATE ITEM" << modelIndex;
788 if (asynchronous && m_asyncRequestedIndex != -1)
789 return nullptr;
791 m_asyncRequestedIndex = -1;
792#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
793 QQuickItem *item = m_delegateModel->item(modelIndex, asynchronous);
794#else
795 QObject* object = m_delegateModel->object(modelIndex, asynchronous);
796 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
797#endif
798 if (!item) {
799#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
800 m_asyncRequestedIndex = modelIndex;
801#else
802 if (object) {
803 m_delegateModel->release(object);
804 if (!m_delegateValidated) {
805 m_delegateValidated = true;
806 QObject* delegateObj = delegate();
807 qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
808 }
809 } else {
810 m_asyncRequestedIndex = modelIndex;
811 }
812#endif
813 return 0;
814 } else {
815// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
816 ListItem *listItem = new ListItem;
817 listItem->m_item = item;
818 listItem->m_sectionItem = getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/);
819 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
820 ListItem *prevItem = itemAtIndex(modelIndex - 1);
821 bool lostItem = false; // Is an item that we requested async but because of model changes
822 // it is no longer attached to any of the existing items (has no prev nor next item)
823 // nor is the first item
824 if (prevItem) {
825 listItem->setY(prevItem->y() + prevItem->height());
826 } else {
827 ListItem *currItem = itemAtIndex(modelIndex);
828 if (currItem) {
829 // There's something already in m_visibleItems at out index, meaning this is an insert, so attach to its top
830 listItem->setY(currItem->y() - listItem->height());
831 } else {
832 ListItem *nextItem = itemAtIndex(modelIndex + 1);
833 if (nextItem) {
834 listItem->setY(nextItem->y() - listItem->height());
835 } else if (modelIndex == 0 && m_headerItem) {
836 listItem->setY(m_headerItem->height());
837 } else if (!m_visibleItems.isEmpty()) {
838 lostItem = true;
839 }
840 }
841 }
842 if (lostItem) {
843 releaseItem(listItem);
844 listItem = nullptr;
845 } else {
846 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() <= contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
847 if (m_visibleItems.isEmpty()) {
848 m_visibleItems << listItem;
849 } else {
850 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
851 }
852 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
853 m_firstVisibleIndex = modelIndex;
854 polish();
855 }
856 if (listItem->m_sectionItem) {
857 QQmlContext *context = QQmlEngine::contextForObject(listItem->m_sectionItem)->parentContext();
858 context->setContextProperty(QLatin1String("delegateIndex"), modelIndex);
859 }
860 adjustMinYExtent();
861 m_contentHeightDirty = true;
862 }
863 return listItem;
864 }
867#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
868void ListViewWithPageHeader::itemCreated(int modelIndex, QQuickItem *item)
870#else
871void ListViewWithPageHeader::itemCreated(int modelIndex, QObject *object)
873 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
874 if (!item) {
875 qWarning() << "ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
876 return;
877 }
878#endif
879// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
880 // Check we are not being taken down and don't paint anything
881 // TODO Check if we still need this in 5.2
882 // For reproduction just inifnite loop testDash or testDashContent
883 if (!QQmlEngine::contextForObject(this)->parentContext())
884 return;
886 item->setParentItem(m_clipItem);
887 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
888 context->setContextProperty(QLatin1String("ListViewWithPageHeader"), this);
889 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
890 if (modelIndex == m_asyncRequestedIndex) {
891 createItem(modelIndex, false);
892 refill();
893 }
896void ListViewWithPageHeader::updateClipItem()
898 m_clipItem->setHeight(height() - m_headerItemShownHeight);
899 m_clipItem->setY(contentY() + m_headerItemShownHeight);
900 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
903void ListViewWithPageHeader::onContentHeightChanged()
905 updateClipItem();
908void ListViewWithPageHeader::onContentWidthChanged()
910 m_clipItem->setWidth(contentItem()->width());
913void ListViewWithPageHeader::onHeightChanged()
915 polish();
919#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
920void ListViewWithPageHeader::onModelUpdated(const QQuickChangeSet &changeSet, bool /*reset*/)
921#else
922void ListViewWithPageHeader::onModelUpdated(const QQmlChangeSet &changeSet, bool /*reset*/)
923#endif
925 // TODO Do something with reset
926// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
927 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
929#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
930 Q_FOREACH(const QQuickChangeSet::Remove &remove, changeSet.removes()) {
931#else
932 Q_FOREACH(const QQmlChangeSet::Remove &remove, changeSet.removes()) {
933#endif
934// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove.index << remove.count;
935 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
936 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
937 // If all the items we are removing are either not created or culled
938 // we have to grow down to avoid viewport changing
939 bool growDown = true;
940 for (int i = 0; growDown && i < remove.count; ++i) {
941 const int modelIndex = remove.index + i;
942 ListItem *item = itemAtIndex(modelIndex);
943 if (item && !item->culled()) {
944 growDown = false;
945 }
946 }
947 for (int i = remove.count - 1; i >= 0; --i) {
948 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
949 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
950 ListItem *item = m_visibleItems[visibleIndex];
951 // Pass the section item down if needed
952 if (item->m_sectionItem && visibleIndex + 1 < m_visibleItems.count()) {
953 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
954 if (!nextItem->m_sectionItem) {
955 nextItem->m_sectionItem = item->m_sectionItem;
956 item->m_sectionItem = nullptr;
957 }
958 }
959 releaseItem(item);
960 m_visibleItems.removeAt(visibleIndex);
961 }
962 }
963 if (growDown) {
964 adjustMinYExtent();
965 } else if (remove.index <= m_firstVisibleIndex) {
966 if (!m_visibleItems.isEmpty()) {
967 // We removed the first item that is the one that positions the rest
968 // position the new first item correctly
969 m_visibleItems.first()->setY(oldFirstValidIndexPos);
970 } else {
971 m_firstVisibleIndex = -1;
972 }
973 }
974 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
975 m_firstVisibleIndex -= remove.count;
976 }
977 for (int i = remove.count - 1; i >= 0; --i) {
978 const int modelIndex = remove.index + i;
979 if (modelIndex == m_asyncRequestedIndex) {
980 m_asyncRequestedIndex = -1;
981 } else if (modelIndex < m_asyncRequestedIndex) {
982 m_asyncRequestedIndex--;
983 }
984 }
985 }
987#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
988 Q_FOREACH(const QQuickChangeSet::Insert &insert, changeSet.inserts()) {
989#else
990 Q_FOREACH(const QQmlChangeSet::Insert &insert, changeSet.inserts()) {
991#endif
992// qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
993 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
994 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
995 if (insertingInValidIndexes || firstItemWithViewOnTop)
996 {
997 // If the items we are adding won't be really visible
998 // we grow up instead of down to not change the viewport
999 bool growUp = false;
1000 if (!firstItemWithViewOnTop) {
1001 for (int i = 0; i < m_visibleItems.count(); ++i) {
1002 if (!m_visibleItems[i]->culled()) {
1003 if (insert.index <= m_firstVisibleIndex + i) {
1004 growUp = true;
1006 break;
1011 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
1012 for (int i = insert.count - 1; i >= 0; --i) {
1013 const int modelIndex = insert.index + i;
1014 ListItem *item = createItem(modelIndex, false);
1015 if (growUp) {
1016 ListItem *firstItem = m_visibleItems.first();
1017 firstItem->setY(firstItem->y() - item->height());
1019 // Adding an item may break a "same section" chain, so check
1020 // if we need adding a new section item
1021 if (m_sectionDelegate) {
1022 ListItem *nextItem = itemAtIndex(modelIndex + 1);
1023 if (nextItem && !nextItem->m_sectionItem) {
1024 nextItem->m_sectionItem = getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/);
1025 if (growUp && nextItem->m_sectionItem) {
1026 ListItem *firstItem = m_visibleItems.first();
1027 firstItem->setY(firstItem->y() - nextItem->m_sectionItem->height());
1032 if (firstItemWithViewOnTop) {
1033 ListItem *firstItem = m_visibleItems.first();
1034 firstItem->setY(oldFirstValidIndexPos);
1036 adjustMinYExtent();
1037 } else if (insert.index <= m_firstVisibleIndex) {
1038 m_firstVisibleIndex += insert.count;
1041 for (int i = insert.count - 1; i >= 0; --i) {
1042 const int modelIndex = insert.index + i;
1043 if (modelIndex <= m_asyncRequestedIndex) {
1044 m_asyncRequestedIndex++;
1049#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
1050 Q_FOREACH(const QQuickChangeSet::Change &change, changeSet.changes()) {
1051#else
1052 Q_FOREACH(const QQmlChangeSet::Change &change, changeSet.changes()) {
1053#endif
1054// qDebug() << "ListViewWithPageHeader::onModelUpdated Change" << change.index << change.count;
1055 for (int i = change.index; i < change.count; ++i) {
1056 ListItem *item = itemAtIndex(i);
1057 if (item && item->m_sectionItem) {
1058 QQmlContext *context = QQmlEngine::contextForObject(item->m_sectionItem)->parentContext();
1059 const QString sectionText = m_delegateModel->stringValue(i, m_sectionProperty);
1060 context->setContextProperty(QLatin1String("section"), sectionText);
1065 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
1066 adjustMinYExtent();
1069 for (int i = 0; i < m_visibleItems.count(); ++i) {
1070 ListItem *item = m_visibleItems[i];
1071 if (item->m_sectionItem) {
1072 QQmlContext *context = QQmlEngine::contextForObject(item->m_sectionItem)->parentContext();
1073 context->setContextProperty(QLatin1String("delegateIndex"), m_firstVisibleIndex + i);
1077 layout();
1078 polish();
1079 m_contentHeightDirty = true;
1082void ListViewWithPageHeader::onShowHeaderAnimationFinished()
1084 m_contentHeightDirty = true;
1085 polish();
1088void ListViewWithPageHeader::itemGeometryChanged(QQuickItem * /*item*/, const QRectF &newGeometry, const QRectF &oldGeometry)
1090 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
1091 if (heightDiff != 0) {
1092 if (oldGeometry.y() + oldGeometry.height() + m_clipItem->y() <= contentY() && !m_visibleItems.isEmpty()) {
1093 ListItem *firstItem = m_visibleItems.first();
1094 firstItem->setY(firstItem->y() - heightDiff);
1095 adjustMinYExtent();
1096 layout();
1098 refill();
1099 adjustMinYExtent();
1100 polish();
1101 m_contentHeightDirty = true;
1105void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
1107 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
1108 if (m_headerItemShownHeight > 0) {
1109 // If the header is shown because of the clip
1110 // Change its size
1111 m_headerItemShownHeight += heightDiff;
1112 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
1113 updateClipItem();
1114 adjustMinYExtent();
1115 } else {
1116 if (oldHeaderY + oldHeaderHeight > contentY()) {
1117 // If the header is shown because its position
1118 // Change its size
1119 ListItem *firstItem = m_visibleItems.first();
1120 firstItem->setY(firstItem->y() + heightDiff);
1121 layout();
1122 } else {
1123 // If the header is not on screen, just change the start of the list
1124 // so the viewport is not changed
1125 adjustMinYExtent();
1131void ListViewWithPageHeader::adjustMinYExtent()
1133 if (m_visibleItems.isEmpty()) {
1134 m_minYExtent = 0;
1135 } else {
1136 qreal nonCreatedHeight = 0;
1137 if (m_firstVisibleIndex != 0) {
1138 // Calculate the average height of items to estimate the position of the list start
1139 const int visibleItems = m_visibleItems.count();
1140 qreal visibleItemsHeight = 0;
1141 Q_FOREACH(ListItem *item, m_visibleItems) {
1142 visibleItemsHeight += item->height();
1144 nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
1145// qDebug() << m_firstVisibleIndex << visibleItemsHeight << visibleItems << nonCreatedHeight;
1147 const qreal headerHeight = (m_headerItem ? m_headerItem->implicitHeight() : 0);
1148 m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + headerHeight;
1149 if (m_minYExtent != 0 && qFuzzyIsNull(m_minYExtent)) {
1150 m_minYExtent = 0;
1151 m_visibleItems.first()->setY(nonCreatedHeight - m_clipItem->y() + headerHeight);
1156ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
1158 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
1159 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
1160 return m_visibleItems[visibleIndexedModelIndex];
1162 return nullptr;
1165void ListViewWithPageHeader::layout()
1167 if (m_inLayout)
1168 return;
1170 m_inLayout = true;
1171 if (!m_visibleItems.isEmpty()) {
1172 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
1173 const qreal visibleTo = contentY() + height() - m_clipItem->y();
1175 qreal pos = m_visibleItems.first()->y();
1177// qDebug() << "ListViewWithPageHeader::layout Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
1178 int firstReallyVisibleItem = -1;
1179 int modelIndex = m_firstVisibleIndex;
1180 Q_FOREACH(ListItem *item, m_visibleItems) {
1181 const bool cull = pos + item->height() <= visibleFrom || pos >= visibleTo;
1182 item->setCulled(cull);
1183 item->setY(pos);
1184 if (!cull && firstReallyVisibleItem == -1) {
1185 firstReallyVisibleItem = modelIndex;
1186 if (m_topSectionItem) {
1187 // Positing the top section sticky item is a two step process
1188 // First we set it either we cull it (because it doesn't need to be sticked to the top)
1189 // or stick it to the top
1190 // Then after the loop we'll make sure that if there's another section just below it
1191 // pushed the sticky section up to make it disappear
1192 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
1193 bool showStickySectionItem;
1194 // We need to show the "top section sticky item" when the position at the "top" of the
1195 // viewport is bigger than the start of the position of the first visible item
1196 // i.e. the first visible item starts before the viewport, or when the first
1197 // visible item starts just at the viewport start and it does not have its own section item
1198 if (topSectionStickPos > pos) {
1199 showStickySectionItem = true;
1200 } else if (topSectionStickPos == pos) {
1201 showStickySectionItem = !item->m_sectionItem;
1202 } else {
1203 showStickySectionItem = false;
1205 if (!showStickySectionItem) {
1206 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
1207 if (item->m_sectionItem) {
1208 // This seems it should happen since why would we cull the top section
1209 // if the first visible item has no section header? This only happens briefly
1210 // when increasing the height of a list that is at the bottom, the m_topSectionItem
1211 // gets shown shortly in the next polish call
1212 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(false);
1214 } else {
1215 // Update the top sticky section header
1216 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1217 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
1218 context->setContextProperty(QLatin1String("section"), section);
1220 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
1221 m_topSectionItem->setY(topSectionStickPos);
1222 int delegateIndex = modelIndex;
1223 // Look for the first index with this section text
1224 while (delegateIndex > 0) {
1225 const QString prevSection = m_delegateModel->stringValue(delegateIndex - 1, m_sectionProperty);
1226 if (prevSection != section)
1227 break;
1228 delegateIndex--;
1230 context->setContextProperty(QLatin1String("delegateIndex"), delegateIndex);
1231 if (item->m_sectionItem) {
1232 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(true);
1237 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
1238 const qreal clipFrom = visibleFrom + (!item->m_sectionItem && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
1239 if (!cull && pos < clipFrom) {
1240 context->setContextProperty(QLatin1String("heightToClip"), clipFrom - pos);
1241 } else {
1242 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
1244// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
1245 pos += item->height();
1246 ++modelIndex;
1249 // Second step of section sticky item positioning
1250 // Look at the next section header, check if it's pushing up the sticky one
1251 if (m_topSectionItem) {
1252 if (firstReallyVisibleItem >= 0) {
1253 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
1254 ListItem *item = m_visibleItems[i];
1255 if (item->m_sectionItem) {
1256 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
1257 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
1259 break;
1265 m_inLayout = false;
1268void ListViewWithPageHeader::updatePolish()
1270 // Check we are not being taken down and don't paint anything
1271 // TODO Check if we still need this in 5.2
1272 // For reproduction just inifnite loop testDash or testDashContent
1273 if (!QQmlEngine::contextForObject(this)->parentContext())
1274 return;
1276 Q_FOREACH(ListItem *item, m_itemsToRelease)
1277 reallyReleaseItem(item);
1278 m_itemsToRelease.clear();
1280 if (!model())
1281 return;
1283 layout();
1285 refill();
1287 if (m_contentHeightDirty) {
1288 // We need to make sure all bindings have updated otherwise
1289 // we may end up with bindings updating when we call setContentHeight
1290 // and then everything gets out of sync, i.e. testHeaderPositionBug1240118
1291 QCoreApplication::instance()->processEvents();
1293 qreal contentHeight;
1294 if (m_visibleItems.isEmpty()) {
1295 contentHeight = m_headerItem ? m_headerItem->height() : 0;
1296 } else {
1297 const int modelCount = model()->rowCount();
1298 const int visibleItems = m_visibleItems.count();
1299 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
1300 qreal nonCreatedHeight = 0;
1301 if (lastValidIndex != modelCount - 1) {
1302 const int visibleItems = m_visibleItems.count();
1303 qreal visibleItemsHeight = 0;
1304 Q_FOREACH(ListItem *item, m_visibleItems) {
1305 visibleItemsHeight += item->height();
1307 const int unknownSizes = modelCount - (m_firstVisibleIndex + visibleItems);
1308 nonCreatedHeight = unknownSizes * visibleItemsHeight / visibleItems;
1310 ListItem *item = m_visibleItems.last();
1311 contentHeight = nonCreatedHeight + item->y() + item->height() + m_clipItem->y();
1312 if (m_firstVisibleIndex != 0) {
1313 // Make sure that if we are shrinking we tell the view we still fit
1314 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
1318 m_contentHeightDirty = false;
1319 adjustMinYExtent();
1320 m_inContentHeightKeepHeaderShown = m_headerItem && m_headerItem->y() == contentY();
1321 setContentHeight(contentHeight);
1322 m_inContentHeightKeepHeaderShown = false;
1326#include "moc_listviewwithpageheader.cpp"