forked from mcclure/XmlEdit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmledit.cpp
222 lines (182 loc) · 6.41 KB
/
xmledit.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
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
#include "xmledit.h"
#include <QMessageBox>
#include <QTextStream>
#include <QStack>
#include <QLabel>
#include <QLineEdit>
#include <QPlainTextEdit>
DocumentEdit::DocumentEdit(QWidget *parent) : QScrollArea(parent) {
setWidgetResizable(true);
}
void DocumentEdit::clearUi() {
setWidget(new QWidget());
}
XmlEdit::XmlEdit(QWidget *parent) : DocumentEdit(parent), vLayout(NULL) {
}
XmlEdit::~XmlEdit() {
}
void XmlEdit::clearUi() {
DocumentEdit::clearUi();
vLayout = new QVBoxLayout(widget());
widget()->setLayout(vLayout);
}
static const QString &nodeTypeName(QDomNode::NodeType nodeType) {
static QString Element("Element"), Attribute("Attribute"), Text("Text"),
CDATA("CData"), EntityReference("Entity Reference"), Entity("Entity"),
ProcessingInstruction("Processing Instruction"), Comment("Comment"),
Document("Document"), DocumentType("Document Type"),
DocumentFragment("DocumentFragment"), Notation("Notation"),
Base("Untyped?"), CharacterData("CharacterData");
switch(nodeType) {
case QDomNode::ElementNode: return Element;
case QDomNode::AttributeNode: return Attribute;
case QDomNode::TextNode: return Text;
case QDomNode::CDATASectionNode: return CDATA;
case QDomNode::EntityReferenceNode: return EntityReference;
case QDomNode::EntityNode: return Entity;
case QDomNode::ProcessingInstructionNode: return ProcessingInstruction;
case QDomNode::CommentNode: return Comment;
case QDomNode::DocumentNode: return Document;
case QDomNode::DocumentTypeNode: return DocumentType;
case QDomNode::DocumentFragmentNode: return DocumentFragment;
case QDomNode::NotationNode: return Notation;
case QDomNode::BaseNode: return Base;
case QDomNode::CharacterDataNode: return CharacterData;
}
}
#include <watchers.h>
void XmlEdit::addNode(QDomNode node, int depth) {
QWidget *pane = new QWidget(widget());
QWidget *content = pane;
if (depth > 0) {
QHBoxLayout *hPaneLayout = new QHBoxLayout(pane);
hPaneLayout->setContentsMargins(0,0,0,0);
pane->setLayout(hPaneLayout);
hPaneLayout->addSpacing(depth*40);
content = new QWidget(pane);
hPaneLayout->addWidget(content);
}
QVBoxLayout *vContentLayout = new QVBoxLayout(content);
vContentLayout->setContentsMargins(0,0,0,0);
content->setLayout(vContentLayout);
QLabel *kindLabel = new QLabel(content); vContentLayout->addWidget(kindLabel);
kindLabel->setText(nodeTypeName(node.nodeType()));
vContentLayout->addWidget(kindLabel);
switch(node.nodeType()) {
case QDomNode::ElementNode: {
QDomElement element = node.toElement();
QLineEdit *tagEdit = new QLineEdit(content);
vContentLayout->addWidget(tagEdit);
QFontMetrics metrics(tagEdit->font());
int columnWidth = metrics.averageCharWidth();
tagEdit->setFixedWidth(30*columnWidth);
tagEdit->setText(element.tagName());
new TagNameWatcher(tagEdit, element);
QDomNamedNodeMap attributes = element.attributes();
for(int c = 0; c < attributes.length(); c++) {
QDomNode attributeNode = attributes.item(c);
QDomAttr attribute = attributeNode.toAttr();
if (!attribute.isNull()) {
QWidget *assign = new QWidget(content);
QHBoxLayout *hAssignLayout = new QHBoxLayout(assign);
hAssignLayout->setContentsMargins(0,0,0,0);
assign->setLayout(hAssignLayout);
vContentLayout->addWidget(assign);
QLineEdit *assignLeft = new QLineEdit(assign);
hAssignLayout->addWidget(assignLeft);
assignLeft->setFixedWidth(30*columnWidth);
assignLeft->setText(attribute.name());
QLabel *assignLabel = new QLabel("=", assign);
hAssignLayout->addWidget(assignLabel);
QLineEdit *assignRight = new QLineEdit(assign);
hAssignLayout->addWidget(assignRight);
assignRight->setFixedWidth(38*columnWidth);
assignRight->setText(attribute.value());
new AttrWatcher(assignLeft, assignRight, element);
hAssignLayout->addStretch();
}
}
} break;
case QDomNode::TextNode:
case QDomNode::CommentNode: {
QDomCharacterData data = node.toCharacterData();
QPlainTextEdit *textEdit = new QPlainTextEdit(content);
QFontMetrics metrics(textEdit->font());
int rowHeight = metrics.lineSpacing();
int columnWidth = metrics.averageCharWidth();
textEdit->setFixedHeight(6*rowHeight);
textEdit->setFixedWidth(80*columnWidth);
textEdit->setPlainText(data.data());
vContentLayout->addWidget(textEdit);
new CharacterDataWatcher(textEdit, data);
} break;
// These should be impossible
case QDomNode::AttributeNode:
case QDomNode::BaseNode:
case QDomNode::CharacterDataNode:
// I don't know what these are
case QDomNode::ProcessingInstructionNode:
case QDomNode::DocumentNode:
case QDomNode::DocumentTypeNode:
case QDomNode::DocumentFragmentNode:
case QDomNode::NotationNode:
case QDomNode::CDATASectionNode:
case QDomNode::EntityReferenceNode:
case QDomNode::EntityNode:
break;
}
//label->setFrameStyle(QFrame::Panel | QFrame::Sunken);
//label->setAlignment(Qt::AlignBottom | Qt::AlignRight);
vLayout->addWidget(pane);
}
bool XmlEdit::isModified() const {
return false;
}
#ifndef QT_NO_CLIPBOARD
void XmlEdit::cut() {
}
void XmlEdit::copy() {
}
void XmlEdit::paste() {
}
#endif
bool XmlEdit::read(QIODevice *device) {
QString errorStr;
int errorLine;
int errorColumn;
if (!domDocument.setContent(device, true, &errorStr, &errorLine,
&errorColumn)) {
QMessageBox::information(window(), tr("XML Editor"),
tr("Parse error at line %1, column %2:\n%3")
.arg(errorLine)
.arg(errorColumn)
.arg(errorStr));
return false;
}
clearUi();
QDomElement root = domDocument.documentElement();
QStack<QDomNode> stack;
QDomNode current = root;
while(1) {
if (!current.isNull()) {
addNode(current, stack.count());
stack.push(current);
current = current.firstChild();
} else if (!stack.count()) {
break;
} else {
current = stack.pop().nextSibling();
}
}
return true;
}
bool XmlEdit::write(QIODevice *device) const {
const int IndentSize = 4;
QTextStream out(device);
domDocument.save(out, IndentSize);
return true;
}
void XmlEdit::clear() { // Also resets file state
domDocument.clear();
clearUi();
}