source: flair-src/trunk/tools/FlairGCS/src/XmlWidget.cpp@ 129

Last change on this file since 129 was 68, checked in by Sanahuja Guillaume, 8 years ago

gps vtg

File size: 8.7 KB
Line 
1// %flair:license{
2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
4// %flair:license}
5#include "XmlWidget.h"
6#include <QWidget>
7#include "ConnectionLayout.h"
8#include <stdio.h>
9
10XmlWidget::XmlWidget(QString name, QString type, XmlWidget *parent)
11 : QObject() {
12 childs = new QList<XmlWidget *>;
13 isContainer = false;
14 isExpandable = false;
15 visible_widget = NULL;
16 parent_widget = parent;
17
18 red_pal.setColor(QPalette::Text, QColor(255, 0, 0));
19 red_pal.setColor(QPalette::Foreground, QColor(255, 0, 0));
20 red_pal_greyed.setColor(QPalette::Text, QColor(128, 0, 0));
21 red_pal_greyed.setColor(QPalette::Foreground, QColor(128, 0, 0));
22
23 black_pal.setColor(QPalette::Text, QColor(0, 0, 0));
24 black_pal.setColor(QPalette::Foreground, QColor(0, 0, 0));
25 black_pal_greyed.setColor(QPalette::Text, QColor(128, 128, 128));
26 black_pal_greyed.setColor(QPalette::Foreground, QColor(128, 128, 128));
27
28 setObjectName(name);
29
30 if (parent != NULL) {
31 parent->childs->append(this);
32
33 document = parent->document.cloneNode(true).toDocument();
34
35 write_elem = QDomElement(document.createElement(type));
36 write_elem.setAttribute("name", name);
37 // recupere le node le plus profond
38 QDomNode node = document.firstChild();
39 while (node.firstChild().isNull() == false) {
40 node = node.firstChild();
41 }
42 node.appendChild(write_elem);
43 } else {
44 document = QDomDocument("remote_ui_xml");
45 write_elem = QDomElement(document.createElement(type));
46 write_elem.setAttribute("name", name);
47 document.appendChild(write_elem);
48 }
49}
50
51XmlWidget::~XmlWidget() {
52 if (parent_widget != NULL)
53 parent_widget->childs->removeOne(this);
54
55 // on efface les widgets enfants
56 // dans le delete child on modifie le child du parent, donc on se refere
57 // toujours au premier
58 while (childs->count() != 0) {
59 delete childs->first();
60 }
61
62 delete childs;
63 if (visible_widget != NULL) {
64 delete visible_widget;
65 }
66}
67
68QString XmlWidget::Name(void) { return write_elem.attribute("name"); }
69
70void XmlWidget::SetIsContainer(bool status) { isContainer = status; }
71
72void XmlWidget::SetIsExpandable(bool status) { isExpandable = status; }
73
74XmlWidget *XmlWidget::GetXmlWidget(QString name, QString type) {
75 // printf("recherche %s
76 // %s\n",name.toLocal8Bit().constData(),type.toLocal8Bit().constData());
77
78 for (int i = 0; i < childs->count(); i++) {
79 // printf("child name
80 // %s\n",childs->at(i)->write_elem.attribute("name").toLocal8Bit().constData());
81 // printf("child tag
82 // %s\n",childs->at(i)->write_elem.tagName().toLocal8Bit().constData());
83 if (childs->at(i)->write_elem.attribute("name") == name &&
84 childs->at(i)->write_elem.tagName() == type)
85 return childs->at(i);
86 }
87 return NULL;
88}
89
90void XmlWidget::ParseXml(QDomElement to_parse) {
91
92 if (to_parse.isNull())
93 return;
94
95 QString type = to_parse.tagName();
96 QString name = to_parse.attribute("name");
97
98 // printf("parse %s
99 // %s\n",type.toLocal8Bit().constData(),name.toLocal8Bit().constData());
100 XmlWidget *match;
101 match = GetXmlWidget(name, type);
102
103 if (match == NULL) {
104 // printf("not match\n");
105 XmlEvent(to_parse);
106 } else {
107 // printf("match\n");
108 // si on a une balise IsEnabled, on ne traite que ca
109 if (match->visible_widget != NULL) {
110 if (to_parse.attribute("IsEnabled") == "0") {
111 match->visible_widget->setEnabled(false);
112 return;
113 }
114 if (to_parse.attribute("IsEnabled") == "1") {
115 match->visible_widget->setEnabled(true);
116 return;
117 }
118 }
119
120 // si on a une balise delete, on ne traite que ca
121 if (to_parse.attribute("Delete") == "1") {
122 // printf("delete flag\n");
123 if (match->isContainer == true && match->childs->count() != 0) {
124 // printf("non vide
125 // %s\n",match->objectName().toLocal8Bit().constData());
126 return;
127 }
128
129 delete match;
130 return;
131 }
132
133 if (to_parse.firstChild().isNull() == true &&
134 match->isExpandable == false) {
135 QString new_name;
136 printf("possible doublon\n");
137 for (int i = 0; i < 65535; i++) {
138 new_name = QString("%1_%2").arg(name).arg(i);
139 bool continuer = false;
140 for (int i = 0; i < childs->count(); i++) {
141 if (childs->at(i)->write_elem.attribute("name") == new_name) {
142 continuer = true;
143 break;
144 }
145 }
146 if (continuer == false)
147 break;
148 }
149 printf("new_name %s\n", new_name.toLocal8Bit().constData());
150 to_parse.setAttribute("name", new_name);
151 to_parse.setAttribute("old_name", name);
152
153 XmlEvent(to_parse);
154
155 // return -1;//ou retourner le xml a renvoyer pour chager de nom
156 } else {
157 if (to_parse.firstChild().toElement().isNull()) {
158 match->XmlEvent(to_parse);
159 return;
160 } else {
161 match->ParseXml(to_parse.firstChild().toElement());
162 }
163 }
164 }
165}
166
167void XmlWidget::LoadXml(QDomElement to_parse) {
168 if (to_parse.isNull())
169 return;
170
171 LoadEvent(to_parse);
172
173 QDomElement elem = to_parse.firstChild().toElement();
174
175 while (!elem.isNull()) {
176
177 QString type = elem.tagName();
178 QString name = elem.attribute("name");
179 // printf("%s
180 // %s\n",type.toLocal8Bit().constData(),name.toLocal8Bit().constData());
181 XmlWidget *match;
182 match = GetXmlWidget(name, type);
183
184 if (match != NULL) {
185 // printf("match\n");
186 match->LoadXml(elem);
187 }
188 elem = elem.nextSibling().toElement();
189 }
190}
191
192void XmlWidget::GetFullXml(QDomElement *doc) {
193 QDomDocument tmp_doc = XmlDoc();
194 merge((QDomElement *)&tmp_doc, doc);
195
196 for (int i = 0; i < childs->count(); i++) {
197 childs->at(i)->GetFullXml(doc);
198 }
199}
200
201void XmlWidget::GetUpdateXml(QDomElement *doc) {
202 if (IsUptodate() == false && isContainer == false) {
203 SetUptodate();
204 QDomDocument tmp_doc = XmlDoc();
205 merge((QDomElement *)&tmp_doc, doc);
206 }
207
208 for (int i = 0; i < childs->count(); i++) {
209 childs->at(i)->GetUpdateXml(doc);
210 }
211}
212
213void XmlWidget::ResetAllChilds(void) {
214 Reset();
215 for (int i = 0; i < childs->count(); i++) {
216 childs->at(i)->ResetAllChilds();
217 }
218}
219
220void XmlWidget::merge(QDomElement *from, QDomElement *into) {
221 QDomElement tmp_into, tmp_from;
222 tmp_from = from->firstChildElement();
223
224 while (tmp_from.isNull() == false) {
225 // search corresponding child
226 bool match = false;
227 tmp_into = into->firstChildElement(tmp_from.tagName());
228 while (tmp_into.isNull() == false) {
229 if (tmp_into.attribute("name") == tmp_from.attribute("name")) {
230 merge(&tmp_from, &tmp_into);
231 match = true;
232 break;
233 }
234 tmp_into = tmp_into.nextSiblingElement(tmp_from.tagName());
235 }
236
237 if (match == false) {
238 into->appendChild(tmp_from.cloneNode());
239 }
240
241 tmp_from = tmp_from.nextSiblingElement();
242 }
243}
244
245QDomDocument XmlWidget::XmlDoc(void) {
246 return document.cloneNode(true).toDocument();
247}
248
249QDomElement *XmlWidget::AddXmlChild(QString type) {
250 QDomElement *elem;
251
252 elem = new QDomElement(document.createElement(type));
253 write_elem.appendChild(*elem);
254
255 return elem;
256}
257
258void XmlWidget::RemoveXmlChild(QDomElement *element) {
259 write_elem.removeChild(*element);
260 delete element;
261}
262
263void XmlWidget::ClearDoc(void) { document.clear(); }
264
265void XmlWidget::SetValue(QString value) {
266 write_elem.setAttribute("value", value);
267}
268
269void XmlWidget::SetAttribute(const QString &name, const QString &value) {
270 write_elem.setAttribute(name, value);
271}
272
273void XmlWidget::SetAttribute(const QString &name, qlonglong value) {
274 write_elem.setAttribute(name, value);
275}
276
277void XmlWidget::SetAttribute(const QString &name, qulonglong value) {
278 write_elem.setAttribute(name, value);
279}
280
281void XmlWidget::SetAttribute(const QString &name, float value) {
282 write_elem.setAttribute(name, value);
283}
284
285void XmlWidget::SetAttribute(const QString &name, double value) {
286 write_elem.setAttribute(name, value);
287}
288
289void XmlWidget::RemoveAttribute(const QString &name) {
290 write_elem.removeAttribute(name);
291}
292
293void XmlWidget::RemoveAllAttributes() {
294 QString name = write_elem.attribute("name");
295
296 QDomNamedNodeMap attributes=write_elem.attributes();
297 while(attributes.count()!=0) {
298 //printf("%i %s\n",attributes.count(),attributes.item(0).toAttr().name().toLocal8Bit().constData());
299 write_elem.removeAttribute(attributes.item(0).toAttr().name());
300 }
301 SetAttribute("name", name);
302}
303
304void XmlWidget::RenamedFrom(QString old_name) {
305 QString name = write_elem.attribute("name");
306 SetAttribute("name", old_name);
307 SetAttribute("new_name", name);
308 connectionLayout()->XmlToSend(XmlDoc());
309 SetAttribute("name", name);
310 write_elem.removeAttribute("new_name");
311}
312
313ConnectionLayout *XmlWidget::connectionLayout(void) {
314 if (parent_widget != NULL) {
315 return (ConnectionLayout *)(parent_widget->connectionLayout());
316 } else {
317 return (ConnectionLayout *)this;
318 }
319}
Note: See TracBrowser for help on using the repository browser.