on xenial help sign in
Update count when overridden model count changes
98cdd95Michal Hruby
plugins/Unity/categories.cpp+21
@@ -104,6 +104,25 @@ Categories::onCountChanged()
104 }
104 }
105}
105}
106
106
107+void Categories::onRowCountChanged()
108+{
109+ QAbstractItemModel* model = qobject_cast<QAbstractItemModel*>(sender());
110+ // find the corresponding category index
111+ for (auto iter = m_overriddenCategories.begin(); iter != m_overriddenCategories.end(); ++iter) {
112+ if (iter.value() == model) {
113+ for (int i = 0; i < rowCount(); i++) {
114+ auto id = data(index(i), RoleCategoryId).toString();
115+ if (id != iter.key()) continue;
116+ QVector<int> roles;
117+ roles.append(RoleCount);
118+ QModelIndex changedIndex = index(i);
119+ Q_EMIT dataChanged(changedIndex, changedIndex, roles);
120+ break;
121+ }
122+ }
123+ }
124+}
125+
107void
126void
108Categories::onEmitCountChanged()
127Categories::onEmitCountChanged()
109{
128{
@@ -127,6 +146,8 @@ void Categories::overrideResults(const QString& categoryId, QAbstractItemModel*
127{
146{
128 m_overriddenCategories[categoryId] = model;
147 m_overriddenCategories[categoryId] = model;
129 // TODO: change the parent of the model?
148 // TODO: change the parent of the model?
149+ connect(model, &QAbstractItemModel::rowsInserted, this, &Categories::onRowCountChanged);
150+ connect(model, &QAbstractItemModel::rowsRemoved, this, &Categories::onRowCountChanged);
130
151
131 // emit the dataChanged signal if the category is already in the model
152 // emit the dataChanged signal if the category is already in the model
132 for (int i = 0; i < rowCount(); i++) {
153 for (int i = 0; i < rowCount(); i++) {
plugins/Unity/categories.h+1
@@ -64,6 +64,7 @@ public:
64
64
65private Q_SLOTS:
65private Q_SLOTS:
66 void onCountChanged();
66 void onCountChanged();
67+ void onRowCountChanged();
67 void onEmitCountChanged();
68 void onEmitCountChanged();
68
69
69private:
70private: