on e82caeb help sign in
unity8/plugins/Unity/categories.cpp
238 lines 8.6 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::RoleProgressSource] = "progressSource";
44 m_roles[Categories::RoleHints] = "hints";
45 m_roles[Categories::RoleResults] = "results";
46 m_roles[Categories::RoleCount] = "count";
48 // TODO This should not be needed but accumulatting the count changes
49 // makes the visualization more stable and also makes crashes on fast
50 // change of the search term harder to reproduce
51 m_timer.setSingleShot(true);
52 m_timer.setInterval(50);
53 connect(&m_timer, &QTimer::timeout, this, &Categories::onEmitCountChanged);
56DeeListModel*
57Categories::getResults(int index) const
59 if (!m_results.contains(index)) {
60 CategoryResults* results = new CategoryResults(const_cast<Categories*>(this));
61 results->setCategoryIndex(index);
62 connect(results, &DeeListModel::countChanged, this, &Categories::onCountChanged);
64 unsigned categoryIndex = static_cast<unsigned>(index);
65 auto unity_results = m_unityScope->GetResultsForCategory(categoryIndex);
66 if (unity_results) {
67 results->setModel(unity_results->model());
68 } else {
69 // No results model returned by unity core; this can be the case when the global
70 // results model of this scope is still not set in unity core. Don't set backend
71 // model in DeeListModel - it will still beahve properly as an empty model. Since
72 // we're connected to the category model change signal, and it is set by unity core
73 // at the same time as results model (on channel opening), we'll reset category
74 // results models with proper models when we're notifed again.
75 qWarning() << "No results model for category" << categoryIndex;
76 }
78 m_results.insert(index, results);
79 }
81 return m_results[index];
84void Categories::onCategoriesModelChanged(unity::glib::Object<DeeModel> model)
86 m_updatedCategories.clear();
87 // FIXME: this might destroy the renderer view and re-create it, optimize?
88 Q_FOREACH(DeeListModel* model, m_results) {
89 delete model;
90 }
91 m_results.clear();
92 setModel(model);
95void
96Categories::setUnityScope(const unity::dash::Scope::Ptr& scope)
98 m_unityScope = scope;
100 // no need to call this, we'll get notified
101 //setModel(m_unityScope->categories()->model());
103 m_categoriesChangedConnection.disconnect();
104 m_categoriesChangedConnection =
105 m_unityScope->categories()->model.changed.connect(sigc::mem_fun(this, &Categories::onCategoriesModelChanged));
108void
109Categories::onCountChanged()
111 CategoryResults* results = qobject_cast<CategoryResults*>(sender());
112 if (results) {
113 m_updatedCategories << results->categoryIndex();
114 m_timer.start();
115 }
118void Categories::onRowCountChanged()
120 QAbstractItemModel* model = qobject_cast<QAbstractItemModel*>(sender());
121 // find the corresponding category index
122 for (auto iter = m_overriddenCategories.begin(); iter != m_overriddenCategories.end(); ++iter) {
123 if (iter.value() == model) {
124 for (int i = 0; i < rowCount(); i++) {
125 auto id = data(index(i), RoleCategoryId).toString();
126 if (id != iter.key()) continue;
127 QVector<int> roles;
128 roles.append(RoleCount);
129 QModelIndex changedIndex = index(i);
130 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
131 break;
132 }
133 }
134 }
137void
138Categories::onEmitCountChanged()
140 QVector<int> roles;
141 roles.append(Categories::RoleCount);
142 Q_FOREACH(int categoryIndex, m_updatedCategories) {
143 if (!m_results.contains(categoryIndex)) continue;
144 QModelIndex changedIndex = index(categoryIndex);
145 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
146 }
147 m_updatedCategories.clear();
150QHash<int, QByteArray>
151Categories::roleNames() const
153 return m_roles;
156void Categories::onOverrideModelDestroyed()
158 QObject* model = sender();
159 auto iter = m_overriddenCategories.begin();
160 while (iter != m_overriddenCategories.end()) {
161 if (iter.value() == model) {
162 iter = m_overriddenCategories.erase(iter);
163 continue;
164 }
165 ++iter;
166 }
169void Categories::overrideResults(const QString& categoryId, QAbstractItemModel* model)
171 m_overriddenCategories[categoryId] = model;
172 // watch the model
173 connect(model, &QObject::destroyed, this, &Categories::onOverrideModelDestroyed);
174 connect(model, &QAbstractItemModel::rowsInserted, this, &Categories::onRowCountChanged);
175 connect(model, &QAbstractItemModel::rowsRemoved, this, &Categories::onRowCountChanged);
176 connect(model, &QAbstractItemModel::modelReset, this, &Categories::onRowCountChanged);
178 // emit the dataChanged signal if the category is already in the model
179 for (int i = 0; i < rowCount(); i++) {
180 auto id = data(index(i), RoleCategoryId).toString();
181 if (id != categoryId) continue;
182 QVector<int> roles;
183 roles.append(RoleCount);
184 roles.append(RoleResults);
185 QModelIndex changedIndex = index(i);
186 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
187 break;
188 }
191QVariant
192Categories::data(const QModelIndex& index, int role) const
194 if (!index.isValid()) {
195 return QVariant();
196 }
198 switch (role) {
199 case RoleCategoryId:
200 return DeeListModel::data(index, CategoryColumn::ID);
201 case RoleName:
202 return DeeListModel::data(index, CategoryColumn::DISPLAY_NAME);
203 case RoleIcon:
204 return DeeListModel::data(index, CategoryColumn::ICON_HINT);
205 case RoleRenderer:
206 return DeeListModel::data(index, CategoryColumn::RENDERER_NAME);
207 case RoleContentType:
208 {
209 auto hints = DeeListModel::data(index, CategoryColumn::HINTS).toHash();
210 return hints.contains("content-type") ? hints["content-type"] : QVariant(QString("default"));
211 }
212 case RoleProgressSource:
213 {
214 auto hints = DeeListModel::data(index, CategoryColumn::HINTS).toHash();
215 return hints.contains("progress-source") ? hints["progress-source"] : QVariant();
216 }
217 case RoleHints:
218 return DeeListModel::data(index, CategoryColumn::HINTS);
219 case RoleResults:
220 if (m_overriddenCategories.size() > 0)
221 {
222 auto id = DeeListModel::data(index, CategoryColumn::ID).toString();
223 if (m_overriddenCategories.find(id) != m_overriddenCategories.end())
224 return QVariant::fromValue(m_overriddenCategories[id]);
225 }
226 return QVariant::fromValue(getResults(index.row()));
227 case RoleCount:
228 if (m_overriddenCategories.size() > 0)
229 {
230 auto id = DeeListModel::data(index, CategoryColumn::ID).toString();
231 if (m_overriddenCategories.find(id) != m_overriddenCategories.end())
232 return QVariant::fromValue(m_overriddenCategories[id]->rowCount());
233 }
234 return QVariant::fromValue(getResults(index.row())->rowCount());
235 default:
236 return QVariant();
237 }