~ps-jenkins/unity-scopes-shell/ubuntu-rtm-14.09-proposed

« back to all changes in this revision

Viewing changes to src/scopes.cpp

  • Committer: Michal Hruby
  • Date: 2013-11-07 17:48:16 UTC
  • Revision ID: michal.mhr@gmail.com-20131107174816-w1vqq6juarrawx23
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
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();
 
44
}
 
45
 
 
46
QHash<int, QByteArray> Scopes::roleNames() const
 
47
{
 
48
    return m_roles;
 
49
}
 
50
 
 
51
int Scopes::rowCount(const QModelIndex& parent) const
 
52
{
 
53
    Q_UNUSED(parent)
 
54
 
 
55
    return m_scopes.count();
 
56
}
 
57
 
 
58
QVariant Scopes::data(const QModelIndex& index, int role) const
 
59
{
 
60
    Q_UNUSED(role)
 
61
 
 
62
    if (!index.isValid()) {
 
63
        return QVariant();
 
64
    }
 
65
 
 
66
    Scope* scope = m_scopes.at(index.row());
 
67
 
 
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());
 
74
    } else {
 
75
        return QVariant();
 
76
    }
 
77
}
 
78
 
 
79
QVariant Scopes::get(int row) const
 
80
{
 
81
    return data(QAbstractListModel::index(row), 0);
 
82
}
 
83
 
 
84
QVariant Scopes::get(const QString& scope_id) const
 
85
{
 
86
    Q_FOREACH(Scope* scope, m_scopes) {
 
87
        if (scope->id() == scope_id) {
 
88
            return QVariant::fromValue(scope);
 
89
        }
 
90
    }
 
91
 
 
92
    return QVariant();
 
93
}
 
94
 
 
95
bool Scopes::loaded() const
 
96
{
 
97
    return m_loaded;
 
98
}
 
99
 
 
100
void Scopes::onScopeAdded(const unity::dash::Scope::Ptr& scope, int /*position*/)
 
101
{
 
102
    int index = m_scopes.count();
 
103
    beginInsertRows(QModelIndex(), index, index);
 
104
    addUnityScope(scope);
 
105
    endInsertRows();
 
106
 
 
107
    // FIXME: do only once after all loaded?
 
108
    m_loaded = true;
 
109
    Q_EMIT loadedChanged(m_loaded);
 
110
}
 
111
 
 
112
void Scopes::onScopesLoaded()
 
113
{
 
114
}
 
115
 
 
116
void Scopes::onScopeRemoved(const unity::dash::Scope::Ptr& scope)
 
117
{
 
118
    auto id = QString::fromStdString(scope->id);
 
119
    auto index = findScopeById(id);
 
120
    if (index >= 0) {
 
121
        beginRemoveRows(QModelIndex(), index, index);
 
122
        removeUnityScope(index);
 
123
        endRemoveRows();
 
124
 
 
125
        Q_EMIT scopeRemoved(id, index);
 
126
    }
 
127
}
 
128
 
 
129
void Scopes::onScopesReordered(const unity::dash::Scopes::ScopeList& scopes)
 
130
{
 
131
    // FIXME Should use beginMoveRows()/endMoveRows() to not recreate the UI for every scope.
 
132
    beginResetModel();
 
133
 
 
134
    // remove existing scopes
 
135
    for (auto i=m_scopes.count()-1; i>=0; i--) {
 
136
        removeUnityScope(i);
 
137
    }
 
138
 
 
139
    // re-create scopes
 
140
    for (uint i=0; i<scopes.size(); i++) {
 
141
        addUnityScope(scopes[i]);
 
142
    }
 
143
    endResetModel();
 
144
}
 
145
 
 
146
void Scopes::onScopePropertyChanged()
 
147
{
 
148
    QModelIndex scopeIndex = index(m_scopes.indexOf(qobject_cast<Scope*>(sender())));
 
149
    Q_EMIT dataChanged(scopeIndex, scopeIndex);
 
150
}
 
151
 
 
152
void Scopes::addUnityScope(const unity::dash::Scope::Ptr& unity_scope)
 
153
{
 
154
    Scope* scope = new Scope(this);
 
155
    scope->setUnityScope(unity_scope);
 
156
    /* DOCME */
 
157
    QObject::connect(scope, SIGNAL(visibleChanged(bool)), this, SLOT(onScopePropertyChanged()));
 
158
    m_scopes.append(scope);
 
159
}
 
160
 
 
161
void Scopes::removeUnityScope(int index)
 
162
{
 
163
    Scope* scope = m_scopes.takeAt(index);
 
164
 
 
165
    delete scope;
 
166
}
 
167
 
 
168
int Scopes::findScopeById(const QString& scope_id)
 
169
{
 
170
    for (int i=0; i<m_scopes.count(); i++) {
 
171
        if (m_scopes[i]->id() == scope_id) {
 
172
            return i;
 
173
        }
 
174
    }
 
175
    return -1;
 
176
}