-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathjsonlistmodel.cpp
More file actions
275 lines (238 loc) · 6.97 KB
/
jsonlistmodel.cpp
File metadata and controls
275 lines (238 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <QDebug>
#include <QtCore/QReadWriteLock>
#include <QtQml/QJSValueIterator>
#include "jsonlistmodel.h"
static const int BASE_ROLE = Qt::UserRole + 1;
JsonListModel::JsonListModel(QObject *parent) :
QAbstractItemModel(parent),
m_lock(new QReadWriteLock(QReadWriteLock::Recursive)),
m_idAttribute("id")
{
}
void JsonListModel::extractRoles(const QJSValue &item,
const QString &prefix = QString())
{
QJSValueIterator it(item);
while (it.next()) {
QString n = prefix + it.name();
addRole(n);
QJSValue v = it.value();
if (!v.isArray() && v.isObject())
extractRoles(it.value(), n + ".");
}
}
int JsonListModel::addItem(const QJSValue &item)
{
int row = -1;
QString id;
if (item.isString() || item.isNumber() || item.isDate()) {
id = item.toString();
addRole("modelData");
} else if (item.isObject()) {
if (!item.hasProperty(m_idAttribute)) {
qWarning() << QString("Object does not have a %1 property").arg(m_idAttribute);
return row;
}
id = item.property(m_idAttribute).toString();
}
QJSValue existingItem = m_items[id];
if (existingItem.strictlyEquals(item)) {
return row;
}
m_items[id] = item;
if (existingItem.isUndefined()) {
m_keys.append(id);
row = m_keys.count() - 1;
return row;
}
return m_keys.indexOf(id);
}
void JsonListModel::add(const QJSValue &item)
{
m_lock->lockForWrite();
int originalSize = m_keys.count();
if (item.isArray()) {
QJSValueIterator array(item);
int updateFrom = INT_MAX;
int updateTo = INT_MIN;
// Only extract roles from the first item (assumes uniform data)
if (array.next()) {
if (array.hasNext()) { // ignore if last element
extractRoles(array.value());
int row = addItem(array.value());
if (row >= 0 && row < originalSize) {
updateFrom = qMin(updateFrom, row);
updateTo = qMax(updateTo, row);
}
}
}
while (array.next()) {
if (!array.hasNext())
break; // last value in array is an int with the length
int row = addItem(array.value());
if (row >= 0 && row < originalSize) {
updateFrom = qMin(updateFrom, row);
updateTo = qMax(updateTo, row);
}
}
int newSize = m_keys.count();
m_lock->unlock();
// emit signals after the mutex is unlocked
if (newSize > originalSize) {
beginInsertRows(QModelIndex(), originalSize, newSize - 1);
endInsertRows();
} else {
if (updateFrom != INT_MAX && updateTo != INT_MIN) {
emit dataChanged(createIndex(updateFrom, 0), createIndex(updateTo, 0));
}
}
} else {
extractRoles(item);
int row = addItem(item);
if (row >= 0 && row < originalSize) {
QModelIndex index = createIndex(row, 0);
m_lock->unlock();
emit dataChanged(index, index);
return;
}
int newSize = m_keys.count();
m_lock->unlock();
if (newSize > originalSize) {
beginInsertRows(QModelIndex(), originalSize, newSize - 1);
endInsertRows();
}
}
}
void JsonListModel::remove(const QJSValue &item)
{
int index;
{
QWriteLocker writeLocker(m_lock);
QString key;
if (item.isString() || item.isNumber() || item.isDate()) {
key = item.toString();
index = m_keys.indexOf(key);
} else if (item.hasProperty(m_idAttribute)){
key = item.property(m_idAttribute).toString();
index = m_keys.indexOf(key);
} else {
qWarning() << "Unable to remove item";
return;
}
m_keys.removeAt(index);
m_items.remove(key);
}
beginRemoveRows(QModelIndex(), index, index);
endRemoveRows();
}
void JsonListModel::clear()
{
m_lock->lockForWrite();
int originalSize = m_keys.length();
m_keys.clear();
m_items.clear();
m_lock->unlock();
beginRemoveRows(QModelIndex(), 0, originalSize);
endRemoveRows();
}
QJSValue JsonListModel::at(int row) const
{
QReadLocker locker(m_lock);
if (row >= 0 && row < m_keys.count()) {
return m_items[m_keys[row]];
}
return QJSValue();
}
QModelIndex JsonListModel::index(int row, int column, const QModelIndex &) const
{
QReadLocker readLock(m_lock);
if (row >= 0 && row < m_keys.count()) {
return createIndex(row, column);
} else {
qWarning() << "Out of bounds";
}
return QModelIndex();
}
QModelIndex JsonListModel::parent(const QModelIndex &) const
{
return QModelIndex();
}
int JsonListModel::rowCount(const QModelIndex &) const
{
QReadLocker locker(m_lock);
return m_keys.count();
}
int JsonListModel::columnCount(const QModelIndex &) const
{
return 1;
}
QVariant JsonListModel::data(const QModelIndex &index, int role) const
{
QReadLocker readLock(m_lock);
int row = index.row();
if (row < 0 || row > m_keys.count()) {
qWarning() << "Out of bounds";
return QVariant();
}
QJSValue item = m_items[m_keys[row]];
QString roleName = getRole(role);
if (item.isString() || item.isNumber() || item.isDate()) {
return item.toVariant();
} else {
QStringList parts = roleName.split(".");
roleName = parts.takeLast();
for (QStringList::const_iterator p = parts.constBegin(); p != parts.end(); p++) {
item = item.property(*p);
}
if (item.hasProperty(roleName))
return item.property(roleName).toVariant();
return QJSValue().toVariant();
}
}
QHash<int, QByteArray> JsonListModel::roleNames() const
{
QHash<int, QByteArray> roles;
for (int i = 0; i < m_roles.count(); ++i) {
roles.insert(BASE_ROLE + i, m_roles.at(i).toUtf8());
}
return roles;
}
QString JsonListModel::idAttribute() const
{
return m_idAttribute;
}
void JsonListModel::addRole(const QString &r)
{
foreach (const QString s, m_roles) {
if (s == r)
return;
}
m_roles.append(r);
emit roleAdded(r);
// TODO: emit dataChanged(); ?
}
QString JsonListModel::getRole(int role) const
{
role -= BASE_ROLE;
if (role < 0 || role > m_roles.count())
return "";
return m_roles[role];
}
int JsonListModel::getRole(const QString &role) const
{
int roleIndex = m_roles.indexOf(role);
if (roleIndex < 0)
return Qt::DisplayRole;
return BASE_ROLE + roleIndex;
}
void JsonListModel::setIdAttribute(QString idAttribute)
{
if (m_idAttribute == idAttribute)
return;
m_idAttribute = idAttribute;
emit idAttributeChanged(idAttribute);
}
bool JsonListModel::setData(const QModelIndex &, const QVariant &, int)
{
return false;
}