on 2c12ffa help sign in
unity8/plugins/Unity/categories.cpp
276 lines 9.5 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// Qt
26#include <QDebug>
28// TODO: use something from libunity once it's public
29enum CategoryColumn {
30 ID,
31 DISPLAY_NAME,
32 ICON_HINT,
33 RENDERER_NAME,
34 HINTS
35};
37Categories::Categories(QObject* parent)
38 : DeeListModel(parent)
40 m_roles[Categories::RoleCategoryId] = "categoryId";
41 m_roles[Categories::RoleName] = "name";
42 m_roles[Categories::RoleIcon] = "icon";
43 m_roles[Categories::RoleRenderer] = "renderer";
44 m_roles[Categories::RoleContentType] = "contentType";
45 m_roles[Categories::RoleProgressSource] = "progressSource";
46 m_roles[Categories::RoleHints] = "hints";
47 m_roles[Categories::RoleResults] = "results";
48 m_roles[Categories::RoleCount] = "count";
49 m_roles[Categories::RoleCategoryIndex] = "categoryIndex";
51 // TODO This should not be needed but accumulatting the count changes
52 // makes the visualization more stable and also makes crashes on fast
53 // change of the search term harder to reproduce
54 m_timer.setSingleShot(true);
55 m_timer.setInterval(50);
56 connect(&m_timer, &QTimer::timeout, this, &Categories::onEmitCountChanged);
59DeeListModel*
60Categories::getResults(int index) const
62 if (!m_results.contains(index)) {
63 CategoryResults* results = new CategoryResults(const_cast<Categories*>(this));
64 results->setCategoryIndex(index);
65 connect(results, &DeeListModel::countChanged, this, &Categories::onCountChanged);
67 unsigned categoryIndex = static_cast<unsigned>(index);
68 auto unity_results = m_unityScope->GetResultsForCategory(categoryIndex);
69 results->setModel(unity_results->model());
71 m_results.insert(index, results);
72 }
74 return m_results[index];
77void Categories::onCategoriesModelChanged(unity::glib::Object<DeeModel> model)
79 m_updatedCategories.clear();
80 // FIXME: this might destroy the renderer view and re-create it, optimize?
81 Q_FOREACH(DeeListModel* model, m_results) {
82 delete model;
83 }
84 m_results.clear();
86 setModel(model);
89void Categories::onCategoryOrderChanged(std::vector<unsigned int> cat_order)
91 for (unsigned int pos = 0; pos<cat_order.size(); pos++)
92 {
93 unsigned int cat = cat_order[pos];
94 const int old_pos = m_categoryOrder.indexOf(cat);
96 if (old_pos < 0) {
97 qWarning() << "No such category index:" << cat;
98 continue;
99 }
101 if (static_cast<int>(pos) != old_pos) {
102 beginMoveRows(QModelIndex(), old_pos, old_pos, QModelIndex(), pos);
103 m_categoryOrder.move(old_pos, pos);
104 endMoveRows();
105 }
106 }
109void
110Categories::setUnityScope(const unity::dash::Scope::Ptr& scope)
112 m_unityScope = scope;
114 // no need to call this, we'll get notified
115 //setModel(m_unityScope->categories()->model());
117 m_signals.disconnectAll();
118 m_signals << m_unityScope->categories()->model.changed.connect(sigc::mem_fun(this, &Categories::onCategoriesModelChanged));
119 m_signals << m_unityScope->category_order.changed.connect(sigc::mem_fun(this, &Categories::onCategoryOrderChanged));
122void
123Categories::onCountChanged()
125 CategoryResults* results = qobject_cast<CategoryResults*>(sender());
126 if (results) {
127 m_updatedCategories << results->categoryIndex();
128 m_timer.start();
129 }
132void Categories::onRowCountChanged()
134 QAbstractItemModel* model = qobject_cast<QAbstractItemModel*>(sender());
135 // find the corresponding category index
136 for (auto iter = m_overriddenCategories.begin(); iter != m_overriddenCategories.end(); ++iter) {
137 if (iter.value() == model) {
138 for (int i = 0; i < rowCount(); i++) {
139 auto id = data(index(i), RoleCategoryId).toString();
140 if (id != iter.key()) continue;
141 const int realIndex = m_categoryOrder.indexOf(i);
142 QVector<int> roles;
143 roles.append(RoleCount);
144 QModelIndex changedIndex = index(realIndex);
145 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
146 break;
147 }
148 }
149 }
152void
153Categories::onEmitCountChanged()
155 QVector<int> roles;
156 roles.append(Categories::RoleCount);
157 Q_FOREACH(int categoryIndex, m_updatedCategories) {
158 if (!m_results.contains(categoryIndex)) continue;
159 const int realIndex = m_categoryOrder.indexOf(categoryIndex);
160 const QModelIndex changedIndex = index(realIndex);
161 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
162 }
163 m_updatedCategories.clear();
166QHash<int, QByteArray>
167Categories::roleNames() const
169 return m_roles;
172void Categories::onOverrideModelDestroyed()
174 QObject* model = sender();
175 auto iter = m_overriddenCategories.begin();
176 while (iter != m_overriddenCategories.end()) {
177 if (iter.value() == model) {
178 iter = m_overriddenCategories.erase(iter);
179 continue;
180 }
181 ++iter;
182 }
185void Categories::overrideResults(const QString& categoryId, QAbstractItemModel* model)
187 m_overriddenCategories[categoryId] = model;
188 // watch the model
189 connect(model, &QObject::destroyed, this, &Categories::onOverrideModelDestroyed);
190 connect(model, &QAbstractItemModel::rowsInserted, this, &Categories::onRowCountChanged);
191 connect(model, &QAbstractItemModel::rowsRemoved, this, &Categories::onRowCountChanged);
192 connect(model, &QAbstractItemModel::modelReset, this, &Categories::onRowCountChanged);
194 // emit the dataChanged signal if the category is already in the model
195 for (int i = 0; i < rowCount(); i++) {
196 auto id = data(index(i), RoleCategoryId).toString();
197 if (id != categoryId) continue;
198 QVector<int> roles;
199 roles.append(RoleCount);
200 roles.append(RoleResults);
201 QModelIndex changedIndex = index(i);
202 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
203 break;
204 }
207QVariant
208Categories::data(const QModelIndex& index, int role) const
210 if (!index.isValid()) {
211 return QVariant();
212 }
214 if (m_categoryOrder.size() != rowCount())
215 {
216 // populate category order vector with 0..n
217 m_categoryOrder.clear();
218 const unsigned int lim = rowCount();
219 for (unsigned int i = 0; i<lim; i++) {
220 m_categoryOrder.append(i);
221 }
222 }
224 int realRow = index.row();
225 if (index.row() < m_categoryOrder.size()) {
226 realRow = m_categoryOrder[index.row()];
227 } else {
228 qWarning() << "Invalid index" << index.row();
229 return QVariant();
230 }
232 const QModelIndex realIndex = createIndex(realRow, index.column());
234 switch (role) {
235 case RoleCategoryId:
236 return DeeListModel::data(realIndex, CategoryColumn::ID);
237 case RoleName:
238 return DeeListModel::data(realIndex, CategoryColumn::DISPLAY_NAME);
239 case RoleIcon:
240 return DeeListModel::data(realIndex, CategoryColumn::ICON_HINT);
241 case RoleRenderer:
242 return DeeListModel::data(realIndex, CategoryColumn::RENDERER_NAME);
243 case RoleContentType:
244 {
245 auto hints = DeeListModel::data(realIndex, CategoryColumn::HINTS).toHash();
246 return hints.contains("content-type") ? hints["content-type"] : QVariant(QString("default"));
247 }
248 case RoleProgressSource:
249 {
250 auto hints = DeeListModel::data(realIndex, CategoryColumn::HINTS).toHash();
251 return hints.contains("progress-source") ? hints["progress-source"] : QVariant();
252 }
253 case RoleHints:
254 return DeeListModel::data(realIndex, CategoryColumn::HINTS);
255 case RoleResults:
256 if (m_overriddenCategories.size() > 0)
257 {
258 auto id = DeeListModel::data(realIndex, CategoryColumn::ID).toString();
259 if (m_overriddenCategories.find(id) != m_overriddenCategories.end())
260 return QVariant::fromValue(m_overriddenCategories[id]);
261 }
262 return QVariant::fromValue(getResults(realRow));
263 case RoleCount:
264 if (m_overriddenCategories.size() > 0)
265 {
266 auto id = DeeListModel::data(realIndex, CategoryColumn::ID).toString();
267 if (m_overriddenCategories.find(id) != m_overriddenCategories.end())
268 return QVariant::fromValue(m_overriddenCategories[id]->rowCount());
269 }
270 return QVariant::fromValue(getResults(realIndex.row())->rowCount());
271 case RoleCategoryIndex:
272 return QVariant::fromValue(index.row()); // no remapping to realIndex
273 default:
274 return QVariant();
275 }