on 0ec559b help sign in
unity8/tests/mocks/Unity/fake_scopes.cpp
279 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));
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));
75 m_scopesOverview = new ScopesOverview(this);
77 if (!m_loaded) {
78 m_loaded = true;
79 Q_EMIT loadedChanged();
80 Q_EMIT overviewScopeChanged();
81 }
84void Scopes::clear()
86 timer.stop();
87 if (m_scopes.size() > 0) {
88 beginRemoveRows(QModelIndex(), 0, m_scopes.count()-1);
89 qDeleteAll(m_allScopes);
90 m_allScopes.clear();
91 m_scopes.clear();
92 endRemoveRows();
93 }
94 delete m_scopesOverview;
95 m_scopesOverview = nullptr;
97 if (m_loaded) {
98 m_loaded = false;
99 Q_EMIT loadedChanged();
100 }
103void Scopes::clearFavorites()
105 if (m_scopes.size() > 0) {
106 beginRemoveRows(QModelIndex(), 0, m_scopes.count()-1);
107 Q_FOREACH(Scope *scope, m_scopes) {
108 m_scopesOverview->setFavorite(scope, false);
109 }
110 m_scopes.clear();
111 endRemoveRows();
112 }
115void Scopes::load()
117 timer.start();
120int Scopes::rowCount(const QModelIndex&) const
122 return m_scopes.count();
125QVariant Scopes::data(const QModelIndex& index, int role) const
127 if (!index.isValid() || index.row() >= m_scopes.size()) {
128 return QVariant();
129 }
131 Scope* scope = m_scopes.at(index.row());
133 if (role == Scopes::RoleScope) {
134 return QVariant::fromValue(scope);
135 } else if (role == Scopes::RoleId) {
136 return QVariant::fromValue(scope->id());
137 } else if (role == Scopes::RoleTitle) {
138 return QVariant::fromValue(scope->name());
139 } else {
140 return QVariant();
141 }
144unity::shell::scopes::ScopeInterface* Scopes::getScope(int row) const
146 if (row < 0 || row >= m_scopes.size()) {
147 return nullptr;
148 }
150 return m_scopes[row];
153unity::shell::scopes::ScopeInterface* Scopes::getScope(QString const &scope_id) const
155 // According to mh3 Scopes::getScope should only return favorite scopes (i.e the ones in the model)
156 for (Scope *scope : m_scopes) {
157 if (scope->id() == scope_id)
158 return scope;
159 }
160 return nullptr;
163Scope* Scopes::getScopeFromAll(const QString& scope_id) const
165 for (Scope *scope : m_allScopes) {
166 if (scope->id() == scope_id)
167 return scope;
168 }
169 return nullptr;
172QModelIndex Scopes::parent(const QModelIndex&) const
174 return QModelIndex();
177bool Scopes::loaded() const
179 return m_loaded;
182int Scopes::count() const
184 return rowCount();
187unity::shell::scopes::ScopeInterface* Scopes::overviewScope() const
189 return m_scopesOverview;
192void Scopes::addTempScope(unity::shell::scopes::ScopeInterface* scope)
194 m_tempScopes.insert(scope);
197void Scopes::closeScope(unity::shell::scopes::ScopeInterface* scope)
199 Q_ASSERT(m_tempScopes.contains(scope));
200 m_tempScopes.remove(scope);
203void Scopes::setFavorite(const QString& scopeId, bool favorite)
205 if (favorite) {
206 for (Scope *scope : m_scopes) {
207 // Check it's not already there
208 Q_ASSERT(scope->id() != scopeId);
209 }
210 for (Scope *scope : m_allScopes) {
211 if (scope->id() == scopeId) {
212 const int index = rowCount();
213 beginInsertRows(QModelIndex(), index, index);
214 m_scopes << scope;
215 endInsertRows();
216 m_scopesOverview->setFavorite(scope, true);
217 return;
218 }
219 }
220 Q_ASSERT(false && "Unknown scopeId");
221 } else {
222 for (Scope *scope : m_scopes) {
223 if (scope->id() == scopeId) {
224 const int index = m_scopes.indexOf(scope);
225 beginRemoveRows(QModelIndex(), index, index);
226 m_scopes.removeAt(index);
227 endRemoveRows();
228 m_scopesOverview->setFavorite(scope, false);
229 return;
230 }
231 }
232 Q_ASSERT(false && "Unknown scopeId");
233 }
236void Scopes::moveFavoriteTo(const QString& scopeId, int to)
238 int from = -1;
239 for (int i = 0; i < m_scopes.count(); ++i) {
240 if (m_scopes[i]->id() == scopeId) {
241 from = i;
242 break;
243 }
244 }
245 Q_ASSERT(from != -1);
246 beginMoveRows(QModelIndex(), from, from, QModelIndex(), to + (to > from ? 1 : 0));
247 m_scopes.move(from, to);
248 endMoveRows();
249 m_scopesOverview->moveFavoriteTo(m_scopes[to], to);
252QList<Scope*> Scopes::favScopes() const
254 return m_scopes;
257QList<Scope*> Scopes::nonFavScopes() const
259 QList<Scope*> res;
260 for (Scope *scope : m_allScopes) {
261 if (!m_scopes.contains(scope))
262 res << scope;
263 }
264 return res;
267void Scopes::addScope(Scope* scope)
269 int index = rowCount();
270 if (scope->favorite()) {
271 beginInsertRows(QModelIndex(), index, index);
272 m_scopes.append(scope);
273 endInsertRows();
274 connect(scope, &Scope::favoriteChanged, [this, scope]{
275 setFavorite(scope->id(), scope->favorite());
276 });
277 }
278 m_allScopes.append(scope);