2
* Copyright (C) 2013 Canonical, Ltd.
5
* Michał Sawicz <michal.sawicz@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/>.
21
#include "categories.h"
22
#include "categoryfilter.h"
24
Categories::Categories(QObject* parent)
25
: DeeListModel(parent)
27
, m_globalResultModel(0)
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";
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()));
48
Categories::~Categories()
50
qDeleteAll(m_filters);
51
qDeleteAll(m_globalFilters);
55
Categories::getFilter(int index) const
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);
63
m_filters.insert(index, filter);
66
return m_filters[index];
70
Categories::getGlobalFilter(int index) const
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);
78
m_globalFilters.insert(index, filter);
81
return m_globalFilters[index];
85
Categories::setResultModel(DeeListModel* model)
87
if (model != m_resultModel) {
88
m_resultModel = model;
90
Q_FOREACH(CategoryFilter* filter, m_filters) {
91
filter->setModel(m_resultModel);
94
Q_EMIT resultModelChanged(m_resultModel);
99
Categories::setGlobalResultModel(DeeListModel* model)
101
if (model != m_globalResultModel) {
102
m_globalResultModel = model;
104
Q_FOREACH(CategoryFilter* filter, m_globalFilters) {
105
filter->setModel(m_globalResultModel);
108
Q_EMIT globalResultModelChanged(m_globalResultModel);
113
Categories::onCountChanged()
115
CategoryFilter* filter = qobject_cast<CategoryFilter*>(sender());
117
m_timerFilters << filter;
123
Categories::onEmitCountChanged()
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);
131
m_timerFilters.clear();
135
Categories::onGlobalCountChanged()
137
CategoryFilter* filter = qobject_cast<CategoryFilter*>(sender());
139
QModelIndex changedIndex = index(filter->index());
141
roles.append(Categories::RoleGlobalCount);
142
Q_EMIT dataChanged(changedIndex, changedIndex, roles);
146
QHash<int, QByteArray>
147
Categories::roleNames() const
153
Categories::data(const QModelIndex& index, int role) const
155
if (!index.isValid()) {
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());