Unity 8
listviewwithpageheader.cpp
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  */
16 
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  */
91 
92 #include "listviewwithpageheader.h"
93 
94 #include <QCoreApplication>
95 #include <QDebug>
96 #include <qqmlinfo.h>
97 #include <qqmlengine.h>
98 #pragma GCC diagnostic push
99 #pragma GCC diagnostic ignored "-pedantic"
100 #include <private/qqmlcontext_p.h>
101 #include <private/qqmldelegatemodel_p.h>
102 #include <private/qqmlglobal_p.h>
103 #include <private/qquickitem_p.h>
104 #include <private/qquickanimation_p.h>
105 #pragma GCC diagnostic pop
106 // #include <private/qquickrectangle_p.h>
107 
108 qreal ListViewWithPageHeader::ListItem::height() const
109 {
110  return m_item->height() + (m_sectionItem ? m_sectionItem->height() : 0);
111 }
112 
113 qreal ListViewWithPageHeader::ListItem::y() const
114 {
115  return m_item->y() - (m_sectionItem ? m_sectionItem->height() : 0);
116 }
117 
118 void ListViewWithPageHeader::ListItem::setY(qreal newY)
119 {
120  if (m_sectionItem) {
121  m_sectionItem->setY(newY);
122  m_item->setY(newY + m_sectionItem->height());
123  } else {
124  m_item->setY(newY);
125  }
126 }
127 
128 bool ListViewWithPageHeader::ListItem::culled() const
129 {
130  return QQuickItemPrivate::get(m_item)->culled;
131 }
132 
133 void ListViewWithPageHeader::ListItem::setCulled(bool culled)
134 {
135  QQuickItemPrivate::get(m_item)->setCulled(culled);
136  if (m_sectionItem)
137  QQuickItemPrivate::get(m_sectionItem)->setCulled(culled);
138 }
139 
140 void ListViewWithPageHeader::ListItem::setSectionItem(QQuickItem *sectionItem)
141 {
142  m_sectionItem = sectionItem;
143 }
144 
145 ListViewWithPageHeader::ListViewWithPageHeader()
146  : m_delegateModel(nullptr)
147  , m_asyncRequestedIndex(-1)
148  , m_delegateValidated(false)
149  , m_firstVisibleIndex(-1)
150  , m_minYExtent(0)
151  , m_contentHeightDirty(false)
152  , m_headerItem(nullptr)
153  , m_previousContentY(0)
154  , m_headerItemShownHeight(0)
155  , m_sectionDelegate(nullptr)
156  , m_topSectionItem(nullptr)
157  , m_forceNoClip(false)
158  , m_inLayout(false)
159  , m_inContentHeightKeepHeaderShown(false)
160  , m_cacheBuffer(0)
161 {
162  m_clipItem = new QQuickItem(contentItem());
163 // m_clipItem = new QQuickRectangle(contentItem());
164 // ((QQuickRectangle*)m_clipItem)->setColor(Qt::gray);
165 
166  m_contentYAnimation = new QQuickNumberAnimation(this);
167  m_contentYAnimation->setEasing(QEasingCurve::OutQuad);
168  m_contentYAnimation->setProperty(QStringLiteral("contentY"));
169  m_contentYAnimation->setDuration(200);
170  m_contentYAnimation->setTargetObject(this);
171 
172  connect(contentItem(), &QQuickItem::widthChanged, this, &ListViewWithPageHeader::onContentWidthChanged);
173  connect(this, &ListViewWithPageHeader::contentHeightChanged, this, &ListViewWithPageHeader::onContentHeightChanged);
174  connect(this, &ListViewWithPageHeader::heightChanged, this, &ListViewWithPageHeader::onHeightChanged);
175  connect(m_contentYAnimation, &QQuickNumberAnimation::runningChanged, this, &ListViewWithPageHeader::contentYAnimationRunningChanged);
176 
177  setFlickableDirection(VerticalFlick);
178 }
179 
180 ListViewWithPageHeader::~ListViewWithPageHeader()
181 {
182 }
183 
184 QAbstractItemModel *ListViewWithPageHeader::model() const
185 {
186  return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
187 }
188 
189 void ListViewWithPageHeader::setModel(QAbstractItemModel *model)
190 {
191  if (model != this->model()) {
192  if (!m_delegateModel) {
193  createDelegateModel();
194  } else {
195  disconnect(m_delegateModel, &QQmlDelegateModel::modelUpdated, this, &ListViewWithPageHeader::onModelUpdated);
196  }
197  m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
198  connect(m_delegateModel, &QQmlDelegateModel::modelUpdated, this, &ListViewWithPageHeader::onModelUpdated);
199  Q_EMIT modelChanged();
200  polish();
201  // TODO?
202 // Q_EMIT contentHeightChanged();
203 // Q_EMIT contentYChanged();
204  }
205 }
206 
207 QQmlComponent *ListViewWithPageHeader::delegate() const
208 {
209  return m_delegateModel ? m_delegateModel->delegate() : nullptr;
210 }
211 
212 void ListViewWithPageHeader::setDelegate(QQmlComponent *delegate)
213 {
214  if (delegate != this->delegate()) {
215  if (!m_delegateModel) {
216  createDelegateModel();
217  }
218 
219  // Cleanup the existing items
220  Q_FOREACH(ListItem *item, m_visibleItems)
221  releaseItem(item);
222  m_visibleItems.clear();
223  initializeValuesForEmptyList();
224 
225  m_delegateModel->setDelegate(delegate);
226 
227  Q_EMIT delegateChanged();
228  m_delegateValidated = false;
229  m_contentHeightDirty = true;
230  polish();
231  }
232 }
233 
234 void ListViewWithPageHeader::initializeValuesForEmptyList()
235 {
236  m_firstVisibleIndex = -1;
237  adjustMinYExtent();
238  setContentY(0);
239  m_clipItem->setY(0);
240  if (m_topSectionItem) {
241  QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
242  }
243 }
244 
245 QQuickItem *ListViewWithPageHeader::header() const
246 {
247  return m_headerItem;
248 }
249 
250 void ListViewWithPageHeader::setHeader(QQuickItem *headerItem)
251 {
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  m_previousHeaderImplicitHeight = m_headerItem->implicitHeight();
265  QQuickItemPrivate::get(m_headerItem)->addItemChangeListener(this, QQuickItemPrivate::ImplicitHeight);
266  }
267  qreal newHeaderHeight = m_headerItem ? m_headerItem->height() : 0;
268  if (!m_visibleItems.isEmpty() && newHeaderHeight != oldHeaderHeight) {
269  headerHeightChanged(newHeaderHeight, oldHeaderHeight, oldHeaderY);
270  polish();
271  m_contentHeightDirty = true;
272  }
273  Q_EMIT headerChanged();
274  }
275 }
276 
277 QQmlComponent *ListViewWithPageHeader::sectionDelegate() const
278 {
279  return m_sectionDelegate;
280 }
281 
282 void ListViewWithPageHeader::setSectionDelegate(QQmlComponent *delegate)
283 {
284  if (delegate != m_sectionDelegate) {
285  // TODO clean existing sections
286 
287  m_sectionDelegate = delegate;
288 
289  m_topSectionItem = getSectionItem(QString(), false /*watchGeometry*/);
290  if (m_topSectionItem) {
291  m_topSectionItem->setZ(3);
292  QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
293  connect(m_topSectionItem, &QQuickItem::heightChanged, this, &ListViewWithPageHeader::stickyHeaderHeightChanged);
294  }
295 
296  // TODO create sections for existing items
297 
298  Q_EMIT sectionDelegateChanged();
299  Q_EMIT stickyHeaderHeightChanged();
300  }
301 }
302 
303 QString ListViewWithPageHeader::sectionProperty() const
304 {
305  return m_sectionProperty;
306 }
307 
308 void ListViewWithPageHeader::setSectionProperty(const QString &property)
309 {
310  if (property != m_sectionProperty) {
311  m_sectionProperty = property;
312 
313  updateWatchedRoles();
314 
315  // TODO recreate sections
316 
317  Q_EMIT sectionPropertyChanged();
318  }
319 }
320 
321 bool ListViewWithPageHeader::forceNoClip() const
322 {
323  return m_forceNoClip;
324 }
325 
326 void ListViewWithPageHeader::setForceNoClip(bool noClip)
327 {
328  if (noClip != m_forceNoClip) {
329  m_forceNoClip = noClip;
330  updateClipItem();
331  Q_EMIT forceNoClipChanged();
332  }
333 }
334 
335 int ListViewWithPageHeader::stickyHeaderHeight() const
336 {
337  return m_topSectionItem ? m_topSectionItem->height() : 0;
338 }
339 
340 qreal ListViewWithPageHeader::headerItemShownHeight() const
341 {
342  return m_headerItemShownHeight;
343 }
344 
345 int ListViewWithPageHeader::cacheBuffer() const
346 {
347  return m_cacheBuffer;
348 }
349 
350 void ListViewWithPageHeader::setCacheBuffer(int cacheBuffer)
351 {
352  if (cacheBuffer < 0) {
353  qmlInfo(this) << "Cannot set a negative cache buffer";
354  return;
355  }
356 
357  if (cacheBuffer != m_cacheBuffer) {
358  m_cacheBuffer = cacheBuffer;
359  Q_EMIT cacheBufferChanged();
360  polish();
361  }
362 }
363 
364 void ListViewWithPageHeader::positionAtBeginning()
365 {
366  if (m_delegateModel->count() <= 0)
367  return;
368 
369  qreal headerHeight = (m_headerItem ? m_headerItem->height() : 0);
370  if (m_firstVisibleIndex != 0) {
371  // TODO This could be optimized by trying to reuse the interesection
372  // of items that may end up intersecting between the existing
373  // m_visibleItems and the items we are creating in the next loop
374  Q_FOREACH(ListItem *item, m_visibleItems)
375  releaseItem(item);
376  m_visibleItems.clear();
377  m_firstVisibleIndex = -1;
378 
379  // Create the item 0, it will be already correctly positioned at createItem()
380  m_clipItem->setY(0);
381  ListItem *item = createItem(0, false);
382  // Create the subsequent items
383  int modelIndex = 1;
384  qreal pos = item->y() + item->height();
385  const qreal bufferTo = height() + m_cacheBuffer;
386  while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
387  if (!(item = createItem(modelIndex, false)))
388  break;
389  pos += item->height();
390  ++modelIndex;
391  }
392 
393  m_previousContentY = m_visibleItems.first()->y() - headerHeight;
394  }
395  setContentY(m_visibleItems.first()->y() + m_clipItem->y() - headerHeight);
396  if (m_headerItem) {
397  // TODO This should not be needed and the code that adjust the m_headerItem position
398  // in viewportMoved() should be enough but in some cases we have not found a way to reproduce
399  // yet the code of viewportMoved() fails so here we make sure that at least if we are calling
400  // positionAtBeginning the header item will be correctly positioned
401  m_headerItem->setY(-m_minYExtent);
402  }
403 }
404 
405 static inline bool uFuzzyCompare(qreal r1, qreal r2)
406 {
407  return qFuzzyCompare(r1, r2) || (qFuzzyIsNull(r1) && qFuzzyIsNull(r2));
408 }
409 
410 void ListViewWithPageHeader::showHeader()
411 {
412  if (!m_headerItem)
413  return;
414 
415  const auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
416  if (!uFuzzyCompare(to, contentY())) {
417  const bool headerShownByItsOwn = contentY() < m_headerItem->y() + m_headerItem->height();
418  if (headerShownByItsOwn && m_headerItemShownHeight == 0) {
419  // We are not clipping since we are just at the top of the viewport
420  // but because of the showHeader animation we will need to, so
421  // enable the clipping without logically moving the items
422  m_headerItemShownHeight = m_headerItem->y() + m_headerItem->height() - contentY();
423  if (!m_visibleItems.isEmpty()) {
424  updateClipItem();
425  ListItem *firstItem = m_visibleItems.first();
426  firstItem->setY(firstItem->y() - m_headerItemShownHeight);
427  layout();
428  }
429  Q_EMIT headerItemShownHeightChanged();
430  }
431  m_contentYAnimation->setTo(to);
432  contentYAnimationType = ContentYAnimationShowHeader;
433  m_contentYAnimation->start();
434  }
435 }
436 
437 int ListViewWithPageHeader::firstCreatedIndex() const
438 {
439  return m_firstVisibleIndex;
440 }
441 
442 int ListViewWithPageHeader::createdItemCount() const
443 {
444  return m_visibleItems.count();
445 }
446 
447 QQuickItem *ListViewWithPageHeader::item(int modelIndex) const
448 {
449  ListItem *item = itemAtIndex(modelIndex);
450  if (item)
451  return item->m_item;
452  else
453  return nullptr;
454 }
455 
456 bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex)
457 {
458  ListItem *listItem = itemAtIndex(modelIndex);
459  if (listItem) {
460  return maximizeVisibleArea(listItem, listItem->height());
461  }
462 
463  return false;
464 }
465 
466 bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex, int itemHeight)
467 {
468  if (itemHeight < 0)
469  return false;
470 
471  ListItem *listItem = itemAtIndex(modelIndex);
472  if (listItem) {
473  return maximizeVisibleArea(listItem, itemHeight + (listItem->sectionItem() ? listItem->sectionItem()->height() : 0));
474  }
475 
476  return false;
477 }
478 
479 bool ListViewWithPageHeader::maximizeVisibleArea(ListItem *listItem, int listItemHeight)
480 {
481  if (listItem) {
482  layout();
483  const auto listItemY = m_clipItem->y() + listItem->y();
484  if (listItemY > contentY() && listItemY + listItemHeight > contentY() + height()) {
485  // we can scroll the list up to show more stuff
486  const auto to = qMin(listItemY, listItemY + listItemHeight - height());
487  m_contentYAnimation->setTo(to);
488  contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
489  m_contentYAnimation->start();
490  } else if ((listItemY < contentY() && listItemY + listItemHeight < contentY() + height()) ||
491  (m_topSectionItem && !listItem->sectionItem() && listItemY - m_topSectionItem->height() < contentY() && listItemY + listItemHeight < contentY() + height()))
492  {
493  // we can scroll the list down to show more stuff
494  auto realVisibleListItemY = listItemY;
495  if (m_topSectionItem) {
496  // If we are showing the top section sticky item and this item doesn't have a section
497  // item we have to make sure to scroll it a bit more so that it is not underlapping
498  // the top section sticky item
499  bool topSectionShown = !QQuickItemPrivate::get(m_topSectionItem)->culled;
500  if (topSectionShown && !listItem->sectionItem()) {
501  realVisibleListItemY -= m_topSectionItem->height();
502  }
503  }
504  const auto to = qMax(realVisibleListItemY, listItemY + listItemHeight - height());
505  m_contentYAnimation->setTo(to);
506  contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
507  m_contentYAnimation->start();
508  }
509  return true;
510  }
511  return false;
512 }
513 
514 qreal ListViewWithPageHeader::minYExtent() const
515 {
516 // qDebug() << "ListViewWithPageHeader::minYExtent" << m_minYExtent;
517  return m_minYExtent;
518 }
519 
520 qreal ListViewWithPageHeader::maxYExtent() const
521 {
522  return height() - contentHeight();
523 }
524 
525 void ListViewWithPageHeader::componentComplete()
526 {
527  if (m_delegateModel)
528  m_delegateModel->componentComplete();
529 
530  QQuickFlickable::componentComplete();
531 
532  polish();
533 }
534 
535 void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
536 {
537  // Check we are not being taken down and don't paint anything
538  // TODO Check if we still need this in 5.2
539  // For reproduction just inifnite loop testDash or testDashContent
540  if (!QQmlEngine::contextForObject(this)->parentContext())
541  return;
542 
543  QQuickFlickable::viewportMoved(orient);
544 // qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
545  const qreal diff = m_previousContentY - contentY();
546  adjustHeader(diff);
547  m_previousContentY = contentY();
548  layout();
549  polish();
550 }
551 
552 void ListViewWithPageHeader::adjustHeader(qreal diff)
553 {
554  const bool showHeaderAnimationRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationShowHeader;
555  if (m_headerItem) {
556  const auto oldHeaderItemShownHeight = m_headerItemShownHeight;
557  if (uFuzzyCompare(contentY(), -m_minYExtent) || contentY() > -m_minYExtent) {
558  m_headerItem->setHeight(m_headerItem->implicitHeight());
559  // We are going down (but it's not because of the rebound at the end)
560  // (but the header was not shown by it's own position)
561  // or the header is partially shown and we are not doing a maximizeVisibleArea either
562  const bool scrolledUp = m_previousContentY > contentY();
563  const bool notRebounding = qRound(contentY() + height()) < qRound(contentHeight());
564  const bool notShownByItsOwn = contentY() + diff >= m_headerItem->y() + m_headerItem->height();
565  const bool maximizeVisibleAreaRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationMaximizeVisibleArea;
566 
567  if (!scrolledUp && (contentY() == -m_minYExtent || (m_headerItemShownHeight == 0 && m_previousContentY == m_headerItem->y()))) {
568  m_headerItemShownHeight = 0;
569  m_headerItem->setY(-m_minYExtent);
570  } else if ((scrolledUp && notRebounding && notShownByItsOwn && !maximizeVisibleAreaRunning) || (m_headerItemShownHeight > 0) || m_inContentHeightKeepHeaderShown) {
571  if (maximizeVisibleAreaRunning && diff > 0) {
572  // If we are maximizing and the header was shown, make sure we hide it
573  m_headerItemShownHeight -= diff;
574  } else {
575  m_headerItemShownHeight += diff;
576  }
577  if (uFuzzyCompare(contentY(), -m_minYExtent)) {
578  m_headerItemShownHeight = 0;
579  } else {
580  m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
581  }
582  if (m_headerItemShownHeight > 0) {
583  if (uFuzzyCompare(m_headerItem->height(), m_headerItemShownHeight)) {
584  m_headerItem->setY(contentY());
585  m_headerItemShownHeight = m_headerItem->height();
586  } else {
587  m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
588  }
589  } else {
590  m_headerItem->setY(-m_minYExtent);
591  }
592  }
593  Q_EMIT headerItemShownHeightChanged();
594  } else {
595  // Stick the header item to the top when dragging down
596  m_headerItem->setY(contentY());
597  m_headerItem->setHeight(m_headerItem->implicitHeight() + (-m_minYExtent - contentY()));
598  }
599  // We will be changing the clip item, need to accomadate for it
600  // otherwise we move the firstItem down/up twice (unless the
601  // show header animation is running, where we want to keep the viewport stable)
602  if (!showHeaderAnimationRunning) {
603  diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
604  } else {
605  diff = -diff;
606  }
607  }
608  if (!m_visibleItems.isEmpty()) {
609  updateClipItem();
610  ListItem *firstItem = m_visibleItems.first();
611  firstItem->setY(firstItem->y() + diff);
612  if (showHeaderAnimationRunning) {
613  adjustMinYExtent();
614  }
615  }
616 }
617 
618 void ListViewWithPageHeader::createDelegateModel()
619 {
620  m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
621  connect(m_delegateModel, &QQmlDelegateModel::createdItem, this, &ListViewWithPageHeader::itemCreated);
622  if (isComponentComplete())
623  m_delegateModel->componentComplete();
624  updateWatchedRoles();
625 }
626 
627 void ListViewWithPageHeader::refill()
628 {
629  if (m_inLayout) {
630  return;
631  }
632  if (!isComponentComplete()) {
633  return;
634  }
635 
636  const qreal from = contentY();
637  const qreal to = from + height();
638  const qreal bufferFrom = from - m_cacheBuffer;
639  const qreal bufferTo = to + m_cacheBuffer;
640 
641  bool added = addVisibleItems(from, to, false);
642  bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
643  added |= addVisibleItems(bufferFrom, bufferTo, true);
644 
645  if (added || removed) {
646  m_contentHeightDirty = true;
647  }
648 }
649 
650 bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
651 {
652  if (!delegate())
653  return false;
654 
655  if (m_delegateModel->count() == 0)
656  return false;
657 
658  ListItem *item;
659 // qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
660 
661  int modelIndex = 0;
662  qreal pos = 0;
663  if (!m_visibleItems.isEmpty()) {
664  modelIndex = m_firstVisibleIndex + m_visibleItems.count();
665  item = m_visibleItems.last();
666  pos = item->y() + item->height() + m_clipItem->y();
667  }
668  bool changed = false;
669 // qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
670  while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
671 // qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
672  if (!(item = createItem(modelIndex, asynchronous)))
673  break;
674  pos += item->height();
675  ++modelIndex;
676  changed = true;
677  }
678 
679  modelIndex = 0;
680  pos = 0;
681  if (!m_visibleItems.isEmpty()) {
682  modelIndex = m_firstVisibleIndex - 1;
683  item = m_visibleItems.first();
684  pos = item->y() + m_clipItem->y();
685  }
686  while (modelIndex >= 0 && pos > fillFrom) {
687 // qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
688  if (!(item = createItem(modelIndex, asynchronous)))
689  break;
690  pos -= item->height();
691  --modelIndex;
692  changed = true;
693  }
694 
695  return changed;
696 }
697 
698 void ListViewWithPageHeader::reallyReleaseItem(ListItem *listItem)
699 {
700  QQuickItem *item = listItem->m_item;
701  QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
702  if (flags & QQmlDelegateModel::Destroyed) {
703  item->setParentItem(nullptr);
704  }
705  listItem->sectionItem()->deleteLater();
706  delete listItem;
707 }
708 
709 void ListViewWithPageHeader::releaseItem(ListItem *listItem)
710 {
711  QQuickItemPrivate::get(listItem->m_item)->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
712  if (listItem->sectionItem()) {
713  QQuickItemPrivate::get(listItem->sectionItem())->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
714  }
715  m_itemsToRelease << listItem;
716 }
717 
718 void ListViewWithPageHeader::updateWatchedRoles()
719 {
720  if (m_delegateModel) {
721  QList<QByteArray> roles;
722  if (!m_sectionProperty.isEmpty())
723  roles << m_sectionProperty.toUtf8();
724  m_delegateModel->setWatchedRoles(roles);
725  }
726 }
727 
728 QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
729 {
730  if (!m_sectionDelegate)
731  return nullptr;
732 
733  const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
734 
735  if (modelIndex > 0) {
736  const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
737  if (section == prevSection)
738  return nullptr;
739  }
740  if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
741  // Already inserted items can't steal next section header
742  const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
743  if (section == nextSection) {
744  // Steal the section header
745  ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
746  if (nextItem) {
747  QQuickItem *sectionItem = nextItem->sectionItem();
748  nextItem->setSectionItem(nullptr);
749  return sectionItem;
750  }
751  }
752  }
753 
754  return getSectionItem(section);
755 }
756 
757 QQuickItem *ListViewWithPageHeader::getSectionItem(const QString &sectionText, bool watchGeometry)
758 {
759  QQuickItem *sectionItem = nullptr;
760 
761  QQmlContext *creationContext = m_sectionDelegate->creationContext();
762  QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
763  QObject *nobj = m_sectionDelegate->beginCreate(context);
764  if (nobj) {
765  QQml_setParent_noEvent(context, nobj);
766  sectionItem = qobject_cast<QQuickItem *>(nobj);
767  if (!sectionItem) {
768  delete nobj;
769  } else {
770  sectionItem->setProperty("text", sectionText);
771  sectionItem->setProperty("delegateIndex", -1);
772  sectionItem->setZ(2);
773  QQml_setParent_noEvent(sectionItem, m_clipItem);
774  sectionItem->setParentItem(m_clipItem);
775  }
776  } else {
777  delete context;
778  }
779  m_sectionDelegate->completeCreate();
780 
781  if (watchGeometry && sectionItem) {
782  QQuickItemPrivate::get(sectionItem)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
783  }
784 
785  return sectionItem;
786 }
787 
788 void ListViewWithPageHeader::updateSectionItem(int modelIndex)
789 {
790  ListItem *item = itemAtIndex(modelIndex);
791  if (item) {
792  const QString sectionText = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
793 
794  bool needSectionHeader = true;
795  // if it is the same section as the previous item need to drop the section
796  if (modelIndex > 0) {
797  const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
798  if (sectionText == prevSection) {
799  needSectionHeader = false;
800  }
801  }
802 
803  if (needSectionHeader) {
804  if (!item->sectionItem()) {
805  item->setSectionItem(getSectionItem(sectionText));
806  } else {
807  item->sectionItem()->setProperty("text", sectionText);
808  }
809  } else {
810  if (item->sectionItem()) {
811  item->sectionItem()->deleteLater();
812  item->setSectionItem(nullptr);
813  }
814  }
815  }
816 }
817 
818 bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
819 {
820 // qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
821  // Do not remove items if we are overshooting up or down, since we'll come back
822  // to the "stable" position and delete/create items without any reason
823  if (contentY() < -m_minYExtent) {
824  return false;
825  } else if (contentY() + height() > contentHeight()) {
826  return false;
827  }
828  bool changed = false;
829 
830  bool foundVisible = false;
831  int i = 0;
832  int removedItems = 0;
833  const auto oldFirstVisibleIndex = m_firstVisibleIndex;
834  while (i < m_visibleItems.count()) {
835  ListItem *item = m_visibleItems[i];
836  const qreal pos = item->y() + m_clipItem->y();
837 // qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
838  if (pos + item->height() < bufferFrom || pos > bufferTo) {
839 // qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
840  releaseItem(item);
841  m_visibleItems.removeAt(i);
842  changed = true;
843  ++removedItems;
844  } else {
845  if (!foundVisible) {
846  foundVisible = true;
847  const int itemIndex = m_firstVisibleIndex + removedItems + i;
848  m_firstVisibleIndex = itemIndex;
849  }
850  ++i;
851  }
852  }
853  if (!foundVisible) {
854  initializeValuesForEmptyList();
855  }
856  if (m_firstVisibleIndex != oldFirstVisibleIndex) {
857  adjustMinYExtent();
858  }
859 
860  return changed;
861 }
862 
863 ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
864 {
865 // qDebug() << "CREATE ITEM" << modelIndex;
866  if (asynchronous && m_asyncRequestedIndex != -1)
867  return nullptr;
868 
869  m_asyncRequestedIndex = -1;
870  QObject* object = m_delegateModel->object(modelIndex, asynchronous);
871  QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
872  if (!item) {
873  if (object) {
874  m_delegateModel->release(object);
875  if (!m_delegateValidated) {
876  m_delegateValidated = true;
877  QObject* delegateObj = delegate();
878  qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
879  }
880  } else {
881  m_asyncRequestedIndex = modelIndex;
882  }
883  return 0;
884  } else {
885 // qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
886  ListItem *listItem = new ListItem;
887  listItem->m_item = item;
888  listItem->setSectionItem(getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/));
889  QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
890  ListItem *prevItem = itemAtIndex(modelIndex - 1);
891  bool lostItem = false; // Is an item that we requested async but because of model changes
892  // it is no longer attached to any of the existing items (has no prev nor next item)
893  // nor is the first item
894  if (prevItem) {
895  listItem->setY(prevItem->y() + prevItem->height());
896  } else {
897  ListItem *currItem = itemAtIndex(modelIndex);
898  if (currItem) {
899  // There's something already in m_visibleItems at out index, meaning this is an insert, so attach to its top
900  listItem->setY(currItem->y() - listItem->height());
901  } else {
902  ListItem *nextItem = itemAtIndex(modelIndex + 1);
903  if (nextItem) {
904  listItem->setY(nextItem->y() - listItem->height());
905  } else if (modelIndex == 0) {
906  listItem->setY(-m_clipItem->y() + (m_headerItem ? m_headerItem->height() : 0));
907  } else if (!m_visibleItems.isEmpty()) {
908  lostItem = true;
909  }
910  }
911  }
912  if (lostItem) {
913  listItem->setCulled(true);
914  releaseItem(listItem);
915  listItem = nullptr;
916  } else {
917  listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() <= contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
918  if (m_visibleItems.isEmpty()) {
919  m_visibleItems << listItem;
920  } else {
921  m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
922  }
923  if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
924  m_firstVisibleIndex = modelIndex;
925  polish();
926  }
927  if (listItem->sectionItem()) {
928  listItem->sectionItem()->setProperty("delegateIndex", modelIndex);
929  }
930  adjustMinYExtent();
931  m_contentHeightDirty = true;
932  }
933  return listItem;
934  }
935 }
936 
937 void ListViewWithPageHeader::itemCreated(int modelIndex, QObject *object)
938 {
939  QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
940  if (!item) {
941  qWarning() << "ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
942  return;
943  }
944 // qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
945  // Check we are not being taken down and don't paint anything
946  // TODO Check if we still need this in 5.2
947  // For reproduction just inifnite loop testDash or testDashContent
948  if (!QQmlEngine::contextForObject(this)->parentContext())
949  return;
950 
951  item->setParentItem(m_clipItem);
952  // FIXME Why do we need the refreshExpressions call?
953  QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
954  QQmlContextPrivate::get(context)->data->refreshExpressions();
955  item->setProperty("heightToClip", QVariant::fromValue<int>(0));
956  if (modelIndex == m_asyncRequestedIndex) {
957  createItem(modelIndex, false);
958  refill();
959  }
960 }
961 
962 void ListViewWithPageHeader::updateClipItem()
963 {
964  m_clipItem->setHeight(height() - m_headerItemShownHeight);
965  m_clipItem->setY(contentY() + m_headerItemShownHeight);
966  m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
967 }
968 
969 void ListViewWithPageHeader::onContentHeightChanged()
970 {
971  updateClipItem();
972 }
973 
974 void ListViewWithPageHeader::onContentWidthChanged()
975 {
976  m_clipItem->setWidth(contentItem()->width());
977 }
978 
979 void ListViewWithPageHeader::onHeightChanged()
980 {
981  polish();
982 }
983 
984 
985 void ListViewWithPageHeader::onModelUpdated(const QQmlChangeSet &changeSet, bool /*reset*/)
986 {
987  // TODO Do something with reset
988 // qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
989  const auto oldFirstVisibleIndex = m_firstVisibleIndex;
990 
991  Q_FOREACH(const QQmlChangeSet::Change remove, changeSet.removes()) {
992 // qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove.index << remove.count;
993  if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
994  const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
995  // If all the items we are removing are either not created or culled
996  // we have to grow down to avoid viewport changing
997  bool growDown = true;
998  for (int i = 0; growDown && i < remove.count; ++i) {
999  const int modelIndex = remove.index + i;
1000  ListItem *item = itemAtIndex(modelIndex);
1001  if (item && !item->culled()) {
1002  growDown = false;
1003  }
1004  }
1005  for (int i = remove.count - 1; i >= 0; --i) {
1006  const int visibleIndex = remove.index + i - m_firstVisibleIndex;
1007  if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
1008  ListItem *item = m_visibleItems[visibleIndex];
1009  // Pass the section item down if needed
1010  if (item->sectionItem() && visibleIndex + 1 < m_visibleItems.count()) {
1011  ListItem *nextItem = m_visibleItems[visibleIndex + 1];
1012  if (!nextItem->sectionItem()) {
1013  nextItem->setSectionItem(item->sectionItem());
1014  item->setSectionItem(nullptr);
1015  }
1016  }
1017  releaseItem(item);
1018  m_visibleItems.removeAt(visibleIndex);
1019  }
1020  }
1021  if (growDown) {
1022  adjustMinYExtent();
1023  } else if (remove.index <= m_firstVisibleIndex && !m_visibleItems.isEmpty()) {
1024  m_visibleItems.first()->setY(oldFirstValidIndexPos);
1025  }
1026  if (m_visibleItems.isEmpty()) {
1027  m_firstVisibleIndex = -1;
1028  } else {
1029  m_firstVisibleIndex -= qMax(0, m_firstVisibleIndex - remove.index);
1030  }
1031  } else if (remove.index + remove.count <= m_firstVisibleIndex) {
1032  m_firstVisibleIndex -= remove.count;
1033  }
1034  for (int i = remove.count - 1; i >= 0; --i) {
1035  const int modelIndex = remove.index + i;
1036  if (modelIndex == m_asyncRequestedIndex) {
1037  m_asyncRequestedIndex = -1;
1038  } else if (modelIndex < m_asyncRequestedIndex) {
1039  m_asyncRequestedIndex--;
1040  }
1041  }
1042  }
1043 
1044  Q_FOREACH(const QQmlChangeSet::Change insert, changeSet.inserts()) {
1045 // qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
1046  const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
1047  const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
1048  if (insertingInValidIndexes || firstItemWithViewOnTop)
1049  {
1050  // If the items we are adding won't be really visible
1051  // we grow up instead of down to not change the viewport
1052  bool growUp = false;
1053  if (!firstItemWithViewOnTop) {
1054  for (int i = 0; i < m_visibleItems.count(); ++i) {
1055  if (!m_visibleItems[i]->culled()) {
1056  if (insert.index <= m_firstVisibleIndex + i) {
1057  growUp = true;
1058  }
1059  break;
1060  }
1061  }
1062  }
1063 
1064  const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
1065  for (int i = insert.count - 1; i >= 0; --i) {
1066  const int modelIndex = insert.index + i;
1067  ListItem *item = createItem(modelIndex, false);
1068  if (growUp) {
1069  ListItem *firstItem = m_visibleItems.first();
1070  firstItem->setY(firstItem->y() - item->height());
1071  }
1072  // Adding an item may break a "same section" chain, so check
1073  // if we need adding a new section item
1074  if (m_sectionDelegate) {
1075  ListItem *nextItem = itemAtIndex(modelIndex + 1);
1076  if (nextItem && !nextItem->sectionItem()) {
1077  nextItem->setSectionItem(getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/));
1078  if (growUp && nextItem->sectionItem()) {
1079  ListItem *firstItem = m_visibleItems.first();
1080  firstItem->setY(firstItem->y() - nextItem->sectionItem()->height());
1081  }
1082  }
1083  }
1084  }
1085  if (firstItemWithViewOnTop) {
1086  ListItem *firstItem = m_visibleItems.first();
1087  firstItem->setY(oldFirstValidIndexPos);
1088  }
1089  adjustMinYExtent();
1090  } else if (insert.index <= m_firstVisibleIndex) {
1091  m_firstVisibleIndex += insert.count;
1092  }
1093 
1094  for (int i = insert.count - 1; i >= 0; --i) {
1095  const int modelIndex = insert.index + i;
1096  if (modelIndex <= m_asyncRequestedIndex) {
1097  m_asyncRequestedIndex++;
1098  }
1099  }
1100  }
1101 
1102  Q_FOREACH(const QQmlChangeSet::Change change, changeSet.changes()) {
1103  for (int i = change.start(); i < change.end(); ++i) {
1104  updateSectionItem(i);
1105  }
1106  // Also update the section header for the next item after the change since it may be influenced
1107  updateSectionItem(change.end());
1108  }
1109 
1110  if (m_firstVisibleIndex != oldFirstVisibleIndex) {
1111  if (m_visibleItems.isEmpty()) {
1112  initializeValuesForEmptyList();
1113  } else {
1114  adjustMinYExtent();
1115  }
1116  }
1117 
1118  for (int i = 0; i < m_visibleItems.count(); ++i) {
1119  ListItem *item = m_visibleItems[i];
1120  if (item->sectionItem()) {
1121  item->sectionItem()->setProperty("delegateIndex", m_firstVisibleIndex + i);
1122  }
1123  }
1124 
1125  layout();
1126  polish();
1127  m_contentHeightDirty = true;
1128 }
1129 
1130 void ListViewWithPageHeader::contentYAnimationRunningChanged(bool running)
1131 {
1132  setInteractive(!running);
1133  if (!running) {
1134  m_contentHeightDirty = true;
1135  polish();
1136  }
1137 }
1138 
1139 void ListViewWithPageHeader::itemGeometryChanged(QQuickItem *item, const QRectF &newGeometry, const QRectF &oldGeometry)
1140 {
1141  const qreal heightDiff = newGeometry.height() - oldGeometry.height();
1142  if (heightDiff != 0) {
1143  if (!m_visibleItems.isEmpty()) {
1144  ListItem *firstItem = m_visibleItems.first();
1145  const auto prevFirstItemY = firstItem->y();
1146  if (!m_inContentHeightKeepHeaderShown && oldGeometry.y() + oldGeometry.height() + m_clipItem->y() <= contentY()) {
1147  firstItem->setY(firstItem->y() - heightDiff);
1148  } else if (item == firstItem->sectionItem()) {
1149  firstItem->setY(firstItem->y() + heightDiff);
1150  }
1151 
1152  if (firstItem->y() != prevFirstItemY) {
1153  adjustMinYExtent();
1154  layout();
1155  }
1156  }
1157  refill();
1158  adjustMinYExtent();
1159  polish();
1160  m_contentHeightDirty = true;
1161  }
1162 }
1163 
1164 void ListViewWithPageHeader::itemImplicitHeightChanged(QQuickItem *item)
1165 {
1166  if (item == m_headerItem) {
1167  const qreal diff = m_headerItem->implicitHeight() - m_previousHeaderImplicitHeight;
1168  if (diff != 0) {
1169  adjustHeader(diff);
1170  m_previousHeaderImplicitHeight = m_headerItem->implicitHeight();
1171  layout();
1172  polish();
1173  m_contentHeightDirty = true;
1174  }
1175  }
1176 }
1177 
1178 void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
1179 {
1180  const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
1181  if (m_headerItemShownHeight > 0) {
1182  // If the header is shown because of the clip
1183  // Change its size
1184  m_headerItemShownHeight += heightDiff;
1185  m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
1186  updateClipItem();
1187  adjustMinYExtent();
1188  Q_EMIT headerItemShownHeightChanged();
1189  } else {
1190  if (oldHeaderY + oldHeaderHeight > contentY()) {
1191  // If the header is shown because its position
1192  // Change its size
1193  ListItem *firstItem = m_visibleItems.first();
1194  firstItem->setY(firstItem->y() + heightDiff);
1195  layout();
1196  } else {
1197  // If the header is not on screen, just change the start of the list
1198  // so the viewport is not changed
1199  adjustMinYExtent();
1200  }
1201  }
1202 }
1203 
1204 
1205 void ListViewWithPageHeader::adjustMinYExtent()
1206 {
1207  if (m_visibleItems.isEmpty() || (contentHeight() + m_minYExtent < height())) {
1208  m_minYExtent = 0;
1209  } else {
1210  qreal nonCreatedHeight = 0;
1211  if (m_firstVisibleIndex != 0) {
1212  // Calculate the average height of items to estimate the position of the list start
1213  const int visibleItems = m_visibleItems.count();
1214  qreal visibleItemsHeight = 0;
1215  Q_FOREACH(ListItem *item, m_visibleItems) {
1216  visibleItemsHeight += item->height();
1217  }
1218  nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
1219 // qDebug() << m_firstVisibleIndex << visibleItemsHeight << visibleItems << nonCreatedHeight;
1220  }
1221  const qreal headerHeight = (m_headerItem ? m_headerItem->implicitHeight() : 0);
1222  m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + headerHeight;
1223  if (m_minYExtent != 0 && qFuzzyIsNull(m_minYExtent)) {
1224  m_minYExtent = 0;
1225  m_visibleItems.first()->setY(nonCreatedHeight - m_clipItem->y() + headerHeight);
1226  }
1227  }
1228 }
1229 
1230 ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
1231 {
1232  const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
1233  if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
1234  return m_visibleItems[visibleIndexedModelIndex];
1235 
1236  return nullptr;
1237 }
1238 
1239 void ListViewWithPageHeader::layout()
1240 {
1241  if (m_inLayout)
1242  return;
1243 
1244  m_inLayout = true;
1245  if (!m_visibleItems.isEmpty()) {
1246  const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
1247  const qreal visibleTo = contentY() + height() - m_clipItem->y();
1248 
1249  qreal pos = m_visibleItems.first()->y();
1250 
1251 // qDebug() << "ListViewWithPageHeader::layout Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
1252  int firstReallyVisibleItem = -1;
1253  int modelIndex = m_firstVisibleIndex;
1254  Q_FOREACH(ListItem *item, m_visibleItems) {
1255  const bool cull = pos + item->height() <= visibleFrom || pos >= visibleTo;
1256  item->setCulled(cull);
1257  item->setY(pos);
1258  if (!cull && firstReallyVisibleItem == -1) {
1259  firstReallyVisibleItem = modelIndex;
1260  if (m_topSectionItem) {
1261  // Positing the top section sticky item is a two step process
1262  // First we set it either we cull it (because it doesn't need to be sticked to the top)
1263  // or stick it to the top
1264  // Then after the loop we'll make sure that if there's another section just below it
1265  // pushed the sticky section up to make it disappear
1266  const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
1267  bool showStickySectionItem;
1268  // We need to show the "top section sticky item" when the position at the "top" of the
1269  // viewport is bigger than the start of the position of the first visible item
1270  // i.e. the first visible item starts before the viewport, or when the first
1271  // visible item starts just at the viewport start and it does not have its own section item
1272  if (topSectionStickPos > pos) {
1273  showStickySectionItem = true;
1274  } else if (topSectionStickPos == pos) {
1275  showStickySectionItem = !item->sectionItem();
1276  } else {
1277  showStickySectionItem = false;
1278  }
1279  if (!showStickySectionItem) {
1280  QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
1281  if (item->sectionItem()) {
1282  // This seems it should happen since why would we cull the top section
1283  // if the first visible item has no section header? This only happens briefly
1284  // when increasing the height of a list that is at the bottom, the m_topSectionItem
1285  // gets shown shortly in the next polish call
1286  QQuickItemPrivate::get(item->sectionItem())->setCulled(false);
1287  }
1288  } else {
1289  // Update the top sticky section header
1290  const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1291  m_topSectionItem->setProperty("text", section);
1292 
1293  QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
1294  m_topSectionItem->setY(topSectionStickPos);
1295  int delegateIndex = modelIndex;
1296  // Look for the first index with this section text
1297  while (delegateIndex > 0) {
1298  const QString prevSection = m_delegateModel->stringValue(delegateIndex - 1, m_sectionProperty);
1299  if (prevSection != section)
1300  break;
1301  delegateIndex--;
1302  }
1303  m_topSectionItem->setProperty("delegateIndex", delegateIndex);
1304  if (item->sectionItem()) {
1305  QQuickItemPrivate::get(item->sectionItem())->setCulled(true);
1306  }
1307  }
1308  }
1309  }
1310  const qreal clipFrom = visibleFrom + (!item->sectionItem() && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
1311  if (!cull && pos < clipFrom) {
1312  item->m_item->setProperty("heightToClip", clipFrom - pos);
1313  } else {
1314  item->m_item->setProperty("heightToClip", QVariant::fromValue<int>(0));
1315  }
1316 // qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
1317  pos += item->height();
1318  ++modelIndex;
1319  }
1320 
1321  // Second step of section sticky item positioning
1322  // Look at the next section header, check if it's pushing up the sticky one
1323  if (m_topSectionItem) {
1324  if (firstReallyVisibleItem >= 0) {
1325  for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
1326  ListItem *item = m_visibleItems[i];
1327  if (item->sectionItem()) {
1328  if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
1329  m_topSectionItem->setY(item->y() - m_topSectionItem->height());
1330  }
1331  break;
1332  }
1333  }
1334  }
1335  }
1336  }
1337  m_inLayout = false;
1338 }
1339 
1340 void ListViewWithPageHeader::updatePolish()
1341 {
1342  // Check we are not being taken down and don't paint anything
1343  // TODO Check if we still need this in 5.2
1344  // For reproduction just inifnite loop testDash or testDashContent
1345  if (!QQmlEngine::contextForObject(this)->parentContext())
1346  return;
1347 
1348  Q_FOREACH(ListItem *item, m_itemsToRelease)
1349  reallyReleaseItem(item);
1350  m_itemsToRelease.clear();
1351 
1352  if (!model())
1353  return;
1354 
1355  layout();
1356 
1357  refill();
1358 
1359  if (m_contentHeightDirty) {
1360  qreal contentHeight;
1361  if (m_visibleItems.isEmpty()) {
1362  contentHeight = m_headerItem ? m_headerItem->height() : 0;
1363  } else {
1364  const int modelCount = model()->rowCount();
1365  const int visibleItems = m_visibleItems.count();
1366  const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
1367  qreal nonCreatedHeight = 0;
1368  if (lastValidIndex != modelCount - 1) {
1369  const int visibleItems = m_visibleItems.count();
1370  qreal visibleItemsHeight = 0;
1371  Q_FOREACH(ListItem *item, m_visibleItems) {
1372  visibleItemsHeight += item->height();
1373  }
1374  const int unknownSizes = modelCount - (m_firstVisibleIndex + visibleItems);
1375  nonCreatedHeight = unknownSizes * visibleItemsHeight / visibleItems;
1376  }
1377  ListItem *item = m_visibleItems.last();
1378  contentHeight = nonCreatedHeight + item->y() + item->height() + m_clipItem->y();
1379  if (m_firstVisibleIndex != 0) {
1380  // Make sure that if we are shrinking we tell the view we still fit
1381  m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
1382  }
1383  }
1384 
1385  m_contentHeightDirty = false;
1386  adjustMinYExtent();
1387  if (contentHeight + m_minYExtent < height()) {
1388  // need this since in the previous call to adjustMinYExtent contentHeight is not set yet
1389  m_minYExtent = 0;
1390  }
1391  m_inContentHeightKeepHeaderShown = m_headerItem && m_headerItem->y() == contentY();
1392  setContentHeight(contentHeight);
1393  m_inContentHeightKeepHeaderShown = false;
1394  }
1395 }
1396 
1397 #include "moc_listviewwithpageheader.cpp"