on 3bcda1f help sign in
unity8/tests/mocks/Unity/fake_scopes.cpp
133 lines 3.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"
22// TODO: Implement remaining pieces, like Categories (i.e. LensView now gives warnings)
24// Qt
25#include <QTimer>
27Scopes::Scopes(QObject *parent)
28 : unity::shell::scopes::ScopesInterface(parent)
29 , m_loaded(false)
30 , timer(this)
32 timer.setSingleShot(true);
33 timer.setInterval(100);
34 QObject::connect(&timer, SIGNAL(timeout()), this, SLOT(updateScopes()));
35 load();
38Scopes::~Scopes()
42void Scopes::updateScopes()
44 clear();
45 addScope(new Scope("MockScope1", "People", true, this));
46 addScope(new Scope("MockScope2", "Music", false, this));
47 addScope(new Scope("clickscope", "Apps", true, this));
48 addScope(new Scope("MockScope5", "Videos", true, this));
50 if (!m_loaded) {
51 m_loaded = true;
52 Q_EMIT loadedChanged();
53 }
56void Scopes::clear()
58 timer.stop();
59 if (m_scopes.size() > 0) {
60 beginRemoveRows(QModelIndex(), 0, m_scopes.count()-1);
61 qDeleteAll(m_scopes);
62 m_scopes.clear();
63 endRemoveRows();
64 }
66 if (m_loaded) {
67 m_loaded = false;
68 Q_EMIT loadedChanged();
69 }
72void Scopes::load()
74 timer.start();
77int Scopes::rowCount(const QModelIndex&) const
79 return m_scopes.count();
82QVariant Scopes::data(const QModelIndex& index, int role) const
84 if (!index.isValid() || index.row() >= m_scopes.size()) {
85 return QVariant();
86 }
88 Scope* scope = m_scopes.at(index.row());
90 if (role == Scopes::RoleScope) {
91 return QVariant::fromValue(scope);
92 } else if (role == Scopes::RoleId) {
93 return QVariant::fromValue(scope->id());
94 } else if (role == Scopes::RoleVisible) {
95 return QVariant::fromValue(scope->visible());
96 } else if (role == Scopes::RoleTitle) {
97 return QVariant::fromValue(scope->name());
98 } else {
99 return QVariant();
100 }
103unity::shell::scopes::ScopeInterface* Scopes::getScope(int row) const
105 if (row < 0 || row >= m_scopes.size()) {
106 return nullptr;
107 }
109 return m_scopes[row];
112unity::shell::scopes::ScopeInterface* Scopes::getScope(QString const&) const
114 return nullptr;
117QModelIndex Scopes::parent(const QModelIndex&) const
119 return QModelIndex();
122bool Scopes::loaded() const
124 return m_loaded;
127void Scopes::addScope(Scope* scope)
129 int index = rowCount();
130 beginInsertRows(QModelIndex(), index, index);
131 m_scopes.append(scope);
132 endInsertRows();