on f20331b help sign in
unity8/tests/mocks/Unity/fake_scopes.cpp
286 lines 7.9 kB history raw
1/*
2 * Copyright (C) 2013 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 *
16 * Author: Nick Dedekind <nick.dedekind@canonical.com>
17 */
19// Self
20#include "fake_scopes.h"
21#include "fake_scopesoverview.h"
23// TODO: Implement remaining pieces, like Categories (i.e. LensView now gives warnings)
25// Qt
26#include <QDebug>
27#include <QTimer>
29Scopes::Scopes(QObject *parent)
30 : unity::shell::scopes::ScopesInterface(parent)
31 , m_scopesOverview(nullptr)
32 , m_loaded(false)
33 , timer(this)
35 timer.setSingleShot(true);
36 timer.setInterval(100);
37 QObject::connect(&timer, &QTimer::timeout, this, &Scopes::updateScopes);
39 QObject::connect(this, &Scopes::rowsInserted, this, &Scopes::countChanged);
40 QObject::connect(this, &Scopes::rowsRemoved, this, &Scopes::countChanged);
41 QObject::connect(this, &Scopes::modelReset, this, &Scopes::countChanged);
43 load();
46Scopes::~Scopes()
50void Scopes::updateScopes()
52 clear();
53 addScope(new Scope("MockScope1", "People", true, this));
54 addScope(new Scope("MockScope2", "Music", false, this));
55 addScope(new Scope("clickscope", "Apps", true, this));
56 addScope(new Scope("MockScope5", "Videos this is long ab cd ef gh ij kl", true, this));
57 addScope(new Scope("SingleCategoryScope", "Single", true, this, 1));
58 addScope(new Scope("MockScope4", "MS4", true, this));
59 addScope(new Scope("MockScope6", "MS6", true, this));
60 addScope(new Scope("MockScope7", "MS7", false, this));
61 addScope(new Scope("MockScope8", "MS8", false, this));
62 addScope(new Scope("MockScope9", "MS9", false, this));
64 Scope *longNavigationScope = new Scope(
65 "LongPrimaryNavigation", "LongPrimaryNavigation", true, this, 1, false,
66 QStringList() << "Meat" << "Vegetables" << "BBQ" << "Burritos"
67 << "Pasta" << "Pancakes" << "Seafood" << "Soup" << "Bugs"
68 << "Pudding" << "Chocolate" << "Gluten-Free" << "Hamburgers"
69 << "Pho" << "Panang Curry" << "Lumpia" << "Sushi");
71 longNavigationScope->setHasNavigation(false);
72 addScope(longNavigationScope);
74 addScope(new Scope("NullPreviewScope", "NPS", false, this, 1, true));
76 // Add enough scopes to test autoscrolling on the scopes managment page
77 for (int i = 0; i < 20; i++) {
78 addScope(new Scope("UselessScope" + QString::number(i),
79 "US" + QString::number(i), true, this));
80 }
82 m_scopesOverview = new ScopesOverview(this);
84 if (!m_loaded) {
85 m_loaded = true;
86 Q_EMIT loadedChanged();
87 Q_EMIT overviewScopeChanged();
88 }
91void Scopes::clear()
93 timer.stop();
94 if (m_scopes.size() > 0) {
95 beginRemoveRows(QModelIndex(), 0, m_scopes.count()-1);
96 qDeleteAll(m_allScopes);
97 m_allScopes.clear();
98 m_scopes.clear();
99 endRemoveRows();
100 }
101 delete m_scopesOverview;
102 m_scopesOverview = nullptr;
104 if (m_loaded) {
105 m_loaded = false;
106 Q_EMIT loadedChanged();
107 }
110void Scopes::clearFavorites()
112 if (m_scopes.size() > 0) {
113 beginRemoveRows(QModelIndex(), 0, m_scopes.count()-1);
114 Q_FOREACH(Scope *scope, m_scopes) {
115 m_scopesOverview->setFavorite(scope, false);
116 }
117 m_scopes.clear();
118 endRemoveRows();
119 }
122void Scopes::load()
124 timer.start();
127int Scopes::rowCount(const QModelIndex&) const
129 return m_scopes.count();
132QVariant Scopes::data(const QModelIndex& index, int role) const
134 if (!index.isValid() || index.row() >= m_scopes.size()) {
135 return QVariant();
136 }
138 Scope* scope = m_scopes.at(index.row());
140 if (role == Scopes::RoleScope) {
141 return QVariant::fromValue(scope);
142 } else if (role == Scopes::RoleId) {
143 return QVariant::fromValue(scope->id());
144 } else if (role == Scopes::RoleTitle) {
145 return QVariant::fromValue(scope->name());
146 } else {
147 return QVariant();
148 }
151unity::shell::scopes::ScopeInterface* Scopes::getScope(int row) const
153 if (row < 0 || row >= m_scopes.size()) {
154 return nullptr;
155 }
157 return m_scopes[row];
160unity::shell::scopes::ScopeInterface* Scopes::getScope(QString const &scope_id) const
162 // According to mh3 Scopes::getScope should only return favorite scopes (i.e the ones in the model)
163 for (Scope *scope : m_scopes) {
164 if (scope->id() == scope_id)
165 return scope;
166 }
167 return nullptr;
170Scope* Scopes::getScopeFromAll(const QString& scope_id) const
172 for (Scope *scope : m_allScopes) {
173 if (scope->id() == scope_id)
174 return scope;
175 }
176 return nullptr;
179QModelIndex Scopes::parent(const QModelIndex&) const
181 return QModelIndex();
184bool Scopes::loaded() const
186 return m_loaded;
189int Scopes::count() const
191 return rowCount();
194unity::shell::scopes::ScopeInterface* Scopes::overviewScope() const
196 return m_scopesOverview;
199void Scopes::addTempScope(unity::shell::scopes::ScopeInterface* scope)
201 m_tempScopes.insert(scope);
204void Scopes::closeScope(unity::shell::scopes::ScopeInterface* scope)
206 Q_ASSERT(m_tempScopes.contains(scope));
207 m_tempScopes.remove(scope);
210void Scopes::setFavorite(const QString& scopeId, bool favorite)
212 if (favorite) {
213 for (Scope *scope : m_scopes) {
214 // Check it's not already there
215 Q_ASSERT(scope->id() != scopeId);
216 }
217 for (Scope *scope : m_allScopes) {
218 if (scope->id() == scopeId) {
219 const int index = rowCount();
220 beginInsertRows(QModelIndex(), index, index);
221 m_scopes << scope;
222 endInsertRows();
223 m_scopesOverview->setFavorite(scope, true);
224 return;
225 }
226 }
227 Q_ASSERT(false && "Unknown scopeId");
228 } else {
229 for (Scope *scope : m_scopes) {
230 if (scope->id() == scopeId) {
231 const int index = m_scopes.indexOf(scope);
232 beginRemoveRows(QModelIndex(), index, index);
233 m_scopes.removeAt(index);
234 endRemoveRows();
235 m_scopesOverview->setFavorite(scope, false);
236 return;
237 }
238 }
239 Q_ASSERT(false && "Unknown scopeId");
240 }
243void Scopes::moveFavoriteTo(const QString& scopeId, int to)
245 int from = -1;
246 for (int i = 0; i < m_scopes.count(); ++i) {
247 if (m_scopes[i]->id() == scopeId) {
248 from = i;
249 break;
250 }
251 }
252 Q_ASSERT(from != -1);
253 beginMoveRows(QModelIndex(), from, from, QModelIndex(), to + (to > from ? 1 : 0));
254 m_scopes.move(from, to);
255 endMoveRows();
256 m_scopesOverview->moveFavoriteTo(m_scopes[to], to);
259QList<Scope*> Scopes::favScopes() const
261 return m_scopes;
264QList<Scope*> Scopes::nonFavScopes() const
266 QList<Scope*> res;
267 for (Scope *scope : m_allScopes) {
268 if (!m_scopes.contains(scope))
269 res << scope;
270 }
271 return res;
274void Scopes::addScope(Scope* scope)
276 int index = rowCount();
277 if (scope->favorite()) {
278 beginInsertRows(QModelIndex(), index, index);
279 m_scopes.append(scope);
280 endInsertRows();
281 connect(scope, &Scope::favoriteChanged, this, [this, scope]{
282 setFavorite(scope->id(), scope->favorite());
283 });
284 }
285 m_allScopes.append(scope);