on 16fa310 help sign in
unity8/tests/mocks/Unity/fake_scopes.cpp
280 lines 7.7 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));
76 m_scopesOverview = new ScopesOverview(this);
78 if (!m_loaded) {
79 m_loaded = true;
80 Q_EMIT loadedChanged();
81 Q_EMIT overviewScopeChanged();
82 }
85void Scopes::clear()
87 timer.stop();
88 if (m_scopes.size() > 0) {
89 beginRemoveRows(QModelIndex(), 0, m_scopes.count()-1);
90 qDeleteAll(m_allScopes);
91 m_allScopes.clear();
92 m_scopes.clear();
93 endRemoveRows();
94 }
95 delete m_scopesOverview;
96 m_scopesOverview = nullptr;
98 if (m_loaded) {
99 m_loaded = false;
100 Q_EMIT loadedChanged();
101 }
104void Scopes::clearFavorites()
106 if (m_scopes.size() > 0) {
107 beginRemoveRows(QModelIndex(), 0, m_scopes.count()-1);
108 Q_FOREACH(Scope *scope, m_scopes) {
109 m_scopesOverview->setFavorite(scope, false);
110 }
111 m_scopes.clear();
112 endRemoveRows();
113 }
116void Scopes::load()
118 timer.start();
121int Scopes::rowCount(const QModelIndex&) const
123 return m_scopes.count();
126QVariant Scopes::data(const QModelIndex& index, int role) const
128 if (!index.isValid() || index.row() >= m_scopes.size()) {
129 return QVariant();
130 }
132 Scope* scope = m_scopes.at(index.row());
134 if (role == Scopes::RoleScope) {
135 return QVariant::fromValue(scope);
136 } else if (role == Scopes::RoleId) {
137 return QVariant::fromValue(scope->id());
138 } else if (role == Scopes::RoleTitle) {
139 return QVariant::fromValue(scope->name());
140 } else {
141 return QVariant();
142 }
145unity::shell::scopes::ScopeInterface* Scopes::getScope(int row) const
147 if (row < 0 || row >= m_scopes.size()) {
148 return nullptr;
149 }
151 return m_scopes[row];
154unity::shell::scopes::ScopeInterface* Scopes::getScope(QString const &scope_id) const
156 // According to mh3 Scopes::getScope should only return favorite scopes (i.e the ones in the model)
157 for (Scope *scope : m_scopes) {
158 if (scope->id() == scope_id)
159 return scope;
160 }
161 return nullptr;
164Scope* Scopes::getScopeFromAll(const QString& scope_id) const
166 for (Scope *scope : m_allScopes) {
167 if (scope->id() == scope_id)
168 return scope;
169 }
170 return nullptr;
173QModelIndex Scopes::parent(const QModelIndex&) const
175 return QModelIndex();
178bool Scopes::loaded() const
180 return m_loaded;
183int Scopes::count() const
185 return rowCount();
188unity::shell::scopes::ScopeInterface* Scopes::overviewScope() const
190 return m_scopesOverview;
193void Scopes::addTempScope(unity::shell::scopes::ScopeInterface* scope)
195 m_tempScopes.insert(scope);
198void Scopes::closeScope(unity::shell::scopes::ScopeInterface* scope)
200 Q_ASSERT(m_tempScopes.contains(scope));
201 m_tempScopes.remove(scope);
204void Scopes::setFavorite(const QString& scopeId, bool favorite)
206 if (favorite) {
207 for (Scope *scope : m_scopes) {
208 // Check it's not already there
209 Q_ASSERT(scope->id() != scopeId);
210 }
211 for (Scope *scope : m_allScopes) {
212 if (scope->id() == scopeId) {
213 const int index = rowCount();
214 beginInsertRows(QModelIndex(), index, index);
215 m_scopes << scope;
216 endInsertRows();
217 m_scopesOverview->setFavorite(scope, true);
218 return;
219 }
220 }
221 Q_ASSERT(false && "Unknown scopeId");
222 } else {
223 for (Scope *scope : m_scopes) {
224 if (scope->id() == scopeId) {
225 const int index = m_scopes.indexOf(scope);
226 beginRemoveRows(QModelIndex(), index, index);
227 m_scopes.removeAt(index);
228 endRemoveRows();
229 m_scopesOverview->setFavorite(scope, false);
230 return;
231 }
232 }
233 Q_ASSERT(false && "Unknown scopeId");
234 }
237void Scopes::moveFavoriteTo(const QString& scopeId, int to)
239 int from = -1;
240 for (int i = 0; i < m_scopes.count(); ++i) {
241 if (m_scopes[i]->id() == scopeId) {
242 from = i;
243 break;
244 }
245 }
246 Q_ASSERT(from != -1);
247 beginMoveRows(QModelIndex(), from, from, QModelIndex(), to + (to > from ? 1 : 0));
248 m_scopes.move(from, to);
249 endMoveRows();
250 m_scopesOverview->moveFavoriteTo(m_scopes[to], to);
253QList<Scope*> Scopes::favScopes() const
255 return m_scopes;
258QList<Scope*> Scopes::nonFavScopes() const
260 QList<Scope*> res;
261 for (Scope *scope : m_allScopes) {
262 if (!m_scopes.contains(scope))
263 res << scope;
264 }
265 return res;
268void Scopes::addScope(Scope* scope)
270 int index = rowCount();
271 if (scope->favorite()) {
272 beginInsertRows(QModelIndex(), index, index);
273 m_scopes.append(scope);
274 endInsertRows();
275 connect(scope, &Scope::favoriteChanged, [this, scope]{
276 setFavorite(scope->id(), scope->favorite());
277 });
278 }
279 m_allScopes.append(scope);