on ff326f5 help sign in
unity8/plugins/ListViewWithPageHeader/listviewwithpageheader.cpp
975 lines 38 kB history raw
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 */
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 * There are a few things that are not really implemented or tested properly
74 * which we don't use at the moment like changing the model, changing
75 * the section delegate, having a section delegate that changes its size, etc.
76 * The known missing features are marked with TODOs along the code.
77 */
79#include "listviewwithpageheader.h"
81#include <QDebug>
82#include <qqmlinfo.h>
83#include <qqmlengine.h>
84#include <private/qquickvisualdatamodel_p.h>
85#include <private/qqmlglobal_p.h>
86#include <private/qquickitem_p.h>
87#include <private/qquickanimation_p.h>
88// #include <private/qquickrectangle_p.h>
90qreal ListViewWithPageHeader::ListItem::height() const
92 return m_item->height() + (m_sectionItem ? m_sectionItem->height() : 0);
95qreal ListViewWithPageHeader::ListItem::y() const
97 return m_item->y() - (m_sectionItem ? m_sectionItem->height() : 0);
100void ListViewWithPageHeader::ListItem::setY(qreal newY)
102 if (m_sectionItem) {
103 m_sectionItem->setY(newY);
104 m_item->setY(newY + m_sectionItem->height());
105 } else {
106 m_item->setY(newY);
107 }
110bool ListViewWithPageHeader::ListItem::culled() const
112 return QQuickItemPrivate::get(m_item)->culled;
115void ListViewWithPageHeader::ListItem::setCulled(bool culled)
117 QQuickItemPrivate::get(m_item)->setCulled(culled);
118 if (m_sectionItem)
119 QQuickItemPrivate::get(m_sectionItem)->setCulled(culled);
122ListViewWithPageHeader::ListViewWithPageHeader()
123 : m_delegateModel(nullptr)
124 , m_asyncRequestedIndex(-1)
125 , m_delegateValidated(false)
126 , m_firstVisibleIndex(-1)
127 , m_minYExtent(0)
128 , m_contentHeightDirty(false)
129 , m_headerItem(nullptr)
130 , m_previousContentY(0)
131 , m_headerItemShownHeight(0)
132 , m_sectionDelegate(nullptr)
133 , m_topSectionItem(nullptr)
134 , m_forceNoClip(false)
136 m_clipItem = new QQuickItem(contentItem());
137// m_clipItem = new QQuickRectangle(contentItem());
138// ((QQuickRectangle*)m_clipItem)->setColor(Qt::gray);
140 m_headerShowAnimation = new QQuickNumberAnimation(this);
141 m_headerShowAnimation->setEasing(QEasingCurve::OutQuad);
142 m_headerShowAnimation->setProperty("contentY");
143 m_headerShowAnimation->setDuration(200);
144 m_headerShowAnimation->setTargetObject(this);
146 connect(this, SIGNAL(contentWidthChanged()), this, SLOT(onContentWidthChanged()));
147 connect(this, SIGNAL(contentHeightChanged()), this, SLOT(onContentHeightChanged()));
148 connect(this, SIGNAL(heightChanged()), this, SLOT(onHeightChanged()));
151ListViewWithPageHeader::~ListViewWithPageHeader()
155QAbstractItemModel *ListViewWithPageHeader::model() const
157 return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
160void ListViewWithPageHeader::setModel(QAbstractItemModel *model)
162 if (model != this->model()) {
163 if (!m_delegateModel) {
164 createDelegateModel();
165 } else {
166 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
167 }
168 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
169 connect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
170 Q_EMIT modelChanged();
171 // TODO?
172// Q_EMIT contentHeightChanged();
173// Q_EMIT contentYChanged();
174 }
177QQmlComponent *ListViewWithPageHeader::delegate() const
179 return m_delegateModel ? m_delegateModel->delegate() : nullptr;
182void ListViewWithPageHeader::setDelegate(QQmlComponent *delegate)
184 if (delegate != this->delegate()) {
185 if (!m_delegateModel) {
186 createDelegateModel();
187 }
189 // Cleanup the existing items
190 foreach(ListItem *item, m_visibleItems)
191 releaseItem(item);
192 m_visibleItems.clear();
193 m_firstVisibleIndex = -1;
194 setContentY(0);
195 m_clipItem->setY(0);
197 m_delegateModel->setDelegate(delegate);
199 Q_EMIT delegateChanged();
200 m_delegateValidated = false;
201 m_contentHeightDirty = true;
202 polish();
203 }
206QQuickItem *ListViewWithPageHeader::header() const
208 return m_headerItem;
211void ListViewWithPageHeader::setHeader(QQuickItem *headerItem)
213 if (m_headerItem != headerItem) {
214 qreal oldHeaderHeight = 0;
215 qreal oldHeaderY = 0;
216 if (m_headerItem) {
217 oldHeaderHeight = m_headerItem->height();
218 oldHeaderY = m_headerItem->y();
219 QQuickItemPrivate::get(m_headerItem)->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
220 m_headerItem->setParentItem(nullptr);
221 }
222 m_headerItem = headerItem;
223 if (m_headerItem) {
224 m_headerItem->setParentItem(contentItem());
225 m_headerItem->setZ(1);
226 QQuickItemPrivate::get(m_headerItem)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
227 }
228 qreal newHeaderHeight = m_headerItem ? m_headerItem->height() : 0;
229 if (!m_visibleItems.isEmpty() && newHeaderHeight != oldHeaderHeight) {
230 headerHeightChanged(newHeaderHeight, oldHeaderHeight, oldHeaderY);
231 polish();
232 m_contentHeightDirty = true;
233 }
234 Q_EMIT headerChanged();
235 }
238QQmlComponent *ListViewWithPageHeader::sectionDelegate() const
240 return m_sectionDelegate;
243void ListViewWithPageHeader::setSectionDelegate(QQmlComponent *delegate)
245 if (delegate != m_sectionDelegate) {
246 // TODO clean existing sections
248 m_sectionDelegate = delegate;
250 m_topSectionItem = getSectionItem(QString());
251 m_topSectionItem->setZ(3);
252 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
254 // TODO create sections for existing items
256 Q_EMIT sectionDelegateChanged();
257 }
260QString ListViewWithPageHeader::sectionProperty() const
262 return m_sectionProperty;
265void ListViewWithPageHeader::setSectionProperty(const QString &property)
267 if (property != m_sectionProperty) {
268 m_sectionProperty = property;
270 updateWatchedRoles();
272 // TODO recreate sections
274 Q_EMIT sectionPropertyChanged();
275 }
278bool ListViewWithPageHeader::forceNoClip() const
280 return m_forceNoClip;
283void ListViewWithPageHeader::setForceNoClip(bool noClip)
285 if (noClip != m_forceNoClip) {
286 m_forceNoClip = noClip;
287 updateClipItem();
288 Q_EMIT forceNoClipChanged();
289 }
292void ListViewWithPageHeader::positionAtBeginning()
294 qreal headerHeight = (m_headerItem ? m_headerItem->height() : 0);
295 if (m_firstVisibleIndex != 0) {
296 // This could be optimized by trying to reuse the interesection
297 // of items that may end up intersecting between the existing
298 // m_visibleItems and the items we are creating in the next loop
299 foreach(ListItem *item, m_visibleItems)
300 releaseItem(item);
301 m_visibleItems.clear();
302 m_firstVisibleIndex = -1;
304 // Create the item 0, it will be already correctly positioned at createItem()
305 m_clipItem->setY(0);
306 ListItem *item = createItem(0, false);
307 // Create the subsequent items
308 int modelIndex = 1;
309 qreal pos = item->y() + item->height();
310 const qreal buffer = height() / 2;
311 const qreal bufferTo = height() + buffer;
312 while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
313 if (!(item = createItem(modelIndex, false)))
314 break;
315 pos += item->height();
316 ++modelIndex;
317 }
319 m_previousContentY = m_visibleItems.first()->y() - headerHeight;
320 }
321 setContentY(m_visibleItems.first()->y() + m_clipItem->y() - headerHeight);
324void ListViewWithPageHeader::showHeader()
326 auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
327 if (to != contentY()) {
328 m_headerShowAnimation->setTo(to);
329 m_headerShowAnimation->start();
330 }
333qreal ListViewWithPageHeader::minYExtent() const
335// qDebug() << "ListViewWithPageHeader::minYExtent" << m_minYExtent;
336 return m_minYExtent;
339void ListViewWithPageHeader::componentComplete()
341 if (m_delegateModel)
342 m_delegateModel->componentComplete();
344 QQuickFlickable::componentComplete();
346 polish();
349void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
351 QQuickFlickable::viewportMoved(orient);
352// qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
353 qreal diff = m_previousContentY - contentY();
354 if (m_headerItem) {
355 auto oldHeaderItemShownHeight = m_headerItemShownHeight;
356 if (contentY() < -m_minYExtent) {
357 // Stick the header item to the top when dragging down
358 m_headerItem->setY(contentY());
359 } else {
360 // We are going down (but it's not because of the rebound at the end)
361 // (but the header was not shown by it's own position)
362 // or the header is partially shown
363 const bool scrolledUp = m_previousContentY > contentY();
364 const bool notRebounding = contentY() + height() < contentHeight();
365 const bool notShownByItsOwn = contentY() + diff > m_headerItem->y() + m_headerItem->height();
367 if (!scrolledUp && contentY() == -m_minYExtent) {
368 m_headerItemShownHeight = 0;
369 m_headerItem->setY(contentY());
370 } else if ((scrolledUp && notRebounding && notShownByItsOwn) || (m_headerItemShownHeight > 0)) {
371 m_headerItemShownHeight += diff;
372 if (contentY() == -m_minYExtent) {
373 m_headerItemShownHeight = 0;
374 } else {
375 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
376 }
377 if (m_headerItemShownHeight > 0) {
378 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
379 } else {
380 m_headerItem->setY(-m_minYExtent);
381 }
382 }
383 }
384 // We will be changing the clip item, need to accomadate for it
385 // otherwise we move the firstItem down/up twice (unless the
386 // show header animation is running, where we want to keep the viewport stable)
387 if (!m_headerShowAnimation->isRunning())
388 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
389 else
390 diff = -diff;
391 }
392 if (!m_visibleItems.isEmpty()) {
393 updateClipItem();
394 ListItem *firstItem = m_visibleItems.first();
395 firstItem->setY(firstItem->y() + diff);
396 if (m_headerShowAnimation->isRunning()) {
397 adjustMinYExtent();
398 qDebug() << "ListViewWithPageHeader::viewportMoved" << m_minYExtent;
399 }
400 }
402 m_previousContentY = contentY();
403 layout();
404 polish();
407void ListViewWithPageHeader::createDelegateModel()
409 m_delegateModel = new QQuickVisualDataModel(qmlContext(this), this);
410 connect(m_delegateModel, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(itemCreated(int,QQuickItem*)));
411 if (isComponentComplete())
412 m_delegateModel->componentComplete();
413 updateWatchedRoles();
416void ListViewWithPageHeader::refill()
418 if (!isComponentComplete()) {
419 return;
420 }
422 const qreal buffer = height() / 2;
423 const qreal from = contentY();
424 const qreal to = from + height();
425 const qreal bufferFrom = from - buffer;
426 const qreal bufferTo = to + buffer;
428 bool added = addVisibleItems(from, to, false);
429 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
430 added |= addVisibleItems(bufferFrom, bufferTo, true);
432 if (added || removed) {
433 m_contentHeightDirty = true;
434 }
437bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
439 if (!delegate())
440 return false;
442 if (m_delegateModel->count() == 0)
443 return false;
445 ListItem *item;
446// qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
448 int modelIndex = 0;
449 qreal pos = 0;
450 if (!m_visibleItems.isEmpty()) {
451 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
452 item = m_visibleItems.last();
453 pos = item->y() + item->height() + m_clipItem->y();
454 }
455 bool changed = false;
456// qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
457 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
458// qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
459 if (!(item = createItem(modelIndex, asynchronous)))
460 break;
461 pos += item->height();
462 ++modelIndex;
463 changed = true;
464 }
466 modelIndex = 0;
467 pos = 0;
468 if (!m_visibleItems.isEmpty()) {
469 modelIndex = m_firstVisibleIndex - 1;
470 item = m_visibleItems.first();
471 pos = item->y() + m_clipItem->y();
472 }
473 while (modelIndex >= 0 && pos > fillFrom) {
474// qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
475 if (!(item = createItem(modelIndex, asynchronous)))
476 break;
477 pos -= item->height();
478 --modelIndex;
479 changed = true;
480 }
482 return changed;
485void ListViewWithPageHeader::releaseItem(ListItem *listItem)
487 QQuickItem *item = listItem->m_item;
488 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
489 itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
490 QQuickVisualModel::ReleaseFlags flags = m_delegateModel->release(item);
491 if (flags & QQuickVisualModel::Destroyed) {
492 item->setParentItem(nullptr);
493 }
494 delete listItem->m_sectionItem;
495 delete listItem;
498void ListViewWithPageHeader::updateWatchedRoles()
500 if (m_delegateModel) {
501 QList<QByteArray> roles;
502 if (!m_sectionProperty.isEmpty())
503 roles << m_sectionProperty.toUtf8();
504 m_delegateModel->setWatchedRoles(roles);
505 }
508QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
510 if (!m_sectionDelegate)
511 return nullptr;
513 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
514 if (modelIndex > 0) {
515 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
516 if (section == prevSection)
517 return nullptr;
518 }
519 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
520 // Already inserted items can't steal next section header
521 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
522 if (section == nextSection) {
523 // Steal the section header
524 ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
525 if (nextItem) {
526 QQuickItem *sectionItem = nextItem->m_sectionItem;
527 nextItem->m_sectionItem = nullptr;
528 return sectionItem;
529 }
530 }
531 }
533 return getSectionItem(section);
536QQuickItem *ListViewWithPageHeader::getSectionItem(const QString &sectionText)
538 QQuickItem *sectionItem = nullptr;
540 QQmlContext *creationContext = m_sectionDelegate->creationContext();
541 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
542 context->setContextProperty(QLatin1String("section"), sectionText);
543 QObject *nobj = m_sectionDelegate->beginCreate(context);
544 if (nobj) {
545 QQml_setParent_noEvent(context, nobj);
546 sectionItem = qobject_cast<QQuickItem *>(nobj);
547 if (!sectionItem) {
548 delete nobj;
549 } else {
550 sectionItem->setZ(2);
551 QQml_setParent_noEvent(sectionItem, m_clipItem);
552 sectionItem->setParentItem(m_clipItem);
553 }
554 } else {
555 delete context;
556 }
557 m_sectionDelegate->completeCreate();
559 // TODO attach to sectionItem so we can accomodate to it changing its height
561 return sectionItem;
564bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
566 qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
567 bool changed = false;
569 bool foundVisible = false;
570 int i = 0;
571 int removedItems = 0;
572 while (i < m_visibleItems.count()) {
573 ListItem *item = m_visibleItems[i];
574 const qreal pos = item->y() + m_clipItem->y();
575 qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
576 if (pos + item->height() < bufferFrom || pos > bufferTo) {
577 qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
578 releaseItem(item);
579 m_visibleItems.removeAt(i);
580 changed = true;
581 ++removedItems;
582 } else {
583 if (!foundVisible) {
584 foundVisible = true;
585 const int itemIndex = m_firstVisibleIndex + removedItems + i;
586 m_firstVisibleIndex = itemIndex;
587 }
588 ++i;
589 }
590 }
592 return changed;
595ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
597// qDebug() << "CREATE ITEM" << modelIndex;
598 if (asynchronous && m_asyncRequestedIndex != -1)
599 return nullptr;
601 m_asyncRequestedIndex = -1;
602 QQuickItem *item = m_delegateModel->item(modelIndex, asynchronous);
603 if (!item) {
604 m_asyncRequestedIndex = modelIndex;
605 return 0;
606 } else {
607// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
608 ListItem *listItem = new ListItem;
609 listItem->m_item = item;
610 listItem->m_sectionItem = getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/);
611 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
612 ListItem *prevItem = itemAtIndex(modelIndex - 1);
613 bool lostItem = false; // Is an item that we requested async but because of model changes
614 // it is no longer attached to any of the existing items (has no prev nor next item)
615 // nor is the first item
616 if (prevItem) {
617 listItem->setY(prevItem->y() + prevItem->height());
618 } else {
619 ListItem *nextItem = itemAtIndex(modelIndex + 1);
620 if (nextItem) {
621 listItem->setY(nextItem->y() - listItem ->height());
622 } else if (modelIndex == 0 && m_headerItem) {
623 listItem->setY(m_headerItem->height());
624 } else if (!m_visibleItems.isEmpty()) {
625 lostItem = true;
626 }
627 }
628 if (lostItem) {
629 releaseItem(listItem);
630 listItem = nullptr;
631 } else {
632 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() < contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
633 if (m_visibleItems.isEmpty()) {
634 m_visibleItems << listItem;
635 } else {
636 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
637 }
638 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
639 m_firstVisibleIndex = modelIndex;
640 adjustMinYExtent();
641 qDebug() << "ListViewWithPageHeader::createItem" << m_minYExtent;
642 }
643 }
644 return listItem;
645 }
648void ListViewWithPageHeader::itemCreated(int modelIndex, QQuickItem *item)
650// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
652 item->setParentItem(m_clipItem);
653 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
654 context->setContextProperty(QLatin1String("ListViewWithPageHeader"), this);
655 if (modelIndex == m_asyncRequestedIndex) {
656 createItem(modelIndex, false);
657 }
660void ListViewWithPageHeader::updateClipItem()
662 m_clipItem->setHeight(height() - m_headerItemShownHeight);
663 m_clipItem->setY(contentY() + m_headerItemShownHeight);
664 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
667void ListViewWithPageHeader::onContentHeightChanged()
669 updateClipItem();
672void ListViewWithPageHeader::onContentWidthChanged()
674 m_clipItem->setWidth(contentItem()->width());
677void ListViewWithPageHeader::onHeightChanged()
679 setMaximumFlickVelocity(height() * 10);
680 setFlickDeceleration(height() * 2);
683void ListViewWithPageHeader::onModelUpdated(const QQuickChangeSet &changeSet, bool /*reset*/)
685 // TODO Do something with reset
686// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
687 foreach(const QQuickChangeSet::Remove &remove, changeSet.removes()) {
688// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove;
689 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
690 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
691 // If all the items we are removing are either not created or culled
692 // we have to grow down to avoid viewport changing
693 bool growDown = true;
694 for (int i = 0; growDown && i < remove.count; ++i) {
695 const int modelIndex = remove.index + i;
696 ListItem *item = itemAtIndex(modelIndex);
697 if (item && !item->culled()) {
698 growDown = false;
699 }
700 }
701 for (int i = remove.count - 1; i >= 0; --i) {
702 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
703 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
704 ListItem *item = m_visibleItems[visibleIndex];
705 // Pass the section item down if needed
706 if (item->m_sectionItem && visibleIndex + 1 < m_visibleItems.count()) {
707 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
708 if (!nextItem->m_sectionItem) {
709 nextItem->m_sectionItem = item->m_sectionItem;
710 item->m_sectionItem = nullptr;
711 }
712 }
713 releaseItem(item);
714 m_visibleItems.removeAt(visibleIndex);
715 }
716 }
717 if (growDown) {
718 adjustMinYExtent();
719 qDebug() << "ListViewWithPageHeader::onModelUpdated grow down" << m_minYExtent;
720 } else if (remove.index <= m_firstVisibleIndex && !m_visibleItems.isEmpty()) {
721 // We removed the first item that is the one that positions the rest
722 // position the new first item correctly
723 m_visibleItems.first()->setY(oldFirstValidIndexPos);
724 }
725 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
726 m_firstVisibleIndex -= remove.count;
727 }
728 for (int i = remove.count - 1; i >= 0; --i) {
729 const int modelIndex = remove.index + i;
730 if (modelIndex == m_asyncRequestedIndex) {
731 m_asyncRequestedIndex = -1;
732 } else if (modelIndex < m_asyncRequestedIndex) {
733 m_asyncRequestedIndex--;
734 }
735 }
736 }
738 foreach(const QQuickChangeSet::Insert &insert, changeSet.inserts()) {
739 qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
740 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
741 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
742 if (insertingInValidIndexes || firstItemWithViewOnTop)
743 {
744 // If the items we are adding won't be really visible
745 // we grow up instead of down to not change the viewport
746 bool growUp = false;
747 if (!firstItemWithViewOnTop) {
748 for (int i = 0; i < m_visibleItems.count(); ++i) {
749 if (!m_visibleItems[i]->culled()) {
750 if (insert.index <= m_firstVisibleIndex + i) {
751 growUp = true;
752 }
753 break;
754 }
755 }
756 }
758 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
759 for (int i = insert.count - 1; i >= 0; --i) {
760 const int modelIndex = insert.index + i;
761 ListItem *item = createItem(modelIndex, false);
762 if (growUp) {
763 ListItem *firstItem = m_visibleItems.first();
764 firstItem->setY(firstItem->y() - item->height());
765 adjustMinYExtent();
766 qDebug() << "ListViewWithPageHeader::onModelUpdated growed up" << m_minYExtent;
767 }
768 // Adding an item may break a "same section" chain, so check
769 // if we need adding a new section item
770 if (m_sectionDelegate) {
771 ListItem *nextItem = itemAtIndex(modelIndex + 1);
772 if (nextItem && !nextItem->m_sectionItem) {
773 nextItem->m_sectionItem = getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/);
774 if (growUp) {
775 ListItem *firstItem = m_visibleItems.first();
776 firstItem->setY(firstItem->y() - nextItem->m_sectionItem->height());
777 adjustMinYExtent();
778 }
779 }
780 }
781 }
782 if (firstItemWithViewOnTop) {
783 ListItem *firstItem = m_visibleItems.first();
784 firstItem->setY(oldFirstValidIndexPos);
785 }
786 } else if (insert.index <= m_firstVisibleIndex) {
787 m_firstVisibleIndex += insert.count;
788 }
790 for (int i = insert.count - 1; i >= 0; --i) {
791 const int modelIndex = insert.index + i;
792 if (modelIndex <= m_asyncRequestedIndex) {
793 m_asyncRequestedIndex++;
794 }
795 }
796 }
798 layout();
799 polish();
800 m_contentHeightDirty = true;
803void ListViewWithPageHeader::itemGeometryChanged(QQuickItem *item, const QRectF &newGeometry, const QRectF &oldGeometry)
805 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
806 if (heightDiff != 0) {
807 if (item == m_headerItem) {
808 headerHeightChanged(newGeometry.height(), oldGeometry.height(), oldGeometry.y());
809 } else {
810 if (oldGeometry.y() + oldGeometry.height() + m_clipItem->y() < contentY() && !m_visibleItems.isEmpty()) {
811 ListItem *firstItem = m_visibleItems.first();
812 firstItem->setY(firstItem->y() - heightDiff);
813 adjustMinYExtent();
814 qDebug() << "ListViewWithPageHeader::itemGeometryChanged before viewport" << m_minYExtent;
815 }
816 }
817 polish();
818 m_contentHeightDirty = true;
819 }
822void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
824 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
825 if (m_headerItemShownHeight > 0) {
826 // If the header is shown because of the clip
827 // Change its size
828 m_headerItemShownHeight += heightDiff;
829 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
830 updateClipItem();
831 adjustMinYExtent();
832 layout();
833 qDebug() << "ListViewWithPageHeader::headerHeightChanged A" << m_minYExtent;
834 } else {
835 if (oldHeaderY + oldHeaderHeight > contentY()) {
836 // If the header is shown because its position
837 // Change its size
838 ListItem *firstItem = m_visibleItems.first();
839 firstItem->setY(firstItem->y() + heightDiff);
840 } else {
841 // If the header is not on screen, just change the start of the list
842 // so the viewport is not changed
843 adjustMinYExtent();
844 qDebug() << "ListViewWithPageHeader::headerHeightChanged B" << m_minYExtent;
845 }
846 }
850void ListViewWithPageHeader::adjustMinYExtent()
852 if (m_visibleItems.isEmpty()) {
853 m_minYExtent = 0;
854 } else {
855 m_minYExtent = -m_visibleItems.first()->y() - m_clipItem->y() + (m_headerItem ? m_headerItem->height() : 0);
856 }
859ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
861 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
862 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
863 return m_visibleItems[visibleIndexedModelIndex];
865 return nullptr;
868void ListViewWithPageHeader::layout()
870 if (!m_visibleItems.isEmpty()) {
871 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
872 const qreal visibleTo = contentY() + height() - m_clipItem->y();
874 qreal pos = m_visibleItems.first()->y();
876 qDebug() << "ListViewWithPageHeader::updatePolish Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
877 int firstReallyVisibleItem = -1;
878 int modelIndex = m_firstVisibleIndex;
879 foreach(ListItem *item, m_visibleItems) {
880 const bool cull = pos + item->height() < visibleFrom || pos >= visibleTo;
881 item->setCulled(cull);
882 item->setY(pos);
883 if (!cull && firstReallyVisibleItem == -1) {
884 firstReallyVisibleItem = modelIndex;
885 if (m_topSectionItem) {
886 // Update the top sticky section header
887 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
888 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
889 context->setContextProperty(QLatin1String("section"), section);
891 // Positing the top section sticky item is a two step process
892 // First we set it either we cull it (because it doesn't need to be sticked to the top)
893 // or stick it to the top
894 // Then after the loop we'll make sure that if there's another section just below it
895 // pushed the sticky section up to make it disappear
896 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
897 if (topSectionStickPos <= pos) {
898 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
899 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(false);
900 } else {
901 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
902 m_topSectionItem->setY(topSectionStickPos);
903 if (item->m_sectionItem) {
904 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(true);
905 }
906 }
907 }
908 }
909 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
910 if (!cull && pos < visibleFrom) {
911 context->setContextProperty(QLatin1String("heightToClip"), visibleFrom - pos);
912 } else {
913 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
914 }
915 qDebug() << "ListViewWithPageHeader::updatePolish" << item->m_item;
916 pos += item->height();
917 ++modelIndex;
918 }
920 // Second step of section sticky item positioning
921 // Look at the next section header, check if it's pushing up the sticky one
922 if (m_topSectionItem) {
923 if (firstReallyVisibleItem >= 0) {
924 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
925 ListItem *item = m_visibleItems[i];
926 if (item->m_sectionItem) {
927 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
928 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
929 }
930 break;
931 }
932 }
933 }
934 }
935 }
938void ListViewWithPageHeader::updatePolish()
940 if (!model())
941 return;
943 layout();
945 refill();
947 if (m_contentHeightDirty) {
948 qreal contentHeight;
949 const int modelCount = model()->rowCount();
950 const int visibleItems = m_visibleItems.count();
951 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
952 if (lastValidIndex == modelCount - 1) {
953 ListItem *item = m_visibleItems.last();
954 contentHeight = item->y() + item->height() + m_clipItem->y();
955 if (m_firstVisibleIndex != 0) {
956 // Make sure that if we are shrinking we tell the view we still fit
957 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
958 }
959 } else {
960 contentHeight = m_headerItem ? m_headerItem->height() : 0;
961 if (!m_visibleItems.isEmpty()) {
962 foreach(ListItem *item, m_visibleItems) {
963 contentHeight += item->height();
964 }
965 const int unknownSizes = modelCount - visibleItems;
966 contentHeight += unknownSizes * contentHeight / visibleItems;
967 }
968 }
970 m_contentHeightDirty = false;
971 setContentHeight(contentHeight);
972 }
975#include "moc_listviewwithpageheader.cpp"