on 4e86d64 help sign in
unity8/tests/mocks/Unity/fake_categories.cpp
175 lines 5.8 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 RoleResults:
120 return QVariant();
121 case RoleCount:
122 return catData.countObject->property("count");
123 default:
124 qFatal("Using un-implemented Categories role");
125 return QVariant();
126 }
127 } else {
128 switch (role) {
129 case RoleCategoryId:
130 return QString("%1").arg(index.row());
131 case RoleName:
132 return QString("Category %1").arg(index.row());
133 case RoleIcon:
134 return "gtk-apply";
135 case RoleRawRendererTemplate:
136 qFatal("Using un-implemented RoleRawRendererTemplate Categories role");
137 return QVariant();
138 case RoleRenderer:
139 {
140 QVariantMap map;
141 if (index.row() % 2 == 0) {
142 map["category-layout"] = "grid";
143 } else {
144 map["category-layout"] = "carousel";
145 map["card-size"] = "medium";
146 map["collapsed-rows"] = 2;
147 map["overlay"] = true;
148 }
149 map["card-size"] = "small";
150 return map;
151 }
152 case RoleComponents:
153 {
154 QVariantMap map, artMap;
155 if (index.row() % 2 != 0) {
156 artMap["aspect-ratio"] = QString("1.%1").arg(index.row());
157 } else {
158 artMap["aspect-ratio"] = "1.0";
159 }
160 artMap["field"] = "art";
161 map["art"] = artMap;
162 map["title"] = "HOLA";
163 map["subtitle"] = "HOLA";
164 return map;
165 }
166 case RoleResults:
167 return QVariant::fromValue(resultsModel);
168 case RoleCount:
169 return resultsModel->rowCount();
170 default:
171 qFatal("Using un-implemented Categories role");
172 return QVariant();
173 }
174 }