on 785cf4a help sign in
unity8/tests/mocks/Unity/fake_categories.cpp
152 lines 4.5 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 return QString("%1").arg(index.row());
51 case RoleName:
52 return QString("Category %1").arg(index.row());
53 case RoleIcon:
54 return "gtk-apply";
55 case RoleRawRendererTemplate:
56 qFatal("Using un-implemented RoleRawRendererTemplate Categories role");
57 return QVariant();
58 case RoleRenderer:
59 {
60 QVariantMap map;
61 if (index.row() % 2 == 0) {
62 map["category-layout"] = "grid";
63 } else {
64 map["category-layout"] = "carousel";
65 }
66 if (index.row() == 18) {
67 map["category-layout"] = "horizontal-list";
68 }
69 if (index.row() == 19) {
70 map["category-layout"] = "grid";
71 map["collapsed-rows"] = 0;
72 }
73 map["card-size"] = "small";
75 map["category-layout"] = m_layouts.value(index.row(), map["category-layout"].toString());
77 if (map["category-layout"] == "carousel") {
78 map["overlay"] = true;
79 }
81 return map;
82 }
83 case RoleComponents:
84 {
85 QVariantMap map, artMap, attributeMap;
86 if (index.row() % 2 != 0) {
87 artMap["aspect-ratio"] = QString("1.%1").arg(index.row());
88 } else {
89 artMap["aspect-ratio"] = "1.0";
90 }
91 artMap["field"] = "art";
92 map["art"] = artMap;
93 map["title"] = "HOLA";
94 map["subtitle"] = "HOLA";
95 attributeMap["field"] = "attribute";
96 map["attributes"] = attributeMap;
97 return map;
98 }
99 case RoleHeaderLink:
100 {
101 QString res;
102 if (index.row() == 1 || index.row() == 4) {
103 res = QString("scope://query/1");
104 }
105 res = m_headerLinks.value(index.row(), res);
106 return res;
107 }
108 case RoleResults:
109 return QVariant::fromValue(resultsModel);
110 case RoleCount:
111 return resultsModel->rowCount();
112 default:
113 qFatal("Using un-implemented Categories role");
114 return QVariant();
115 }
118QVariant
119Categories::data(int row, int role) const
121 return data(index(row, 0), role);
124void Categories::setCount(int count)
126 if (m_category_count != count) {
127 beginResetModel(); // This is just for test setup so we can be lazy and reset
128 m_category_count = count;
129 endResetModel();
130 }
133ResultsModel* Categories::resultModel(int row) const
135 ResultsModel *result = m_resultsModels[row];
136 if (!result) {
137 Categories *that = const_cast<Categories*>(this);
138 result = new ResultsModel(15, row, that);
139 m_resultsModels[row] = result;
140 }
141 return result;
144void Categories::setLayout(int row, const QString &layout)
146 m_layouts[row] = layout;
149void Categories::setHeaderLink(int row, const QString &headerLink)
151 m_headerLinks[row] = headerLink;