on 770ffe6 help sign in
unity8/tests/mocks/Unity/fake_scopes.cpp
160 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", 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;
75 m_scopesOverview = nullptr;
77 if (m_loaded) {
78 m_loaded = false;
79 Q_EMIT loadedChanged();
80 }
83void Scopes::load()
85 timer.start();
88int Scopes::rowCount(const QModelIndex&) const
90 return m_scopes.count();
93QVariant Scopes::data(const QModelIndex& index, int role) const
95 if (!index.isValid() || index.row() >= m_scopes.size()) {
96 return QVariant();
97 }
99 Scope* scope = m_scopes.at(index.row());
101 if (role == Scopes::RoleScope) {
102 return QVariant::fromValue(scope);
103 } else if (role == Scopes::RoleId) {
104 return QVariant::fromValue(scope->id());
105 } else if (role == Scopes::RoleVisible) {
106 return QVariant::fromValue(scope->visible());
107 } else if (role == Scopes::RoleTitle) {
108 return QVariant::fromValue(scope->name());
109 } else {
110 return QVariant();
111 }
114unity::shell::scopes::ScopeInterface* Scopes::getScope(int row) const
116 if (row < 0 || row >= m_scopes.size()) {
117 return nullptr;
118 }
120 return m_scopes[row];
123unity::shell::scopes::ScopeInterface* Scopes::getScope(QString const &scope_id) const
125 if (scope_id == "scopesOverview") return m_scopesOverview;
126 for (Scope *scope : m_scopes) {
127 if (scope->id() == scope_id)
128 return scope;
129 }
130 return nullptr;
133QModelIndex Scopes::parent(const QModelIndex&) const
135 return QModelIndex();
138bool Scopes::loaded() const
140 return m_loaded;
143QList<unity::shell::scopes::ScopeInterface *> Scopes::scopes(bool onlyVisible) const
145 QList<unity::shell::scopes::ScopeInterface *> res;
146 for (Scope *scope : m_scopes) {
147 if (!onlyVisible || scope->visible()) {
148 res << scope;
149 }
150 }
151 return res;
154void Scopes::addScope(Scope* scope)
156 int index = rowCount();
157 beginInsertRows(QModelIndex(), index, index);
158 m_scopes.append(scope);
159 endInsertRows();