on 76feda2 help sign in
unity8/tests/mocks/Unity/fake_scopes.cpp
268 lines 7.2 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("NullPreviewScope", "NPS", false, this, 1, true));
64 m_scopesOverview = new ScopesOverview(this);
66 if (!m_loaded) {
67 m_loaded = true;
68 Q_EMIT loadedChanged();
69 Q_EMIT overviewScopeChanged();
70 }
73void Scopes::clear()
75 timer.stop();
76 if (m_scopes.size() > 0) {
77 beginRemoveRows(QModelIndex(), 0, m_scopes.count()-1);
78 qDeleteAll(m_allScopes);
79 m_allScopes.clear();
80 m_scopes.clear();
81 endRemoveRows();
82 }
83 delete m_scopesOverview;
84 m_scopesOverview = nullptr;
86 if (m_loaded) {
87 m_loaded = false;
88 Q_EMIT loadedChanged();
89 }
92void Scopes::clearFavorites()
94 if (m_scopes.size() > 0) {
95 beginRemoveRows(QModelIndex(), 0, m_scopes.count()-1);
96 Q_FOREACH(Scope *scope, m_scopes) {
97 m_scopesOverview->setFavorite(scope, false);
98 }
99 m_scopes.clear();
100 endRemoveRows();
101 }
104void Scopes::load()
106 timer.start();
109int Scopes::rowCount(const QModelIndex&) const
111 return m_scopes.count();
114QVariant Scopes::data(const QModelIndex& index, int role) const
116 if (!index.isValid() || index.row() >= m_scopes.size()) {
117 return QVariant();
118 }
120 Scope* scope = m_scopes.at(index.row());
122 if (role == Scopes::RoleScope) {
123 return QVariant::fromValue(scope);
124 } else if (role == Scopes::RoleId) {
125 return QVariant::fromValue(scope->id());
126 } else if (role == Scopes::RoleTitle) {
127 return QVariant::fromValue(scope->name());
128 } else {
129 return QVariant();
130 }
133unity::shell::scopes::ScopeInterface* Scopes::getScope(int row) const
135 if (row < 0 || row >= m_scopes.size()) {
136 return nullptr;
137 }
139 return m_scopes[row];
142unity::shell::scopes::ScopeInterface* Scopes::getScope(QString const &scope_id) const
144 // According to mh3 Scopes::getScope should only return favorite scopes (i.e the ones in the model)
145 for (Scope *scope : m_scopes) {
146 if (scope->id() == scope_id)
147 return scope;
148 }
149 return nullptr;
152Scope* Scopes::getScopeFromAll(const QString& scope_id) const
154 for (Scope *scope : m_allScopes) {
155 if (scope->id() == scope_id)
156 return scope;
157 }
158 return nullptr;
161QModelIndex Scopes::parent(const QModelIndex&) const
163 return QModelIndex();
166bool Scopes::loaded() const
168 return m_loaded;
171int Scopes::count() const
173 return rowCount();
176unity::shell::scopes::ScopeInterface* Scopes::overviewScope() const
178 return m_scopesOverview;
181void Scopes::addTempScope(unity::shell::scopes::ScopeInterface* scope)
183 m_tempScopes.insert(scope);
186void Scopes::closeScope(unity::shell::scopes::ScopeInterface* scope)
188 Q_ASSERT(m_tempScopes.contains(scope));
189 m_tempScopes.remove(scope);
192void Scopes::setFavorite(const QString& scopeId, bool favorite)
194 if (favorite) {
195 for (Scope *scope : m_scopes) {
196 // Check it's not already there
197 Q_ASSERT(scope->id() != scopeId);
198 }
199 for (Scope *scope : m_allScopes) {
200 if (scope->id() == scopeId) {
201 const int index = rowCount();
202 beginInsertRows(QModelIndex(), index, index);
203 m_scopes << scope;
204 endInsertRows();
205 m_scopesOverview->setFavorite(scope, true);
206 return;
207 }
208 }
209 Q_ASSERT(false && "Unknown scopeId");
210 } else {
211 for (Scope *scope : m_scopes) {
212 if (scope->id() == scopeId) {
213 const int index = m_scopes.indexOf(scope);
214 beginRemoveRows(QModelIndex(), index, index);
215 m_scopes.removeAt(index);
216 endRemoveRows();
217 m_scopesOverview->setFavorite(scope, false);
218 return;
219 }
220 }
221 Q_ASSERT(false && "Unknown scopeId");
222 }
225void Scopes::moveFavoriteTo(const QString& scopeId, int to)
227 int from = -1;
228 for (int i = 0; i < m_scopes.count(); ++i) {
229 if (m_scopes[i]->id() == scopeId) {
230 from = i;
231 break;
232 }
233 }
234 Q_ASSERT(from != -1);
235 beginMoveRows(QModelIndex(), from, from, QModelIndex(), to + (to > from ? 1 : 0));
236 m_scopes.move(from, to);
237 endMoveRows();
238 m_scopesOverview->moveFavoriteTo(m_scopes[to], to);
241QList<Scope*> Scopes::favScopes() const
243 return m_scopes;
246QList<Scope*> Scopes::nonFavScopes() const
248 QList<Scope*> res;
249 for (Scope *scope : m_allScopes) {
250 if (!m_scopes.contains(scope))
251 res << scope;
252 }
253 return res;
256void Scopes::addScope(Scope* scope)
258 int index = rowCount();
259 if (scope->favorite()) {
260 beginInsertRows(QModelIndex(), index, index);
261 m_scopes.append(scope);
262 endInsertRows();
263 connect(scope, &Scope::favoriteChanged, [this, scope]{
264 setFavorite(scope->id(), scope->favorite());
265 });
266 }
267 m_allScopes.append(scope);