on 1663e5c help sign in
unity8/plugins/DashViews/listviewwithpageheader.cpp
1300 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);
371void ListViewWithPageHeader::showHeader()
373 if (!m_headerItem)
374 return;
376 auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
377 if (to != contentY()) {
378 const bool headerShownByItsOwn = contentY() < m_headerItem->y() + m_headerItem->height();
379 if (headerShownByItsOwn && m_headerItemShownHeight == 0) {
380 // We are not clipping since we are just at the top of the viewport
381 // but because of the showHeader animation we will need to, so
382 // enable the clipping without logically moving the items
383 m_headerItemShownHeight = m_headerItem->y() + m_headerItem->height() - contentY();
384 if (!m_visibleItems.isEmpty()) {
385 updateClipItem();
386 ListItem *firstItem = m_visibleItems.first();
387 firstItem->setY(firstItem->y() - m_headerItemShownHeight);
388 layout();
389 }
390 }
391 m_contentYAnimation->setTo(to);
392 contentYAnimationType = ContentYAnimationShowHeader;
393 m_contentYAnimation->start();
394 }
397QQuickItem *ListViewWithPageHeader::item(int modelIndex) const
399 ListItem *item = itemAtIndex(modelIndex);
400 if (item)
401 return item->m_item;
402 else
403 return nullptr;
406bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex)
408 ListItem *listItem = itemAtIndex(modelIndex);
409 if (listItem) {
410 return maximizeVisibleArea(listItem, listItem->height());
411 }
413 return false;
416bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex, int itemHeight)
418 if (itemHeight < 0)
419 return false;
421 ListItem *listItem = itemAtIndex(modelIndex);
422 if (listItem) {
423 return maximizeVisibleArea(listItem, itemHeight + (listItem->m_sectionItem ? listItem->m_sectionItem->height() : 0));
424 }
426 return false;
429bool ListViewWithPageHeader::maximizeVisibleArea(ListItem *listItem, int listItemHeight)
431 if (listItem) {
432 layout();
433 const auto listItemY = m_clipItem->y() + listItem->y();
434 if (listItemY > contentY() && listItemY + listItemHeight > contentY() + height()) {
435 // we can scroll the list up to show more stuff
436 const auto to = qMin(listItemY, listItemY + listItemHeight - height());
437 m_contentYAnimation->setTo(to);
438 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
439 m_contentYAnimation->start();
440 } else if ((listItemY < contentY() && listItemY + listItemHeight < contentY() + height()) ||
441 (m_topSectionItem && !listItem->m_sectionItem && listItemY - m_topSectionItem->height() < contentY() && listItemY + listItemHeight < contentY() + height()))
442 {
443 // we can scroll the list down to show more stuff
444 auto realVisibleListItemY = listItemY;
445 if (m_topSectionItem) {
446 // If we are showing the top section sticky item and this item doesn't have a section
447 // item we have to make sure to scroll it a bit more so that it is not underlapping
448 // the top section sticky item
449 bool topSectionShown = !QQuickItemPrivate::get(m_topSectionItem)->culled;
450 if (topSectionShown && !listItem->m_sectionItem) {
451 realVisibleListItemY -= m_topSectionItem->height();
452 }
453 }
454 const auto to = qMax(realVisibleListItemY, listItemY + listItemHeight - height());
455 m_contentYAnimation->setTo(to);
456 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
457 m_contentYAnimation->start();
458 }
459 return true;
460 }
461 return false;
464qreal ListViewWithPageHeader::minYExtent() const
466// qDebug() << "ListViewWithPageHeader::minYExtent" << m_minYExtent;
467 return m_minYExtent;
470void ListViewWithPageHeader::componentComplete()
472 if (m_delegateModel)
473 m_delegateModel->componentComplete();
475 QQuickFlickable::componentComplete();
477 polish();
480void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
482 // Check we are not being taken down and don't paint anything
483 // TODO Check if we still need this in 5.2
484 // For reproduction just inifnite loop testDash or testDashContent
485 if (!QQmlEngine::contextForObject(this)->parentContext())
486 return;
488 QQuickFlickable::viewportMoved(orient);
489// qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
490 qreal diff = m_previousContentY - contentY();
491 const bool showHeaderAnimationRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationShowHeader;
492 if (m_headerItem) {
493 const auto oldHeaderItemShownHeight = m_headerItemShownHeight;
494 if (contentY() < -m_minYExtent) {
495 // Stick the header item to the top when dragging down
496 m_headerItem->setY(contentY());
497 m_headerItem->setHeight(m_headerItem->implicitHeight() + (-m_minYExtent - contentY()));
498 } else {
499 m_headerItem->setHeight(m_headerItem->implicitHeight());
500 // We are going down (but it's not because of the rebound at the end)
501 // (but the header was not shown by it's own position)
502 // or the header is partially shown and we are not doing a maximizeVisibleArea either
503 const bool scrolledUp = m_previousContentY > contentY();
504 const bool notRebounding = contentY() + height() < contentHeight();
505 const bool notShownByItsOwn = contentY() + diff >= m_headerItem->y() + m_headerItem->height();
506 const bool maximizeVisibleAreaRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationMaximizeVisibleArea;
508 if (!scrolledUp && (contentY() == -m_minYExtent || (m_headerItemShownHeight == 0 && m_previousContentY == m_headerItem->y()))) {
509 m_headerItemShownHeight = 0;
510 m_headerItem->setY(-m_minYExtent);
511 } else if ((scrolledUp && notRebounding && notShownByItsOwn && !maximizeVisibleAreaRunning) || (m_headerItemShownHeight > 0) || m_inContentHeightKeepHeaderShown) {
512 if (maximizeVisibleAreaRunning && diff > 0) // If we are maximizing and the header was shown, make sure we hide it
513 m_headerItemShownHeight -= diff;
514 else
515 m_headerItemShownHeight += diff;
516 if (contentY() == -m_minYExtent) {
517 m_headerItemShownHeight = 0;
518 } else {
519 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
520 }
521 if (m_headerItemShownHeight > 0) {
522 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
523 } else {
524 m_headerItem->setY(-m_minYExtent);
525 }
526 }
527 }
528 // We will be changing the clip item, need to accomadate for it
529 // otherwise we move the firstItem down/up twice (unless the
530 // show header animation is running, where we want to keep the viewport stable)
531 if (!showHeaderAnimationRunning) {
532 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
533 } else {
534 diff = -diff;
535 }
536 }
537 if (!m_visibleItems.isEmpty()) {
538 updateClipItem();
539 ListItem *firstItem = m_visibleItems.first();
540 firstItem->setY(firstItem->y() + diff);
541 if (showHeaderAnimationRunning) {
542 adjustMinYExtent();
543 }
544 }
546 m_previousContentY = contentY();
547 layout();
548 polish();
551void ListViewWithPageHeader::createDelegateModel()
553#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
554 m_delegateModel = new QQuickVisualDataModel(qmlContext(this), this);
555 connect(m_delegateModel, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(itemCreated(int,QQuickItem*)));
556#else
557 m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
558 connect(m_delegateModel, SIGNAL(createdItem(int,QObject*)), this, SLOT(itemCreated(int,QObject*)));
559#endif
560 if (isComponentComplete())
561 m_delegateModel->componentComplete();
562 updateWatchedRoles();
565void ListViewWithPageHeader::refill()
567 if (m_inLayout) {
568 return;
569 }
570 if (!isComponentComplete()) {
571 return;
572 }
574 const qreal buffer = height() * bufferRatio;
575 const qreal from = contentY();
576 const qreal to = from + height();
577 const qreal bufferFrom = from - buffer;
578 const qreal bufferTo = to + buffer;
580 bool added = addVisibleItems(from, to, false);
581 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
582 added |= addVisibleItems(bufferFrom, bufferTo, true);
584 if (added || removed) {
585 m_contentHeightDirty = true;
586 }
589bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
591 if (!delegate())
592 return false;
594 if (m_delegateModel->count() == 0)
595 return false;
597 ListItem *item;
598// qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
600 int modelIndex = 0;
601 qreal pos = 0;
602 if (!m_visibleItems.isEmpty()) {
603 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
604 item = m_visibleItems.last();
605 pos = item->y() + item->height() + m_clipItem->y();
606 }
607 bool changed = false;
608// qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
609 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
610// qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
611 if (!(item = createItem(modelIndex, asynchronous)))
612 break;
613 pos += item->height();
614 ++modelIndex;
615 changed = true;
616 }
618 modelIndex = 0;
619 pos = 0;
620 if (!m_visibleItems.isEmpty()) {
621 modelIndex = m_firstVisibleIndex - 1;
622 item = m_visibleItems.first();
623 pos = item->y() + m_clipItem->y();
624 }
625 while (modelIndex >= 0 && pos > fillFrom) {
626// qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
627 if (!(item = createItem(modelIndex, asynchronous)))
628 break;
629 pos -= item->height();
630 --modelIndex;
631 changed = true;
632 }
634 return changed;
637void ListViewWithPageHeader::reallyReleaseItem(ListItem *listItem)
639 QQuickItem *item = listItem->m_item;
640#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
641 QQuickVisualModel::ReleaseFlags flags = m_delegateModel->release(item);
642 if (flags & QQuickVisualModel::Destroyed) {
643#else
644 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
645 if (flags & QQmlDelegateModel::Destroyed) {
646#endif
647 item->setParentItem(nullptr);
648 }
649 delete listItem->m_sectionItem;
650 delete listItem;
653void ListViewWithPageHeader::releaseItem(ListItem *listItem)
655 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(listItem->m_item);
656 itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
657 m_itemsToRelease << listItem;
660void ListViewWithPageHeader::updateWatchedRoles()
662 if (m_delegateModel) {
663 QList<QByteArray> roles;
664 if (!m_sectionProperty.isEmpty())
665 roles << m_sectionProperty.toUtf8();
666 m_delegateModel->setWatchedRoles(roles);
667 }
670QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
672 if (!m_sectionDelegate)
673 return nullptr;
675 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
676 if (modelIndex > 0) {
677 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
678 if (section == prevSection)
679 return nullptr;
680 }
681 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
682 // Already inserted items can't steal next section header
683 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
684 if (section == nextSection) {
685 // Steal the section header
686 ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
687 if (nextItem) {
688 QQuickItem *sectionItem = nextItem->m_sectionItem;
689 nextItem->m_sectionItem = nullptr;
690 return sectionItem;
691 }
692 }
693 }
695 return getSectionItem(section);
698QQuickItem *ListViewWithPageHeader::getSectionItem(const QString &sectionText)
700 QQuickItem *sectionItem = nullptr;
702 QQmlContext *creationContext = m_sectionDelegate->creationContext();
703 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
704 context->setContextProperty(QLatin1String("section"), sectionText);
705 context->setContextProperty(QLatin1String("delegateIndex"), -1);
706 QObject *nobj = m_sectionDelegate->beginCreate(context);
707 if (nobj) {
708 QQml_setParent_noEvent(context, nobj);
709 sectionItem = qobject_cast<QQuickItem *>(nobj);
710 if (!sectionItem) {
711 delete nobj;
712 } else {
713 sectionItem->setZ(2);
714 QQml_setParent_noEvent(sectionItem, m_clipItem);
715 sectionItem->setParentItem(m_clipItem);
716 }
717 } else {
718 delete context;
719 }
720 m_sectionDelegate->completeCreate();
722 // TODO attach to sectionItem so we can accomodate to it changing its height
724 return sectionItem;
727bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
729// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
730 // Do not remove items if we are overshooting up or down, since we'll come back
731 // to the "stable" position and delete/create items without any reason
732 if (contentY() < -m_minYExtent) {
733 return false;
734 } else if (contentY() + height() > contentHeight()) {
735 return false;
736 }
737 bool changed = false;
739 bool foundVisible = false;
740 int i = 0;
741 int removedItems = 0;
742 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
743 while (i < m_visibleItems.count()) {
744 ListItem *item = m_visibleItems[i];
745 const qreal pos = item->y() + m_clipItem->y();
746// qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
747 if (pos + item->height() < bufferFrom || pos > bufferTo) {
748// qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
749 releaseItem(item);
750 m_visibleItems.removeAt(i);
751 changed = true;
752 ++removedItems;
753 } else {
754 if (!foundVisible) {
755 foundVisible = true;
756 const int itemIndex = m_firstVisibleIndex + removedItems + i;
757 m_firstVisibleIndex = itemIndex;
758 }
759 ++i;
760 }
761 }
762 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
763 adjustMinYExtent();
764 }
766 return changed;
769ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
771// qDebug() << "CREATE ITEM" << modelIndex;
772 if (asynchronous && m_asyncRequestedIndex != -1)
773 return nullptr;
775 m_asyncRequestedIndex = -1;
776#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
777 QQuickItem *item = m_delegateModel->item(modelIndex, asynchronous);
778#else
779 QObject* object = m_delegateModel->object(modelIndex, asynchronous);
780 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
781#endif
782 if (!item) {
783#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
784 m_asyncRequestedIndex = modelIndex;
785#else
786 if (object) {
787 m_delegateModel->release(object);
788 if (!m_delegateValidated) {
789 m_delegateValidated = true;
790 QObject* delegateObj = delegate();
791 qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
792 }
793 } else {
794 m_asyncRequestedIndex = modelIndex;
795 }
796#endif
797 return 0;
798 } else {
799// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
800 ListItem *listItem = new ListItem;
801 listItem->m_item = item;
802 listItem->m_sectionItem = getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/);
803 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
804 ListItem *prevItem = itemAtIndex(modelIndex - 1);
805 bool lostItem = false; // Is an item that we requested async but because of model changes
806 // it is no longer attached to any of the existing items (has no prev nor next item)
807 // nor is the first item
808 if (prevItem) {
809 listItem->setY(prevItem->y() + prevItem->height());
810 } else {
811 ListItem *currItem = itemAtIndex(modelIndex);
812 if (currItem) {
813 // There's something already in m_visibleItems at out index, meaning this is an insert, so attach to its top
814 listItem->setY(currItem->y() - listItem->height());
815 } else {
816 ListItem *nextItem = itemAtIndex(modelIndex + 1);
817 if (nextItem) {
818 listItem->setY(nextItem->y() - listItem->height());
819 } else if (modelIndex == 0 && m_headerItem) {
820 listItem->setY(m_headerItem->height());
821 } else if (!m_visibleItems.isEmpty()) {
822 lostItem = true;
823 }
824 }
825 }
826 if (lostItem) {
827 releaseItem(listItem);
828 listItem = nullptr;
829 } else {
830 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() <= contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
831 if (m_visibleItems.isEmpty()) {
832 m_visibleItems << listItem;
833 } else {
834 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
835 }
836 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
837 m_firstVisibleIndex = modelIndex;
838 polish();
839 }
840 if (listItem->m_sectionItem) {
841 QQmlContext *context = QQmlEngine::contextForObject(listItem->m_sectionItem)->parentContext();
842 context->setContextProperty(QLatin1String("delegateIndex"), modelIndex);
843 }
844 adjustMinYExtent();
845 m_contentHeightDirty = true;
846 }
847 return listItem;
848 }
851#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
852void ListViewWithPageHeader::itemCreated(int modelIndex, QQuickItem *item)
854#else
855void ListViewWithPageHeader::itemCreated(int modelIndex, QObject *object)
857 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
858 if (!item) {
859 qWarning() << "ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
860 return;
861 }
862#endif
863// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
864 // Check we are not being taken down and don't paint anything
865 // TODO Check if we still need this in 5.2
866 // For reproduction just inifnite loop testDash or testDashContent
867 if (!QQmlEngine::contextForObject(this)->parentContext())
868 return;
870 item->setParentItem(m_clipItem);
871 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
872 context->setContextProperty(QLatin1String("ListViewWithPageHeader"), this);
873 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
874 if (modelIndex == m_asyncRequestedIndex) {
875 createItem(modelIndex, false);
876 refill();
877 }
880void ListViewWithPageHeader::updateClipItem()
882 m_clipItem->setHeight(height() - m_headerItemShownHeight);
883 m_clipItem->setY(contentY() + m_headerItemShownHeight);
884 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
887void ListViewWithPageHeader::onContentHeightChanged()
889 updateClipItem();
892void ListViewWithPageHeader::onContentWidthChanged()
894 m_clipItem->setWidth(contentItem()->width());
897void ListViewWithPageHeader::onHeightChanged()
899 polish();
903#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
904void ListViewWithPageHeader::onModelUpdated(const QQuickChangeSet &changeSet, bool /*reset*/)
905#else
906void ListViewWithPageHeader::onModelUpdated(const QQmlChangeSet &changeSet, bool /*reset*/)
907#endif
909 // TODO Do something with reset
910// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
911 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
913#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
914 Q_FOREACH(const QQuickChangeSet::Remove &remove, changeSet.removes()) {
915#else
916 Q_FOREACH(const QQmlChangeSet::Remove &remove, changeSet.removes()) {
917#endif
918// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove.index << remove.count;
919 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
920 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
921 // If all the items we are removing are either not created or culled
922 // we have to grow down to avoid viewport changing
923 bool growDown = true;
924 for (int i = 0; growDown && i < remove.count; ++i) {
925 const int modelIndex = remove.index + i;
926 ListItem *item = itemAtIndex(modelIndex);
927 if (item && !item->culled()) {
928 growDown = false;
929 }
930 }
931 for (int i = remove.count - 1; i >= 0; --i) {
932 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
933 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
934 ListItem *item = m_visibleItems[visibleIndex];
935 // Pass the section item down if needed
936 if (item->m_sectionItem && visibleIndex + 1 < m_visibleItems.count()) {
937 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
938 if (!nextItem->m_sectionItem) {
939 nextItem->m_sectionItem = item->m_sectionItem;
940 item->m_sectionItem = nullptr;
941 }
942 }
943 releaseItem(item);
944 m_visibleItems.removeAt(visibleIndex);
945 }
946 }
947 if (growDown) {
948 adjustMinYExtent();
949 } else if (remove.index <= m_firstVisibleIndex) {
950 if (!m_visibleItems.isEmpty()) {
951 // We removed the first item that is the one that positions the rest
952 // position the new first item correctly
953 m_visibleItems.first()->setY(oldFirstValidIndexPos);
954 } else {
955 m_firstVisibleIndex = -1;
956 }
957 }
958 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
959 m_firstVisibleIndex -= remove.count;
960 }
961 for (int i = remove.count - 1; i >= 0; --i) {
962 const int modelIndex = remove.index + i;
963 if (modelIndex == m_asyncRequestedIndex) {
964 m_asyncRequestedIndex = -1;
965 } else if (modelIndex < m_asyncRequestedIndex) {
966 m_asyncRequestedIndex--;
967 }
968 }
969 }
971#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
972 Q_FOREACH(const QQuickChangeSet::Insert &insert, changeSet.inserts()) {
973#else
974 Q_FOREACH(const QQmlChangeSet::Insert &insert, changeSet.inserts()) {
975#endif
976// qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
977 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
978 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
979 if (insertingInValidIndexes || firstItemWithViewOnTop)
980 {
981 // If the items we are adding won't be really visible
982 // we grow up instead of down to not change the viewport
983 bool growUp = false;
984 if (!firstItemWithViewOnTop) {
985 for (int i = 0; i < m_visibleItems.count(); ++i) {
986 if (!m_visibleItems[i]->culled()) {
987 if (insert.index <= m_firstVisibleIndex + i) {
988 growUp = true;
989 }
990 break;
991 }
992 }
993 }
995 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
996 for (int i = insert.count - 1; i >= 0; --i) {
997 const int modelIndex = insert.index + i;
998 ListItem *item = createItem(modelIndex, false);
999 if (growUp) {
1000 ListItem *firstItem = m_visibleItems.first();
1001 firstItem->setY(firstItem->y() - item->height());
1003 // Adding an item may break a "same section" chain, so check
1004 // if we need adding a new section item
1005 if (m_sectionDelegate) {
1006 ListItem *nextItem = itemAtIndex(modelIndex + 1);
1007 if (nextItem && !nextItem->m_sectionItem) {
1008 nextItem->m_sectionItem = getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/);
1009 if (growUp && nextItem->m_sectionItem) {
1010 ListItem *firstItem = m_visibleItems.first();
1011 firstItem->setY(firstItem->y() - nextItem->m_sectionItem->height());
1016 if (firstItemWithViewOnTop) {
1017 ListItem *firstItem = m_visibleItems.first();
1018 firstItem->setY(oldFirstValidIndexPos);
1020 adjustMinYExtent();
1021 } else if (insert.index <= m_firstVisibleIndex) {
1022 m_firstVisibleIndex += insert.count;
1025 for (int i = insert.count - 1; i >= 0; --i) {
1026 const int modelIndex = insert.index + i;
1027 if (modelIndex <= m_asyncRequestedIndex) {
1028 m_asyncRequestedIndex++;
1033#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
1034 Q_FOREACH(const QQuickChangeSet::Change &change, changeSet.changes()) {
1035#else
1036 Q_FOREACH(const QQmlChangeSet::Change &change, changeSet.changes()) {
1037#endif
1038// qDebug() << "ListViewWithPageHeader::onModelUpdated Change" << change.index << change.count;
1039 for (int i = change.index; i < change.count; ++i) {
1040 ListItem *item = itemAtIndex(i);
1041 if (item && item->m_sectionItem) {
1042 QQmlContext *context = QQmlEngine::contextForObject(item->m_sectionItem)->parentContext();
1043 const QString sectionText = m_delegateModel->stringValue(i, m_sectionProperty);
1044 context->setContextProperty(QLatin1String("section"), sectionText);
1049 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
1050 adjustMinYExtent();
1053 for (int i = 0; i < m_visibleItems.count(); ++i) {
1054 ListItem *item = m_visibleItems[i];
1055 if (item->m_sectionItem) {
1056 QQmlContext *context = QQmlEngine::contextForObject(item->m_sectionItem)->parentContext();
1057 context->setContextProperty(QLatin1String("delegateIndex"), m_firstVisibleIndex + i);
1061 layout();
1062 polish();
1063 m_contentHeightDirty = true;
1066void ListViewWithPageHeader::onShowHeaderAnimationFinished()
1068 m_contentHeightDirty = true;
1069 polish();
1072void ListViewWithPageHeader::itemGeometryChanged(QQuickItem * /*item*/, const QRectF &newGeometry, const QRectF &oldGeometry)
1074 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
1075 if (heightDiff != 0) {
1076 if (oldGeometry.y() + oldGeometry.height() + m_clipItem->y() < contentY() && !m_visibleItems.isEmpty()) {
1077 ListItem *firstItem = m_visibleItems.first();
1078 firstItem->setY(firstItem->y() - heightDiff);
1079 adjustMinYExtent();
1080 layout();
1082 refill();
1083 adjustMinYExtent();
1084 polish();
1085 m_contentHeightDirty = true;
1089void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
1091 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
1092 if (m_headerItemShownHeight > 0) {
1093 // If the header is shown because of the clip
1094 // Change its size
1095 m_headerItemShownHeight += heightDiff;
1096 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
1097 updateClipItem();
1098 adjustMinYExtent();
1099 } else {
1100 if (oldHeaderY + oldHeaderHeight > contentY()) {
1101 // If the header is shown because its position
1102 // Change its size
1103 ListItem *firstItem = m_visibleItems.first();
1104 firstItem->setY(firstItem->y() + heightDiff);
1105 layout();
1106 } else {
1107 // If the header is not on screen, just change the start of the list
1108 // so the viewport is not changed
1109 adjustMinYExtent();
1115void ListViewWithPageHeader::adjustMinYExtent()
1117 if (m_visibleItems.isEmpty()) {
1118 m_minYExtent = 0;
1119 } else {
1120 qreal nonCreatedHeight = 0;
1121 if (m_firstVisibleIndex != 0) {
1122 // Calculate the average height of items to estimate the position of the list start
1123 const int visibleItems = m_visibleItems.count();
1124 qreal visibleItemsHeight = 0;
1125 Q_FOREACH(ListItem *item, m_visibleItems) {
1126 visibleItemsHeight += item->height();
1128 nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
1129// qDebug() << m_firstVisibleIndex << visibleItemsHeight << visibleItems << nonCreatedHeight;
1131 m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + (m_headerItem ? m_headerItem->implicitHeight() : 0);
1135ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
1137 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
1138 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
1139 return m_visibleItems[visibleIndexedModelIndex];
1141 return nullptr;
1144void ListViewWithPageHeader::layout()
1146 if (m_inLayout)
1147 return;
1149 m_inLayout = true;
1150 if (!m_visibleItems.isEmpty()) {
1151 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
1152 const qreal visibleTo = contentY() + height() - m_clipItem->y();
1154 qreal pos = m_visibleItems.first()->y();
1156// qDebug() << "ListViewWithPageHeader::layout Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
1157 int firstReallyVisibleItem = -1;
1158 int modelIndex = m_firstVisibleIndex;
1159 Q_FOREACH(ListItem *item, m_visibleItems) {
1160 const bool cull = pos + item->height() <= visibleFrom || pos >= visibleTo;
1161 item->setCulled(cull);
1162 item->setY(pos);
1163 if (!cull && firstReallyVisibleItem == -1) {
1164 firstReallyVisibleItem = modelIndex;
1165 if (m_topSectionItem) {
1166 // Positing the top section sticky item is a two step process
1167 // First we set it either we cull it (because it doesn't need to be sticked to the top)
1168 // or stick it to the top
1169 // Then after the loop we'll make sure that if there's another section just below it
1170 // pushed the sticky section up to make it disappear
1171 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
1172 bool showStickySectionItem;
1173 // We need to show the "top section sticky item" when the position at the "top" of the
1174 // viewport is bigger than the start of the position of the first visible item
1175 // i.e. the first visible item starts before the viewport, or when the first
1176 // visible item starts just at the viewport start and it does not have its own section item
1177 if (topSectionStickPos > pos) {
1178 showStickySectionItem = true;
1179 } else if (topSectionStickPos == pos) {
1180 showStickySectionItem = !item->m_sectionItem;
1181 } else {
1182 showStickySectionItem = false;
1184 if (!showStickySectionItem) {
1185 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
1186 if (item->m_sectionItem) {
1187 // This seems it should happen since why would we cull the top section
1188 // if the first visible item has no section header? This only happens briefly
1189 // when increasing the height of a list that is at the bottom, the m_topSectionItem
1190 // gets shown shortly in the next polish call
1191 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(false);
1193 } else {
1194 // Update the top sticky section header
1195 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1196 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
1197 context->setContextProperty(QLatin1String("section"), section);
1199 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
1200 m_topSectionItem->setY(topSectionStickPos);
1201 int delegateIndex = modelIndex;
1202 // Look for the first index with this section text
1203 while (delegateIndex > 0) {
1204 const QString prevSection = m_delegateModel->stringValue(delegateIndex - 1, m_sectionProperty);
1205 if (prevSection != section)
1206 break;
1207 delegateIndex--;
1209 context->setContextProperty(QLatin1String("delegateIndex"), delegateIndex);
1210 if (item->m_sectionItem) {
1211 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(true);
1216 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
1217 const qreal clipFrom = visibleFrom + (!item->m_sectionItem && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
1218 if (!cull && pos < clipFrom) {
1219 context->setContextProperty(QLatin1String("heightToClip"), clipFrom - pos);
1220 } else {
1221 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
1223// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
1224 pos += item->height();
1225 ++modelIndex;
1228 // Second step of section sticky item positioning
1229 // Look at the next section header, check if it's pushing up the sticky one
1230 if (m_topSectionItem) {
1231 if (firstReallyVisibleItem >= 0) {
1232 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
1233 ListItem *item = m_visibleItems[i];
1234 if (item->m_sectionItem) {
1235 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
1236 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
1238 break;
1244 m_inLayout = false;
1247void ListViewWithPageHeader::updatePolish()
1249 // Check we are not being taken down and don't paint anything
1250 // TODO Check if we still need this in 5.2
1251 // For reproduction just inifnite loop testDash or testDashContent
1252 if (!QQmlEngine::contextForObject(this)->parentContext())
1253 return;
1255 Q_FOREACH(ListItem *item, m_itemsToRelease)
1256 reallyReleaseItem(item);
1257 m_itemsToRelease.clear();
1259 if (!model())
1260 return;
1262 layout();
1264 refill();
1266 if (m_contentHeightDirty) {
1267 qreal contentHeight;
1268 if (m_visibleItems.isEmpty()) {
1269 contentHeight = m_headerItem ? m_headerItem->height() : 0;
1270 } else {
1271 const int modelCount = model()->rowCount();
1272 const int visibleItems = m_visibleItems.count();
1273 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
1274 qreal nonCreatedHeight = 0;
1275 if (lastValidIndex != modelCount - 1) {
1276 const int visibleItems = m_visibleItems.count();
1277 qreal visibleItemsHeight = 0;
1278 Q_FOREACH(ListItem *item, m_visibleItems) {
1279 visibleItemsHeight += item->height();
1281 const int unknownSizes = modelCount - (m_firstVisibleIndex + visibleItems);
1282 nonCreatedHeight = unknownSizes * visibleItemsHeight / visibleItems;
1284 ListItem *item = m_visibleItems.last();
1285 contentHeight = nonCreatedHeight + item->y() + item->height() + m_clipItem->y();
1286 if (m_firstVisibleIndex != 0) {
1287 // Make sure that if we are shrinking we tell the view we still fit
1288 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
1292 m_contentHeightDirty = false;
1293 adjustMinYExtent();
1294 m_inContentHeightKeepHeaderShown = m_headerItem && m_headerItem->y() == contentY();
1295 setContentHeight(contentHeight);
1296 m_inContentHeightKeepHeaderShown = false;
1300#include "moc_listviewwithpageheader.cpp"