on ae54754 help sign in
unity8/plugins/Unity/categories.cpp
294 lines 10 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// TODO: use something from libunity once it's public
27enum CategoryColumn {
28 ID,
29 DISPLAY_NAME,
30 ICON_HINT,
31 RENDERER_NAME,
32 HINTS
33};
35Categories::Categories(QObject* parent)
36 : DeeListModel(parent)
38 m_roles[Categories::RoleCategoryId] = "categoryId";
39 m_roles[Categories::RoleName] = "name";
40 m_roles[Categories::RoleIcon] = "icon";
41 m_roles[Categories::RoleRenderer] = "renderer";
42 m_roles[Categories::RoleContentType] = "contentType";
43 m_roles[Categories::RoleRendererHint] = "rendererHint";
44 m_roles[Categories::RoleProgressSource] = "progressSource";
45 m_roles[Categories::RoleHints] = "hints";
46 m_roles[Categories::RoleResults] = "results";
47 m_roles[Categories::RoleCount] = "count";
49 // TODO This should not be needed but accumulatting the count changes
50 // makes the visualization more stable and also makes crashes on fast
51 // change of the search term harder to reproduce
52 m_timer.setSingleShot(true);
53 m_timer.setInterval(50);
54 connect(&m_timer, &QTimer::timeout, this, &Categories::onEmitCountChanged);
57DeeListModel*
58Categories::getResults(int index) const
60 if (!m_results.contains(index)) {
61 CategoryResults* results = new CategoryResults(const_cast<Categories*>(this));
62 results->setCategoryIndex(index);
63 connect(results, &DeeListModel::countChanged, this, &Categories::onCountChanged);
65 unsigned categoryIndex = static_cast<unsigned>(index);
66 auto unity_results = m_unityScope->GetResultsForCategory(categoryIndex);
67 if (unity_results) {
68 results->setModel(unity_results->model());
69 } else {
70 // No results model returned by unity core; this can be the case when the global
71 // results model of this scope is still not set in unity core. Don't set backend
72 // model in DeeListModel - it will still beahve properly as an empty model. Since
73 // we're connected to the category model change signal, and it is set by unity core
74 // at the same time as results model (on channel opening), we'll reset category
75 // results models with proper models when we're notifed again.
76 qWarning() << "No results model for category" << categoryIndex;
77 }
79 m_results.insert(index, results);
80 }
82 return m_results[index];
85void Categories::onCategoriesModelChanged(unity::glib::Object<DeeModel> model)
87 m_updatedCategories.clear();
88 // FIXME: this might destroy the renderer view and re-create it, optimize?
89 Q_FOREACH(DeeListModel* model, m_results) {
90 delete model;
91 }
92 m_results.clear();
94 setModel(model);
97void Categories::onCategoryOrderChanged(const std::vector<unsigned int>& cat_order)
99 for (unsigned int pos = 0; pos<cat_order.size(); pos++)
100 {
101 unsigned int cat = cat_order[pos];
102 const int old_pos = m_categoryOrder.indexOf(cat);
104 if (old_pos < 0) {
105 qWarning() << "No such category index:" << cat;
106 continue;
107 }
109 if (static_cast<int>(pos) != old_pos) {
110 int target_pos = pos;
111 const bool status = beginMoveRows(QModelIndex(), old_pos, old_pos, QModelIndex(), target_pos);
112 if (status)
113 m_categoryOrder.move(old_pos, pos);
114 else
115 qWarning("beginMoveRows failed");
116 endMoveRows();
117 }
118 }
121void
122Categories::setUnityScope(const unity::dash::Scope::Ptr& scope)
124 m_unityScope = scope;
126 // no need to call this, we'll get notified
127 //setModel(m_unityScope->categories()->model());
129 m_signals.disconnectAll();
130 m_signals << m_unityScope->categories()->model.changed.connect(sigc::mem_fun(this, &Categories::onCategoriesModelChanged));
131 m_signals << m_unityScope->category_order.changed.connect(sigc::mem_fun(this, &Categories::onCategoryOrderChanged));
134void
135Categories::onCountChanged()
137 CategoryResults* results = qobject_cast<CategoryResults*>(sender());
138 if (results) {
139 if (!m_updatedCategories.contains(results->categoryIndex())) {
140 m_updatedCategories << results->categoryIndex();
141 }
142 m_timer.start();
143 }
146void Categories::onRowCountChanged()
148 QAbstractItemModel* model = qobject_cast<QAbstractItemModel*>(sender());
149 // find the corresponding category index
150 for (auto iter = m_overriddenCategories.begin(); iter != m_overriddenCategories.end(); ++iter) {
151 if (iter.value() == model) {
152 for (int i = 0; i < rowCount(); i++) {
153 auto id = data(index(i), RoleCategoryId).toString();
154 if (id != iter.key()) continue;
155 QVector<int> roles;
156 roles.append(RoleCount);
157 QModelIndex changedIndex = index(i);
158 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
159 break;
160 }
161 }
162 }
165void
166Categories::onEmitCountChanged()
168 QVector<int> roles;
169 roles.append(Categories::RoleCount);
170 Q_FOREACH(int categoryIndex, m_updatedCategories) {
171 if (!m_results.contains(categoryIndex)) continue;
172 const int realIndex = m_categoryOrder.indexOf(categoryIndex);
173 const QModelIndex changedIndex = index(realIndex);
174 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
175 }
176 m_updatedCategories.clear();
179QHash<int, QByteArray>
180Categories::roleNames() const
182 return m_roles;
185void Categories::onOverrideModelDestroyed()
187 QObject* model = sender();
188 auto iter = m_overriddenCategories.begin();
189 while (iter != m_overriddenCategories.end()) {
190 if (iter.value() == model) {
191 iter = m_overriddenCategories.erase(iter);
192 continue;
193 }
194 ++iter;
195 }
198void Categories::overrideResults(const QString& categoryId, QAbstractItemModel* model)
200 m_overriddenCategories[categoryId] = model;
201 // watch the model
202 connect(model, &QObject::destroyed, this, &Categories::onOverrideModelDestroyed);
203 connect(model, &QAbstractItemModel::rowsInserted, this, &Categories::onRowCountChanged);
204 connect(model, &QAbstractItemModel::rowsRemoved, this, &Categories::onRowCountChanged);
205 connect(model, &QAbstractItemModel::modelReset, this, &Categories::onRowCountChanged);
207 // emit the dataChanged signal if the category is already in the model
208 for (int i = 0; i < rowCount(); i++) {
209 auto id = data(index(i), RoleCategoryId).toString();
210 if (id != categoryId) continue;
211 QVector<int> roles;
212 roles.append(RoleCount);
213 roles.append(RoleResults);
214 QModelIndex changedIndex = index(i);
215 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
216 break;
217 }
220QVariant
221Categories::data(const QModelIndex& index, int role) const
223 if (!index.isValid()) {
224 return QVariant();
225 }
227 if (m_categoryOrder.size() != rowCount())
228 {
229 // populate category order vector with 0..n
230 m_categoryOrder.clear();
231 const unsigned int lim = rowCount();
232 for (unsigned int i = 0; i<lim; i++) {
233 m_categoryOrder.append(i);
234 }
235 }
237 int realRow = index.row();
238 if (index.row() < m_categoryOrder.size()) {
239 realRow = m_categoryOrder[index.row()];
240 } else {
241 qWarning() << "Invalid index" << index.row();
242 return QVariant();
243 }
245 const QModelIndex realIndex = createIndex(realRow, index.column());
247 switch (role) {
248 case RoleCategoryId:
249 return DeeListModel::data(realIndex, CategoryColumn::ID);
250 case RoleName:
251 return DeeListModel::data(realIndex, CategoryColumn::DISPLAY_NAME);
252 case RoleIcon:
253 return DeeListModel::data(realIndex, CategoryColumn::ICON_HINT);
254 case RoleRenderer:
255 return DeeListModel::data(realIndex, CategoryColumn::RENDERER_NAME);
256 case RoleContentType:
257 {
258 auto hints = DeeListModel::data(realIndex, CategoryColumn::HINTS).toHash();
259 return hints.contains("content-type") ? hints["content-type"] : QVariant(QString("default"));
260 }
261 case RoleRendererHint:
262 {
263 auto hints = DeeListModel::data(index, CategoryColumn::HINTS).toHash();
264 return hints.contains("renderer-hint") ? hints["renderer-hint"] : QVariant(QString());
265 }
266 case RoleProgressSource:
267 {
268 auto hints = DeeListModel::data(realIndex, CategoryColumn::HINTS).toHash();
269 return hints.contains("progress-source") ? hints["progress-source"] : QVariant();
270 }
271 case RoleHints:
272 return DeeListModel::data(realIndex, CategoryColumn::HINTS);
273 case RoleResults:
274 if (m_overriddenCategories.size() > 0)
275 {
276 auto id = DeeListModel::data(realIndex, CategoryColumn::ID).toString();
277 if (m_overriddenCategories.find(id) != m_overriddenCategories.end())
278 return QVariant::fromValue(m_overriddenCategories[id]);
279 }
280 return QVariant::fromValue(getResults(realRow));
281 case RoleCount:
282 if (m_overriddenCategories.size() > 0)
283 {
284 auto id = DeeListModel::data(realIndex, CategoryColumn::ID).toString();
285 if (m_overriddenCategories.find(id) != m_overriddenCategories.end())
286 {
287 return QVariant::fromValue(m_overriddenCategories[id]->rowCount());
288 }
289 }
290 return QVariant::fromValue(getResults(realRow)->rowCount());
291 default:
292 return QVariant();
293 }