on 9bb49bf help sign in
unity8/tests/mocks/Unity/fake_scopesoverview.cpp
280 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 resultsModel = new ScopesOverviewResultsModel(m_scopes->scopes(index.row() == 0), categoryId, that);
85 m_resultsModels[index.row()] = resultsModel;
86 }
87 switch (role) {
88 case RoleCategoryId:
89 return categoryId;
90 case RoleName:
91 return index.row() == 0 ? "Favorites" : "All";
92 case RoleIcon:
93 return QVariant();
94 case RoleRawRendererTemplate:
95 qFatal("Using un-implemented RoleRawRendererTemplate Categories role");
96 return QVariant();
97 case RoleRenderer:
98 {
99 QVariantMap map;
100 map["category-layout"] = "grid";
101 map["card-size"] = "small";
102 map["overlay"] = true;
103 return map;
104 }
105 case RoleComponents:
106 {
107 QVariantMap map, artMap;
108 artMap["aspect-ratio"] = "0.5";
109 artMap["field"] = "art";
110 map["art"] = artMap;
111 map["title"] = "HOLA";
112 return map;
113 }
114 case RoleResults:
115 return QVariant::fromValue(resultsModel);
116 case RoleCount:
117 return resultsModel->rowCount();
118 case RoleHeaderLink:
119 return QString();
120 default:
121 qFatal("Using un-implemented Categories role");
122 return QVariant();
123 }
128ScopesOverviewSearchCategories::ScopesOverviewSearchCategories(Scopes *scopes, QObject* parent)
129 : unity::shell::scopes::CategoriesInterface(parent)
130 , m_scopes(scopes)
134int ScopesOverviewSearchCategories::rowCount(const QModelIndex& /*parent*/) const
136 return 2;
139void ScopesOverviewSearchCategories::addSpecialCategory(QString const&, QString const&, QString const&, QString const&, QObject*)
141 qFatal("Using un-implemented ScopesOverviewSearchCategories::addSpecialCategory");
144bool ScopesOverviewSearchCategories::overrideCategoryJson(QString const& /* categoryId */, QString const& /* json */)
146 qFatal("Using un-implemented ScopesOverviewSearchCategories::overrideCategoryJson");
149QVariant
150ScopesOverviewSearchCategories::data(const QModelIndex& index, int role) const
152 if (!index.isValid()) {
153 return QVariant();
154 }
156 const QString categoryId = index.row() == 0 ? "searchA" : "searchB";
158 unity::shell::scopes::ResultsModelInterface *resultsModel = m_resultsModels[index.row()];
159 if (!resultsModel) {
160 QObject *that = const_cast<ScopesOverviewSearchCategories*>(this);
161 QList<unity::shell::scopes::ScopeInterface *> scopes;
162 if (index.row() == 0) {
163 scopes << m_scopes->getScopeFromAll("clickscope") << nullptr << m_scopes->getScopeFromAll("MockScope2");
164 } else {
165 scopes << nullptr << m_scopes->getScopeFromAll("MockScope7") << nullptr << m_scopes->getScopeFromAll("MockScope1");
166 }
167 resultsModel = new ScopesOverviewResultsModel(scopes, categoryId, that);
168 m_resultsModels[index.row()] = resultsModel;
169 }
170 switch (role) {
171 case RoleCategoryId:
172 return categoryId;
173 case RoleName:
174 return index.row() == 0 ? "SearchA" : "SearchB";
175 case RoleIcon:
176 return QVariant();
177 case RoleRawRendererTemplate:
178 qFatal("Using un-implemented RoleRawRendererTemplate Categories role");
179 return QVariant();
180 case RoleRenderer:
181 {
182 QVariantMap map;
183 map["category-layout"] = "grid";
184 map["card-size"] = "small";
185 map["overlay"] = true;
186 return map;
187 }
188 case RoleComponents:
189 {
190 QVariantMap map, artMap;
191 artMap["aspect-ratio"] = "1";
192 artMap["field"] = "art";
193 map["art"] = artMap;
194 map["title"] = "HOLA";
195 return map;
196 }
197 case RoleResults:
198 return QVariant::fromValue(resultsModel);
199 case RoleCount:
200 return resultsModel->rowCount();
201 case RoleHeaderLink:
202 return QString();
203 default:
204 qFatal("Using un-implemented Categories role");
205 return QVariant();
206 }
210ScopesOverviewResultsModel::ScopesOverviewResultsModel(const QList<unity::shell::scopes::ScopeInterface *> &scopes, const QString &categoryId, QObject* parent)
211 : unity::shell::scopes::ResultsModelInterface(parent)
212 , m_scopes(scopes)
213 , m_categoryId(categoryId)
217QString ScopesOverviewResultsModel::categoryId() const
219 return m_categoryId;
222void ScopesOverviewResultsModel::setCategoryId(QString const& /*id*/)
224 qFatal("Calling un-implemented ScopesOverviewResultsModel::setCategoryId");
227int ScopesOverviewResultsModel::scopeIndex(QString const& id) const
229 const int scopeCount = count();
230 for (int i = 0; i < scopeCount; ++i) {
231 if (m_scopes[i]->id() == id)
232 return i;
233 }
234 return -1;
237QHash<int, QByteArray> ScopesOverviewResultsModel::roleNames() const
239 QHash<int, QByteArray> roles = unity::shell::scopes::ResultsModelInterface::roleNames();
240 roles[RoleBackground + 1] = "scopeId";
241 return roles;
244int ScopesOverviewResultsModel::rowCount(const QModelIndex& parent) const
246 Q_UNUSED(parent);
248 return m_scopes.count();
251int ScopesOverviewResultsModel::count() const
253 return rowCount();
256QVariant
257ScopesOverviewResultsModel::data(const QModelIndex& index, int role) const
259 unity::shell::scopes::ScopeInterface *scope = m_scopes[index.row()];
260 switch (role) {
261 case RoleUri:
262 case RoleCategoryId:
263 case RoleDndUri:
264 return QString();
265 case RoleResult:
266 return scope ? scope->id() : QString("Result.%1.%2").arg(categoryId()).arg(index.row());
267 case RoleTitle:
268 return scope ? scope->name() : QString("Title.%1.%2").arg(categoryId()).arg(index.row());
269 case RoleArt:
270 return qmlDirectory() + "graphics/applicationIcons/dash.png";
271 case RoleMascot:
272 case RoleEmblem:
273 case RoleSummary:
274 case RoleBackground + 1: // scopeId
275 return scope ? scope->id() : nullptr;
276 break;
277 default:
278 return QVariant();
279 }