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