on 83ce061 help sign in
unity8/plugins/DashViews/listviewwithpageheader.cpp
1307 lines 52 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 <QDebug>
95#include <qqmlinfo.h>
96#include <qqmlengine.h>
97#pragma GCC diagnostic push
98#pragma GCC diagnostic ignored "-pedantic"
99#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
100#include <private/qquickvisualdatamodel_p.h>
101#else
102#include <private/qqmldelegatemodel_p.h>
103#endif
104#include <private/qqmlglobal_p.h>
105#include <private/qquickitem_p.h>
106#include <private/qquickanimation_p.h>
107#pragma GCC diagnostic pop
108// #include <private/qquickrectangle_p.h>
110static const qreal bufferRatio = 0.5;
112qreal ListViewWithPageHeader::ListItem::height() const
114 return m_item->height() + (m_sectionItem ? m_sectionItem->height() : 0);
117qreal ListViewWithPageHeader::ListItem::y() const
119 return m_item->y() - (m_sectionItem ? m_sectionItem->height() : 0);
122void ListViewWithPageHeader::ListItem::setY(qreal newY)
124 if (m_sectionItem) {
125 m_sectionItem->setY(newY);
126 m_item->setY(newY + m_sectionItem->height());
127 } else {
128 m_item->setY(newY);
129 }
132bool ListViewWithPageHeader::ListItem::culled() const
134 return QQuickItemPrivate::get(m_item)->culled;
137void ListViewWithPageHeader::ListItem::setCulled(bool culled)
139 QQuickItemPrivate::get(m_item)->setCulled(culled);
140 if (m_sectionItem)
141 QQuickItemPrivate::get(m_sectionItem)->setCulled(culled);
144ListViewWithPageHeader::ListViewWithPageHeader()
145 : m_delegateModel(nullptr)
146 , m_asyncRequestedIndex(-1)
147 , m_delegateValidated(false)
148 , m_firstVisibleIndex(-1)
149 , m_minYExtent(0)
150 , m_contentHeightDirty(false)
151 , m_headerItem(nullptr)
152 , m_previousContentY(0)
153 , m_headerItemShownHeight(0)
154 , m_sectionDelegate(nullptr)
155 , m_topSectionItem(nullptr)
156 , m_forceNoClip(false)
157 , m_inLayout(false)
158 , m_inContentHeightKeepHeaderShown(false)
160 m_clipItem = new QQuickItem(contentItem());
161// m_clipItem = new QQuickRectangle(contentItem());
162// ((QQuickRectangle*)m_clipItem)->setColor(Qt::gray);
164 m_contentYAnimation = new QQuickNumberAnimation(this);
165 m_contentYAnimation->setEasing(QEasingCurve::OutQuad);
166 m_contentYAnimation->setProperty("contentY");
167 m_contentYAnimation->setDuration(200);
168 m_contentYAnimation->setTargetObject(this);
170 connect(this, SIGNAL(contentWidthChanged()), this, SLOT(onContentWidthChanged()));
171 connect(this, SIGNAL(contentHeightChanged()), this, SLOT(onContentHeightChanged()));
172 connect(this, SIGNAL(heightChanged()), this, SLOT(onHeightChanged()));
173 connect(m_contentYAnimation, SIGNAL(stopped()), this, SLOT(onShowHeaderAnimationFinished()));
175 setFlickableDirection(VerticalFlick);
178ListViewWithPageHeader::~ListViewWithPageHeader()
182QAbstractItemModel *ListViewWithPageHeader::model() const
184 return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
187void ListViewWithPageHeader::setModel(QAbstractItemModel *model)
189 if (model != this->model()) {
190 if (!m_delegateModel) {
191 createDelegateModel();
192 } else {
193#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
194 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
195 }
196 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
197 connect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
198#else
199 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
200 }
201 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
202 connect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
203#endif
204 Q_EMIT modelChanged();
205 polish();
206 // TODO?
207// Q_EMIT contentHeightChanged();
208// Q_EMIT contentYChanged();
209 }
212QQmlComponent *ListViewWithPageHeader::delegate() const
214 return m_delegateModel ? m_delegateModel->delegate() : nullptr;
217void ListViewWithPageHeader::setDelegate(QQmlComponent *delegate)
219 if (delegate != this->delegate()) {
220 if (!m_delegateModel) {
221 createDelegateModel();
222 }
224 // Cleanup the existing items
225 Q_FOREACH(ListItem *item, m_visibleItems)
226 releaseItem(item);
227 m_visibleItems.clear();
228 m_firstVisibleIndex = -1;
229 adjustMinYExtent();
230 setContentY(0);
231 m_clipItem->setY(0);
232 if (m_topSectionItem) {
233 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
234 }
236 m_delegateModel->setDelegate(delegate);
238 Q_EMIT delegateChanged();
239 m_delegateValidated = false;
240 m_contentHeightDirty = true;
241 polish();
242 }
245QQuickItem *ListViewWithPageHeader::header() const
247 return m_headerItem;
250void ListViewWithPageHeader::setHeader(QQuickItem *headerItem)
252 if (m_headerItem != headerItem) {
253 qreal oldHeaderHeight = 0;
254 qreal oldHeaderY = 0;
255 if (m_headerItem) {
256 oldHeaderHeight = m_headerItem->height();
257 oldHeaderY = m_headerItem->y();
258 m_headerItem->setParentItem(nullptr);
259 }
260 m_headerItem = headerItem;
261 if (m_headerItem) {
262 m_headerItem->setParentItem(contentItem());
263 m_headerItem->setZ(1);
264 }
265 qreal newHeaderHeight = m_headerItem ? m_headerItem->height() : 0;
266 if (!m_visibleItems.isEmpty() && newHeaderHeight != oldHeaderHeight) {
267 headerHeightChanged(newHeaderHeight, oldHeaderHeight, oldHeaderY);
268 polish();
269 m_contentHeightDirty = true;
270 }
271 Q_EMIT headerChanged();
272 }
275QQmlComponent *ListViewWithPageHeader::sectionDelegate() const
277 return m_sectionDelegate;
280void ListViewWithPageHeader::setSectionDelegate(QQmlComponent *delegate)
282 if (delegate != m_sectionDelegate) {
283 // TODO clean existing sections
285 m_sectionDelegate = delegate;
287 m_topSectionItem = getSectionItem(QString());
288 m_topSectionItem->setZ(3);
289 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
290 connect(m_topSectionItem, SIGNAL(heightChanged()), SIGNAL(stickyHeaderHeightChanged()));
292 // TODO create sections for existing items
294 Q_EMIT sectionDelegateChanged();
295 Q_EMIT stickyHeaderHeightChanged();
296 }
299QString ListViewWithPageHeader::sectionProperty() const
301 return m_sectionProperty;
304void ListViewWithPageHeader::setSectionProperty(const QString &property)
306 if (property != m_sectionProperty) {
307 m_sectionProperty = property;
309 updateWatchedRoles();
311 // TODO recreate sections
313 Q_EMIT sectionPropertyChanged();
314 }
317bool ListViewWithPageHeader::forceNoClip() const
319 return m_forceNoClip;
322void ListViewWithPageHeader::setForceNoClip(bool noClip)
324 if (noClip != m_forceNoClip) {
325 m_forceNoClip = noClip;
326 updateClipItem();
327 Q_EMIT forceNoClipChanged();
328 }
331int ListViewWithPageHeader::stickyHeaderHeight() const
333 return m_topSectionItem ? m_topSectionItem->height() : 0;
336void ListViewWithPageHeader::positionAtBeginning()
338 if (m_delegateModel->count() <= 0)
339 return;
341 qreal headerHeight = (m_headerItem ? m_headerItem->height() : 0);
342 if (m_firstVisibleIndex != 0) {
343 // TODO This could be optimized by trying to reuse the interesection
344 // of items that may end up intersecting between the existing
345 // m_visibleItems and the items we are creating in the next loop
346 Q_FOREACH(ListItem *item, m_visibleItems)
347 releaseItem(item);
348 m_visibleItems.clear();
349 m_firstVisibleIndex = -1;
351 // Create the item 0, it will be already correctly positioned at createItem()
352 m_clipItem->setY(0);
353 ListItem *item = createItem(0, false);
354 // Create the subsequent items
355 int modelIndex = 1;
356 qreal pos = item->y() + item->height();
357 const qreal buffer = height() * bufferRatio;
358 const qreal bufferTo = height() + buffer;
359 while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
360 if (!(item = createItem(modelIndex, false)))
361 break;
362 pos += item->height();
363 ++modelIndex;
364 }
366 m_previousContentY = m_visibleItems.first()->y() - headerHeight;
367 }
368 setContentY(m_visibleItems.first()->y() + m_clipItem->y() - headerHeight);
369 if (m_headerItem) {
370 // TODO This should not be needed and the code that adjust the m_headerItem position
371 // in viewportMoved() should be enough but in some cases we have not found a way to reproduce
372 // yet the code of viewportMoved() fails so here we make sure that at least if we are calling
373 // positionAtBeginning the header item will be correctly positioned
374 m_headerItem->setY(-m_minYExtent);
375 }
378void ListViewWithPageHeader::showHeader()
380 if (!m_headerItem)
381 return;
383 auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
384 if (to != contentY()) {
385 const bool headerShownByItsOwn = contentY() < m_headerItem->y() + m_headerItem->height();
386 if (headerShownByItsOwn && m_headerItemShownHeight == 0) {
387 // We are not clipping since we are just at the top of the viewport
388 // but because of the showHeader animation we will need to, so
389 // enable the clipping without logically moving the items
390 m_headerItemShownHeight = m_headerItem->y() + m_headerItem->height() - contentY();
391 if (!m_visibleItems.isEmpty()) {
392 updateClipItem();
393 ListItem *firstItem = m_visibleItems.first();
394 firstItem->setY(firstItem->y() - m_headerItemShownHeight);
395 layout();
396 }
397 }
398 m_contentYAnimation->setTo(to);
399 contentYAnimationType = ContentYAnimationShowHeader;
400 m_contentYAnimation->start();
401 }
404QQuickItem *ListViewWithPageHeader::item(int modelIndex) const
406 ListItem *item = itemAtIndex(modelIndex);
407 if (item)
408 return item->m_item;
409 else
410 return nullptr;
413bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex)
415 ListItem *listItem = itemAtIndex(modelIndex);
416 if (listItem) {
417 return maximizeVisibleArea(listItem, listItem->height());
418 }
420 return false;
423bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex, int itemHeight)
425 if (itemHeight < 0)
426 return false;
428 ListItem *listItem = itemAtIndex(modelIndex);
429 if (listItem) {
430 return maximizeVisibleArea(listItem, itemHeight + (listItem->m_sectionItem ? listItem->m_sectionItem->height() : 0));
431 }
433 return false;
436bool ListViewWithPageHeader::maximizeVisibleArea(ListItem *listItem, int listItemHeight)
438 if (listItem) {
439 layout();
440 const auto listItemY = m_clipItem->y() + listItem->y();
441 if (listItemY > contentY() && listItemY + listItemHeight > contentY() + height()) {
442 // we can scroll the list up to show more stuff
443 const auto to = qMin(listItemY, listItemY + listItemHeight - height());
444 m_contentYAnimation->setTo(to);
445 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
446 m_contentYAnimation->start();
447 } else if ((listItemY < contentY() && listItemY + listItemHeight < contentY() + height()) ||
448 (m_topSectionItem && !listItem->m_sectionItem && listItemY - m_topSectionItem->height() < contentY() && listItemY + listItemHeight < contentY() + height()))
449 {
450 // we can scroll the list down to show more stuff
451 auto realVisibleListItemY = listItemY;
452 if (m_topSectionItem) {
453 // If we are showing the top section sticky item and this item doesn't have a section
454 // item we have to make sure to scroll it a bit more so that it is not underlapping
455 // the top section sticky item
456 bool topSectionShown = !QQuickItemPrivate::get(m_topSectionItem)->culled;
457 if (topSectionShown && !listItem->m_sectionItem) {
458 realVisibleListItemY -= m_topSectionItem->height();
459 }
460 }
461 const auto to = qMax(realVisibleListItemY, listItemY + listItemHeight - height());
462 m_contentYAnimation->setTo(to);
463 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
464 m_contentYAnimation->start();
465 }
466 return true;
467 }
468 return false;
471qreal ListViewWithPageHeader::minYExtent() const
473// qDebug() << "ListViewWithPageHeader::minYExtent" << m_minYExtent;
474 return m_minYExtent;
477void ListViewWithPageHeader::componentComplete()
479 if (m_delegateModel)
480 m_delegateModel->componentComplete();
482 QQuickFlickable::componentComplete();
484 polish();
487void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
489 // Check we are not being taken down and don't paint anything
490 // TODO Check if we still need this in 5.2
491 // For reproduction just inifnite loop testDash or testDashContent
492 if (!QQmlEngine::contextForObject(this)->parentContext())
493 return;
495 QQuickFlickable::viewportMoved(orient);
496// qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
497 qreal diff = m_previousContentY - contentY();
498 const bool showHeaderAnimationRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationShowHeader;
499 if (m_headerItem) {
500 const auto oldHeaderItemShownHeight = m_headerItemShownHeight;
501 if (contentY() < -m_minYExtent) {
502 // Stick the header item to the top when dragging down
503 m_headerItem->setY(contentY());
504 m_headerItem->setHeight(m_headerItem->implicitHeight() + (-m_minYExtent - contentY()));
505 } else {
506 m_headerItem->setHeight(m_headerItem->implicitHeight());
507 // We are going down (but it's not because of the rebound at the end)
508 // (but the header was not shown by it's own position)
509 // or the header is partially shown and we are not doing a maximizeVisibleArea either
510 const bool scrolledUp = m_previousContentY > contentY();
511 const bool notRebounding = contentY() + height() < contentHeight();
512 const bool notShownByItsOwn = contentY() + diff >= m_headerItem->y() + m_headerItem->height();
513 const bool maximizeVisibleAreaRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationMaximizeVisibleArea;
515 if (!scrolledUp && (contentY() == -m_minYExtent || (m_headerItemShownHeight == 0 && m_previousContentY == m_headerItem->y()))) {
516 m_headerItemShownHeight = 0;
517 m_headerItem->setY(-m_minYExtent);
518 } else if ((scrolledUp && notRebounding && notShownByItsOwn && !maximizeVisibleAreaRunning) || (m_headerItemShownHeight > 0) || m_inContentHeightKeepHeaderShown) {
519 if (maximizeVisibleAreaRunning && diff > 0) // If we are maximizing and the header was shown, make sure we hide it
520 m_headerItemShownHeight -= diff;
521 else
522 m_headerItemShownHeight += diff;
523 if (contentY() == -m_minYExtent) {
524 m_headerItemShownHeight = 0;
525 } else {
526 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
527 }
528 if (m_headerItemShownHeight > 0) {
529 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
530 } else {
531 m_headerItem->setY(-m_minYExtent);
532 }
533 }
534 }
535 // We will be changing the clip item, need to accomadate for it
536 // otherwise we move the firstItem down/up twice (unless the
537 // show header animation is running, where we want to keep the viewport stable)
538 if (!showHeaderAnimationRunning) {
539 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
540 } else {
541 diff = -diff;
542 }
543 }
544 if (!m_visibleItems.isEmpty()) {
545 updateClipItem();
546 ListItem *firstItem = m_visibleItems.first();
547 firstItem->setY(firstItem->y() + diff);
548 if (showHeaderAnimationRunning) {
549 adjustMinYExtent();
550 }
551 }
553 m_previousContentY = contentY();
554 layout();
555 polish();
558void ListViewWithPageHeader::createDelegateModel()
560#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
561 m_delegateModel = new QQuickVisualDataModel(qmlContext(this), this);
562 connect(m_delegateModel, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(itemCreated(int,QQuickItem*)));
563#else
564 m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
565 connect(m_delegateModel, SIGNAL(createdItem(int,QObject*)), this, SLOT(itemCreated(int,QObject*)));
566#endif
567 if (isComponentComplete())
568 m_delegateModel->componentComplete();
569 updateWatchedRoles();
572void ListViewWithPageHeader::refill()
574 if (m_inLayout) {
575 return;
576 }
577 if (!isComponentComplete()) {
578 return;
579 }
581 const qreal buffer = height() * bufferRatio;
582 const qreal from = contentY();
583 const qreal to = from + height();
584 const qreal bufferFrom = from - buffer;
585 const qreal bufferTo = to + buffer;
587 bool added = addVisibleItems(from, to, false);
588 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
589 added |= addVisibleItems(bufferFrom, bufferTo, true);
591 if (added || removed) {
592 m_contentHeightDirty = true;
593 }
596bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
598 if (!delegate())
599 return false;
601 if (m_delegateModel->count() == 0)
602 return false;
604 ListItem *item;
605// qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
607 int modelIndex = 0;
608 qreal pos = 0;
609 if (!m_visibleItems.isEmpty()) {
610 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
611 item = m_visibleItems.last();
612 pos = item->y() + item->height() + m_clipItem->y();
613 }
614 bool changed = false;
615// qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
616 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
617// qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
618 if (!(item = createItem(modelIndex, asynchronous)))
619 break;
620 pos += item->height();
621 ++modelIndex;
622 changed = true;
623 }
625 modelIndex = 0;
626 pos = 0;
627 if (!m_visibleItems.isEmpty()) {
628 modelIndex = m_firstVisibleIndex - 1;
629 item = m_visibleItems.first();
630 pos = item->y() + m_clipItem->y();
631 }
632 while (modelIndex >= 0 && pos > fillFrom) {
633// qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
634 if (!(item = createItem(modelIndex, asynchronous)))
635 break;
636 pos -= item->height();
637 --modelIndex;
638 changed = true;
639 }
641 return changed;
644void ListViewWithPageHeader::reallyReleaseItem(ListItem *listItem)
646 QQuickItem *item = listItem->m_item;
647#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
648 QQuickVisualModel::ReleaseFlags flags = m_delegateModel->release(item);
649 if (flags & QQuickVisualModel::Destroyed) {
650#else
651 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
652 if (flags & QQmlDelegateModel::Destroyed) {
653#endif
654 item->setParentItem(nullptr);
655 }
656 delete listItem->m_sectionItem;
657 delete listItem;
660void ListViewWithPageHeader::releaseItem(ListItem *listItem)
662 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(listItem->m_item);
663 itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
664 m_itemsToRelease << listItem;
667void ListViewWithPageHeader::updateWatchedRoles()
669 if (m_delegateModel) {
670 QList<QByteArray> roles;
671 if (!m_sectionProperty.isEmpty())
672 roles << m_sectionProperty.toUtf8();
673 m_delegateModel->setWatchedRoles(roles);
674 }
677QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
679 if (!m_sectionDelegate)
680 return nullptr;
682 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
683 if (modelIndex > 0) {
684 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
685 if (section == prevSection)
686 return nullptr;
687 }
688 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
689 // Already inserted items can't steal next section header
690 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
691 if (section == nextSection) {
692 // Steal the section header
693 ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
694 if (nextItem) {
695 QQuickItem *sectionItem = nextItem->m_sectionItem;
696 nextItem->m_sectionItem = nullptr;
697 return sectionItem;
698 }
699 }
700 }
702 return getSectionItem(section);
705QQuickItem *ListViewWithPageHeader::getSectionItem(const QString &sectionText)
707 QQuickItem *sectionItem = nullptr;
709 QQmlContext *creationContext = m_sectionDelegate->creationContext();
710 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
711 context->setContextProperty(QLatin1String("section"), sectionText);
712 context->setContextProperty(QLatin1String("delegateIndex"), -1);
713 QObject *nobj = m_sectionDelegate->beginCreate(context);
714 if (nobj) {
715 QQml_setParent_noEvent(context, nobj);
716 sectionItem = qobject_cast<QQuickItem *>(nobj);
717 if (!sectionItem) {
718 delete nobj;
719 } else {
720 sectionItem->setZ(2);
721 QQml_setParent_noEvent(sectionItem, m_clipItem);
722 sectionItem->setParentItem(m_clipItem);
723 }
724 } else {
725 delete context;
726 }
727 m_sectionDelegate->completeCreate();
729 // TODO attach to sectionItem so we can accomodate to it changing its height
731 return sectionItem;
734bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
736// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
737 // Do not remove items if we are overshooting up or down, since we'll come back
738 // to the "stable" position and delete/create items without any reason
739 if (contentY() < -m_minYExtent) {
740 return false;
741 } else if (contentY() + height() > contentHeight()) {
742 return false;
743 }
744 bool changed = false;
746 bool foundVisible = false;
747 int i = 0;
748 int removedItems = 0;
749 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
750 while (i < m_visibleItems.count()) {
751 ListItem *item = m_visibleItems[i];
752 const qreal pos = item->y() + m_clipItem->y();
753// qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
754 if (pos + item->height() < bufferFrom || pos > bufferTo) {
755// qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
756 releaseItem(item);
757 m_visibleItems.removeAt(i);
758 changed = true;
759 ++removedItems;
760 } else {
761 if (!foundVisible) {
762 foundVisible = true;
763 const int itemIndex = m_firstVisibleIndex + removedItems + i;
764 m_firstVisibleIndex = itemIndex;
765 }
766 ++i;
767 }
768 }
769 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
770 adjustMinYExtent();
771 }
773 return changed;
776ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
778// qDebug() << "CREATE ITEM" << modelIndex;
779 if (asynchronous && m_asyncRequestedIndex != -1)
780 return nullptr;
782 m_asyncRequestedIndex = -1;
783#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
784 QQuickItem *item = m_delegateModel->item(modelIndex, asynchronous);
785#else
786 QObject* object = m_delegateModel->object(modelIndex, asynchronous);
787 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
788#endif
789 if (!item) {
790#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
791 m_asyncRequestedIndex = modelIndex;
792#else
793 if (object) {
794 m_delegateModel->release(object);
795 if (!m_delegateValidated) {
796 m_delegateValidated = true;
797 QObject* delegateObj = delegate();
798 qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
799 }
800 } else {
801 m_asyncRequestedIndex = modelIndex;
802 }
803#endif
804 return 0;
805 } else {
806// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
807 ListItem *listItem = new ListItem;
808 listItem->m_item = item;
809 listItem->m_sectionItem = getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/);
810 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
811 ListItem *prevItem = itemAtIndex(modelIndex - 1);
812 bool lostItem = false; // Is an item that we requested async but because of model changes
813 // it is no longer attached to any of the existing items (has no prev nor next item)
814 // nor is the first item
815 if (prevItem) {
816 listItem->setY(prevItem->y() + prevItem->height());
817 } else {
818 ListItem *currItem = itemAtIndex(modelIndex);
819 if (currItem) {
820 // There's something already in m_visibleItems at out index, meaning this is an insert, so attach to its top
821 listItem->setY(currItem->y() - listItem->height());
822 } else {
823 ListItem *nextItem = itemAtIndex(modelIndex + 1);
824 if (nextItem) {
825 listItem->setY(nextItem->y() - listItem->height());
826 } else if (modelIndex == 0 && m_headerItem) {
827 listItem->setY(m_headerItem->height());
828 } else if (!m_visibleItems.isEmpty()) {
829 lostItem = true;
830 }
831 }
832 }
833 if (lostItem) {
834 releaseItem(listItem);
835 listItem = nullptr;
836 } else {
837 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() <= contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
838 if (m_visibleItems.isEmpty()) {
839 m_visibleItems << listItem;
840 } else {
841 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
842 }
843 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
844 m_firstVisibleIndex = modelIndex;
845 polish();
846 }
847 if (listItem->m_sectionItem) {
848 QQmlContext *context = QQmlEngine::contextForObject(listItem->m_sectionItem)->parentContext();
849 context->setContextProperty(QLatin1String("delegateIndex"), modelIndex);
850 }
851 adjustMinYExtent();
852 m_contentHeightDirty = true;
853 }
854 return listItem;
855 }
858#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
859void ListViewWithPageHeader::itemCreated(int modelIndex, QQuickItem *item)
861#else
862void ListViewWithPageHeader::itemCreated(int modelIndex, QObject *object)
864 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
865 if (!item) {
866 qWarning() << "ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
867 return;
868 }
869#endif
870// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
871 // Check we are not being taken down and don't paint anything
872 // TODO Check if we still need this in 5.2
873 // For reproduction just inifnite loop testDash or testDashContent
874 if (!QQmlEngine::contextForObject(this)->parentContext())
875 return;
877 item->setParentItem(m_clipItem);
878 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
879 context->setContextProperty(QLatin1String("ListViewWithPageHeader"), this);
880 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
881 if (modelIndex == m_asyncRequestedIndex) {
882 createItem(modelIndex, false);
883 refill();
884 }
887void ListViewWithPageHeader::updateClipItem()
889 m_clipItem->setHeight(height() - m_headerItemShownHeight);
890 m_clipItem->setY(contentY() + m_headerItemShownHeight);
891 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
894void ListViewWithPageHeader::onContentHeightChanged()
896 updateClipItem();
899void ListViewWithPageHeader::onContentWidthChanged()
901 m_clipItem->setWidth(contentItem()->width());
904void ListViewWithPageHeader::onHeightChanged()
906 polish();
910#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
911void ListViewWithPageHeader::onModelUpdated(const QQuickChangeSet &changeSet, bool /*reset*/)
912#else
913void ListViewWithPageHeader::onModelUpdated(const QQmlChangeSet &changeSet, bool /*reset*/)
914#endif
916 // TODO Do something with reset
917// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
918 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
920#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
921 Q_FOREACH(const QQuickChangeSet::Remove &remove, changeSet.removes()) {
922#else
923 Q_FOREACH(const QQmlChangeSet::Remove &remove, changeSet.removes()) {
924#endif
925// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove.index << remove.count;
926 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
927 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
928 // If all the items we are removing are either not created or culled
929 // we have to grow down to avoid viewport changing
930 bool growDown = true;
931 for (int i = 0; growDown && i < remove.count; ++i) {
932 const int modelIndex = remove.index + i;
933 ListItem *item = itemAtIndex(modelIndex);
934 if (item && !item->culled()) {
935 growDown = false;
936 }
937 }
938 for (int i = remove.count - 1; i >= 0; --i) {
939 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
940 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
941 ListItem *item = m_visibleItems[visibleIndex];
942 // Pass the section item down if needed
943 if (item->m_sectionItem && visibleIndex + 1 < m_visibleItems.count()) {
944 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
945 if (!nextItem->m_sectionItem) {
946 nextItem->m_sectionItem = item->m_sectionItem;
947 item->m_sectionItem = nullptr;
948 }
949 }
950 releaseItem(item);
951 m_visibleItems.removeAt(visibleIndex);
952 }
953 }
954 if (growDown) {
955 adjustMinYExtent();
956 } else if (remove.index <= m_firstVisibleIndex) {
957 if (!m_visibleItems.isEmpty()) {
958 // We removed the first item that is the one that positions the rest
959 // position the new first item correctly
960 m_visibleItems.first()->setY(oldFirstValidIndexPos);
961 } else {
962 m_firstVisibleIndex = -1;
963 }
964 }
965 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
966 m_firstVisibleIndex -= remove.count;
967 }
968 for (int i = remove.count - 1; i >= 0; --i) {
969 const int modelIndex = remove.index + i;
970 if (modelIndex == m_asyncRequestedIndex) {
971 m_asyncRequestedIndex = -1;
972 } else if (modelIndex < m_asyncRequestedIndex) {
973 m_asyncRequestedIndex--;
974 }
975 }
976 }
978#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
979 Q_FOREACH(const QQuickChangeSet::Insert &insert, changeSet.inserts()) {
980#else
981 Q_FOREACH(const QQmlChangeSet::Insert &insert, changeSet.inserts()) {
982#endif
983// qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
984 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
985 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
986 if (insertingInValidIndexes || firstItemWithViewOnTop)
987 {
988 // If the items we are adding won't be really visible
989 // we grow up instead of down to not change the viewport
990 bool growUp = false;
991 if (!firstItemWithViewOnTop) {
992 for (int i = 0; i < m_visibleItems.count(); ++i) {
993 if (!m_visibleItems[i]->culled()) {
994 if (insert.index <= m_firstVisibleIndex + i) {
995 growUp = true;
996 }
997 break;
998 }
999 }
1002 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
1003 for (int i = insert.count - 1; i >= 0; --i) {
1004 const int modelIndex = insert.index + i;
1005 ListItem *item = createItem(modelIndex, false);
1006 if (growUp) {
1007 ListItem *firstItem = m_visibleItems.first();
1008 firstItem->setY(firstItem->y() - item->height());
1010 // Adding an item may break a "same section" chain, so check
1011 // if we need adding a new section item
1012 if (m_sectionDelegate) {
1013 ListItem *nextItem = itemAtIndex(modelIndex + 1);
1014 if (nextItem && !nextItem->m_sectionItem) {
1015 nextItem->m_sectionItem = getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/);
1016 if (growUp && nextItem->m_sectionItem) {
1017 ListItem *firstItem = m_visibleItems.first();
1018 firstItem->setY(firstItem->y() - nextItem->m_sectionItem->height());
1023 if (firstItemWithViewOnTop) {
1024 ListItem *firstItem = m_visibleItems.first();
1025 firstItem->setY(oldFirstValidIndexPos);
1027 adjustMinYExtent();
1028 } else if (insert.index <= m_firstVisibleIndex) {
1029 m_firstVisibleIndex += insert.count;
1032 for (int i = insert.count - 1; i >= 0; --i) {
1033 const int modelIndex = insert.index + i;
1034 if (modelIndex <= m_asyncRequestedIndex) {
1035 m_asyncRequestedIndex++;
1040#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
1041 Q_FOREACH(const QQuickChangeSet::Change &change, changeSet.changes()) {
1042#else
1043 Q_FOREACH(const QQmlChangeSet::Change &change, changeSet.changes()) {
1044#endif
1045// qDebug() << "ListViewWithPageHeader::onModelUpdated Change" << change.index << change.count;
1046 for (int i = change.index; i < change.count; ++i) {
1047 ListItem *item = itemAtIndex(i);
1048 if (item && item->m_sectionItem) {
1049 QQmlContext *context = QQmlEngine::contextForObject(item->m_sectionItem)->parentContext();
1050 const QString sectionText = m_delegateModel->stringValue(i, m_sectionProperty);
1051 context->setContextProperty(QLatin1String("section"), sectionText);
1056 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
1057 adjustMinYExtent();
1060 for (int i = 0; i < m_visibleItems.count(); ++i) {
1061 ListItem *item = m_visibleItems[i];
1062 if (item->m_sectionItem) {
1063 QQmlContext *context = QQmlEngine::contextForObject(item->m_sectionItem)->parentContext();
1064 context->setContextProperty(QLatin1String("delegateIndex"), m_firstVisibleIndex + i);
1068 layout();
1069 polish();
1070 m_contentHeightDirty = true;
1073void ListViewWithPageHeader::onShowHeaderAnimationFinished()
1075 m_contentHeightDirty = true;
1076 polish();
1079void ListViewWithPageHeader::itemGeometryChanged(QQuickItem * /*item*/, const QRectF &newGeometry, const QRectF &oldGeometry)
1081 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
1082 if (heightDiff != 0) {
1083 if (oldGeometry.y() + oldGeometry.height() + m_clipItem->y() <= contentY() && !m_visibleItems.isEmpty()) {
1084 ListItem *firstItem = m_visibleItems.first();
1085 firstItem->setY(firstItem->y() - heightDiff);
1086 adjustMinYExtent();
1087 layout();
1089 refill();
1090 adjustMinYExtent();
1091 polish();
1092 m_contentHeightDirty = true;
1096void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
1098 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
1099 if (m_headerItemShownHeight > 0) {
1100 // If the header is shown because of the clip
1101 // Change its size
1102 m_headerItemShownHeight += heightDiff;
1103 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
1104 updateClipItem();
1105 adjustMinYExtent();
1106 } else {
1107 if (oldHeaderY + oldHeaderHeight > contentY()) {
1108 // If the header is shown because its position
1109 // Change its size
1110 ListItem *firstItem = m_visibleItems.first();
1111 firstItem->setY(firstItem->y() + heightDiff);
1112 layout();
1113 } else {
1114 // If the header is not on screen, just change the start of the list
1115 // so the viewport is not changed
1116 adjustMinYExtent();
1122void ListViewWithPageHeader::adjustMinYExtent()
1124 if (m_visibleItems.isEmpty()) {
1125 m_minYExtent = 0;
1126 } else {
1127 qreal nonCreatedHeight = 0;
1128 if (m_firstVisibleIndex != 0) {
1129 // Calculate the average height of items to estimate the position of the list start
1130 const int visibleItems = m_visibleItems.count();
1131 qreal visibleItemsHeight = 0;
1132 Q_FOREACH(ListItem *item, m_visibleItems) {
1133 visibleItemsHeight += item->height();
1135 nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
1136// qDebug() << m_firstVisibleIndex << visibleItemsHeight << visibleItems << nonCreatedHeight;
1138 m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + (m_headerItem ? m_headerItem->implicitHeight() : 0);
1142ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
1144 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
1145 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
1146 return m_visibleItems[visibleIndexedModelIndex];
1148 return nullptr;
1151void ListViewWithPageHeader::layout()
1153 if (m_inLayout)
1154 return;
1156 m_inLayout = true;
1157 if (!m_visibleItems.isEmpty()) {
1158 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
1159 const qreal visibleTo = contentY() + height() - m_clipItem->y();
1161 qreal pos = m_visibleItems.first()->y();
1163// qDebug() << "ListViewWithPageHeader::layout Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
1164 int firstReallyVisibleItem = -1;
1165 int modelIndex = m_firstVisibleIndex;
1166 Q_FOREACH(ListItem *item, m_visibleItems) {
1167 const bool cull = pos + item->height() <= visibleFrom || pos >= visibleTo;
1168 item->setCulled(cull);
1169 item->setY(pos);
1170 if (!cull && firstReallyVisibleItem == -1) {
1171 firstReallyVisibleItem = modelIndex;
1172 if (m_topSectionItem) {
1173 // Positing the top section sticky item is a two step process
1174 // First we set it either we cull it (because it doesn't need to be sticked to the top)
1175 // or stick it to the top
1176 // Then after the loop we'll make sure that if there's another section just below it
1177 // pushed the sticky section up to make it disappear
1178 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
1179 bool showStickySectionItem;
1180 // We need to show the "top section sticky item" when the position at the "top" of the
1181 // viewport is bigger than the start of the position of the first visible item
1182 // i.e. the first visible item starts before the viewport, or when the first
1183 // visible item starts just at the viewport start and it does not have its own section item
1184 if (topSectionStickPos > pos) {
1185 showStickySectionItem = true;
1186 } else if (topSectionStickPos == pos) {
1187 showStickySectionItem = !item->m_sectionItem;
1188 } else {
1189 showStickySectionItem = false;
1191 if (!showStickySectionItem) {
1192 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
1193 if (item->m_sectionItem) {
1194 // This seems it should happen since why would we cull the top section
1195 // if the first visible item has no section header? This only happens briefly
1196 // when increasing the height of a list that is at the bottom, the m_topSectionItem
1197 // gets shown shortly in the next polish call
1198 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(false);
1200 } else {
1201 // Update the top sticky section header
1202 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1203 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
1204 context->setContextProperty(QLatin1String("section"), section);
1206 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
1207 m_topSectionItem->setY(topSectionStickPos);
1208 int delegateIndex = modelIndex;
1209 // Look for the first index with this section text
1210 while (delegateIndex > 0) {
1211 const QString prevSection = m_delegateModel->stringValue(delegateIndex - 1, m_sectionProperty);
1212 if (prevSection != section)
1213 break;
1214 delegateIndex--;
1216 context->setContextProperty(QLatin1String("delegateIndex"), delegateIndex);
1217 if (item->m_sectionItem) {
1218 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(true);
1223 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
1224 const qreal clipFrom = visibleFrom + (!item->m_sectionItem && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
1225 if (!cull && pos < clipFrom) {
1226 context->setContextProperty(QLatin1String("heightToClip"), clipFrom - pos);
1227 } else {
1228 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
1230// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
1231 pos += item->height();
1232 ++modelIndex;
1235 // Second step of section sticky item positioning
1236 // Look at the next section header, check if it's pushing up the sticky one
1237 if (m_topSectionItem) {
1238 if (firstReallyVisibleItem >= 0) {
1239 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
1240 ListItem *item = m_visibleItems[i];
1241 if (item->m_sectionItem) {
1242 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
1243 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
1245 break;
1251 m_inLayout = false;
1254void ListViewWithPageHeader::updatePolish()
1256 // Check we are not being taken down and don't paint anything
1257 // TODO Check if we still need this in 5.2
1258 // For reproduction just inifnite loop testDash or testDashContent
1259 if (!QQmlEngine::contextForObject(this)->parentContext())
1260 return;
1262 Q_FOREACH(ListItem *item, m_itemsToRelease)
1263 reallyReleaseItem(item);
1264 m_itemsToRelease.clear();
1266 if (!model())
1267 return;
1269 layout();
1271 refill();
1273 if (m_contentHeightDirty) {
1274 qreal contentHeight;
1275 if (m_visibleItems.isEmpty()) {
1276 contentHeight = m_headerItem ? m_headerItem->height() : 0;
1277 } else {
1278 const int modelCount = model()->rowCount();
1279 const int visibleItems = m_visibleItems.count();
1280 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
1281 qreal nonCreatedHeight = 0;
1282 if (lastValidIndex != modelCount - 1) {
1283 const int visibleItems = m_visibleItems.count();
1284 qreal visibleItemsHeight = 0;
1285 Q_FOREACH(ListItem *item, m_visibleItems) {
1286 visibleItemsHeight += item->height();
1288 const int unknownSizes = modelCount - (m_firstVisibleIndex + visibleItems);
1289 nonCreatedHeight = unknownSizes * visibleItemsHeight / visibleItems;
1291 ListItem *item = m_visibleItems.last();
1292 contentHeight = nonCreatedHeight + item->y() + item->height() + m_clipItem->y();
1293 if (m_firstVisibleIndex != 0) {
1294 // Make sure that if we are shrinking we tell the view we still fit
1295 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
1299 m_contentHeightDirty = false;
1300 adjustMinYExtent();
1301 m_inContentHeightKeepHeaderShown = m_headerItem && m_headerItem->y() == contentY();
1302 setContentHeight(contentHeight);
1303 m_inContentHeightKeepHeaderShown = false;
1307#include "moc_listviewwithpageheader.cpp"