-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlreader.cpp
84 lines (72 loc) · 2.54 KB
/
xmlreader.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
#include <QtGui>
#include "xmlreader.h"
XmlReader::XmlReader(QTreeWidget *treeWidget,QMap<QString, QColor> itemColors)
: treeWidget(treeWidget)
{
itemColorsXML = itemColors;
}
bool XmlReader::read(QIODevice *device)
{
xml.setDevice(device);
if (xml.readNextStartElement()) {
if (xml.name() == "xml" && xml.attributes().value("version") == "1.0")
readXML();
else
xml.raiseError(QObject::tr("The file is not an XML version 1.0 file."));
}
return !xml.error();
}
QString XmlReader::errorString() const
{
return QObject::tr("%1\nLine %2, column %3")
.arg(xml.errorString())
.arg(xml.lineNumber())
.arg(xml.columnNumber());
}
void XmlReader::readXML()
{
Q_ASSERT(xml.isStartElement() && xml.name() == "xml");
while (xml.readNextStartElement()) {
if (xml.name() == "exercise")
readExercise();
else
xml.skipCurrentElement();
}
}
void XmlReader::readExercise()
{
Q_ASSERT(xml.isStartElement() && xml.name() == "exercise");
QTreeWidgetItem *exercise = createChildItem();
while (xml.readNextStartElement()) {
if (xml.name() == "id") {
Q_ASSERT(xml.isStartElement() && xml.name() == "id");
QString id = xml.readElementText();
exercise->setText(0,id);
QColor bgcolor = itemColorsXML.value(id);
exercise->setBackgroundColor(1,bgcolor);
exercise->setBackgroundColor(2,bgcolor);
exercise->setBackgroundColor(3,bgcolor);
} else if (xml.name() == "action") {
Q_ASSERT(xml.isStartElement() && xml.name() == "action");
QString action = xml.readElementText();
exercise->setText(1,action);
} else if (xml.name() == "time") {
Q_ASSERT(xml.isStartElement() && xml.name() == "time");
QString time = xml.readElementText();
exercise->setText(2,time);
} else if (xml.name() == "description") {
Q_ASSERT(xml.isStartElement() && xml.name() == "description");
QString description = xml.readElementText();
exercise->setText(3,description);
} else {
xml.skipCurrentElement();
}
}
}
QTreeWidgetItem* XmlReader::createChildItem()
{
QTreeWidgetItem * childItem = new QTreeWidgetItem(treeWidget);
childItem->setData(0, Qt::UserRole, xml.name().toString());
childItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEditable | Qt::ItemIsEnabled);
return childItem;
}