on 45dbde8 help sign in
unity8/plugins/Unity/categories.cpp
132 lines 3.9 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)
28 // FIXME: need to clean up unused filters on countChanged
29 m_roles[Categories::RoleId] = "id";
30 m_roles[Categories::RoleName] = "name";
31 m_roles[Categories::RoleIcon] = "icon";
32 m_roles[Categories::RoleRenderer] = "renderer";
33 m_roles[Categories::RoleHints] = "hints";
34 m_roles[Categories::RoleResults] = "results";
35 m_roles[Categories::RoleCount] = "count";
37 // TODO This should not be needed but accumulatting the count changes
38 // makes the visualization more stable and also makes crashes on fast
39 // change of the search term harder to reproduce
40 m_timer.setSingleShot(true);
41 m_timer.setInterval(50);
42 connect(&m_timer, SIGNAL(timeout()), this, SLOT(onEmitCountChanged()));
45Categories::~Categories()
47 qDeleteAll(m_filters);
50CategoryFilter*
51Categories::getFilter(int index) const
53 if (!m_filters.contains(index)) {
54 CategoryFilter* filter = new CategoryFilter();
55 connect(filter, SIGNAL(countChanged()), this, SLOT(onCountChanged()));
56 filter->setModel(m_resultModel);
57 filter->setIndex(index);
59 m_filters.insert(index, filter);
60 }
62 return m_filters[index];
65void
66Categories::setResultModel(DeeListModel* model)
68 if (model != m_resultModel) {
69 m_resultModel = model;
71 Q_FOREACH(CategoryFilter* filter, m_filters) {
72 filter->setModel(m_resultModel);
73 }
75 Q_EMIT resultModelChanged(m_resultModel);
76 }
79void
80Categories::onCountChanged()
82 CategoryFilter* filter = qobject_cast<CategoryFilter*>(sender());
83 if (filter) {
84 m_timerFilters << filter;
85 m_timer.start();
86 }
89void
90Categories::onEmitCountChanged()
92 QVector<int> roles;
93 roles.append(Categories::RoleCount);
94 Q_FOREACH(CategoryFilter* filter, m_timerFilters) {
95 QModelIndex changedIndex = index(filter->index());
96 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
97 }
98 m_timerFilters.clear();
101QHash<int, QByteArray>
102Categories::roleNames() const
104 return m_roles;
107QVariant
108Categories::data(const QModelIndex& index, int role) const
110 if (!index.isValid()) {
111 return QVariant();
112 }
114 if (role == RoleId) {
115 return QVariant::fromValue(index.row());
116 } else if (role == RoleName) {
117 return QVariant::fromValue(DeeListModel::data(index, 1)); //DISPLAY_NAME
118 } else if (role == RoleIcon) {
119 return QVariant::fromValue(DeeListModel::data(index, 2)); //ICON_HINT
120 } else if (role == RoleRenderer) {
121 return QVariant::fromValue(DeeListModel::data(index, 3)); //RENDERER_NAME
122 } else if (role == RoleHints) {
123 return QVariant::fromValue(DeeListModel::data(index, 4)); //HINTS
124 } else if (role == RoleResults) {
125 return QVariant::fromValue(getFilter(index.row()));
126 } else if (role == RoleCount) {
127 CategoryFilter* filter = getFilter(index.row());
128 return QVariant::fromValue(filter->rowCount());
129 } else {
130 return QVariant();
131 }