on c7abf64 help sign in
unity8/tests/mocks/Unity/fake_scopesoverview.cpp
186 lines 5.7 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 m_searchCategories = m_categories; // save the usual categories as search result
27 m_scopesOverviewCategories = new ScopesOverviewCategories(parent, this);
28 m_categories = m_scopesOverviewCategories;
31void ScopesOverview::setSearchQuery(const QString& search_query)
33 Scope::setSearchQuery(search_query);
35 auto origCategories = m_categories;
36 if (search_query.isEmpty()) m_categories = m_scopesOverviewCategories;
37 else m_categories = m_searchCategories;
39 if (m_categories != origCategories)
40 Q_EMIT categoriesChanged();
44ScopesOverviewCategories::ScopesOverviewCategories(Scopes *scopes, QObject* parent)
45 : unity::shell::scopes::CategoriesInterface(parent)
46 , m_scopes(scopes)
50int ScopesOverviewCategories::rowCount(const QModelIndex& /*parent*/) const
52 return 2;
55void ScopesOverviewCategories::addSpecialCategory(QString const&, QString const&, QString const&, QString const&, QObject*)
57 qFatal("Using un-implemented ScopesOverviewCategories::addSpecialCategory");
60bool ScopesOverviewCategories::overrideCategoryJson(QString const& /* categoryId */, QString const& /* json */)
62 qFatal("Using un-implemented ScopesOverviewCategories::overrideCategoryJson");
65QVariant
66ScopesOverviewCategories::data(const QModelIndex& index, int role) const
68 if (!index.isValid()) {
69 return QVariant();
70 }
72 unity::shell::scopes::ResultsModelInterface *resultsModel = m_resultsModels[index.row()];
73 if (!resultsModel) {
74 QObject *that = const_cast<ScopesOverviewCategories*>(this);
75 resultsModel = new ScopesOverviewResultsModel(m_scopes, index.row() == 0, that);
76 m_resultsModels[index.row()] = resultsModel;
77 }
78 switch (role) {
79 case RoleCategoryId:
80 return index.row() == 0 ? "favorites" : "all";
81 case RoleName:
82 return index.row() == 0 ? "Favorites" : "All";
83 case RoleIcon:
84 return QVariant();
85 case RoleRawRendererTemplate:
86 qFatal("Using un-implemented RoleRawRendererTemplate Categories role");
87 return QVariant();
88 case RoleRenderer:
89 {
90 QVariantMap map;
91 map["category-layout"] = "grid";
92 map["card-size"] = "small";
93 map["overlay"] = true;
94 return map;
95 }
96 case RoleComponents:
97 {
98 QVariantMap map, artMap;
99 artMap["aspect-ratio"] = "0.5";
100 artMap["field"] = "art";
101 map["art"] = artMap;
102 map["title"] = "HOLA";
103 return map;
104 }
105 case RoleResults:
106 return QVariant::fromValue(resultsModel);
107 case RoleCount:
108 return resultsModel->rowCount();
109 default:
110 qFatal("Using un-implemented Categories role");
111 return QVariant();
112 }
117ScopesOverviewResultsModel::ScopesOverviewResultsModel(Scopes *scopes, bool isFavoriteCategory, QObject* parent)
118 : unity::shell::scopes::ResultsModelInterface(parent)
119 , m_scopes(scopes)
120 , m_isFavoriteCategory(isFavoriteCategory)
124QString ScopesOverviewResultsModel::categoryId() const
126 return m_isFavoriteCategory ? "favorites" : "all";
129void ScopesOverviewResultsModel::setCategoryId(QString const& /*id*/)
131 qFatal("Calling un-implemented ScopesOverviewResultsModel::setCategoryId");
134int ScopesOverviewResultsModel::scopeIndex(QString const& id) const
136 const int scopeCount = count();
137 for (int i = 0; i < scopeCount; ++i) {
138 if (m_scopes->scopeAt(i, m_isFavoriteCategory)->id() == id)
139 return i;
140 }
141 return -1;
144QHash<int, QByteArray> ScopesOverviewResultsModel::roleNames() const
146 QHash<int, QByteArray> roles = unity::shell::scopes::ResultsModelInterface::roleNames();
147 roles[RoleBackground + 1] = "scopeId";
148 return roles;
151int ScopesOverviewResultsModel::rowCount(const QModelIndex& parent) const
153 Q_UNUSED(parent);
155 return m_scopes->count(m_isFavoriteCategory);
158int ScopesOverviewResultsModel::count() const
160 return rowCount();
163QVariant
164ScopesOverviewResultsModel::data(const QModelIndex& index, int role) const
166 switch (role) {
167 case RoleUri:
168 case RoleCategoryId:
169 case RoleDndUri:
170 return QString();
171 case RoleResult:
172 return m_scopes->scopeAt(index.row(), m_isFavoriteCategory)->id();
173 case RoleTitle:
174 return m_scopes->scopeAt(index.row(), m_isFavoriteCategory)->name();
175 case RoleArt:
176 return qmlDirectory() + "graphics/applicationIcons/dash.png";
177 case RoleMascot:
178 case RoleEmblem:
179 case RoleSummary:
180 case RoleBackground + 1: // scopeId
181 return m_scopes->scopeAt(index.row(), m_isFavoriteCategory)->id();
182 break;
183 default:
184 return QVariant();
185 }