2 * Copyright (C) 2013 Canonical, Ltd.
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.
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.
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/>.
18 * Some documentation on how this thing works:
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.
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.
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.
39 * * Make sure all items are positioned correctly
40 * * Add/Remove items if needed
41 * * Update the content height if it was dirty
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
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.
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())
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.
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.
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.
79#include "listviewwithpageheader.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)
103 m_sectionItem->setY(newY);
104 m_item->setY(newY + m_sectionItem->height());
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);
119 QQuickItemPrivate::get(m_sectionItem)->setCulled(culled);
122ListViewWithPageHeader::ListViewWithPageHeader()
123 : m_delegateModel(nullptr)
124 , m_delegateValidated(false)
125 , m_firstVisibleIndex(-1)
127 , m_contentHeightDirty(false)
128 , m_headerItem(nullptr)
129 , m_previousContentY(0)
130 , m_headerItemShownHeight(0)
131 , m_sectionDelegate(nullptr)
132 , m_topSectionItem(nullptr)
133 , m_forceNoClip(false)
135 m_clipItem = new QQuickItem(contentItem());
136// m_clipItem = new QQuickRectangle(contentItem());
137// ((QQuickRectangle*)m_clipItem)->setColor(Qt::gray);
139 m_headerShowAnimation = new QQuickNumberAnimation(this);
140 m_headerShowAnimation->setEasing(QEasingCurve::OutQuad);
141 m_headerShowAnimation->setProperty("contentY");
142 m_headerShowAnimation->setDuration(200);
143 m_headerShowAnimation->setTargetObject(this);
145 connect(this, SIGNAL(contentWidthChanged()), this, SLOT(onContentWidthChanged()));
146 connect(this, SIGNAL(contentHeightChanged()), this, SLOT(onContentHeightChanged()));
147 connect(this, SIGNAL(heightChanged()), this, SLOT(onHeightChanged()));
150ListViewWithPageHeader::~ListViewWithPageHeader()
154QAbstractItemModel *ListViewWithPageHeader::model() const
156 return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
159void ListViewWithPageHeader::setModel(QAbstractItemModel *model)
161 if (model != this->model()) {
162 if (!m_delegateModel) {
163 createDelegateModel();
165 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
167 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
168 connect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
169 Q_EMIT modelChanged();
171// Q_EMIT contentHeightChanged();
172// Q_EMIT contentYChanged();
176QQmlComponent *ListViewWithPageHeader::delegate() const
178 return m_delegateModel ? m_delegateModel->delegate() : nullptr;
181void ListViewWithPageHeader::setDelegate(QQmlComponent *delegate)
183 if (delegate != this->delegate()) {
184 if (!m_delegateModel) {
185 createDelegateModel();
188 // Cleanup the existing items
189 foreach(ListItem *item, m_visibleItems)
191 m_visibleItems.clear();
192 m_firstVisibleIndex = -1;
196 m_delegateModel->setDelegate(delegate);
198 Q_EMIT delegateChanged();
199 m_delegateValidated = false;
200 m_contentHeightDirty = true;
205QQuickItem *ListViewWithPageHeader::header() const
210void ListViewWithPageHeader::setHeader(QQuickItem *headerItem)
212 if (m_headerItem != headerItem) {
213 qreal oldHeaderHeight = 0;
214 qreal oldHeaderY = 0;
216 oldHeaderHeight = m_headerItem->height();
217 oldHeaderY = m_headerItem->y();
218 QQuickItemPrivate::get(m_headerItem)->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
219 m_headerItem->setParentItem(nullptr);
221 m_headerItem = headerItem;
223 m_headerItem->setParentItem(contentItem());
224 m_headerItem->setZ(1);
225 QQuickItemPrivate::get(m_headerItem)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
227 qreal newHeaderHeight = m_headerItem ? m_headerItem->height() : 0;
228 if (!m_visibleItems.isEmpty() && newHeaderHeight != oldHeaderHeight) {
229 headerHeightChanged(newHeaderHeight, oldHeaderHeight, oldHeaderY);
231 m_contentHeightDirty = true;
233 Q_EMIT headerChanged();
237QQmlComponent *ListViewWithPageHeader::sectionDelegate() const
239 return m_sectionDelegate;
242void ListViewWithPageHeader::setSectionDelegate(QQmlComponent *delegate)
244 if (delegate != m_sectionDelegate) {
245 // TODO clean existing sections
247 m_sectionDelegate = delegate;
249 m_topSectionItem = getSectionItem(QString());
250 m_topSectionItem->setZ(3);
251 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
253 // TODO create sections for existing items
255 Q_EMIT sectionDelegateChanged();
259QString ListViewWithPageHeader::sectionProperty() const
261 return m_sectionProperty;
264void ListViewWithPageHeader::setSectionProperty(const QString &property)
266 if (property != m_sectionProperty) {
267 m_sectionProperty = property;
269 updateWatchedRoles();
271 // TODO recreate sections
273 Q_EMIT sectionPropertyChanged();
277bool ListViewWithPageHeader::forceNoClip() const
279 return m_forceNoClip;
282void ListViewWithPageHeader::setForceNoClip(bool noClip)
284 if (noClip != m_forceNoClip) {
285 m_forceNoClip = noClip;
287 Q_EMIT forceNoClipChanged();
291void ListViewWithPageHeader::positionAtBeginning()
293 qreal headerHeight = (m_headerItem ? m_headerItem->height() : 0);
294 if (m_firstVisibleIndex != 0) {
295 // This could be optimized by trying to reuse the interesection
296 // of items that may end up intersecting between the existing
297 // m_visibleItems and the items we are creating in the next loop
298 foreach(ListItem *item, m_visibleItems)
300 m_visibleItems.clear();
301 m_firstVisibleIndex = -1;
303 // Create the item 0, it will be already correctly positioned at createItem()
305 ListItem *item = createItem(0, false);
306 // Create the subsequent items
308 qreal pos = item->y() + item->height();
309 const qreal buffer = height() / 2;
310 const qreal bufferTo = height() + buffer;
311 while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
312 if (!(item = createItem(modelIndex, false)))
314 pos += item->height();
318 m_previousContentY = m_visibleItems.first()->y() - headerHeight;
320 setContentY(m_visibleItems.first()->y() + m_clipItem->y() - headerHeight);
323void ListViewWithPageHeader::showHeader()
325 auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
326 if (to != contentY()) {
327 m_headerShowAnimation->setTo(to);
328 m_headerShowAnimation->start();
332qreal ListViewWithPageHeader::minYExtent() const
334// qDebug() << "ListViewWithPageHeader::minYExtent" << m_minYExtent;
338void ListViewWithPageHeader::componentComplete()
341 m_delegateModel->componentComplete();
343 QQuickFlickable::componentComplete();
348void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
350 QQuickFlickable::viewportMoved(orient);
351// qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
352 qreal diff = m_previousContentY - contentY();
354 auto oldHeaderItemShownHeight = m_headerItemShownHeight;
355 if (contentY() < -m_minYExtent) {
356 // Stick the header item to the top when dragging down
357 m_headerItem->setY(contentY());
359 // We are going down (but it's not because of the rebound at the end)
360 // (but the header was not shown by it's own position)
361 // or the header is partially shown
362 const bool scrolledUp = m_previousContentY > contentY();
363 const bool notRebounding = contentY() + height() < contentHeight();
364 const bool notShownByItsOwn = contentY() + diff > m_headerItem->y() + m_headerItem->height();
366 if (!scrolledUp && contentY() == -m_minYExtent) {
367 m_headerItemShownHeight = 0;
368 m_headerItem->setY(contentY());
369 } else if ((scrolledUp && notRebounding && notShownByItsOwn) || (m_headerItemShownHeight > 0)) {
370 m_headerItemShownHeight += diff;
371 if (contentY() == -m_minYExtent) {
372 m_headerItemShownHeight = 0;
374 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
376 if (m_headerItemShownHeight > 0) {
377 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
379 m_headerItem->setY(-m_minYExtent);
383 // We will be changing the clip item, need to accomadate for it
384 // otherwise we move the firstItem down/up twice (unless the
385 // show header animation is running, where we want to keep the viewport stable)
386 if (!m_headerShowAnimation->isRunning())
387 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
391 if (!m_visibleItems.isEmpty()) {
393 ListItem *firstItem = m_visibleItems.first();
394 firstItem->setY(firstItem->y() + diff);
395 if (m_headerShowAnimation->isRunning()) {
400 m_previousContentY = contentY();
404void ListViewWithPageHeader::createDelegateModel()
406 m_delegateModel = new QQuickVisualDataModel(qmlContext(this), this);
407 connect(m_delegateModel, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(itemCreated(int,QQuickItem*)));
408 if (isComponentComplete())
409 m_delegateModel->componentComplete();
410 updateWatchedRoles();
413void ListViewWithPageHeader::refill()
415 if (!isComponentComplete()) {
419 const qreal buffer = height() / 2;
420 const qreal from = contentY();
421 const qreal to = from + height();
422 const qreal bufferFrom = from - buffer;
423 const qreal bufferTo = to + buffer;
425 bool added = addVisibleItems(from, to, false);
426 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
427 added |= addVisibleItems(bufferFrom, bufferTo, true);
429 if (added || removed) {
430 m_contentHeightDirty = true;
434bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
439 if (m_delegateModel->count() == 0)
443// qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
447 if (!m_visibleItems.isEmpty()) {
448 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
449 item = m_visibleItems.last();
450 pos = item->y() + item->height() + m_clipItem->y();
452 bool changed = false;
453// qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
454 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
455// qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
456 if (!(item = createItem(modelIndex, asynchronous)))
458 pos += item->height();
465 if (!m_visibleItems.isEmpty()) {
466 modelIndex = m_firstVisibleIndex - 1;
467 item = m_visibleItems.first();
468 pos = item->y() + m_clipItem->y();
470 while (modelIndex >= 0 && pos > fillFrom) {
471// qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
472 if (!(item = createItem(modelIndex, asynchronous)))
474 pos -= item->height();
482void ListViewWithPageHeader::releaseItem(ListItem *listItem)
484 QQuickItem *item = listItem->m_item;
485 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
486 itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
487 QQuickVisualModel::ReleaseFlags flags = m_delegateModel->release(item);
488 if (flags & QQuickVisualModel::Destroyed) {
489 item->setParentItem(nullptr);
491 delete listItem->m_sectionItem;
495void ListViewWithPageHeader::updateWatchedRoles()
497 if (m_delegateModel) {
498 QList<QByteArray> roles;
499 if (!m_sectionProperty.isEmpty())
500 roles << m_sectionProperty.toUtf8();
501 m_delegateModel->setWatchedRoles(roles);
505QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex)
507 if (!m_sectionDelegate)
510 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
511 if (modelIndex > 0) {
512 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
513 if (section == prevSection)
517 return getSectionItem(section);
520QQuickItem *ListViewWithPageHeader::getSectionItem(const QString §ionText)
522 QQuickItem *sectionItem = nullptr;
524 QQmlContext *creationContext = m_sectionDelegate->creationContext();
525 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
526 context->setContextProperty(QLatin1String("section"), sectionText);
527 QObject *nobj = m_sectionDelegate->beginCreate(context);
529 QQml_setParent_noEvent(context, nobj);
530 sectionItem = qobject_cast<QQuickItem *>(nobj);
534 sectionItem->setZ(2);
535 QQml_setParent_noEvent(sectionItem, m_clipItem);
536 sectionItem->setParentItem(m_clipItem);
541 m_sectionDelegate->completeCreate();
543 // TODO attach to sectionItem so we can accomodate to it changing its height
548bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
550// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
551 bool changed = false;
553 bool foundVisible = false;
555 int removedItems = 0;
556 while (i < m_visibleItems.count()) {
557 ListItem *item = m_visibleItems[i];
558 const qreal pos = item->y() + m_clipItem->y();
559// qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
560 if (pos + item->height() < bufferFrom || pos > bufferTo) {
561// qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
563 m_visibleItems.removeAt(i);
569 const int itemIndex = m_firstVisibleIndex + removedItems + i;
570 m_firstVisibleIndex = itemIndex;
579ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
581// qDebug() << "CREATE ITEM" << modelIndex;
582 if (asynchronous && m_asyncRequestedIndexes.contains(modelIndex))
585 m_asyncRequestedIndexes.remove(modelIndex);
586 QQuickItem *item = m_delegateModel->item(modelIndex, asynchronous);
588 m_asyncRequestedIndexes << modelIndex;
591// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
592 ListItem *listItem = new ListItem;
593 listItem->m_item = item;
594 listItem->m_sectionItem = getSectionItem(modelIndex);
595 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
596 ListItem *prevItem = itemAtIndex(modelIndex - 1);
598 listItem->setY(prevItem->y() + prevItem->height());
600 ListItem *nextItem = itemAtIndex(modelIndex + 1);
602 listItem->setY(nextItem->y() - listItem ->height());
603 } else if (modelIndex == 0 && m_headerItem) {
604 listItem->setY(m_headerItem->height());
607 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() < contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
608 if (m_visibleItems.isEmpty()) {
609 m_visibleItems << listItem;
611 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
613 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
614 m_firstVisibleIndex = modelIndex;
621void ListViewWithPageHeader::itemCreated(int modelIndex, QQuickItem *item)
623// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
625 item->setParentItem(m_clipItem);
626 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
627 context->setContextProperty(QLatin1String("ListViewWithPageHeader"), this);
628 if (m_asyncRequestedIndexes.remove(modelIndex)) {
629 createItem(modelIndex, false);
633void ListViewWithPageHeader::updateClipItem()
635 m_clipItem->setHeight(height() - m_headerItemShownHeight);
636 m_clipItem->setY(contentY() + m_headerItemShownHeight);
637 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
640void ListViewWithPageHeader::onContentHeightChanged()
645void ListViewWithPageHeader::onContentWidthChanged()
647 m_clipItem->setWidth(contentItem()->width());
650void ListViewWithPageHeader::onHeightChanged()
652 setMaximumFlickVelocity(height() * 10);
653 setFlickDeceleration(height() * 2);
656void ListViewWithPageHeader::onModelUpdated(const QQuickChangeSet &changeSet, bool /*reset*/)
658 // TODO Do something with reset
659// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
660 foreach(const QQuickChangeSet::Remove &remove, changeSet.removes()) {
661// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove;
662 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
663 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
664 // If all the items we are removing are either not created or culled
665 // we have to grow down to avoid viewport changing
666 bool growDown = true;
667 for (int i = 0; growDown && i < remove.count; ++i) {
668 const int modelIndex = remove.index + i;
669 ListItem *item = itemAtIndex(modelIndex);
670 if (item && !item->culled()) {
674 for (int i = remove.count - 1; i >= 0; --i) {
675 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
676 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
677 ListItem *item = m_visibleItems[visibleIndex];
679 m_visibleItems.removeAt(visibleIndex);
684 } else if (remove.index <= m_firstVisibleIndex && !m_visibleItems.isEmpty()) {
685 // We removed the first item that is the one that positions the rest
686 // position the new first item correctly
687 m_visibleItems.first()->setY(oldFirstValidIndexPos);
689 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
690 m_firstVisibleIndex -= remove.count;
694 foreach(const QQuickChangeSet::Insert &insert, changeSet.inserts()) {
695// qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert;
696 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
697 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
698 if (insertingInValidIndexes || firstItemWithViewOnTop)
700 // If the items we are adding won't be really visible
701 // we grow up instead of down to not change the viewport
703 if (!firstItemWithViewOnTop) {
704 for (int i = 0; i < m_visibleItems.count(); ++i) {
705 if (!m_visibleItems[i]->culled()) {
706 if (insert.index <= m_firstVisibleIndex + i) {
714 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
715 for (int i = insert.count - 1; i >= 0; --i) {
716 const int modelIndex = insert.index + i;
717 ListItem *item = createItem(modelIndex, false);
719 ListItem *firstItem = m_visibleItems.first();
720 firstItem->setY(firstItem->y() - item->height());
724 if (firstItemWithViewOnTop) {
725 ListItem *firstItem = m_visibleItems.first();
726 firstItem->setY(oldFirstValidIndexPos);
728 } else if (insert.index <= m_firstVisibleIndex) {
729 m_firstVisibleIndex += insert.count;
734 m_contentHeightDirty = true;
737void ListViewWithPageHeader::itemGeometryChanged(QQuickItem *item, const QRectF &newGeometry, const QRectF &oldGeometry)
739 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
740 if (heightDiff != 0) {
741 if (item == m_headerItem) {
742 headerHeightChanged(newGeometry.height(), oldGeometry.height(), oldGeometry.y());
744 if (oldGeometry.y() + oldGeometry.height() + m_clipItem->y() < contentY() && !m_visibleItems.isEmpty()) {
745 ListItem *firstItem = m_visibleItems.first();
746 firstItem->setY(firstItem->y() - heightDiff);
751 m_contentHeightDirty = true;
755void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
757 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
758 if (m_headerItemShownHeight > 0) {
759 // If the header is shown because of the clip
761 m_headerItemShownHeight += heightDiff;
762 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
766 if (oldHeaderY + oldHeaderHeight > contentY()) {
767 // If the header is shown because its position
769 ListItem *firstItem = m_visibleItems.first();
770 firstItem->setY(firstItem->y() + heightDiff);
772 // If the header is not on screen, just change the start of the list
773 // so the viewport is not changed
780void ListViewWithPageHeader::adjustMinYExtent()
782 if (m_visibleItems.isEmpty()) {
785 m_minYExtent = -m_visibleItems.first()->y() - m_clipItem->y() + (m_headerItem ? m_headerItem->height() : 0);
789ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
791 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
792 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
793 return m_visibleItems[visibleIndexedModelIndex];
798void ListViewWithPageHeader::updatePolish()
803 if (!m_visibleItems.isEmpty()) {
804 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
805 const qreal visibleTo = contentY() + height() - m_clipItem->y();
807 qreal pos = m_visibleItems.first()->y();
809// qDebug() << "ListViewWithPageHeader::updatePolish Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
810 int firstReallyVisibleItem = -1;
811 int modelIndex = m_firstVisibleIndex;
812 foreach(ListItem *item, m_visibleItems) {
813 const bool cull = pos + item->height() < visibleFrom || pos >= visibleTo;
814 item->setCulled(cull);
816 if (!cull && firstReallyVisibleItem == -1) {
817 firstReallyVisibleItem = modelIndex;
818 if (m_topSectionItem) {
819 // Update the top sticky section header
820 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
821 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
822 context->setContextProperty(QLatin1String("section"), section);
824 // Positing the top section sticky item is a two step process
825 // First we set it either we cull it (because it doesn't need to be sticked to the top)
826 // or stick it to the top
827 // Then after the loop we'll make sure that if there's another section just below it
828 // pushed the sticky section up to make it disappear
829 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
830 if (topSectionStickPos <= pos) {
831 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
832 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(false);
834 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
835 m_topSectionItem->setY(topSectionStickPos);
836 if (item->m_sectionItem) {
837 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(true);
842 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
843 if (!cull && pos < visibleFrom) {
844 context->setContextProperty(QLatin1String("heightToClip"), visibleFrom - pos);
846 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
848// qDebug() << "ListViewWithPageHeader::updatePolish" << /*index << */pos + m_clipItem->y() << /*QQuickItemPrivate::get(item)->culled <<*/ (pos + item->height() < visibleFrom) << (pos > visibleTo);
849 pos += item->height();
853 // Second step of section sticky item positioning
854 // Look at the next section header, check if it's pushing up the sticky one
855 if (m_topSectionItem) {
856 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
857 ListItem *item = m_visibleItems[i];
858 if (item->m_sectionItem) {
859 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
860 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
870 if (m_contentHeightDirty) {
872 const int modelCount = model()->rowCount();
873 const int visibleItems = m_visibleItems.count();
874 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
875 if (lastValidIndex == modelCount - 1) {
876 ListItem *item = m_visibleItems.last();
877 contentHeight = item->y() + item->height() + m_clipItem->y();
878 if (m_firstVisibleIndex != 0) {
879 // Make sure that if we are shrinking we tell the view we still fit
880 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
883 contentHeight = m_headerItem ? m_headerItem->height() : 0;
884 if (!m_visibleItems.isEmpty()) {
885 foreach(ListItem *item, m_visibleItems) {
886 contentHeight += item->height();
888 const int unknownSizes = modelCount - visibleItems;
889 contentHeight += unknownSizes * contentHeight / visibleItems;
893 m_contentHeightDirty = false;
894 setContentHeight(contentHeight);
898#include "moc_listviewwithpageheader.cpp"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list