summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Jones <martin.jones@nokia.com>2010-10-12 03:11:38 (GMT)
committerMartin Jones <martin.jones@nokia.com>2010-10-12 03:11:38 (GMT)
commit650a0078e2cef43eff107fe8d2505f64a0bfedf0 (patch)
treebaea5084ec32f1ba492d33919be379d88b023a55
parent3f0eb0416205600b97a0504006d7fbb09a10e035 (diff)
downloadQt-650a0078e2cef43eff107fe8d2505f64a0bfedf0.zip
Qt-650a0078e2cef43eff107fe8d2505f64a0bfedf0.tar.gz
Qt-650a0078e2cef43eff107fe8d2505f64a0bfedf0.tar.bz2
Update sections if model content changes.
We didn't handle the section property changing, e.g. due to asynchronous model. Task-number: QT-4093 Reviewed-by: Aaron Kennedy
-rw-r--r--src/declarative/graphicsitems/qdeclarativelistview.cpp40
-rw-r--r--src/declarative/graphicsitems/qdeclarativelistview_p.h2
-rw-r--r--src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp21
-rw-r--r--src/declarative/graphicsitems/qdeclarativevisualitemmodel_p.h4
-rw-r--r--tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp11
5 files changed, 75 insertions, 3 deletions
diff --git a/src/declarative/graphicsitems/qdeclarativelistview.cpp b/src/declarative/graphicsitems/qdeclarativelistview.cpp
index 4943aef..6fd3b71 100644
--- a/src/declarative/graphicsitems/qdeclarativelistview.cpp
+++ b/src/declarative/graphicsitems/qdeclarativelistview.cpp
@@ -775,7 +775,6 @@ void QDeclarativeListViewPrivate::layout()
setPosition(0);
return;
}
- updateSections();
if (!visibleItems.isEmpty()) {
qreal oldEnd = visibleItems.last()->endPosition();
qreal pos = visibleItems.first()->endPosition() + spacing + 1;
@@ -934,6 +933,7 @@ void QDeclarativeListViewPrivate::createSection(FxListItem *listItem)
return;
if (listItem->attached->m_prevSection != listItem->attached->m_section) {
if (!listItem->section) {
+ qreal pos = listItem->position();
int i = sectionCacheSize-1;
while (i >= 0 && !sectionCache[i])
--i;
@@ -961,8 +961,10 @@ void QDeclarativeListViewPrivate::createSection(FxListItem *listItem)
delete context;
}
}
+ listItem->setPosition(pos);
}
} else if (listItem->section) {
+ qreal pos = listItem->position();
int i = 0;
do {
if (!sectionCache[i]) {
@@ -975,12 +977,13 @@ void QDeclarativeListViewPrivate::createSection(FxListItem *listItem)
} while (i < sectionCacheSize);
delete listItem->section;
listItem->section = 0;
+ listItem->setPosition(pos);
}
}
void QDeclarativeListViewPrivate::updateSections()
{
- if (sectionCriteria) {
+ if (sectionCriteria && !visibleItems.isEmpty()) {
QString prevSection;
if (visibleIndex > 0)
prevSection = sectionAt(visibleIndex-1);
@@ -990,6 +993,8 @@ void QDeclarativeListViewPrivate::updateSections()
if (visibleItems.at(i)->index != -1) {
QDeclarativeListViewAttached *attached = visibleItems.at(i)->attached;
attached->setPrevSection(prevSection);
+ QString propValue = model->stringValue(visibleItems.at(i)->index, sectionCriteria->property());
+ attached->setSection(sectionCriteria->sectionString(propValue));
if (prevAtt)
prevAtt->setNextSection(attached->section());
createSection(visibleItems.at(i));
@@ -1560,6 +1565,7 @@ void QDeclarativeListView::setModel(const QVariant &model)
disconnect(d->model, SIGNAL(itemsInserted(int,int)), this, SLOT(itemsInserted(int,int)));
disconnect(d->model, SIGNAL(itemsRemoved(int,int)), this, SLOT(itemsRemoved(int,int)));
disconnect(d->model, SIGNAL(itemsMoved(int,int,int)), this, SLOT(itemsMoved(int,int,int)));
+ disconnect(d->model, SIGNAL(itemsChanged(int,int)), this, SLOT(itemsChanged(int,int)));
disconnect(d->model, SIGNAL(modelReset()), this, SLOT(modelReset()));
disconnect(d->model, SIGNAL(createdItem(int, QDeclarativeItem*)), this, SLOT(createdItem(int,QDeclarativeItem*)));
disconnect(d->model, SIGNAL(destroyingItem(QDeclarativeItem*)), this, SLOT(destroyingItem(QDeclarativeItem*)));
@@ -1590,6 +1596,7 @@ void QDeclarativeListView::setModel(const QVariant &model)
if (d->model) {
d->bufferMode = QDeclarativeListViewPrivate::BufferBefore | QDeclarativeListViewPrivate::BufferAfter;
if (isComponentComplete()) {
+ updateSections();
refill();
if (d->currentIndex >= d->model->count() || d->currentIndex < 0) {
setCurrentIndex(0);
@@ -1605,6 +1612,7 @@ void QDeclarativeListView::setModel(const QVariant &model)
connect(d->model, SIGNAL(itemsInserted(int,int)), this, SLOT(itemsInserted(int,int)));
connect(d->model, SIGNAL(itemsRemoved(int,int)), this, SLOT(itemsRemoved(int,int)));
connect(d->model, SIGNAL(itemsMoved(int,int,int)), this, SLOT(itemsMoved(int,int,int)));
+ connect(d->model, SIGNAL(itemsChanged(int,int)), this, SLOT(itemsChanged(int,int)));
connect(d->model, SIGNAL(modelReset()), this, SLOT(modelReset()));
connect(d->model, SIGNAL(createdItem(int, QDeclarativeItem*)), this, SLOT(createdItem(int,QDeclarativeItem*)));
connect(d->model, SIGNAL(destroyingItem(QDeclarativeItem*)), this, SLOT(destroyingItem(QDeclarativeItem*)));
@@ -1662,6 +1670,7 @@ void QDeclarativeListView::setDelegate(QDeclarativeComponent *delegate)
d->visibleItems.clear();
d->releaseItem(d->currentItem);
d->currentItem = 0;
+ updateSections();
refill();
d->moveReason = QDeclarativeListViewPrivate::SetIndex;
d->updateCurrent(d->currentIndex);
@@ -2075,8 +2084,10 @@ void QDeclarativeListView::setCacheBuffer(int b)
QDeclarativeViewSection *QDeclarativeListView::sectionCriteria()
{
Q_D(QDeclarativeListView);
- if (!d->sectionCriteria)
+ if (!d->sectionCriteria) {
d->sectionCriteria = new QDeclarativeViewSection(this);
+ connect(d->sectionCriteria, SIGNAL(propertyChanged()), this, SLOT(updateSections()));
+ }
return d->sectionCriteria;
}
@@ -2669,6 +2680,7 @@ void QDeclarativeListView::componentComplete()
{
Q_D(QDeclarativeListView);
QDeclarativeFlickable::componentComplete();
+ updateSections();
if (d->isValid()) {
refill();
d->moveReason = QDeclarativeListViewPrivate::SetIndex;
@@ -2685,6 +2697,18 @@ void QDeclarativeListView::componentComplete()
}
}
+void QDeclarativeListView::updateSections()
+{
+ Q_D(QDeclarativeListView);
+ if (isComponentComplete() && d->model) {
+ QList<QByteArray> roles;
+ if (d->sectionCriteria && !d->sectionCriteria->property().isEmpty())
+ roles << d->sectionCriteria->property().toUtf8();
+ d->model->setWatchedRoles(roles);
+ d->updateSections();
+ }
+}
+
void QDeclarativeListView::refill()
{
Q_D(QDeclarativeListView);
@@ -2894,6 +2918,7 @@ void QDeclarativeListView::itemsInserted(int modelIndex, int count)
for (int j = 0; j < added.count(); ++j)
added.at(j)->attached->emitAdd();
+ d->updateSections();
d->itemCount += count;
emit countChanged();
}
@@ -2986,6 +3011,7 @@ void QDeclarativeListView::itemsRemoved(int modelIndex, int count)
}
}
+ d->updateSections();
emit countChanged();
}
@@ -3107,6 +3133,14 @@ void QDeclarativeListView::itemsMoved(int from, int to, int count)
// Ensure we don't cause an ugly list scroll.
d->visibleItems.first()->setPosition(d->visibleItems.first()->position() + moveBy);
+ d->updateSections();
+ d->layout();
+}
+
+void QDeclarativeListView::itemsChanged(int, int)
+{
+ Q_D(QDeclarativeListView);
+ d->updateSections();
d->layout();
}
diff --git a/src/declarative/graphicsitems/qdeclarativelistview_p.h b/src/declarative/graphicsitems/qdeclarativelistview_p.h
index 735b248..2678b90 100644
--- a/src/declarative/graphicsitems/qdeclarativelistview_p.h
+++ b/src/declarative/graphicsitems/qdeclarativelistview_p.h
@@ -250,11 +250,13 @@ protected:
virtual void componentComplete();
private Q_SLOTS:
+ void updateSections();
void refill();
void trackedPositionChanged();
void itemsInserted(int index, int count);
void itemsRemoved(int index, int count);
void itemsMoved(int from, int to, int count);
+ void itemsChanged(int index, int count);
void modelReset();
void destroyRemoved();
void createdItem(int index, QDeclarativeItem *item);
diff --git a/src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp b/src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp
index 439f500..e569dd2 100644
--- a/src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp
+++ b/src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp
@@ -403,6 +403,8 @@ public:
QDeclarativeListAccessor *m_listAccessor;
QModelIndex m_root;
+ QList<QByteArray> watchedRoles;
+ QList<int> watchedRoleIds;
};
class QDeclarativeVisualDataModelDataMetaObject : public QDeclarativeOpenMetaObject
@@ -1170,10 +1172,25 @@ int QDeclarativeVisualDataModel::indexOf(QDeclarativeItem *item, QObject *) cons
return -1;
}
+void QDeclarativeVisualDataModel::setWatchedRoles(QList<QByteArray> roles)
+{
+ Q_D(QDeclarativeVisualDataModel);
+ d->watchedRoles = roles;
+ d->watchedRoleIds.clear();
+}
+
void QDeclarativeVisualDataModel::_q_itemsChanged(int index, int count,
const QList<int> &roles)
{
Q_D(QDeclarativeVisualDataModel);
+ bool changed = false;
+ if (!d->watchedRoles.isEmpty() && d->watchedRoleIds.isEmpty()) {
+ foreach (QByteArray r, d->watchedRoles) {
+ if (d->m_roleNames.contains(r))
+ d->watchedRoleIds << d->m_roleNames.value(r);
+ }
+ }
+
for (QHash<int,QDeclarativeVisualDataModelPrivate::ObjectRef>::ConstIterator iter = d->m_cache.begin();
iter != d->m_cache.end(); ++iter) {
const int idx = iter.key();
@@ -1183,6 +1200,8 @@ void QDeclarativeVisualDataModel::_q_itemsChanged(int index, int count,
QDeclarativeVisualDataModelData *data = d->data(objRef.obj);
for (int roleIdx = 0; roleIdx < roles.count(); ++roleIdx) {
int role = roles.at(roleIdx);
+ if (!changed && !d->watchedRoleIds.isEmpty() && d->watchedRoleIds.contains(role))
+ changed = true;
int propId = data->propForRole(role);
if (propId != -1) {
if (data->hasValue(propId)) {
@@ -1217,6 +1236,8 @@ void QDeclarativeVisualDataModel::_q_itemsChanged(int index, int count,
}
}
}
+ if (changed)
+ emit itemsChanged(index, count);
}
void QDeclarativeVisualDataModel::_q_itemsInserted(int index, int count)
diff --git a/src/declarative/graphicsitems/qdeclarativevisualitemmodel_p.h b/src/declarative/graphicsitems/qdeclarativevisualitemmodel_p.h
index f09d8dd..5e187c2 100644
--- a/src/declarative/graphicsitems/qdeclarativevisualitemmodel_p.h
+++ b/src/declarative/graphicsitems/qdeclarativevisualitemmodel_p.h
@@ -79,6 +79,7 @@ public:
virtual bool completePending() const = 0;
virtual void completeItem() = 0;
virtual QString stringValue(int, const QString &) = 0;
+ virtual void setWatchedRoles(QList<QByteArray> roles) = 0;
virtual int indexOf(QDeclarativeItem *item, QObject *objectContext) const = 0;
@@ -87,6 +88,7 @@ Q_SIGNALS:
void itemsInserted(int index, int count);
void itemsRemoved(int index, int count);
void itemsMoved(int from, int to, int count);
+ void itemsChanged(int index, int count);
void modelReset();
void createdItem(int index, QDeclarativeItem *item);
void destroyingItem(QDeclarativeItem *item);
@@ -120,6 +122,7 @@ public:
virtual bool completePending() const;
virtual void completeItem();
virtual QString stringValue(int index, const QString &role);
+ virtual void setWatchedRoles(QList<QByteArray>) {}
virtual int indexOf(QDeclarativeItem *item, QObject *objectContext) const;
@@ -174,6 +177,7 @@ public:
bool completePending() const;
void completeItem();
virtual QString stringValue(int index, const QString &role);
+ virtual void setWatchedRoles(QList<QByteArray> roles);
int indexOf(QDeclarativeItem *item, QObject *objectContext) const;
diff --git a/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp b/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp
index 65ff635..7a58773 100644
--- a/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp
+++ b/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp
@@ -985,6 +985,17 @@ void tst_QDeclarativeListView::sections()
QTRY_VERIFY(item);
QTRY_COMPARE(item->height(), 20.0);
+ // check that headers change when item changes
+ listview->setContentY(0);
+ model.modifyItem(0, "changed", "2");
+
+ canvas->show();
+ qApp->exec();
+
+ item = findItem<QDeclarativeItem>(contentItem, "wrapper", 1);
+ QTRY_VERIFY(item);
+ QTRY_COMPARE(item->height(), 40.0);
+
delete canvas;
}