on baa4162 help sign in
unity8/tests/mocks/Unity/fake_scopesoverview.cpp
349 lines 11 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 if (scopes->getScope(result.toString())) {
48 Q_EMIT gotoScope(result.toString());
49 } else {
50 m_openScope = scopes->getScopeFromAll(result.toString());
51 Q_EMIT openScope(m_openScope);
52 }
55void ScopesOverview::setFavorite(Scope *scope, bool favorite)
57 m_scopesOverviewCategories->setFavorite(scope, favorite);
60void ScopesOverview::moveFavoriteTo(Scope *scope, int index)
62 m_scopesOverviewCategories->moveFavoriteTo(scope, index);
65ScopesOverviewCategories::ScopesOverviewCategories(Scopes *scopes, QObject* parent)
66 : unity::shell::scopes::CategoriesInterface(parent)
67 , m_scopes(scopes)
71int ScopesOverviewCategories::rowCount(const QModelIndex& /*parent*/) const
73 return 2;
76void ScopesOverviewCategories::addSpecialCategory(QString const&, QString const&, QString const&, QString const&, QObject*)
78 qFatal("Using un-implemented ScopesOverviewCategories::addSpecialCategory");
81bool ScopesOverviewCategories::overrideCategoryJson(QString const& /* categoryId */, QString const& /* json */)
83 qFatal("Using un-implemented ScopesOverviewCategories::overrideCategoryJson");
86void ScopesOverviewCategories::setFavorite(Scope *scope, bool favorite)
88 if (m_resultsModels.value(0)) {
89 if (favorite) {
90 m_resultsModels[0]->appendScope(scope);
91 } else {
92 m_resultsModels[0]->removeScope(scope);
93 }
94 }
95 if (m_resultsModels.value(1)) {
96 if (favorite) {
97 m_resultsModels[1]->removeScope(scope);
98 } else {
99 m_resultsModels[1]->appendScope(scope);
100 }
101 }
104void ScopesOverviewCategories::moveFavoriteTo(Scope *scope, int index)
106 m_resultsModels[0]->moveScopeTo(scope, index);
109QVariant ScopesOverviewCategories::data(const QModelIndex& index, int role) const
111 if (!index.isValid()) {
112 return QVariant();
113 }
115 const QString categoryId = index.row() == 0 ? "favorites" : "other";
117 ScopesOverviewResultsModel *resultsModel = m_resultsModels[index.row()];
118 if (!resultsModel) {
119 QObject *that = const_cast<ScopesOverviewCategories*>(this);
120 QList<Scope*> scopes = index.row() == 0 ? m_scopes->favScopes() : m_scopes->nonFavScopes();
121 resultsModel = new ScopesOverviewResultsModel(scopes, categoryId, that);
122 m_resultsModels[index.row()] = resultsModel;
123 }
124 switch (role) {
125 case RoleCategoryId:
126 return categoryId;
127 case RoleName:
128 return index.row() == 0 ? "Favorites" : "Non Favorites";
129 case RoleIcon:
130 return QVariant();
131 case RoleRawRendererTemplate:
132 qFatal("Using un-implemented RoleRawRendererTemplate Categories role");
133 return QVariant();
134 case RoleRenderer:
135 {
136 QVariantMap map;
137 map["category-layout"] = "grid";
138 map["card-size"] = "small";
139 map["overlay"] = true;
140 return map;
141 }
142 case RoleComponents:
143 {
144 QVariantMap map, artMap, attributesMap;
145 artMap["aspect-ratio"] = "0.5";
146 artMap["field"] = "art";
147 map["art"] = artMap;
148 map["title"] = "HOLA";
149 map["attributes"] = attributesMap;
150 return map;
151 }
152 case RoleResults:
153 return QVariant::fromValue(resultsModel);
154 case RoleCount:
155 return resultsModel->rowCount();
156 case RoleHeaderLink:
157 return QString();
158 default:
159 qFatal("Using un-implemented Categories role");
160 return QVariant();
161 }
166ScopesOverviewSearchCategories::ScopesOverviewSearchCategories(Scopes *scopes, QObject* parent)
167 : unity::shell::scopes::CategoriesInterface(parent)
168 , m_scopes(scopes)
172int ScopesOverviewSearchCategories::rowCount(const QModelIndex& /*parent*/) const
174 return 2;
177void ScopesOverviewSearchCategories::addSpecialCategory(QString const&, QString const&, QString const&, QString const&, QObject*)
179 qFatal("Using un-implemented ScopesOverviewSearchCategories::addSpecialCategory");
182bool ScopesOverviewSearchCategories::overrideCategoryJson(QString const& /* categoryId */, QString const& /* json */)
184 qFatal("Using un-implemented ScopesOverviewSearchCategories::overrideCategoryJson");
187QVariant
188ScopesOverviewSearchCategories::data(const QModelIndex& index, int role) const
190 if (!index.isValid()) {
191 return QVariant();
192 }
194 const QString categoryId = index.row() == 0 ? "searchA" : "searchB";
196 ScopesOverviewResultsModel *resultsModel = m_resultsModels[index.row()];
197 if (!resultsModel) {
198 QObject *that = const_cast<ScopesOverviewSearchCategories*>(this);
199 QList<Scope *> scopes;
200 if (index.row() == 0) {
201 scopes << m_scopes->getScopeFromAll("clickscope") << nullptr << m_scopes->getScopeFromAll("MockScope2");
202 } else {
203 scopes << nullptr << m_scopes->getScopeFromAll("MockScope7") << nullptr << m_scopes->getScopeFromAll("MockScope1");
204 }
205 resultsModel = new ScopesOverviewResultsModel(scopes, categoryId, that);
206 m_resultsModels[index.row()] = resultsModel;
207 }
208 switch (role) {
209 case RoleCategoryId:
210 return categoryId;
211 case RoleName:
212 return index.row() == 0 ? "SearchA" : "SearchB";
213 case RoleIcon:
214 return QVariant();
215 case RoleRawRendererTemplate:
216 qFatal("Using un-implemented RoleRawRendererTemplate Categories role");
217 return QVariant();
218 case RoleRenderer:
219 {
220 QVariantMap map;
221 map["category-layout"] = "grid";
222 map["card-size"] = "small";
223 map["overlay"] = true;
224 return map;
225 }
226 case RoleComponents:
227 {
228 QVariantMap map, artMap, attributesMap;
229 artMap["aspect-ratio"] = "1";
230 artMap["field"] = "art";
231 map["art"] = artMap;
232 map["title"] = "HOLA";
233 map["attributes"] = attributesMap;
234 return map;
235 }
236 case RoleResults:
237 return QVariant::fromValue(resultsModel);
238 case RoleCount:
239 return resultsModel->rowCount();
240 case RoleHeaderLink:
241 return QString();
242 default:
243 qFatal("Using un-implemented Categories role");
244 return QVariant();
245 }
249ScopesOverviewResultsModel::ScopesOverviewResultsModel(const QList<Scope *> &scopes, const QString &categoryId, QObject* parent)
250 : unity::shell::scopes::ResultsModelInterface(parent)
251 , m_scopes(scopes)
252 , m_categoryId(categoryId)
256QString ScopesOverviewResultsModel::categoryId() const
258 return m_categoryId;
261void ScopesOverviewResultsModel::setCategoryId(QString const& /*id*/)
263 qFatal("Calling un-implemented ScopesOverviewResultsModel::setCategoryId");
266int ScopesOverviewResultsModel::scopeIndex(QString const& id) const
268 const int scopeCount = count();
269 for (int i = 0; i < scopeCount; ++i) {
270 if (m_scopes[i]->id() == id)
271 return i;
272 }
273 return -1;
276QHash<int, QByteArray> ScopesOverviewResultsModel::roleNames() const
278 QHash<int, QByteArray> roles = unity::shell::scopes::ResultsModelInterface::roleNames();
279 roles[RoleBackground + 1] = "scopeId";
280 return roles;
283int ScopesOverviewResultsModel::rowCount(const QModelIndex& parent) const
285 Q_UNUSED(parent);
287 return m_scopes.count();
290int ScopesOverviewResultsModel::count() const
292 return rowCount();
295QVariant ScopesOverviewResultsModel::data(const QModelIndex& index, int role) const
297 unity::shell::scopes::ScopeInterface *scope = m_scopes[index.row()];
298 switch (role) {
299 case RoleUri:
300 case RoleCategoryId:
301 case RoleDndUri:
302 return QString();
303 case RoleResult:
304 return scope ? scope->id() : QString("Result.%1.%2").arg(categoryId()).arg(index.row());
305 case RoleTitle:
306 return scope ? scope->name() : QString("Title.%1.%2").arg(categoryId()).arg(index.row());
307 case RoleSubtitle:
308 return scope && scope->name() == "Videos this is long ab cd ef gh ij kl" ? "tube, movies, cinema, pictures, art, moving images, magic in a box" : QString();
309 case RoleArt:
310 return qmlDirectory() + "graphics/applicationIcons/dash.png";
311 case RoleMascot:
312 case RoleEmblem:
313 case RoleSummary:
314 case RoleBackground + 1: // scopeId
315 return scope ? scope->id() : nullptr;
316 break;
317 default:
318 return QVariant();
319 }
322void ScopesOverviewResultsModel::appendScope(Scope *scope)
324 Q_ASSERT(!m_scopes.contains(scope));
325 const int index = rowCount();
326 beginInsertRows(QModelIndex(), index, index);
327 m_scopes << scope;
328 endInsertRows();
329 Q_EMIT countChanged();
332void ScopesOverviewResultsModel::removeScope(Scope *scope)
334 const int index = m_scopes.indexOf(scope);
335 Q_ASSERT(index != -1);
336 beginRemoveRows(QModelIndex(), index, index);
337 m_scopes.removeAt(index);
338 endRemoveRows();
339 Q_EMIT countChanged();
342void ScopesOverviewResultsModel::moveScopeTo(Scope *scope, int to)
344 const int from = m_scopes.indexOf(scope);
345 Q_ASSERT(from!= -1);
346 beginMoveRows(QModelIndex(), from, from, QModelIndex(), to + (to > from ? 1 : 0));
347 m_scopes.move(from, to);
348 endMoveRows();