on 8efbd79 help sign in
unity8/tests/mocks/Unity/fake_scopesoverview.cpp
283 lines 9.1 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 QList<Scope*> scopes = index.row() == 0 ? m_scopes->scopes() : m_scopes->allScopes();
85 resultsModel = new ScopesOverviewResultsModel(scopes, categoryId, that);
86 m_resultsModels[index.row()] = resultsModel;
87 }
88 switch (role) {
89 case RoleCategoryId:
90 return categoryId;
91 case RoleName:
92 return index.row() == 0 ? "Favorites" : "All";
93 case RoleIcon:
94 return QVariant();
95 case RoleRawRendererTemplate:
96 qFatal("Using un-implemented RoleRawRendererTemplate Categories role");
97 return QVariant();
98 case RoleRenderer:
99 {
100 QVariantMap map;
101 map["category-layout"] = "grid";
102 map["card-size"] = "small";
103 map["overlay"] = true;
104 return map;
105 }
106 case RoleComponents:
107 {
108 QVariantMap map, artMap, attributesMap;
109 artMap["aspect-ratio"] = "0.5";
110 artMap["field"] = "art";
111 map["art"] = artMap;
112 map["title"] = "HOLA";
113 map["attributes"] = attributesMap;
114 return map;
115 }
116 case RoleResults:
117 return QVariant::fromValue(resultsModel);
118 case RoleCount:
119 return resultsModel->rowCount();
120 case RoleHeaderLink:
121 return QString();
122 default:
123 qFatal("Using un-implemented Categories role");
124 return QVariant();
125 }
130ScopesOverviewSearchCategories::ScopesOverviewSearchCategories(Scopes *scopes, QObject* parent)
131 : unity::shell::scopes::CategoriesInterface(parent)
132 , m_scopes(scopes)
136int ScopesOverviewSearchCategories::rowCount(const QModelIndex& /*parent*/) const
138 return 2;
141void ScopesOverviewSearchCategories::addSpecialCategory(QString const&, QString const&, QString const&, QString const&, QObject*)
143 qFatal("Using un-implemented ScopesOverviewSearchCategories::addSpecialCategory");
146bool ScopesOverviewSearchCategories::overrideCategoryJson(QString const& /* categoryId */, QString const& /* json */)
148 qFatal("Using un-implemented ScopesOverviewSearchCategories::overrideCategoryJson");
151QVariant
152ScopesOverviewSearchCategories::data(const QModelIndex& index, int role) const
154 if (!index.isValid()) {
155 return QVariant();
156 }
158 const QString categoryId = index.row() == 0 ? "searchA" : "searchB";
160 unity::shell::scopes::ResultsModelInterface *resultsModel = m_resultsModels[index.row()];
161 if (!resultsModel) {
162 QObject *that = const_cast<ScopesOverviewSearchCategories*>(this);
163 QList<Scope *> scopes;
164 if (index.row() == 0) {
165 scopes << m_scopes->getScopeFromAll("clickscope") << nullptr << m_scopes->getScopeFromAll("MockScope2");
166 } else {
167 scopes << nullptr << m_scopes->getScopeFromAll("MockScope7") << nullptr << m_scopes->getScopeFromAll("MockScope1");
168 }
169 resultsModel = new ScopesOverviewResultsModel(scopes, categoryId, that);
170 m_resultsModels[index.row()] = resultsModel;
171 }
172 switch (role) {
173 case RoleCategoryId:
174 return categoryId;
175 case RoleName:
176 return index.row() == 0 ? "SearchA" : "SearchB";
177 case RoleIcon:
178 return QVariant();
179 case RoleRawRendererTemplate:
180 qFatal("Using un-implemented RoleRawRendererTemplate Categories role");
181 return QVariant();
182 case RoleRenderer:
183 {
184 QVariantMap map;
185 map["category-layout"] = "grid";
186 map["card-size"] = "small";
187 map["overlay"] = true;
188 return map;
189 }
190 case RoleComponents:
191 {
192 QVariantMap map, artMap, attributesMap;
193 artMap["aspect-ratio"] = "1";
194 artMap["field"] = "art";
195 map["art"] = artMap;
196 map["title"] = "HOLA";
197 map["attributes"] = attributesMap;
198 return map;
199 }
200 case RoleResults:
201 return QVariant::fromValue(resultsModel);
202 case RoleCount:
203 return resultsModel->rowCount();
204 case RoleHeaderLink:
205 return QString();
206 default:
207 qFatal("Using un-implemented Categories role");
208 return QVariant();
209 }
213ScopesOverviewResultsModel::ScopesOverviewResultsModel(const QList<Scope *> &scopes, const QString &categoryId, QObject* parent)
214 : unity::shell::scopes::ResultsModelInterface(parent)
215 , m_scopes(scopes)
216 , m_categoryId(categoryId)
220QString ScopesOverviewResultsModel::categoryId() const
222 return m_categoryId;
225void ScopesOverviewResultsModel::setCategoryId(QString const& /*id*/)
227 qFatal("Calling un-implemented ScopesOverviewResultsModel::setCategoryId");
230int ScopesOverviewResultsModel::scopeIndex(QString const& id) const
232 const int scopeCount = count();
233 for (int i = 0; i < scopeCount; ++i) {
234 if (m_scopes[i]->id() == id)
235 return i;
236 }
237 return -1;
240QHash<int, QByteArray> ScopesOverviewResultsModel::roleNames() const
242 QHash<int, QByteArray> roles = unity::shell::scopes::ResultsModelInterface::roleNames();
243 roles[RoleBackground + 1] = "scopeId";
244 return roles;
247int ScopesOverviewResultsModel::rowCount(const QModelIndex& parent) const
249 Q_UNUSED(parent);
251 return m_scopes.count();
254int ScopesOverviewResultsModel::count() const
256 return rowCount();
259QVariant
260ScopesOverviewResultsModel::data(const QModelIndex& index, int role) const
262 unity::shell::scopes::ScopeInterface *scope = m_scopes[index.row()];
263 switch (role) {
264 case RoleUri:
265 case RoleCategoryId:
266 case RoleDndUri:
267 return QString();
268 case RoleResult:
269 return scope ? scope->id() : QString("Result.%1.%2").arg(categoryId()).arg(index.row());
270 case RoleTitle:
271 return scope ? scope->name() : QString("Title.%1.%2").arg(categoryId()).arg(index.row());
272 case RoleArt:
273 return qmlDirectory() + "graphics/applicationIcons/dash.png";
274 case RoleMascot:
275 case RoleEmblem:
276 case RoleSummary:
277 case RoleBackground + 1: // scopeId
278 return scope ? scope->id() : nullptr;
279 break;
280 default:
281 return QVariant();
282 }