-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxmltreemodel.cpp
More file actions
394 lines (322 loc) · 10.5 KB
/
xmltreemodel.cpp
File metadata and controls
394 lines (322 loc) · 10.5 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include "xmltreemodel.h"
#include <QDebug>
#include <QTextCodec>
TreeItem::TreeItem(const QString &name, TreeItem *parent)
{
parentItem = parent;
itemName = name;
itemDescription = "";
}
TreeItem::TreeItem(const QString &name,const QList<Attribute> &attributes,const QString &description, TreeItem *parent){
parentItem = parent;
itemName = name;
itemDescription = description;
itemAttributes = attributes;
}
TreeItem::~TreeItem()
{
qDeleteAll(childItems);
}
void TreeItem::appendChild(TreeItem *item)
{
Q_ASSERT(item);//debug only
childItems.append(item);
}
void TreeItem::insertChild(int index, const QString &key){
childItems.insert(index,new TreeItem(key,this));
}
TreeItem *TreeItem::child(int row)
{
return childItems.value(row);
}
int TreeItem::childCount() const
{
return childItems.count();
}
TreeItem *TreeItem::parent()
{
return parentItem;
}
int TreeItem::row() const
{
if (parentItem)//because parent of root is 0x0
return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));
return 0;
}
QString TreeItem::name() const
{
return itemName;
}
QString TreeItem::description() const
{
return itemDescription;
}
void TreeItem::setDescription(QString description)
{
itemDescription = description;
}
QList<TreeItem::Attribute> TreeItem::attributes() const
{
return itemAttributes;
}
QString TreeItem::setAttribute(const QString &key,const QString &value)
{
QString ret("");
if (!itemAttributes.isEmpty()){
//test if attribute exists -> replace value
QList<Attribute>::iterator i;
for(i = itemAttributes.begin(); (i != itemAttributes.end()) && ret.isEmpty(); i++){
if (key == (*i).key){//found attribute
ret = (*i).value; //store old value
(*i).value = value; //overwrite with new
}
}
}
//if attribute hasn't exist append a new one
if (ret.isEmpty()){
TreeItem::Attribute a;
a.key = key;
a.value = value;
itemAttributes.append(a);
}
return ret;//return the old value (empty if attribute hasn't exist)
}
void TreeItem::insertAttribute(int index, const QString &key, const QString &value)
{
TreeItem::Attribute a;
a.key = key;
a.value = value;
itemAttributes.insert(index,a);
}
TreeItem::Attribute TreeItem::attribute(int index) const
{
return itemAttributes.value(index);
}
void TreeItem::removeAttribute(int index)
{
itemAttributes.removeAt(index);
}
void TreeItem::removeChild(int index)
{
childItems.removeAt(index);
}
XmlTreeModel::XmlTreeModel(QFile &device, QObject *par) : QAbstractItemModel(par){
//for now readonly
init();
if (device.open(QIODevice::ReadOnly)){
QXmlStreamReader xmlReader(&device);
while (!xmlReader.atEnd() && (xmlReader.name().toString() == "" || xmlReader.isWhitespace())){
xmlReader.readNext();
}
if (!xmlReader.atEnd()){
TreeItem * parent;
//cathing <config>-Tag (or any other first tag) as root item
root = new TreeItem(xmlReader.name().toString(),0);
parent = root;
while (!xmlReader.atEnd()) {
xmlReader.readNext();
//create a new childItem for every start tag
if (xmlReader.isStartElement()) {
//summing attributes
QList<TreeItem::Attribute> *attr = new QList<TreeItem::Attribute>();
foreach(QXmlStreamAttribute elem, xmlReader.attributes()){
TreeItem::Attribute a;
a.key = elem.name().toString();
a.value = elem.value().toString();
(*attr).append(a);
}
//create the new item an append to parent
TreeItem * item = new TreeItem(xmlReader.name().toString(),*attr,xmlReader.text().toString(),parent);
parent->appendChild(item);
//step up in the tree
parent = item;
} else if (xmlReader.isEndElement()) {
//step down from the tree
parent = parent->parent();
// if token contains text content set it as parents description
} else if (xmlReader.isCharacters() && !xmlReader.isWhitespace()) {
parent->setDescription(xmlReader.text().toString());
}
}
}
// warning on error
if (xmlReader.hasError()) {
qWarning() << xmlReader.errorString();
}
xmlReader.clear();
device.close();
}else{
throw device.error();
}
}
XmlTreeModel::~XmlTreeModel(){
}
int XmlTreeModel::columnCount(const QModelIndex &parent) const
{
// if (!parent.isValid())
// return 0;
// else
return 1;
}
QVariant XmlTreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || role != Qt::DisplayRole)
return QVariant();
return QVariant(static_cast<TreeItem*>(index.internalPointer())->name());
}
Qt::ItemFlags XmlTreeModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return 0;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
QVariant XmlTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
return QVariant(root->name());
else
return QVariant();
}
QModelIndex XmlTreeModel::index(int row, int column, const QModelIndex &parent) const
{
if (!parent.isValid())
return createIndex(0, 0, root);
else {
TreeItem *childItem = static_cast<TreeItem*>(parent.internalPointer())->child(row);
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
}
}
QModelIndex XmlTreeModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
TreeItem *parentItem = static_cast<TreeItem*>(index.internalPointer())->parent();
if (parentItem == 0)
return QModelIndex();
return createIndex(parentItem->row(), 0, parentItem);
}
int XmlTreeModel::rowCount(const QModelIndex &parent) const
{
TreeItem *parentItem;
if (parent.column() > 0)
return 0;
if (!parent.isValid())
return 1;
else
parentItem = static_cast<TreeItem*>(parent.internalPointer());
return parentItem->childCount();
}
QString XmlTreeModel::description(const QModelIndex &parent) const
{
if (!parent.isValid())
return "";
TreeItem * item = static_cast<TreeItem*>(parent.internalPointer());
if(item != 0x0){
QString ret;
do {
ret = item->description();
item = item->parent();
}while (ret.isEmpty() && item != 0x0);
return ret;
}else{
return QString();
}
}
QList<TreeItem::Attribute> XmlTreeModel::attributes(const QModelIndex &parent) const
{
if (!parent.isValid())
return QList<TreeItem::Attribute>();
return static_cast<TreeItem*>(parent.internalPointer())->attributes();
}
bool XmlTreeModel::changeAttribute(const QModelIndex &parent, const QString &key, const QString &value)
{
if (!parent.isValid())
return false;
return (!static_cast<TreeItem*>(parent.internalPointer())->setAttribute(key,value).isEmpty());
}
bool XmlTreeModel::save(QFile &device)
{
device.open(QIODevice::WriteOnly);
QXmlStreamWriter xmlWriter(&device);
xmlWriter.setAutoFormatting(true);//adds line breaks
xmlWriter.setAutoFormattingIndent(true);//<tag /> have bad indentation anyway
// xmlWriter.setCodec(); //should be automatically UTF-8 from header
xmlWriter.writeStartDocument();
TreeItem * item = root;
recursiveWriting(xmlWriter,item);//recurse through model tree
xmlWriter.writeEndDocument();
xmlWriter.~QXmlStreamWriter();//to fully write buffer
device.close();
return true;
}
void XmlTreeModel::recursiveWriting(QXmlStreamWriter &xmlWriter, TreeItem *item)
{
QList<TreeItem::Attribute>::iterator i;
//NAME (parent)
xmlWriter.writeStartElement(item->name());
//ATTRIBUTES
QList<TreeItem::Attribute> att = item->attributes();
for (i = att.begin(); i != att.end(); i++){
xmlWriter.writeAttribute((*i).key,(*i).value);
}
//DESCRIPTION as first child characters
if(!item->description().isEmpty()){
xmlWriter.writeCharacters(item->description());
}
//recurse to children
for (int j = 0; j < item->childCount(); j++){
recursiveWriting(xmlWriter,item->child(j));
}
//close parent
xmlWriter.writeEndElement();
}
QList<QModelIndex> XmlTreeModel::findItems(const QString &searchString) const
{
QList<QModelIndex> list;
if (!searchString.isEmpty())
recursiveSearch(&list, 0, root, searchString);//recurse through tree
return list;
}
void XmlTreeModel::recursiveSearch(QList<QModelIndex> *list, int row, TreeItem *item, const QString &searchString) const
{
if (item->name().contains(searchString,Qt::CaseInsensitive) || item->description().contains(searchString,Qt::CaseInsensitive)){
//append item to results if name or description contain the searchString
list->append(createIndex(row,0,item));
}
for(int i = 0; i < item->childCount(); i++){//recurse to children
recursiveSearch(list,i,item->child(i),searchString);
}
}
void XmlTreeModel::insertAttribute(const QModelIndex &parent, int index, const QString &key, const QString &value)
{
if (parent.isValid()){
static_cast<TreeItem*>(parent.internalPointer())->insertAttribute(index,key,value);
}
}
TreeItem::Attribute XmlTreeModel::attribute(const QModelIndex &parent, int index) const
{
if (parent.isValid()){
return static_cast<TreeItem*>(parent.internalPointer())->attribute(index);
}
}
void XmlTreeModel::removeAttribute(const QModelIndex &parent, int index)
{
if (parent.isValid()){
return static_cast<TreeItem*>(parent.internalPointer())->removeAttribute(index);
}
}
void XmlTreeModel::insertChild(QModelIndex index, int aIndex, QString name)
{
this->beginInsertRows(index, aIndex, aIndex);
static_cast<TreeItem*>(index.internalPointer())->insertChild(aIndex,name);
this->endInsertRows();
}
void XmlTreeModel::removeChild(QModelIndex index, int aIndex)
{
this->beginRemoveRows(index,aIndex, aIndex);
static_cast<TreeItem*>(index.internalPointer())->removeChild(aIndex);
this->endRemoveRows();
}