on 78ccb79 help sign in
unity8/plugins/Unity/categories.cpp
230 lines 8.0 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"
25// TODO: use something from libunity once it's public
26enum CategoryColumn {
27 ID,
28 DISPLAY_NAME,
29 ICON_HINT,
30 RENDERER_NAME,
31 HINTS
32};
34Categories::Categories(QObject* parent)
35 : DeeListModel(parent)
37 m_roles[Categories::RoleCategoryId] = "categoryId";
38 m_roles[Categories::RoleName] = "name";
39 m_roles[Categories::RoleIcon] = "icon";
40 m_roles[Categories::RoleRenderer] = "renderer";
41 m_roles[Categories::RoleContentType] = "contentType";
42 m_roles[Categories::RoleProgressSource] = "progressSource";
43 m_roles[Categories::RoleHints] = "hints";
44 m_roles[Categories::RoleResults] = "results";
45 m_roles[Categories::RoleCount] = "count";
46 m_roles[Categories::RoleCategoryIndex] = "categoryIndex";
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 results->setModel(unity_results->model());
68 m_results.insert(index, results);
69 }
71 return m_results[index];
74void Categories::onCategoriesModelChanged(unity::glib::Object<DeeModel> model)
76 m_updatedCategories.clear();
77 // FIXME: this might destroy the renderer view and re-create it, optimize?
78 Q_FOREACH(DeeListModel* model, m_results) {
79 delete model;
80 }
81 m_results.clear();
82 setModel(model);
85void
86Categories::setUnityScope(const unity::dash::Scope::Ptr& scope)
88 m_unityScope = scope;
90 // no need to call this, we'll get notified
91 //setModel(m_unityScope->categories()->model());
93 m_categoriesChangedConnection.disconnect();
94 m_categoriesChangedConnection =
95 m_unityScope->categories()->model.changed.connect(sigc::mem_fun(this, &Categories::onCategoriesModelChanged));
98void
99Categories::onCountChanged()
101 CategoryResults* results = qobject_cast<CategoryResults*>(sender());
102 if (results) {
103 m_updatedCategories << results->categoryIndex();
104 m_timer.start();
105 }
108void Categories::onRowCountChanged()
110 QAbstractItemModel* model = qobject_cast<QAbstractItemModel*>(sender());
111 // find the corresponding category index
112 for (auto iter = m_overriddenCategories.begin(); iter != m_overriddenCategories.end(); ++iter) {
113 if (iter.value() == model) {
114 for (int i = 0; i < rowCount(); i++) {
115 auto id = data(index(i), RoleCategoryId).toString();
116 if (id != iter.key()) continue;
117 QVector<int> roles;
118 roles.append(RoleCount);
119 QModelIndex changedIndex = index(i);
120 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
121 break;
122 }
123 }
124 }
127void
128Categories::onEmitCountChanged()
130 QVector<int> roles;
131 roles.append(Categories::RoleCount);
132 Q_FOREACH(int categoryIndex, m_updatedCategories) {
133 if (!m_results.contains(categoryIndex)) continue;
134 QModelIndex changedIndex = index(categoryIndex);
135 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
136 }
137 m_updatedCategories.clear();
140QHash<int, QByteArray>
141Categories::roleNames() const
143 return m_roles;
146void Categories::onOverrideModelDestroyed()
148 QObject* model = sender();
149 auto iter = m_overriddenCategories.begin();
150 while (iter != m_overriddenCategories.end()) {
151 if (iter.value() == model) {
152 iter = m_overriddenCategories.erase(iter);
153 continue;
154 }
155 ++iter;
156 }
159void Categories::overrideResults(const QString& categoryId, QAbstractItemModel* model)
161 m_overriddenCategories[categoryId] = model;
162 // watch the model
163 connect(model, &QObject::destroyed, this, &Categories::onOverrideModelDestroyed);
164 connect(model, &QAbstractItemModel::rowsInserted, this, &Categories::onRowCountChanged);
165 connect(model, &QAbstractItemModel::rowsRemoved, this, &Categories::onRowCountChanged);
166 connect(model, &QAbstractItemModel::modelReset, this, &Categories::onRowCountChanged);
168 // emit the dataChanged signal if the category is already in the model
169 for (int i = 0; i < rowCount(); i++) {
170 auto id = data(index(i), RoleCategoryId).toString();
171 if (id != categoryId) continue;
172 QVector<int> roles;
173 roles.append(RoleCount);
174 roles.append(RoleResults);
175 QModelIndex changedIndex = index(i);
176 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
177 break;
178 }
181QVariant
182Categories::data(const QModelIndex& index, int role) const
184 if (!index.isValid()) {
185 return QVariant();
186 }
188 switch (role) {
189 case RoleCategoryId:
190 return DeeListModel::data(index, CategoryColumn::ID);
191 case RoleName:
192 return DeeListModel::data(index, CategoryColumn::DISPLAY_NAME);
193 case RoleIcon:
194 return DeeListModel::data(index, CategoryColumn::ICON_HINT);
195 case RoleRenderer:
196 return DeeListModel::data(index, CategoryColumn::RENDERER_NAME);
197 case RoleContentType:
198 {
199 auto hints = DeeListModel::data(index, CategoryColumn::HINTS).toHash();
200 return hints.contains("content-type") ? hints["content-type"] : QVariant(QString("default"));
201 }
202 case RoleProgressSource:
203 {
204 auto hints = DeeListModel::data(index, CategoryColumn::HINTS).toHash();
205 return hints.contains("progress-source") ? hints["progress-source"] : QVariant();
206 }
207 case RoleHints:
208 return DeeListModel::data(index, CategoryColumn::HINTS);
209 case RoleResults:
210 if (m_overriddenCategories.size() > 0)
211 {
212 auto id = DeeListModel::data(index, CategoryColumn::ID).toString();
213 if (m_overriddenCategories.find(id) != m_overriddenCategories.end())
214 return QVariant::fromValue(m_overriddenCategories[id]);
215 }
216 return QVariant::fromValue(getResults(index.row()));
217 case RoleCount:
218 if (m_overriddenCategories.size() > 0)
219 {
220 auto id = DeeListModel::data(index, CategoryColumn::ID).toString();
221 if (m_overriddenCategories.find(id) != m_overriddenCategories.end())
222 return QVariant::fromValue(m_overriddenCategories[id]->rowCount());
223 }
224 return QVariant::fromValue(getResults(index.row())->rowCount());
225 case RoleCategoryIndex:
226 return QVariant::fromValue(index.row());
227 default:
228 return QVariant();
229 }