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/>.
17#include "listviewwithpageheader.h"
21#include <qqmlengine.h>
22#include <private/qquickvisualdatamodel_p.h>
23#include <private/qqmlglobal_p.h>
24#include <private/qquickitem_p.h>
25#include <private/qquickanimation_p.h>
26// #include <private/qquickrectangle_p.h>
28qreal ListViewWithPageHeader::ListItem::height() const
30 return m_item->height() + (m_sectionItem ? m_sectionItem->height() : 0);
33qreal ListViewWithPageHeader::ListItem::y() const
35 return m_item->y() - (m_sectionItem ? m_sectionItem->height() : 0);
38void ListViewWithPageHeader::ListItem::setY(qreal newY)
41 m_sectionItem->setY(newY);
42 m_item->setY(newY + m_sectionItem->height());
48bool ListViewWithPageHeader::ListItem::culled() const
50 return QQuickItemPrivate::get(m_item)->culled;
53void ListViewWithPageHeader::ListItem::setCulled(bool culled)
55 QQuickItemPrivate::get(m_item)->setCulled(culled);
57 QQuickItemPrivate::get(m_sectionItem)->setCulled(culled);
60ListViewWithPageHeader::ListViewWithPageHeader()
61 : m_delegateModel(nullptr)
62 , m_delegateValidated(false)
63 , m_firstVisibleIndex(-1)
65 , m_contentHeightDirty(false)
66 , m_headerItem(nullptr)
67 , m_previousContentY(0)
68 , m_headerItemShownHeight(0)
69 , m_sectionDelegate(nullptr)
70 , m_topSectionItem(nullptr)
71 , m_forceNoClip(false)
73 m_clipItem = new QQuickItem(contentItem());
74// m_clipItem = new QQuickRectangle(contentItem());
75// ((QQuickRectangle*)m_clipItem)->setColor(Qt::gray);
77 m_headerShowAnimation = new QQuickNumberAnimation(this);
78 m_headerShowAnimation->setEasing(QEasingCurve::OutQuad);
79 m_headerShowAnimation->setProperty("contentY");
80 m_headerShowAnimation->setDuration(200);
81 m_headerShowAnimation->setTargetObject(this);
83 connect(this, SIGNAL(contentWidthChanged()), this, SLOT(onContentWidthChanged()));
84 connect(this, SIGNAL(contentHeightChanged()), this, SLOT(onContentHeightChanged()));
85 connect(this, SIGNAL(heightChanged()), this, SLOT(onHeightChanged()));
88ListViewWithPageHeader::~ListViewWithPageHeader()
92QAbstractItemModel *ListViewWithPageHeader::model() const
94 return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
97void ListViewWithPageHeader::setModel(QAbstractItemModel *model)
99 if (model != this->model()) {
100 if (!m_delegateModel) {
101 createDelegateModel();
103 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
105 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
106 connect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
107 Q_EMIT modelChanged();
109// Q_EMIT contentHeightChanged();
110// Q_EMIT contentYChanged();
114QQmlComponent *ListViewWithPageHeader::delegate() const
116 return m_delegateModel ? m_delegateModel->delegate() : nullptr;
119void ListViewWithPageHeader::setDelegate(QQmlComponent *delegate)
121 if (delegate != this->delegate()) {
122 if (!m_delegateModel) {
123 createDelegateModel();
126 // Cleanup the existing items
127 foreach(ListItem *item, m_visibleItems)
129 m_visibleItems.clear();
130 m_firstVisibleIndex = -1;
134 m_delegateModel->setDelegate(delegate);
136 Q_EMIT delegateChanged();
137 m_delegateValidated = false;
138 m_contentHeightDirty = true;
143QQuickItem *ListViewWithPageHeader::header() const
148void ListViewWithPageHeader::setHeader(QQuickItem *headerItem)
150 if (m_headerItem != headerItem) {
151 qreal oldHeaderHeight = 0;
152 qreal oldHeaderY = 0;
154 oldHeaderHeight = m_headerItem->height();
155 oldHeaderY = m_headerItem->y();
156 QQuickItemPrivate::get(m_headerItem)->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
157 m_headerItem->setParentItem(nullptr);
159 m_headerItem = headerItem;
161 m_headerItem->setParentItem(contentItem());
162 m_headerItem->setZ(1);
163 QQuickItemPrivate::get(m_headerItem)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
165 qreal newHeaderHeight = m_headerItem ? m_headerItem->height() : 0;
166 if (!m_visibleItems.isEmpty() && newHeaderHeight != oldHeaderHeight) {
167 headerHeightChanged(newHeaderHeight, oldHeaderHeight, oldHeaderY);
169 m_contentHeightDirty = true;
171 Q_EMIT headerChanged();
175QQmlComponent *ListViewWithPageHeader::sectionDelegate() const
177 return m_sectionDelegate;
180void ListViewWithPageHeader::setSectionDelegate(QQmlComponent *delegate)
182 if (delegate != m_sectionDelegate) {
183 // TODO clean existing sections
185 m_sectionDelegate = delegate;
187 m_topSectionItem = getSectionItem(QString());
188 m_topSectionItem->setZ(3);
189 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
191 // TODO create sections for existing items
193 Q_EMIT sectionDelegateChanged();
197QString ListViewWithPageHeader::sectionProperty() const
199 return m_sectionProperty;
202void ListViewWithPageHeader::setSectionProperty(const QString &property)
204 if (property != m_sectionProperty) {
205 m_sectionProperty = property;
207 updateWatchedRoles();
209 // TODO recreate sections
211 Q_EMIT sectionPropertyChanged();
215bool ListViewWithPageHeader::forceNoClip() const
217 return m_forceNoClip;
220void ListViewWithPageHeader::setForceNoClip(bool noClip)
222 if (noClip != m_forceNoClip) {
223 m_forceNoClip = noClip;
225 Q_EMIT forceNoClipChanged();
229void ListViewWithPageHeader::positionAtBeginning()
231 qreal headerHeight = (m_headerItem ? m_headerItem->height() : 0);
232 if (m_firstVisibleIndex != 0) {
233 // This could be optimized by trying to reuse the interesection
234 // of items that may end up intersecting between the existing
235 // m_visibleItems and the items we are creating in the next loop
236 foreach(ListItem *item, m_visibleItems)
238 m_visibleItems.clear();
239 m_firstVisibleIndex = -1;
241 // Create the item 0, it will be already correctly positioned at createItem()
243 ListItem *item = createItem(0, false);
244 // Create the subsequent items
246 qreal pos = item->y() + item->height();
247 const qreal buffer = height() / 2;
248 const qreal bufferTo = height() + buffer;
249 while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
250 if (!(item = createItem(modelIndex, false)))
252 pos += item->height();
256 m_previousContentY = m_visibleItems.first()->y() - headerHeight;
258 setContentY(m_visibleItems.first()->y() + m_clipItem->y() - headerHeight);
261void ListViewWithPageHeader::showHeader()
263 auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
264 if (to != contentY()) {
265 m_headerShowAnimation->setTo(to);
266 m_headerShowAnimation->start();
270qreal ListViewWithPageHeader::minYExtent() const
272// qDebug() << "ListViewWithPageHeader::minYExtent" << m_minYExtent;
276void ListViewWithPageHeader::componentComplete()
279 m_delegateModel->componentComplete();
281 QQuickFlickable::componentComplete();
286void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
288 QQuickFlickable::viewportMoved(orient);
289// qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
290 qreal diff = m_previousContentY - contentY();
292 auto oldHeaderItemShownHeight = m_headerItemShownHeight;
293 if (contentY() < -m_minYExtent) {
294 // Stick the header item to the top when dragging down
295 m_headerItem->setY(contentY());
297 // We are going down (but it's not because of the rebound at the end)
298 // (but the header was not shown by it's own position)
299 // or the header is partially shown
300 const bool scrolledUp = m_previousContentY > contentY();
301 const bool notRebounding = contentY() + height() < contentHeight();
302 const bool notShownByItsOwn = contentY() + diff > m_headerItem->y() + m_headerItem->height();
304 if (!scrolledUp && contentY() == -m_minYExtent) {
305 m_headerItemShownHeight = 0;
306 m_headerItem->setY(contentY());
307 } else if ((scrolledUp && notRebounding && notShownByItsOwn) || (m_headerItemShownHeight > 0)) {
308 m_headerItemShownHeight += diff;
309 if (contentY() == -m_minYExtent) {
310 m_headerItemShownHeight = 0;
312 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
314 if (m_headerItemShownHeight > 0) {
315 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
317 m_headerItem->setY(-m_minYExtent);
321 // We will be changing the clip item, need to accomadate for it
322 // otherwise we move the firstItem down/up twice (unless the
323 // show header animation is running, where we want a different effect)
324 if (!m_headerShowAnimation->isRunning())
325 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
329 if (!m_visibleItems.isEmpty()) {
331 ListItem *firstItem = m_visibleItems.first();
332 firstItem->setY(firstItem->y() + diff);
333 if (m_headerShowAnimation->isRunning()) {
338 m_previousContentY = contentY();
342void ListViewWithPageHeader::createDelegateModel()
344 m_delegateModel = new QQuickVisualDataModel(qmlContext(this), this);
345 connect(m_delegateModel, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(itemCreated(int,QQuickItem*)));
346 if (isComponentComplete())
347 m_delegateModel->componentComplete();
348 updateWatchedRoles();
351void ListViewWithPageHeader::refill()
353 if (!isComponentComplete()) {
354 qWarning() << "Incomplete ListViewWithPageHeader::refill";
358 const qreal buffer = height() / 2;
359 const qreal from = contentY();
360 const qreal to = from + height();
361 const qreal bufferFrom = from - buffer;
362 const qreal bufferTo = to + buffer;
364 bool added = addVisibleItems(from, to, false);
365 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
366 added |= addVisibleItems(bufferFrom, bufferTo, true);
368 if (added || removed) {
369 m_contentHeightDirty = true;
373bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
378 if (m_delegateModel->count() == 0)
382// qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;
386 if (!m_visibleItems.isEmpty()) {
387 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
388 item = m_visibleItems.last();
389 pos = item->y() + item->height() + m_clipItem->y();
391 bool changed = false;
392// qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
393 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
394// qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
395 if (!(item = createItem(modelIndex, asynchronous)))
397 pos += item->height();
404 if (!m_visibleItems.isEmpty()) {
405 modelIndex = m_firstVisibleIndex - 1;
406 item = m_visibleItems.first();
407 pos = item->y() + m_clipItem->y();
409 while (modelIndex >= 0 && pos > fillFrom) {
410// qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
411 if (!(item = createItem(modelIndex, asynchronous)))
413 pos -= item->height();
421void ListViewWithPageHeader::releaseItem(ListItem *listItem)
423 QQuickItem *item = listItem->m_item;
424 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
425 itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
426 QQuickVisualModel::ReleaseFlags flags = m_delegateModel->release(item);
427 if (flags & QQuickVisualModel::Destroyed) {
428 item->setParentItem(nullptr);
430 delete listItem->m_sectionItem;
434void ListViewWithPageHeader::updateWatchedRoles()
436 if (m_delegateModel) {
437 QList<QByteArray> roles;
438 if (!m_sectionProperty.isEmpty())
439 roles << m_sectionProperty.toUtf8();
440 m_delegateModel->setWatchedRoles(roles);
444QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex)
446 if (!m_sectionDelegate)
449 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
450 if (modelIndex > 0) {
451 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
452 if (section == prevSection)
456 return getSectionItem(section);
459QQuickItem *ListViewWithPageHeader::getSectionItem(const QString §ionText)
461 QQuickItem *sectionItem = nullptr;
463 QQmlContext *creationContext = m_sectionDelegate->creationContext();
464 QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
465 context->setContextProperty(QLatin1String("section"), sectionText);
466 QObject *nobj = m_sectionDelegate->beginCreate(context);
468 QQml_setParent_noEvent(context, nobj);
469 sectionItem = qobject_cast<QQuickItem *>(nobj);
473 sectionItem->setZ(2);
474 QQml_setParent_noEvent(sectionItem, m_clipItem);
475 sectionItem->setParentItem(m_clipItem);
480 m_sectionDelegate->completeCreate();
482 // TODO attach to sectionItem so we can accomodate to it changing its height
487bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
489// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
490 bool changed = false;
492 bool foundVisible = false;
494 int removedItems = 0;
495 while (i < m_visibleItems.count()) {
496 ListItem *item = m_visibleItems[i];
497 const qreal pos = item->y() + m_clipItem->y();
498// qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
499 if (pos + item->height() < bufferFrom || pos > bufferTo) {
500// qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
502 m_visibleItems.removeAt(i);
508 const int itemIndex = m_firstVisibleIndex + removedItems + i;
509 m_firstVisibleIndex = itemIndex;
518ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
520// qDebug() << "CREATE ITEM" << modelIndex;
521 if (asynchronous && m_asyncRequestedIndexes.contains(modelIndex))
524 m_asyncRequestedIndexes.remove(modelIndex);
525 QQuickItem *item = m_delegateModel->item(modelIndex, asynchronous);
527 m_asyncRequestedIndexes << modelIndex;
530// qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
531 ListItem *listItem = new ListItem;
532 listItem->m_item = item;
533 listItem->m_sectionItem = getSectionItem(modelIndex);
534 QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
535 ListItem *prevItem = itemAtIndex(modelIndex - 1);
537 listItem->setY(prevItem->y() + prevItem->height());
539 ListItem *nextItem = itemAtIndex(modelIndex + 1);
541 listItem->setY(nextItem->y() - listItem ->height());
542 } else if (modelIndex == 0 && m_headerItem) {
543 listItem->setY(m_headerItem->height());
546 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() < contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
547 if (m_visibleItems.isEmpty()) {
548 m_visibleItems << listItem;
550 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
552 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
553 m_firstVisibleIndex = modelIndex;
560void ListViewWithPageHeader::itemCreated(int modelIndex, QQuickItem *item)
562// qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
564 item->setParentItem(m_clipItem);
565 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
566 context->setContextProperty(QLatin1String("ListViewWithPageHeader"), this);
567 if (m_asyncRequestedIndexes.remove(modelIndex)) {
568 createItem(modelIndex, false);
572void ListViewWithPageHeader::updateClipItem()
574 m_clipItem->setHeight(height() - m_headerItemShownHeight);
575 m_clipItem->setY(contentY() + m_headerItemShownHeight);
576 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
579void ListViewWithPageHeader::onContentHeightChanged()
584void ListViewWithPageHeader::onContentWidthChanged()
586 m_clipItem->setWidth(contentItem()->width());
589void ListViewWithPageHeader::onHeightChanged()
591 setMaximumFlickVelocity(height() * 10);
592 setFlickDeceleration(height() * 2);
595void ListViewWithPageHeader::onModelUpdated(const QQuickChangeSet &changeSet, bool /*reset*/)
597 // TODO Do something with reset
598// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
599 foreach(const QQuickChangeSet::Remove &remove, changeSet.removes()) {
600// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove;
601 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
602 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
603 // If all the items we are removing are either not created or culled
604 // we have to grow down to avoid viewport changing
605 bool growDown = true;
606 for (int i = 0; growDown && i < remove.count; ++i) {
607 const int modelIndex = remove.index + i;
608 ListItem *item = itemAtIndex(modelIndex);
609 if (item && !item->culled()) {
613 for (int i = remove.count - 1; i >= 0; --i) {
614 const int visibleIndex = remove.index + i - m_firstVisibleIndex;
615 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
616 ListItem *item = m_visibleItems[visibleIndex];
618 m_visibleItems.removeAt(visibleIndex);
623 } else if (remove.index <= m_firstVisibleIndex && !m_visibleItems.isEmpty()) {
624 // We removed the first item that is the one that positions the rest
625 // position the new first item correctly
626 m_visibleItems.first()->setY(oldFirstValidIndexPos);
628 } else if (remove.index + remove.count <= m_firstVisibleIndex) {
629 m_firstVisibleIndex -= remove.count;
633 foreach(const QQuickChangeSet::Insert &insert, changeSet.inserts()) {
634// qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert;
635 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
636 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
637 if (insertingInValidIndexes || firstItemWithViewOnTop)
639 // If the items we are adding won't be really visible
640 // we grow up instead of down to not change the viewport
642 if (!firstItemWithViewOnTop) {
643 for (int i = 0; i < m_visibleItems.count(); ++i) {
644 if (!m_visibleItems[i]->culled()) {
645 if (insert.index <= m_firstVisibleIndex + i) {
653 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
654 for (int i = insert.count - 1; i >= 0; --i) {
655 const int modelIndex = insert.index + i;
656 ListItem *item = createItem(modelIndex, false);
658 ListItem *firstItem = m_visibleItems.first();
659 firstItem->setY(firstItem->y() - item->height());
663 if (firstItemWithViewOnTop) {
664 ListItem *firstItem = m_visibleItems.first();
665 firstItem->setY(oldFirstValidIndexPos);
667 } else if (insert.index <= m_firstVisibleIndex) {
668 m_firstVisibleIndex += insert.count;
673 m_contentHeightDirty = true;
676void ListViewWithPageHeader::itemGeometryChanged(QQuickItem *item, const QRectF &newGeometry, const QRectF &oldGeometry)
678 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
679 if (heightDiff != 0) {
680 if (item == m_headerItem) {
681 headerHeightChanged(newGeometry.height(), oldGeometry.height(), oldGeometry.y());
683 if (oldGeometry.y() + oldGeometry.height() + m_clipItem->y() < contentY() && !m_visibleItems.isEmpty()) {
684 ListItem *firstItem = m_visibleItems.first();
685 firstItem->setY(firstItem->y() - heightDiff);
690 m_contentHeightDirty = true;
694void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
696 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
697 if (m_headerItemShownHeight > 0) {
698 // If the header is shown because of the clip
700 m_headerItemShownHeight += heightDiff;
701 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
705 if (oldHeaderY + oldHeaderHeight > contentY()) {
706 // If the header is shown because its position
708 ListItem *firstItem = m_visibleItems.first();
709 firstItem->setY(firstItem->y() + heightDiff);
711 // If the header is not on screen, just change the start of the list
712 // so the viewport is not changed
719void ListViewWithPageHeader::adjustMinYExtent()
721 if (m_visibleItems.isEmpty()) {
724 m_minYExtent = -m_visibleItems.first()->y() - m_clipItem->y() + (m_headerItem ? m_headerItem->height() : 0);
728ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
730 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
731 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
732 return m_visibleItems[visibleIndexedModelIndex];
737void ListViewWithPageHeader::updatePolish()
742 if (!m_visibleItems.isEmpty()) {
743 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
744 const qreal visibleTo = contentY() + height() - m_clipItem->y();
746 qreal pos = m_visibleItems.first()->y();
748// qDebug() << "ListViewWithPageHeader::updatePolish Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
749 int firstReallyVisibleItem = -1;
750 int modelIndex = m_firstVisibleIndex;
751 foreach(ListItem *item, m_visibleItems) {
752 const bool cull = pos + item->height() < visibleFrom || pos >= visibleTo;
753 item->setCulled(cull);
755 if (!cull && firstReallyVisibleItem == -1) {
756 firstReallyVisibleItem = modelIndex;
757 if (m_topSectionItem) {
758 // Update the top sticky section header
759 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
760 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
761 context->setContextProperty(QLatin1String("section"), section);
763 // Positing the top section sticky item is a two step process
764 // First we set it either we cull it (because it doesn't need to be sticked to the top)
765 // or stick it to the top
766 // Then after the loop we'll make sure that if there's another section just below it
767 // pushed the sticky section up to make it disappear
768 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
769 if (topSectionStickPos <= pos) {
770 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
771 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(false);
773 QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
774 m_topSectionItem->setY(topSectionStickPos);
775 if (item->m_sectionItem) {
776 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(true);
781 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
782 if (!cull && pos < visibleFrom) {
783 context->setContextProperty(QLatin1String("heightToClip"), visibleFrom - pos);
785 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
787// qDebug() << "ListViewWithPageHeader::updatePolish" << /*index << */pos + m_clipItem->y() << /*QQuickItemPrivate::get(item)->culled <<*/ (pos + item->height() < visibleFrom) << (pos > visibleTo);
788 pos += item->height();
792 // Second step of section sticky item positioning
793 // Look at the next section header, check if it's pushing up the sticky one
794 if (m_topSectionItem) {
795 for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
796 ListItem *item = m_visibleItems[i];
797 if (item->m_sectionItem) {
798 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
799 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
809 if (m_contentHeightDirty) {
811 const int modelCount = model()->rowCount();
812 const int visibleItems = m_visibleItems.count();
813 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
814 if (lastValidIndex == modelCount - 1) {
815 ListItem *item = m_visibleItems.last();
816 contentHeight = item->y() + item->height() + m_clipItem->y();
817 if (m_firstVisibleIndex != 0) {
818 // Make sure that if we are shrinking we tell the view we still fit
819 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
822 contentHeight = m_headerItem ? m_headerItem->height() : 0;
823 if (!m_visibleItems.isEmpty()) {
824 foreach(ListItem *item, m_visibleItems) {
825 contentHeight += item->height();
827 const int unknownSizes = modelCount - visibleItems;
828 contentHeight += unknownSizes * contentHeight / visibleItems;
832 m_contentHeightDirty = false;
833 setContentHeight(contentHeight);
838#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