on da009e4 help sign in
unity8/tests/mocks/Unity/fake_scopesoverview.cpp
325 lines 10 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);
56ScopesOverviewCategories::ScopesOverviewCategories(Scopes *scopes, QObject* parent)
57 : unity::shell::scopes::CategoriesInterface(parent)
58 , m_scopes(scopes)
62int ScopesOverviewCategories::rowCount(const QModelIndex& /*parent*/) const
64 return 2;
67void ScopesOverviewCategories::addSpecialCategory(QString const&, QString const&, QString const&, QString const&, QObject*)
69 qFatal("Using un-implemented ScopesOverviewCategories::addSpecialCategory");
72bool ScopesOverviewCategories::overrideCategoryJson(QString const& /* categoryId */, QString const& /* json */)
74 qFatal("Using un-implemented ScopesOverviewCategories::overrideCategoryJson");
77void ScopesOverviewCategories::setFavorite(Scope *scope, bool favorite)
79 if (m_resultsModels.value(0)) {
80 if (favorite) {
81 m_resultsModels[0]->appendScope(scope);
82 } else {
83 m_resultsModels[0]->removeScope(scope);
84 }
85 }
86 if (m_resultsModels.value(1)) {
87 if (favorite) {
88 m_resultsModels[1]->removeScope(scope);
89 } else {
90 m_resultsModels[1]->appendScope(scope);
91 }
92 }
95QVariant
96ScopesOverviewCategories::data(const QModelIndex& index, int role) const
98 if (!index.isValid()) {
99 return QVariant();
100 }
102 const QString categoryId = index.row() == 0 ? "favorites" : "nonfavorites";
104 ScopesOverviewResultsModel *resultsModel = m_resultsModels[index.row()];
105 if (!resultsModel) {
106 QObject *that = const_cast<ScopesOverviewCategories*>(this);
107 QList<Scope*> scopes = index.row() == 0 ? m_scopes->favScopes() : m_scopes->nonFavScopes();
108 resultsModel = new ScopesOverviewResultsModel(scopes, categoryId, that);
109 m_resultsModels[index.row()] = resultsModel;
110 }
111 switch (role) {
112 case RoleCategoryId:
113 return categoryId;
114 case RoleName:
115 return index.row() == 0 ? "Favorites" : "Non Favorites";
116 case RoleIcon:
117 return QVariant();
118 case RoleRawRendererTemplate:
119 qFatal("Using un-implemented RoleRawRendererTemplate Categories role");
120 return QVariant();
121 case RoleRenderer:
122 {
123 QVariantMap map;
124 map["category-layout"] = "grid";
125 map["card-size"] = "small";
126 map["overlay"] = true;
127 return map;
128 }
129 case RoleComponents:
130 {
131 QVariantMap map, artMap, attributesMap;
132 artMap["aspect-ratio"] = "0.5";
133 artMap["field"] = "art";
134 map["art"] = artMap;
135 map["title"] = "HOLA";
136 map["attributes"] = attributesMap;
137 return map;
138 }
139 case RoleResults:
140 return QVariant::fromValue(resultsModel);
141 case RoleCount:
142 return resultsModel->rowCount();
143 case RoleHeaderLink:
144 return QString();
145 default:
146 qFatal("Using un-implemented Categories role");
147 return QVariant();
148 }
153ScopesOverviewSearchCategories::ScopesOverviewSearchCategories(Scopes *scopes, QObject* parent)
154 : unity::shell::scopes::CategoriesInterface(parent)
155 , m_scopes(scopes)
159int ScopesOverviewSearchCategories::rowCount(const QModelIndex& /*parent*/) const
161 return 2;
164void ScopesOverviewSearchCategories::addSpecialCategory(QString const&, QString const&, QString const&, QString const&, QObject*)
166 qFatal("Using un-implemented ScopesOverviewSearchCategories::addSpecialCategory");
169bool ScopesOverviewSearchCategories::overrideCategoryJson(QString const& /* categoryId */, QString const& /* json */)
171 qFatal("Using un-implemented ScopesOverviewSearchCategories::overrideCategoryJson");
174QVariant
175ScopesOverviewSearchCategories::data(const QModelIndex& index, int role) const
177 if (!index.isValid()) {
178 return QVariant();
179 }
181 const QString categoryId = index.row() == 0 ? "searchA" : "searchB";
183 ScopesOverviewResultsModel *resultsModel = m_resultsModels[index.row()];
184 if (!resultsModel) {
185 QObject *that = const_cast<ScopesOverviewSearchCategories*>(this);
186 QList<Scope *> scopes;
187 if (index.row() == 0) {
188 scopes << m_scopes->getScopeFromAll("clickscope") << nullptr << m_scopes->getScopeFromAll("MockScope2");
189 } else {
190 scopes << nullptr << m_scopes->getScopeFromAll("MockScope7") << nullptr << m_scopes->getScopeFromAll("MockScope1");
191 }
192 resultsModel = new ScopesOverviewResultsModel(scopes, categoryId, that);
193 m_resultsModels[index.row()] = resultsModel;
194 }
195 switch (role) {
196 case RoleCategoryId:
197 return categoryId;
198 case RoleName:
199 return index.row() == 0 ? "SearchA" : "SearchB";
200 case RoleIcon:
201 return QVariant();
202 case RoleRawRendererTemplate:
203 qFatal("Using un-implemented RoleRawRendererTemplate Categories role");
204 return QVariant();
205 case RoleRenderer:
206 {
207 QVariantMap map;
208 map["category-layout"] = "grid";
209 map["card-size"] = "small";
210 map["overlay"] = true;
211 return map;
212 }
213 case RoleComponents:
214 {
215 QVariantMap map, artMap, attributesMap;
216 artMap["aspect-ratio"] = "1";
217 artMap["field"] = "art";
218 map["art"] = artMap;
219 map["title"] = "HOLA";
220 map["attributes"] = attributesMap;
221 return map;
222 }
223 case RoleResults:
224 return QVariant::fromValue(resultsModel);
225 case RoleCount:
226 return resultsModel->rowCount();
227 case RoleHeaderLink:
228 return QString();
229 default:
230 qFatal("Using un-implemented Categories role");
231 return QVariant();
232 }
236ScopesOverviewResultsModel::ScopesOverviewResultsModel(const QList<Scope *> &scopes, const QString &categoryId, QObject* parent)
237 : unity::shell::scopes::ResultsModelInterface(parent)
238 , m_scopes(scopes)
239 , m_categoryId(categoryId)
243QString ScopesOverviewResultsModel::categoryId() const
245 return m_categoryId;
248void ScopesOverviewResultsModel::setCategoryId(QString const& /*id*/)
250 qFatal("Calling un-implemented ScopesOverviewResultsModel::setCategoryId");
253int ScopesOverviewResultsModel::scopeIndex(QString const& id) const
255 const int scopeCount = count();
256 for (int i = 0; i < scopeCount; ++i) {
257 if (m_scopes[i]->id() == id)
258 return i;
259 }
260 return -1;
263QHash<int, QByteArray> ScopesOverviewResultsModel::roleNames() const
265 QHash<int, QByteArray> roles = unity::shell::scopes::ResultsModelInterface::roleNames();
266 roles[RoleBackground + 1] = "scopeId";
267 return roles;
270int ScopesOverviewResultsModel::rowCount(const QModelIndex& parent) const
272 Q_UNUSED(parent);
274 return m_scopes.count();
277int ScopesOverviewResultsModel::count() const
279 return rowCount();
282QVariant ScopesOverviewResultsModel::data(const QModelIndex& index, int role) const
284 unity::shell::scopes::ScopeInterface *scope = m_scopes[index.row()];
285 switch (role) {
286 case RoleUri:
287 case RoleCategoryId:
288 case RoleDndUri:
289 return QString();
290 case RoleResult:
291 return scope ? scope->id() : QString("Result.%1.%2").arg(categoryId()).arg(index.row());
292 case RoleTitle:
293 return scope ? scope->name() : QString("Title.%1.%2").arg(categoryId()).arg(index.row());
294 case RoleArt:
295 return qmlDirectory() + "graphics/applicationIcons/dash.png";
296 case RoleMascot:
297 case RoleEmblem:
298 case RoleSummary:
299 case RoleBackground + 1: // scopeId
300 return scope ? scope->id() : nullptr;
301 break;
302 default:
303 return QVariant();
304 }
307void ScopesOverviewResultsModel::appendScope(Scope *scope)
309 Q_ASSERT(!m_scopes.contains(scope));
310 const int index = rowCount();
311 beginInsertRows(QModelIndex(), index, index);
312 m_scopes << scope;
313 endInsertRows();
314 Q_EMIT countChanged();
317void ScopesOverviewResultsModel::removeScope(Scope *scope)
319 const int index = m_scopes.indexOf(scope);
320 Q_ASSERT(index != -1);
321 beginRemoveRows(QModelIndex(), index, index);
322 m_scopes.removeAt(index);
323 endRemoveRows();
324 Q_EMIT countChanged();