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"
26// TODO: use something from libunity once it's public
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::RoleRendererHint] = "rendererHint";
44 m_roles[Categories::RoleProgressSource] = "progressSource";
45 m_roles[Categories::RoleHints] = "hints";
46 m_roles[Categories::RoleResults] = "results";
47 m_roles[Categories::RoleCount] = "count";
49 // TODO This should not be needed but accumulatting the count changes
50 // makes the visualization more stable and also makes crashes on fast
51 // change of the search term harder to reproduce
52 m_timer.setSingleShot(true);
53 m_timer.setInterval(50);
54 connect(&m_timer, &QTimer::timeout, this, &Categories::onEmitCountChanged);
58Categories::getResults(int index) const
60 if (!m_results.contains(index)) {
61 CategoryResults* results = new CategoryResults(const_cast<Categories*>(this));
62 results->setCategoryIndex(index);
63 connect(results, &DeeListModel::countChanged, this, &Categories::onCountChanged);
65 unsigned categoryIndex = static_cast<unsigned>(index);
66 auto unity_results = m_unityScope->GetResultsForCategory(categoryIndex);
68 results->setModel(unity_results->model());
70 // No results model returned by unity core; this can be the case when the global
71 // results model of this scope is still not set in unity core. Don't set backend
72 // model in DeeListModel - it will still beahve properly as an empty model. Since
73 // we're connected to the category model change signal, and it is set by unity core
74 // at the same time as results model (on channel opening), we'll reset category
75 // results models with proper models when we're notifed again.
76 qWarning() << "No results model for category" << categoryIndex;
79 m_results.insert(index, results);
82 return m_results[index];
85void Categories::onCategoriesModelChanged(unity::glib::Object<DeeModel> model)
87 m_updatedCategories.clear();
88 // FIXME: this might destroy the renderer view and re-create it, optimize?
89 Q_FOREACH(DeeListModel* model, m_results) {
93 m_categoryOrder.clear();
98void Categories::onCategoryOrderChanged(const std::vector<unsigned int>& cat_order)
100 for (unsigned int pos = 0; pos<cat_order.size(); pos++)
102 unsigned int cat = cat_order[pos];
103 const int old_pos = m_categoryOrder.indexOf(cat);
106 qWarning() << "No such category index:" << cat;
110 if (static_cast<int>(pos) != old_pos) {
111 int target_pos = pos;
112 if (target_pos > old_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:" << old_pos << target_pos;
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));
135 // FIXME: Don't handle category order changes for now as it causes UI issues (https://bugs.launchpad.net/unity8/+bug/1239584).
136 //m_signals << m_unityScope->category_order.changed.connect(sigc::mem_fun(this, &Categories::onCategoryOrderChanged));
140Categories::onCountChanged()
142 CategoryResults* results = qobject_cast<CategoryResults*>(sender());
144 if (!m_updatedCategories.contains(results->categoryIndex())) {
145 m_updatedCategories << results->categoryIndex();
151void Categories::onRowCountChanged()
153 QAbstractItemModel* model = qobject_cast<QAbstractItemModel*>(sender());
154 // find the corresponding category index
155 for (auto iter = m_overriddenCategories.begin(); iter != m_overriddenCategories.end(); ++iter) {
156 if (iter.value() == model) {
157 for (int i = 0; i < rowCount(); i++) {
158 auto id = data(index(i), RoleCategoryId).toString();
159 if (id != iter.key()) continue;
161 roles.append(RoleCount);
162 QModelIndex changedIndex = index(i);
163 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
171Categories::onEmitCountChanged()
174 roles.append(Categories::RoleCount);
175 Q_FOREACH(int categoryIndex, m_updatedCategories) {
176 if (!m_results.contains(categoryIndex)) continue;
177 const int realIndex = m_categoryOrder.indexOf(categoryIndex);
178 const QModelIndex changedIndex = index(realIndex);
179 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
181 m_updatedCategories.clear();
184QHash<int, QByteArray>
185Categories::roleNames() const
190void Categories::onOverrideModelDestroyed()
192 QObject* model = sender();
193 auto iter = m_overriddenCategories.begin();
194 while (iter != m_overriddenCategories.end()) {
195 if (iter.value() == model) {
196 iter = m_overriddenCategories.erase(iter);
203void Categories::overrideResults(const QString& categoryId, QAbstractItemModel* model)
205 m_overriddenCategories[categoryId] = model;
207 connect(model, &QObject::destroyed, this, &Categories::onOverrideModelDestroyed);
208 connect(model, &QAbstractItemModel::rowsInserted, this, &Categories::onRowCountChanged);
209 connect(model, &QAbstractItemModel::rowsRemoved, this, &Categories::onRowCountChanged);
210 connect(model, &QAbstractItemModel::modelReset, this, &Categories::onRowCountChanged);
212 // emit the dataChanged signal if the category is already in the model
213 for (int i = 0; i < rowCount(); i++) {
214 auto id = data(index(i), RoleCategoryId).toString();
215 if (id != categoryId) continue;
217 roles.append(RoleCount);
218 roles.append(RoleResults);
219 QModelIndex changedIndex = index(i);
220 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
226Categories::data(const QModelIndex& index, int role) const
228 if (!index.isValid()) {
232 if (m_categoryOrder.size() != rowCount())
234 // populate category order vector with 0..n
235 m_categoryOrder.clear();
236 const unsigned int lim = rowCount();
237 for (unsigned int i = 0; i<lim; i++) {
238 m_categoryOrder.append(i);
242 int realRow = index.row();
243 if (index.row() < m_categoryOrder.size()) {
244 realRow = m_categoryOrder[index.row()];
246 qWarning() << "Invalid index" << index.row();
250 const QModelIndex realIndex = createIndex(realRow, index.column());
254 return DeeListModel::data(realIndex, CategoryColumn::ID);
256 return DeeListModel::data(realIndex, CategoryColumn::DISPLAY_NAME);
258 return DeeListModel::data(realIndex, CategoryColumn::ICON_HINT);
260 return DeeListModel::data(realIndex, CategoryColumn::RENDERER_NAME);
261 case RoleContentType:
263 auto hints = DeeListModel::data(realIndex, CategoryColumn::HINTS).toHash();
264 return hints.contains("content-type") ? hints["content-type"] : QVariant(QString("default"));
266 case RoleRendererHint:
268 auto hints = DeeListModel::data(realIndex, CategoryColumn::HINTS).toHash();
269 return hints.contains("renderer-hint") ? hints["renderer-hint"] : QVariant(QString());
271 case RoleProgressSource:
273 auto hints = DeeListModel::data(realIndex, CategoryColumn::HINTS).toHash();
274 return hints.contains("progress-source") ? hints["progress-source"] : QVariant();
277 return DeeListModel::data(realIndex, CategoryColumn::HINTS);
279 if (m_overriddenCategories.size() > 0)
281 auto id = DeeListModel::data(realIndex, CategoryColumn::ID).toString();
282 if (m_overriddenCategories.find(id) != m_overriddenCategories.end())
283 return QVariant::fromValue(m_overriddenCategories[id]);
285 return QVariant::fromValue(getResults(realRow));
287 if (m_overriddenCategories.size() > 0)
289 auto id = DeeListModel::data(realIndex, CategoryColumn::ID).toString();
290 if (m_overriddenCategories.find(id) != m_overriddenCategories.end())
292 return QVariant::fromValue(m_overriddenCategories[id]->rowCount());
295 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