on 7ce2be3 help sign in
unity8/tests/mocks/Unity/fake_scopesoverview.cpp
270 lines 8.6 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();
45ScopesOverviewCategories::ScopesOverviewCategories(Scopes *scopes, QObject* parent)
46 : unity::shell::scopes::CategoriesInterface(parent)
47 , m_scopes(scopes)
51int ScopesOverviewCategories::rowCount(const QModelIndex& /*parent*/) const
53 return 2;
56void ScopesOverviewCategories::addSpecialCategory(QString const&, QString const&, QString const&, QString const&, QObject*)
58 qFatal("Using un-implemented ScopesOverviewCategories::addSpecialCategory");
61bool ScopesOverviewCategories::overrideCategoryJson(QString const& /* categoryId */, QString const& /* json */)
63 qFatal("Using un-implemented ScopesOverviewCategories::overrideCategoryJson");
66QVariant
67ScopesOverviewCategories::data(const QModelIndex& index, int role) const
69 if (!index.isValid()) {
70 return QVariant();
71 }
73 const QString categoryId = index.row() == 0 ? "favorites" : "all";
75 unity::shell::scopes::ResultsModelInterface *resultsModel = m_resultsModels[index.row()];
76 if (!resultsModel) {
77 QObject *that = const_cast<ScopesOverviewCategories*>(this);
78 resultsModel = new ScopesOverviewResultsModel(m_scopes->scopes(index.row() == 0), categoryId, that);
79 m_resultsModels[index.row()] = resultsModel;
80 }
81 switch (role) {
82 case RoleCategoryId:
83 return categoryId;
84 case RoleName:
85 return index.row() == 0 ? "Favorites" : "All";
86 case RoleIcon:
87 return QVariant();
88 case RoleRawRendererTemplate:
89 qFatal("Using un-implemented RoleRawRendererTemplate Categories role");
90 return QVariant();
91 case RoleRenderer:
92 {
93 QVariantMap map;
94 map["category-layout"] = "grid";
95 map["card-size"] = "small";
96 map["overlay"] = true;
97 return map;
98 }
99 case RoleComponents:
100 {
101 QVariantMap map, artMap;
102 artMap["aspect-ratio"] = "0.5";
103 artMap["field"] = "art";
104 map["art"] = artMap;
105 map["title"] = "HOLA";
106 return map;
107 }
108 case RoleResults:
109 return QVariant::fromValue(resultsModel);
110 case RoleCount:
111 return resultsModel->rowCount();
112 default:
113 qFatal("Using un-implemented Categories role");
114 return QVariant();
115 }
120ScopesOverviewSearchCategories::ScopesOverviewSearchCategories(Scopes *scopes, QObject* parent)
121 : unity::shell::scopes::CategoriesInterface(parent)
122 , m_scopes(scopes)
126int ScopesOverviewSearchCategories::rowCount(const QModelIndex& /*parent*/) const
128 return 2;
131void ScopesOverviewSearchCategories::addSpecialCategory(QString const&, QString const&, QString const&, QString const&, QObject*)
133 qFatal("Using un-implemented ScopesOverviewSearchCategories::addSpecialCategory");
136bool ScopesOverviewSearchCategories::overrideCategoryJson(QString const& /* categoryId */, QString const& /* json */)
138 qFatal("Using un-implemented ScopesOverviewSearchCategories::overrideCategoryJson");
141QVariant
142ScopesOverviewSearchCategories::data(const QModelIndex& index, int role) const
144 if (!index.isValid()) {
145 return QVariant();
146 }
148 const QString categoryId = index.row() == 0 ? "searchA" : "searchB";
150 unity::shell::scopes::ResultsModelInterface *resultsModel = m_resultsModels[index.row()];
151 if (!resultsModel) {
152 QObject *that = const_cast<ScopesOverviewSearchCategories*>(this);
153 QList<unity::shell::scopes::ScopeInterface *> scopes;
154 if (index.row() == 0) {
155 scopes << m_scopes->getScope("clickscope") << nullptr << m_scopes->getScope("MockScope2");
156 } else {
157 scopes << nullptr << m_scopes->getScope("MockScope7") << nullptr << m_scopes->getScope("MockScope1");
158 }
159 resultsModel = new ScopesOverviewResultsModel(scopes, categoryId, that);
160 m_resultsModels[index.row()] = resultsModel;
161 }
162 switch (role) {
163 case RoleCategoryId:
164 return categoryId;
165 case RoleName:
166 return index.row() == 0 ? "SearchA" : "SearchB";
167 case RoleIcon:
168 return QVariant();
169 case RoleRawRendererTemplate:
170 qFatal("Using un-implemented RoleRawRendererTemplate Categories role");
171 return QVariant();
172 case RoleRenderer:
173 {
174 QVariantMap map;
175 map["category-layout"] = "grid";
176 map["card-size"] = "small";
177 map["overlay"] = true;
178 return map;
179 }
180 case RoleComponents:
181 {
182 QVariantMap map, artMap;
183 artMap["aspect-ratio"] = "1";
184 artMap["field"] = "art";
185 map["art"] = artMap;
186 map["title"] = "HOLA";
187 return map;
188 }
189 case RoleResults:
190 return QVariant::fromValue(resultsModel);
191 case RoleCount:
192 return resultsModel->rowCount();
193 default:
194 qFatal("Using un-implemented Categories role");
195 return QVariant();
196 }
200ScopesOverviewResultsModel::ScopesOverviewResultsModel(const QList<unity::shell::scopes::ScopeInterface *> &scopes, const QString &categoryId, QObject* parent)
201 : unity::shell::scopes::ResultsModelInterface(parent)
202 , m_scopes(scopes)
203 , m_categoryId(categoryId)
207QString ScopesOverviewResultsModel::categoryId() const
209 return m_categoryId;
212void ScopesOverviewResultsModel::setCategoryId(QString const& /*id*/)
214 qFatal("Calling un-implemented ScopesOverviewResultsModel::setCategoryId");
217int ScopesOverviewResultsModel::scopeIndex(QString const& id) const
219 const int scopeCount = count();
220 for (int i = 0; i < scopeCount; ++i) {
221 if (m_scopes[i]->id() == id)
222 return i;
223 }
224 return -1;
227QHash<int, QByteArray> ScopesOverviewResultsModel::roleNames() const
229 QHash<int, QByteArray> roles = unity::shell::scopes::ResultsModelInterface::roleNames();
230 roles[RoleBackground + 1] = "scopeId";
231 return roles;
234int ScopesOverviewResultsModel::rowCount(const QModelIndex& parent) const
236 Q_UNUSED(parent);
238 return m_scopes.count();
241int ScopesOverviewResultsModel::count() const
243 return rowCount();
246QVariant
247ScopesOverviewResultsModel::data(const QModelIndex& index, int role) const
249 unity::shell::scopes::ScopeInterface *scope = m_scopes[index.row()];
250 switch (role) {
251 case RoleUri:
252 case RoleCategoryId:
253 case RoleDndUri:
254 return QString();
255 case RoleResult:
256 return scope ? scope->id() : QString("Result.%1.%2").arg(categoryId()).arg(index.row());
257 case RoleTitle:
258 return scope ? scope->name() : QString("Title.%1.%2").arg(categoryId()).arg(index.row());
259 case RoleArt:
260 return qmlDirectory() + "graphics/applicationIcons/dash.png";
261 case RoleMascot:
262 case RoleEmblem:
263 case RoleSummary:
264 case RoleBackground + 1: // scopeId
265 return scope ? scope->id() : nullptr;
266 break;
267 default:
268 return QVariant();
269 }