on ff2ea35 help sign in
unity8/tests/mocks/Unity/fake_categories.cpp
191 lines 6.2 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 if (index.row() % 2 == 0) {
102 map["category-layout"] = "grid";
103 } else {
104 map["category-layout"] = "carousel";
105 map["overlay"] = true;
106 }
107 map["card-size"] = "small";
108 return map;
109 }
110 case RoleComponents:
111 {
112 QVariantMap map, artMap;
113 artMap["aspect-ratio"] = "1.0";
114 artMap["field"] = "art";
115 map["art"] = artMap;
116 map["title"] = "HOLA";
117 return map;
118 }
119 case RoleHeaderLink:
120 return QString();
121 case RoleResults:
122 return QVariant();
123 case RoleCount:
124 return catData.countObject->property("count");
125 default:
126 qFatal("Using un-implemented Categories role");
127 return QVariant();
128 }
129 } else {
130 switch (role) {
131 case RoleCategoryId:
132 return QString("%1").arg(index.row());
133 case RoleName:
134 return QString("Category %1").arg(index.row());
135 case RoleIcon:
136 return "gtk-apply";
137 case RoleRawRendererTemplate:
138 qFatal("Using un-implemented RoleRawRendererTemplate Categories role");
139 return QVariant();
140 case RoleRenderer:
141 {
142 QVariantMap map;
143 if (index.row() % 2 == 0) {
144 map["category-layout"] = "grid";
145 } else {
146 map["category-layout"] = "carousel";
147 map["card-size"] = "medium";
148 map["overlay"] = true;
149 }
150 if (index.row() == 19) {
151 map["category-layout"] = "grid";
152 map["collapsed-rows"] = 0;
153 }
154 map["card-size"] = "small";
155 return map;
156 }
157 case RoleComponents:
158 {
159 QVariantMap map, artMap;
160 if (index.row() % 2 != 0) {
161 artMap["aspect-ratio"] = QString("1.%1").arg(index.row());
162 } else {
163 artMap["aspect-ratio"] = "1.0";
164 }
165 artMap["field"] = "art";
166 map["art"] = artMap;
167 map["title"] = "HOLA";
168 map["subtitle"] = "HOLA";
169 return map;
170 }
171 case RoleHeaderLink:
172 if (index.row() == 1 || index.row() == 4) {
173 return QString("scope://query/1");
174 }
175 return QString();
176 case RoleResults:
177 return QVariant::fromValue(resultsModel);
178 case RoleCount:
179 return resultsModel->rowCount();
180 default:
181 qFatal("Using un-implemented Categories role");
182 return QVariant();
183 }
184 }
187QVariant
188Categories::data(int row, int role) const
190 return data(index(row, 0), role);