@@ -21,15 +21,23 @@
21#include "categories.h"
21#include "categories.h"
22#include "categoryresults.h"
22#include "categoryresults.h"
24+// TODO: use something from libunity once it's public
24Categories::Categories(QObject* parent)
33Categories::Categories(QObject* parent)
25 : DeeListModel(parent)
34 : DeeListModel(parent)
27− // FIXME: need to clean up unused filters on countChanged
28 m_roles[Categories::RoleId] = "id";
36 m_roles[Categories::RoleId] = "id";
29 m_roles[Categories::RoleName] = "name";
37 m_roles[Categories::RoleName] = "name";
30 m_roles[Categories::RoleIcon] = "icon";
38 m_roles[Categories::RoleIcon] = "icon";
31 m_roles[Categories::RoleRenderer] = "renderer";
39 m_roles[Categories::RoleRenderer] = "renderer";
32− m_roles[Categories::RoleContentType] = "content_type";
40+ m_roles[Categories::RoleContentType] = "contentType";
33 m_roles[Categories::RoleHints] = "hints";
41 m_roles[Categories::RoleHints] = "hints";
34 m_roles[Categories::RoleResults] = "results";
42 m_roles[Categories::RoleResults] = "results";
35 m_roles[Categories::RoleCount] = "count";
43 m_roles[Categories::RoleCount] = "count";
@@ -39,23 +47,19 @@ Categories::Categories(QObject* parent)
39 // change of the search term harder to reproduce
47 // change of the search term harder to reproduce
40 m_timer.setSingleShot(true);
48 m_timer.setSingleShot(true);
41 m_timer.setInterval(50);
49 m_timer.setInterval(50);
42− connect(&m_timer, SIGNAL(timeout()), this, SLOT(onEmitCountChanged()));
50+ connect(&m_timer, &QTimer::timeout, this, &Categories::onEmitCountChanged);
45−Categories::~Categories()
49DeeListModel*
53DeeListModel*
50−Categories::getFilter(int index) const
54+Categories::getResults(int index) const
52− if (!m_results.contains(index) || m_results[index].isNull()) {
56+ if (!m_results.contains(index)) {
53− auto results = new CategoryResults;
57+ CategoryResults* results = new CategoryResults(const_cast<Categories*>(this));
54 results->setCategoryIndex(index);
58 results->setCategoryIndex(index);
55 connect(results, &DeeListModel::countChanged, this, &Categories::onCountChanged);
59 connect(results, &DeeListModel::countChanged, this, &Categories::onCountChanged);
57− unsigned cat_index = static_cast<unsigned>(index);
61+ unsigned categoryIndex = static_cast<unsigned>(index);
58− auto unity_results = m_unityScope->GetResultsForCategory(cat_index);
62+ auto unity_results = m_unityScope->GetResultsForCategory(categoryIndex);
59 results->setModel(unity_results->model());
63 results->setModel(unity_results->model());
61 m_results.insert(index, results);
65 m_results.insert(index, results);
@@ -68,6 +72,9 @@ void Categories::onCategoriesModelChanged(unity::glib::Object<DeeModel> model)
69 m_updatedCategories.clear();
73 m_updatedCategories.clear();
70 // FIXME: this might destroy the renderer view and re-create it, optimize?
74 // FIXME: this might destroy the renderer view and re-create it, optimize?
75+ Q_FOREACH(DeeListModel* model, m_results) {
71 m_results.clear();
78 m_results.clear();
72 setModel(model);
79 setModel(model);
@@ -80,7 +87,9 @@ Categories::setUnityScope(const unity::dash::Scope::Ptr& scope)
80 // no need to call this, we'll get notified
87 // no need to call this, we'll get notified
81 //setModel(m_unityScope->categories()->model());
88 //setModel(m_unityScope->categories()->model());
90+ m_categoriesChangedConnection.disconnect();
91+ m_categoriesChangedConnection =
83− m_unityScope->categories()->model.changed.connect(sigc::mem_fun(this, &Categories::onCategoriesModelChanged));
92+ m_unityScope->categories()->model.changed.connect(sigc::mem_fun(this, &Categories::onCategoriesModelChanged));
@@ -98,9 +107,9 @@ Categories::onEmitCountChanged()
99 QVector<int> roles;
108 QVector<int> roles;
100 roles.append(Categories::RoleCount);
109 roles.append(Categories::RoleCount);
101− Q_FOREACH(int cat_index, m_updatedCategories) {
110+ Q_FOREACH(int categoryIndex, m_updatedCategories) {
102− if (m_results[cat_index].isNull()) continue;
111+ if (!m_results.contains(categoryIndex)) continue;
103− QModelIndex changedIndex = index(cat_index);
112+ QModelIndex changedIndex = index(categoryIndex);
104 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
113 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
106 m_updatedCategories.clear();
115 m_updatedCategories.clear();
@@ -123,22 +132,22 @@ Categories::data(const QModelIndex& index, int role) const
123 case RoleId:
132 case RoleId:
124 return QVariant::fromValue(index.row());
133 return QVariant::fromValue(index.row());
125 case RoleName:
134 case RoleName:
126− return DeeListModel::data(index, 1); //DISPLAY_NAME
135+ return DeeListModel::data(index, CategoryColumn::DISPLAY_NAME);
127 case RoleIcon:
136 case RoleIcon:
128− return DeeListModel::data(index, 2); //ICON_HINT
137+ return DeeListModel::data(index, CategoryColumn::ICON_HINT);
129 case RoleRenderer:
138 case RoleRenderer:
130− return DeeListModel::data(index, 3); //RENDERER_NAME
139+ return DeeListModel::data(index, CategoryColumn::RENDERER_NAME);
131 case RoleContentType:
140 case RoleContentType:
133− auto hints = DeeListModel::data(index, 4).toHash();
142+ auto hints = DeeListModel::data(index, CategoryColumn::HINTS).toHash();
134 return hints.contains("content-type") ? hints["content-type"] : QVariant(QString("default"));
143 return hints.contains("content-type") ? hints["content-type"] : QVariant(QString("default"));
136 case RoleHints:
145 case RoleHints:
137− return DeeListModel::data(index, 4); //HINTS
146+ return DeeListModel::data(index, CategoryColumn::HINTS);
138 case RoleResults:
147 case RoleResults:
139− return QVariant::fromValue(getFilter(index.row()));
148+ return QVariant::fromValue(getResults(index.row()));
140 case RoleCount:
149 case RoleCount:
141− return QVariant::fromValue(getFilter(index.row())->rowCount());
150+ return QVariant::fromValue(getResults(index.row())->rowCount());
143 return QVariant();
152 return QVariant();