on xenial help sign in
Take into account that sectionItem height can change

Approved by: Josh Arenson

commit0ed2df34710ed33a0f87992c8ddf39c4ac970ffc authorAlbert Astals Cid <albert.astals@canonical.com> dateWed 27 Apr 2016, 14:59 (10 years ago) parentb7c493a61a1a94
4 files changed+39−12
plugins/Dash/listviewwithpageheader.cpp+22−10 plugins/Dash/listviewwithpageheader.h+1−1 tests/plugins/Dash/listviewwithpageheadersectiontest.cpp+15 tests/plugins/Dash/listviewwithpageheadertestsection.qml+1−1
plugins/Dash/listviewwithpageheader.cpp+22−10
@@ -285,7 +285,7 @@ void ListViewWithPageHeader::setSectionDelegate(QQmlComponent *delegate)
285285
286286 m_sectionDelegate = delegate;
287287
288− m_topSectionItem = getSectionItem(QString());
288+ m_topSectionItem = getSectionItem(QString(), false /*watchGeometry*/);
289289 m_topSectionItem->setZ(3);
290290 QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
291291 connect(m_topSectionItem, &QQuickItem::heightChanged, this, &ListViewWithPageHeader::stickyHeaderHeightChanged);
@@ -705,8 +705,10 @@ void ListViewWithPageHeader::reallyReleaseItem(ListItem *listItem)
705705
706706void ListViewWithPageHeader::releaseItem(ListItem *listItem)
707707{
708− QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(listItem->m_item);
709− itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
708+ QQuickItemPrivate::get(listItem->m_item)->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
709+ if (listItem->sectionItem()) {
710+ QQuickItemPrivate::get(listItem->sectionItem())->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
711+ }
710712 m_itemsToRelease << listItem;
711713}
712714
@@ -749,7 +751,7 @@ QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyI
749751 return getSectionItem(section);
750752}
751753
752−QQuickItem *ListViewWithPageHeader::getSectionItem(const QString &sectionText)
754+QQuickItem *ListViewWithPageHeader::getSectionItem(const QString &sectionText, bool watchGeometry)
753755{
754756 QQuickItem *sectionItem = nullptr;
755757
@@ -773,7 +775,9 @@ QQuickItem *ListViewWithPageHeader::getSectionItem(const QString &sectionText)
773775 }
774776 m_sectionDelegate->completeCreate();
775777
776− // TODO attach to sectionItem so we can accomodate to it changing its height
778+ if (watchGeometry) {
779+ QQuickItemPrivate::get(sectionItem)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
780+ }
777781
778782 return sectionItem;
779783}
@@ -1131,15 +1135,23 @@ void ListViewWithPageHeader::contentYAnimationRunningChanged(bool running)
11311135 }
11321136}
11331137
1134−void ListViewWithPageHeader::itemGeometryChanged(QQuickItem * /*item*/, const QRectF &newGeometry, const QRectF &oldGeometry)
1138+void ListViewWithPageHeader::itemGeometryChanged(QQuickItem *item, const QRectF &newGeometry, const QRectF &oldGeometry)
11351139{
11361140 const qreal heightDiff = newGeometry.height() - oldGeometry.height();
11371141 if (heightDiff != 0) {
1138− if (!m_inContentHeightKeepHeaderShown && oldGeometry.y() + oldGeometry.height() + m_clipItem->y() <= contentY() && !m_visibleItems.isEmpty()) {
1142+ if (!m_visibleItems.isEmpty()) {
11391143 ListItem *firstItem = m_visibleItems.first();
1140− firstItem->setY(firstItem->y() - heightDiff);
1141− adjustMinYExtent();
1142− layout();
1144+ const auto prevFirstItemY = firstItem->y();
1145+ if (!m_inContentHeightKeepHeaderShown && oldGeometry.y() + oldGeometry.height() + m_clipItem->y() <= contentY()) {
1146+ firstItem->setY(firstItem->y() - heightDiff);
1147+ } else if (item == firstItem->sectionItem()) {
1148+ firstItem->setY(firstItem->y() + heightDiff);
1149+ }
1150+
1151+ if (firstItem->y() != prevFirstItemY) {
1152+ adjustMinYExtent();
1153+ layout();
1154+ }
11431155 }
11441156 refill();
11451157 adjustMinYExtent();
plugins/Dash/listviewwithpageheader.h+1−1
@@ -167,7 +167,7 @@ private:
167167 void reallyReleaseItem(ListItem *item);
168168 void updateWatchedRoles();
169169 QQuickItem *getSectionItem(int modelIndex, bool alreadyInserted);
170− QQuickItem *getSectionItem(const QString &sectionText);
170+ QQuickItem *getSectionItem(const QString &sectionText, bool watchGeometry = true);
171171 void updateSectionItem(int modelIndex);
172172 void initializeValuesForEmptyList();
173173
tests/plugins/Dash/listviewwithpageheadersectiontest.cpp+15
@@ -2180,6 +2180,21 @@ private Q_SLOTS:
21802180 verifyItem(2, 480., 390., false, "Agressive", false);
21812181 }
21822182
2183+ void firstItemSectionHeightChange()
2184+ {
2185+ QMetaObject::invokeMethod(model, "removeItems", Q_ARG(QVariant, 1), Q_ARG(QVariant, 5));
2186+ model->setProperty(0, "type", "halfheight");
2187+ verifyItem(0, 50., 170., false, "halfheight", false);
2188+ }
2189+
2190+ void secondItemSectionHeightChange()
2191+ {
2192+ QMetaObject::invokeMethod(model, "removeItems", Q_ARG(QVariant, 2), Q_ARG(QVariant, 4));
2193+ model->setProperty(1, "type", "halfheight");
2194+ verifyItem(0, 50., 190., false, "Agressive", false);
2195+ verifyItem(1, 240., 220., false, "halfheight", false);
2196+ }
2197+
21832198private:
21842199 QQuickView *view;
21852200 ListViewWithPageHeader *lvwph;
tests/plugins/Dash/listviewwithpageheadertestsection.qml+1−1
@@ -98,7 +98,7 @@ Rectangle {
9898 sectionDelegate: Component {
9999 Rectangle {
100100 color: "green"
101− height: 40
101+ height: section === "" ? 0 : section != "halfheight" ? 40 : 20;
102102 Text { text: section; font.pixelSize: 34 }
103103 anchors { left: parent.left; right: parent.right }
104104 }