92 #include "listviewwithpageheader.h"
94 #include <QCoreApplication>
97 #include <qqmlengine.h>
98 #pragma GCC diagnostic push
99 #pragma GCC diagnostic ignored "-pedantic"
100 #if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
101 #include <private/qquickvisualdatamodel_p.h>
103 #include <private/qqmldelegatemodel_p.h>
105 #include <private/qqmlglobal_p.h>
106 #include <private/qquickitem_p.h>
107 #include <private/qquickanimation_p.h>
108 #pragma GCC diagnostic pop
111 static const qreal bufferRatio = 0.5;
113 qreal ListViewWithPageHeader::ListItem::height()
const
115 return m_item->height() + (m_sectionItem ? m_sectionItem->height() : 0);
118 qreal ListViewWithPageHeader::ListItem::y()
const
120 return m_item->y() - (m_sectionItem ? m_sectionItem->height() : 0);
123 void ListViewWithPageHeader::ListItem::setY(qreal newY)
126 m_sectionItem->setY(newY);
127 m_item->setY(newY + m_sectionItem->height());
133 bool ListViewWithPageHeader::ListItem::culled()
const
135 return QQuickItemPrivate::get(m_item)->culled;
138 void ListViewWithPageHeader::ListItem::setCulled(
bool culled)
140 QQuickItemPrivate::get(m_item)->setCulled(culled);
142 QQuickItemPrivate::get(m_sectionItem)->setCulled(culled);
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)
161 m_clipItem =
new QQuickItem(contentItem());
165 m_contentYAnimation =
new QQuickNumberAnimation(
this);
166 m_contentYAnimation->setEasing(QEasingCurve::OutQuad);
167 m_contentYAnimation->setProperty(
"contentY");
168 m_contentYAnimation->setDuration(200);
169 m_contentYAnimation->setTargetObject(
this);
171 connect(
this, SIGNAL(contentWidthChanged()),
this, SLOT(onContentWidthChanged()));
172 connect(
this, SIGNAL(contentHeightChanged()),
this, SLOT(onContentHeightChanged()));
173 connect(
this, SIGNAL(heightChanged()),
this, SLOT(onHeightChanged()));
174 connect(m_contentYAnimation, SIGNAL(stopped()),
this, SLOT(onShowHeaderAnimationFinished()));
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 #if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
195 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,
bool)),
this, SLOT(onModelUpdated(QQuickChangeSet,
bool)));
197 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
198 connect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,
bool)),
this, SLOT(onModelUpdated(QQuickChangeSet,
bool)));
200 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,
bool)),
this, SLOT(onModelUpdated(QQmlChangeSet,
bool)));
202 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
203 connect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,
bool)),
this, SLOT(onModelUpdated(QQmlChangeSet,
bool)));
205 Q_EMIT modelChanged();
213 QQmlComponent *ListViewWithPageHeader::delegate()
const
215 return m_delegateModel ? m_delegateModel->delegate() :
nullptr;
218 void ListViewWithPageHeader::setDelegate(QQmlComponent *delegate)
220 if (delegate != this->delegate()) {
221 if (!m_delegateModel) {
222 createDelegateModel();
226 Q_FOREACH(ListItem *item, m_visibleItems)
228 m_visibleItems.clear();
229 m_firstVisibleIndex = -1;
233 if (m_topSectionItem) {
234 QQuickItemPrivate::get(m_topSectionItem)->setCulled(
true);
237 m_delegateModel->setDelegate(delegate);
239 Q_EMIT delegateChanged();
240 m_delegateValidated =
false;
241 m_contentHeightDirty =
true;
246 QQuickItem *ListViewWithPageHeader::header()
const
251 void ListViewWithPageHeader::setHeader(QQuickItem *headerItem)
253 if (m_headerItem != headerItem) {
254 qreal oldHeaderHeight = 0;
255 qreal oldHeaderY = 0;
257 oldHeaderHeight = m_headerItem->height();
258 oldHeaderY = m_headerItem->y();
259 m_headerItem->setParentItem(
nullptr);
261 m_headerItem = headerItem;
263 m_headerItem->setParentItem(contentItem());
264 m_headerItem->setZ(1);
266 qreal newHeaderHeight = m_headerItem ? m_headerItem->height() : 0;
267 if (!m_visibleItems.isEmpty() && newHeaderHeight != oldHeaderHeight) {
268 headerHeightChanged(newHeaderHeight, oldHeaderHeight, oldHeaderY);
270 m_contentHeightDirty =
true;
272 Q_EMIT headerChanged();
276 QQmlComponent *ListViewWithPageHeader::sectionDelegate()
const
278 return m_sectionDelegate;
281 void ListViewWithPageHeader::setSectionDelegate(QQmlComponent *delegate)
283 if (delegate != m_sectionDelegate) {
286 m_sectionDelegate = delegate;
288 m_topSectionItem = getSectionItem(QString());
289 m_topSectionItem->setZ(3);
290 QQuickItemPrivate::get(m_topSectionItem)->setCulled(
true);
291 connect(m_topSectionItem, SIGNAL(heightChanged()), SIGNAL(stickyHeaderHeightChanged()));
295 Q_EMIT sectionDelegateChanged();
296 Q_EMIT stickyHeaderHeightChanged();
300 QString ListViewWithPageHeader::sectionProperty()
const
302 return m_sectionProperty;
305 void ListViewWithPageHeader::setSectionProperty(
const QString &property)
307 if (property != m_sectionProperty) {
308 m_sectionProperty = property;
310 updateWatchedRoles();
314 Q_EMIT sectionPropertyChanged();
318 bool ListViewWithPageHeader::forceNoClip()
const
320 return m_forceNoClip;
323 void ListViewWithPageHeader::setForceNoClip(
bool noClip)
325 if (noClip != m_forceNoClip) {
326 m_forceNoClip = noClip;
328 Q_EMIT forceNoClipChanged();
332 int ListViewWithPageHeader::stickyHeaderHeight()
const
334 return m_topSectionItem ? m_topSectionItem->height() : 0;
337 void ListViewWithPageHeader::positionAtBeginning()
339 if (m_delegateModel->count() <= 0)
342 qreal headerHeight = (m_headerItem ? m_headerItem->height() : 0);
343 if (m_firstVisibleIndex != 0) {
347 Q_FOREACH(ListItem *item, m_visibleItems)
349 m_visibleItems.clear();
350 m_firstVisibleIndex = -1;
354 ListItem *item = createItem(0, false);
357 qreal pos = item->y() + item->height();
358 const qreal buffer = height() * bufferRatio;
359 const qreal bufferTo = height() + buffer;
360 while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
361 if (!(item = createItem(modelIndex,
false)))
363 pos += item->height();
367 m_previousContentY = m_visibleItems.first()->y() - headerHeight;
369 setContentY(m_visibleItems.first()->y() + m_clipItem->y() - headerHeight);
375 m_headerItem->setY(-m_minYExtent);
379 static inline bool uFuzzyCompare(qreal r1, qreal r2)
381 return qFuzzyCompare(r1, r2) || (qFuzzyIsNull(r1) && qFuzzyIsNull(r2));
384 void ListViewWithPageHeader::showHeader()
389 const auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
390 if (!uFuzzyCompare(to, contentY())) {
391 const bool headerShownByItsOwn = contentY() < m_headerItem->y() + m_headerItem->height();
392 if (headerShownByItsOwn && m_headerItemShownHeight == 0) {
396 m_headerItemShownHeight = m_headerItem->y() + m_headerItem->height() - contentY();
397 if (!m_visibleItems.isEmpty()) {
399 ListItem *firstItem = m_visibleItems.first();
400 firstItem->setY(firstItem->y() - m_headerItemShownHeight);
404 m_contentYAnimation->setTo(to);
405 contentYAnimationType = ContentYAnimationShowHeader;
406 m_contentYAnimation->start();
410 QQuickItem *ListViewWithPageHeader::item(
int modelIndex)
const
412 ListItem *item = itemAtIndex(modelIndex);
419 bool ListViewWithPageHeader::maximizeVisibleArea(
int modelIndex)
421 ListItem *listItem = itemAtIndex(modelIndex);
423 return maximizeVisibleArea(listItem, listItem->height());
429 bool ListViewWithPageHeader::maximizeVisibleArea(
int modelIndex,
int itemHeight)
434 ListItem *listItem = itemAtIndex(modelIndex);
436 return maximizeVisibleArea(listItem, itemHeight + (listItem->m_sectionItem ? listItem->m_sectionItem->height() : 0));
442 bool ListViewWithPageHeader::maximizeVisibleArea(ListItem *listItem,
int listItemHeight)
446 const auto listItemY = m_clipItem->y() + listItem->y();
447 if (listItemY > contentY() && listItemY + listItemHeight > contentY() + height()) {
449 const auto to = qMin(listItemY, listItemY + listItemHeight - height());
450 m_contentYAnimation->setTo(to);
451 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
452 m_contentYAnimation->start();
453 }
else if ((listItemY < contentY() && listItemY + listItemHeight < contentY() + height()) ||
454 (m_topSectionItem && !listItem->m_sectionItem && listItemY - m_topSectionItem->height() < contentY() && listItemY + listItemHeight < contentY() + height()))
457 auto realVisibleListItemY = listItemY;
458 if (m_topSectionItem) {
462 bool topSectionShown = !QQuickItemPrivate::get(m_topSectionItem)->culled;
463 if (topSectionShown && !listItem->m_sectionItem) {
464 realVisibleListItemY -= m_topSectionItem->height();
467 const auto to = qMax(realVisibleListItemY, listItemY + listItemHeight - height());
468 m_contentYAnimation->setTo(to);
469 contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
470 m_contentYAnimation->start();
477 qreal ListViewWithPageHeader::minYExtent()
const
483 void ListViewWithPageHeader::componentComplete()
486 m_delegateModel->componentComplete();
488 QQuickFlickable::componentComplete();
493 void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
498 if (!QQmlEngine::contextForObject(
this)->parentContext())
501 QQuickFlickable::viewportMoved(orient);
503 qreal diff = m_previousContentY - contentY();
504 const bool showHeaderAnimationRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationShowHeader;
506 const auto oldHeaderItemShownHeight = m_headerItemShownHeight;
507 if (uFuzzyCompare(contentY(), -m_minYExtent) || contentY() > -m_minYExtent) {
508 m_headerItem->setHeight(m_headerItem->implicitHeight());
512 const bool scrolledUp = m_previousContentY > contentY();
513 const bool notRebounding = contentY() + height() < contentHeight();
514 const bool notShownByItsOwn = contentY() + diff >= m_headerItem->y() + m_headerItem->height();
515 const bool maximizeVisibleAreaRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationMaximizeVisibleArea;
517 if (!scrolledUp && (contentY() == -m_minYExtent || (m_headerItemShownHeight == 0 && m_previousContentY == m_headerItem->y()))) {
518 m_headerItemShownHeight = 0;
519 m_headerItem->setY(-m_minYExtent);
520 }
else if ((scrolledUp && notRebounding && notShownByItsOwn && !maximizeVisibleAreaRunning) || (m_headerItemShownHeight > 0) || m_inContentHeightKeepHeaderShown) {
521 if (maximizeVisibleAreaRunning && diff > 0) {
523 m_headerItemShownHeight -= diff;
525 m_headerItemShownHeight += diff;
527 if (uFuzzyCompare(contentY(), -m_minYExtent)) {
528 m_headerItemShownHeight = 0;
530 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
532 if (m_headerItemShownHeight > 0) {
533 if (uFuzzyCompare(m_headerItem->height(), m_headerItemShownHeight)) {
534 m_headerItem->setY(contentY());
535 m_headerItemShownHeight = m_headerItem->height();
537 m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
540 m_headerItem->setY(-m_minYExtent);
545 m_headerItem->setY(contentY());
546 m_headerItem->setHeight(m_headerItem->implicitHeight() + (-m_minYExtent - contentY()));
551 if (!showHeaderAnimationRunning) {
552 diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
557 if (!m_visibleItems.isEmpty()) {
559 ListItem *firstItem = m_visibleItems.first();
560 firstItem->setY(firstItem->y() + diff);
561 if (showHeaderAnimationRunning) {
566 m_previousContentY = contentY();
571 void ListViewWithPageHeader::createDelegateModel()
573 #if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
574 m_delegateModel =
new QQuickVisualDataModel(qmlContext(
this),
this);
575 connect(m_delegateModel, SIGNAL(createdItem(
int,QQuickItem*)),
this, SLOT(itemCreated(
int,QQuickItem*)));
577 m_delegateModel =
new QQmlDelegateModel(qmlContext(
this),
this);
578 connect(m_delegateModel, SIGNAL(createdItem(
int,QObject*)),
this, SLOT(itemCreated(
int,QObject*)));
580 if (isComponentComplete())
581 m_delegateModel->componentComplete();
582 updateWatchedRoles();
585 void ListViewWithPageHeader::refill()
590 if (!isComponentComplete()) {
594 const qreal buffer = height() * bufferRatio;
595 const qreal from = contentY();
596 const qreal to = from + height();
597 const qreal bufferFrom = from - buffer;
598 const qreal bufferTo = to + buffer;
600 bool added = addVisibleItems(from, to,
false);
601 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
602 added |= addVisibleItems(bufferFrom, bufferTo,
true);
604 if (added || removed) {
605 m_contentHeightDirty =
true;
609 bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo,
bool asynchronous)
614 if (m_delegateModel->count() == 0)
622 if (!m_visibleItems.isEmpty()) {
623 modelIndex = m_firstVisibleIndex + m_visibleItems.count();
624 item = m_visibleItems.last();
625 pos = item->y() + item->height() + m_clipItem->y();
627 bool changed =
false;
629 while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
631 if (!(item = createItem(modelIndex, asynchronous)))
633 pos += item->height();
640 if (!m_visibleItems.isEmpty()) {
641 modelIndex = m_firstVisibleIndex - 1;
642 item = m_visibleItems.first();
643 pos = item->y() + m_clipItem->y();
645 while (modelIndex >= 0 && pos > fillFrom) {
647 if (!(item = createItem(modelIndex, asynchronous)))
649 pos -= item->height();
657 void ListViewWithPageHeader::reallyReleaseItem(ListItem *listItem)
659 QQuickItem *item = listItem->m_item;
660 #if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
661 QQuickVisualModel::ReleaseFlags flags = m_delegateModel->release(item);
662 if (flags & QQuickVisualModel::Destroyed) {
664 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
665 if (flags & QQmlDelegateModel::Destroyed) {
667 item->setParentItem(
nullptr);
669 delete listItem->m_sectionItem;
673 void ListViewWithPageHeader::releaseItem(ListItem *listItem)
675 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(listItem->m_item);
676 itemPrivate->removeItemChangeListener(
this, QQuickItemPrivate::Geometry);
677 m_itemsToRelease << listItem;
680 void ListViewWithPageHeader::updateWatchedRoles()
682 if (m_delegateModel) {
683 QList<QByteArray> roles;
684 if (!m_sectionProperty.isEmpty())
685 roles << m_sectionProperty.toUtf8();
686 m_delegateModel->setWatchedRoles(roles);
690 QQuickItem *ListViewWithPageHeader::getSectionItem(
int modelIndex,
bool alreadyInserted)
692 if (!m_sectionDelegate)
695 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
696 if (modelIndex > 0) {
697 const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
698 if (section == prevSection)
701 if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
703 const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
704 if (section == nextSection) {
706 ListItem *nextItem = itemAtIndex(modelIndex);
708 QQuickItem *sectionItem = nextItem->m_sectionItem;
709 nextItem->m_sectionItem =
nullptr;
715 return getSectionItem(section);
718 QQuickItem *ListViewWithPageHeader::getSectionItem(
const QString §ionText)
720 QQuickItem *sectionItem =
nullptr;
722 QQmlContext *creationContext = m_sectionDelegate->creationContext();
723 QQmlContext *context =
new QQmlContext(creationContext ? creationContext : qmlContext(
this));
724 context->setContextProperty(QLatin1String(
"section"), sectionText);
725 context->setContextProperty(QLatin1String(
"delegateIndex"), -1);
726 QObject *nobj = m_sectionDelegate->beginCreate(context);
728 QQml_setParent_noEvent(context, nobj);
729 sectionItem = qobject_cast<QQuickItem *>(nobj);
733 sectionItem->setZ(2);
734 QQml_setParent_noEvent(sectionItem, m_clipItem);
735 sectionItem->setParentItem(m_clipItem);
740 m_sectionDelegate->completeCreate();
747 bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
752 if (contentY() < -m_minYExtent) {
754 }
else if (contentY() + height() > contentHeight()) {
757 bool changed =
false;
759 bool foundVisible =
false;
761 int removedItems = 0;
762 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
763 while (i < m_visibleItems.count()) {
764 ListItem *item = m_visibleItems[i];
765 const qreal pos = item->y() + m_clipItem->y();
767 if (pos + item->height() < bufferFrom || pos > bufferTo) {
770 m_visibleItems.removeAt(i);
776 const int itemIndex = m_firstVisibleIndex + removedItems + i;
777 m_firstVisibleIndex = itemIndex;
783 m_firstVisibleIndex = -1;
785 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
792 ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(
int modelIndex,
bool asynchronous)
795 if (asynchronous && m_asyncRequestedIndex != -1)
798 m_asyncRequestedIndex = -1;
799 #if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
800 QQuickItem *item = m_delegateModel->item(modelIndex, asynchronous);
802 QObject*
object = m_delegateModel->object(modelIndex, asynchronous);
803 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
806 #if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
807 m_asyncRequestedIndex = modelIndex;
810 m_delegateModel->release(
object);
811 if (!m_delegateValidated) {
812 m_delegateValidated =
true;
813 QObject* delegateObj = delegate();
814 qmlInfo(delegateObj ? delegateObj :
this) <<
"Delegate must be of Item type";
817 m_asyncRequestedIndex = modelIndex;
823 ListItem *listItem =
new ListItem;
824 listItem->m_item = item;
825 listItem->m_sectionItem = getSectionItem(modelIndex,
false );
826 QQuickItemPrivate::get(item)->addItemChangeListener(
this, QQuickItemPrivate::Geometry);
827 ListItem *prevItem = itemAtIndex(modelIndex - 1);
828 bool lostItem =
false;
832 listItem->setY(prevItem->y() + prevItem->height());
834 ListItem *currItem = itemAtIndex(modelIndex);
837 listItem->setY(currItem->y() - listItem->height());
839 ListItem *nextItem = itemAtIndex(modelIndex + 1);
841 listItem->setY(nextItem->y() - listItem->height());
842 }
else if (modelIndex == 0 && m_headerItem) {
843 listItem->setY(m_headerItem->height());
844 }
else if (!m_visibleItems.isEmpty()) {
850 listItem->setCulled(
true);
851 releaseItem(listItem);
854 listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() <= contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
855 if (m_visibleItems.isEmpty()) {
856 m_visibleItems << listItem;
858 m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
860 if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
861 m_firstVisibleIndex = modelIndex;
864 if (listItem->m_sectionItem) {
865 QQmlContext *context = QQmlEngine::contextForObject(listItem->m_sectionItem)->parentContext();
866 context->setContextProperty(QLatin1String(
"delegateIndex"), modelIndex);
869 m_contentHeightDirty =
true;
875 #if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
876 void ListViewWithPageHeader::itemCreated(
int modelIndex, QQuickItem *item)
879 void ListViewWithPageHeader::itemCreated(
int modelIndex, QObject *
object)
881 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
883 qWarning() <<
"ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
891 if (!QQmlEngine::contextForObject(
this)->parentContext())
894 item->setParentItem(m_clipItem);
895 QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
896 context->setContextProperty(QLatin1String(
"ListViewWithPageHeader"),
this);
897 context->setContextProperty(QLatin1String(
"heightToClip"), QVariant::fromValue<int>(0));
898 if (modelIndex == m_asyncRequestedIndex) {
899 createItem(modelIndex,
false);
904 void ListViewWithPageHeader::updateClipItem()
906 m_clipItem->setHeight(height() - m_headerItemShownHeight);
907 m_clipItem->setY(contentY() + m_headerItemShownHeight);
908 m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
911 void ListViewWithPageHeader::onContentHeightChanged()
916 void ListViewWithPageHeader::onContentWidthChanged()
918 m_clipItem->setWidth(contentItem()->width());
921 void ListViewWithPageHeader::onHeightChanged()
927 #if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
928 void ListViewWithPageHeader::onModelUpdated(
const QQuickChangeSet &changeSet,
bool )
930 void ListViewWithPageHeader::onModelUpdated(
const QQmlChangeSet &changeSet,
bool )
935 const auto oldFirstVisibleIndex = m_firstVisibleIndex;
937 #if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
938 Q_FOREACH(
const QQuickChangeSet::Remove &
remove, changeSet.removes()) {
940 Q_FOREACH(
const QQmlChangeSet::Remove &
remove, changeSet.removes()) {
943 if (
remove.index +
remove.count > m_firstVisibleIndex &&
remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
944 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
947 bool growDown =
true;
948 for (
int i = 0; growDown && i <
remove.count; ++i) {
949 const int modelIndex =
remove.index + i;
950 ListItem *item = itemAtIndex(modelIndex);
951 if (item && !item->culled()) {
955 for (
int i =
remove.count - 1; i >= 0; --i) {
956 const int visibleIndex =
remove.index + i - m_firstVisibleIndex;
957 if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
958 ListItem *item = m_visibleItems[visibleIndex];
960 if (item->m_sectionItem && visibleIndex + 1 < m_visibleItems.count()) {
961 ListItem *nextItem = m_visibleItems[visibleIndex + 1];
962 if (!nextItem->m_sectionItem) {
963 nextItem->m_sectionItem = item->m_sectionItem;
964 item->m_sectionItem =
nullptr;
968 m_visibleItems.removeAt(visibleIndex);
973 }
else if (
remove.index <= m_firstVisibleIndex) {
974 if (!m_visibleItems.isEmpty()) {
977 m_visibleItems.first()->setY(oldFirstValidIndexPos);
979 m_firstVisibleIndex = -1;
982 }
else if (
remove.index +
remove.count <= m_firstVisibleIndex) {
983 m_firstVisibleIndex -=
remove.count;
985 for (
int i =
remove.count - 1; i >= 0; --i) {
986 const int modelIndex =
remove.index + i;
987 if (modelIndex == m_asyncRequestedIndex) {
988 m_asyncRequestedIndex = -1;
989 }
else if (modelIndex < m_asyncRequestedIndex) {
990 m_asyncRequestedIndex--;
995 #if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
996 Q_FOREACH(
const QQuickChangeSet::Insert &insert, changeSet.inserts()) {
998 Q_FOREACH(
const QQmlChangeSet::Insert &insert, changeSet.inserts()) {
1001 const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
1002 const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
1003 if (insertingInValidIndexes || firstItemWithViewOnTop)
1007 bool growUp =
false;
1008 if (!firstItemWithViewOnTop) {
1009 for (
int i = 0; i < m_visibleItems.count(); ++i) {
1010 if (!m_visibleItems[i]->culled()) {
1011 if (insert.index <= m_firstVisibleIndex + i) {
1019 const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
1020 for (
int i = insert.count - 1; i >= 0; --i) {
1021 const int modelIndex = insert.index + i;
1022 ListItem *item = createItem(modelIndex,
false);
1024 ListItem *firstItem = m_visibleItems.first();
1025 firstItem->setY(firstItem->y() - item->height());
1029 if (m_sectionDelegate) {
1030 ListItem *nextItem = itemAtIndex(modelIndex + 1);
1031 if (nextItem && !nextItem->m_sectionItem) {
1032 nextItem->m_sectionItem = getSectionItem(modelIndex + 1,
true );
1033 if (growUp && nextItem->m_sectionItem) {
1034 ListItem *firstItem = m_visibleItems.first();
1035 firstItem->setY(firstItem->y() - nextItem->m_sectionItem->height());
1040 if (firstItemWithViewOnTop) {
1041 ListItem *firstItem = m_visibleItems.first();
1042 firstItem->setY(oldFirstValidIndexPos);
1045 }
else if (insert.index <= m_firstVisibleIndex) {
1046 m_firstVisibleIndex += insert.count;
1049 for (
int i = insert.count - 1; i >= 0; --i) {
1050 const int modelIndex = insert.index + i;
1051 if (modelIndex <= m_asyncRequestedIndex) {
1052 m_asyncRequestedIndex++;
1057 #if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
1058 Q_FOREACH(
const QQuickChangeSet::Change &change, changeSet.changes()) {
1060 Q_FOREACH(
const QQmlChangeSet::Change &change, changeSet.changes()) {
1063 for (
int i = change.index; i < change.count; ++i) {
1064 ListItem *item = itemAtIndex(i);
1065 if (item && item->m_sectionItem) {
1066 QQmlContext *context = QQmlEngine::contextForObject(item->m_sectionItem)->parentContext();
1067 const QString sectionText = m_delegateModel->stringValue(i, m_sectionProperty);
1068 context->setContextProperty(QLatin1String(
"section"), sectionText);
1073 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
1077 for (
int i = 0; i < m_visibleItems.count(); ++i) {
1078 ListItem *item = m_visibleItems[i];
1079 if (item->m_sectionItem) {
1080 QQmlContext *context = QQmlEngine::contextForObject(item->m_sectionItem)->parentContext();
1081 context->setContextProperty(QLatin1String(
"delegateIndex"), m_firstVisibleIndex + i);
1087 m_contentHeightDirty =
true;
1090 void ListViewWithPageHeader::onShowHeaderAnimationFinished()
1092 m_contentHeightDirty =
true;
1096 void ListViewWithPageHeader::itemGeometryChanged(QQuickItem * ,
const QRectF &newGeometry,
const QRectF &oldGeometry)
1098 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
1099 if (heightDiff != 0) {
1100 if (!m_inContentHeightKeepHeaderShown && oldGeometry.y() + oldGeometry.height() + m_clipItem->y() <= contentY() && !m_visibleItems.isEmpty()) {
1101 ListItem *firstItem = m_visibleItems.first();
1102 firstItem->setY(firstItem->y() - heightDiff);
1109 m_contentHeightDirty =
true;
1113 void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
1115 const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
1116 if (m_headerItemShownHeight > 0) {
1119 m_headerItemShownHeight += heightDiff;
1120 m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
1124 if (oldHeaderY + oldHeaderHeight > contentY()) {
1127 ListItem *firstItem = m_visibleItems.first();
1128 firstItem->setY(firstItem->y() + heightDiff);
1139 void ListViewWithPageHeader::adjustMinYExtent()
1141 if (m_visibleItems.isEmpty()) {
1144 qreal nonCreatedHeight = 0;
1145 if (m_firstVisibleIndex != 0) {
1147 const int visibleItems = m_visibleItems.count();
1148 qreal visibleItemsHeight = 0;
1149 Q_FOREACH(ListItem *item, m_visibleItems) {
1150 visibleItemsHeight += item->height();
1152 nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
1155 const qreal headerHeight = (m_headerItem ? m_headerItem->implicitHeight() : 0);
1156 m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + headerHeight;
1157 if (m_minYExtent != 0 && qFuzzyIsNull(m_minYExtent)) {
1159 m_visibleItems.first()->setY(nonCreatedHeight - m_clipItem->y() + headerHeight);
1164 ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(
int modelIndex)
const
1166 const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
1167 if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
1168 return m_visibleItems[visibleIndexedModelIndex];
1173 void ListViewWithPageHeader::layout()
1179 if (!m_visibleItems.isEmpty()) {
1180 const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
1181 const qreal visibleTo = contentY() + height() - m_clipItem->y();
1183 qreal pos = m_visibleItems.first()->y();
1186 int firstReallyVisibleItem = -1;
1187 int modelIndex = m_firstVisibleIndex;
1188 Q_FOREACH(ListItem *item, m_visibleItems) {
1189 const bool cull = pos + item->height() <= visibleFrom || pos >= visibleTo;
1190 item->setCulled(cull);
1192 if (!cull && firstReallyVisibleItem == -1) {
1193 firstReallyVisibleItem = modelIndex;
1194 if (m_topSectionItem) {
1200 const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
1201 bool showStickySectionItem;
1206 if (topSectionStickPos > pos) {
1207 showStickySectionItem =
true;
1208 }
else if (topSectionStickPos == pos) {
1209 showStickySectionItem = !item->m_sectionItem;
1211 showStickySectionItem =
false;
1213 if (!showStickySectionItem) {
1214 QQuickItemPrivate::get(m_topSectionItem)->setCulled(
true);
1215 if (item->m_sectionItem) {
1220 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(
false);
1224 const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
1225 QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
1226 context->setContextProperty(QLatin1String(
"section"), section);
1228 QQuickItemPrivate::get(m_topSectionItem)->setCulled(
false);
1229 m_topSectionItem->setY(topSectionStickPos);
1230 int delegateIndex = modelIndex;
1232 while (delegateIndex > 0) {
1233 const QString prevSection = m_delegateModel->stringValue(delegateIndex - 1, m_sectionProperty);
1234 if (prevSection != section)
1238 context->setContextProperty(QLatin1String(
"delegateIndex"), delegateIndex);
1239 if (item->m_sectionItem) {
1240 QQuickItemPrivate::get(item->m_sectionItem)->setCulled(
true);
1245 QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
1246 const qreal clipFrom = visibleFrom + (!item->m_sectionItem && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
1247 if (!cull && pos < clipFrom) {
1248 context->setContextProperty(QLatin1String(
"heightToClip"), clipFrom - pos);
1250 context->setContextProperty(QLatin1String(
"heightToClip"), QVariant::fromValue<int>(0));
1253 pos += item->height();
1259 if (m_topSectionItem) {
1260 if (firstReallyVisibleItem >= 0) {
1261 for (
int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
1262 ListItem *item = m_visibleItems[i];
1263 if (item->m_sectionItem) {
1264 if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
1265 m_topSectionItem->setY(item->y() - m_topSectionItem->height());
1276 void ListViewWithPageHeader::updatePolish()
1281 if (!QQmlEngine::contextForObject(
this)->parentContext())
1284 Q_FOREACH(ListItem *item, m_itemsToRelease)
1285 reallyReleaseItem(item);
1286 m_itemsToRelease.clear();
1295 if (m_contentHeightDirty) {
1296 qreal contentHeight;
1297 if (m_visibleItems.isEmpty()) {
1298 contentHeight = m_headerItem ? m_headerItem->height() : 0;
1300 const int modelCount = model()->rowCount();
1301 const int visibleItems = m_visibleItems.count();
1302 const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
1303 qreal nonCreatedHeight = 0;
1304 if (lastValidIndex != modelCount - 1) {
1305 const int visibleItems = m_visibleItems.count();
1306 qreal visibleItemsHeight = 0;
1307 Q_FOREACH(ListItem *item, m_visibleItems) {
1308 visibleItemsHeight += item->height();
1310 const int unknownSizes = modelCount - (m_firstVisibleIndex + visibleItems);
1311 nonCreatedHeight = unknownSizes * visibleItemsHeight / visibleItems;
1313 ListItem *item = m_visibleItems.last();
1314 contentHeight = nonCreatedHeight + item->y() + item->height() + m_clipItem->y();
1315 if (m_firstVisibleIndex != 0) {
1317 m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
1321 m_contentHeightDirty =
false;
1323 m_inContentHeightKeepHeaderShown = m_headerItem && m_headerItem->y() == contentY();
1324 setContentHeight(contentHeight);
1325 m_inContentHeightKeepHeaderShown =
false;
1329 #include "moc_listviewwithpageheader.cpp"