on 2bfccfb help sign in
unity8/plugins/Unity/categories.cpp
182 lines 5.4 kB history raw
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 */
20// self
21#include "categories.h"
22#include "categoryfilter.h"
24Categories::Categories(QObject* parent)
25 : DeeListModel(parent)
26 , m_resultModel(0)
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()));
48Categories::~Categories()
50 qDeleteAll(m_filters);
51 qDeleteAll(m_globalFilters);
54CategoryFilter*
55Categories::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);
64 }
66 return m_filters[index];
69CategoryFilter*
70Categories::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);
79 }
81 return m_globalFilters[index];
84void
85Categories::setResultModel(DeeListModel* model)
87 if (model != m_resultModel) {
88 m_resultModel = model;
90 Q_FOREACH(CategoryFilter* filter, m_filters) {
91 filter->setModel(m_resultModel);
92 }
94 Q_EMIT resultModelChanged(m_resultModel);
95 }
98void
99Categories::setGlobalResultModel(DeeListModel* model)
101 if (model != m_globalResultModel) {
102 m_globalResultModel = model;
104 Q_FOREACH(CategoryFilter* filter, m_globalFilters) {
105 filter->setModel(m_globalResultModel);
106 }
108 Q_EMIT globalResultModelChanged(m_globalResultModel);
109 }
112void
113Categories::onCountChanged()
115 CategoryFilter* filter = qobject_cast<CategoryFilter*>(sender());
116 if (filter) {
117 m_timerFilters << filter;
118 m_timer.start();
119 }
122void
123Categories::onEmitCountChanged()
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();
134void
135Categories::onGlobalCountChanged()
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 }
146QHash<int, QByteArray>
147Categories::roleNames() const
149 return m_roles;
152QVariant
153Categories::data(const QModelIndex& index, int role) const
155 if (!index.isValid()) {
156 return QVariant();
157 }
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 }