on e671b82 help sign in
unity8/tests/mocks/Unity/fake_categories.cpp
158 lines 5.1 kB history raw
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Author: Nick Dedekind <nick.dedekind@canonical.com>
17 */
19// self
20#include "fake_categories.h"
22#include "fake_resultsmodel.h"
24Categories::Categories(int category_count, QObject* parent)
25 : unity::shell::scopes::CategoriesInterface(parent)
26 , m_category_count(category_count)
30int Categories::rowCount(const QModelIndex& /*parent*/) const
32 return m_category_count + m_specialCategories.count();
35void Categories::addSpecialCategory(QString const& categoryId, QString const& name, QString const& icon, QString const& rawTemplate, QObject* countObject)
37 CategoryData catData;
38 catData.categoryId = categoryId;
39 catData.name = name;
40 catData.icon = icon;
41 catData.rawTemplate = rawTemplate;
42 catData.countObject = countObject;
44 beginInsertRows(QModelIndex(), 0, 0);
45 m_specialCategories.prepend(catData);
46 endInsertRows();
48 if (countObject) {
49 connect(countObject, SIGNAL(countChanged()), this, SLOT(countChanged()));
50 }
53bool Categories::overrideCategoryJson(QString const& categoryId, QString const& json)
55 qFatal("Using un-implemented Categories::overrideCategoryJson");
58void Categories::countChanged()
60 QObject* countObject = sender();
62 for (int i = 0; i < m_specialCategories.count(); ++i) {
63 const CategoryData &catData = m_specialCategories.at(i);
64 if (catData.countObject == countObject) {
65 QVector<int> roles;
66 roles.append(RoleCount);
68 QModelIndex changedIndex(index(i));
69 dataChanged(changedIndex, changedIndex, roles);
70 break;
71 }
72 }
75QVariant
76Categories::data(const QModelIndex& index, int role) const
78 if (!index.isValid()) {
79 return QVariant();
80 }
82 ResultsModel *resultsModel = m_resultsModels[index.row()];
83 if (!resultsModel) {
84 resultsModel = new ResultsModel(15, index.row());
85 m_resultsModels[index.row()] = resultsModel;
86 }
87 if (index.row() < m_specialCategories.count()) {
88 const CategoryData &catData = m_specialCategories.at(index.row());
89 switch (role) {
90 case RoleCategoryId:
91 return catData.categoryId;
92 case RoleName:
93 return catData.name;
94 case RoleIcon:
95 return catData.icon;
96 case RoleRawRendererTemplate:
97 return catData.rawTemplate;
98 case RoleRenderer:
99 {
100 QVariantMap map;
101 map["category-layout"] = index.row() % 2 == 0 ? "grid" : "carousel";
102 map["card-size"] = "small";
103 return map;
104 }
105 case RoleComponents:
106 {
107 QVariantMap map, artMap;
108 artMap["aspect-ratio"] = "1.0";
109 artMap["field"] = "art";
110 map["art"] = artMap;
111 map["title"] = "HOLA";
112 return map;
113 }
114 case RoleResults:
115 return QVariant();
116 case RoleCount:
117 return catData.countObject->property("count");
118 default:
119 qFatal("Using un-implemented Categories role");
120 return QVariant();
121 }
122 } else {
123 switch (role) {
124 case RoleCategoryId:
125 return QString("%1").arg(index.row());
126 case RoleName:
127 return QString("Category %1").arg(index.row());
128 case RoleIcon:
129 return "gtk-apply";
130 case RoleRawRendererTemplate:
131 qFatal("Using un-implemented RoleRawRendererTemplate Categories role");
132 return QVariant();
133 case RoleRenderer:
134 {
135 QVariantMap map;
136 map["category-layout"] = index.row() % 2 == 0 ? "grid" : "carousel";
137 map["card-size"] = "small";
138 return map;
139 }
140 case RoleComponents:
141 {
142 QVariantMap map, artMap;
143 artMap["aspect-ratio"] = "1.0";
144 artMap["field"] = "art";
145 map["art"] = artMap;
146 map["title"] = "HOLA";
147 return map;
148 }
149 case RoleResults:
150 return QVariant::fromValue(resultsModel);
151 case RoleCount:
152 return resultsModel->rowCount();
153 default:
154 qFatal("Using un-implemented Categories role");
155 return QVariant();
156 }
157 }