on 0a01fee help sign in
unity8/plugins/Unity/categories.cpp
153 lines 4.5 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 "categoryresults.h"
24Categories::Categories(QObject* parent)
25 : DeeListModel(parent)
27 // FIXME: need to clean up unused filters on countChanged
28 m_roles[Categories::RoleId] = "id";
29 m_roles[Categories::RoleName] = "name";
30 m_roles[Categories::RoleIcon] = "icon";
31 m_roles[Categories::RoleRenderer] = "renderer";
32 m_roles[Categories::RoleContentType] = "content_type";
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()
49DeeListModel*
50Categories::getFilter(int index) const
52 if (!m_filters.contains(index)) {
53 auto results = std::make_shared<CategoryResults> ();
54 results->setCategoryIndex(index);
55 connect(results.get(), SIGNAL(countChanged()), this, SLOT(onCountChanged()));
57 unsigned cat_index = static_cast<unsigned>(index);
58 auto unity_results = m_unityScope->GetResultsForCategory(cat_index);
59 results->setModel(unity_results->model());
61 m_filters.insert(index, results);
62 }
64 return m_filters[index].get();
67void Categories::onCategoriesModelChanged(unity::glib::Object<DeeModel> model)
69 m_timerFilters.clear();
70 m_filters.clear();
72 setModel(model);
75void
76Categories::setUnityScope(const unity::dash::Scope::Ptr& scope)
78 m_unityScope = scope;
80 // no need to call this, we'll get notified
81 //setModel(m_unityScope->categories()->model());
83 m_unityScope->categories()->model.changed.connect(sigc::mem_fun(this, &Categories::onCategoriesModelChanged));
85 /*
86 if (model != m_resultModel) {
87 Q_FOREACH(CategoryFilter* filter, m_filters) {
88 filter->setModel(m_resultModel);
89 }
90 }
91 */
94void
95Categories::onCountChanged()
97 DeeListModel* filter = qobject_cast<DeeListModel*>(sender());
98 if (filter) {
99 m_timerFilters << filter;
100 m_timer.start();
101 }
104void
105Categories::onEmitCountChanged()
107 QVector<int> roles;
108 roles.append(Categories::RoleCount);
109 Q_FOREACH(DeeListModel* results, m_timerFilters) {
110 auto cat_results = qobject_cast<CategoryResults*>(results);
111 QModelIndex changedIndex = index(cat_results->categoryIndex());
112 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
113 }
114 m_timerFilters.clear();
117QHash<int, QByteArray>
118Categories::roleNames() const
120 return m_roles;
123QVariant
124Categories::data(const QModelIndex& index, int role) const
126 if (!index.isValid()) {
127 return QVariant();
128 }
130 switch (role) {
131 case RoleId:
132 return QVariant::fromValue(index.row());
133 case RoleName:
134 return DeeListModel::data(index, 1); //DISPLAY_NAME
135 case RoleIcon:
136 return DeeListModel::data(index, 2); //ICON_HINT
137 case RoleRenderer:
138 return DeeListModel::data(index, 3); //RENDERER_NAME
139 case RoleContentType:
140 {
141 auto hints = DeeListModel::data(index, 4).toHash();
142 return hints.contains("content-type") ? hints["content-type"] : QVariant(QString("default"));
143 }
144 case RoleHints:
145 return DeeListModel::data(index, 4); //HINTS
146 case RoleResults:
147 return QVariant::fromValue(getFilter(index.row()));
148 case RoleCount:
149 return QVariant::fromValue(getFilter(index.row())->rowCount());
150 default:
151 return QVariant();
152 }