forked from andreimacavei/FinTP-Configuration-Wizard
/
fintpconfigwizard.cpp
164 lines (147 loc) · 5.31 KB
/
fintpconfigwizard.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
#include "fintpconfigwizard.h"
#include "ui_fintpconfigwizard.h"
#include <QtWidgets>
/*
FinTPConfigWizard::FinTPConfigWizard(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::FinTPConfigWizard)
{
ui->setupUi(this);
}
FinTPConfigWizard::~FinTPConfigWizard()
{
delete ui;
}
*/
TabDialog::TabDialog(QWidget *parent)
: QDialog(parent)
{
tabWidget = new QTabWidget;
/*
* TO DO: Populate tabWidget with tabs retrieved from parsed XML
*/
parseXML();
buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->setSizeConstraint(QLayout::SetNoConstraint);
mainLayout->addWidget(tabWidget);
mainLayout->addWidget(buttonBox);
setLayout(mainLayout);
setWindowTitle(tr("FinTP Config GUI"));
}
void TabDialog::parseXML() {
/* We'll parse the example.xml */
QFile* file = new QFile("../FinTP-Configuration-Wizard/example.xml");
/* If we can't open it, let's show an error message. */
if (!file->open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::critical(this,
"TabDialog::parseXML",
"Couldn't open example.xml",
QMessageBox::Ok);
return;
}
/* QXmlStreamReader takes any QIODevice. */
QXmlStreamReader xml(file);
QList< QMap<QString,QString> > sections;
/* We'll parse the XML until we reach end of it.*/
while(!xml.atEnd() &&
!xml.hasError()) {
/* Read next element.*/
QXmlStreamReader::TokenType token = xml.readNext();
/* If token is just StartDocument, we'll go to next.*/
if(token == QXmlStreamReader::StartDocument) {
continue;
}
/* If token is StartElement, we'll see if we can read it.*/
if(token == QXmlStreamReader::StartElement) {
/* If it's named configuration, we'll go to the next.*/
if(xml.name() == "configuration") {
continue;
}
/* If it's named sectionGroup, we'll dig the information from there.*/
if(xml.name() == "sectionGroup") {
sections.append(this->parseSectionGroup(xml));
}
}
}
/* Error handling. */
if(xml.hasError()) {
QMessageBox::critical(this,
"TabDialog::parseXML",
xml.errorString(),
QMessageBox::Ok);
}
/* Removes any device() or data from the reader
* and resets its internal state to the initial state. */
xml.clear();
this->addSectionsToUI(sections);
}
QMap<QString, QString> TabDialog::parseSectionGroup(QXmlStreamReader& xml) {
QMap<QString, QString> sectionGroup;
/* Let's check that we're really getting a sectionGroup. */
if(xml.tokenType() != QXmlStreamReader::StartElement &&
xml.name() == "sectionGroup") {
return sectionGroup;
}
/* Let's get the attributes for sectionGroup */
QXmlStreamAttributes attributes = xml.attributes();
/* Let's check that sectionGroup has name attribute. */
if(attributes.hasAttribute("name")) {
/* We'll add it to the map. */
sectionGroup["name"] = attributes.value("name").toString();
}
/* Next element... */
xml.readNext();
/*
* We're going to loop over the things because the order might change.
* We'll continue the loop until we hit an EndElement named sectionGroup.
*/
while(!(xml.tokenType() == QXmlStreamReader::EndElement &&
xml.name() == "sectionGroup")) {
if(xml.tokenType() == QXmlStreamReader::StartElement) {
/* We've add all section filters to Map. */
if(xml.name() == "section") {
this->addElementDataToMap(xml, sectionGroup);
}
}
/* ...and next... */
xml.readNext();
}
return sectionGroup;
}
void TabDialog::addElementDataToMap(QXmlStreamReader& xml,
QMap<QString, QString>& map) const {
/* We need a start element, like <foo> */
if(xml.tokenType() != QXmlStreamReader::StartElement) {
return;
}
/* Let's read the filter */
QString elementName = xml.name().toString();
/* ...go to the next. */
xml.readNext();
/*
* This elements needs to contain Characters so we know it's
* actually data, if it's not we'll leave.
*/
if(xml.tokenType() != QXmlStreamReader::Characters) {
return;
}
/* Now we can add it to the map.*/
map.insert(elementName, xml.text().toString());
}
void TabDialog::addSectionsToUI(QList< QMap<QString,QString> >& sections) {
while(!sections.isEmpty()) {
QWidget *tab = new QWidget();
QFormLayout* layout = new QFormLayout;
QMap<QString,QString> section = sections.takeFirst();
QMap<QString, QString>::const_iterator iter = section.constBegin();
while ( iter != section.constEnd()){
layout->addRow(iter.key(), new QLineEdit(iter.value()));
++iter;
}
tab->setLayout(layout);
this->tabWidget->addTab(tab, section.value("name", "Unknown"));
}
}