on 6fe90db help sign in
unity8/tests/mocks/Unity/fake_scopes.cpp
170 lines 4.3 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()));
37 load();
40Scopes::~Scopes()
44void Scopes::updateScopes()
46 clear();
47 addScope(new Scope("MockScope1", "People", true, this));
48 addScope(new Scope("MockScope2", "Music", false, this));
49 addScope(new Scope("clickscope", "Apps", true, this));
50 addScope(new Scope("MockScope5", "Videos", true, this));
51 addScope(new Scope("MockScope3", "MS3", true, this));
52 addScope(new Scope("MockScope4", "MS4", true, this));
53 addScope(new Scope("MockScope6", "MS6", true, this));
54 addScope(new Scope("MockScope7", "MS7", false, this));
55 addScope(new Scope("MockScope8", "MS8", false, this));
56 addScope(new Scope("MockScope9", "MS9", false, this));
57 m_scopesOverview = new ScopesOverview(this);
59 if (!m_loaded) {
60 m_loaded = true;
61 Q_EMIT loadedChanged();
62 }
65void Scopes::clear()
67 timer.stop();
68 if (m_scopes.size() > 0) {
69 beginRemoveRows(QModelIndex(), 0, m_scopes.count()-1);
70 qDeleteAll(m_scopes);
71 m_scopes.clear();
72 endRemoveRows();
73 }
74 delete m_scopesOverview;
76 if (m_loaded) {
77 m_loaded = false;
78 Q_EMIT loadedChanged();
79 }
82void Scopes::load()
84 timer.start();
87int Scopes::rowCount(const QModelIndex&) const
89 return m_scopes.count();
92QVariant Scopes::data(const QModelIndex& index, int role) const
94 if (!index.isValid() || index.row() >= m_scopes.size()) {
95 return QVariant();
96 }
98 Scope* scope = m_scopes.at(index.row());
100 if (role == Scopes::RoleScope) {
101 return QVariant::fromValue(scope);
102 } else if (role == Scopes::RoleId) {
103 return QVariant::fromValue(scope->id());
104 } else if (role == Scopes::RoleVisible) {
105 return QVariant::fromValue(scope->visible());
106 } else if (role == Scopes::RoleTitle) {
107 return QVariant::fromValue(scope->name());
108 } else {
109 return QVariant();
110 }
113unity::shell::scopes::ScopeInterface* Scopes::getScope(int row) const
115 if (row < 0 || row >= m_scopes.size()) {
116 return nullptr;
117 }
119 return m_scopes[row];
122unity::shell::scopes::ScopeInterface* Scopes::getScope(QString const &scope_id) const
124 if (scope_id == "scopesOverview") return m_scopesOverview;
125 for (Scope *scope : m_scopes) {
126 if (scope->id() == scope_id)
127 return scope;
128 }
129 return nullptr;
132QModelIndex Scopes::parent(const QModelIndex&) const
134 return QModelIndex();
137bool Scopes::loaded() const
139 return m_loaded;
142int Scopes::count(bool onlyVisible) const
144 int count = 0;
145 for (Scope *scope : m_scopes) {
146 if (!onlyVisible || scope->visible())
147 count++;
148 }
149 return count;
152Scope *Scopes::scopeAt(int index, bool onlyVisible) const
154 int count = 0;
155 for (Scope *scope : m_scopes) {
156 if (!onlyVisible || scope->visible()) {
157 if (index == count) return scope;
158 count++;
159 }
160 }
161 return nullptr;
164void Scopes::addScope(Scope* scope)
166 int index = rowCount();
167 beginInsertRows(QModelIndex(), index, index);
168 m_scopes.append(scope);
169 endInsertRows();