on b5b03cd help sign in
unity8/tests/mocks/Unity/fake_categories.cpp
174 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 : QAbstractListModel(parent)
26 , m_category_count(category_count)
28 m_roles[Categories::RoleCategoryId] = "categoryId";
29 m_roles[Categories::RoleName] = "name";
30 m_roles[Categories::RoleIcon] = "icon";
31 m_roles[Categories::RoleRawRendererTemplate] = "rawRendererTemplate";
32 m_roles[Categories::RoleRenderer] = "renderer";
33 m_roles[Categories::RoleComponents] = "components";
34 m_roles[Categories::RoleProgressSource] = "progressSource";
35 m_roles[Categories::RoleResults] = "results";
36 m_roles[Categories::RoleCount] = "count";
39QHash<int, QByteArray>
40Categories::roleNames() const
42 return m_roles;
45int Categories::rowCount(const QModelIndex& /*parent*/) const
47 return m_category_count + m_specialCategories.count();
50void Categories::addSpecialCategory(QString const& categoryId, QString const& name, QString const& icon, QString const& rawTemplate, QObject* countObject)
52 CategoryData catData;
53 catData.categoryId = categoryId;
54 catData.name = name;
55 catData.icon = icon;
56 catData.rawTemplate = rawTemplate;
57 catData.countObject = countObject;
59 beginInsertRows(QModelIndex(), 0, 0);
60 m_specialCategories.prepend(catData);
61 endInsertRows();
63 if (countObject) {
64 connect(countObject, SIGNAL(countChanged()), this, SLOT(countChanged()));
65 }
68void Categories::countChanged()
70 QObject* countObject = sender();
72 for (int i = 0; i < m_specialCategories.count(); ++i) {
73 const CategoryData &catData = m_specialCategories.at(i);
74 if (catData.countObject == countObject) {
75 QVector<int> roles;
76 roles.append(RoleCount);
78 QModelIndex changedIndex(index(i));
79 dataChanged(changedIndex, changedIndex, roles);
80 break;
81 }
82 }
85QVariant
86Categories::data(const QModelIndex& index, int role) const
88 if (!index.isValid()) {
89 return QVariant();
90 }
92 ResultsModel *resultsModel = m_resultsModels[index.row()];
93 if (!resultsModel) {
94 resultsModel = new ResultsModel(15, index.row());
95 m_resultsModels[index.row()] = resultsModel;
96 }
97 if (index.row() < m_specialCategories.count()) {
98 const CategoryData &catData = m_specialCategories.at(index.row());
99 switch (role) {
100 case RoleCategoryId:
101 return catData.categoryId;
102 case RoleName:
103 return catData.name;
104 case RoleIcon:
105 return catData.icon;
106 case RoleRawRendererTemplate:
107 return catData.rawTemplate;
108 case RoleRenderer:
109 {
110 QVariantMap map;
111 map["category-layout"] = index.row() % 2 == 0 ? "grid" : "carousel";
112 map["card-size"] = "small";
113 return map;
114 }
115 case RoleComponents:
116 {
117 QVariantMap map, artMap;
118 artMap["aspect-ratio"] = "1.0";
119 artMap["field"] = "art";
120 map["art"] = artMap;
121 map["title"] = "HOLA";
122 return map;
123 }
124 case RoleProgressSource:
125 qFatal("Using un-implemented RoleProgressSource Categories role");
126 return QVariant();
127 case RoleResults:
128 return QVariant();
129 case RoleCount:
130 return catData.countObject->property("count");
131 default:
132 qFatal("Using un-implemented Categories role");
133 return QVariant();
134 }
135 } else {
136 switch (role) {
137 case RoleCategoryId:
138 return QString("%1").arg(index.row());
139 case RoleName:
140 return QString("Category %1").arg(index.row());
141 case RoleIcon:
142 return "gtk-apply";
143 case RoleRawRendererTemplate:
144 qFatal("Using un-implemented RoleRawRendererTemplate Categories role");
145 return QVariant();
146 case RoleRenderer:
147 {
148 QVariantMap map;
149 map["category-layout"] = index.row() % 2 == 0 ? "grid" : "carousel";
150 map["card-size"] = "small";
151 return map;
152 }
153 case RoleComponents:
154 {
155 QVariantMap map, artMap;
156 artMap["aspect-ratio"] = "1.0";
157 artMap["field"] = "art";
158 map["art"] = artMap;
159 map["title"] = "HOLA";
160 return map;
161 }
162 case RoleProgressSource:
163 qFatal("Using un-implemented RoleProgressSource Categories role");
164 return QVariant();
165 case RoleResults:
166 return QVariant::fromValue(resultsModel);
167 case RoleCount:
168 return resultsModel->rowCount();
169 default:
170 qFatal("Using un-implemented Categories role");
171 return QVariant();
172 }
173 }