on xenial help sign in
LVWPH: Process correctly section changes Fixes: #1519893

Approved by: Michael Zanetti

commit1e594bb7024e61c75f93ee5542953f2eab3b4a0b authorAlbert Astals Cid <albert.astals@canonical.com> dateTue 8 Dec 2015, 15:37 (11 years ago) parent67aa7982f05716
3 files changed+67−8
plugins/Dash/listviewwithpageheader.cpp+35−8 plugins/Dash/listviewwithpageheader.h+1 tests/plugins/Dash/listviewwithpageheadersectiontest.cpp+31
plugins/Dash/listviewwithpageheader.cpp+35−8
@@ -768,6 +768,37 @@ QQuickItem *ListViewWithPageHeader::getSectionItem(const QString &sectionText)
768768 return sectionItem;
769769}
770770
771+void ListViewWithPageHeader::updateSectionItem(int modelIndex)
772+{
773+ ListItem *item = itemAtIndex(modelIndex);
774+ if (item) {
775+ const QString sectionText = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
776+
777+ bool needSectionHeader = true;
778+ // if it is the same section as the previous item need to drop the section
779+ if (modelIndex > 0) {
780+ const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
781+ if (sectionText == prevSection) {
782+ needSectionHeader = false;
783+ }
784+ }
785+
786+ if (needSectionHeader) {
787+ if (!item->sectionItem()) {
788+ item->setSectionItem(getSectionItem(sectionText));
789+ } else {
790+ QQmlContext *context = QQmlEngine::contextForObject(item->sectionItem())->parentContext();
791+ context->setContextProperty(QStringLiteral("section"), sectionText);
792+ }
793+ } else {
794+ if (item->sectionItem()) {
795+ item->sectionItem()->deleteLater();
796+ item->setSectionItem(nullptr);
797+ }
798+ }
799+ }
800+}
801+
771802bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
772803{
773804// qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
@@ -1053,15 +1084,11 @@ void ListViewWithPageHeader::onModelUpdated(const QQmlChangeSet &changeSet, bool
10531084 }
10541085
10551086 Q_FOREACH(const QQmlChangeSet::Change change, changeSet.changes()) {
1056−// qDebug() << "ListViewWithPageHeader::onModelUpdated Change" << change.index << change.count;
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);
1063− }
1087+ for (int i = change.start(); i < change.end(); ++i) {
1088+ updateSectionItem(i);
10641089 }
1090+ // Also update the section header for the next item after the change since it may be influenced
1091+ updateSectionItem(change.end());
10651092 }
10661093
10671094 if (m_firstVisibleIndex != oldFirstVisibleIndex) {
plugins/Dash/listviewwithpageheader.h+1
@@ -167,6 +167,7 @@ private:
167167 void updateWatchedRoles();
168168 QQuickItem *getSectionItem(int modelIndex, bool alreadyInserted);
169169 QQuickItem *getSectionItem(const QString &sectionText);
170+ void updateSectionItem(int modelIndex);
170171
171172 QQmlDelegateModel *m_delegateModel;
172173
tests/plugins/Dash/listviewwithpageheadersectiontest.cpp+31
@@ -2149,6 +2149,37 @@ private Q_SLOTS:
21492149 QVERIFY(QQuickItemPrivate::get(lvwph->m_topSectionItem)->culled);
21502150 }
21512151
2152+ void changeSectionProperty()
2153+ {
2154+ model->setProperty(1, "type", "Rojo");
2155+ verifyItem(1, 240., 240., false, "Rojo", false);
2156+
2157+ model->setProperty(1, "type", "Agressive");
2158+ verifyItem(1, 240., 200., false, QString(), true);
2159+
2160+ model->setProperty(1, "type", "Rojo");
2161+ verifyItem(1, 240., 240., false, "Rojo", false);
2162+
2163+ model->setProperty(1, "type", "Mild");
2164+ verifyItem(1, 240., 240., false, "Mild", false);
2165+ verifyItem(2, 480., 350., false, QString(), false);
2166+
2167+ model->setProperty(1, "type", "Rojo");
2168+ verifyItem(1, 240., 240., false, "Rojo", false);
2169+ verifyItem(2, 480., 390., false, "Mild", false);
2170+
2171+ model->setProperty(2, "type", "Agressive");
2172+ verifyItem(2, 480., 390., false, "Agressive", false);
2173+
2174+ model->setProperty(1, "type", "Agressive");
2175+ verifyItem(1, 240., 200., false, QString(), true);
2176+ verifyItem(2, 440., 350., false, QString(), false);
2177+
2178+ model->setProperty(1, "type", "Rojo");
2179+ verifyItem(1, 240., 240., false, "Rojo", false);
2180+ verifyItem(2, 480., 390., false, "Agressive", false);
2181+ }
2182+
21522183private:
21532184 QQuickView *view;
21542185 ListViewWithPageHeader *lvwph;