92 #include "listviewwithpageheader.h"
94 #include <QCoreApplication>
97 #include <qqmlengine.h>
98 #pragma GCC diagnostic push
99 #pragma GCC diagnostic ignored "-pedantic"
100 #include <private/qqmldelegatemodel_p.h>
101 #include <private/qqmlglobal_p.h>
102 #include <private/qquickitem_p.h>
103 #include <private/qquickanimation_p.h>
104 #pragma GCC diagnostic pop
107 qreal ListViewWithPageHeader::ListItem::height()
const
109 return m_item->height() + (m_sectionItem ? m_sectionItem->height() : 0);
112 qreal ListViewWithPageHeader::ListItem::y()
const
114 return m_item->y() - (m_sectionItem ? m_sectionItem->height() : 0);
117 void ListViewWithPageHeader::ListItem::setY(qreal newY)
120 m_sectionItem->setY(newY);
121 m_item->setY(newY + m_sectionItem->height());
127 bool ListViewWithPageHeader::ListItem::culled()
const
129 return QQuickItemPrivate::get(m_item)->culled;
132 void ListViewWithPageHeader::ListItem::setCulled(
bool culled)
134 QQuickItemPrivate::get(m_item)->setCulled(culled);
136 QQuickItemPrivate::get(m_sectionItem)->setCulled(culled);
139 void ListViewWithPageHeader::ListItem::setSectionItem(QQuickItem *sectionItem)
141 m_sectionItem = sectionItem;
144 ListViewWithPageHeader::ListViewWithPageHeader()
145 : m_delegateModel(nullptr)
146 , m_asyncRequestedIndex(-1)
147 , m_delegateValidated(false)
148 , m_firstVisibleIndex(-1)
150 , m_contentHeightDirty(false)
151 , m_headerItem(nullptr)
152 , m_previousContentY(0)
153 , m_headerItemShownHeight(0)
154 , m_sectionDelegate(nullptr)
155 , m_topSectionItem(nullptr)
156 , m_forceNoClip(false)
158 , m_inContentHeightKeepHeaderShown(false)
161 m_clipItem =
new QQuickItem(contentItem());
165 m_contentYAnimation =
new QQuickNumberAnimation(
this);
166 m_contentYAnimation->setEasing(QEasingCurve::OutQuad);
167 m_contentYAnimation->setProperty(QStringLiteral(
"contentY"));
168 m_contentYAnimation->setDuration(200);
169 m_contentYAnimation->setTargetObject(
this);
171 connect(
this, &ListViewWithPageHeader::contentWidthChanged,
this, &ListViewWithPageHeader::onContentWidthChanged);
172 connect(
this, &ListViewWithPageHeader::contentHeightChanged,
this, &ListViewWithPageHeader::onContentHeightChanged);
173 connect(
this, &ListViewWithPageHeader::heightChanged,
this, &ListViewWithPageHeader::onHeightChanged);
174 connect(m_contentYAnimation, &QQuickNumberAnimation::runningChanged,
this, &ListViewWithPageHeader::contentYAnimationRunningChanged);
176 setFlickableDirection(VerticalFlick);
179 ListViewWithPageHeader::~ListViewWithPageHeader()
183 QAbstractItemModel *ListViewWithPageHeader::model()
const
185 return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() :
nullptr;
188 void ListViewWithPageHeader::setModel(QAbstractItemModel *model)
190 if (model != this->model()) {
191 if (!m_delegateModel) {
192 createDelegateModel();
194 disconnect(m_delegateModel, &QQmlDelegateModel::modelUpdated,
this, &ListViewWithPageHeader::onModelUpdated);
196 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
197 connect(m_delegateModel, &QQmlDelegateModel::modelUpdated,
this, &ListViewWithPageHeader::onModelUpdated);
198 Q_EMIT modelChanged();
206 QQmlComponent *ListViewWithPageHeader::delegate()
const
208 return m_delegateModel ? m_delegateModel->delegate() :
nullptr;
211 void ListViewWithPageHeader::setDelegate(QQmlComponent *delegate)
213 if (delegate != this->delegate()) {
214 if (!m_delegateModel) {
215 createDelegateModel();
219 Q_FOREACH(ListItem *item, m_visibleItems)
221 m_visibleItems.clear();
222 m_firstVisibleIndex = -1;
226 if (m_topSectionItem) {
227 QQuickItemPrivate::get(m_topSectionItem)->setCulled(
true);
230 m_delegateModel->setDelegate(delegate);
232 Q_EMIT delegateChanged();
233 m_delegateValidated =
false;
234 m_contentHeightDirty =
true;
239 QQuickItem *ListViewWithPageHeader::header()
const
244 void ListViewWithPageHeader::setHeader(QQuickItem *headerItem)
246 if (m_headerItem != headerItem) {
247 qreal oldHeaderHeight = 0;
248 qreal oldHeaderY = 0;
250 oldHeaderHeight = m_headerItem->height();
251 oldHeaderY = m_headerItem->y();
252 m_headerItem->setParentItem(
nullptr);
254 m_headerItem = headerItem;
256 m_headerItem->setParentItem(contentItem());
257 m_headerItem->setZ(1);
258 m_previousHeaderImplicitHeight = m_headerItem->implicitHeight();
259 QQuickItemPrivate::get(m_headerItem)->addItemChangeListener(
this, QQuickItemPrivate::ImplicitHeight);
261 qreal newHeaderHeight = m_headerItem ? m_headerItem->height() : 0;
262 if (!m_visibleItems.isEmpty() && newHeaderHeight != oldHeaderHeight) {
263 headerHeightChanged(newHeaderHeight, oldHeaderHeight, oldHeaderY);
265 m_contentHeightDirty =
true;
267 Q_EMIT headerChanged();
271 QQmlComponent *ListViewWithPageHeader::sectionDelegate()
const
273 return m_sectionDelegate;
276 void ListViewWithPageHeader::setSectionDelegate(QQmlComponent *delegate)
278 if (delegate != m_sectionDelegate) {
281 m_sectionDelegate = delegate;
283 m_topSectionItem = getSectionItem(QString());
284 m_topSectionItem->setZ(3);
285 QQuickItemPrivate::get(m_topSectionItem)->setCulled(
true);
286 connect(m_topSectionItem, &QQuickItem::heightChanged,
this, &ListViewWithPageHeader::stickyHeaderHeightChanged);
290 Q_EMIT sectionDelegateChanged();
291 Q_EMIT stickyHeaderHeightChanged();
295 QString ListViewWithPageHeader::sectionProperty()
const
297 return m_sectionProperty;
300 void ListViewWithPageHeader::setSectionProperty(
const QString &property)
302 if (property != m_sectionProperty) {
303 m_sectionProperty = property;
305 updateWatchedRoles();
309 Q_EMIT sectionPropertyChanged();
313 bool ListViewWithPageHeader::forceNoClip()
const
315 return m_forceNoClip;
318 void ListViewWithPageHeader::setForceNoClip(
bool noClip)
320 if (noClip != m_forceNoClip) {
321 m_forceNoClip = noClip;
323 Q_EMIT forceNoClipChanged();
327 int ListViewWithPageHeader::stickyHeaderHeight()
const
329 return m_topSectionItem ? m_topSectionItem->height() : 0;
332 qreal ListViewWithPageHeader::headerItemShownHeight()
const
334 return m_headerItemShownHeight;
337 int ListViewWithPageHeader::cacheBuffer()
const
339 return m_cacheBuffer;
342 void ListViewWithPageHeader::setCacheBuffer(
int cacheBuffer)
344 if (cacheBuffer < 0) {
345 qmlInfo(
this) <<
"Cannot set a negative cache buffer";
349 if (cacheBuffer != m_cacheBuffer) {
350 m_cacheBuffer = cacheBuffer;
351 Q_EMIT cacheBufferChanged();
356 void ListViewWithPageHeader::positionAtBeginning()
358 if (m_delegateModel->count() <= 0)
361 qreal headerHeight = (m_headerItem ? m_headerItem->height() : 0);
362 if (m_firstVisibleIndex != 0) {
366 Q_FOREACH(ListItem *item, m_visibleItems)
368 m_visibleItems.clear();
369 m_firstVisibleIndex = -1;
373 ListItem *item = createItem(0, false);
376 qreal pos = item->y() + item->height();
377 const qreal bufferTo = height() + m_cacheBuffer;
378 while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
379 if (!(item = createItem(modelIndex,
false)))
381 pos += item->height();
385 m_previousContentY = m_visibleItems.first()->y() - headerHeight;
387 setContentY(m_visibleItems.first()->y() + m_clipItem->y() - headerHeight);
393 m_headerItem->setY(-m_minYExtent);
397 static inline bool uFuzzyCompare(qreal r1, qreal r2)
399 return qFuzzyCompare(r1, r2) || (qFuzzyIsNull(r1) && qFuzzyIsNull(r2));
402 void ListViewWithPageHeader::showHeader()
407 const auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
408 if (!uFuzzyCompare(to, contentY())) {
409 const bool headerShownByItsOwn = contentY() < m_headerItem->y() + m_headerItem->height();
410 if (headerShownByItsOwn && m_headerItemShownHeight == 0) {
414 m_headerItemShownHeight = m_headerItem->y() + m_headerItem->height() - contentY();
415 if (!m_visibleItems.isEmpty()) {
417 ListItem *firstItem = m_visibleItems.first();
418 firstItem->setY(firstItem->y() - m_headerItemShownHeight);
421 Q_EMIT headerItemShownHeightChanged();
423 m_contentYAnimation->setTo(to);
424 contentYAnimationType = ContentYAnimationShowHeader;
425 m_contentYAnimation->start();
429 int ListViewWithPageHeader::firstCreatedIndex()
const
431 return m_firstVisibleIndex;
434 int ListViewWithPageHeader::createdItemCount()
const
436 return m_visibleItems.count();
439 QQuickItem *ListViewWithPageHeader::item(
int modelIndex)
const
441 ListItem *item = itemAtIndex(modelIndex);
448 bool ListViewWithPageHeader::maximizeVisibleArea(
int modelIndex)
450 ListItem *listItem = itemAtIndex(modelIndex);
452 return maximizeVisibleArea(listItem, listItem->height());
458 bool ListViewWithPageHeader::maximizeVisibleArea(
int modelIndex,
int itemHeight)
463 ListItem *listItem = itemAtIndex(modelIndex);
465 return maximizeVisibleArea(listItem, itemHeight + (listItem->sectionItem() ? listItem->sectionItem()->height() : 0));
471 bool ListViewWithPageHeader::maximizeVisibleArea(ListItem *listItem,
int listItemHeight)
475 const auto listItemY = m_clipItem->y() + listItem->y();
476 if (listItemY > contentY() && listItemY + listItemHeight > contentY() + height()) {
478 const auto to = qMin(listItemY, listItemY + listItemHeight - height());
479 m_contentYAnimation->setTo(to);
480 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
481 m_contentYAnimation->start();
482 }
else if ((listItemY < contentY() && listItemY + listItemHeight < contentY() + height()) ||
483 (m_topSectionItem && !listItem->sectionItem() && listItemY - m_topSectionItem->height() < contentY() && listItemY + listItemHeight < contentY() + height()))
486 auto realVisibleListItemY = listItemY;
487 if (m_topSectionItem) {
491 bool topSectionShown = !QQuickItemPrivate::get(m_topSectionItem)->culled;
492 if (topSectionShown && !listItem->sectionItem()) {
493 realVisibleListItemY -= m_topSectionItem->height();
496 const auto to = qMax(realVisibleListItemY, listItemY + listItemHeight - height());
497 m_contentYAnimation->setTo(to);
498 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
499 m_contentYAnimation->start();
506 qreal ListViewWithPageHeader::minYExtent()
const
512 void ListViewWithPageHeader::componentComplete()
515 m_delegateModel->componentComplete();
517 QQuickFlickable::componentComplete();
522 void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
527 if (!QQmlEngine::contextForObject(
this)->parentContext())
530 QQuickFlickable::viewportMoved(orient);
532 const qreal diff = m_previousContentY - contentY();
534 m_previousContentY = contentY();
539 void ListViewWithPageHeader::adjustHeader(qreal diff)
541 const bool showHeaderAnimationRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationShowHeader;
543 const auto oldHeaderItemShownHeight = m_headerItemShownHeight;
544 if (uFuzzyCompare(contentY(), -m_minYExtent) || contentY() > -m_minYExtent) {
545 m_headerItem->setHeight(m_headerItem->implicitHeight());
549 const bool scrolledUp = m_previousContentY > contentY();
550 const bool notRebounding = qRound(contentY() + height()) < qRound(contentHeight());
551 const bool notShownByItsOwn = contentY() + diff >= m_headerItem->y() + m_headerItem->height();
552 const bool maximizeVisibleAreaRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationMaximizeVisibleArea;
554 if (!scrolledUp && (contentY() == -m_minYExtent || (m_headerItemShownHeight == 0 && m_previousContentY == m_headerItem->y()))) {
555 m_headerItemShownHeight = 0;
556 m_headerItem->setY(-m_minYExtent);
557 }
else if ((scrolledUp && notRebounding && notShownByItsOwn && !maximizeVisibleAreaRunning) || (m_headerItemShownHeight > 0) || m_inContentHeightKeepHeaderShown) {
558 if (maximizeVisibleAreaRunning && diff > 0) {
560 m_headerItemShownHeight -= diff;
562 m_headerItemShownHeight += diff;
564 if (uFuzzyCompare(contentY(), -m_minYExtent)) {
565 m_headerItemShownHeight = 0;
567 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
569 if (m_headerItemShownHeight > 0) {
570 if (uFuzzyCompare(m_headerItem->height(), m_headerItemShownHeight)) {
571 m_headerItem->setY(contentY());
572 m_headerItemShownHeight = m_headerItem->height();
574 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
577 m_headerItem->setY(-m_minYExtent);
580 Q_EMIT headerItemShownHeightChanged();
583 m_headerItem->setY(contentY());
584 m_headerItem->setHeight(m_headerItem->implicitHeight() + (-m_minYExtent - contentY()));
589 if (!showHeaderAnimationRunning) {
590 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
595 if (!m_visibleItems.isEmpty()) {
597 ListItem *firstItem = m_visibleItems.first();
598 firstItem->setY(firstItem->y() + diff);
599 if (showHeaderAnimationRunning) {
605 void ListViewWithPageHeader::createDelegateModel()
607 m_delegateModel =
new QQmlDelegateModel(qmlContext(
this),
this);
608 connect(m_delegateModel, &QQmlDelegateModel::createdItem,
this, &ListViewWithPageHeader::itemCreated);
609 if (isComponentComplete())
610 m_delegateModel->componentComplete();
611 updateWatchedRoles();
614 void ListViewWithPageHeader::refill()
619 if (!isComponentComplete()) {
623 const qreal from = contentY();
624 const qreal to = from + height();
625 const qreal bufferFrom = from - m_cacheBuffer;
626 const qreal bufferTo = to + m_cacheBuffer;
628 bool added = addVisibleItems(from, to,
false);
629 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
630 added |= addVisibleItems(bufferFrom, bufferTo,
true);
632 if (added || removed) {
633 m_contentHeightDirty =
true;
637 bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo,
bool asynchronous)
642 if (m_delegateModel->count() == 0)
650 if (!m_visibleItems.isEmpty()) {
651 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
652 item = m_visibleItems.last();
653 pos = item->y() + item->height() + m_clipItem->y();
655 bool changed =
false;
657 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
659 if (!(item = createItem(modelIndex, asynchronous)))
661 pos += item->height();
668 if (!m_visibleItems.isEmpty()) {
669 modelIndex = m_firstVisibleIndex - 1;
670 item = m_visibleItems.first();
671 pos = item->y() + m_clipItem->y();
673 while (modelIndex >= 0 && pos > fillFrom) {
675 if (!(item = createItem(modelIndex, asynchronous)))
677 pos -= item->height();
685 void ListViewWithPageHeader::reallyReleaseItem(ListItem *listItem)
687 QQuickItem *item = listItem->m_item;
688 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
689 if (flags & QQmlDelegateModel::Destroyed) {
690 item->setParentItem(
nullptr);
692 listItem->sectionItem()->deleteLater();
696 void ListViewWithPageHeader::releaseItem(ListItem *listItem)
698 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(listItem->m_item);
699 itemPrivate->removeItemChangeListener(
this, QQuickItemPrivate::Geometry);
700 m_itemsToRelease << listItem;
703 void ListViewWithPageHeader::updateWatchedRoles()
705 if (m_delegateModel) {
706 QList<QByteArray> roles;
707 if (!m_sectionProperty.isEmpty())
708 roles << m_sectionProperty.toUtf8();
709 m_delegateModel->setWatchedRoles(roles);
713 QQuickItem *ListViewWithPageHeader::getSectionItem(
int modelIndex,
bool alreadyInserted)
715 if (!m_sectionDelegate)
718 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
720 if (modelIndex > 0) {
721 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
722 if (section == prevSection)
725 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
727 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
728 if (section == nextSection) {
730 ListItem *nextItem = itemAtIndex(modelIndex);
732 QQuickItem *sectionItem = nextItem->sectionItem();
733 nextItem->setSectionItem(
nullptr);
739 return getSectionItem(section);
742 QQuickItem *ListViewWithPageHeader::getSectionItem(
const QString §ionText)
744 QQuickItem *sectionItem =
nullptr;
746 QQmlContext *creationContext = m_sectionDelegate->creationContext();
747 QQmlContext *context =
new QQmlContext(creationContext ? creationContext : qmlContext(
this));
748 context->setContextProperty(QStringLiteral(
"section"), sectionText);
749 context->setContextProperty(QStringLiteral(
"delegateIndex"), -1);
750 QObject *nobj = m_sectionDelegate->beginCreate(context);
752 QQml_setParent_noEvent(context, nobj);
753 sectionItem = qobject_cast<QQuickItem *>(nobj);
757 sectionItem->setZ(2);
758 QQml_setParent_noEvent(sectionItem, m_clipItem);
759 sectionItem->setParentItem(m_clipItem);
764 m_sectionDelegate->completeCreate();
771 bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
776 if (contentY() < -m_minYExtent) {
778 }
else if (contentY() + height() > contentHeight()) {
781 bool changed =
false;
783 bool foundVisible =
false;
785 int removedItems = 0;
786 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
787 while (i < m_visibleItems.count()) {
788 ListItem *item = m_visibleItems[i];
789 const qreal pos = item->y() + m_clipItem->y();
791 if (pos + item->height() < bufferFrom || pos > bufferTo) {
794 m_visibleItems.removeAt(i);
800 const int itemIndex = m_firstVisibleIndex + removedItems + i;
801 m_firstVisibleIndex = itemIndex;
807 m_firstVisibleIndex = -1;
809 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
816 ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(
int modelIndex,
bool asynchronous)
819 if (asynchronous && m_asyncRequestedIndex != -1)
822 m_asyncRequestedIndex = -1;
823 QObject*
object = m_delegateModel->object(modelIndex, asynchronous);
824 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
827 m_delegateModel->release(
object);
828 if (!m_delegateValidated) {
829 m_delegateValidated =
true;
830 QObject* delegateObj = delegate();
831 qmlInfo(delegateObj ? delegateObj :
this) <<
"Delegate must be of Item type";
834 m_asyncRequestedIndex = modelIndex;
839 ListItem *listItem =
new ListItem;
840 listItem->m_item = item;
841 listItem->setSectionItem(getSectionItem(modelIndex,
false ));
842 QQuickItemPrivate::get(item)->addItemChangeListener(
this, QQuickItemPrivate::Geometry);
843 ListItem *prevItem = itemAtIndex(modelIndex - 1);
844 bool lostItem =
false;
848 listItem->setY(prevItem->y() + prevItem->height());
850 ListItem *currItem = itemAtIndex(modelIndex);
853 listItem->setY(currItem->y() - listItem->height());
855 ListItem *nextItem = itemAtIndex(modelIndex + 1);
857 listItem->setY(nextItem->y() - listItem->height());
858 }
else if (modelIndex == 0) {
859 listItem->setY(-m_clipItem->y() + (m_headerItem ? m_headerItem->height() : 0));
860 }
else if (!m_visibleItems.isEmpty()) {
866 listItem->setCulled(
true);
867 releaseItem(listItem);
870 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() <= contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
871 if (m_visibleItems.isEmpty()) {
872 m_visibleItems << listItem;
874 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
876 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
877 m_firstVisibleIndex = modelIndex;
880 if (listItem->sectionItem()) {
881 QQmlContext *context = QQmlEngine::contextForObject(listItem->sectionItem())->parentContext();
882 context->setContextProperty(QStringLiteral(
"delegateIndex"), modelIndex);
885 m_contentHeightDirty =
true;
891 void ListViewWithPageHeader::itemCreated(
int modelIndex, QObject *
object)
893 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
895 qWarning() <<
"ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
902 if (!QQmlEngine::contextForObject(
this)->parentContext())
905 item->setParentItem(m_clipItem);
906 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
907 context->setContextProperty(QStringLiteral(
"ListViewWithPageHeader"),
this);
908 context->setContextProperty(QStringLiteral(
"heightToClip"), QVariant::fromValue<int>(0));
909 if (modelIndex == m_asyncRequestedIndex) {
910 createItem(modelIndex,
false);
915 void ListViewWithPageHeader::updateClipItem()
917 m_clipItem->setHeight(height() - m_headerItemShownHeight);
918 m_clipItem->setY(contentY() + m_headerItemShownHeight);
919 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
922 void ListViewWithPageHeader::onContentHeightChanged()
927 void ListViewWithPageHeader::onContentWidthChanged()
929 m_clipItem->setWidth(contentItem()->width());
932 void ListViewWithPageHeader::onHeightChanged()
938 void ListViewWithPageHeader::onModelUpdated(
const QQmlChangeSet &changeSet,
bool )
942 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
944 Q_FOREACH(
const QQmlChangeSet::Change &
remove, changeSet.removes()) {
946 if (
remove.index +
remove.count > m_firstVisibleIndex &&
remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
947 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
950 bool growDown =
true;
951 for (
int i = 0; growDown && i <
remove.count; ++i) {
952 const int modelIndex =
remove.index + i;
953 ListItem *item = itemAtIndex(modelIndex);
954 if (item && !item->culled()) {
958 for (
int i =
remove.count - 1; i >= 0; --i) {
959 const int visibleIndex =
remove.index + i - m_firstVisibleIndex;
960 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
961 ListItem *item = m_visibleItems[visibleIndex];
963 if (item->sectionItem() && visibleIndex + 1 < m_visibleItems.count()) {
964 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
965 if (!nextItem->sectionItem()) {
966 nextItem->setSectionItem(item->sectionItem());
967 item->setSectionItem(
nullptr);
971 m_visibleItems.removeAt(visibleIndex);
976 }
else if (
remove.index <= m_firstVisibleIndex && !m_visibleItems.isEmpty()) {
977 m_visibleItems.first()->setY(oldFirstValidIndexPos);
979 if (m_visibleItems.isEmpty()) {
980 m_firstVisibleIndex = -1;
982 m_firstVisibleIndex -= qMax(0, m_firstVisibleIndex -
remove.index);
984 }
else if (
remove.index +
remove.count <= m_firstVisibleIndex) {
985 m_firstVisibleIndex -=
remove.count;
987 for (
int i =
remove.count - 1; i >= 0; --i) {
988 const int modelIndex =
remove.index + i;
989 if (modelIndex == m_asyncRequestedIndex) {
990 m_asyncRequestedIndex = -1;
991 }
else if (modelIndex < m_asyncRequestedIndex) {
992 m_asyncRequestedIndex--;
997 Q_FOREACH(
const QQmlChangeSet::Change &insert, changeSet.inserts()) {
999 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
1000 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
1001 if (insertingInValidIndexes || firstItemWithViewOnTop)
1005 bool growUp =
false;
1006 if (!firstItemWithViewOnTop) {
1007 for (
int i = 0; i < m_visibleItems.count(); ++i) {
1008 if (!m_visibleItems[i]->culled()) {
1009 if (insert.index <= m_firstVisibleIndex + i) {
1017 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
1018 for (
int i = insert.count - 1; i >= 0; --i) {
1019 const int modelIndex = insert.index + i;
1020 ListItem *item = createItem(modelIndex,
false);
1022 ListItem *firstItem = m_visibleItems.first();
1023 firstItem->setY(firstItem->y() - item->height());
1027 if (m_sectionDelegate) {
1028 ListItem *nextItem = itemAtIndex(modelIndex + 1);
1029 if (nextItem && !nextItem->sectionItem()) {
1030 nextItem->setSectionItem(getSectionItem(modelIndex + 1,
true ));
1031 if (growUp && nextItem->sectionItem()) {
1032 ListItem *firstItem = m_visibleItems.first();
1033 firstItem->setY(firstItem->y() - nextItem->sectionItem()->height());
1038 if (firstItemWithViewOnTop) {
1039 ListItem *firstItem = m_visibleItems.first();
1040 firstItem->setY(oldFirstValidIndexPos);
1043 }
else if (insert.index <= m_firstVisibleIndex) {
1044 m_firstVisibleIndex += insert.count;
1047 for (
int i = insert.count - 1; i >= 0; --i) {
1048 const int modelIndex = insert.index + i;
1049 if (modelIndex <= m_asyncRequestedIndex) {
1050 m_asyncRequestedIndex++;
1055 Q_FOREACH(
const QQmlChangeSet::Change &change, changeSet.changes()) {
1057 for (
int i = change.index; i < change.count; ++i) {
1058 ListItem *item = itemAtIndex(i);
1059 if (item && item->sectionItem()) {
1060 QQmlContext *context = QQmlEngine::contextForObject(item->sectionItem())->parentContext();
1061 const QString sectionText = m_delegateModel->stringValue(i, m_sectionProperty);
1062 context->setContextProperty(QStringLiteral(
"section"), sectionText);
1067 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
1071 for (
int i = 0; i < m_visibleItems.count(); ++i) {
1072 ListItem *item = m_visibleItems[i];
1073 if (item->sectionItem()) {
1074 QQmlContext *context = QQmlEngine::contextForObject(item->sectionItem())->parentContext();
1075 context->setContextProperty(QStringLiteral(
"delegateIndex"), m_firstVisibleIndex + i);
1081 m_contentHeightDirty =
true;
1084 void ListViewWithPageHeader::contentYAnimationRunningChanged(
bool running)
1086 setInteractive(!running);
1088 m_contentHeightDirty =
true;
1093 void ListViewWithPageHeader::itemGeometryChanged(QQuickItem * ,
const QRectF &newGeometry,
const QRectF &oldGeometry)
1095 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
1096 if (heightDiff != 0) {
1097 if (!m_inContentHeightKeepHeaderShown && oldGeometry.y() + oldGeometry.height() + m_clipItem->y() <= contentY() && !m_visibleItems.isEmpty()) {
1098 ListItem *firstItem = m_visibleItems.first();
1099 firstItem->setY(firstItem->y() - heightDiff);
1106 m_contentHeightDirty =
true;
1110 void ListViewWithPageHeader::itemImplicitHeightChanged(QQuickItem *item)
1112 if (item == m_headerItem) {
1113 const qreal diff = m_headerItem->implicitHeight() - m_previousHeaderImplicitHeight;
1116 m_previousHeaderImplicitHeight = m_headerItem->implicitHeight();
1119 m_contentHeightDirty =
true;
1124 void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
1126 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
1127 if (m_headerItemShownHeight > 0) {
1130 m_headerItemShownHeight += heightDiff;
1131 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
1134 Q_EMIT headerItemShownHeightChanged();
1136 if (oldHeaderY + oldHeaderHeight > contentY()) {
1139 ListItem *firstItem = m_visibleItems.first();
1140 firstItem->setY(firstItem->y() + heightDiff);
1151 void ListViewWithPageHeader::adjustMinYExtent()
1153 if (m_visibleItems.isEmpty()) {
1156 qreal nonCreatedHeight = 0;
1157 if (m_firstVisibleIndex != 0) {
1159 const int visibleItems = m_visibleItems.count();
1160 qreal visibleItemsHeight = 0;
1161 Q_FOREACH(ListItem *item, m_visibleItems) {
1162 visibleItemsHeight += item->height();
1164 nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
1167 const qreal headerHeight = (m_headerItem ? m_headerItem->implicitHeight() : 0);
1168 m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + headerHeight;
1169 if (m_minYExtent != 0 && qFuzzyIsNull(m_minYExtent)) {
1171 m_visibleItems.first()->setY(nonCreatedHeight - m_clipItem->y() + headerHeight);
1176 ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(
int modelIndex)
const
1178 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
1179 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
1180 return m_visibleItems[visibleIndexedModelIndex];
1185 void ListViewWithPageHeader::layout()
1191 if (!m_visibleItems.isEmpty()) {
1192 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
1193 const qreal visibleTo = contentY() + height() - m_clipItem->y();
1195 qreal pos = m_visibleItems.first()->y();
1198 int firstReallyVisibleItem = -1;
1199 int modelIndex = m_firstVisibleIndex;
1200 Q_FOREACH(ListItem *item, m_visibleItems) {
1201 const bool cull = pos + item->height() <= visibleFrom || pos >= visibleTo;
1202 item->setCulled(cull);
1204 if (!cull && firstReallyVisibleItem == -1) {
1205 firstReallyVisibleItem = modelIndex;
1206 if (m_topSectionItem) {
1212 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
1213 bool showStickySectionItem;
1218 if (topSectionStickPos > pos) {
1219 showStickySectionItem =
true;
1220 }
else if (topSectionStickPos == pos) {
1221 showStickySectionItem = !item->sectionItem();
1223 showStickySectionItem =
false;
1225 if (!showStickySectionItem) {
1226 QQuickItemPrivate::get(m_topSectionItem)->setCulled(
true);
1227 if (item->sectionItem()) {
1232 QQuickItemPrivate::get(item->sectionItem())->setCulled(
false);
1236 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1237 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
1238 context->setContextProperty(QStringLiteral(
"section"), section);
1240 QQuickItemPrivate::get(m_topSectionItem)->setCulled(
false);
1241 m_topSectionItem->setY(topSectionStickPos);
1242 int delegateIndex = modelIndex;
1244 while (delegateIndex > 0) {
1245 const QString prevSection = m_delegateModel->stringValue(delegateIndex - 1, m_sectionProperty);
1246 if (prevSection != section)
1250 context->setContextProperty(QStringLiteral(
"delegateIndex"), delegateIndex);
1251 if (item->sectionItem()) {
1252 QQuickItemPrivate::get(item->sectionItem())->setCulled(
true);
1257 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
1258 const qreal clipFrom = visibleFrom + (!item->sectionItem() && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
1259 if (!cull && pos < clipFrom) {
1260 context->setContextProperty(QStringLiteral(
"heightToClip"), clipFrom - pos);
1262 context->setContextProperty(QStringLiteral(
"heightToClip"), QVariant::fromValue<int>(0));
1265 pos += item->height();
1271 if (m_topSectionItem) {
1272 if (firstReallyVisibleItem >= 0) {
1273 for (
int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
1274 ListItem *item = m_visibleItems[i];
1275 if (item->sectionItem()) {
1276 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
1277 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
1288 void ListViewWithPageHeader::updatePolish()
1293 if (!QQmlEngine::contextForObject(
this)->parentContext())
1296 Q_FOREACH(ListItem *item, m_itemsToRelease)
1297 reallyReleaseItem(item);
1298 m_itemsToRelease.clear();
1307 if (m_contentHeightDirty) {
1308 qreal contentHeight;
1309 if (m_visibleItems.isEmpty()) {
1310 contentHeight = m_headerItem ? m_headerItem->height() : 0;
1312 const int modelCount = model()->rowCount();
1313 const int visibleItems = m_visibleItems.count();
1314 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
1315 qreal nonCreatedHeight = 0;
1316 if (lastValidIndex != modelCount - 1) {
1317 const int visibleItems = m_visibleItems.count();
1318 qreal visibleItemsHeight = 0;
1319 Q_FOREACH(ListItem *item, m_visibleItems) {
1320 visibleItemsHeight += item->height();
1322 const int unknownSizes = modelCount - (m_firstVisibleIndex + visibleItems);
1323 nonCreatedHeight = unknownSizes * visibleItemsHeight / visibleItems;
1325 ListItem *item = m_visibleItems.last();
1326 contentHeight = nonCreatedHeight + item->y() + item->height() + m_clipItem->y();
1327 if (m_firstVisibleIndex != 0) {
1329 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
1333 m_contentHeightDirty =
false;
1335 m_inContentHeightKeepHeaderShown = m_headerItem && m_headerItem->y() == contentY();
1336 setContentHeight(contentHeight);
1337 m_inContentHeightKeepHeaderShown =
false;
1341 #include "moc_listviewwithpageheader.cpp"