-
Notifications
You must be signed in to change notification settings - Fork 19
/
listmodel.cpp
169 lines (129 loc) · 4.44 KB
/
listmodel.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
#include "_cgo_export.h"
#include "listmodel.h"
// Overrided method for QAbstractListModel
int QamelListModel::rowCount(const QModelIndex &) const {
return _contents.size();
}
QVariant QamelListModel::data(const QModelIndex &index, int role) const {
if (role != Qt::DisplayRole) {
return QVariant();
}
return _contents.at(index.row());
}
QHash<int, QByteArray> QamelListModel::roleNames() const {
QHash<int, QByteArray> role;
role[Qt::DisplayRole] = "display";
return role;
}
bool QamelListModel::insertRows(int row, int count, const QModelIndex &parent) {
return QAbstractListModel::insertRows(row, count, parent);
}
bool QamelListModel::removeRows(int row, int count, const QModelIndex &parent) {
return QAbstractListModel::removeRows(row, count, parent);
}
bool QamelListModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) {
return QAbstractListModel::moveRows(sourceParent, sourceRow, count, destinationParent, destinationChild);
}
// Methods for custom QML properties
QVariantList QamelListModel::contents() const {
return _contents;
}
void QamelListModel::setContents(QVariantList contents) {
beginResetModel();
_contents = contents;
endResetModel();
emit contentsChanged();
}
// Methods for custom slots
QVariant QamelListModel::get(int row) {
return _contents.at(row);
}
int QamelListModel::count() {
return _contents.count();
}
void QamelListModel::clear() {
beginResetModel();
_contents.clear();
endResetModel();
}
void QamelListModel::insertRow(int row, QVariant obj) {
beginInsertRows(QModelIndex(), row, row);
_contents.insert(row, obj);
endInsertRows();
emit contentsChanged();
}
void QamelListModel::insertRows(int row, QVariantList objects) {
beginInsertRows(QModelIndex(), row, row+objects.count()-1);
for (int i = 0; i < objects.count(); ++i) {
_contents.insert(row+i, objects[i]);
}
endInsertRows();
emit contentsChanged();
}
void QamelListModel::appendRow(QVariant obj) {
int row = _contents.count();
beginInsertRows(QModelIndex(), row, row);
_contents.insert(row, obj);
endInsertRows();
emit contentsChanged();
}
void QamelListModel::appendRows(QVariantList objects) {
int row = _contents.count();
int last = row + objects.count() - 1;
beginInsertRows(QModelIndex(), row, last);
for (int i = 0; i < objects.count(); ++i) {
_contents.insert(row+i, objects[i]);
}
endInsertRows();
emit contentsChanged();
}
void QamelListModel::deleteRows(int row, int count) {
if (row < 0 || row >= rowCount()) return;
int last = row+count-1;
beginRemoveRows(QModelIndex(), row, last);
for (int i = last; i >= row; --i) {
_contents.removeAt(i);
}
endRemoveRows();
emit contentsChanged();
}
void QamelListModel::setRow(int row, QVariantMap newObj) {
if (row < 0 || row >= rowCount()) return;
_contents[row] = newObj;
emit contentsChanged();
emit dataChanged(createIndex(row, 0), createIndex(row, 0));
}
void QamelListModel::setRowProperty(int row, QString propName, QVariant newProp) {
if (row < 0 || row >= rowCount()) return;
QVariant obj = _contents.at(row);
QVariantMap map = obj.toMap();
map[propName] = newProp;
_contents[row] = QVariant(map);
emit contentsChanged();
emit dataChanged(createIndex(row, 0), createIndex(row, 0));
}
void QamelListModel::swapRow(int i, int j) {
if (i == j) return;
if (i < 0 || i >= rowCount()) return;
if (j < 0 || j >= rowCount()) return;
_contents.swap(i, j);
emit dataChanged(createIndex(i, 0), createIndex(i, 0));
emit dataChanged(createIndex(j, 0), createIndex(j, 0));
}
void QamelListModel::moveRows(int first, int last, int dst) {
int lastDst = dst + (last - first);
if (first < 0 || first >= rowCount()) return;
if (last < 0 || last >= rowCount()) return;
if (dst< 0 || dst >= rowCount()) return;
if (lastDst < 0 || lastDst >= rowCount()) return;
beginMoveRows(QModelIndex(), first, last, QModelIndex(), dst);
for (int i = last; i >= first; --i) {
_contents.move(i, lastDst - (last - i));
}
endMoveRows();
emit contentsChanged();
}
void QamelListModel_RegisterQML(char* uri, int versionMajor, int versionMinor, char* qmlName) {
qmlRegisterType<QamelListModel>(uri, versionMajor, versionMinor, qmlName);
}
#include "moc-listmodel.h"