-
Notifications
You must be signed in to change notification settings - Fork 0
/
qjsonlistmodel.cpp
executable file
·179 lines (155 loc) · 6.08 KB
/
qjsonlistmodel.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
#include "qjsonlistmodel.h"
#include <QDebug>
#include <QJsonObject>
QJsonListModel::QJsonListModel(QObject *parent)
:QAbstractListModel(parent)
{
}
QVariant QJsonListModel::data(const QModelIndex &index, int role) const
{
if(role < Qt::UserRole) {
qDebug() << "Nope: QJsonListModel::data with non UserRole";
return QVariant{};
}
if(role == m_roleNames.key("json"))
{
auto o = jsonObjects[index.row()].toObject();
//qDebug() << "accessing json: " << o;
return o;
}
return jsonObjects[index.row()].toObject()[this->m_roleNames[role]].toVariant();
}
QVariant QJsonListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
return "Header";
}
int QJsonListModel::rowCount(const QModelIndex &parent) const
{
return this->jsonObjects.size();
}
void QJsonListModel::generateRoleNames()
{
m_roleNames.clear();
int id = 1;
// qDebug() << "generateRoleNames: " << jsonObjects.first().toObject();
for (auto key : jsonObjects.first().toObject().keys()) {
auto new_id = Qt::UserRole + id++;
m_roleNames.insert(new_id, key.toUtf8());
m_roleNumbers.insert(key.toUtf8(), new_id);
//qDebug() << "adding role " << key;
}
m_roleNames.insert(Qt::UserRole + id, "json");
m_roleNumbers.insert("json", id++);
}
QModelIndexList QJsonListModel::load(QByteArray arr)
{
QJsonParseError err;
auto jsonDocument = QJsonDocument::fromJson(arr, &err);
qDebug() << err.error;
return load(jsonDocument.array());
}
struct _Counter {
_Counter(int start=0):m_c(start) {}
int operator()() {return m_c++;}
int m_c;
};
#include <functional>
template <typename _A>
struct _Generator :public _Counter {
_Generator(std::function<_A(int)> fn, int start=0): _Counter(start) {this->fn=fn;}
std::function<_A(int)> fn;
_A operator()() {return fn(m_c++);}
};
#include <algorithm>
QModelIndexList QJsonListModel::load(QJsonArray arr)
{
beginResetModel();
jsonObjects = arr;
qDebug() << arr.size();
qDebug() << arr[0].toObject().keys();
generateRoleNames();
endResetModel();
QVector<QModelIndex> l{jsonObjects.size()};/*
QVector<int> _v{jsonObjects.size(), 0};
std::transform(_v.begin(), _v.begin() + jsonObjects.size() - 1,
_v.begin() + 1, [](int a){qDebug() << "ääääh" << a; return ++a;});
std::transform(_v.begin(), _v.end(),
l.begin(), [this](int a){return index(a);});
qDebug() << "Listy " << _v << "size: " << _v.size()
<< "should be " << jsonObjects.size() << " l " << l;
return {l.toList()};*/
QVector<QModelIndex> ret{arr.size()};
//qDebug() << "DDYYYYYIIINNG";
std::generate(ret.begin(), ret.end(),
_Generator<QModelIndex>{[this](int i){return this->index(i);},
rowCount() - arr.size()});
return ret.toList();
}
QModelIndexList QJsonListModel::add(QJsonArray arr)
{
if(rowCount() == 0)
return {QList<QModelIndex>{} << load(arr)};
beginInsertRows(index(0).parent(), rowCount(), rowCount() + arr.count() - 1);
jsonObjects += arr;
endInsertRows();
QVector<QModelIndex> ret{arr.size()};
std::generate(ret.begin(), ret.end(),
_Generator<QModelIndex>{[this](int i){return this->index(i);},
rowCount() - arr.size()});
return {ret.toList()};
}
QModelIndex QJsonListModel::add(QJsonObject obj)
{
if(obj.isEmpty())
return {};
if(rowCount() == 0)
return load(QJsonArray{obj})[0];
auto newIndex = rowCount();
beginInsertRows({}, newIndex, newIndex);
// beginInsertRows(index(0).parent(), rowCount(), rowCount());
jsonObjects.append(obj);
endInsertRows();
qDebug() << "New rowcount " << rowCount(index(0)) << ":" << rowCount()
<< " json size " << jsonObjects.size();
return index(newIndex);
}
bool QJsonListModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)
{
/* On models that support this, moves count rows starting with the given sourceRow under parent sourceParent to row destinationChild under parent destinationParent.
Returns true if the rows were successfully moved; otherwise returns false.
*/
QVector<QJsonValue> items{count};
// sourceAffectedRows
int sarMin = sourceRow, sarMax = sourceRow + count - 1;
auto newIndex = destinationChild < sourceRow ?
destinationChild
: destinationChild - count + 1;
beginMoveRows(sourceParent, sarMin, sarMax,
sourceParent,
destinationChild < sourceRow ? newIndex
: newIndex + 1);
auto it = items.begin();
for (int i = sarMin; i <= sarMax; i++)
*it++ = (jsonObjects.takeAt(i));
qDebug() << "(source row, destination row) "
<< sourceRow << destinationChild
<< " newIndex " << newIndex;
it = items.begin();
for (int i = 0; i < count; i++)
jsonObjects.insert(newIndex + i, *it++);
endMoveRows();
return true;
/*
* bool QAbstractItemModel::beginMoveRows(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationChild)
*
Specify the first and last row numbers for the span of rows in the source parent you want to move in the model. Also specify the row in the destination parent to move the span to.
For example, as shown in the diagram, we move three rows from row 2 to 4 in the source, so sourceFirst is 2 and sourceLast is 4. We move those items to above row 2 in the destination, so destinationChild is 2.
beginMoveRows(sourceParent, 2, 4, destinationParent, 2);
This moves the three rows rows 2, 3, and 4 in the source to become 2, 3 and 4 in the destination. Other affected siblings are displaced accordingly.
*/
}
bool QJsonListModel::move(int fromIndex, int toIndex)
{
return moveRows({}, fromIndex, 1,
{}, toIndex);
}