Debian Codegraph @

tests/mocks/Lomiri/Indicators/fakeindicatorsmodel.cpp

raw
1 /*
2 * Copyright 2014 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authors:
17 * Nick Dedekind <[email protected]>
18 */
19
20 #include "fakeindicatorsmodel.h"
21 #include "indicators.h"
22
23 FakeIndicatorsModel::FakeIndicatorsModel(QObject *parent)
24 : QAbstractListModel(parent),
25 m_profile("phone")
26 {
27 QObject::connect(this, &FakeIndicatorsModel::rowsInserted, this, &FakeIndicatorsModel::countChanged);
28 QObject::connect(this, &FakeIndicatorsModel::rowsRemoved, this, &FakeIndicatorsModel::countChanged);
29 QObject::connect(this, &FakeIndicatorsModel::modelReset, this, &FakeIndicatorsModel::countChanged);
30 }
31
32 /*! \internal */
33 FakeIndicatorsModel::~FakeIndicatorsModel()
34 {
35 }
36
37 int FakeIndicatorsModel::count() const
38 {
39 return rowCount();
40 }
41
42 QString FakeIndicatorsModel::profile() const
43 {
44 return m_profile;
45 }
46
47 void FakeIndicatorsModel::setProfile(const QString& profile)
48 {
49 m_profile = profile;
50 Q_EMIT profileChanged();
51 }
52
53 void FakeIndicatorsModel::load(const QString&)
54 {
55 }
56
57 void FakeIndicatorsModel::unload()
58 {
59 beginResetModel();
60
61 m_modelData.clear();
62 Q_EMIT modelDataChanged();
63
64 endResetModel();
65 }
66
67
68 void FakeIndicatorsModel::append(const QVariantMap& data)
69 {
70 QList<QVariant> allData = m_modelData.toList();
71 beginInsertRows(QModelIndex(), allData.count(), allData.count());
72
73 allData.append(data);
74 m_modelData = allData;
75 Q_EMIT modelDataChanged();
76
77 endInsertRows();
78 }
79
80 void FakeIndicatorsModel::insert(int row, const QVariantMap& data)
81 {
82 QList<QVariant> allData = m_modelData.toList();
83 row = qMax(0, qMin(row, allData.count()));
84
85 beginInsertRows(QModelIndex(), row, row);
86
87 allData.insert(row, data);
88 m_modelData = allData;
89 Q_EMIT modelDataChanged();
90
91 endInsertRows();
92 }
93
94 void FakeIndicatorsModel::remove(int row)
95 {
96 QList<QVariant> allData = m_modelData.toList();
97 row = qMax(0, qMin(row, allData.count()));
98
99 beginRemoveRows(QModelIndex(), row, row);
100
101 allData.removeAt(row);
102 m_modelData = allData;
103 Q_EMIT modelDataChanged();
104
105 endRemoveRows();
106 }
107
108 void FakeIndicatorsModel::setModelData(const QVariant& modelData)
109 {
110 beginResetModel();
111
112 m_modelData = modelData;
113 Q_EMIT modelDataChanged();
114
115 endResetModel();
116 }
117
118 QHash<int, QByteArray> FakeIndicatorsModel::roleNames() const
119 {
120 static QHash<int, QByteArray> roles;
121 if (roles.isEmpty())
122 {
123 roles[IndicatorsModelRole::Identifier] = "identifier";
124 roles[IndicatorsModelRole::Position] = "position";
125 roles[IndicatorsModelRole::IndicatorProperties] = "indicatorProperties";
126 }
127 return roles;
128 }
129
130 int FakeIndicatorsModel::columnCount(const QModelIndex &) const
131 {
132 return 1;
133 }
134
135 QVariant FakeIndicatorsModel::data(int row, int role) const
136 {
137 return data(index(row, 0), role);
138 }
139
140 QVariant FakeIndicatorsModel::data(const QModelIndex &index, int role) const
141 {
142 QList<QVariant> dataList = m_modelData.toList();
143 if (!index.isValid() || index.row() >= dataList.size())
144 return QVariant();
145
146 return dataList[index.row()].toMap()[roleNames()[role]];
147 }
148
149 QModelIndex FakeIndicatorsModel::parent(const QModelIndex&) const
150 {
151 return QModelIndex();
152 }
153
154 int FakeIndicatorsModel::rowCount(const QModelIndex&) const
155 {
156 return m_modelData.toList().count();
157 }