on bc9befc help sign in
unity8/tests/mocks/Unity/fake_scopesoverview.cpp
276 lines 8.9 kB history raw
1/*
2 * Copyright (C) 2014 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#include "fake_scopesoverview.h"
19#include "fake_scopes.h"
21#include <paths.h>
23ScopesOverview::ScopesOverview(Scopes* parent)
24 : Scope("scopesOverview", "Scopes Overview", false, parent)
26 delete m_categories; // delete the usual categories, we're not going to use it
27 m_scopesOverviewCategories = new ScopesOverviewCategories(parent, this);
28 m_searchCategories = new ScopesOverviewSearchCategories(parent, this);
29 m_categories = m_scopesOverviewCategories;
32void ScopesOverview::setSearchQuery(const QString& search_query)
34 Scope::setSearchQuery(search_query);
36 auto origCategories = m_categories;
37 if (search_query.isEmpty()) m_categories = m_scopesOverviewCategories;
38 else m_categories = m_searchCategories;
40 if (m_categories != origCategories)
41 Q_EMIT categoriesChanged();
44Q_INVOKABLE void ScopesOverview::activate(QVariant const& result)
46 Scopes *scopes = dynamic_cast<Scopes*>(parent());
47 m_openScope = scopes->getScopeFromAll(result.toString());
48 Q_EMIT openScope(m_openScope);
51ScopesOverviewCategories::ScopesOverviewCategories(Scopes *scopes, QObject* parent)
52 : unity::shell::scopes::CategoriesInterface(parent)
53 , m_scopes(scopes)
57int ScopesOverviewCategories::rowCount(const QModelIndex& /*parent*/) const
59 return 2;
62void ScopesOverviewCategories::addSpecialCategory(QString const&, QString const&, QString const&, QString const&, QObject*)
64 qFatal("Using un-implemented ScopesOverviewCategories::addSpecialCategory");
67bool ScopesOverviewCategories::overrideCategoryJson(QString const& /* categoryId */, QString const& /* json */)
69 qFatal("Using un-implemented ScopesOverviewCategories::overrideCategoryJson");
72QVariant
73ScopesOverviewCategories::data(const QModelIndex& index, int role) const
75 if (!index.isValid()) {
76 return QVariant();
77 }
79 const QString categoryId = index.row() == 0 ? "favorites" : "all";
81 unity::shell::scopes::ResultsModelInterface *resultsModel = m_resultsModels[index.row()];
82 if (!resultsModel) {
83 QObject *that = const_cast<ScopesOverviewCategories*>(this);
84 resultsModel = new ScopesOverviewResultsModel(m_scopes->scopes(index.row() == 0), categoryId, that);
85 m_resultsModels[index.row()] = resultsModel;
86 }
87 switch (role) {
88 case RoleCategoryId:
89 return categoryId;
90 case RoleName:
91 return index.row() == 0 ? "Favorites" : "All";
92 case RoleIcon:
93 return QVariant();
94 case RoleRawRendererTemplate:
95 qFatal("Using un-implemented RoleRawRendererTemplate Categories role");
96 return QVariant();
97 case RoleRenderer:
98 {
99 QVariantMap map;
100 map["category-layout"] = "grid";
101 map["card-size"] = "small";
102 map["overlay"] = true;
103 return map;
104 }
105 case RoleComponents:
106 {
107 QVariantMap map, artMap;
108 artMap["aspect-ratio"] = "0.5";
109 artMap["field"] = "art";
110 map["art"] = artMap;
111 map["title"] = "HOLA";
112 return map;
113 }
114 case RoleResults:
115 return QVariant::fromValue(resultsModel);
116 case RoleCount:
117 return resultsModel->rowCount();
118 default:
119 qFatal("Using un-implemented Categories role");
120 return QVariant();
121 }
126ScopesOverviewSearchCategories::ScopesOverviewSearchCategories(Scopes *scopes, QObject* parent)
127 : unity::shell::scopes::CategoriesInterface(parent)
128 , m_scopes(scopes)
132int ScopesOverviewSearchCategories::rowCount(const QModelIndex& /*parent*/) const
134 return 2;
137void ScopesOverviewSearchCategories::addSpecialCategory(QString const&, QString const&, QString const&, QString const&, QObject*)
139 qFatal("Using un-implemented ScopesOverviewSearchCategories::addSpecialCategory");
142bool ScopesOverviewSearchCategories::overrideCategoryJson(QString const& /* categoryId */, QString const& /* json */)
144 qFatal("Using un-implemented ScopesOverviewSearchCategories::overrideCategoryJson");
147QVariant
148ScopesOverviewSearchCategories::data(const QModelIndex& index, int role) const
150 if (!index.isValid()) {
151 return QVariant();
152 }
154 const QString categoryId = index.row() == 0 ? "searchA" : "searchB";
156 unity::shell::scopes::ResultsModelInterface *resultsModel = m_resultsModels[index.row()];
157 if (!resultsModel) {
158 QObject *that = const_cast<ScopesOverviewSearchCategories*>(this);
159 QList<unity::shell::scopes::ScopeInterface *> scopes;
160 if (index.row() == 0) {
161 scopes << m_scopes->getScopeFromAll("clickscope") << nullptr << m_scopes->getScopeFromAll("MockScope2");
162 } else {
163 scopes << nullptr << m_scopes->getScopeFromAll("MockScope7") << nullptr << m_scopes->getScopeFromAll("MockScope1");
164 }
165 resultsModel = new ScopesOverviewResultsModel(scopes, categoryId, that);
166 m_resultsModels[index.row()] = resultsModel;
167 }
168 switch (role) {
169 case RoleCategoryId:
170 return categoryId;
171 case RoleName:
172 return index.row() == 0 ? "SearchA" : "SearchB";
173 case RoleIcon:
174 return QVariant();
175 case RoleRawRendererTemplate:
176 qFatal("Using un-implemented RoleRawRendererTemplate Categories role");
177 return QVariant();
178 case RoleRenderer:
179 {
180 QVariantMap map;
181 map["category-layout"] = "grid";
182 map["card-size"] = "small";
183 map["overlay"] = true;
184 return map;
185 }
186 case RoleComponents:
187 {
188 QVariantMap map, artMap;
189 artMap["aspect-ratio"] = "1";
190 artMap["field"] = "art";
191 map["art"] = artMap;
192 map["title"] = "HOLA";
193 return map;
194 }
195 case RoleResults:
196 return QVariant::fromValue(resultsModel);
197 case RoleCount:
198 return resultsModel->rowCount();
199 default:
200 qFatal("Using un-implemented Categories role");
201 return QVariant();
202 }
206ScopesOverviewResultsModel::ScopesOverviewResultsModel(const QList<unity::shell::scopes::ScopeInterface *> &scopes, const QString &categoryId, QObject* parent)
207 : unity::shell::scopes::ResultsModelInterface(parent)
208 , m_scopes(scopes)
209 , m_categoryId(categoryId)
213QString ScopesOverviewResultsModel::categoryId() const
215 return m_categoryId;
218void ScopesOverviewResultsModel::setCategoryId(QString const& /*id*/)
220 qFatal("Calling un-implemented ScopesOverviewResultsModel::setCategoryId");
223int ScopesOverviewResultsModel::scopeIndex(QString const& id) const
225 const int scopeCount = count();
226 for (int i = 0; i < scopeCount; ++i) {
227 if (m_scopes[i]->id() == id)
228 return i;
229 }
230 return -1;
233QHash<int, QByteArray> ScopesOverviewResultsModel::roleNames() const
235 QHash<int, QByteArray> roles = unity::shell::scopes::ResultsModelInterface::roleNames();
236 roles[RoleBackground + 1] = "scopeId";
237 return roles;
240int ScopesOverviewResultsModel::rowCount(const QModelIndex& parent) const
242 Q_UNUSED(parent);
244 return m_scopes.count();
247int ScopesOverviewResultsModel::count() const
249 return rowCount();
252QVariant
253ScopesOverviewResultsModel::data(const QModelIndex& index, int role) const
255 unity::shell::scopes::ScopeInterface *scope = m_scopes[index.row()];
256 switch (role) {
257 case RoleUri:
258 case RoleCategoryId:
259 case RoleDndUri:
260 return QString();
261 case RoleResult:
262 return scope ? scope->id() : QString("Result.%1.%2").arg(categoryId()).arg(index.row());
263 case RoleTitle:
264 return scope ? scope->name() : QString("Title.%1.%2").arg(categoryId()).arg(index.row());
265 case RoleArt:
266 return qmlDirectory() + "graphics/applicationIcons/dash.png";
267 case RoleMascot:
268 case RoleEmblem:
269 case RoleSummary:
270 case RoleBackground + 1: // scopeId
271 return scope ? scope->id() : nullptr;
272 break;
273 default:
274 return QVariant();
275 }