/
FileReader.cpp
97 lines (91 loc) · 3.73 KB
/
FileReader.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
#include <QString>
#include <QByteArray>
#include <QXmlSchema>
#include <QXmlSchemaValidator>
#include <QMessageBox>
#include <QDomDocument>
#include <QDomNode>
#include "FileReader.h"
FileReader::FileReader(QString fileName, QAbstractItemModel* d, MyWidget* p) : file(fileName), model(d), parent(p) {
QByteArray data("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
"<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"
"<xsd:element name=\"chart\" type=\"chartType\"/>"
" <xsd:complexType name=\"chartType\">"
" <xsd:sequence>"
" <xsd:element name=\"title\" type=\"xsd:string\"/>"
" <xsd:element name=\"xlabel\" type=\"xsd:string\" minOccurs=\"0\"/>"
" <xsd:element name=\"ylabel\" type=\"xsd:string\" minOccurs=\"0\"/>"
" <xsd:element name=\"point\" type=\"pointType\" maxOccurs=\"unbounded\"/>"
" </xsd:sequence>"
" </xsd:complexType>"
" <xsd:complexType name=\"pointType\">"
" <xsd:sequence>"
" <xsd:element name=\"x\" type=\"xsd:string\"/>"
" <xsd:element name=\"y\" type=\"xsd:string\"/>"
" </xsd:sequence>"
" </xsd:complexType>"
"</xsd:schema>");
if(!file.open(QFile::ReadOnly | QFile::Text)) {
QMessageBox::warning(parent, tr("qCharts"), tr("Cannot read file %1:\n%2.").arg(fileName).arg(file.errorString()));
return;
}
QXmlSchema schema;
schema.load(data);
if(schema.isValid()) {
QXmlSchemaValidator validator(schema);
if(validator.validate(&file, QUrl::fromLocalFile(file.fileName()))){
isReadable=true;
}
else {
isReadable=false;
QMessageBox::warning(this, tr("qCharts"), tr("The file that you are trying to open isn't valid."));
}
}
file.close();
}
void FileReader::read() {
if(isReadable) {
file.open(QFile::ReadOnly | QFile::Text);
QDomDocument doc;
if(!doc.setContent(&file, false))
QMessageBox::warning(this, tr("qCharts"), tr("There was an error trying to parse the file."));
QDomElement root = doc.documentElement();
parseDocument(root);
file.close();
}
}
void FileReader::parseDocument(const QDomElement& element) {
model->removeRows(0, model->rowCount());
int row=0;
QDomNode child = element.firstChild();
while(!child.isNull()) {
if(child.toElement().tagName() == "title")
readTitle(child.toElement());
else if(child.toElement().tagName() == "xlabel")
readXLabel(child.toElement());
else if(child.toElement().tagName() == "ylabel")
readYLabel(child.toElement());
else if(child.toElement().tagName() == "point") {
model->insertRow(row);
QDomNode nephew = child.firstChild();
while(!nephew.isNull()) {
if(nephew.toElement().tagName() == "x")
model->setData(model->index(row, 0), nephew.toElement().text());
else if(nephew.toElement().tagName() == "y")
model->setData(model->index(row, 1), nephew.toElement().text());
nephew = nephew.nextSibling();
}
row++;
}
child = child.nextSibling();
}
}
void FileReader::readTitle(const QDomElement& element) {
parent->setTitle(element.text());
}
void FileReader::readXLabel(const QDomElement& element) {
parent->setXLabel(element.text());
}
void FileReader::readYLabel(const QDomElement& element) {
parent->setYLabel(element.text());
}