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