on 0b01bee help sign in
unity8/plugins/Unity/categories.cpp
189 lines 5.6 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"
23#include <QDebug>
25Categories::Categories(QObject* parent)
26 : DeeListModel(parent)
27 , m_resultModel(0)
28// , m_globalResultModel(0)
30 // FIXME: need to clean up unused filters on countChanged
31 m_roles[Categories::RoleId] = "id";
32 m_roles[Categories::RoleName] = "name";
33 m_roles[Categories::RoleIcon] = "icon";
34 m_roles[Categories::RoleRenderer] = "renderer";
35 m_roles[Categories::RoleHints] = "hints";
36 m_roles[Categories::RoleResults] = "results";
37 m_roles[Categories::RoleGlobalResults] = "globalResults";
38 m_roles[Categories::RoleCount] = "count";
39 m_roles[Categories::RoleGlobalCount] = "globalCount";
41 // TODO This should not be needed but accumulatting the count changes
42 // makes the visualization more stable and also makes crashes on fast
43 // change of the search term harder to reproduce
44 m_timer.setSingleShot(true);
45 m_timer.setInterval(50);
46 connect(&m_timer, SIGNAL(timeout()), this, SLOT(onEmitCountChanged()));
49Categories::~Categories()
51 qDeleteAll(m_filters);
52// qDeleteAll(m_globalFilters);
55CategoryFilter*
56Categories::getFilter(int index) const
58 if (!m_filters.contains(index)) {
59 CategoryFilter* filter = new CategoryFilter();
60 connect(filter, SIGNAL(countChanged()), this, SLOT(onCountChanged()));
61 filter->setModel(m_resultModel);
62 filter->setIndex(index);
64 m_filters.insert(index, filter);
65 }
67 return m_filters[index];
70/*CategoryFilter*
71Categories::getGlobalFilter(int index) const
73 if (!m_globalFilters.contains(index)) {
74 CategoryFilter* filter = new CategoryFilter();
75 connect(filter, SIGNAL(countChanged()), this, SLOT(onGlobalCountChanged()));
76 filter->setModel(m_globalResultModel);
77 filter->setIndex(index);
79 m_globalFilters.insert(index, filter);
80 }
82 return m_globalFilters[index];
83 }*/
85void
86Categories::setResultModel(DeeListModel* model)
88 if (model != m_resultModel) {
89 m_resultModel = model;
91 Q_FOREACH(CategoryFilter* filter, m_filters) {
92 filter->setModel(m_resultModel);
93 }
95 Q_EMIT resultModelChanged(m_resultModel);
96 }
99/*
100void
101Categories::setGlobalResultModel(DeeListModel* model)
103 if (model != m_globalResultModel) {
104 m_globalResultModel = model;
106 Q_FOREACH(CategoryFilter* filter, m_globalFilters) {
107 filter->setModel(m_globalResultModel);
108 }
110 Q_EMIT globalResultModelChanged(m_globalResultModel);
111 }
112 }*/
114void
115Categories::onCountChanged()
117 CategoryFilter* filter = qobject_cast<CategoryFilter*>(sender());
118 if (filter) {
119 m_timerFilters << filter;
120 m_timer.start();
121 }
124void
125Categories::onEmitCountChanged()
127 QVector<int> roles;
128 roles.append(Categories::RoleCount);
129 Q_FOREACH(CategoryFilter* filter, m_timerFilters) {
130 QModelIndex changedIndex = index(filter->index());
131 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
132 }
133 m_timerFilters.clear();
136/*
137void
138Categories::onGlobalCountChanged()
140 CategoryFilter* filter = qobject_cast<CategoryFilter*>(sender());
141 if (filter) {
142 QModelIndex changedIndex = index(filter->index());
143 QVector<int> roles;
144 roles.append(Categories::RoleGlobalCount);
145 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
146 }
147 }*/
149QHash<int, QByteArray>
150Categories::roleNames() const
152 return m_roles;
155QVariant
156Categories::data(const QModelIndex& index, int role) const
158 if (!index.isValid()) {
159 return QVariant();
160 }
162 if (role == RoleId) {
163 return QVariant::fromValue(index.row());
164 } else if (role == RoleName) {
165 return QVariant::fromValue(DeeListModel::data(index, 1)); //DISPLAY_NAME
166 } else if (role == RoleIcon) {
167 return QVariant::fromValue(DeeListModel::data(index, 2)); //ICON_HINT
168 } else if (role == RoleRenderer) {
169 return QVariant::fromValue(DeeListModel::data(index, 3)); //RENDERER_NAME
170 } else if (role == RoleHints) {
171 return QVariant::fromValue(DeeListModel::data(index, 4)); //HINTS
172 } else if (role == RoleResults) {
173 return QVariant::fromValue(getFilter(index.row()));
174 } else if (role == RoleGlobalResults) {
175 qWarning() << "GLOBAL RESULTS???";
176// return QVariant::fromValue(getGlobalFilter(index.row()));
177 return QVariant();
178 } else if (role == RoleCount) {
179 CategoryFilter* filter = getFilter(index.row());
180 return QVariant::fromValue(filter->rowCount());
181 } else if (role == RoleGlobalCount) {
182 qWarning() << "GLOBAL COUNT???";
183// QSortFilterProxyModel* filter = getGlobalFilter(index.row());
184 // return QVariant::fromValue(filter->rowCount());
185 return QVariant();
186 } else {
187 return QVariant();
188 }