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