From 8987c745c6744e383463ed95e1da99b97f786aef Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Wed, 30 Jun 2010 16:53:08 +1000 Subject: Add ListView.nextSection attached property and also renamed ListView.prevSection to ListView.previousSection Task-number: QTBUG-11372 --- src/declarative/QmlChanges.txt | 2 ++ .../graphicsitems/qdeclarativelistview.cpp | 33 +++++++++++++++++++--- .../graphicsitems/qdeclarativelistview_p.h | 13 ++++++++- .../data/listview-sections.qml | 15 ++++++---- .../tst_qdeclarativelistview.cpp | 2 ++ 5 files changed, 55 insertions(+), 10 deletions(-) diff --git a/src/declarative/QmlChanges.txt b/src/declarative/QmlChanges.txt index 432b5d6..336fbbf 100644 --- a/src/declarative/QmlChanges.txt +++ b/src/declarative/QmlChanges.txt @@ -14,6 +14,8 @@ Component: - isReady, isLoading, isError and isNull properties removed, use status property instead - errorsString() renamed to errorString() +ListView: + - ListView.prevSection property changed to ListView.previousSection TextInput xToPosition -> positionAt (to match TextEdit.positionAt) diff --git a/src/declarative/graphicsitems/qdeclarativelistview.cpp b/src/declarative/graphicsitems/qdeclarativelistview.cpp index a69fc3f..4be8705 100644 --- a/src/declarative/graphicsitems/qdeclarativelistview.cpp +++ b/src/declarative/graphicsitems/qdeclarativelistview.cpp @@ -575,6 +575,10 @@ FxListItem *QDeclarativeListViewPrivate::createItem(int modelIndex) listItem->attached->m_prevSection = item->attached->section(); else listItem->attached->m_prevSection = sectionAt(modelIndex-1); + if (FxListItem *item = visibleItem(modelIndex+1)) + listItem->attached->m_nextSection = item->attached->section(); + else + listItem->attached->m_nextSection = sectionAt(modelIndex+1); } } if (model->completePending()) { @@ -951,14 +955,26 @@ void QDeclarativeListViewPrivate::updateSections() QString prevSection; if (visibleIndex > 0) prevSection = sectionAt(visibleIndex-1); + QDeclarativeListViewAttached *prevAtt = 0; + int idx = -1; for (int i = 0; i < visibleItems.count(); ++i) { if (visibleItems.at(i)->index != -1) { QDeclarativeListViewAttached *attached = visibleItems.at(i)->attached; attached->setPrevSection(prevSection); + if (prevAtt) + prevAtt->setNextSection(attached->section()); createSection(visibleItems.at(i)); prevSection = attached->section(); + prevAtt = attached; + idx = visibleItems.at(i)->index; } } + if (prevAtt) { + if (idx > 0 && idx < model->count()-1) + prevAtt->setNextSection(sectionAt(idx+1)); + else + prevAtt->setNextSection(QString()); + } } } @@ -1418,7 +1434,7 @@ QDeclarativeListView::~QDeclarativeListView() */ /*! - \qmlattachedproperty string ListView::prevSection + \qmlattachedproperty string ListView::previousSection This attached property holds the section of the previous element. It is attached to each instance of the delegate. @@ -1427,6 +1443,15 @@ QDeclarativeListView::~QDeclarativeListView() */ /*! + \qmlattachedproperty string ListView::nextSection + This attached property holds the section of the next element. + + It is attached to each instance of the delegate. + + The section is evaluated using the \l {ListView::section.property}{section} properties. +*/ + +/*! \qmlattachedproperty string ListView::section This attached property holds the section of this element. @@ -1963,9 +1988,9 @@ void QDeclarativeListView::setCacheBuffer(int b) \c section.delegate holds the delegate component for each section. - Each item in the list has attached properties named \c ListView.section and - \c ListView.prevSection. These may be used to place a section header for - related items. + Each item in the list has attached properties named \c ListView.section, + \c ListView.previousSection and \c ListView.nextSection. These may be + used to place a section header for related items. For example, here is a ListView that displays a list of animals, separated into sections. Each item in the ListView is placed in a different section diff --git a/src/declarative/graphicsitems/qdeclarativelistview_p.h b/src/declarative/graphicsitems/qdeclarativelistview_p.h index 505f659..f55db9b 100644 --- a/src/declarative/graphicsitems/qdeclarativelistview_p.h +++ b/src/declarative/graphicsitems/qdeclarativelistview_p.h @@ -279,7 +279,7 @@ public: } } - Q_PROPERTY(QString prevSection READ prevSection NOTIFY prevSectionChanged) + Q_PROPERTY(QString previousSection READ prevSection NOTIFY prevSectionChanged) QString prevSection() const { return m_prevSection; } void setPrevSection(const QString §) { if (m_prevSection != sect) { @@ -288,6 +288,15 @@ public: } } + Q_PROPERTY(QString nextSection READ nextSection NOTIFY nextSectionChanged) + QString nextSection() const { return m_nextSection; } + void setNextSection(const QString §) { + if (m_nextSection != sect) { + m_nextSection = sect; + emit nextSectionChanged(); + } + } + Q_PROPERTY(QString section READ section NOTIFY sectionChanged) QString section() const { return m_section; } void setSection(const QString §) { @@ -313,6 +322,7 @@ Q_SIGNALS: void currentItemChanged(); void sectionChanged(); void prevSectionChanged(); + void nextSectionChanged(); void delayRemoveChanged(); void add(); void remove(); @@ -321,6 +331,7 @@ public: QDeclarativeListView *m_view; mutable QString m_section; QString m_prevSection; + QString m_nextSection; bool m_isCurrent : 1; bool m_delayRemove : 1; }; diff --git a/tests/auto/declarative/qdeclarativelistview/data/listview-sections.qml b/tests/auto/declarative/qdeclarativelistview/data/listview-sections.qml index a6f3ab8..9a5ea55 100644 --- a/tests/auto/declarative/qdeclarativelistview/data/listview-sections.qml +++ b/tests/auto/declarative/qdeclarativelistview/data/listview-sections.qml @@ -10,10 +10,10 @@ Rectangle { Item { id: wrapper objectName: "wrapper" - height: ListView.prevSection != ListView.section ? 40 : 20; + height: ListView.previousSection != ListView.section ? 40 : 20; width: 240 Rectangle { - y: wrapper.ListView.prevSection != wrapper.ListView.section ? 20 : 0 + y: wrapper.ListView.previousSection != wrapper.ListView.section ? 20 : 0 height: 20 width: parent.width color: wrapper.ListView.isCurrentItem ? "lightsteelblue" : "white" @@ -27,21 +27,26 @@ Rectangle { text: name } Text { - x: 120 + x: 100 id: textNumber objectName: "textNumber" text: number } Text { + objectName: "nextSection" + x: 150 + text: wrapper.ListView.nextSection + } + Text { x: 200 text: wrapper.y } } Rectangle { color: "#99bb99" - height: wrapper.ListView.prevSection != wrapper.ListView.section ? 20 : 0 + height: wrapper.ListView.previousSection != wrapper.ListView.section ? 20 : 0 width: parent.width - visible: wrapper.ListView.prevSection != wrapper.ListView.section ? true : false + visible: wrapper.ListView.previousSection != wrapper.ListView.section ? true : false Text { text: wrapper.ListView.section } } } diff --git a/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp b/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp index 048f0f3..e13bd94 100644 --- a/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp +++ b/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp @@ -902,6 +902,8 @@ void tst_QDeclarativeListView::sections() QDeclarativeItem *item = findItem(contentItem, "wrapper", i); QTRY_VERIFY(item); QTRY_COMPARE(item->y(), qreal(i*20 + ((i+4)/5) * 20)); + QDeclarativeText *next = findItem(item, "nextSection"); + QCOMPARE(next->text().toInt(), (i+1)/5); } // Remove section boundary -- cgit v0.12