on 21b035c help sign in
unity8/tests/mocks/Unity/fake_scopes.cpp
167 lines 4.1 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", false, this));
52 addScope(new Scope("MockScope4", "MS4", false, this));
53 addScope(new Scope("MockScope6", "MS6", false, this));
54 m_scopesOverview = new ScopesOverview(this);
56 if (!m_loaded) {
57 m_loaded = true;
58 Q_EMIT loadedChanged();
59 }
62void Scopes::clear()
64 timer.stop();
65 if (m_scopes.size() > 0) {
66 beginRemoveRows(QModelIndex(), 0, m_scopes.count()-1);
67 qDeleteAll(m_scopes);
68 m_scopes.clear();
69 endRemoveRows();
70 }
71 delete m_scopesOverview;
73 if (m_loaded) {
74 m_loaded = false;
75 Q_EMIT loadedChanged();
76 }
79void Scopes::load()
81 timer.start();
84int Scopes::rowCount(const QModelIndex&) const
86 return m_scopes.count();
89QVariant Scopes::data(const QModelIndex& index, int role) const
91 if (!index.isValid() || index.row() >= m_scopes.size()) {
92 return QVariant();
93 }
95 Scope* scope = m_scopes.at(index.row());
97 if (role == Scopes::RoleScope) {
98 return QVariant::fromValue(scope);
99 } else if (role == Scopes::RoleId) {
100 return QVariant::fromValue(scope->id());
101 } else if (role == Scopes::RoleVisible) {
102 return QVariant::fromValue(scope->visible());
103 } else if (role == Scopes::RoleTitle) {
104 return QVariant::fromValue(scope->name());
105 } else {
106 return QVariant();
107 }
110unity::shell::scopes::ScopeInterface* Scopes::getScope(int row) const
112 if (row < 0 || row >= m_scopes.size()) {
113 return nullptr;
114 }
116 return m_scopes[row];
119unity::shell::scopes::ScopeInterface* Scopes::getScope(QString const &scope_id) const
121 if (scope_id == "scopesOverview") return m_scopesOverview;
122 for (Scope *scope : m_scopes) {
123 if (scope->id() == scope_id)
124 return scope;
125 }
126 return nullptr;
129QModelIndex Scopes::parent(const QModelIndex&) const
131 return QModelIndex();
134bool Scopes::loaded() const
136 return m_loaded;
139int Scopes::count(bool onlyVisible) const
141 int count = 0;
142 for (Scope *scope : m_scopes) {
143 if (!onlyVisible || scope->visible())
144 count++;
145 }
146 return count;
149Scope *Scopes::scopeAt(int index, bool onlyVisible) const
151 int count = 0;
152 for (Scope *scope : m_scopes) {
153 if (!onlyVisible || scope->visible()) {
154 if (index == count) return scope;
155 count++;
156 }
157 }
158 return nullptr;
161void Scopes::addScope(Scope* scope)
163 int index = rowCount();
164 beginInsertRows(QModelIndex(), index, index);
165 m_scopes.append(scope);
166 endInsertRows();