on 808987a help sign in
unity8/plugins/Unity/categories.cpp
307 lines 11 kB history raw
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * Authors:
5 * Michał Sawicz <michal.sawicz@canonical.com>
6 * Michal Hruby <michal.hruby@canonical.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 3.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
21// self
22#include "categories.h"
23#include "categoryresults.h"
24#include <QDebug>
26// Qt
27#include <QDebug>
29// TODO: use something from libunity once it's public
30enum CategoryColumn {
31 ID,
32 DISPLAY_NAME,
33 ICON_HINT,
34 RENDERER_NAME,
35 HINTS
36};
38Categories::Categories(QObject* parent)
39 : DeeListModel(parent)
41 m_roles[Categories::RoleCategoryId] = "categoryId";
42 m_roles[Categories::RoleName] = "name";
43 m_roles[Categories::RoleIcon] = "icon";
44 m_roles[Categories::RoleRenderer] = "renderer";
45 m_roles[Categories::RoleContentType] = "contentType";
46 m_roles[Categories::RoleProgressSource] = "progressSource";
47 m_roles[Categories::RoleHints] = "hints";
48 m_roles[Categories::RoleResults] = "results";
49 m_roles[Categories::RoleCount] = "count";
51 // TODO This should not be needed but accumulatting the count changes
52 // makes the visualization more stable and also makes crashes on fast
53 // change of the search term harder to reproduce
54 m_timer.setSingleShot(true);
55 m_timer.setInterval(50);
56 connect(&m_timer, &QTimer::timeout, this, &Categories::onEmitCountChanged);
59DeeListModel*
60Categories::getResults(int index) const
62 if (!m_results.contains(index)) {
63 CategoryResults* results = new CategoryResults(const_cast<Categories*>(this));
64 results->setCategoryIndex(index);
65 connect(results, &DeeListModel::countChanged, this, &Categories::onCountChanged);
67 unsigned categoryIndex = static_cast<unsigned>(index);
68 auto unity_results = m_unityScope->GetResultsForCategory(categoryIndex);
69 if (unity_results) {
70 results->setModel(unity_results->model());
71 } else {
72 // No results model returned by unity core; this can be the case when the global
73 // results model of this scope is still not set in unity core. Don't set backend
74 // model in DeeListModel - it will still beahve properly as an empty model. Since
75 // we're connected to the category model change signal, and it is set by unity core
76 // at the same time as results model (on channel opening), we'll reset category
77 // results models with proper models when we're notifed again.
78 qWarning() << "No results model for category" << categoryIndex;
79 }
81 m_results.insert(index, results);
82 }
84 return m_results[index];
87void Categories::onCategoriesModelChanged(unity::glib::Object<DeeModel> model)
89 m_updatedCategories.clear();
90 // FIXME: this might destroy the renderer view and re-create it, optimize?
91 Q_FOREACH(DeeListModel* model, m_results) {
92 delete model;
93 }
94 m_results.clear();
96 setModel(model);
99void Categories::onCategoryOrderChanged(std::vector<unsigned int> cat_order)
101 for (unsigned int pos = 0; pos<cat_order.size(); pos++)
102 {
103 unsigned int cat = cat_order[pos];
104 const int old_pos = m_categoryOrder.indexOf(cat);
106 if (old_pos < 0) {
107 qWarning() << "No such category index:" << cat;
108 continue;
109 }
111 if (static_cast<int>(pos) != old_pos) {
112 m_categoryOrder.move(old_pos, pos);
113 int target_pos = pos;
114 //if (pos > old_pos)
115 // target_pos = target_pos - old_pos;
116 bool status = beginMoveRows(QModelIndex(), old_pos, old_pos, QModelIndex(), target_pos);
117 if (status != true)
118 qWarning("!!!!!!!!!!!!!!!!!!!!!!!");
120 qDebug() << "Moving category" << cat <<
121 DeeListModel::data(createIndex(cat, 0),CategoryColumn::DISPLAY_NAME).toString()
122 << ":" << old_pos << "->" << pos << target_pos << "g";
123 endMoveRows();
124 }
125 }
127 Q_FOREACH(auto cat, m_categoryOrder)
128 {
129 qDebug() << "Category" << DeeListModel::data(createIndex(cat, 0),CategoryColumn::DISPLAY_NAME).toString();
130 }
133void
134Categories::setUnityScope(const unity::dash::Scope::Ptr& scope)
136 m_unityScope = scope;
138 // no need to call this, we'll get notified
139 //setModel(m_unityScope->categories()->model());
141 m_signals.disconnectAll();
142 m_signals << m_unityScope->categories()->model.changed.connect(sigc::mem_fun(this, &Categories::onCategoriesModelChanged));
143 m_signals << m_unityScope->category_order.changed.connect(sigc::mem_fun(this, &Categories::onCategoryOrderChanged));
146void
147Categories::onCountChanged()
149 CategoryResults* results = qobject_cast<CategoryResults*>(sender());
150 if (results) {
151 if (!m_updatedCategories.contains(results->categoryIndex())) {
152 m_updatedCategories << results->categoryIndex();
153 qDebug() << "Updated count of" << results->categoryIndex();
154 }
155 m_timer.start();
156 }
159void Categories::onRowCountChanged()
161 qDebug() << "Overriden model updated";
162 QAbstractItemModel* model = qobject_cast<QAbstractItemModel*>(sender());
163 // find the corresponding category index
164 for (auto iter = m_overriddenCategories.begin(); iter != m_overriddenCategories.end(); ++iter) {
165 if (iter.value() == model) {
166 for (int i = 0; i < rowCount(); i++) {
167 auto id = data(index(i), RoleCategoryId).toString();
168 if (id != iter.key()) continue;
169 const int mappedIndex = m_categoryOrder.indexOf(i);
170 QVector<int> roles;
171 roles.append(RoleCount);
172 QModelIndex changedIndex = index(i);
173 qDebug() << "Overriden model" << id << "count update at index" << i;
174 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
175 break;
176 }
177 }
178 }
181void
182Categories::onEmitCountChanged()
184 QVector<int> roles;
185 roles.append(Categories::RoleCount);
186 Q_FOREACH(int categoryIndex, m_updatedCategories) {
187 if (!m_results.contains(categoryIndex)) continue;
188 const int realIndex = m_categoryOrder.indexOf(categoryIndex);
189 const QModelIndex changedIndex = index(realIndex);
190 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
191 }
192 m_updatedCategories.clear();
195QHash<int, QByteArray>
196Categories::roleNames() const
198 return m_roles;
201void Categories::onOverrideModelDestroyed()
203 QObject* model = sender();
204 auto iter = m_overriddenCategories.begin();
205 while (iter != m_overriddenCategories.end()) {
206 if (iter.value() == model) {
207 iter = m_overriddenCategories.erase(iter);
208 continue;
209 }
210 ++iter;
211 }
214void Categories::overrideResults(const QString& categoryId, QAbstractItemModel* model)
216 m_overriddenCategories[categoryId] = model;
217 // watch the model
218 connect(model, &QObject::destroyed, this, &Categories::onOverrideModelDestroyed);
219 connect(model, &QAbstractItemModel::rowsInserted, this, &Categories::onRowCountChanged);
220 connect(model, &QAbstractItemModel::rowsRemoved, this, &Categories::onRowCountChanged);
221 connect(model, &QAbstractItemModel::modelReset, this, &Categories::onRowCountChanged);
223 // emit the dataChanged signal if the category is already in the model
224 for (int i = 0; i < rowCount(); i++) {
225 auto id = data(index(i), RoleCategoryId).toString();
226 if (id != categoryId) continue;
227 QVector<int> roles;
228 roles.append(RoleCount);
229 roles.append(RoleResults);
230 QModelIndex changedIndex = index(i);
231 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
232 break;
233 }
236QVariant
237Categories::data(const QModelIndex& index, int role) const
239 if (!index.isValid()) {
240 return QVariant();
241 }
243 if (m_categoryOrder.size() != rowCount())
244 {
245 // populate category order vector with 0..n
246 m_categoryOrder.clear();
247 const unsigned int lim = rowCount();
248 for (unsigned int i = 0; i<lim; i++) {
249 m_categoryOrder.append(i);
250 }
251 }
253 int realRow = index.row();
254 if (index.row() < m_categoryOrder.size()) {
255 realRow = m_categoryOrder[index.row()];
256 } else {
257 qWarning() << "Invalid index" << index.row();
258 return QVariant();
259 }
261 const QModelIndex realIndex = createIndex(realRow, index.column());
263 switch (role) {
264 case RoleCategoryId:
265 return DeeListModel::data(realIndex, CategoryColumn::ID);
266 case RoleName:
267 return DeeListModel::data(realIndex, CategoryColumn::DISPLAY_NAME);
268 case RoleIcon:
269 return DeeListModel::data(realIndex, CategoryColumn::ICON_HINT);
270 case RoleRenderer:
271 return DeeListModel::data(realIndex, CategoryColumn::RENDERER_NAME);
272 case RoleContentType:
273 {
274 auto hints = DeeListModel::data(realIndex, CategoryColumn::HINTS).toHash();
275 return hints.contains("content-type") ? hints["content-type"] : QVariant(QString("default"));
276 }
277 case RoleProgressSource:
278 {
279 auto hints = DeeListModel::data(realIndex, CategoryColumn::HINTS).toHash();
280 return hints.contains("progress-source") ? hints["progress-source"] : QVariant();
281 }
282 case RoleHints:
283 return DeeListModel::data(realIndex, CategoryColumn::HINTS);
284 case RoleResults:
285 if (m_overriddenCategories.size() > 0)
286 {
287 auto id = DeeListModel::data(realIndex, CategoryColumn::ID).toString();
288 if (m_overriddenCategories.find(id) != m_overriddenCategories.end())
289 return QVariant::fromValue(m_overriddenCategories[id]);
290 }
291 return QVariant::fromValue(getResults(realRow));
292 case RoleCount:
293 if (m_overriddenCategories.size() > 0)
294 {
295 auto id = DeeListModel::data(realIndex, CategoryColumn::ID).toString();
296 if (m_overriddenCategories.find(id) != m_overriddenCategories.end())
297 {
298 qDebug() << "Get count of overriden cat" << realRow << id << "at pos" << index.row() << "Count=" << m_overriddenCategories[id]->rowCount();
299 return QVariant::fromValue(m_overriddenCategories[id]->rowCount());
300 }
301 }
302 qDebug() << "Get count of cat" << DeeListModel::data(realIndex, CategoryColumn::ID).toString() << realRow << "at pos" << index.row() << "Count=" << getResults(realIndex.row())->rowCount();
303 return QVariant::fromValue(getResults(realRow)->rowCount());
304 default:
305 return QVariant();
306 }