on 0267639 help sign in
unity8/tests/mocks/Unity/fake_scopes.cpp
253 lines 6.8 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 <QTimer>
28Scopes::Scopes(QObject *parent)
29 : unity::shell::scopes::ScopesInterface(parent)
30 , m_scopesOverview(nullptr)
31 , m_loaded(false)
32 , timer(this)
34 timer.setSingleShot(true);
35 timer.setInterval(100);
36 QObject::connect(&timer, SIGNAL(timeout()), this, SLOT(updateScopes()));
38 QObject::connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SIGNAL(countChanged()));
39 QObject::connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)), this, SIGNAL(countChanged()));
40 QObject::connect(this, SIGNAL(modelReset()), this, SIGNAL(countChanged()));
42 load();
45Scopes::~Scopes()
49void Scopes::updateScopes()
51 clear();
52 addScope(new Scope("MockScope1", "People", true, this));
53 addScope(new Scope("MockScope2", "Music", false, this));
54 addScope(new Scope("clickscope", "Apps", true, this));
55 addScope(new Scope("MockScope5", "Videos", true, this));
56 addScope(new Scope("SingleCategoryScope", "Single", true, this, 1));
57 addScope(new Scope("MockScope4", "MS4", true, this));
58 addScope(new Scope("MockScope6", "MS6", true, this));
59 addScope(new Scope("MockScope7", "MS7", false, this));
60 addScope(new Scope("MockScope8", "MS8", false, this));
61 addScope(new Scope("MockScope9", "MS9", false, this));
62 addScope(new Scope("NullPreviewScope", "NPS", false, this, 1, true));
63 m_scopesOverview = new ScopesOverview(this);
65 if (!m_loaded) {
66 m_loaded = true;
67 Q_EMIT loadedChanged();
68 Q_EMIT overviewScopeChanged();
69 }
72void Scopes::clear()
74 timer.stop();
75 if (m_scopes.size() > 0) {
76 beginRemoveRows(QModelIndex(), 0, m_scopes.count()-1);
77 qDeleteAll(m_allScopes);
78 m_allScopes.clear();
79 m_scopes.clear();
80 endRemoveRows();
81 }
82 delete m_scopesOverview;
83 m_scopesOverview = nullptr;
85 if (m_loaded) {
86 m_loaded = false;
87 Q_EMIT loadedChanged();
88 }
91void Scopes::clearFavorites()
93 if (m_scopes.size() > 0) {
94 beginRemoveRows(QModelIndex(), 0, m_scopes.count()-1);
95 Q_FOREACH(Scope *scope, m_scopes) {
96 m_scopesOverview->setFavorite(scope, false);
97 }
98 m_scopes.clear();
99 endRemoveRows();
100 }
103void Scopes::load()
105 timer.start();
108int Scopes::rowCount(const QModelIndex&) const
110 return m_scopes.count();
113QVariant Scopes::data(const QModelIndex& index, int role) const
115 if (!index.isValid() || index.row() >= m_scopes.size()) {
116 return QVariant();
117 }
119 Scope* scope = m_scopes.at(index.row());
121 if (role == Scopes::RoleScope) {
122 return QVariant::fromValue(scope);
123 } else if (role == Scopes::RoleId) {
124 return QVariant::fromValue(scope->id());
125 } else if (role == Scopes::RoleTitle) {
126 return QVariant::fromValue(scope->name());
127 } else {
128 return QVariant();
129 }
132unity::shell::scopes::ScopeInterface* Scopes::getScope(int row) const
134 if (row < 0 || row >= m_scopes.size()) {
135 return nullptr;
136 }
138 return m_scopes[row];
141unity::shell::scopes::ScopeInterface* Scopes::getScope(QString const &scope_id) const
143 // According to mh3 Scopes::getScope should only return favorite scopes (i.e the ones in the model)
144 for (Scope *scope : m_scopes) {
145 if (scope->id() == scope_id)
146 return scope;
147 }
148 return nullptr;
151Scope* Scopes::getScopeFromAll(const QString& scope_id) const
153 for (Scope *scope : m_allScopes) {
154 if (scope->id() == scope_id)
155 return scope;
156 }
157 return nullptr;
160QModelIndex Scopes::parent(const QModelIndex&) const
162 return QModelIndex();
165bool Scopes::loaded() const
167 return m_loaded;
170int Scopes::count() const
172 return rowCount();
175unity::shell::scopes::ScopeInterface* Scopes::overviewScope() const
177 return m_scopesOverview;
180void Scopes::setFavorite(const QString& scopeId, bool favorite)
182 if (favorite) {
183 for (Scope *scope : m_scopes) {
184 // Check it's not already there
185 Q_ASSERT(scope->id() != scopeId);
186 }
187 for (Scope *scope : m_allScopes) {
188 if (scope->id() == scopeId) {
189 const int index = rowCount();
190 beginInsertRows(QModelIndex(), index, index);
191 m_scopes << scope;
192 endInsertRows();
193 m_scopesOverview->setFavorite(scope, true);
194 return;
195 }
196 }
197 Q_ASSERT(false && "Unknown scopeId");
198 } else {
199 for (Scope *scope : m_scopes) {
200 if (scope->id() == scopeId) {
201 const int index = m_scopes.indexOf(scope);
202 beginRemoveRows(QModelIndex(), index, index);
203 m_scopes.removeAt(index);
204 endRemoveRows();
205 m_scopesOverview->setFavorite(scope, false);
206 return;
207 }
208 }
209 Q_ASSERT(false && "Unknown scopeId");
210 }
213void Scopes::moveFavoriteTo(const QString& scopeId, int to)
215 int from = -1;
216 for (int i = 0; i < m_scopes.count(); ++i) {
217 if (m_scopes[i]->id() == scopeId) {
218 from = i;
219 break;
220 }
221 }
222 Q_ASSERT(from != -1);
223 beginMoveRows(QModelIndex(), from, from, QModelIndex(), to + (to > from ? 1 : 0));
224 m_scopes.move(from, to);
225 endMoveRows();
226 m_scopesOverview->moveFavoriteTo(m_scopes[to], to);
229QList<Scope*> Scopes::favScopes() const
231 return m_scopes;
234QList<Scope*> Scopes::nonFavScopes() const
236 QList<Scope*> res;
237 for (Scope *scope : m_allScopes) {
238 if (!m_scopes.contains(scope))
239 res << scope;
240 }
241 return res;
244void Scopes::addScope(Scope* scope)
246 int index = rowCount();
247 if (scope->favorite()) {
248 beginInsertRows(QModelIndex(), index, index);
249 m_scopes.append(scope);
250 endInsertRows();
251 }
252 m_allScopes.append(scope);