on deafa96 help sign in
unity8/plugins/Unity/categories.cpp
299 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"
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 m_categoryOrder.move(old_pos, pos);
103 int target_pos = pos;
104 //if (pos > old_pos)
105 // target_pos = target_pos - old_pos;
106 bool status = beginMoveRows(QModelIndex(), old_pos, old_pos, QModelIndex(), target_pos);
107 if (status != true)
108 qWarning("!!!!!!!!!!!!!!!!!!!!!!!");
110 qDebug() << "Moving category" << cat <<
111 DeeListModel::data(createIndex(cat, 0),CategoryColumn::DISPLAY_NAME).toString()
112 << ":" << old_pos << "->" << pos << target_pos << "g";
113 endMoveRows();
114 }
115 }
117 Q_FOREACH(auto cat, m_categoryOrder)
118 {
119 qDebug() << "Category" << DeeListModel::data(createIndex(cat, 0),CategoryColumn::DISPLAY_NAME).toString();
120 }
123void
124Categories::setUnityScope(const unity::dash::Scope::Ptr& scope)
126 m_unityScope = scope;
128 // no need to call this, we'll get notified
129 //setModel(m_unityScope->categories()->model());
131 m_signals.disconnectAll();
132 m_signals << m_unityScope->categories()->model.changed.connect(sigc::mem_fun(this, &Categories::onCategoriesModelChanged));
133 m_signals << m_unityScope->category_order.changed.connect(sigc::mem_fun(this, &Categories::onCategoryOrderChanged));
136void
137Categories::onCountChanged()
139 CategoryResults* results = qobject_cast<CategoryResults*>(sender());
140 if (results) {
141 if (!m_updatedCategories.contains(results->categoryIndex())) {
142 m_updatedCategories << results->categoryIndex();
143 qDebug() << "Updated count of" << results->categoryIndex();
144 }
145 m_timer.start();
146 }
149void Categories::onRowCountChanged()
151 qDebug() << "Overriden model updated";
152 QAbstractItemModel* model = qobject_cast<QAbstractItemModel*>(sender());
153 // find the corresponding category index
154 for (auto iter = m_overriddenCategories.begin(); iter != m_overriddenCategories.end(); ++iter) {
155 if (iter.value() == model) {
156 for (int i = 0; i < rowCount(); i++) {
157 auto id = data(index(i), RoleCategoryId).toString();
158 if (id != iter.key()) continue;
159 const int mappedIndex = m_categoryOrder.indexOf(i);
160 QVector<int> roles;
161 roles.append(RoleCount);
162 QModelIndex changedIndex = index(i);
163 qDebug() << "Overriden model" << id << "count update at index" << i;
164 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
165 break;
166 }
167 }
168 }
171void
172Categories::onEmitCountChanged()
174 QVector<int> roles;
175 roles.append(Categories::RoleCount);
176 Q_FOREACH(int categoryIndex, m_updatedCategories) {
177 if (!m_results.contains(categoryIndex)) continue;
178 const int realIndex = m_categoryOrder.indexOf(categoryIndex);
179 const QModelIndex changedIndex = index(realIndex);
180 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
181 }
182 m_updatedCategories.clear();
185QHash<int, QByteArray>
186Categories::roleNames() const
188 return m_roles;
191void Categories::onOverrideModelDestroyed()
193 QObject* model = sender();
194 auto iter = m_overriddenCategories.begin();
195 while (iter != m_overriddenCategories.end()) {
196 if (iter.value() == model) {
197 iter = m_overriddenCategories.erase(iter);
198 continue;
199 }
200 ++iter;
201 }
204void Categories::overrideResults(const QString& categoryId, QAbstractItemModel* model)
206 m_overriddenCategories[categoryId] = model;
207 // watch the model
208 connect(model, &QObject::destroyed, this, &Categories::onOverrideModelDestroyed);
209 connect(model, &QAbstractItemModel::rowsInserted, this, &Categories::onRowCountChanged);
210 connect(model, &QAbstractItemModel::rowsRemoved, this, &Categories::onRowCountChanged);
211 connect(model, &QAbstractItemModel::modelReset, this, &Categories::onRowCountChanged);
213 // emit the dataChanged signal if the category is already in the model
214 for (int i = 0; i < rowCount(); i++) {
215 auto id = data(index(i), RoleCategoryId).toString();
216 if (id != categoryId) continue;
217 QVector<int> roles;
218 roles.append(RoleCount);
219 roles.append(RoleResults);
220 QModelIndex changedIndex = index(i);
221 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
222 break;
223 }
226QVariant
227Categories::data(const QModelIndex& index, int role) const
229 if (!index.isValid()) {
230 return QVariant();
231 }
233 if (m_categoryOrder.size() != rowCount())
234 {
235 // populate category order vector with 0..n
236 m_categoryOrder.clear();
237 const unsigned int lim = rowCount();
238 for (unsigned int i = 0; i<lim; i++) {
239 m_categoryOrder.append(i);
240 }
241 }
243 int realRow = index.row();
244 if (index.row() < m_categoryOrder.size()) {
245 realRow = m_categoryOrder[index.row()];
246 } else {
247 qWarning() << "Invalid index" << index.row();
248 return QVariant();
249 }
251 const QModelIndex realIndex = createIndex(realRow, index.column());
253 switch (role) {
254 case RoleCategoryId:
255 return DeeListModel::data(realIndex, CategoryColumn::ID);
256 case RoleName:
257 return DeeListModel::data(realIndex, CategoryColumn::DISPLAY_NAME);
258 case RoleIcon:
259 return DeeListModel::data(realIndex, CategoryColumn::ICON_HINT);
260 case RoleRenderer:
261 return DeeListModel::data(realIndex, CategoryColumn::RENDERER_NAME);
262 case RoleContentType:
263 {
264 auto hints = DeeListModel::data(realIndex, CategoryColumn::HINTS).toHash();
265 return hints.contains("content-type") ? hints["content-type"] : QVariant(QString("default"));
266 }
267 case RoleProgressSource:
268 {
269 auto hints = DeeListModel::data(realIndex, CategoryColumn::HINTS).toHash();
270 return hints.contains("progress-source") ? hints["progress-source"] : QVariant();
271 }
272 case RoleHints:
273 return DeeListModel::data(realIndex, CategoryColumn::HINTS);
274 case RoleResults:
275 if (m_overriddenCategories.size() > 0)
276 {
277 auto id = DeeListModel::data(realIndex, CategoryColumn::ID).toString();
278 if (m_overriddenCategories.find(id) != m_overriddenCategories.end())
279 return QVariant::fromValue(m_overriddenCategories[id]);
280 }
281 return QVariant::fromValue(getResults(realRow));
282 case RoleCount:
283 if (m_overriddenCategories.size() > 0)
284 {
285 auto id = DeeListModel::data(realIndex, CategoryColumn::ID).toString();
286 if (m_overriddenCategories.find(id) != m_overriddenCategories.end())
287 {
288 qDebug() << "Get count of overriden cat" << realRow << id << "at pos" << index.row() << "Count=" << m_overriddenCategories[id]->rowCount();
289 return QVariant::fromValue(m_overriddenCategories[id]->rowCount());
290 }
291 }
292 qDebug() << "Get count of cat" << DeeListModel::data(realIndex, CategoryColumn::ID).toString() << realRow << "at pos" << index.row() << "Count=" << getResults(realIndex.row())->rowCount();
293 return QVariant::fromValue(getResults(realRow)->rowCount());
294 case RoleCategoryIndex:
295 return QVariant::fromValue(index.row()); // no remapping to realIndex
296 default:
297 return QVariant();
298 }