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