on d345593 help sign in
unity8/tests/mocks/Unity/fake_scopesoverview.cpp
340 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, QString const& /*categoryId*/)
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 scopes->addTempScope(m_openScope);
52 Q_EMIT openScope(m_openScope);
53 }
56void ScopesOverview::setFavorite(Scope *scope, bool favorite)
58 m_scopesOverviewCategories->setFavorite(scope, favorite);
61void ScopesOverview::moveFavoriteTo(Scope *scope, int index)
63 m_scopesOverviewCategories->moveFavoriteTo(scope, index);
66ScopesOverviewCategories::ScopesOverviewCategories(Scopes *scopes, QObject* parent)
67 : unity::shell::scopes::CategoriesInterface(parent)
68 , m_scopes(scopes)
72int ScopesOverviewCategories::rowCount(const QModelIndex& /*parent*/) const
74 return 2;
77bool ScopesOverviewCategories::overrideCategoryJson(QString const& /* categoryId */, QString const& /* json */)
79 qFatal("Using un-implemented ScopesOverviewCategories::overrideCategoryJson");
82void ScopesOverviewCategories::setFavorite(Scope *scope, bool favorite)
84 if (m_resultsModels.value(0)) {
85 if (favorite) {
86 m_resultsModels[0]->appendScope(scope);
87 } else {
88 m_resultsModels[0]->removeScope(scope);
89 }
90 }
91 if (m_resultsModels.value(1)) {
92 if (favorite) {
93 m_resultsModels[1]->removeScope(scope);
94 } else {
95 m_resultsModels[1]->appendScope(scope);
96 }
97 }
100void ScopesOverviewCategories::moveFavoriteTo(Scope *scope, int index)
102 m_resultsModels[0]->moveScopeTo(scope, index);
105QVariant ScopesOverviewCategories::data(const QModelIndex& index, int role) const
107 if (!index.isValid()) {
108 return QVariant();
109 }
111 const QString categoryId = index.row() == 0 ? "favorites" : "other";
113 ScopesOverviewResultsModel *resultsModel = m_resultsModels[index.row()];
114 if (!resultsModel) {
115 QObject *that = const_cast<ScopesOverviewCategories*>(this);
116 QList<Scope*> scopes = index.row() == 0 ? m_scopes->favScopes() : m_scopes->nonFavScopes();
117 resultsModel = new ScopesOverviewResultsModel(scopes, categoryId, that);
118 m_resultsModels[index.row()] = resultsModel;
119 }
120 switch (role) {
121 case RoleCategoryId:
122 return categoryId;
123 case RoleName:
124 return index.row() == 0 ? "Favorites" : "Non Favorites";
125 case RoleIcon:
126 return QVariant();
127 case RoleRawRendererTemplate:
128 qFatal("Using un-implemented RoleRawRendererTemplate Categories role");
129 return QVariant();
130 case RoleRenderer:
131 {
132 QVariantMap map;
133 map["category-layout"] = "grid";
134 map["card-size"] = "small";
135 map["overlay"] = true;
136 return map;
137 }
138 case RoleComponents:
139 {
140 QVariantMap map, artMap, attributesMap;
141 artMap["aspect-ratio"] = "0.5";
142 artMap["field"] = "art";
143 map["art"] = artMap;
144 map["title"] = "HOLA";
145 map["attributes"] = attributesMap;
146 return map;
147 }
148 case RoleResults:
149 return QVariant::fromValue(resultsModel);
150 case RoleCount:
151 return resultsModel->rowCount();
152 case RoleHeaderLink:
153 return QString();
154 default:
155 qFatal("Using un-implemented Categories role");
156 return QVariant();
157 }
162ScopesOverviewSearchCategories::ScopesOverviewSearchCategories(Scopes *scopes, QObject* parent)
163 : unity::shell::scopes::CategoriesInterface(parent)
164 , m_scopes(scopes)
168int ScopesOverviewSearchCategories::rowCount(const QModelIndex& /*parent*/) const
170 return 2;
173bool ScopesOverviewSearchCategories::overrideCategoryJson(QString const& /* categoryId */, QString const& /* json */)
175 qFatal("Using un-implemented ScopesOverviewSearchCategories::overrideCategoryJson");
178QVariant
179ScopesOverviewSearchCategories::data(const QModelIndex& index, int role) const
181 if (!index.isValid()) {
182 return QVariant();
183 }
185 const QString categoryId = index.row() == 0 ? "searchA" : "searchB";
187 ScopesOverviewResultsModel *resultsModel = m_resultsModels[index.row()];
188 if (!resultsModel) {
189 QObject *that = const_cast<ScopesOverviewSearchCategories*>(this);
190 QList<Scope *> scopes;
191 if (index.row() == 0) {
192 scopes << m_scopes->getScopeFromAll("clickscope") << nullptr << m_scopes->getScopeFromAll("MockScope2");
193 } else {
194 scopes << nullptr << m_scopes->getScopeFromAll("MockScope7") << nullptr << m_scopes->getScopeFromAll("MockScope1");
195 }
196 resultsModel = new ScopesOverviewResultsModel(scopes, categoryId, that);
197 m_resultsModels[index.row()] = resultsModel;
198 }
199 switch (role) {
200 case RoleCategoryId:
201 return categoryId;
202 case RoleName:
203 return index.row() == 0 ? "SearchA" : "SearchB";
204 case RoleIcon:
205 return QVariant();
206 case RoleRawRendererTemplate:
207 qFatal("Using un-implemented RoleRawRendererTemplate Categories role");
208 return QVariant();
209 case RoleRenderer:
210 {
211 QVariantMap map;
212 map["category-layout"] = "grid";
213 map["card-size"] = "small";
214 map["overlay"] = true;
215 return map;
216 }
217 case RoleComponents:
218 {
219 QVariantMap map, artMap, attributesMap;
220 artMap["aspect-ratio"] = "1";
221 artMap["field"] = "art";
222 map["art"] = artMap;
223 map["title"] = "HOLA";
224 map["attributes"] = attributesMap;
225 return map;
226 }
227 case RoleResults:
228 return QVariant::fromValue(resultsModel);
229 case RoleCount:
230 return resultsModel->rowCount();
231 case RoleHeaderLink:
232 return QString();
233 default:
234 qFatal("Using un-implemented Categories role");
235 return QVariant();
236 }
240ScopesOverviewResultsModel::ScopesOverviewResultsModel(const QList<Scope *> &scopes, const QString &categoryId, QObject* parent)
241 : unity::shell::scopes::ResultsModelInterface(parent)
242 , m_scopes(scopes)
243 , m_categoryId(categoryId)
247QString ScopesOverviewResultsModel::categoryId() const
249 return m_categoryId;
252void ScopesOverviewResultsModel::setCategoryId(QString const& /*id*/)
254 qFatal("Calling un-implemented ScopesOverviewResultsModel::setCategoryId");
257int ScopesOverviewResultsModel::scopeIndex(QString const& id) const
259 const int scopeCount = count();
260 for (int i = 0; i < scopeCount; ++i) {
261 if (m_scopes[i]->id() == id)
262 return i;
263 }
264 return -1;
267QHash<int, QByteArray> ScopesOverviewResultsModel::roleNames() const
269 QHash<int, QByteArray> roles = unity::shell::scopes::ResultsModelInterface::roleNames();
270 roles[RoleBackground + 1] = "scopeId";
271 return roles;
274int ScopesOverviewResultsModel::rowCount(const QModelIndex& parent) const
276 Q_UNUSED(parent);
278 return m_scopes.count();
281int ScopesOverviewResultsModel::count() const
283 return rowCount();
286QVariant ScopesOverviewResultsModel::data(const QModelIndex& index, int role) const
288 unity::shell::scopes::ScopeInterface *scope = m_scopes[index.row()];
289 switch (role) {
290 case RoleUri:
291 case RoleCategoryId:
292 case RoleDndUri:
293 return QString();
294 case RoleResult:
295 return scope ? scope->id() : QString("Result.%1.%2").arg(categoryId()).arg(index.row());
296 case RoleTitle:
297 return scope ? scope->name() : QString("Title.%1.%2").arg(categoryId()).arg(index.row());
298 case RoleSubtitle:
299 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();
300 case RoleArt:
301 return QString("../../tests/graphics/applicationIcons/dash.png");
302 case RoleMascot:
303 case RoleEmblem:
304 case RoleSummary:
305 case RoleBackground + 1: // scopeId
306 return scope ? scope->id() : nullptr;
307 break;
308 default:
309 return QVariant();
310 }
313void ScopesOverviewResultsModel::appendScope(Scope *scope)
315 Q_ASSERT(!m_scopes.contains(scope));
316 const int index = rowCount();
317 beginInsertRows(QModelIndex(), index, index);
318 m_scopes << scope;
319 endInsertRows();
320 Q_EMIT countChanged();
323void ScopesOverviewResultsModel::removeScope(Scope *scope)
325 const int index = m_scopes.indexOf(scope);
326 Q_ASSERT(index != -1);
327 beginRemoveRows(QModelIndex(), index, index);
328 m_scopes.removeAt(index);
329 endRemoveRows();
330 Q_EMIT countChanged();
333void ScopesOverviewResultsModel::moveScopeTo(Scope *scope, int to)
335 const int from = m_scopes.indexOf(scope);
336 Q_ASSERT(from!= -1);
337 beginMoveRows(QModelIndex(), from, from, QModelIndex(), to + (to > from ? 1 : 0));
338 m_scopes.move(from, to);
339 endMoveRows();