on 58095eb help sign in
unity8/tests/mocks/Unity/fake_scopes.cpp
161 lines 3.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_loaded(false)
31 , timer(this)
33 timer.setSingleShot(true);
34 timer.setInterval(100);
35 QObject::connect(&timer, SIGNAL(timeout()), this, SLOT(updateScopes()));
36 load();
39Scopes::~Scopes()
43void Scopes::updateScopes()
45 clear();
46 addScope(new Scope("MockScope1", "People", true, this));
47 addScope(new Scope("MockScope2", "Music", false, this));
48 addScope(new Scope("clickscope", "Apps", true, this));
49 addScope(new Scope("MockScope5", "Videos", true, this));
50 addScope(new ScopesOverview());
52 if (!m_loaded) {
53 m_loaded = true;
54 Q_EMIT loadedChanged();
55 }
58void Scopes::clear()
60 timer.stop();
61 if (m_scopes.size() > 0) {
62 beginRemoveRows(QModelIndex(), 0, m_scopes.count()-1);
63 qDeleteAll(m_scopes);
64 m_scopes.clear();
65 endRemoveRows();
66 }
68 if (m_loaded) {
69 m_loaded = false;
70 Q_EMIT loadedChanged();
71 }
74void Scopes::load()
76 timer.start();
79int Scopes::rowCount(const QModelIndex&) const
81 return m_scopes.count();
84QVariant Scopes::data(const QModelIndex& index, int role) const
86 if (!index.isValid() || index.row() >= m_scopes.size()) {
87 return QVariant();
88 }
90 Scope* scope = m_scopes.at(index.row());
92 if (role == Scopes::RoleScope) {
93 return QVariant::fromValue(scope);
94 } else if (role == Scopes::RoleId) {
95 return QVariant::fromValue(scope->id());
96 } else if (role == Scopes::RoleVisible) {
97 return QVariant::fromValue(scope->visible());
98 } else if (role == Scopes::RoleTitle) {
99 return QVariant::fromValue(scope->name());
100 } else {
101 return QVariant();
102 }
105unity::shell::scopes::ScopeInterface* Scopes::getScope(int row) const
107 if (row < 0 || row >= m_scopes.size()) {
108 return nullptr;
109 }
111 return m_scopes[row];
114unity::shell::scopes::ScopeInterface* Scopes::getScope(QString const &scope_id) const
116 for (Scope *scope : m_scopes) {
117 if (scope->id() == scope_id)
118 return scope;
119 }
120 return nullptr;
123QModelIndex Scopes::parent(const QModelIndex&) const
125 return QModelIndex();
128bool Scopes::loaded() const
130 return m_loaded;
133int Scopes::count(bool onlyVisible) const
135 int count = 0;
136 for (Scope *scope : m_scopes) {
137 if (!onlyVisible || scope->visible())
138 count++;
139 }
140 return count;
143Scope *Scopes::scopeAt(int index, bool onlyVisible) const
145 int count = 0;
146 for (Scope *scope : m_scopes) {
147 if (!onlyVisible || scope->visible()) {
148 if (index == count) return scope;
149 count++;
150 }
151 }
152 return nullptr;
155void Scopes::addScope(Scope* scope)
157 int index = rowCount();
158 beginInsertRows(QModelIndex(), index, index);
159 m_scopes.append(scope);
160 endInsertRows();