-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml.cpp
126 lines (96 loc) · 3.07 KB
/
xml.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
#include <QtXml>
#include "macrosdialog.h"
#include "ui_macrosdialog.h"
///////////////////////////////////////////////////////////////////////////////
QDomElement MacrosDialog::paramToNode(QDomDocument &d, const ItemClass &ic)
{
QDomElement e = d.createElement("Row");
e.setAttribute("Command", ic.command);
e.setAttribute("Data", ic.data);
e.setAttribute("Response", ic.response);
e.setAttribute("Comment", ic.comment);
return e;
}
///////////////////////////////////////////////////////////////////////////////
bool MacrosDialog::saveDocument(const QString &filename)
{
QDomDocument doc("NITERM_ML");
QDomElement root = doc.createElement("Macros");
doc.appendChild(root);
ItemClass ic;
for (int i=0; i<ui->table->rowCount(); i++)
{
ic.command = ui->table->item(i, COL_MACRO_COMMAND) ->text();
ic.data = ui->table->item(i, COL_MACRO_DATA) ->text();
ic.response = ui->table->item(i, COL_MACRO_RESPONSE)->text();
ic.comment = ui->table->item(i, COL_MACRO_COMMENT) ->text();
root.appendChild(paramToNode(doc, ic));
}
QFile file(filename);
if (!file.open(QIODevice::WriteOnly))
{
return false;
}
QTextStream ts(&file);
ts.setCodec("UTF-8");
ts << "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\r\n";
ts << doc.toString();
file.close();
return true;
}
///////////////////////////////////////////////////////////////////////////////
bool MacrosDialog::loadDocument(const QString &filename)
{
clearDocument();
QDomDocument doc("NITERM_ML");
QFile file(filename);
if (!file.open(QIODevice::ReadOnly))
{
return false;
}
if (!doc.setContent(&file))
{
file.close();
return false;
}
file.close();
QDomElement root = doc.documentElement();
if (root.tagName() != "Macros")
{
return false;
}
QDomNode n = root.firstChild();
while (!n.isNull())
{
QDomElement e = n.toElement();
if (!e.isNull())
{
if (e.tagName() == "Row")
{
ItemClass ic;
ic.command = e.attribute("Command", "");
ic.data = e.attribute("Data", "");
ic.response = e.attribute("Response", "");
ic.comment = e.attribute("Comment", "");
int row = insertTableRow(ui->table->rowCount());
ui->table->item(row, COL_MACRO_COMMAND) ->setText(ic.command);
ui->table->item(row, COL_MACRO_DATA) ->setText(ic.data);
ui->table->item(row, COL_MACRO_RESPONSE)->setText(ic.response);
ui->table->item(row, COL_MACRO_COMMENT) ->setText(ic.comment);
}
}
n = n.nextSibling();
}
return true;
}
///////////////////////////////////////////////////////////////////////////////
void MacrosDialog::clearDocument()
{
if (ui->table->rowCount())
{
for (int i=ui->table->rowCount()-1; i>=0; i--)
{
ui->table->removeRow(i);
}
}
}