on 2492425 help sign in
unity8/tests/mocks/Unity/fake_scopes.cpp
148 lines 3.6 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: QAbstractListModel(parent)
29, m_loaded(false)
30, timer(this)
32 m_roles[Scopes::RoleScope] = "scope";
33 m_roles[Scopes::RoleId] = "id";
34 m_roles[Scopes::RoleVisible] = "visible";
35 m_roles[Scopes::RoleTitle] = "title";
37 QObject::connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SIGNAL(countChanged()));
38 QObject::connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)), this, SIGNAL(countChanged()));
39 QObject::connect(this, SIGNAL(modelReset()), this, SIGNAL(countChanged()));
41 timer.setSingleShot(true);
42 timer.setInterval(100);
43 QObject::connect(&timer, SIGNAL(timeout()), this, SLOT(updateScopes()));
44 load();
47Scopes::~Scopes()
51void Scopes::updateScopes()
53 clear();
54 addScope(new Scope("MockScope1", "People", true, this));
55 addScope(new Scope("MockScope2", "Music", false, this));
56 addScope(new Scope("clickscope", "Apps", true, this));
57 addScope(new Scope("MockScope5", "Videos", true, this));
59 if (!m_loaded) {
60 m_loaded = true;
61 Q_EMIT loadedChanged(m_loaded);
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 }
75 if (m_loaded) {
76 m_loaded = false;
77 Q_EMIT loadedChanged(m_loaded);
78 }
81void Scopes::load()
83 timer.start();
86QHash<int, QByteArray> Scopes::roleNames() const
88 return m_roles;
91int Scopes::rowCount(const QModelIndex&) const
93 return m_scopes.count();
96QVariant Scopes::data(const QModelIndex& index, int role) const
98 if (!index.isValid() || index.row() >= m_scopes.size()) {
99 return QVariant();
100 }
102 Scope* scope = m_scopes.at(index.row());
104 if (role == Scopes::RoleScope) {
105 return QVariant::fromValue(scope);
106 } else if (role == Scopes::RoleId) {
107 return QVariant::fromValue(scope->id());
108 } else if (role == Scopes::RoleVisible) {
109 return QVariant::fromValue(scope->visible());
110 } else if (role == Scopes::RoleTitle) {
111 return QVariant::fromValue(scope->name());
112 } else {
113 return QVariant();
114 }
117QVariant Scopes::get(int row) const
119 return data(QAbstractListModel::index(row), 0);
122QVariant Scopes::get(QString const&) const
124 return QVariant();
127QModelIndex Scopes::parent(const QModelIndex&) const
129 return QModelIndex();
132bool Scopes::loaded() const
134 return m_loaded;
137int Scopes::count() const
139 return rowCount();
142void Scopes::addScope(Scope* scope)
144 int index = rowCount();
145 beginInsertRows(QModelIndex(), index, index);
146 m_scopes.append(scope);
147 endInsertRows();