on bc9e93f help sign in
unity8/tests/mocks/Unity/fake_scopes.cpp
287 lines 8.0 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));
63 addScope(new Scope("libertine-scope.ubuntu_libertine-scope", "Libertine", true, this));
65 Scope *longNavigationScope = new Scope(
66 "LongPrimaryNavigation", "LongPrimaryNavigation", true, this, 1, false,
67 QStringList() << "Meat" << "Vegetables" << "BBQ" << "Burritos"
68 << "Pasta" << "Pancakes" << "Seafood" << "Soup" << "Bugs"
69 << "Pudding" << "Chocolate" << "Gluten-Free" << "Hamburgers"
70 << "Pho" << "Panang Curry" << "Lumpia" << "Sushi");
72 longNavigationScope->setHasNavigation(false);
73 addScope(longNavigationScope);
75 addScope(new Scope("NullPreviewScope", "NPS", false, this, 1, true));
77 // Add enough scopes to test autoscrolling on the scopes managment page
78 for (int i = 0; i < 20; i++) {
79 addScope(new Scope("UselessScope" + QString::number(i),
80 "US" + QString::number(i), true, this));
81 }
83 m_scopesOverview = new ScopesOverview(this);
85 if (!m_loaded) {
86 m_loaded = true;
87 Q_EMIT loadedChanged();
88 Q_EMIT overviewScopeChanged();
89 }
92void Scopes::clear()
94 timer.stop();
95 if (m_scopes.size() > 0) {
96 beginRemoveRows(QModelIndex(), 0, m_scopes.count()-1);
97 qDeleteAll(m_allScopes);
98 m_allScopes.clear();
99 m_scopes.clear();
100 endRemoveRows();
101 }
102 delete m_scopesOverview;
103 m_scopesOverview = nullptr;
105 if (m_loaded) {
106 m_loaded = false;
107 Q_EMIT loadedChanged();
108 }
111void Scopes::clearFavorites()
113 if (m_scopes.size() > 0) {
114 beginRemoveRows(QModelIndex(), 0, m_scopes.count()-1);
115 Q_FOREACH(Scope *scope, m_scopes) {
116 m_scopesOverview->setFavorite(scope, false);
117 }
118 m_scopes.clear();
119 endRemoveRows();
120 }
123void Scopes::load()
125 timer.start();
128int Scopes::rowCount(const QModelIndex&) const
130 return m_scopes.count();
133QVariant Scopes::data(const QModelIndex& index, int role) const
135 if (!index.isValid() || index.row() >= m_scopes.size()) {
136 return QVariant();
137 }
139 Scope* scope = m_scopes.at(index.row());
141 if (role == Scopes::RoleScope) {
142 return QVariant::fromValue(scope);
143 } else if (role == Scopes::RoleId) {
144 return QVariant::fromValue(scope->id());
145 } else if (role == Scopes::RoleTitle) {
146 return QVariant::fromValue(scope->name());
147 } else {
148 return QVariant();
149 }
152unity::shell::scopes::ScopeInterface* Scopes::getScope(int row) const
154 if (row < 0 || row >= m_scopes.size()) {
155 return nullptr;
156 }
158 return m_scopes[row];
161unity::shell::scopes::ScopeInterface* Scopes::getScope(QString const &scope_id) const
163 // According to mh3 Scopes::getScope should only return favorite scopes (i.e the ones in the model)
164 for (Scope *scope : m_scopes) {
165 if (scope->id() == scope_id)
166 return scope;
167 }
168 return nullptr;
171Scope* Scopes::getScopeFromAll(const QString& scope_id) const
173 for (Scope *scope : m_allScopes) {
174 if (scope->id() == scope_id)
175 return scope;
176 }
177 return nullptr;
180QModelIndex Scopes::parent(const QModelIndex&) const
182 return QModelIndex();
185bool Scopes::loaded() const
187 return m_loaded;
190int Scopes::count() const
192 return rowCount();
195unity::shell::scopes::ScopeInterface* Scopes::overviewScope() const
197 return m_scopesOverview;
200void Scopes::addTempScope(unity::shell::scopes::ScopeInterface* scope)
202 m_tempScopes.insert(scope);
205void Scopes::closeScope(unity::shell::scopes::ScopeInterface* scope)
207 Q_ASSERT(m_tempScopes.contains(scope));
208 m_tempScopes.remove(scope);
211void Scopes::setFavorite(const QString& scopeId, bool favorite)
213 if (favorite) {
214 for (Scope *scope : m_scopes) {
215 // Check it's not already there
216 Q_ASSERT(scope->id() != scopeId);
217 }
218 for (Scope *scope : m_allScopes) {
219 if (scope->id() == scopeId) {
220 const int index = rowCount();
221 beginInsertRows(QModelIndex(), index, index);
222 m_scopes << scope;
223 endInsertRows();
224 m_scopesOverview->setFavorite(scope, true);
225 return;
226 }
227 }
228 Q_ASSERT(false && "Unknown scopeId");
229 } else {
230 for (Scope *scope : m_scopes) {
231 if (scope->id() == scopeId) {
232 const int index = m_scopes.indexOf(scope);
233 beginRemoveRows(QModelIndex(), index, index);
234 m_scopes.removeAt(index);
235 endRemoveRows();
236 m_scopesOverview->setFavorite(scope, false);
237 return;
238 }
239 }
240 Q_ASSERT(false && "Unknown scopeId");
241 }
244void Scopes::moveFavoriteTo(const QString& scopeId, int to)
246 int from = -1;
247 for (int i = 0; i < m_scopes.count(); ++i) {
248 if (m_scopes[i]->id() == scopeId) {
249 from = i;
250 break;
251 }
252 }
253 Q_ASSERT(from != -1);
254 beginMoveRows(QModelIndex(), from, from, QModelIndex(), to + (to > from ? 1 : 0));
255 m_scopes.move(from, to);
256 endMoveRows();
257 m_scopesOverview->moveFavoriteTo(m_scopes[to], to);
260QList<Scope*> Scopes::favScopes() const
262 return m_scopes;
265QList<Scope*> Scopes::nonFavScopes() const
267 QList<Scope*> res;
268 for (Scope *scope : m_allScopes) {
269 if (!m_scopes.contains(scope))
270 res << scope;
271 }
272 return res;
275void Scopes::addScope(Scope* scope)
277 int index = rowCount();
278 if (scope->favorite()) {
279 beginInsertRows(QModelIndex(), index, index);
280 m_scopes.append(scope);
281 endInsertRows();
282 connect(scope, &Scope::favoriteChanged, this, [this, scope]{
283 setFavorite(scope->id(), scope->favorite());
284 });
285 }
286 m_allScopes.append(scope);