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::RoleProgressSource] = "progressSource";
44 m_roles[Categories::RoleHints] = "hints";
45 m_roles[Categories::RoleResults] = "results";
46 m_roles[Categories::RoleCount] = "count";
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);
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);
67 results->setModel(unity_results->model());
69 // No results model returned by unity core; this can be the case when the global
70 // results model of this scope became invalid in unity core. Don't set backend
71 // model in DeeListModel - it will still beahve properly as an empty model. Since
72 // we're connected to the results model change signal, we'll reset category models
73 // when results model is set again.
74 qWarning() << "No results model for category" << categoryIndex;
77 m_results.insert(index, results);
80 return m_results[index];
83void Categories::onCategoriesModelChanged(unity::glib::Object<DeeModel> model)
85 m_updatedCategories.clear();
86 // FIXME: this might destroy the renderer view and re-create it, optimize?
87 Q_FOREACH(DeeListModel* model, m_results) {
95void Categories::onScopeResultsModelChanged(unity::glib::Object<DeeModel> /* model */)
97 // if scope results model changes, we need to reset all category models
98 onCategoriesModelChanged(m_unityScope->categories()->model());
102Categories::setUnityScope(const unity::dash::Scope::Ptr& scope)
104 m_unityScope = scope;
106 // no need to call this, we'll get notified
107 //setModel(m_unityScope->categories()->model());
109 m_categoriesChangedConnection.disconnect();
110 m_categoriesChangedConnection =
111 m_unityScope->categories()->model.changed.connect(sigc::mem_fun(this, &Categories::onCategoriesModelChanged));
113 m_resultshangedConnection.disconnect();
114 m_resultshangedConnection = m_unityScope->results()->model.changed.connect(sigc::mem_fun(this, &Categories::onScopeResultsModelChanged));
118Categories::onCountChanged()
120 CategoryResults* results = qobject_cast<CategoryResults*>(sender());
122 m_updatedCategories << results->categoryIndex();
127void Categories::onRowCountChanged()
129 QAbstractItemModel* model = qobject_cast<QAbstractItemModel*>(sender());
130 // find the corresponding category index
131 for (auto iter = m_overriddenCategories.begin(); iter != m_overriddenCategories.end(); ++iter) {
132 if (iter.value() == model) {
133 for (int i = 0; i < rowCount(); i++) {
134 auto id = data(index(i), RoleCategoryId).toString();
135 if (id != iter.key()) continue;
137 roles.append(RoleCount);
138 QModelIndex changedIndex = index(i);
139 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
147Categories::onEmitCountChanged()
150 roles.append(Categories::RoleCount);
151 Q_FOREACH(int categoryIndex, m_updatedCategories) {
152 if (!m_results.contains(categoryIndex)) continue;
153 QModelIndex changedIndex = index(categoryIndex);
154 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
156 m_updatedCategories.clear();
159QHash<int, QByteArray>
160Categories::roleNames() const
165void Categories::onOverrideModelDestroyed()
167 QObject* model = sender();
168 auto iter = m_overriddenCategories.begin();
169 while (iter != m_overriddenCategories.end()) {
170 if (iter.value() == model) {
171 iter = m_overriddenCategories.erase(iter);
178void Categories::overrideResults(const QString& categoryId, QAbstractItemModel* model)
180 m_overriddenCategories[categoryId] = model;
182 connect(model, &QObject::destroyed, this, &Categories::onOverrideModelDestroyed);
183 connect(model, &QAbstractItemModel::rowsInserted, this, &Categories::onRowCountChanged);
184 connect(model, &QAbstractItemModel::rowsRemoved, this, &Categories::onRowCountChanged);
185 connect(model, &QAbstractItemModel::modelReset, this, &Categories::onRowCountChanged);
187 // emit the dataChanged signal if the category is already in the model
188 for (int i = 0; i < rowCount(); i++) {
189 auto id = data(index(i), RoleCategoryId).toString();
190 if (id != categoryId) continue;
192 roles.append(RoleCount);
193 roles.append(RoleResults);
194 QModelIndex changedIndex = index(i);
195 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
201Categories::data(const QModelIndex& index, int role) const
203 if (!index.isValid()) {
209 return DeeListModel::data(index, CategoryColumn::ID);
211 return DeeListModel::data(index, CategoryColumn::DISPLAY_NAME);
213 return DeeListModel::data(index, CategoryColumn::ICON_HINT);
215 return DeeListModel::data(index, CategoryColumn::RENDERER_NAME);
216 case RoleContentType:
218 auto hints = DeeListModel::data(index, CategoryColumn::HINTS).toHash();
219 return hints.contains("content-type") ? hints["content-type"] : QVariant(QString("default"));
221 case RoleProgressSource:
223 auto hints = DeeListModel::data(index, CategoryColumn::HINTS).toHash();
224 return hints.contains("progress-source") ? hints["progress-source"] : QVariant();
227 return DeeListModel::data(index, CategoryColumn::HINTS);
229 if (m_overriddenCategories.size() > 0)
231 auto id = DeeListModel::data(index, CategoryColumn::ID).toString();
232 if (m_overriddenCategories.find(id) != m_overriddenCategories.end())
233 return QVariant::fromValue(m_overriddenCategories[id]);
235 return QVariant::fromValue(getResults(index.row()));
237 if (m_overriddenCategories.size() > 0)
239 auto id = DeeListModel::data(index, CategoryColumn::ID).toString();
240 if (m_overriddenCategories.find(id) != m_overriddenCategories.end())
241 return QVariant::fromValue(m_overriddenCategories[id]->rowCount());
243 return QVariant::fromValue(getResults(index.row())->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