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/qqmlcontext_p.h> 101 #include <private/qqmldelegatemodel_p.h> 102 #include <private/qqmlglobal_p.h> 103 #include <private/qquickitem_p.h> 104 #include <private/qquickanimation_p.h> 105 #pragma GCC diagnostic pop 108 qreal ListViewWithPageHeader::ListItem::height()
const 110 return m_item->height() + (m_sectionItem ? m_sectionItem->height() : 0);
113 qreal ListViewWithPageHeader::ListItem::y()
const 115 return m_item->y() - (m_sectionItem ? m_sectionItem->height() : 0);
118 void ListViewWithPageHeader::ListItem::setY(qreal newY)
121 m_sectionItem->setY(newY);
122 m_item->setY(newY + m_sectionItem->height());
128 bool ListViewWithPageHeader::ListItem::culled()
const 130 return QQuickItemPrivate::get(m_item)->culled;
133 void ListViewWithPageHeader::ListItem::setCulled(
bool culled)
135 QQuickItemPrivate::get(m_item)->setCulled(culled);
137 QQuickItemPrivate::get(m_sectionItem)->setCulled(culled);
140 void ListViewWithPageHeader::ListItem::setSectionItem(QQuickItem *sectionItem)
142 m_sectionItem = sectionItem;
145 ListViewWithPageHeader::ListViewWithPageHeader()
146 : m_delegateModel(nullptr)
147 , m_asyncRequestedIndex(-1)
148 , m_delegateValidated(false)
149 , m_firstVisibleIndex(-1)
151 , m_contentHeightDirty(false)
152 , m_headerItem(nullptr)
153 , m_previousContentY(0)
154 , m_headerItemShownHeight(0)
155 , m_sectionDelegate(nullptr)
156 , m_topSectionItem(nullptr)
157 , m_forceNoClip(false)
159 , m_inContentHeightKeepHeaderShown(false)
162 m_clipItem =
new QQuickItem(contentItem());
166 m_contentYAnimation =
new QQuickNumberAnimation(
this);
167 m_contentYAnimation->setEasing(QEasingCurve::OutQuad);
168 m_contentYAnimation->setProperty(QStringLiteral(
"contentY"));
169 m_contentYAnimation->setDuration(200);
170 m_contentYAnimation->setTargetObject(
this);
172 connect(contentItem(), &QQuickItem::widthChanged,
this, &ListViewWithPageHeader::onContentWidthChanged);
173 connect(
this, &ListViewWithPageHeader::contentHeightChanged,
this, &ListViewWithPageHeader::onContentHeightChanged);
174 connect(
this, &ListViewWithPageHeader::heightChanged,
this, &ListViewWithPageHeader::onHeightChanged);
175 connect(m_contentYAnimation, &QQuickNumberAnimation::runningChanged,
this, &ListViewWithPageHeader::contentYAnimationRunningChanged);
177 setFlickableDirection(VerticalFlick);
180 ListViewWithPageHeader::~ListViewWithPageHeader()
184 QAbstractItemModel *ListViewWithPageHeader::model()
const 186 return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() :
nullptr;
189 void ListViewWithPageHeader::setModel(QAbstractItemModel *model)
191 if (model != this->model()) {
192 if (!m_delegateModel) {
193 createDelegateModel();
195 disconnect(m_delegateModel, &QQmlDelegateModel::modelUpdated,
this, &ListViewWithPageHeader::onModelUpdated);
197 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
198 connect(m_delegateModel, &QQmlDelegateModel::modelUpdated,
this, &ListViewWithPageHeader::onModelUpdated);
199 Q_EMIT modelChanged();
207 QQmlComponent *ListViewWithPageHeader::delegate()
const 209 return m_delegateModel ? m_delegateModel->delegate() :
nullptr;
212 void ListViewWithPageHeader::setDelegate(QQmlComponent *delegate)
214 if (delegate != this->delegate()) {
215 if (!m_delegateModel) {
216 createDelegateModel();
220 Q_FOREACH(ListItem *item, m_visibleItems)
222 m_visibleItems.clear();
223 initializeValuesForEmptyList();
225 m_delegateModel->setDelegate(delegate);
227 Q_EMIT delegateChanged();
228 m_delegateValidated = false;
229 m_contentHeightDirty = true;
236 m_firstVisibleIndex = -1;
240 if (m_topSectionItem) {
241 QQuickItemPrivate::get(m_topSectionItem)->setCulled(
true);
245 QQuickItem *ListViewWithPageHeader::header()
const 250 void ListViewWithPageHeader::setHeader(QQuickItem *headerItem)
252 if (m_headerItem != headerItem) {
253 qreal oldHeaderHeight = 0;
254 qreal oldHeaderY = 0;
256 oldHeaderHeight = m_headerItem->height();
257 oldHeaderY = m_headerItem->y();
258 m_headerItem->setParentItem(
nullptr);
260 m_headerItem = headerItem;
262 m_headerItem->setParentItem(contentItem());
263 m_headerItem->setZ(1);
264 m_previousHeaderImplicitHeight = m_headerItem->implicitHeight();
265 QQuickItemPrivate::get(m_headerItem)->addItemChangeListener(
this, QQuickItemPrivate::ImplicitHeight);
267 qreal newHeaderHeight = m_headerItem ? m_headerItem->height() : 0;
268 if (!m_visibleItems.isEmpty() && newHeaderHeight != oldHeaderHeight) {
269 headerHeightChanged(newHeaderHeight, oldHeaderHeight, oldHeaderY);
271 m_contentHeightDirty =
true;
273 Q_EMIT headerChanged();
277 QQmlComponent *ListViewWithPageHeader::sectionDelegate()
const 279 return m_sectionDelegate;
282 void ListViewWithPageHeader::setSectionDelegate(QQmlComponent *delegate)
284 if (delegate != m_sectionDelegate) {
287 m_sectionDelegate = delegate;
289 m_topSectionItem = getSectionItem(QString(),
false );
290 m_topSectionItem->setZ(3);
291 QQuickItemPrivate::get(m_topSectionItem)->setCulled(
true);
292 connect(m_topSectionItem, &QQuickItem::heightChanged,
this, &ListViewWithPageHeader::stickyHeaderHeightChanged);
296 Q_EMIT sectionDelegateChanged();
297 Q_EMIT stickyHeaderHeightChanged();
301 QString ListViewWithPageHeader::sectionProperty()
const 303 return m_sectionProperty;
306 void ListViewWithPageHeader::setSectionProperty(
const QString &property)
308 if (property != m_sectionProperty) {
309 m_sectionProperty = property;
311 updateWatchedRoles();
315 Q_EMIT sectionPropertyChanged();
319 bool ListViewWithPageHeader::forceNoClip()
const 321 return m_forceNoClip;
324 void ListViewWithPageHeader::setForceNoClip(
bool noClip)
326 if (noClip != m_forceNoClip) {
327 m_forceNoClip = noClip;
329 Q_EMIT forceNoClipChanged();
333 int ListViewWithPageHeader::stickyHeaderHeight()
const 335 return m_topSectionItem ? m_topSectionItem->height() : 0;
338 qreal ListViewWithPageHeader::headerItemShownHeight()
const 340 return m_headerItemShownHeight;
343 int ListViewWithPageHeader::cacheBuffer()
const 345 return m_cacheBuffer;
348 void ListViewWithPageHeader::setCacheBuffer(
int cacheBuffer)
350 if (cacheBuffer < 0) {
351 qmlInfo(
this) <<
"Cannot set a negative cache buffer";
355 if (cacheBuffer != m_cacheBuffer) {
356 m_cacheBuffer = cacheBuffer;
357 Q_EMIT cacheBufferChanged();
362 void ListViewWithPageHeader::positionAtBeginning()
364 if (m_delegateModel->count() <= 0)
367 qreal headerHeight = (m_headerItem ? m_headerItem->height() : 0);
368 if (m_firstVisibleIndex != 0) {
372 Q_FOREACH(ListItem *item, m_visibleItems)
374 m_visibleItems.clear();
375 m_firstVisibleIndex = -1;
379 ListItem *item = createItem(0, false);
382 qreal pos = item->y() + item->height();
383 const qreal bufferTo = height() + m_cacheBuffer;
384 while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
385 if (!(item = createItem(modelIndex,
false)))
387 pos += item->height();
391 m_previousContentY = m_visibleItems.first()->y() - headerHeight;
393 setContentY(m_visibleItems.first()->y() + m_clipItem->y() - headerHeight);
399 m_headerItem->setY(-m_minYExtent);
403 static inline bool uFuzzyCompare(qreal r1, qreal r2)
405 return qFuzzyCompare(r1, r2) || (qFuzzyIsNull(r1) && qFuzzyIsNull(r2));
408 void ListViewWithPageHeader::showHeader()
413 const auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
414 if (!uFuzzyCompare(to, contentY())) {
415 const bool headerShownByItsOwn = contentY() < m_headerItem->y() + m_headerItem->height();
416 if (headerShownByItsOwn && m_headerItemShownHeight == 0) {
420 m_headerItemShownHeight = m_headerItem->y() + m_headerItem->height() - contentY();
421 if (!m_visibleItems.isEmpty()) {
423 ListItem *firstItem = m_visibleItems.first();
424 firstItem->setY(firstItem->y() - m_headerItemShownHeight);
427 Q_EMIT headerItemShownHeightChanged();
429 m_contentYAnimation->setTo(to);
430 contentYAnimationType = ContentYAnimationShowHeader;
431 m_contentYAnimation->start();
435 int ListViewWithPageHeader::firstCreatedIndex()
const 437 return m_firstVisibleIndex;
440 int ListViewWithPageHeader::createdItemCount()
const 442 return m_visibleItems.count();
445 QQuickItem *ListViewWithPageHeader::item(
int modelIndex)
const 447 ListItem *item = itemAtIndex(modelIndex);
454 bool ListViewWithPageHeader::maximizeVisibleArea(
int modelIndex)
456 ListItem *listItem = itemAtIndex(modelIndex);
458 return maximizeVisibleArea(listItem, listItem->height());
464 bool ListViewWithPageHeader::maximizeVisibleArea(
int modelIndex,
int itemHeight)
469 ListItem *listItem = itemAtIndex(modelIndex);
471 return maximizeVisibleArea(listItem, itemHeight + (listItem->sectionItem() ? listItem->sectionItem()->height() : 0));
477 bool ListViewWithPageHeader::maximizeVisibleArea(ListItem *listItem,
int listItemHeight)
481 const auto listItemY = m_clipItem->y() + listItem->y();
482 if (listItemY > contentY() && listItemY + listItemHeight > contentY() + height()) {
484 const auto to = qMin(listItemY, listItemY + listItemHeight - height());
485 m_contentYAnimation->setTo(to);
486 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
487 m_contentYAnimation->start();
488 }
else if ((listItemY < contentY() && listItemY + listItemHeight < contentY() + height()) ||
489 (m_topSectionItem && !listItem->sectionItem() && listItemY - m_topSectionItem->height() < contentY() && listItemY + listItemHeight < contentY() + height()))
492 auto realVisibleListItemY = listItemY;
493 if (m_topSectionItem) {
497 bool topSectionShown = !QQuickItemPrivate::get(m_topSectionItem)->culled;
498 if (topSectionShown && !listItem->sectionItem()) {
499 realVisibleListItemY -= m_topSectionItem->height();
502 const auto to = qMax(realVisibleListItemY, listItemY + listItemHeight - height());
503 m_contentYAnimation->setTo(to);
504 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
505 m_contentYAnimation->start();
512 qreal ListViewWithPageHeader::minYExtent()
const 518 qreal ListViewWithPageHeader::maxYExtent()
const 520 return height() - contentHeight();
523 void ListViewWithPageHeader::componentComplete()
526 m_delegateModel->componentComplete();
528 QQuickFlickable::componentComplete();
533 void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
538 if (!QQmlEngine::contextForObject(
this)->parentContext())
541 QQuickFlickable::viewportMoved(orient);
543 const qreal diff = m_previousContentY - contentY();
545 m_previousContentY = contentY();
550 void ListViewWithPageHeader::adjustHeader(qreal diff)
552 const bool showHeaderAnimationRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationShowHeader;
554 const auto oldHeaderItemShownHeight = m_headerItemShownHeight;
555 if (uFuzzyCompare(contentY(), -m_minYExtent) || contentY() > -m_minYExtent) {
556 m_headerItem->setHeight(m_headerItem->implicitHeight());
560 const bool scrolledUp = m_previousContentY > contentY();
561 const bool notRebounding = qRound(contentY() + height()) < qRound(contentHeight());
562 const bool notShownByItsOwn = contentY() + diff >= m_headerItem->y() + m_headerItem->height();
563 const bool maximizeVisibleAreaRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationMaximizeVisibleArea;
565 if (!scrolledUp && (contentY() == -m_minYExtent || (m_headerItemShownHeight == 0 && m_previousContentY == m_headerItem->y()))) {
566 m_headerItemShownHeight = 0;
567 m_headerItem->setY(-m_minYExtent);
568 }
else if ((scrolledUp && notRebounding && notShownByItsOwn && !maximizeVisibleAreaRunning) || (m_headerItemShownHeight > 0) || m_inContentHeightKeepHeaderShown) {
569 if (maximizeVisibleAreaRunning && diff > 0) {
571 m_headerItemShownHeight -= diff;
573 m_headerItemShownHeight += diff;
575 if (uFuzzyCompare(contentY(), -m_minYExtent)) {
576 m_headerItemShownHeight = 0;
578 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
580 if (m_headerItemShownHeight > 0) {
581 if (uFuzzyCompare(m_headerItem->height(), m_headerItemShownHeight)) {
582 m_headerItem->setY(contentY());
583 m_headerItemShownHeight = m_headerItem->height();
585 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
588 m_headerItem->setY(-m_minYExtent);
591 Q_EMIT headerItemShownHeightChanged();
594 m_headerItem->setY(contentY());
595 m_headerItem->setHeight(m_headerItem->implicitHeight() + (-m_minYExtent - contentY()));
600 if (!showHeaderAnimationRunning) {
601 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
606 if (!m_visibleItems.isEmpty()) {
608 ListItem *firstItem = m_visibleItems.first();
609 firstItem->setY(firstItem->y() + diff);
610 if (showHeaderAnimationRunning) {
616 void ListViewWithPageHeader::createDelegateModel()
618 m_delegateModel =
new QQmlDelegateModel(qmlContext(
this),
this);
619 connect(m_delegateModel, &QQmlDelegateModel::createdItem,
this, &ListViewWithPageHeader::itemCreated);
620 if (isComponentComplete())
621 m_delegateModel->componentComplete();
622 updateWatchedRoles();
625 void ListViewWithPageHeader::refill()
630 if (!isComponentComplete()) {
634 const qreal from = contentY();
635 const qreal to = from + height();
636 const qreal bufferFrom = from - m_cacheBuffer;
637 const qreal bufferTo = to + m_cacheBuffer;
639 bool added = addVisibleItems(from, to,
false);
640 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
641 added |= addVisibleItems(bufferFrom, bufferTo,
true);
643 if (added || removed) {
644 m_contentHeightDirty =
true;
648 bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo,
bool asynchronous)
653 if (m_delegateModel->count() == 0)
661 if (!m_visibleItems.isEmpty()) {
662 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
663 item = m_visibleItems.last();
664 pos = item->y() + item->height() + m_clipItem->y();
666 bool changed =
false;
668 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
670 if (!(item = createItem(modelIndex, asynchronous)))
672 pos += item->height();
679 if (!m_visibleItems.isEmpty()) {
680 modelIndex = m_firstVisibleIndex - 1;
681 item = m_visibleItems.first();
682 pos = item->y() + m_clipItem->y();
684 while (modelIndex >= 0 && pos > fillFrom) {
686 if (!(item = createItem(modelIndex, asynchronous)))
688 pos -= item->height();
696 void ListViewWithPageHeader::reallyReleaseItem(ListItem *listItem)
698 QQuickItem *item = listItem->m_item;
699 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
700 if (flags & QQmlDelegateModel::Destroyed) {
701 item->setParentItem(
nullptr);
703 listItem->sectionItem()->deleteLater();
707 void ListViewWithPageHeader::releaseItem(ListItem *listItem)
709 QQuickItemPrivate::get(listItem->m_item)->removeItemChangeListener(
this, QQuickItemPrivate::Geometry);
710 if (listItem->sectionItem()) {
711 QQuickItemPrivate::get(listItem->sectionItem())->removeItemChangeListener(
this, QQuickItemPrivate::Geometry);
713 m_itemsToRelease << listItem;
716 void ListViewWithPageHeader::updateWatchedRoles()
718 if (m_delegateModel) {
719 QList<QByteArray> roles;
720 if (!m_sectionProperty.isEmpty())
721 roles << m_sectionProperty.toUtf8();
722 m_delegateModel->setWatchedRoles(roles);
726 QQuickItem *ListViewWithPageHeader::getSectionItem(
int modelIndex,
bool alreadyInserted)
728 if (!m_sectionDelegate)
731 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
733 if (modelIndex > 0) {
734 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
735 if (section == prevSection)
738 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
740 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
741 if (section == nextSection) {
743 ListItem *nextItem = itemAtIndex(modelIndex);
745 QQuickItem *sectionItem = nextItem->sectionItem();
746 nextItem->setSectionItem(
nullptr);
752 return getSectionItem(section);
755 QQuickItem *ListViewWithPageHeader::getSectionItem(
const QString §ionText,
bool watchGeometry)
757 QQuickItem *sectionItem =
nullptr;
759 QQmlContext *creationContext = m_sectionDelegate->creationContext();
760 QQmlContext *context =
new QQmlContext(creationContext ? creationContext : qmlContext(
this));
761 QObject *nobj = m_sectionDelegate->beginCreate(context);
763 QQml_setParent_noEvent(context, nobj);
764 sectionItem = qobject_cast<QQuickItem *>(nobj);
768 sectionItem->setProperty(
"text", sectionText);
769 sectionItem->setProperty(
"delegateIndex", -1);
770 sectionItem->setZ(2);
771 QQml_setParent_noEvent(sectionItem, m_clipItem);
772 sectionItem->setParentItem(m_clipItem);
777 m_sectionDelegate->completeCreate();
780 QQuickItemPrivate::get(sectionItem)->addItemChangeListener(
this, QQuickItemPrivate::Geometry);
786 void ListViewWithPageHeader::updateSectionItem(
int modelIndex)
788 ListItem *item = itemAtIndex(modelIndex);
790 const QString sectionText = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
792 bool needSectionHeader =
true;
794 if (modelIndex > 0) {
795 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
796 if (sectionText == prevSection) {
797 needSectionHeader =
false;
801 if (needSectionHeader) {
802 if (!item->sectionItem()) {
803 item->setSectionItem(getSectionItem(sectionText));
805 item->sectionItem()->setProperty(
"text", sectionText);
808 if (item->sectionItem()) {
809 item->sectionItem()->deleteLater();
810 item->setSectionItem(
nullptr);
816 bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
821 if (contentY() < -m_minYExtent) {
823 }
else if (contentY() + height() > contentHeight()) {
826 bool changed =
false;
828 bool foundVisible =
false;
830 int removedItems = 0;
831 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
832 while (i < m_visibleItems.count()) {
833 ListItem *item = m_visibleItems[i];
834 const qreal pos = item->y() + m_clipItem->y();
836 if (pos + item->height() < bufferFrom || pos > bufferTo) {
839 m_visibleItems.removeAt(i);
845 const int itemIndex = m_firstVisibleIndex + removedItems + i;
846 m_firstVisibleIndex = itemIndex;
852 initializeValuesForEmptyList();
854 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
861 ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(
int modelIndex,
bool asynchronous)
864 if (asynchronous && m_asyncRequestedIndex != -1)
867 m_asyncRequestedIndex = -1;
868 QObject*
object = m_delegateModel->object(modelIndex, asynchronous);
869 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
872 m_delegateModel->release(
object);
873 if (!m_delegateValidated) {
874 m_delegateValidated =
true;
875 QObject* delegateObj = delegate();
876 qmlInfo(delegateObj ? delegateObj :
this) <<
"Delegate must be of Item type";
879 m_asyncRequestedIndex = modelIndex;
884 ListItem *listItem =
new ListItem;
885 listItem->m_item = item;
886 listItem->setSectionItem(getSectionItem(modelIndex,
false ));
887 QQuickItemPrivate::get(item)->addItemChangeListener(
this, QQuickItemPrivate::Geometry);
888 ListItem *prevItem = itemAtIndex(modelIndex - 1);
889 bool lostItem =
false;
893 listItem->setY(prevItem->y() + prevItem->height());
895 ListItem *currItem = itemAtIndex(modelIndex);
898 listItem->setY(currItem->y() - listItem->height());
900 ListItem *nextItem = itemAtIndex(modelIndex + 1);
902 listItem->setY(nextItem->y() - listItem->height());
903 }
else if (modelIndex == 0) {
904 listItem->setY(-m_clipItem->y() + (m_headerItem ? m_headerItem->height() : 0));
905 }
else if (!m_visibleItems.isEmpty()) {
911 listItem->setCulled(
true);
912 releaseItem(listItem);
915 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() <= contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
916 if (m_visibleItems.isEmpty()) {
917 m_visibleItems << listItem;
919 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
921 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
922 m_firstVisibleIndex = modelIndex;
925 if (listItem->sectionItem()) {
926 listItem->sectionItem()->setProperty(
"delegateIndex", modelIndex);
929 m_contentHeightDirty =
true;
935 void ListViewWithPageHeader::itemCreated(
int modelIndex, QObject *
object)
937 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
939 qWarning() <<
"ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
946 if (!QQmlEngine::contextForObject(
this)->parentContext())
949 item->setParentItem(m_clipItem);
951 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
952 QQmlContextPrivate::get(context)->data->refreshExpressions();
953 item->setProperty(
"heightToClip", QVariant::fromValue<int>(0));
954 if (modelIndex == m_asyncRequestedIndex) {
955 createItem(modelIndex,
false);
960 void ListViewWithPageHeader::updateClipItem()
962 m_clipItem->setHeight(height() - m_headerItemShownHeight);
963 m_clipItem->setY(contentY() + m_headerItemShownHeight);
964 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
967 void ListViewWithPageHeader::onContentHeightChanged()
972 void ListViewWithPageHeader::onContentWidthChanged()
974 m_clipItem->setWidth(contentItem()->width());
977 void ListViewWithPageHeader::onHeightChanged()
983 void ListViewWithPageHeader::onModelUpdated(
const QQmlChangeSet &changeSet,
bool )
987 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
989 Q_FOREACH(
const QQmlChangeSet::Change
remove, changeSet.removes()) {
991 if (
remove.index +
remove.count > m_firstVisibleIndex &&
remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
992 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
995 bool growDown =
true;
996 for (
int i = 0; growDown && i <
remove.count; ++i) {
997 const int modelIndex =
remove.index + i;
998 ListItem *item = itemAtIndex(modelIndex);
999 if (item && !item->culled()) {
1003 for (
int i =
remove.count - 1; i >= 0; --i) {
1004 const int visibleIndex =
remove.index + i - m_firstVisibleIndex;
1005 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
1006 ListItem *item = m_visibleItems[visibleIndex];
1008 if (item->sectionItem() && visibleIndex + 1 < m_visibleItems.count()) {
1009 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
1010 if (!nextItem->sectionItem()) {
1011 nextItem->setSectionItem(item->sectionItem());
1012 item->setSectionItem(
nullptr);
1016 m_visibleItems.removeAt(visibleIndex);
1021 }
else if (
remove.index <= m_firstVisibleIndex && !m_visibleItems.isEmpty()) {
1022 m_visibleItems.first()->setY(oldFirstValidIndexPos);
1024 if (m_visibleItems.isEmpty()) {
1025 m_firstVisibleIndex = -1;
1027 m_firstVisibleIndex -= qMax(0, m_firstVisibleIndex -
remove.index);
1029 }
else if (
remove.index +
remove.count <= m_firstVisibleIndex) {
1030 m_firstVisibleIndex -=
remove.count;
1032 for (
int i =
remove.count - 1; i >= 0; --i) {
1033 const int modelIndex =
remove.index + i;
1034 if (modelIndex == m_asyncRequestedIndex) {
1035 m_asyncRequestedIndex = -1;
1036 }
else if (modelIndex < m_asyncRequestedIndex) {
1037 m_asyncRequestedIndex--;
1042 Q_FOREACH(
const QQmlChangeSet::Change insert, changeSet.inserts()) {
1044 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
1045 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
1046 if (insertingInValidIndexes || firstItemWithViewOnTop)
1050 bool growUp =
false;
1051 if (!firstItemWithViewOnTop) {
1052 for (
int i = 0; i < m_visibleItems.count(); ++i) {
1053 if (!m_visibleItems[i]->culled()) {
1054 if (insert.index <= m_firstVisibleIndex + i) {
1062 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
1063 for (
int i = insert.count - 1; i >= 0; --i) {
1064 const int modelIndex = insert.index + i;
1065 ListItem *item = createItem(modelIndex,
false);
1067 ListItem *firstItem = m_visibleItems.first();
1068 firstItem->setY(firstItem->y() - item->height());
1072 if (m_sectionDelegate) {
1073 ListItem *nextItem = itemAtIndex(modelIndex + 1);
1074 if (nextItem && !nextItem->sectionItem()) {
1075 nextItem->setSectionItem(getSectionItem(modelIndex + 1,
true ));
1076 if (growUp && nextItem->sectionItem()) {
1077 ListItem *firstItem = m_visibleItems.first();
1078 firstItem->setY(firstItem->y() - nextItem->sectionItem()->height());
1083 if (firstItemWithViewOnTop) {
1084 ListItem *firstItem = m_visibleItems.first();
1085 firstItem->setY(oldFirstValidIndexPos);
1088 }
else if (insert.index <= m_firstVisibleIndex) {
1089 m_firstVisibleIndex += insert.count;
1092 for (
int i = insert.count - 1; i >= 0; --i) {
1093 const int modelIndex = insert.index + i;
1094 if (modelIndex <= m_asyncRequestedIndex) {
1095 m_asyncRequestedIndex++;
1100 Q_FOREACH(
const QQmlChangeSet::Change change, changeSet.changes()) {
1101 for (
int i = change.start(); i < change.end(); ++i) {
1102 updateSectionItem(i);
1105 updateSectionItem(change.end());
1108 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
1109 if (m_visibleItems.isEmpty()) {
1110 initializeValuesForEmptyList();
1116 for (
int i = 0; i < m_visibleItems.count(); ++i) {
1117 ListItem *item = m_visibleItems[i];
1118 if (item->sectionItem()) {
1119 item->sectionItem()->setProperty(
"delegateIndex", m_firstVisibleIndex + i);
1125 m_contentHeightDirty =
true;
1128 void ListViewWithPageHeader::contentYAnimationRunningChanged(
bool running)
1130 setInteractive(!running);
1132 m_contentHeightDirty =
true;
1137 void ListViewWithPageHeader::itemGeometryChanged(QQuickItem *item,
const QRectF &newGeometry,
const QRectF &oldGeometry)
1139 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
1140 if (heightDiff != 0) {
1141 if (!m_visibleItems.isEmpty()) {
1142 ListItem *firstItem = m_visibleItems.first();
1143 const auto prevFirstItemY = firstItem->y();
1144 if (!m_inContentHeightKeepHeaderShown && oldGeometry.y() + oldGeometry.height() + m_clipItem->y() <= contentY()) {
1145 firstItem->setY(firstItem->y() - heightDiff);
1146 }
else if (item == firstItem->sectionItem()) {
1147 firstItem->setY(firstItem->y() + heightDiff);
1150 if (firstItem->y() != prevFirstItemY) {
1158 m_contentHeightDirty =
true;
1162 void ListViewWithPageHeader::itemImplicitHeightChanged(QQuickItem *item)
1164 if (item == m_headerItem) {
1165 const qreal diff = m_headerItem->implicitHeight() - m_previousHeaderImplicitHeight;
1168 m_previousHeaderImplicitHeight = m_headerItem->implicitHeight();
1171 m_contentHeightDirty =
true;
1176 void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
1178 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
1179 if (m_headerItemShownHeight > 0) {
1182 m_headerItemShownHeight += heightDiff;
1183 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
1186 Q_EMIT headerItemShownHeightChanged();
1188 if (oldHeaderY + oldHeaderHeight > contentY()) {
1191 ListItem *firstItem = m_visibleItems.first();
1192 firstItem->setY(firstItem->y() + heightDiff);
1203 void ListViewWithPageHeader::adjustMinYExtent()
1205 if (m_visibleItems.isEmpty() || (contentHeight() + m_minYExtent < height())) {
1208 qreal nonCreatedHeight = 0;
1209 if (m_firstVisibleIndex != 0) {
1211 const int visibleItems = m_visibleItems.count();
1212 qreal visibleItemsHeight = 0;
1213 Q_FOREACH(ListItem *item, m_visibleItems) {
1214 visibleItemsHeight += item->height();
1216 nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
1219 const qreal headerHeight = (m_headerItem ? m_headerItem->implicitHeight() : 0);
1220 m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + headerHeight;
1221 if (m_minYExtent != 0 && qFuzzyIsNull(m_minYExtent)) {
1223 m_visibleItems.first()->setY(nonCreatedHeight - m_clipItem->y() + headerHeight);
1228 ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(
int modelIndex)
const 1230 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
1231 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
1232 return m_visibleItems[visibleIndexedModelIndex];
1237 void ListViewWithPageHeader::layout()
1243 if (!m_visibleItems.isEmpty()) {
1244 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
1245 const qreal visibleTo = contentY() + height() - m_clipItem->y();
1247 qreal pos = m_visibleItems.first()->y();
1250 int firstReallyVisibleItem = -1;
1251 int modelIndex = m_firstVisibleIndex;
1252 Q_FOREACH(ListItem *item, m_visibleItems) {
1253 const bool cull = pos + item->height() <= visibleFrom || pos >= visibleTo;
1254 item->setCulled(cull);
1256 if (!cull && firstReallyVisibleItem == -1) {
1257 firstReallyVisibleItem = modelIndex;
1258 if (m_topSectionItem) {
1264 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
1265 bool showStickySectionItem;
1270 if (topSectionStickPos > pos) {
1271 showStickySectionItem =
true;
1272 }
else if (topSectionStickPos == pos) {
1273 showStickySectionItem = !item->sectionItem();
1275 showStickySectionItem =
false;
1277 if (!showStickySectionItem) {
1278 QQuickItemPrivate::get(m_topSectionItem)->setCulled(
true);
1279 if (item->sectionItem()) {
1284 QQuickItemPrivate::get(item->sectionItem())->setCulled(
false);
1288 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1289 m_topSectionItem->setProperty(
"text", section);
1291 QQuickItemPrivate::get(m_topSectionItem)->setCulled(
false);
1292 m_topSectionItem->setY(topSectionStickPos);
1293 int delegateIndex = modelIndex;
1295 while (delegateIndex > 0) {
1296 const QString prevSection = m_delegateModel->stringValue(delegateIndex - 1, m_sectionProperty);
1297 if (prevSection != section)
1301 m_topSectionItem->setProperty(
"delegateIndex", delegateIndex);
1302 if (item->sectionItem()) {
1303 QQuickItemPrivate::get(item->sectionItem())->setCulled(
true);
1308 const qreal clipFrom = visibleFrom + (!item->sectionItem() && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
1309 if (!cull && pos < clipFrom) {
1310 item->m_item->setProperty(
"heightToClip", clipFrom - pos);
1312 item->m_item->setProperty(
"heightToClip", QVariant::fromValue<int>(0));
1315 pos += item->height();
1321 if (m_topSectionItem) {
1322 if (firstReallyVisibleItem >= 0) {
1323 for (
int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
1324 ListItem *item = m_visibleItems[i];
1325 if (item->sectionItem()) {
1326 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
1327 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
1338 void ListViewWithPageHeader::updatePolish()
1343 if (!QQmlEngine::contextForObject(
this)->parentContext())
1346 Q_FOREACH(ListItem *item, m_itemsToRelease)
1347 reallyReleaseItem(item);
1348 m_itemsToRelease.clear();
1357 if (m_contentHeightDirty) {
1358 qreal contentHeight;
1359 if (m_visibleItems.isEmpty()) {
1360 contentHeight = m_headerItem ? m_headerItem->height() : 0;
1362 const int modelCount = model()->rowCount();
1363 const int visibleItems = m_visibleItems.count();
1364 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
1365 qreal nonCreatedHeight = 0;
1366 if (lastValidIndex != modelCount - 1) {
1367 const int visibleItems = m_visibleItems.count();
1368 qreal visibleItemsHeight = 0;
1369 Q_FOREACH(ListItem *item, m_visibleItems) {
1370 visibleItemsHeight += item->height();
1372 const int unknownSizes = modelCount - (m_firstVisibleIndex + visibleItems);
1373 nonCreatedHeight = unknownSizes * visibleItemsHeight / visibleItems;
1375 ListItem *item = m_visibleItems.last();
1376 contentHeight = nonCreatedHeight + item->y() + item->height() + m_clipItem->y();
1377 if (m_firstVisibleIndex != 0) {
1379 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
1383 m_contentHeightDirty =
false;
1385 if (contentHeight + m_minYExtent < height()) {
1389 m_inContentHeightKeepHeaderShown = m_headerItem && m_headerItem->y() == contentY();
1390 setContentHeight(contentHeight);
1391 m_inContentHeightKeepHeaderShown =
false;
1395 #include "moc_listviewwithpageheader.cpp"