a new property for models: count (another thing badly missing from QML, where is necessary to hook it up to a view to figure out how many items there are)

svn path=/trunk/KDE/kdebase/runtime/; revision=1204004
This commit is contained in:
Marco Martin 2010-12-05 22:23:13 +00:00
parent 4c6b625f62
commit 933b88e340
2 changed files with 31 additions and 9 deletions

View File

@ -30,6 +30,12 @@ SortFilterModel::SortFilterModel(QObject* parent)
{
setObjectName("SortFilterModel");
setDynamicSortFilter(true);
connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
this, SIGNAL(countChanged()));
connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
this, SIGNAL(countChanged()));
connect(this, SIGNAL(modelReset()),
this, SIGNAL(countChanged()));
}
SortFilterModel::~SortFilterModel()
@ -118,21 +124,18 @@ DataModel::DataModel(QObject* parent)
m_dataSource(0)
{
setObjectName("DataModel");
connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
this, SIGNAL(countChanged()));
connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
this, SIGNAL(countChanged()));
connect(this, SIGNAL(modelReset()),
this, SIGNAL(countChanged()));
}
DataModel::~DataModel()
{
}
int DataModel::countItems() const
{
int count = 0;
foreach (const QVector<QVariant> &v, m_items) {
count += v.count();
}
return count;
}
void DataModel::dataUpdated(const QString &sourceName, const Plasma::DataEngine::Data &data)
{
if (!m_keyRoleFilter.isEmpty()) {

View File

@ -43,6 +43,7 @@ class SortFilterModel : public QSortFilterProxyModel
Q_PROPERTY(QString filterRole READ filterRole WRITE setFilterRole)
Q_PROPERTY(QString sortRole READ sortRole WRITE setSortRole)
Q_PROPERTY(Qt::SortOrder sortOrder READ sortOrder WRITE setSortOrder)
Q_PROPERTY(int count READ count NOTIFY countChanged)
friend class DataModel;
@ -64,6 +65,11 @@ public:
void setSortOrder(const Qt::SortOrder order);
int count() const {return QSortFilterProxyModel::rowCount();}
Q_SIGNALS:
void countChanged();
protected:
int roleNameToId(const QString &name);
@ -81,6 +87,7 @@ class DataModel : public QAbstractItemModel
Q_OBJECT
Q_PROPERTY(QObject *dataSource READ dataSource WRITE setDataSource)
Q_PROPERTY(QString keyRoleFilter READ keyRoleFilter WRITE setKeyRoleFilter)
Q_PROPERTY(int count READ count NOTIFY countChanged)
public:
DataModel(QObject* parent=0);
@ -104,6 +111,8 @@ public:
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
int count() const {return countItems();}
protected:
void setItems(const QString &sourceName, const QVariantList &list);
inline int countItems() const;
@ -111,6 +120,7 @@ protected:
Q_SIGNALS:
void modelAboutToBeReset();
void modelReset();
void countChanged();
private Q_SLOTS:
void dataUpdated(const QString &sourceName, const Plasma::DataEngine::Data &data);
@ -124,5 +134,14 @@ private:
QHash<QString, int> m_roleIds;
};
int DataModel::countItems() const
{
int count = 0;
foreach (const QVector<QVariant> &v, m_items) {
count += v.count();
}
return count;
}
}
#endif