on ab1237b help sign in
unity8/plugins/ListViewWithPageHeader/listviewwithpageheader.cpp
1002 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 * Note that minYExtent and height are not always totally accurate, since
74 * we don't have the items created we can't guess their heights
75 * so we can only guarantee the values are correct when the first/last
76 * items of the list are visible, otherwise we just live with good enough
77 * values that make the list scrollable
78 *
79 * There are a few things that are not really implemented or tested properly
80 * which we don't use at the moment like changing the model, changing
81 * the section delegate, having a section delegate that changes its size, etc.
82 * The known missing features are marked with TODOs along the code.
83 */
85#include "listviewwithpageheader.h"
87#include <QDebug>
88#include <qqmlinfo.h>
89#include <qqmlengine.h>
90#include <private/qquickvisualdatamodel_p.h>
91#include <private/qqmlglobal_p.h>
92#include <private/qquickitem_p.h>
93#include <private/qquickanimation_p.h>
94// #include <private/qquickrectangle_p.h>
96qreal ListViewWithPageHeader::ListItem::height() const
98 return m_item->height() + (m_sectionItem ? m_sectionItem->height() : 0);
101qreal ListViewWithPageHeader::ListItem::y() const
103 return m_item->y() - (m_sectionItem ? m_sectionItem->height() : 0);
106void ListViewWithPageHeader::ListItem::setY(qreal newY)
108 if (m_sectionItem) {
109 m_sectionItem->setY(newY);
110 m_item->setY(newY + m_sectionItem->height());
111 } else {
112 m_item->setY(newY);
113 }
116bool ListViewWithPageHeader::ListItem::culled() const
118 return QQuickItemPrivate::get(m_item)->culled;
121void ListViewWithPageHeader::ListItem::setCulled(bool culled)
123 QQuickItemPrivate::get(m_item)->setCulled(culled);
124 if (m_sectionItem)
125 QQuickItemPrivate::get(m_sectionItem)->setCulled(culled);
128ListViewWithPageHeader::ListViewWithPageHeader()
129 : m_delegateModel(nullptr)
130 , m_asyncRequestedIndex(-1)
131 , m_delegateValidated(false)
132 , m_firstVisibleIndex(-1)
133 , m_minYExtent(0)
134 , m_contentHeightDirty(false)
135 , m_headerItem(nullptr)
136 , m_previousContentY(0)
137 , m_headerItemShownHeight(0)
138 , m_sectionDelegate(nullptr)
139 , m_topSectionItem(nullptr)
140 , m_forceNoClip(false)
142 m_clipItem = new QQuickItem(contentItem());
143// m_clipItem = new QQuickRectangle(contentItem());
144// ((QQuickRectangle*)m_clipItem)->setColor(Qt::gray);
146 m_headerShowAnimation = new QQuickNumberAnimation(this);
147 m_headerShowAnimation->setEasing(QEasingCurve::OutQuad);
148 m_headerShowAnimation->setProperty("contentY");
149 m_headerShowAnimation->setDuration(200);
150 m_headerShowAnimation->setTargetObject(this);
152 connect(this, SIGNAL(contentWidthChanged()), this, SLOT(onContentWidthChanged()));
153 connect(this, SIGNAL(contentHeightChanged()), this, SLOT(onContentHeightChanged()));
154 connect(this, SIGNAL(heightChanged()), this, SLOT(onHeightChanged()));
157ListViewWithPageHeader::~ListViewWithPageHeader()
161QAbstractItemModel *ListViewWithPageHeader::model() const
163 return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
166void ListViewWithPageHeader::setModel(QAbstractItemModel *model)
168 if (model != this->model()) {
169 if (!m_delegateModel) {
170 createDelegateModel();
171 } else {
172 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
173 }
174 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
175 connect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
176 Q_EMIT modelChanged();
177 // TODO?
178// Q_EMIT contentHeightChanged();
179// Q_EMIT contentYChanged();
180 }
183QQmlComponent *ListViewWithPageHeader::delegate() const
185 return m_delegateModel ? m_delegateModel->delegate() : nullptr;
188void ListViewWithPageHeader::setDelegate(QQmlComponent *delegate)
190 if (delegate != this->delegate()) {
191 if (!m_delegateModel) {
192 createDelegateModel();
193 }
195 // Cleanup the existing items
196 foreach(ListItem *item, m_visibleItems)
197 releaseItem(item);
198 m_visibleItems.clear();
199 m_firstVisibleIndex = -1;
200 adjustMinYExtent();
201 setContentY(0);
202 m_clipItem->setY(0);
203 if (m_topSectionItem) {
204 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
205 }
207 m_delegateModel->setDelegate(delegate);
209 Q_EMIT delegateChanged();
210 m_delegateValidated = false;
211 m_contentHeightDirty = true;
212 polish();
213 }
216QQuickItem *ListViewWithPageHeader::header() const
218 return m_headerItem;
221void ListViewWithPageHeader::setHeader(QQuickItem *headerItem)
223 if (m_headerItem != headerItem) {
224 qreal oldHeaderHeight = 0;
225 qreal oldHeaderY = 0;
226 if (m_headerItem) {
227 oldHeaderHeight = m_headerItem->height();
228 oldHeaderY = m_headerItem->y();
229 QQuickItemPrivate::get(m_headerItem)->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
230 m_headerItem->setParentItem(nullptr);
231 }
232 m_headerItem = headerItem;
233 if (m_headerItem) {
234 m_headerItem->setParentItem(contentItem());
235 m_headerItem->setZ(1);
236 QQuickItemPrivate::get(m_headerItem)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
237 }
238 qreal newHeaderHeight = m_headerItem ? m_headerItem->height() : 0;
239 if (!m_visibleItems.isEmpty() && newHeaderHeight != oldHeaderHeight) {
240 headerHeightChanged(newHeaderHeight, oldHeaderHeight, oldHeaderY);
241 polish();
242 m_contentHeightDirty = true;
243 }
244 Q_EMIT headerChanged();
245 }
248QQmlComponent *ListViewWithPageHeader::sectionDelegate() const
250 return m_sectionDelegate;
253void ListViewWithPageHeader::setSectionDelegate(QQmlComponent *delegate)
255 if (delegate != m_sectionDelegate) {
256 // TODO clean existing sections
258 m_sectionDelegate = delegate;
260 m_topSectionItem = getSectionItem(QString());
261 m_topSectionItem->setZ(3);
262 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
264 // TODO create sections for existing items
266 Q_EMIT sectionDelegateChanged();
267 }
270QString ListViewWithPageHeader::sectionProperty() const
272 return m_sectionProperty;
275void ListViewWithPageHeader::setSectionProperty(const QString &property)
277 if (property != m_sectionProperty) {
278 m_sectionProperty = property;
280 updateWatchedRoles();
282 // TODO recreate sections
284 Q_EMIT sectionPropertyChanged();
285 }
288bool ListViewWithPageHeader::forceNoClip() const
290 return m_forceNoClip;
293void ListViewWithPageHeader::setForceNoClip(bool noClip)
295 if (noClip != m_forceNoClip) {
296 m_forceNoClip = noClip;
297 updateClipItem();
298 Q_EMIT forceNoClipChanged();
299 }
302void ListViewWithPageHeader::positionAtBeginning()
304 qreal headerHeight = (m_headerItem ? m_headerItem->height() : 0);
305 if (m_firstVisibleIndex != 0) {
306 // This could be optimized by trying to reuse the interesection
307 // of items that may end up intersecting between the existing
308 // m_visibleItems and the items we are creating in the next loop
309 foreach(ListItem *item, m_visibleItems)
310 releaseItem(item);
311 m_visibleItems.clear();
312 m_firstVisibleIndex = -1;
314 // Create the item 0, it will be already correctly positioned at createItem()
315 m_clipItem->setY(0);
316 ListItem *item = createItem(0, false);
317 // Create the subsequent items
318 int modelIndex = 1;
319 qreal pos = item->y() + item->height();
320 const qreal buffer = height() / 2;
321 const qreal bufferTo = height() + buffer;
322 while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
323 if (!(item = createItem(modelIndex, false)))
324 break;
325 pos += item->height();
326 ++modelIndex;
327 }
329 m_previousContentY = m_visibleItems.first()->y() - headerHeight;
330 }
331 setContentY(m_visibleItems.first()->y() + m_clipItem->y() - headerHeight);
334void ListViewWithPageHeader::showHeader()
336 auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
337 if (to != contentY()) {
338 m_headerShowAnimation->setTo(to);
339 m_headerShowAnimation->start();
340 }
343qreal ListViewWithPageHeader::minYExtent() const
345// qDebug() << "ListViewWithPageHeader::minYExtent" << m_minYExtent;
346 return m_minYExtent;
349void ListViewWithPageHeader::componentComplete()
351 if (m_delegateModel)
352 m_delegateModel->componentComplete();
354 QQuickFlickable::componentComplete();
356 polish();
359void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
361 QQuickFlickable::viewportMoved(orient);
362// qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
363 qreal diff = m_previousContentY - contentY();
364 if (m_headerItem) {
365 auto oldHeaderItemShownHeight = m_headerItemShownHeight;
366 if (contentY() < -m_minYExtent) {
367 // Stick the header item to the top when dragging down
368 m_headerItem->setY(contentY());
369 } else {
370 // We are going down (but it's not because of the rebound at the end)
371 // (but the header was not shown by it's own position)
372 // or the header is partially shown
373 const bool scrolledUp = m_previousContentY > contentY();
374 const bool notRebounding = contentY() + height() < contentHeight();
375 const bool notShownByItsOwn = contentY() + diff > m_headerItem->y() + m_headerItem->height();
377 if (!scrolledUp && contentY() == -m_minYExtent) {
378 m_headerItemShownHeight = 0;
379 m_headerItem->setY(contentY());
380 } else if ((scrolledUp && notRebounding && notShownByItsOwn) || (m_headerItemShownHeight > 0)) {
381 m_headerItemShownHeight += diff;
382 if (contentY() == -m_minYExtent) {
383 m_headerItemShownHeight = 0;
384 } else {
385 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
386 }
387 if (m_headerItemShownHeight > 0) {
388 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
389 } else {
390 m_headerItem->setY(-m_minYExtent);
391 }
392 }
393 }
394 // We will be changing the clip item, need to accomadate for it
395 // otherwise we move the firstItem down/up twice (unless the
396 // show header animation is running, where we want to keep the viewport stable)
397 if (!m_headerShowAnimation->isRunning())
398 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
399 else
400 diff = -diff;
401 }
402 if (!m_visibleItems.isEmpty()) {
403 updateClipItem();
404 ListItem *firstItem = m_visibleItems.first();
405 firstItem->setY(firstItem->y() + diff);
406 if (m_headerShowAnimation->isRunning()) {
407 adjustMinYExtent();
408 }
409 }
411 m_previousContentY = contentY();
412 layout();
413 polish();
416void ListViewWithPageHeader::createDelegateModel()
418 m_delegateModel = new QQuickVisualDataModel(qmlContext(this), this);
419 connect(m_delegateModel, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(itemCreated(int,QQuickItem*)));
420 if (isComponentComplete())
421 m_delegateModel->componentComplete();
422 updateWatchedRoles();
425void ListViewWithPageHeader::refill()
427 if (!isComponentComplete()) {
428 return;
429 }
431 const qreal buffer = height() / 2;
432 const qreal from = contentY();
433 const qreal to = from + height();
434 const qreal bufferFrom = from - buffer;
435 const qreal bufferTo = to + buffer;
437 bool added = addVisibleItems(from, to, false);
438 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
439 added |= addVisibleItems(bufferFrom, bufferTo, true);
441 if (added || removed) {
442 m_contentHeightDirty = true;
443 }
446bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
448 if (!delegate())
449 return false;
451 if (m_delegateModel->count() == 0)
452 return false;
454 ListItem *item;
455// qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
457 int modelIndex = 0;
458 qreal pos = 0;
459 if (!m_visibleItems.isEmpty()) {
460 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
461 item = m_visibleItems.last();
462 pos = item->y() + item->height() + m_clipItem->y();
463 }
464 bool changed = false;
465// qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
466 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
467// qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
468 if (!(item = createItem(modelIndex, asynchronous)))
469 break;
470 pos += item->height();
471 ++modelIndex;
472 changed = true;
473 }
475 modelIndex = 0;
476 pos = 0;
477 if (!m_visibleItems.isEmpty()) {
478 modelIndex = m_firstVisibleIndex - 1;
479 item = m_visibleItems.first();
480 pos = item->y() + m_clipItem->y();
481 }
482 while (modelIndex >= 0 && pos > fillFrom) {
483// qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
484 if (!(item = createItem(modelIndex, asynchronous)))
485 break;
486 pos -= item->height();
487 --modelIndex;
488 changed = true;
489 }
491 return changed;
494void ListViewWithPageHeader::releaseItem(ListItem *listItem)
496 QQuickItem *item = listItem->m_item;
497 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
498 itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
499 QQuickVisualModel::ReleaseFlags flags = m_delegateModel->release(item);
500 if (flags & QQuickVisualModel::Destroyed) {
501 item->setParentItem(nullptr);
502 }
503 delete listItem->m_sectionItem;
504 delete listItem;
507void ListViewWithPageHeader::updateWatchedRoles()
509 if (m_delegateModel) {
510 QList<QByteArray> roles;
511 if (!m_sectionProperty.isEmpty())
512 roles << m_sectionProperty.toUtf8();
513 m_delegateModel->setWatchedRoles(roles);
514 }
517QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
519 if (!m_sectionDelegate)
520 return nullptr;
522 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
523 if (modelIndex > 0) {
524 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
525 if (section == prevSection)
526 return nullptr;
527 }
528 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
529 // Already inserted items can't steal next section header
530 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
531 if (section == nextSection) {
532 // Steal the section header
533 ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
534 if (nextItem) {
535 QQuickItem *sectionItem = nextItem->m_sectionItem;
536 nextItem->m_sectionItem = nullptr;
537 return sectionItem;
538 }
539 }
540 }
542 return getSectionItem(section);
545QQuickItem *ListViewWithPageHeader::getSectionItem(const QString &sectionText)
547 QQuickItem *sectionItem = nullptr;
549 QQmlContext *creationContext = m_sectionDelegate->creationContext();
550 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
551 context->setContextProperty(QLatin1String("section"), sectionText);
552 QObject *nobj = m_sectionDelegate->beginCreate(context);
553 if (nobj) {
554 QQml_setParent_noEvent(context, nobj);
555 sectionItem = qobject_cast<QQuickItem *>(nobj);
556 if (!sectionItem) {
557 delete nobj;
558 } else {
559 sectionItem->setZ(2);
560 QQml_setParent_noEvent(sectionItem, m_clipItem);
561 sectionItem->setParentItem(m_clipItem);
562 }
563 } else {
564 delete context;
565 }
566 m_sectionDelegate->completeCreate();
568 // TODO attach to sectionItem so we can accomodate to it changing its height
570 return sectionItem;
573bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
575// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
576 bool changed = false;
578 bool foundVisible = false;
579 int i = 0;
580 int removedItems = 0;
581 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
582 while (i < m_visibleItems.count()) {
583 ListItem *item = m_visibleItems[i];
584 const qreal pos = item->y() + m_clipItem->y();
585// qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
586 if (pos + item->height() < bufferFrom || pos > bufferTo) {
587// qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
588 releaseItem(item);
589 m_visibleItems.removeAt(i);
590 changed = true;
591 ++removedItems;
592 } else {
593 if (!foundVisible) {
594 foundVisible = true;
595 const int itemIndex = m_firstVisibleIndex + removedItems + i;
596 m_firstVisibleIndex = itemIndex;
597 }
598 ++i;
599 }
600 }
601 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
602 adjustMinYExtent();
603 }
605 return changed;
608ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
610// qDebug() << "CREATE ITEM" << modelIndex;
611 if (asynchronous && m_asyncRequestedIndex != -1)
612 return nullptr;
614 m_asyncRequestedIndex = -1;
615 QQuickItem *item = m_delegateModel->item(modelIndex, asynchronous);
616 if (!item) {
617 m_asyncRequestedIndex = modelIndex;
618 return 0;
619 } else {
620// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
621 ListItem *listItem = new ListItem;
622 listItem->m_item = item;
623 listItem->m_sectionItem = getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/);
624 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
625 ListItem *prevItem = itemAtIndex(modelIndex - 1);
626 bool lostItem = false; // Is an item that we requested async but because of model changes
627 // it is no longer attached to any of the existing items (has no prev nor next item)
628 // nor is the first item
629 if (prevItem) {
630 listItem->setY(prevItem->y() + prevItem->height());
631 } else {
632 ListItem *nextItem = itemAtIndex(modelIndex + 1);
633 if (nextItem) {
634 listItem->setY(nextItem->y() - listItem ->height());
635 } else if (modelIndex == 0 && m_headerItem) {
636 listItem->setY(m_headerItem->height());
637 } else if (!m_visibleItems.isEmpty()) {
638 lostItem = true;
639 }
640 }
641 if (lostItem) {
642 releaseItem(listItem);
643 listItem = nullptr;
644 } else {
645 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() < contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
646 if (m_visibleItems.isEmpty()) {
647 m_visibleItems << listItem;
648 } else {
649 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
650 }
651 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
652 m_firstVisibleIndex = modelIndex;
653 }
654 adjustMinYExtent();
655 }
656 return listItem;
657 }
660void ListViewWithPageHeader::itemCreated(int modelIndex, QQuickItem *item)
662// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
664 item->setParentItem(m_clipItem);
665 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
666 context->setContextProperty(QLatin1String("ListViewWithPageHeader"), this);
667 if (modelIndex == m_asyncRequestedIndex) {
668 createItem(modelIndex, false);
669 }
672void ListViewWithPageHeader::updateClipItem()
674 m_clipItem->setHeight(height() - m_headerItemShownHeight);
675 m_clipItem->setY(contentY() + m_headerItemShownHeight);
676 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
679void ListViewWithPageHeader::onContentHeightChanged()
681 updateClipItem();
684void ListViewWithPageHeader::onContentWidthChanged()
686 m_clipItem->setWidth(contentItem()->width());
689void ListViewWithPageHeader::onHeightChanged()
691 setMaximumFlickVelocity(height() * 10);
692 setFlickDeceleration(height() * 2);
695void ListViewWithPageHeader::onModelUpdated(const QQuickChangeSet &changeSet, bool /*reset*/)
697 // TODO Do something with reset
698// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
699 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
701 foreach(const QQuickChangeSet::Remove &remove, changeSet.removes()) {
702// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove;
703 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
704 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
705 // If all the items we are removing are either not created or culled
706 // we have to grow down to avoid viewport changing
707 bool growDown = true;
708 for (int i = 0; growDown && i < remove.count; ++i) {
709 const int modelIndex = remove.index + i;
710 ListItem *item = itemAtIndex(modelIndex);
711 if (item && !item->culled()) {
712 growDown = false;
713 }
714 }
715 for (int i = remove.count - 1; i >= 0; --i) {
716 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
717 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
718 ListItem *item = m_visibleItems[visibleIndex];
719 // Pass the section item down if needed
720 if (item->m_sectionItem && visibleIndex + 1 < m_visibleItems.count()) {
721 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
722 if (!nextItem->m_sectionItem) {
723 nextItem->m_sectionItem = item->m_sectionItem;
724 item->m_sectionItem = nullptr;
725 }
726 }
727 releaseItem(item);
728 m_visibleItems.removeAt(visibleIndex);
729 }
730 }
731 if (growDown) {
732 adjustMinYExtent();
733 } else if (remove.index <= m_firstVisibleIndex && !m_visibleItems.isEmpty()) {
734 // We removed the first item that is the one that positions the rest
735 // position the new first item correctly
736 m_visibleItems.first()->setY(oldFirstValidIndexPos);
737 }
738 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
739 m_firstVisibleIndex -= remove.count;
740 }
741 for (int i = remove.count - 1; i >= 0; --i) {
742 const int modelIndex = remove.index + i;
743 if (modelIndex == m_asyncRequestedIndex) {
744 m_asyncRequestedIndex = -1;
745 } else if (modelIndex < m_asyncRequestedIndex) {
746 m_asyncRequestedIndex--;
747 }
748 }
749 }
751 foreach(const QQuickChangeSet::Insert &insert, changeSet.inserts()) {
752// qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
753 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
754 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
755 if (insertingInValidIndexes || firstItemWithViewOnTop)
756 {
757 // If the items we are adding won't be really visible
758 // we grow up instead of down to not change the viewport
759 bool growUp = false;
760 if (!firstItemWithViewOnTop) {
761 for (int i = 0; i < m_visibleItems.count(); ++i) {
762 if (!m_visibleItems[i]->culled()) {
763 if (insert.index <= m_firstVisibleIndex + i) {
764 growUp = true;
765 }
766 break;
767 }
768 }
769 }
771 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
772 for (int i = insert.count - 1; i >= 0; --i) {
773 const int modelIndex = insert.index + i;
774 ListItem *item = createItem(modelIndex, false);
775 if (growUp) {
776 ListItem *firstItem = m_visibleItems.first();
777 firstItem->setY(firstItem->y() - item->height());
778 adjustMinYExtent();
779 }
780 // Adding an item may break a "same section" chain, so check
781 // if we need adding a new section item
782 if (m_sectionDelegate) {
783 ListItem *nextItem = itemAtIndex(modelIndex + 1);
784 if (nextItem && !nextItem->m_sectionItem) {
785 nextItem->m_sectionItem = getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/);
786 if (growUp) {
787 ListItem *firstItem = m_visibleItems.first();
788 firstItem->setY(firstItem->y() - nextItem->m_sectionItem->height());
789 adjustMinYExtent();
790 }
791 }
792 }
793 }
794 if (firstItemWithViewOnTop) {
795 ListItem *firstItem = m_visibleItems.first();
796 firstItem->setY(oldFirstValidIndexPos);
797 }
798 } else if (insert.index <= m_firstVisibleIndex) {
799 m_firstVisibleIndex += insert.count;
800 }
802 for (int i = insert.count - 1; i >= 0; --i) {
803 const int modelIndex = insert.index + i;
804 if (modelIndex <= m_asyncRequestedIndex) {
805 m_asyncRequestedIndex++;
806 }
807 }
808 }
810 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
811 adjustMinYExtent();
812 }
814 layout();
815 polish();
816 m_contentHeightDirty = true;
819void ListViewWithPageHeader::itemGeometryChanged(QQuickItem *item, const QRectF &newGeometry, const QRectF &oldGeometry)
821 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
822 if (heightDiff != 0) {
823 if (item == m_headerItem) {
824 headerHeightChanged(newGeometry.height(), oldGeometry.height(), oldGeometry.y());
825 } else {
826 if (oldGeometry.y() + oldGeometry.height() + m_clipItem->y() < contentY() && !m_visibleItems.isEmpty()) {
827 ListItem *firstItem = m_visibleItems.first();
828 firstItem->setY(firstItem->y() - heightDiff);
829 adjustMinYExtent();
830 layout();
831 }
832 }
833 refill();
834 adjustMinYExtent();
835 polish();
836 m_contentHeightDirty = true;
837 }
840void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
842 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
843 if (m_headerItemShownHeight > 0) {
844 // If the header is shown because of the clip
845 // Change its size
846 m_headerItemShownHeight += heightDiff;
847 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
848 updateClipItem();
849 adjustMinYExtent();
850 } else {
851 if (oldHeaderY + oldHeaderHeight > contentY()) {
852 // If the header is shown because its position
853 // Change its size
854 ListItem *firstItem = m_visibleItems.first();
855 firstItem->setY(firstItem->y() + heightDiff);
856 layout();
857 } else {
858 // If the header is not on screen, just change the start of the list
859 // so the viewport is not changed
860 adjustMinYExtent();
861 }
862 }
866void ListViewWithPageHeader::adjustMinYExtent()
868 if (m_visibleItems.isEmpty()) {
869 m_minYExtent = 0;
870 } else {
871 qreal nonCreatedHeight = 0;
872 if (m_firstVisibleIndex != 0) {
873 // Calculate the average height of items to estimate the position of the list start
874 const int visibleItems = m_visibleItems.count();
875 qreal visibleItemsHeight = 0;
876 foreach(ListItem *item, m_visibleItems) {
877 visibleItemsHeight += item->height();
878 }
879 nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
880// qDebug() << m_firstVisibleIndex << visibleItemsHeight << visibleItems << nonCreatedHeight;
881 }
882 m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + (m_headerItem ? m_headerItem->height() : 0);
883 }
886ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
888 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
889 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
890 return m_visibleItems[visibleIndexedModelIndex];
892 return nullptr;
895void ListViewWithPageHeader::layout()
897 if (!m_visibleItems.isEmpty()) {
898 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
899 const qreal visibleTo = contentY() + height() - m_clipItem->y();
901 qreal pos = m_visibleItems.first()->y();
903// qDebug() << "ListViewWithPageHeader::layout Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
904 int firstReallyVisibleItem = -1;
905 int modelIndex = m_firstVisibleIndex;
906 foreach(ListItem *item, m_visibleItems) {
907 const bool cull = pos + item->height() < visibleFrom || pos >= visibleTo;
908 item->setCulled(cull);
909 item->setY(pos);
910 if (!cull && firstReallyVisibleItem == -1) {
911 firstReallyVisibleItem = modelIndex;
912 if (m_topSectionItem) {
913 // Update the top sticky section header
914 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
915 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
916 context->setContextProperty(QLatin1String("section"), section);
918 // Positing the top section sticky item is a two step process
919 // First we set it either we cull it (because it doesn't need to be sticked to the top)
920 // or stick it to the top
921 // Then after the loop we'll make sure that if there's another section just below it
922 // pushed the sticky section up to make it disappear
923 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
924 if (topSectionStickPos <= pos) {
925 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
926 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(false);
927 } else {
928 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
929 m_topSectionItem->setY(topSectionStickPos);
930 if (item->m_sectionItem) {
931 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(true);
932 }
933 }
934 }
935 }
936 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
937 if (!cull && pos < visibleFrom) {
938 context->setContextProperty(QLatin1String("heightToClip"), visibleFrom - pos);
939 } else {
940 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
941 }
942// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
943 pos += item->height();
944 ++modelIndex;
945 }
947 // Second step of section sticky item positioning
948 // Look at the next section header, check if it's pushing up the sticky one
949 if (m_topSectionItem) {
950 if (firstReallyVisibleItem >= 0) {
951 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
952 ListItem *item = m_visibleItems[i];
953 if (item->m_sectionItem) {
954 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
955 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
956 }
957 break;
958 }
959 }
960 }
961 }
962 }
965void ListViewWithPageHeader::updatePolish()
967 if (!model())
968 return;
970 layout();
972 refill();
974 if (m_contentHeightDirty) {
975 qreal contentHeight;
976 const int modelCount = model()->rowCount();
977 const int visibleItems = m_visibleItems.count();
978 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
979 if (lastValidIndex == modelCount - 1) {
980 ListItem *item = m_visibleItems.last();
981 contentHeight = item->y() + item->height() + m_clipItem->y();
982 if (m_firstVisibleIndex != 0) {
983 // Make sure that if we are shrinking we tell the view we still fit
984 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
985 }
986 } else {
987 contentHeight = m_headerItem ? m_headerItem->height() : 0;
988 if (!m_visibleItems.isEmpty()) {
989 foreach(ListItem *item, m_visibleItems) {
990 contentHeight += item->height();
991 }
992 const int unknownSizes = modelCount - visibleItems;
993 contentHeight += unknownSizes * contentHeight / visibleItems;
994 }
995 }
997 m_contentHeightDirty = false;
998 setContentHeight(contentHeight);
999 }
1002#include "moc_listviewwithpageheader.cpp"