2
* Copyright (C) 2011 Canonical, Ltd.
5
* Florian Boucault <florian.boucault@canonical.com>
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.
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.
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/>.
28
#include <QtCore/QStringList>
29
#include <QtGui/QKeySequence>
31
Scopes::Scopes(QObject *parent)
32
: QAbstractListModel(parent)
33
, m_unityScopes(std::make_shared<unity::dash::GSettingsScopes>())
36
m_roles[Scopes::RoleScope] = "scope";
37
m_roles[Scopes::RoleId] = "id";
38
m_roles[Scopes::RoleVisible] = "visible";
40
m_unityScopes->scope_added.connect(sigc::mem_fun(this, &Scopes::onScopeAdded));
41
m_unityScopes->scope_removed.connect(sigc::mem_fun(this, &Scopes::onScopeRemoved));
42
m_unityScopes->scopes_reordered.connect(sigc::mem_fun(this, &Scopes::onScopesReordered));
43
m_unityScopes->LoadScopes();
46
QHash<int, QByteArray> Scopes::roleNames() const
51
int Scopes::rowCount(const QModelIndex& parent) const
55
return m_scopes.count();
58
QVariant Scopes::data(const QModelIndex& index, int role) const
62
if (!index.isValid()) {
66
Scope* scope = m_scopes.at(index.row());
68
if (role == Scopes::RoleScope) {
69
return QVariant::fromValue(scope);
70
} else if (role == Scopes::RoleId) {
71
return QVariant::fromValue(scope->id());
72
} else if (role == Scopes::RoleVisible) {
73
return QVariant::fromValue(scope->visible());
79
QVariant Scopes::get(int row) const
81
return data(QAbstractListModel::index(row), 0);
84
QVariant Scopes::get(const QString& scope_id) const
86
Q_FOREACH(Scope* scope, m_scopes) {
87
if (scope->id() == scope_id) {
88
return QVariant::fromValue(scope);
95
bool Scopes::loaded() const
100
void Scopes::onScopeAdded(const unity::dash::Scope::Ptr& scope, int /*position*/)
102
int index = m_scopes.count();
103
beginInsertRows(QModelIndex(), index, index);
104
addUnityScope(scope);
107
// FIXME: do only once after all loaded?
109
Q_EMIT loadedChanged(m_loaded);
112
void Scopes::onScopesLoaded()
116
void Scopes::onScopeRemoved(const unity::dash::Scope::Ptr& scope)
118
auto id = QString::fromStdString(scope->id);
119
auto index = findScopeById(id);
121
beginRemoveRows(QModelIndex(), index, index);
122
removeUnityScope(index);
125
Q_EMIT scopeRemoved(id, index);
129
void Scopes::onScopesReordered(const unity::dash::Scopes::ScopeList& scopes)
131
// FIXME Should use beginMoveRows()/endMoveRows() to not recreate the UI for every scope.
134
// remove existing scopes
135
for (auto i=m_scopes.count()-1; i>=0; i--) {
140
for (uint i=0; i<scopes.size(); i++) {
141
addUnityScope(scopes[i]);
146
void Scopes::onScopePropertyChanged()
148
QModelIndex scopeIndex = index(m_scopes.indexOf(qobject_cast<Scope*>(sender())));
149
Q_EMIT dataChanged(scopeIndex, scopeIndex);
152
void Scopes::addUnityScope(const unity::dash::Scope::Ptr& unity_scope)
154
Scope* scope = new Scope(this);
155
scope->setUnityScope(unity_scope);
157
QObject::connect(scope, SIGNAL(visibleChanged(bool)), this, SLOT(onScopePropertyChanged()));
158
m_scopes.append(scope);
161
void Scopes::removeUnityScope(int index)
163
Scope* scope = m_scopes.takeAt(index);
168
int Scopes::findScopeById(const QString& scope_id)
170
for (int i=0; i<m_scopes.count(); i++) {
171
if (m_scopes[i]->id() == scope_id) {