-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathqsjsonlistmodel.cpp
192 lines (132 loc) · 3.97 KB
/
qsjsonlistmodel.cpp
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
#include <QtQml>
#include "qsdiffrunner.h"
#include "qsjsonlistmodel.h"
/*! \qmltype JsonListModel
\inqmlmodule QSyncable
JsonListModel is a syncable list model.
It could be used as a wrapper of other Javascript array object.
Whatever the source is updated, it will trigger synchronization automatically.
It will emit signals of insertion, removal and moving automatically.
*/
/*! \qmlproperty int JsonListModel::count
No. of items in this list model.
*/
/*! \qmlmethod QVariantMap JsonListModel::get(int i) const
Returns the item at index in the list model.
*/
/*! \qmlmethod void JsonListModel::setProperty(int index, QString property, QVariant value)
Apply the changes to a record at index. Only modified value will be set.
*/
/*! \qmlmethod void JsonListModel::set(int index, QVariantMap changes)
Changes the item at index in the list model with the values in changes. Properties not appearing in changes are left unchanged.
If index is equal to count() then a new item is appended to the list. Otherwise, index must be an element in the list.
*/
/*! \qmlmethod void JsonListModel::insert(int index,const QVariantMap& value);
Inserts an item at index position.
*/
/*! \qmlmethod void JsonListModel::remove(int i , int count = 1);
Deletes the content at index from the model. You may specific the no. of items to be removed by count argument.
*/
/*! \qmlmethod void JsonListModel::move(int from, int to, int n)
Moves n items from one position to another.
*/
/*! \qmlmethod void JsonListModel::append(const QVariantMap &value)
Append an items at the end of list
*/
/*! \qmlmethod int JsonListModel::indexOf(QString field, QVariant value)
Get the index of the given record which is equal to input value on given field.
If it is not found, it will return -1.
*/
QSJsonListModel::QSJsonListModel(QObject *parent) : QSListModel(parent)
{
componentCompleted = false;
}
/*! \qmlproperty string JsonListModel::keyField
Set the key field of data source.
The value in key field should be unique.
If it is not set, JsonListModel won't be able to identify insertion, removal
and moving changes.
*/
QString QSJsonListModel::keyField() const
{
return m_keyField;
}
void QSJsonListModel::setKeyField(const QString &keyField)
{
m_keyField = keyField;
emit keyFieldChanged();
}
/*! \qmlproperty array JsonListModel::source
JsonListModel is a wrapper of another Javascript array.
Update this property will trigger the synchronization and emit changes according to the difference.
Example:
\code
JsonListModel {
keyField: "id"
source: [
{ "id": "a", "value": 1},
{ "id": "b", "value": 2}
]
fields: [
"id",
"value"
]
}
\endcode
*/
QVariantList QSJsonListModel::source() const
{
return m_source;
}
void QSJsonListModel::setSource(const QVariantList &source)
{
m_source = source;
if (componentCompleted) {
sync();
}
emit sourceChanged();
}
/*! \qmlproperty array JsonListModel::fields
Define the available fields of this model.
If it is not assigned, JsonListModel will use the first appended record.
Then you are not able to change afterward.
Example:
\code
JsonListModel {
keyField: "id"
fields: [
"id",
"value"
]
}
\endcode
*/
QStringList QSJsonListModel::fields() const
{
return m_fields;
}
void QSJsonListModel::setFields(const QStringList &roleNames)
{
m_fields = roleNames;
setRoleNames(roleNames);
emit fieldsChanged();
}
void QSJsonListModel::classBegin()
{
}
void QSJsonListModel::componentComplete()
{
componentCompleted = true;
if (!m_source.isEmpty()) {
sync();
}
}
void QSJsonListModel::sync()
{
QSDiffRunner runner;
runner.setKeyField(m_keyField);
QList<QSPatch> patches = runner.compare(storage(), m_source);
if (patches.size() > 0) {
runner.patch(this, patches);
}
}