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

« back to all changes in this revision

Viewing changes to src/filters.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, 2013 Canonical, Ltd.
 
3
 *
 
4
 * Authors:
 
5
 *  Florian Boucault <florian.boucault@canonical.com>
 
6
 *  Pawel Stolowski <pawel.stolowski@canonical.com>
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or modify
 
9
 * it under the terms of the GNU General Public License as published by
 
10
 * the Free Software Foundation; version 3.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 */
 
20
 
 
21
// Self
 
22
#include "filters.h"
 
23
 
 
24
// Local
 
25
#include "filter.h"
 
26
#include "genericoptionsmodel.h"
 
27
 
 
28
// Qt
 
29
#include <QDebug>
 
30
 
 
31
Filters::Filters(unity::dash::Filters::Ptr unityFilters, QObject *parent) :
 
32
    QAbstractListModel(parent), m_unityFilters(unityFilters)
 
33
{
 
34
    for (unsigned int i=0; i<m_unityFilters->count(); i++) {
 
35
        unity::dash::Filter::Ptr unityFilter = m_unityFilters->FilterAtIndex(i);
 
36
        addFilter(unityFilter, i);
 
37
    }
 
38
    m_unityFilters->filter_added.connect(sigc::mem_fun(this, &Filters::onFilterAdded));
 
39
    m_unityFilters->filter_changed.connect(sigc::mem_fun(this, &Filters::onFilterChanged));
 
40
    m_unityFilters->filter_removed.connect(sigc::mem_fun(this, &Filters::onFilterRemoved));
 
41
}
 
42
 
 
43
Filters::~Filters()
 
44
{
 
45
    while (!m_filters.isEmpty()) {
 
46
        delete m_filters.takeFirst();
 
47
    }
 
48
}
 
49
 
 
50
int Filters::rowCount(const QModelIndex& parent) const
 
51
{
 
52
    Q_UNUSED(parent)
 
53
 
 
54
    return m_unityFilters->count();
 
55
}
 
56
 
 
57
QVariant Filters::data(const QModelIndex& index, int role) const
 
58
{
 
59
    if (!index.isValid()) {
 
60
        return QVariant();
 
61
    }
 
62
 
 
63
    Filter* filter = m_filters.at(index.row());
 
64
    switch (role)
 
65
    {
 
66
        case Filters::RoleId:
 
67
            return filter->id();
 
68
        case Filters::RoleName:
 
69
        case Qt::DisplayRole:
 
70
            return filter->name();
 
71
        case Filters::RoleIconHint:
 
72
            return filter->iconHint();
 
73
        case Filters::RoleRendererName:
 
74
            return filter->rendererName();
 
75
        case Filters::RoleVisible:
 
76
            return filter->visible();
 
77
        case Filters::RoleCollapsed:
 
78
            return filter->collapsed();
 
79
        case Filters::RoleFiltering:
 
80
            return filter->filtering();
 
81
        case Filters::RoleOptions:
 
82
            return QVariant::fromValue(filter->options());
 
83
        default:
 
84
            break;
 
85
    }
 
86
    return QVariant();
 
87
}
 
88
 
 
89
QHash<int, QByteArray> Filters::roleNames() const
 
90
{
 
91
    QHash<int, QByteArray> roles;
 
92
    roles[Filters::RoleId] = "id";
 
93
    roles[Filters::RoleName] = "name";
 
94
    roles[Filters::RoleIconHint] = "iconHint";
 
95
    roles[Filters::RoleRendererName] = "rendererName";
 
96
    roles[Filters::RoleVisible] = "visible";
 
97
    roles[Filters::RoleCollapsed] = "collapsed";
 
98
    roles[Filters::RoleFiltering] = "filtering";
 
99
    roles[Filters::RoleOptions] = "options";
 
100
    return roles;
 
101
}
 
102
 
 
103
Filter* Filters::getFilter(const QString& id) const
 
104
{
 
105
    Q_FOREACH (Filter* filter, m_filters) {
 
106
        if (filter->id() == id) {
 
107
            return filter;
 
108
        }
 
109
    }
 
110
    return nullptr;
 
111
}
 
112
 
 
113
void Filters::onFilterAdded(unity::dash::Filter::Ptr unityFilter)
 
114
{
 
115
    if (unityFilter == nullptr) {
 
116
        return;
 
117
    }
 
118
 
 
119
    int index = m_filters.count();
 
120
    addFilter(unityFilter, index);
 
121
}
 
122
 
 
123
void Filters::onFilterChanged(unity::dash::Filter::Ptr unityFilter)
 
124
{
 
125
    if (unityFilter == nullptr) {
 
126
        return;
 
127
    }
 
128
 
 
129
    QModelIndex filterIndex = index(indexForFilter(unityFilter));
 
130
    Q_EMIT dataChanged(filterIndex, filterIndex);
 
131
}
 
132
 
 
133
void Filters::onFilterRemoved(unity::dash::Filter::Ptr unityFilter)
 
134
{
 
135
    removeFilter(indexForFilter(unityFilter));
 
136
}
 
137
 
 
138
void Filters::addFilter(unity::dash::Filter::Ptr unityFilter, int index)
 
139
{
 
140
    beginInsertRows(QModelIndex(), index, index);
 
141
    Filter* filter = Filter::newFromUnityFilter(unityFilter);
 
142
    if (filter != nullptr) {
 
143
        m_filters.insert(index, filter);
 
144
    }
 
145
    endInsertRows();
 
146
}
 
147
 
 
148
void Filters::removeFilter(int index)
 
149
{
 
150
    beginRemoveRows(QModelIndex(), index, index);
 
151
    Filter* filter = m_filters.takeAt(index);
 
152
    delete filter;
 
153
    endRemoveRows();
 
154
}
 
155
 
 
156
int Filters::indexForFilter(unity::dash::Filter::Ptr unityFilter)
 
157
{
 
158
    for (int index=0; index<m_filters.count(); index++) {
 
159
        if (m_filters[index]->hasUnityFilter(unityFilter)) {
 
160
            return index;
 
161
        }
 
162
    }
 
163
    qWarning() << "Filter" << QString::fromStdString(unityFilter->name()) << "not found in local cache.";
 
164
    return -1;
 
165
}