on master help sign in
unity8/tests/mocks/Unity/fake_scope.cpp
372 lines 8.9 kB history raw
1/*
2 * Copyright (C) 2013, 2014, 2015 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 <QDebug>
18#include <QUrl>
20#include "fake_scope.h"
22#include "fake_filters.h"
23#include "fake_navigation.h"
24#include "fake_optionselectorfilter.h"
25#include "fake_resultsmodel.h"
26#include "fake_scopes.h"
27#include "fake_settingsmodel.h"
29#include <paths.h> // from unity8/include
31Scope::Scope(Scopes* parent) : Scope("MockScope5", "Mock Scope", false, parent)
35Scope::Scope(QString const& id, QString const& name, bool favorite, Scopes* parent, int categories, bool returnNullPreview, const QStringList primaryNavigationFilterOptionLabels)
36 : unity::shell::scopes::ScopeInterface(parent)
37 , m_id(id)
38 , m_name(name)
39 , m_searching(false)
40 , m_favorite(favorite)
41 , m_isActive(false)
42 , m_hasBeenActive(false)
43 , m_hasNavigation(true)
44 , m_hasPrimaryFilter(true)
45 , m_currentNavigationId("root")
46 , m_previewRendererName("preview-generic")
47 , m_categories(new Categories(categories, this))
48 , m_openScope(nullptr)
49 , m_settings(new SettingsModel(this))
50 , m_filters(new Filters(this))
51 , m_returnNullPreview(returnNullPreview)
53 m_primaryNavigationFilter = new FakeOptionSelectorFilter("OSF3", "PFTag", "Which food you like More", false, primaryNavigationFilterOptionLabels, this);
54 m_filters->addFakeFilters();
55 connect(m_filters, &Filters::activeFiltersCountChanged, this, &Scope::activeFiltersCountChanged);
56 connect(m_primaryNavigationFilter, &FakeOptionSelectorFilter::isActiveChanged,
57 this, &unity::shell::scopes::ScopeInterface::primaryNavigationTagChanged);
60QString Scope::id() const
62 return m_id;
65QString Scope::name() const
67 return m_name;
70QString Scope::searchQuery() const
72 return m_searchQuery;
75QString Scope::iconHint() const
77 return m_iconHint;
80QString Scope::description() const
82 return m_description;
85QString Scope::searchHint() const
87 return QString("Search %1").arg(m_name);
90QString Scope::shortcut() const
92 return QString("");
95bool Scope::searchInProgress() const
97 return m_searching;
100bool Scope::activationInProgress() const
102 return false;
105bool Scope::favorite() const
107 return m_favorite;
110unity::shell::scopes::CategoriesInterface* Scope::categories() const
112 return m_hasBeenActive ? m_categories : nullptr;
115unity::shell::scopes::SettingsModelInterface* Scope::settings() const
117 return m_settings;
120QString Scope::noResultsHint() const
122 return m_noResultsHint;
125QString Scope::formFactor() const
127 return m_formFactor;
130bool Scope::isActive() const
132 return m_isActive;
135void Scope::setSearchQuery(const QString &str)
137 if (str != m_searchQuery) {
138 m_searchQuery = str;
139 Q_EMIT searchQueryChanged();
140 }
143void Scope::setFormFactor(const QString &str)
145 if (str != m_formFactor) {
146 m_formFactor = str;
147 Q_EMIT formFactorChanged();
148 }
151void Scope::setActive(const bool active)
153 if (active != m_isActive) {
154 m_isActive = active;
155 Q_EMIT isActiveChanged();
156 }
157 if (active && !m_hasBeenActive) {
158 m_hasBeenActive = true;
159 Q_EMIT categoriesChanged();
160 }
163void Scope::setFavorite(const bool favorite)
165 if (favorite != m_favorite) {
166 m_favorite = favorite;
167 Q_EMIT favoriteChanged();
168 }
171void Scope::setId(const QString &id)
173 if (id != m_id) {
174 m_id = id;
175 Q_EMIT idChanged();
176 }
179void Scope::setName(const QString &name)
181 if (name != m_name) {
182 m_name = name;
183 Q_EMIT nameChanged();
184 }
187void Scope::setSearchInProgress(const bool inProg)
189 if (inProg != m_searching) {
190 m_searching = inProg;
191 Q_EMIT searchInProgressChanged();
192 }
195void Scope::setNoResultsHint(const QString& str)
197 if (str != m_noResultsHint) {
198 m_noResultsHint = str;
199 Q_EMIT noResultsHintChanged();
200 }
203void Scope::activate(QVariant const& result, QString const& categoryId)
205 qDebug() << "Called activate on scope" << m_id << "with result" << result << "and category" << categoryId;
206 if (result.toString() == "Result.2.2") {
207 Scopes *scopes = static_cast<Scopes*>(parent());
208 m_openScope = scopes->getScopeFromAll("MockScope9");
209 scopes->addTempScope(m_openScope);
210 Q_EMIT openScope(m_openScope);
211 } else {
212 Q_EMIT previewRequested(result);
213 }
216PreviewModel* Scope::preview(QVariant const& result, QString const& /*categoryId*/)
218 Q_UNUSED(result);
220 if (m_returnNullPreview) {
221 return nullptr;
222 } else {
223 return new PreviewModel(this, this);
224 }
227void Scope::cancelActivation()
231void Scope::closeScope(unity::shell::scopes::ScopeInterface* scope)
233 Scopes *scopes = dynamic_cast<Scopes*>(parent());
234 if (scopes) {
235 return scopes->closeScope(scope);
236 }
239QString Scope::currentNavigationId() const
241 return m_currentNavigationId;
244bool Scope::hasNavigation() const
246 return m_hasNavigation;
249void Scope::setHasNavigation(bool hasNavigation)
251 m_hasNavigation = hasNavigation;
252 Q_EMIT hasNavigationChanged();
255Scope::Status Scope::status() const
257 return Status::Okay;
260QVariantMap Scope::customizations() const
262 QVariantMap m, h;
263 if (m_id == "clickscope") {
264 h["foreground-color"] = "yellow";
265 m["background-color"] = "red";
266 m["foreground-color"] = "blue";
267 m["page-header"] = h;
268 } else if (m_id == "MockScope4") {
269 h["navigation-background"] = QUrl(testDataDir() + "/qmltests/Dash/artwork/background.png");
270 m["page-header"] = h;
271 } else if (m_id == "MockScope5") {
272 h["background"] = "gradient:///lightgrey/grey";
273 h["logo"] = QUrl(testDataDir() + "/qmltests/Dash/tst_PageHeader/logo-ubuntu-orange.svg");
274 h["divider-color"] = "red";
275 h["navigation-background"] = "color:///black";
276 m["page-header"] = h;
277 } else if (m_id == "MockScope6") {
278 h["navigation-background"] = "gradient:///blue/red";
279 m["page-header"] = h;
280 }
281 return m;
284void Scope::refresh()
286 Q_EMIT refreshed();
289void Scope::resetFilters()
291 qWarning() << "Scope::resetFilters is unimplemented";
294void Scope::activateAction(QVariant const& /*result*/, QString const& /*categoryId*/, QString const& /*actionId*/)
296 qFatal("Using Scope::activateAction");
299unity::shell::scopes::NavigationInterface* Scope::getNavigation(const QString& id)
301 if (id.isEmpty())
302 return nullptr;
304 QString parentId;
305 QString parentLabel;
306 if (id.startsWith("middle")) {
307 parentId = "root";
308 parentLabel = "root";
309 } else if (id.startsWith("child")) {
310 parentId = id.mid(5, 7);
311 parentLabel = parentId;
312 }
313 return new Navigation(id, id, "all"+id, parentId, parentLabel, this);
316void Scope::setNavigationState(const QString &navigationId)
318 m_currentNavigationId = navigationId;
319 Q_EMIT currentNavigationIdChanged();
320 Q_EMIT primaryNavigationTagChanged();
323unity::shell::scopes::FilterBaseInterface* Scope::primaryNavigationFilter() const
325 return m_hasPrimaryFilter ? m_primaryNavigationFilter : nullptr;
328unity::shell::scopes::FiltersInterface* Scope::filters() const
330 return m_filters;
333QString Scope::primaryNavigationTag() const
335 if (m_hasPrimaryFilter && m_primaryNavigationFilter->isActive())
336 return m_primaryNavigationFilter->filterTag();
337 if (m_currentNavigationId == "root")
338 return QString();
339 else
340 return const_cast<Scope*>(this)->getNavigation(m_currentNavigationId)->label();
343int Scope::activeFiltersCount() const
345 return m_filters->activeFiltersCount();
348void Scope::resetPrimaryNavigationTag()
350 if (m_hasPrimaryFilter && m_primaryNavigationFilter->isActive()) {
351 m_primaryNavigationFilter->clear();
352 }
354 if (m_currentNavigationId != "root") {
355 setNavigationState("root");
356 }
359void Scope::setHasPrimaryFilter(bool hasPrimaryFilter)
361 m_hasPrimaryFilter = hasPrimaryFilter;
362 Q_EMIT primaryNavigationFilterChanged();
365void Scope::performQuery(const QString& query)
367 Q_EMIT queryPerformed(query);
368 if (query.startsWith("scopes://")) {
369 QString scopeId = query.mid(9);
370 Q_EMIT gotoScope(scopeId);
371 }