on 3e61bb2 help sign in
unity8/tests/mocks/Unity/fake_scopes.cpp
152 lines 3.7 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// Local
23#include "fake_applications_scope.h"
25// TODO: Implement remaining pieces, like Categories (i.e. LensView now gives warnings)
27// Qt
28#include <QTimer>
30Scopes::Scopes(QObject *parent)
31: QAbstractListModel(parent)
32, m_loaded(false)
33, timer(this)
35 m_roles[Scopes::RoleScope] = "scope";
36 m_roles[Scopes::RoleId] = "id";
37 m_roles[Scopes::RoleVisible] = "visible";
38 m_roles[Scopes::RoleTitle] = "title";
40 QObject::connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SIGNAL(countChanged()));
41 QObject::connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)), this, SIGNAL(countChanged()));
42 QObject::connect(this, SIGNAL(modelReset()), this, SIGNAL(countChanged()));
44 timer.setSingleShot(true);
45 timer.setInterval(100);
46 QObject::connect(&timer, SIGNAL(timeout()), this, SLOT(updateScopes()));
47 load();
50Scopes::~Scopes()
54void Scopes::updateScopes()
56 clear();
57 addScope(new Scope("MockScope1", "People", true, this));
58 addScope(new Scope("MockScope2", "Music", false, this));
59 addScope(new Scope("home.scope", "Home", true, this));
60 addScope(new ApplicationsScope(true, this));
61 addScope(new Scope("MockScope5", "Videos", true, this));
63 if (!m_loaded) {
64 m_loaded = true;
65 Q_EMIT loadedChanged(m_loaded);
66 }
69void Scopes::clear()
71 timer.stop();
72 if (m_scopes.size() > 0) {
73 beginRemoveRows(QModelIndex(), 0, m_scopes.count()-1);
74 qDeleteAll(m_scopes);
75 m_scopes.clear();
76 endRemoveRows();
77 }
79 if (m_loaded) {
80 m_loaded = false;
81 Q_EMIT loadedChanged(m_loaded);
82 }
85void Scopes::load()
87 timer.start();
90QHash<int, QByteArray> Scopes::roleNames() const
92 return m_roles;
95int Scopes::rowCount(const QModelIndex&) const
97 return m_scopes.count();
100QVariant Scopes::data(const QModelIndex& index, int role) const
102 if (!index.isValid() || index.row() >= m_scopes.size()) {
103 return QVariant();
104 }
106 Scope* scope = m_scopes.at(index.row());
108 if (role == Scopes::RoleScope) {
109 return QVariant::fromValue(scope);
110 } else if (role == Scopes::RoleId) {
111 return QVariant::fromValue(scope->id());
112 } else if (role == Scopes::RoleVisible) {
113 return QVariant::fromValue(scope->visible());
114 } else if (role == Scopes::RoleTitle) {
115 return QVariant::fromValue(scope->name());
116 } else {
117 return QVariant();
118 }
121QVariant Scopes::get(int row) const
123 return data(QAbstractListModel::index(row), 0);
126QVariant Scopes::get(QString const&) const
128 return QVariant();
131QModelIndex Scopes::parent(const QModelIndex&) const
133 return QModelIndex();
136bool Scopes::loaded() const
138 return m_loaded;
141int Scopes::count() const
143 return rowCount();
146void Scopes::addScope(Scope* scope)
148 int index = rowCount();
149 beginInsertRows(QModelIndex(), index, index);
150 m_scopes.append(scope);
151 endInsertRows();