on c0ec99d help sign in
unity8/tests/mocks/Unity/fake_resultsmodel.cpp
118 lines 3.4 kB history raw
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * Authors:
5 * Michal Hruby <michal.hruby@canonical.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 3.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
20// self
21#include "fake_resultsmodel.h"
23#include <paths.h>
25ResultsModel::ResultsModel(int result_count, int categoryId, QObject* parent)
26 : QAbstractListModel(parent)
27 , m_result_count(result_count)
28 , m_categoryId(categoryId)
30 m_roles[ResultsModel::RoleUri] = "uri";
31 m_roles[ResultsModel::RoleCategoryId] = "categoryId";
32 m_roles[ResultsModel::RoleDndUri] = "dndUri";
33 m_roles[ResultsModel::RoleResult] = "result";
34 m_roles[ResultsModel::RoleTitle] = "title";
35 m_roles[ResultsModel::RoleArt] = "art";
36 m_roles[ResultsModel::RoleSubtitle] = "subtitle";
37 m_roles[ResultsModel::RoleMascot] = "mascot";
38 m_roles[ResultsModel::RoleEmblem] = "emblem";
39 m_roles[ResultsModel::RoleOldPrice] = "oldPrice";
40 m_roles[ResultsModel::RolePrice] = "price";
41 m_roles[ResultsModel::RoleAltPrice] = "altPrice";
42 m_roles[ResultsModel::RoleRating] = "rating";
43 m_roles[ResultsModel::RoleAltRating] = "altRating";
44 m_roles[ResultsModel::RoleSummary] = "summary";
47QString ResultsModel::categoryId() const
49 return QString::number(m_categoryId);
52void ResultsModel::setCategoryId(QString const& /*id*/)
54 qFatal("Calling un-implemented ResultsModel::setCategoryId");
57QHash<int, QByteArray>
58ResultsModel::roleNames() const
60 return m_roles;
63int ResultsModel::rowCount(const QModelIndex& parent) const
65 Q_UNUSED(parent);
67 return m_result_count;
70int ResultsModel::count() const
72 return rowCount();
75QVariant ResultsModel::get(int row) const
77 if (row >= m_result_count || row < 0) return QVariantMap();
79 QVariantMap result;
80 QModelIndex modelIndex(index(row));
81 QHashIterator<int, QByteArray> it(roleNames());
82 while (it.hasNext()) {
83 it.next();
84 QVariant val(data(modelIndex, it.key()));
85 if (val.isNull()) continue;
86 result[it.value()] = val;
87 }
89 return result;
92QVariant
93ResultsModel::data(const QModelIndex& index, int role) const
95 switch (role) {
96 case RoleUri:
97 case RoleCategoryId:
98 case RoleDndUri:
99 case RoleResult:
100 return QString();
101 case RoleTitle:
102 return QString("Title.%1.%2").arg(m_categoryId).arg(index.row());
103 case RoleArt:
104 return qmlDirectory() + "graphics/applicationIcons/dash.png";
105 case RoleSubtitle:
106 return QString("Subtitle.%1.%2").arg(m_categoryId).arg(index.row());
107 case RoleMascot:
108 case RoleEmblem:
109 case RoleOldPrice:
110 case RolePrice:
111 case RoleAltPrice:
112 case RoleRating:
113 case RoleAltRating:
114 case RoleSummary:
115 default:
116 return QVariant();
117 }