2 * Copyright (C) 2013 Canonical, Ltd.
5 * Michał Sawicz <michal.sawicz@canonical.com>
6 * Michal Hruby <michal.hruby@canonical.com>
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.
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.
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/>.
22#include "categories.h"
23#include "categoryresults.h"
29// TODO: use something from libunity once it's public
38Categories::Categories(QObject* parent)
39 : DeeListModel(parent)
41 m_roles[Categories::RoleCategoryId] = "categoryId";
42 m_roles[Categories::RoleName] = "name";
43 m_roles[Categories::RoleIcon] = "icon";
44 m_roles[Categories::RoleRenderer] = "renderer";
45 m_roles[Categories::RoleContentType] = "contentType";
46 m_roles[Categories::RoleRendererHint] = "rendererHint";
47 m_roles[Categories::RoleProgressSource] = "progressSource";
48 m_roles[Categories::RoleHints] = "hints";
49 m_roles[Categories::RoleResults] = "results";
50 m_roles[Categories::RoleCount] = "count";
52 // TODO This should not be needed but accumulatting the count changes
53 // makes the visualization more stable and also makes crashes on fast
54 // change of the search term harder to reproduce
55 m_timer.setSingleShot(true);
56 m_timer.setInterval(50);
57 connect(&m_timer, &QTimer::timeout, this, &Categories::onEmitCountChanged);
61Categories::getResults(int index) const
63 if (!m_results.contains(index)) {
64 CategoryResults* results = new CategoryResults(const_cast<Categories*>(this));
65 results->setCategoryIndex(index);
66 connect(results, &DeeListModel::countChanged, this, &Categories::onCountChanged);
68 unsigned categoryIndex = static_cast<unsigned>(index);
69 auto unity_results = m_unityScope->GetResultsForCategory(categoryIndex);
71 results->setModel(unity_results->model());
73 // No results model returned by unity core; this can be the case when the global
74 // results model of this scope is still not set in unity core. Don't set backend
75 // model in DeeListModel - it will still beahve properly as an empty model. Since
76 // we're connected to the category model change signal, and it is set by unity core
77 // at the same time as results model (on channel opening), we'll reset category
78 // results models with proper models when we're notifed again.
79 qWarning() << "No results model for category" << categoryIndex;
82 m_results.insert(index, results);
85 return m_results[index];
88void Categories::onCategoriesModelChanged(unity::glib::Object<DeeModel> model)
90 m_updatedCategories.clear();
91 // FIXME: this might destroy the renderer view and re-create it, optimize?
92 Q_FOREACH(DeeListModel* model, m_results) {
100void Categories::onCategoryOrderChanged(std::vector<unsigned int> cat_order)
102 for (unsigned int pos = 0; pos<cat_order.size(); pos++)
104 unsigned int cat = cat_order[pos];
105 const int old_pos = m_categoryOrder.indexOf(cat);
108 qWarning() << "No such category index:" << cat;
112 if (static_cast<int>(pos) != old_pos) {
113 int target_pos = pos;
114 const bool status = beginMoveRows(QModelIndex(), old_pos, old_pos, QModelIndex(), target_pos);
116 m_categoryOrder.move(old_pos, pos);
118 qWarning("beginMoveRows failed");
125Categories::setUnityScope(const unity::dash::Scope::Ptr& scope)
127 m_unityScope = scope;
129 // no need to call this, we'll get notified
130 //setModel(m_unityScope->categories()->model());
132 m_signals.disconnectAll();
133 m_signals << m_unityScope->categories()->model.changed.connect(sigc::mem_fun(this, &Categories::onCategoriesModelChanged));
134 m_signals << m_unityScope->category_order.changed.connect(sigc::mem_fun(this, &Categories::onCategoryOrderChanged));
138Categories::onCountChanged()
140 CategoryResults* results = qobject_cast<CategoryResults*>(sender());
142 if (!m_updatedCategories.contains(results->categoryIndex())) {
143 m_updatedCategories << results->categoryIndex();
149void Categories::onRowCountChanged()
151 QAbstractItemModel* model = qobject_cast<QAbstractItemModel*>(sender());
152 // find the corresponding category index
153 for (auto iter = m_overriddenCategories.begin(); iter != m_overriddenCategories.end(); ++iter) {
154 if (iter.value() == model) {
155 for (int i = 0; i < rowCount(); i++) {
156 auto id = data(index(i), RoleCategoryId).toString();
157 if (id != iter.key()) continue;
158 const int mappedIndex = m_categoryOrder.indexOf(i);
160 roles.append(RoleCount);
161 QModelIndex changedIndex = index(i);
162 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
170Categories::onEmitCountChanged()
173 roles.append(Categories::RoleCount);
174 Q_FOREACH(int categoryIndex, m_updatedCategories) {
175 if (!m_results.contains(categoryIndex)) continue;
176 const int realIndex = m_categoryOrder.indexOf(categoryIndex);
177 const QModelIndex changedIndex = index(realIndex);
178 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
180 m_updatedCategories.clear();
183QHash<int, QByteArray>
184Categories::roleNames() const
189void Categories::onOverrideModelDestroyed()
191 QObject* model = sender();
192 auto iter = m_overriddenCategories.begin();
193 while (iter != m_overriddenCategories.end()) {
194 if (iter.value() == model) {
195 iter = m_overriddenCategories.erase(iter);
202void Categories::overrideResults(const QString& categoryId, QAbstractItemModel* model)
204 m_overriddenCategories[categoryId] = model;
206 connect(model, &QObject::destroyed, this, &Categories::onOverrideModelDestroyed);
207 connect(model, &QAbstractItemModel::rowsInserted, this, &Categories::onRowCountChanged);
208 connect(model, &QAbstractItemModel::rowsRemoved, this, &Categories::onRowCountChanged);
209 connect(model, &QAbstractItemModel::modelReset, this, &Categories::onRowCountChanged);
211 // emit the dataChanged signal if the category is already in the model
212 for (int i = 0; i < rowCount(); i++) {
213 auto id = data(index(i), RoleCategoryId).toString();
214 if (id != categoryId) continue;
216 roles.append(RoleCount);
217 roles.append(RoleResults);
218 QModelIndex changedIndex = index(i);
219 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
225Categories::data(const QModelIndex& index, int role) const
227 if (!index.isValid()) {
231 if (m_categoryOrder.size() != rowCount())
233 // populate category order vector with 0..n
234 m_categoryOrder.clear();
235 const unsigned int lim = rowCount();
236 for (unsigned int i = 0; i<lim; i++) {
237 m_categoryOrder.append(i);
241 int realRow = index.row();
242 if (index.row() < m_categoryOrder.size()) {
243 realRow = m_categoryOrder[index.row()];
245 qWarning() << "Invalid index" << index.row();
249 const QModelIndex realIndex = createIndex(realRow, index.column());
253 return DeeListModel::data(realIndex, CategoryColumn::ID);
255 return DeeListModel::data(realIndex, CategoryColumn::DISPLAY_NAME);
257 return DeeListModel::data(realIndex, CategoryColumn::ICON_HINT);
259 return DeeListModel::data(realIndex, CategoryColumn::RENDERER_NAME);
260 case RoleContentType:
262 auto hints = DeeListModel::data(realIndex, CategoryColumn::HINTS).toHash();
263 return hints.contains("content-type") ? hints["content-type"] : QVariant(QString("default"));
265 case RoleRendererHint:
267 auto hints = DeeListModel::data(index, CategoryColumn::HINTS).toHash();
268 return hints.contains("renderer-hint") ? hints["renderer-hint"] : QVariant(QString());
270 case RoleProgressSource:
272 auto hints = DeeListModel::data(realIndex, CategoryColumn::HINTS).toHash();
273 return hints.contains("progress-source") ? hints["progress-source"] : QVariant();
276 return DeeListModel::data(realIndex, CategoryColumn::HINTS);
278 if (m_overriddenCategories.size() > 0)
280 auto id = DeeListModel::data(realIndex, CategoryColumn::ID).toString();
281 if (m_overriddenCategories.find(id) != m_overriddenCategories.end())
282 return QVariant::fromValue(m_overriddenCategories[id]);
284 return QVariant::fromValue(getResults(realRow));
286 if (m_overriddenCategories.size() > 0)
288 auto id = DeeListModel::data(realIndex, CategoryColumn::ID).toString();
289 if (m_overriddenCategories.find(id) != m_overriddenCategories.end())
291 return QVariant::fromValue(m_overriddenCategories[id]->rowCount());
294 return QVariant::fromValue(getResults(realRow)->rowCount());
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list