don't remapp all roles each time, keep the old ones, with the same role number/name mapping: avoids role shuffle after an update

svn path=/trunk/KDE/kdebase/runtime/; revision=1216034
This commit is contained in:
Marco Martin 2011-01-20 22:03:52 +00:00
parent ccf35dd608
commit 45c6ac7dfb
2 changed files with 13 additions and 11 deletions

View File

@ -125,7 +125,8 @@ void SortFilterModel::setSortOrder(const Qt::SortOrder order)
DataModel::DataModel(QObject* parent) DataModel::DataModel(QObject* parent)
: QAbstractItemModel(parent), : QAbstractItemModel(parent),
m_dataSource(0) m_dataSource(0),
m_maxRoleId(Qt::UserRole)
{ {
setObjectName("DataModel"); setObjectName("DataModel");
connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)), connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
@ -217,21 +218,21 @@ void DataModel::setItems(const QString &sourceName, const QVariantList &list)
m_items[sourceName] = list.toVector(); m_items[sourceName] = list.toVector();
if (!list.isEmpty()) { if (!list.isEmpty()) {
int role = Qt::UserRole;
m_roleNames.clear();
m_roleIds.clear();
if (list.first().canConvert<QVariantHash>()) { if (list.first().canConvert<QVariantHash>()) {
foreach (const QString& roleName, list.first().value<QVariantHash>().keys()) { foreach (const QString& roleName, list.first().value<QVariantHash>().keys()) {
++role; if (!m_roleIds.contains(roleName)) {
m_roleNames[role] = roleName.toLatin1(); ++m_maxRoleId;
m_roleIds[roleName] = role; m_roleNames[m_maxRoleId] = roleName.toLatin1();
m_roleIds[roleName] = m_maxRoleId;
}
} }
} else { } else {
foreach (const QString& roleName, list.first().value<QVariantMap>().keys()) { foreach (const QString& roleName, list.first().value<QVariantMap>().keys()) {
++role; if (!m_roleIds.contains(roleName)) {
m_roleNames[role] = roleName.toLatin1(); ++m_maxRoleId;
m_roleIds[roleName] = role; m_roleNames[m_maxRoleId] = roleName.toLatin1();
m_roleIds[roleName] = m_maxRoleId;
}
} }
} }

View File

@ -132,6 +132,7 @@ private:
QMap<QString, QVector<QVariant> > m_items; QMap<QString, QVector<QVariant> > m_items;
QHash<int, QByteArray> m_roleNames; QHash<int, QByteArray> m_roleNames;
QHash<QString, int> m_roleIds; QHash<QString, int> m_roleIds;
int m_maxRoleId;
}; };
int DataModel::countItems() const int DataModel::countItems() const