on ffff556 help sign in
unity8/plugins/Unity/categories.cpp
244 lines 8.8 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();
93 setModel(model);
96void
97Categories::setUnityScope(const unity::dash::Scope::Ptr& scope)
99 m_unityScope = scope;
101 // no need to call this, we'll get notified
102 //setModel(m_unityScope->categories()->model());
104 m_categoriesChangedConnection.disconnect();
105 m_categoriesChangedConnection =
106 m_unityScope->categories()->model.changed.connect(sigc::mem_fun(this, &Categories::onCategoriesModelChanged));
109void
110Categories::onCountChanged()
112 CategoryResults* results = qobject_cast<CategoryResults*>(sender());
113 if (results) {
114 m_updatedCategories << results->categoryIndex();
115 m_timer.start();
116 }
119void Categories::onRowCountChanged()
121 QAbstractItemModel* model = qobject_cast<QAbstractItemModel*>(sender());
122 // find the corresponding category index
123 for (auto iter = m_overriddenCategories.begin(); iter != m_overriddenCategories.end(); ++iter) {
124 if (iter.value() == model) {
125 for (int i = 0; i < rowCount(); i++) {
126 auto id = data(index(i), RoleCategoryId).toString();
127 if (id != iter.key()) continue;
128 QVector<int> roles;
129 roles.append(RoleCount);
130 QModelIndex changedIndex = index(i);
131 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
132 break;
133 }
134 }
135 }
138void
139Categories::onEmitCountChanged()
141 QVector<int> roles;
142 roles.append(Categories::RoleCount);
143 Q_FOREACH(int categoryIndex, m_updatedCategories) {
144 if (!m_results.contains(categoryIndex)) continue;
145 QModelIndex changedIndex = index(categoryIndex);
146 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
147 }
148 m_updatedCategories.clear();
151QHash<int, QByteArray>
152Categories::roleNames() const
154 return m_roles;
157void Categories::onOverrideModelDestroyed()
159 QObject* model = sender();
160 auto iter = m_overriddenCategories.begin();
161 while (iter != m_overriddenCategories.end()) {
162 if (iter.value() == model) {
163 iter = m_overriddenCategories.erase(iter);
164 continue;
165 }
166 ++iter;
167 }
170void Categories::overrideResults(const QString& categoryId, QAbstractItemModel* model)
172 m_overriddenCategories[categoryId] = model;
173 // watch the model
174 connect(model, &QObject::destroyed, this, &Categories::onOverrideModelDestroyed);
175 connect(model, &QAbstractItemModel::rowsInserted, this, &Categories::onRowCountChanged);
176 connect(model, &QAbstractItemModel::rowsRemoved, this, &Categories::onRowCountChanged);
177 connect(model, &QAbstractItemModel::modelReset, this, &Categories::onRowCountChanged);
179 // emit the dataChanged signal if the category is already in the model
180 for (int i = 0; i < rowCount(); i++) {
181 auto id = data(index(i), RoleCategoryId).toString();
182 if (id != categoryId) continue;
183 QVector<int> roles;
184 roles.append(RoleCount);
185 roles.append(RoleResults);
186 QModelIndex changedIndex = index(i);
187 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
188 break;
189 }
192QVariant
193Categories::data(const QModelIndex& index, int role) const
195 if (!index.isValid()) {
196 return QVariant();
197 }
199 switch (role) {
200 case RoleCategoryId:
201 return DeeListModel::data(index, CategoryColumn::ID);
202 case RoleName:
203 return DeeListModel::data(index, CategoryColumn::DISPLAY_NAME);
204 case RoleIcon:
205 return DeeListModel::data(index, CategoryColumn::ICON_HINT);
206 case RoleRenderer:
207 return DeeListModel::data(index, CategoryColumn::RENDERER_NAME);
208 case RoleContentType:
209 {
210 auto hints = DeeListModel::data(index, CategoryColumn::HINTS).toHash();
211 return hints.contains("content-type") ? hints["content-type"] : QVariant(QString("default"));
212 }
213 case RoleRendererHint:
214 {
215 auto hints = DeeListModel::data(index, CategoryColumn::HINTS).toHash();
216 return hints.contains("renderer-hint") ? hints["renderer-hint"] : QVariant(QString());
217 }
218 case RoleProgressSource:
219 {
220 auto hints = DeeListModel::data(index, CategoryColumn::HINTS).toHash();
221 return hints.contains("progress-source") ? hints["progress-source"] : QVariant();
222 }
223 case RoleHints:
224 return DeeListModel::data(index, CategoryColumn::HINTS);
225 case RoleResults:
226 if (m_overriddenCategories.size() > 0)
227 {
228 auto id = DeeListModel::data(index, CategoryColumn::ID).toString();
229 if (m_overriddenCategories.find(id) != m_overriddenCategories.end())
230 return QVariant::fromValue(m_overriddenCategories[id]);
231 }
232 return QVariant::fromValue(getResults(index.row()));
233 case RoleCount:
234 if (m_overriddenCategories.size() > 0)
235 {
236 auto id = DeeListModel::data(index, CategoryColumn::ID).toString();
237 if (m_overriddenCategories.find(id) != m_overriddenCategories.end())
238 return QVariant::fromValue(m_overriddenCategories[id]->rowCount());
239 }
240 return QVariant::fromValue(getResults(index.row())->rowCount());
241 default:
242 return QVariant();
243 }