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