on 82b74fc help sign in
unity8/tests/mocks/Unity/fake_scopesoverview.cpp
343 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 m_openScope = scopes->getScopeFromAll(result.toString());
48 Q_EMIT openScope(m_openScope);
51void ScopesOverview::setFavorite(Scope *scope, bool favorite)
53 m_scopesOverviewCategories->setFavorite(scope, favorite);
56void ScopesOverview::moveFavoriteTo(Scope *scope, int index)
58 m_scopesOverviewCategories->moveFavoriteTo(scope, index);
61ScopesOverviewCategories::ScopesOverviewCategories(Scopes *scopes, QObject* parent)
62 : unity::shell::scopes::CategoriesInterface(parent)
63 , m_scopes(scopes)
67int ScopesOverviewCategories::rowCount(const QModelIndex& /*parent*/) const
69 return 2;
72void ScopesOverviewCategories::addSpecialCategory(QString const&, QString const&, QString const&, QString const&, QObject*)
74 qFatal("Using un-implemented ScopesOverviewCategories::addSpecialCategory");
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" : "nonfavorites";
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;
173void ScopesOverviewSearchCategories::addSpecialCategory(QString const&, QString const&, QString const&, QString const&, QObject*)
175 qFatal("Using un-implemented ScopesOverviewSearchCategories::addSpecialCategory");
178bool ScopesOverviewSearchCategories::overrideCategoryJson(QString const& /* categoryId */, QString const& /* json */)
180 qFatal("Using un-implemented ScopesOverviewSearchCategories::overrideCategoryJson");
183QVariant
184ScopesOverviewSearchCategories::data(const QModelIndex& index, int role) const
186 if (!index.isValid()) {
187 return QVariant();
188 }
190 const QString categoryId = index.row() == 0 ? "searchA" : "searchB";
192 ScopesOverviewResultsModel *resultsModel = m_resultsModels[index.row()];
193 if (!resultsModel) {
194 QObject *that = const_cast<ScopesOverviewSearchCategories*>(this);
195 QList<Scope *> scopes;
196 if (index.row() == 0) {
197 scopes << m_scopes->getScopeFromAll("clickscope") << nullptr << m_scopes->getScopeFromAll("MockScope2");
198 } else {
199 scopes << nullptr << m_scopes->getScopeFromAll("MockScope7") << nullptr << m_scopes->getScopeFromAll("MockScope1");
200 }
201 resultsModel = new ScopesOverviewResultsModel(scopes, categoryId, that);
202 m_resultsModels[index.row()] = resultsModel;
203 }
204 switch (role) {
205 case RoleCategoryId:
206 return categoryId;
207 case RoleName:
208 return index.row() == 0 ? "SearchA" : "SearchB";
209 case RoleIcon:
210 return QVariant();
211 case RoleRawRendererTemplate:
212 qFatal("Using un-implemented RoleRawRendererTemplate Categories role");
213 return QVariant();
214 case RoleRenderer:
215 {
216 QVariantMap map;
217 map["category-layout"] = "grid";
218 map["card-size"] = "small";
219 map["overlay"] = true;
220 return map;
221 }
222 case RoleComponents:
223 {
224 QVariantMap map, artMap, attributesMap;
225 artMap["aspect-ratio"] = "1";
226 artMap["field"] = "art";
227 map["art"] = artMap;
228 map["title"] = "HOLA";
229 map["attributes"] = attributesMap;
230 return map;
231 }
232 case RoleResults:
233 return QVariant::fromValue(resultsModel);
234 case RoleCount:
235 return resultsModel->rowCount();
236 case RoleHeaderLink:
237 return QString();
238 default:
239 qFatal("Using un-implemented Categories role");
240 return QVariant();
241 }
245ScopesOverviewResultsModel::ScopesOverviewResultsModel(const QList<Scope *> &scopes, const QString &categoryId, QObject* parent)
246 : unity::shell::scopes::ResultsModelInterface(parent)
247 , m_scopes(scopes)
248 , m_categoryId(categoryId)
252QString ScopesOverviewResultsModel::categoryId() const
254 return m_categoryId;
257void ScopesOverviewResultsModel::setCategoryId(QString const& /*id*/)
259 qFatal("Calling un-implemented ScopesOverviewResultsModel::setCategoryId");
262int ScopesOverviewResultsModel::scopeIndex(QString const& id) const
264 const int scopeCount = count();
265 for (int i = 0; i < scopeCount; ++i) {
266 if (m_scopes[i]->id() == id)
267 return i;
268 }
269 return -1;
272QHash<int, QByteArray> ScopesOverviewResultsModel::roleNames() const
274 QHash<int, QByteArray> roles = unity::shell::scopes::ResultsModelInterface::roleNames();
275 roles[RoleBackground + 1] = "scopeId";
276 return roles;
279int ScopesOverviewResultsModel::rowCount(const QModelIndex& parent) const
281 Q_UNUSED(parent);
283 return m_scopes.count();
286int ScopesOverviewResultsModel::count() const
288 return rowCount();
291QVariant ScopesOverviewResultsModel::data(const QModelIndex& index, int role) const
293 unity::shell::scopes::ScopeInterface *scope = m_scopes[index.row()];
294 switch (role) {
295 case RoleUri:
296 case RoleCategoryId:
297 case RoleDndUri:
298 return QString();
299 case RoleResult:
300 return scope ? scope->id() : QString("Result.%1.%2").arg(categoryId()).arg(index.row());
301 case RoleTitle:
302 return scope ? scope->name() : QString("Title.%1.%2").arg(categoryId()).arg(index.row());
303 case RoleArt:
304 return qmlDirectory() + "graphics/applicationIcons/dash.png";
305 case RoleMascot:
306 case RoleEmblem:
307 case RoleSummary:
308 case RoleBackground + 1: // scopeId
309 return scope ? scope->id() : nullptr;
310 break;
311 default:
312 return QVariant();
313 }
316void ScopesOverviewResultsModel::appendScope(Scope *scope)
318 Q_ASSERT(!m_scopes.contains(scope));
319 const int index = rowCount();
320 beginInsertRows(QModelIndex(), index, index);
321 m_scopes << scope;
322 endInsertRows();
323 Q_EMIT countChanged();
326void ScopesOverviewResultsModel::removeScope(Scope *scope)
328 const int index = m_scopes.indexOf(scope);
329 Q_ASSERT(index != -1);
330 beginRemoveRows(QModelIndex(), index, index);
331 m_scopes.removeAt(index);
332 endRemoveRows();
333 Q_EMIT countChanged();
336void ScopesOverviewResultsModel::moveScopeTo(Scope *scope, int to)
338 const int from = m_scopes.indexOf(scope);
339 Q_ASSERT(from!= -1);
340 beginMoveRows(QModelIndex(), from, from, QModelIndex(), to + (to > from ? 1 : 0));
341 m_scopes.move(from, to);
342 endMoveRows();