on 16fa310 help sign in
unity8/tests/mocks/Unity/fake_categories.cpp
165 lines 5.0 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;
35bool Categories::overrideCategoryJson(QString const& /* categoryId */, QString const& /* json */)
37 qFatal("Using un-implemented Categories::overrideCategoryJson");
40QVariant
41Categories::data(const QModelIndex& index, int role) const
43 if (!index.isValid()) {
44 return QVariant();
45 }
47 ResultsModel *resultsModel = resultModel(index.row());
48 switch (role) {
49 case RoleCategoryId:
50 if (index.row() == 6) {
51 return QString("predefined");
52 } else if (index.row() == 8) {
53 return QString("local");
54 } else {
55 return QString("%1").arg(index.row());
56 }
57 case RoleName:
58 return QString("Category %1").arg(index.row());
59 case RoleIcon:
60 return "gtk-apply";
61 case RoleRawRendererTemplate:
62 qFatal("Using un-implemented RoleRawRendererTemplate Categories role");
63 return QVariant();
64 case RoleRenderer:
65 {
66 QVariantMap map;
67 if (index.row() % 2 == 0) {
68 map["category-layout"] = "grid";
69 } else {
70 map["category-layout"] = "carousel";
71 }
72 if (index.row() == 16) {
73 map["expandable"] = false;
74 map["collapsed-rows"] = 1;
75 }
76 if (index.row() == 18) {
77 map["category-layout"] = "horizontal-list";
78 }
79 if (index.row() == 19) {
80 map["category-layout"] = "grid";
81 map["collapsed-rows"] = 0;
82 }
83 map["card-size"] = "small";
85 map["category-layout"] = m_layouts.value(index.row(), map["category-layout"].toString());
87 if (map["category-layout"] == "carousel") {
88 map["overlay"] = true;
89 }
91 return map;
92 }
93 case RoleComponents:
94 {
95 QVariantMap map, artMap, attributeMap, socialActionMap;
96 if (index.row() % 2 != 0) {
97 artMap["aspect-ratio"] = QString("1.%1").arg(index.row());
98 } else {
99 artMap["aspect-ratio"] = "1.0";
100 }
101 artMap["field"] = "art";
102 map["art"] = artMap;
103 map["title"] = "HOLA";
104 map["subtitle"] = "HOLA";
105 attributeMap["field"] = "attribute";
106 socialActionMap["icon"] = "icon";
107 socialActionMap["id"] = "socialHola";
108 map["socialActions"] = socialActionMap;
109 map["attributes"] = attributeMap;
110 return map;
111 }
112 case RoleHeaderLink:
113 {
114 QString res;
115 if (index.row() == 1 || index.row() == 4) {
116 res = QString("scope://query/1");
117 }
118 res = m_headerLinks.value(index.row(), res);
119 return res;
120 }
121 case RoleResults:
122 return QVariant::fromValue(resultsModel);
123 case RoleCount:
124 return resultsModel->rowCount();
125 default:
126 qFatal("Using un-implemented Categories role");
127 return QVariant();
128 }
131QVariant
132Categories::data(int row, int role) const
134 return data(index(row, 0), role);
137void Categories::setCount(int count)
139 if (m_category_count != count) {
140 beginResetModel(); // This is just for test setup so we can be lazy and reset
141 m_category_count = count;
142 endResetModel();
143 }
146ResultsModel* Categories::resultModel(int row) const
148 ResultsModel *result = m_resultsModels[row];
149 if (!result) {
150 Categories *that = const_cast<Categories*>(this);
151 result = new ResultsModel(25, row, that);
152 m_resultsModels[row] = result;
153 }
154 return result;
157void Categories::setLayout(int row, const QString &layout)
159 m_layouts[row] = layout;
162void Categories::setHeaderLink(int row, const QString &headerLink)
164 m_headerLinks[row] = headerLink;