~paulliu/unity8/lp1378469_MessageMenu

« back to all changes in this revision

Viewing changes to plugins/Unity/categories.cpp

  • Committer: Michał Sawicz
  • Date: 2013-06-05 22:03:08 UTC
  • Revision ID: michal.sawicz@canonical.com-20130605220308-yny8fv3futtr04fg
Inital unity8 commit.

Previous history can be found at https://code.launchpad.net/~unity-team/unity/phablet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013 Canonical, Ltd.
 
3
 *
 
4
 * Authors:
 
5
 *  Michał Sawicz <michal.sawicz@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 "categories.h"
 
22
#include "categoryfilter.h"
 
23
 
 
24
Categories::Categories(QObject* parent)
 
25
    : DeeListModel(parent)
 
26
    , m_resultModel(0)
 
27
    , m_globalResultModel(0)
 
28
{
 
29
    // FIXME: need to clean up unused filters on countChanged
 
30
    m_roles[Categories::RoleId] = "id";
 
31
    m_roles[Categories::RoleName] = "name";
 
32
    m_roles[Categories::RoleIcon] = "icon";
 
33
    m_roles[Categories::RoleRenderer] = "renderer";
 
34
    m_roles[Categories::RoleHints] = "hints";
 
35
    m_roles[Categories::RoleResults] = "results";
 
36
    m_roles[Categories::RoleGlobalResults] = "globalResults";
 
37
    m_roles[Categories::RoleCount] = "count";
 
38
    m_roles[Categories::RoleGlobalCount] = "globalCount";
 
39
 
 
40
    // TODO This should not be needed but accumulatting the count changes
 
41
    // makes the visualization more stable and also makes crashes on fast
 
42
    // change of the search term harder to reproduce
 
43
    m_timer.setSingleShot(true);
 
44
    m_timer.setInterval(50);
 
45
    connect(&m_timer, SIGNAL(timeout()), this, SLOT(onEmitCountChanged()));
 
46
}
 
47
 
 
48
Categories::~Categories()
 
49
{
 
50
    qDeleteAll(m_filters);
 
51
    qDeleteAll(m_globalFilters);
 
52
}
 
53
 
 
54
CategoryFilter*
 
55
Categories::getFilter(int index) const
 
56
{
 
57
    if (!m_filters.contains(index)) {
 
58
        CategoryFilter* filter = new CategoryFilter();
 
59
        connect(filter, SIGNAL(countChanged()), this, SLOT(onCountChanged()));
 
60
        filter->setModel(m_resultModel);
 
61
        filter->setIndex(index);
 
62
 
 
63
        m_filters.insert(index, filter);
 
64
    }
 
65
 
 
66
    return m_filters[index];
 
67
}
 
68
 
 
69
CategoryFilter*
 
70
Categories::getGlobalFilter(int index) const
 
71
{
 
72
    if (!m_globalFilters.contains(index)) {
 
73
        CategoryFilter* filter = new CategoryFilter();
 
74
        connect(filter, SIGNAL(countChanged()), this, SLOT(onGlobalCountChanged()));
 
75
        filter->setModel(m_globalResultModel);
 
76
        filter->setIndex(index);
 
77
 
 
78
        m_globalFilters.insert(index, filter);
 
79
    }
 
80
 
 
81
    return m_globalFilters[index];
 
82
}
 
83
 
 
84
void
 
85
Categories::setResultModel(DeeListModel* model)
 
86
{
 
87
    if (model != m_resultModel) {
 
88
        m_resultModel = model;
 
89
 
 
90
        Q_FOREACH(CategoryFilter* filter, m_filters) {
 
91
            filter->setModel(m_resultModel);
 
92
        }
 
93
 
 
94
        Q_EMIT resultModelChanged(m_resultModel);
 
95
    }
 
96
}
 
97
 
 
98
void
 
99
Categories::setGlobalResultModel(DeeListModel* model)
 
100
{
 
101
    if (model != m_globalResultModel) {
 
102
        m_globalResultModel = model;
 
103
 
 
104
        Q_FOREACH(CategoryFilter* filter, m_globalFilters) {
 
105
            filter->setModel(m_globalResultModel);
 
106
        }
 
107
 
 
108
        Q_EMIT globalResultModelChanged(m_globalResultModel);
 
109
    }
 
110
}
 
111
 
 
112
void
 
113
Categories::onCountChanged()
 
114
{
 
115
    CategoryFilter* filter = qobject_cast<CategoryFilter*>(sender());
 
116
    if (filter) {
 
117
        m_timerFilters << filter;
 
118
        m_timer.start();
 
119
    }
 
120
}
 
121
 
 
122
void
 
123
Categories::onEmitCountChanged()
 
124
{
 
125
    QVector<int> roles;
 
126
    roles.append(Categories::RoleCount);
 
127
    Q_FOREACH(CategoryFilter* filter, m_timerFilters) {
 
128
        QModelIndex changedIndex = index(filter->index());
 
129
        Q_EMIT dataChanged(changedIndex, changedIndex, roles);
 
130
    }
 
131
    m_timerFilters.clear();
 
132
}
 
133
 
 
134
void
 
135
Categories::onGlobalCountChanged()
 
136
{
 
137
    CategoryFilter* filter = qobject_cast<CategoryFilter*>(sender());
 
138
    if (filter) {
 
139
        QModelIndex changedIndex = index(filter->index());
 
140
        QVector<int> roles;
 
141
        roles.append(Categories::RoleGlobalCount);
 
142
        Q_EMIT dataChanged(changedIndex, changedIndex, roles);
 
143
    }
 
144
}
 
145
 
 
146
QHash<int, QByteArray>
 
147
Categories::roleNames() const
 
148
{
 
149
    return m_roles;
 
150
}
 
151
 
 
152
QVariant
 
153
Categories::data(const QModelIndex& index, int role) const
 
154
{
 
155
    if (!index.isValid()) {
 
156
        return QVariant();
 
157
    }
 
158
 
 
159
    if (role == RoleId) {
 
160
        return QVariant::fromValue(index.row());
 
161
    } else if (role == RoleName) {
 
162
        return QVariant::fromValue(DeeListModel::data(index, 0));
 
163
    } else if (role == RoleIcon) {
 
164
        return QVariant::fromValue(DeeListModel::data(index, 1));
 
165
    } else if (role == RoleRenderer) {
 
166
        return QVariant::fromValue(DeeListModel::data(index, 2));
 
167
    } else if (role == RoleHints) {
 
168
        return QVariant::fromValue(DeeListModel::data(index, 3));
 
169
    } else if (role == RoleResults) {
 
170
        return QVariant::fromValue(getFilter(index.row()));
 
171
    } else if (role == RoleGlobalResults) {
 
172
        return QVariant::fromValue(getGlobalFilter(index.row()));
 
173
    } else if (role == RoleCount) {
 
174
        CategoryFilter* filter = getFilter(index.row());
 
175
        return QVariant::fromValue(filter->rowCount());
 
176
    } else if (role == RoleGlobalCount) {
 
177
        QSortFilterProxyModel* filter = getGlobalFilter(index.row());
 
178
        return QVariant::fromValue(filter->rowCount());
 
179
    } else {
 
180
        return QVariant();
 
181
    }
 
182
}