@@ -1,179 +0,0 @@
1−/*
2− * Copyright (C) 2011 Canonical, Ltd.
3− *
4− * Authors:
5− * Florian Boucault <florian.boucault@canonical.com>
6− *
7− * This program is free software; you can redistribute it and/or modify
8− * it under the terms of the GNU General Public License as published by
9− * the Free Software Foundation; version 3.
10− *
11− * This program is distributed in the hope that it will be useful,
12− * but WITHOUT ANY WARRANTY; without even the implied warranty of
13− * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14− * GNU General Public License for more details.
15− *
16− * You should have received a copy of the GNU General Public License
17− * along with this program. If not, see <http://www.gnu.org/licenses/>.
18− */
19−
20−// Self
21−#include "scopes.h"
22−
23−// Local
24−#include "scope.h"
25−
26−// Qt
27−#include <QDebug>
28−#include <QtCore/QStringList>
29−#include <QtGui/QKeySequence>
30−
31−Scopes::Scopes(QObject *parent)
32− : QAbstractListModel(parent)
33− , m_unityScopes(std::make_shared<unity::dash::GSettingsScopes>())
34− , m_loaded(false)
35−{
36− m_roles[Scopes::RoleScope] = "scope";
37− m_roles[Scopes::RoleId] = "id";
38− m_roles[Scopes::RoleVisible] = "visible";
39− m_roles[Scopes::RoleTitle] = "title";
40−
41− m_unityScopes->scope_added.connect(sigc::mem_fun(this, &Scopes::onScopeAdded));
42− m_unityScopes->scope_removed.connect(sigc::mem_fun(this, &Scopes::onScopeRemoved));
43− m_unityScopes->scopes_reordered.connect(sigc::mem_fun(this, &Scopes::onScopesReordered));
44− m_unityScopes->LoadScopes();
45−}
46−
47−QHash<int, QByteArray> Scopes::roleNames() const
48−{
49− return m_roles;
50−}
51−
52−int Scopes::rowCount(const QModelIndex& parent) const
53−{
54− Q_UNUSED(parent)
55−
56− return m_scopes.count();
57−}
58−
59−QVariant Scopes::data(const QModelIndex& index, int role) const
60−{
61− Q_UNUSED(role)
62−
63− if (!index.isValid()) {
64− return QVariant();
65− }
66−
67− Scope* scope = m_scopes.at(index.row());
68−
69− if (role == Scopes::RoleScope) {
70− return QVariant::fromValue(scope);
71− } else if (role == Scopes::RoleId) {
72− return QVariant::fromValue(scope->id());
73− } else if (role == Scopes::RoleVisible) {
74− return QVariant::fromValue(scope->visible());
75− } else if (role == Scopes::RoleTitle) {
76− return scope->name();
77− } else {
78− return QVariant();
79− }
80−}
81−
82−QVariant Scopes::get(int row) const
83−{
84− return data(QAbstractListModel::index(row), 0);
85−}
86−
87−QVariant Scopes::get(const QString& scope_id) const
88−{
89− Q_FOREACH(Scope* scope, m_scopes) {
90− if (scope->id() == scope_id) {
91− return QVariant::fromValue(scope);
92− }
93− }
94−
95− return QVariant();
96−}
97−
98−bool Scopes::loaded() const
99−{
100− return m_loaded;
101−}
102−
103−void Scopes::onScopeAdded(const unity::dash::Scope::Ptr& scope, int /*position*/)
104−{
105− int index = m_scopes.count();
106− beginInsertRows(QModelIndex(), index, index);
107− addUnityScope(scope);
108− endInsertRows();
109−
110− // FIXME: do only once after all loaded?
111− m_loaded = true;
112− Q_EMIT loadedChanged(m_loaded);
113−}
114−
115−void Scopes::onScopesLoaded()
116−{
117−}
118−
119−void Scopes::onScopeRemoved(const unity::dash::Scope::Ptr& scope)
120−{
121− auto id = QString::fromStdString(scope->id);
122− auto index = findScopeById(id);
123− if (index >= 0) {
124− beginRemoveRows(QModelIndex(), index, index);
125− removeUnityScope(index);
126− endRemoveRows();
127−
128− Q_EMIT scopeRemoved(id, index);
129− }
130−}
131−
132−void Scopes::onScopesReordered(const unity::dash::Scopes::ScopeList& scopes)
133−{
134− // FIXME Should use beginMoveRows()/endMoveRows() to not recreate the UI for every scope.
135− beginResetModel();
136−
137− // remove existing scopes
138− for (auto i=m_scopes.count()-1; i>=0; i--) {
139− removeUnityScope(i);
140− }
141−
142− // re-create scopes
143− for (uint i=0; i<scopes.size(); i++) {
144− addUnityScope(scopes[i]);
145− }
146− endResetModel();
147−}
148−
149−void Scopes::onScopePropertyChanged()
150−{
151− QModelIndex scopeIndex = index(m_scopes.indexOf(qobject_cast<Scope*>(sender())));
152− Q_EMIT dataChanged(scopeIndex, scopeIndex);
153−}
154−
155−void Scopes::addUnityScope(const unity::dash::Scope::Ptr& unity_scope)
156−{
157− Scope* scope = new Scope(this);
158− scope->setUnityScope(unity_scope);
159− /* DOCME */
160− QObject::connect(scope, SIGNAL(visibleChanged(bool)), this, SLOT(onScopePropertyChanged()));
161− m_scopes.append(scope);
162−}
163−
164−void Scopes::removeUnityScope(int index)
165−{
166− Scope* scope = m_scopes.takeAt(index);
167−
168− delete scope;
169−}
170−
171−int Scopes::findScopeById(const QString& scope_id)
172−{
173− for (int i=0; i<m_scopes.count(); i++) {
174− if (m_scopes[i]->id() == scope_id) {
175− return i;
176− }
177− }
178− return -1;
179−}