on 6d80eaa help sign in
unity8/tests/mocks/Unity/fake_scopesoverview.cpp
281 lines 9.0 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;
109 artMap["aspect-ratio"] = "0.5";
110 artMap["field"] = "art";
111 map["art"] = artMap;
112 map["title"] = "HOLA";
113 return map;
114 }
115 case RoleResults:
116 return QVariant::fromValue(resultsModel);
117 case RoleCount:
118 return resultsModel->rowCount();
119 case RoleHeaderLink:
120 return QString();
121 default:
122 qFatal("Using un-implemented Categories role");
123 return QVariant();
124 }
129ScopesOverviewSearchCategories::ScopesOverviewSearchCategories(Scopes *scopes, QObject* parent)
130 : unity::shell::scopes::CategoriesInterface(parent)
131 , m_scopes(scopes)
135int ScopesOverviewSearchCategories::rowCount(const QModelIndex& /*parent*/) const
137 return 2;
140void ScopesOverviewSearchCategories::addSpecialCategory(QString const&, QString const&, QString const&, QString const&, QObject*)
142 qFatal("Using un-implemented ScopesOverviewSearchCategories::addSpecialCategory");
145bool ScopesOverviewSearchCategories::overrideCategoryJson(QString const& /* categoryId */, QString const& /* json */)
147 qFatal("Using un-implemented ScopesOverviewSearchCategories::overrideCategoryJson");
150QVariant
151ScopesOverviewSearchCategories::data(const QModelIndex& index, int role) const
153 if (!index.isValid()) {
154 return QVariant();
155 }
157 const QString categoryId = index.row() == 0 ? "searchA" : "searchB";
159 unity::shell::scopes::ResultsModelInterface *resultsModel = m_resultsModels[index.row()];
160 if (!resultsModel) {
161 QObject *that = const_cast<ScopesOverviewSearchCategories*>(this);
162 QList<Scope *> scopes;
163 if (index.row() == 0) {
164 scopes << m_scopes->getScopeFromAll("clickscope") << nullptr << m_scopes->getScopeFromAll("MockScope2");
165 } else {
166 scopes << nullptr << m_scopes->getScopeFromAll("MockScope7") << nullptr << m_scopes->getScopeFromAll("MockScope1");
167 }
168 resultsModel = new ScopesOverviewResultsModel(scopes, categoryId, that);
169 m_resultsModels[index.row()] = resultsModel;
170 }
171 switch (role) {
172 case RoleCategoryId:
173 return categoryId;
174 case RoleName:
175 return index.row() == 0 ? "SearchA" : "SearchB";
176 case RoleIcon:
177 return QVariant();
178 case RoleRawRendererTemplate:
179 qFatal("Using un-implemented RoleRawRendererTemplate Categories role");
180 return QVariant();
181 case RoleRenderer:
182 {
183 QVariantMap map;
184 map["category-layout"] = "grid";
185 map["card-size"] = "small";
186 map["overlay"] = true;
187 return map;
188 }
189 case RoleComponents:
190 {
191 QVariantMap map, artMap;
192 artMap["aspect-ratio"] = "1";
193 artMap["field"] = "art";
194 map["art"] = artMap;
195 map["title"] = "HOLA";
196 return map;
197 }
198 case RoleResults:
199 return QVariant::fromValue(resultsModel);
200 case RoleCount:
201 return resultsModel->rowCount();
202 case RoleHeaderLink:
203 return QString();
204 default:
205 qFatal("Using un-implemented Categories role");
206 return QVariant();
207 }
211ScopesOverviewResultsModel::ScopesOverviewResultsModel(const QList<Scope *> &scopes, const QString &categoryId, QObject* parent)
212 : unity::shell::scopes::ResultsModelInterface(parent)
213 , m_scopes(scopes)
214 , m_categoryId(categoryId)
218QString ScopesOverviewResultsModel::categoryId() const
220 return m_categoryId;
223void ScopesOverviewResultsModel::setCategoryId(QString const& /*id*/)
225 qFatal("Calling un-implemented ScopesOverviewResultsModel::setCategoryId");
228int ScopesOverviewResultsModel::scopeIndex(QString const& id) const
230 const int scopeCount = count();
231 for (int i = 0; i < scopeCount; ++i) {
232 if (m_scopes[i]->id() == id)
233 return i;
234 }
235 return -1;
238QHash<int, QByteArray> ScopesOverviewResultsModel::roleNames() const
240 QHash<int, QByteArray> roles = unity::shell::scopes::ResultsModelInterface::roleNames();
241 roles[RoleBackground + 1] = "scopeId";
242 return roles;
245int ScopesOverviewResultsModel::rowCount(const QModelIndex& parent) const
247 Q_UNUSED(parent);
249 return m_scopes.count();
252int ScopesOverviewResultsModel::count() const
254 return rowCount();
257QVariant
258ScopesOverviewResultsModel::data(const QModelIndex& index, int role) const
260 unity::shell::scopes::ScopeInterface *scope = m_scopes[index.row()];
261 switch (role) {
262 case RoleUri:
263 case RoleCategoryId:
264 case RoleDndUri:
265 return QString();
266 case RoleResult:
267 return scope ? scope->id() : QString("Result.%1.%2").arg(categoryId()).arg(index.row());
268 case RoleTitle:
269 return scope ? scope->name() : QString("Title.%1.%2").arg(categoryId()).arg(index.row());
270 case RoleArt:
271 return qmlDirectory() + "graphics/applicationIcons/dash.png";
272 case RoleMascot:
273 case RoleEmblem:
274 case RoleSummary:
275 case RoleBackground + 1: // scopeId
276 return scope ? scope->id() : nullptr;
277 break;
278 default:
279 return QVariant();
280 }