on 436b607 help sign in
unity8/tests/mocks/Unity/fake_scopesoverview.cpp
350 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 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;
77void ScopesOverviewCategories::addSpecialCategory(QString const&, QString const&, QString const&, QString const&, QObject*)
79 qFatal("Using un-implemented ScopesOverviewCategories::addSpecialCategory");
82bool ScopesOverviewCategories::overrideCategoryJson(QString const& /* categoryId */, QString const& /* json */)
84 qFatal("Using un-implemented ScopesOverviewCategories::overrideCategoryJson");
87void ScopesOverviewCategories::setFavorite(Scope *scope, bool favorite)
89 if (m_resultsModels.value(0)) {
90 if (favorite) {
91 m_resultsModels[0]->appendScope(scope);
92 } else {
93 m_resultsModels[0]->removeScope(scope);
94 }
95 }
96 if (m_resultsModels.value(1)) {
97 if (favorite) {
98 m_resultsModels[1]->removeScope(scope);
99 } else {
100 m_resultsModels[1]->appendScope(scope);
101 }
102 }
105void ScopesOverviewCategories::moveFavoriteTo(Scope *scope, int index)
107 m_resultsModels[0]->moveScopeTo(scope, index);
110QVariant ScopesOverviewCategories::data(const QModelIndex& index, int role) const
112 if (!index.isValid()) {
113 return QVariant();
114 }
116 const QString categoryId = index.row() == 0 ? "favorites" : "other";
118 ScopesOverviewResultsModel *resultsModel = m_resultsModels[index.row()];
119 if (!resultsModel) {
120 QObject *that = const_cast<ScopesOverviewCategories*>(this);
121 QList<Scope*> scopes = index.row() == 0 ? m_scopes->favScopes() : m_scopes->nonFavScopes();
122 resultsModel = new ScopesOverviewResultsModel(scopes, categoryId, that);
123 m_resultsModels[index.row()] = resultsModel;
124 }
125 switch (role) {
126 case RoleCategoryId:
127 return categoryId;
128 case RoleName:
129 return index.row() == 0 ? "Favorites" : "Non Favorites";
130 case RoleIcon:
131 return QVariant();
132 case RoleRawRendererTemplate:
133 qFatal("Using un-implemented RoleRawRendererTemplate Categories role");
134 return QVariant();
135 case RoleRenderer:
136 {
137 QVariantMap map;
138 map["category-layout"] = "grid";
139 map["card-size"] = "small";
140 map["overlay"] = true;
141 return map;
142 }
143 case RoleComponents:
144 {
145 QVariantMap map, artMap, attributesMap;
146 artMap["aspect-ratio"] = "0.5";
147 artMap["field"] = "art";
148 map["art"] = artMap;
149 map["title"] = "HOLA";
150 map["attributes"] = attributesMap;
151 return map;
152 }
153 case RoleResults:
154 return QVariant::fromValue(resultsModel);
155 case RoleCount:
156 return resultsModel->rowCount();
157 case RoleHeaderLink:
158 return QString();
159 default:
160 qFatal("Using un-implemented Categories role");
161 return QVariant();
162 }
167ScopesOverviewSearchCategories::ScopesOverviewSearchCategories(Scopes *scopes, QObject* parent)
168 : unity::shell::scopes::CategoriesInterface(parent)
169 , m_scopes(scopes)
173int ScopesOverviewSearchCategories::rowCount(const QModelIndex& /*parent*/) const
175 return 2;
178void ScopesOverviewSearchCategories::addSpecialCategory(QString const&, QString const&, QString const&, QString const&, QObject*)
180 qFatal("Using un-implemented ScopesOverviewSearchCategories::addSpecialCategory");
183bool ScopesOverviewSearchCategories::overrideCategoryJson(QString const& /* categoryId */, QString const& /* json */)
185 qFatal("Using un-implemented ScopesOverviewSearchCategories::overrideCategoryJson");
188QVariant
189ScopesOverviewSearchCategories::data(const QModelIndex& index, int role) const
191 if (!index.isValid()) {
192 return QVariant();
193 }
195 const QString categoryId = index.row() == 0 ? "searchA" : "searchB";
197 ScopesOverviewResultsModel *resultsModel = m_resultsModels[index.row()];
198 if (!resultsModel) {
199 QObject *that = const_cast<ScopesOverviewSearchCategories*>(this);
200 QList<Scope *> scopes;
201 if (index.row() == 0) {
202 scopes << m_scopes->getScopeFromAll("clickscope") << nullptr << m_scopes->getScopeFromAll("MockScope2");
203 } else {
204 scopes << nullptr << m_scopes->getScopeFromAll("MockScope7") << nullptr << m_scopes->getScopeFromAll("MockScope1");
205 }
206 resultsModel = new ScopesOverviewResultsModel(scopes, categoryId, that);
207 m_resultsModels[index.row()] = resultsModel;
208 }
209 switch (role) {
210 case RoleCategoryId:
211 return categoryId;
212 case RoleName:
213 return index.row() == 0 ? "SearchA" : "SearchB";
214 case RoleIcon:
215 return QVariant();
216 case RoleRawRendererTemplate:
217 qFatal("Using un-implemented RoleRawRendererTemplate Categories role");
218 return QVariant();
219 case RoleRenderer:
220 {
221 QVariantMap map;
222 map["category-layout"] = "grid";
223 map["card-size"] = "small";
224 map["overlay"] = true;
225 return map;
226 }
227 case RoleComponents:
228 {
229 QVariantMap map, artMap, attributesMap;
230 artMap["aspect-ratio"] = "1";
231 artMap["field"] = "art";
232 map["art"] = artMap;
233 map["title"] = "HOLA";
234 map["attributes"] = attributesMap;
235 return map;
236 }
237 case RoleResults:
238 return QVariant::fromValue(resultsModel);
239 case RoleCount:
240 return resultsModel->rowCount();
241 case RoleHeaderLink:
242 return QString();
243 default:
244 qFatal("Using un-implemented Categories role");
245 return QVariant();
246 }
250ScopesOverviewResultsModel::ScopesOverviewResultsModel(const QList<Scope *> &scopes, const QString &categoryId, QObject* parent)
251 : unity::shell::scopes::ResultsModelInterface(parent)
252 , m_scopes(scopes)
253 , m_categoryId(categoryId)
257QString ScopesOverviewResultsModel::categoryId() const
259 return m_categoryId;
262void ScopesOverviewResultsModel::setCategoryId(QString const& /*id*/)
264 qFatal("Calling un-implemented ScopesOverviewResultsModel::setCategoryId");
267int ScopesOverviewResultsModel::scopeIndex(QString const& id) const
269 const int scopeCount = count();
270 for (int i = 0; i < scopeCount; ++i) {
271 if (m_scopes[i]->id() == id)
272 return i;
273 }
274 return -1;
277QHash<int, QByteArray> ScopesOverviewResultsModel::roleNames() const
279 QHash<int, QByteArray> roles = unity::shell::scopes::ResultsModelInterface::roleNames();
280 roles[RoleBackground + 1] = "scopeId";
281 return roles;
284int ScopesOverviewResultsModel::rowCount(const QModelIndex& parent) const
286 Q_UNUSED(parent);
288 return m_scopes.count();
291int ScopesOverviewResultsModel::count() const
293 return rowCount();
296QVariant ScopesOverviewResultsModel::data(const QModelIndex& index, int role) const
298 unity::shell::scopes::ScopeInterface *scope = m_scopes[index.row()];
299 switch (role) {
300 case RoleUri:
301 case RoleCategoryId:
302 case RoleDndUri:
303 return QString();
304 case RoleResult:
305 return scope ? scope->id() : QString("Result.%1.%2").arg(categoryId()).arg(index.row());
306 case RoleTitle:
307 return scope ? scope->name() : QString("Title.%1.%2").arg(categoryId()).arg(index.row());
308 case RoleSubtitle:
309 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();
310 case RoleArt:
311 return qmlDirectory() + "graphics/applicationIcons/dash.png";
312 case RoleMascot:
313 case RoleEmblem:
314 case RoleSummary:
315 case RoleBackground + 1: // scopeId
316 return scope ? scope->id() : nullptr;
317 break;
318 default:
319 return QVariant();
320 }
323void ScopesOverviewResultsModel::appendScope(Scope *scope)
325 Q_ASSERT(!m_scopes.contains(scope));
326 const int index = rowCount();
327 beginInsertRows(QModelIndex(), index, index);
328 m_scopes << scope;
329 endInsertRows();
330 Q_EMIT countChanged();
333void ScopesOverviewResultsModel::removeScope(Scope *scope)
335 const int index = m_scopes.indexOf(scope);
336 Q_ASSERT(index != -1);
337 beginRemoveRows(QModelIndex(), index, index);
338 m_scopes.removeAt(index);
339 endRemoveRows();
340 Q_EMIT countChanged();
343void ScopesOverviewResultsModel::moveScopeTo(Scope *scope, int to)
345 const int from = m_scopes.indexOf(scope);
346 Q_ASSERT(from!= -1);
347 beginMoveRows(QModelIndex(), from, from, QModelIndex(), to + (to > from ? 1 : 0));
348 m_scopes.move(from, to);
349 endMoveRows();