on 217aeab help sign in
unity8/tests/mocks/Unity/fake_scopes.cpp
186 lines 4.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 <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 m_scopesOverview = new ScopesOverview(this);
64 if (!m_loaded) {
65 m_loaded = true;
66 Q_EMIT loadedChanged();
67 Q_EMIT overviewScopeChanged();
68 }
71void Scopes::clear()
73 timer.stop();
74 if (m_scopes.size() > 0) {
75 beginRemoveRows(QModelIndex(), 0, m_scopes.count()-1);
76 qDeleteAll(m_allScopes);
77 m_allScopes.clear();
78 m_scopes.clear();
79 endRemoveRows();
80 }
81 delete m_scopesOverview;
82 m_scopesOverview = nullptr;
84 if (m_loaded) {
85 m_loaded = false;
86 Q_EMIT loadedChanged();
87 }
90void Scopes::load()
92 timer.start();
95int Scopes::rowCount(const QModelIndex&) const
97 return m_scopes.count();
100QVariant Scopes::data(const QModelIndex& index, int role) const
102 if (!index.isValid() || index.row() >= m_scopes.size()) {
103 return QVariant();
104 }
106 Scope* scope = m_scopes.at(index.row());
108 if (role == Scopes::RoleScope) {
109 return QVariant::fromValue(scope);
110 } else if (role == Scopes::RoleId) {
111 return QVariant::fromValue(scope->id());
112 } else if (role == Scopes::RoleTitle) {
113 return QVariant::fromValue(scope->name());
114 } else {
115 return QVariant();
116 }
119unity::shell::scopes::ScopeInterface* Scopes::getScope(int row) const
121 if (row < 0 || row >= m_scopes.size()) {
122 return nullptr;
123 }
125 return m_scopes[row];
128unity::shell::scopes::ScopeInterface* Scopes::getScope(QString const &scope_id) const
130 // According to mh3 Scopes::getScope should only return favorite scopes (i.e the ones in the model)
131 for (Scope *scope : m_scopes) {
132 if (scope->id() == scope_id)
133 return scope;
134 }
135 return nullptr;
138Scope* Scopes::getScopeFromAll(const QString& scope_id) const
140 for (Scope *scope : m_allScopes) {
141 if (scope->id() == scope_id)
142 return scope;
143 }
144 return nullptr;
147QModelIndex Scopes::parent(const QModelIndex&) const
149 return QModelIndex();
152bool Scopes::loaded() const
154 return m_loaded;
157int Scopes::count() const
159 return rowCount();
162unity::shell::scopes::ScopeInterface* Scopes::overviewScope() const
164 return m_scopesOverview;
167QList<Scope*> Scopes::scopes() const
169 return m_scopes;
172QList<Scope*> Scopes::allScopes() const
174 return m_allScopes;
177void Scopes::addScope(Scope* scope)
179 int index = rowCount();
180 if (scope->favorite()) {
181 beginInsertRows(QModelIndex(), index, index);
182 m_scopes.append(scope);
183 endInsertRows();
184 }
185 m_allScopes.append(scope);