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"
25// TODO: use something from libunity once it's public
34Categories::Categories(QObject* parent)
35 : DeeListModel(parent)
37 m_roles[Categories::RoleCategoryId] = "categoryId";
38 m_roles[Categories::RoleName] = "name";
39 m_roles[Categories::RoleIcon] = "icon";
40 m_roles[Categories::RoleRenderer] = "renderer";
41 m_roles[Categories::RoleContentType] = "contentType";
42 m_roles[Categories::RoleProgressSource] = "progressSource";
43 m_roles[Categories::RoleHints] = "hints";
44 m_roles[Categories::RoleResults] = "results";
45 m_roles[Categories::RoleCount] = "count";
47 // TODO This should not be needed but accumulatting the count changes
48 // makes the visualization more stable and also makes crashes on fast
49 // change of the search term harder to reproduce
50 m_timer.setSingleShot(true);
51 m_timer.setInterval(50);
52 connect(&m_timer, &QTimer::timeout, this, &Categories::onEmitCountChanged);
56Categories::getResults(int index) const
58 if (!m_results.contains(index)) {
59 CategoryResults* results = new CategoryResults(const_cast<Categories*>(this));
60 results->setCategoryIndex(index);
61 connect(results, &DeeListModel::countChanged, this, &Categories::onCountChanged);
63 unsigned categoryIndex = static_cast<unsigned>(index);
64 auto unity_results = m_unityScope->GetResultsForCategory(categoryIndex);
65 results->setModel(unity_results->model());
67 m_results.insert(index, results);
70 return m_results[index];
73void Categories::onCategoriesModelChanged(unity::glib::Object<DeeModel> model)
75 m_updatedCategories.clear();
76 // FIXME: this might destroy the renderer view and re-create it, optimize?
77 Q_FOREACH(DeeListModel* model, m_results) {
85Categories::setUnityScope(const unity::dash::Scope::Ptr& scope)
89 // no need to call this, we'll get notified
90 //setModel(m_unityScope->categories()->model());
92 m_categoriesChangedConnection.disconnect();
93 m_categoriesChangedConnection =
94 m_unityScope->categories()->model.changed.connect(sigc::mem_fun(this, &Categories::onCategoriesModelChanged));
98Categories::onCountChanged()
100 CategoryResults* results = qobject_cast<CategoryResults*>(sender());
102 m_updatedCategories << results->categoryIndex();
107void Categories::onRowCountChanged()
109 QAbstractItemModel* model = qobject_cast<QAbstractItemModel*>(sender());
110 // find the corresponding category index
111 for (auto iter = m_overriddenCategories.begin(); iter != m_overriddenCategories.end(); ++iter) {
112 if (iter.value() == model) {
113 for (int i = 0; i < rowCount(); i++) {
114 auto id = data(index(i), RoleCategoryId).toString();
115 if (id != iter.key()) continue;
117 roles.append(RoleCount);
118 QModelIndex changedIndex = index(i);
119 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
127Categories::onEmitCountChanged()
130 roles.append(Categories::RoleCount);
131 Q_FOREACH(int categoryIndex, m_updatedCategories) {
132 if (!m_results.contains(categoryIndex)) continue;
133 QModelIndex changedIndex = index(categoryIndex);
134 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
136 m_updatedCategories.clear();
139QHash<int, QByteArray>
140Categories::roleNames() const
145void Categories::onOverrideModelDestroyed()
147 QObject* model = sender();
148 auto iter = m_overriddenCategories.begin();
149 while (iter != m_overriddenCategories.end()) {
150 if (iter.value() == model) {
151 iter = m_overriddenCategories.erase(iter);
158void Categories::overrideResults(const QString& categoryId, QAbstractItemModel* model)
160 m_overriddenCategories[categoryId] = model;
162 connect(model, &QObject::destroyed, this, &Categories::onOverrideModelDestroyed);
163 connect(model, &QAbstractItemModel::rowsInserted, this, &Categories::onRowCountChanged);
164 connect(model, &QAbstractItemModel::rowsRemoved, this, &Categories::onRowCountChanged);
165 connect(model, &QAbstractItemModel::modelReset, this, &Categories::onRowCountChanged);
167 // emit the dataChanged signal if the category is already in the model
168 for (int i = 0; i < rowCount(); i++) {
169 auto id = data(index(i), RoleCategoryId).toString();
170 if (id != categoryId) continue;
172 roles.append(RoleCount);
173 roles.append(RoleResults);
174 QModelIndex changedIndex = index(i);
175 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
181Categories::data(const QModelIndex& index, int role) const
183 if (!index.isValid()) {
189 return DeeListModel::data(index, CategoryColumn::ID);
191 return DeeListModel::data(index, CategoryColumn::DISPLAY_NAME);
193 return DeeListModel::data(index, CategoryColumn::ICON_HINT);
195 return DeeListModel::data(index, CategoryColumn::RENDERER_NAME);
196 case RoleContentType:
198 auto hints = DeeListModel::data(index, CategoryColumn::HINTS).toHash();
199 return hints.contains("content-type") ? hints["content-type"] : QVariant(QString("default"));
201 case RoleProgressSource:
203 auto hints = DeeListModel::data(index, CategoryColumn::HINTS).toHash();
204 return hints.contains("progress-source") ? hints["progress-source"] : QVariant();
207 return DeeListModel::data(index, CategoryColumn::HINTS);
209 if (m_overriddenCategories.size() > 0)
211 auto id = DeeListModel::data(index, CategoryColumn::ID).toString();
212 if (m_overriddenCategories.find(id) != m_overriddenCategories.end())
213 return QVariant::fromValue(m_overriddenCategories[id]);
215 return QVariant::fromValue(getResults(index.row()));
217 if (m_overriddenCategories.size() > 0)
219 auto id = DeeListModel::data(index, CategoryColumn::ID).toString();
220 if (m_overriddenCategories.find(id) != m_overriddenCategories.end())
221 return QVariant::fromValue(m_overriddenCategories[id]->rowCount());
223 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