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

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

gps vtg

File size: 8.7 KB
RevLine 
[10]1// %flair:license{
[15]2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
[10]4// %flair:license}
[9]5#include "XmlWidget.h"
6#include <QWidget>
7#include "ConnectionLayout.h"
8#include <stdio.h>
9
[15]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;
[9]17
[15]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));
[9]22
[15]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));
[9]27
[15]28 setObjectName(name);
[9]29
[15]30 if (parent != NULL) {
31 parent->childs->append(this);
[9]32
[15]33 document = parent->document.cloneNode(true).toDocument();
[9]34
[15]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();
[9]41 }
[15]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 }
[9]49}
50
[15]51XmlWidget::~XmlWidget() {
52 if (parent_widget != NULL)
53 parent_widget->childs->removeOne(this);
[9]54
[15]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 }
[9]61
[15]62 delete childs;
63 if (visible_widget != NULL) {
64 delete visible_widget;
65 }
[9]66}
67
[15]68QString XmlWidget::Name(void) { return write_elem.attribute("name"); }
[9]69
[15]70void XmlWidget::SetIsContainer(bool status) { isContainer = status; }
[9]71
[15]72void XmlWidget::SetIsExpandable(bool status) { isExpandable = status; }
[9]73
[15]74XmlWidget *XmlWidget::GetXmlWidget(QString name, QString type) {
75 // printf("recherche %s
76 // %s\n",name.toLocal8Bit().constData(),type.toLocal8Bit().constData());
[9]77
[15]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;
[9]88}
89
90void XmlWidget::ParseXml(QDomElement to_parse) {
91
[15]92 if (to_parse.isNull())
93 return;
[9]94
[15]95 QString type = to_parse.tagName();
96 QString name = to_parse.attribute("name");
[9]97
[15]98 // printf("parse %s
99 // %s\n",type.toLocal8Bit().constData(),name.toLocal8Bit().constData());
100 XmlWidget *match;
101 match = GetXmlWidget(name, type);
[9]102
[15]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 }
[9]119
[15]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 }
[9]128
[15]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 }
[9]145 }
[15]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);
[9]152
[15]153 XmlEvent(to_parse);
[9]154
[15]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 }
[9]163 }
[15]164 }
[9]165}
166
167void XmlWidget::LoadXml(QDomElement to_parse) {
[15]168 if (to_parse.isNull())
169 return;
[9]170
[15]171 LoadEvent(to_parse);
[9]172
[15]173 QDomElement elem = to_parse.firstChild().toElement();
[9]174
[15]175 while (!elem.isNull()) {
[9]176
[15]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);
[9]183
[15]184 if (match != NULL) {
185 // printf("match\n");
186 match->LoadXml(elem);
[9]187 }
[15]188 elem = elem.nextSibling().toElement();
189 }
[9]190}
191
[15]192void XmlWidget::GetFullXml(QDomElement *doc) {
193 QDomDocument tmp_doc = XmlDoc();
194 merge((QDomElement *)&tmp_doc, doc);
[9]195
[15]196 for (int i = 0; i < childs->count(); i++) {
197 childs->at(i)->GetFullXml(doc);
198 }
[9]199}
200
[15]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 }
[9]207
[15]208 for (int i = 0; i < childs->count(); i++) {
209 childs->at(i)->GetUpdateXml(doc);
210 }
[9]211}
212
[15]213void XmlWidget::ResetAllChilds(void) {
214 Reset();
215 for (int i = 0; i < childs->count(); i++) {
216 childs->at(i)->ResetAllChilds();
217 }
[9]218}
219
[15]220void XmlWidget::merge(QDomElement *from, QDomElement *into) {
221 QDomElement tmp_into, tmp_from;
222 tmp_from = from->firstChildElement();
[9]223
[15]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 }
[9]236
[15]237 if (match == false) {
238 into->appendChild(tmp_from.cloneNode());
239 }
[9]240
[15]241 tmp_from = tmp_from.nextSiblingElement();
242 }
[9]243}
244
[15]245QDomDocument XmlWidget::XmlDoc(void) {
246 return document.cloneNode(true).toDocument();
[9]247}
248
[15]249QDomElement *XmlWidget::AddXmlChild(QString type) {
250 QDomElement *elem;
[9]251
[15]252 elem = new QDomElement(document.createElement(type));
253 write_elem.appendChild(*elem);
[9]254
[15]255 return elem;
[9]256}
257
[15]258void XmlWidget::RemoveXmlChild(QDomElement *element) {
259 write_elem.removeChild(*element);
260 delete element;
[9]261}
262
[15]263void XmlWidget::ClearDoc(void) { document.clear(); }
[9]264
[15]265void XmlWidget::SetValue(QString value) {
266 write_elem.setAttribute("value", value);
[9]267}
268
[15]269void XmlWidget::SetAttribute(const QString &name, const QString &value) {
270 write_elem.setAttribute(name, value);
[9]271}
272
[15]273void XmlWidget::SetAttribute(const QString &name, qlonglong value) {
274 write_elem.setAttribute(name, value);
[9]275}
276
[15]277void XmlWidget::SetAttribute(const QString &name, qulonglong value) {
278 write_elem.setAttribute(name, value);
[9]279}
280
[15]281void XmlWidget::SetAttribute(const QString &name, float value) {
282 write_elem.setAttribute(name, value);
[9]283}
284
[15]285void XmlWidget::SetAttribute(const QString &name, double value) {
286 write_elem.setAttribute(name, value);
[9]287}
288
[15]289void XmlWidget::RemoveAttribute(const QString &name) {
290 write_elem.removeAttribute(name);
[9]291}
292
[67]293void XmlWidget::RemoveAllAttributes() {
294 QString name = write_elem.attribute("name");
295
296 QDomNamedNodeMap attributes=write_elem.attributes();
297 while(attributes.count()!=0) {
[68]298 //printf("%i %s\n",attributes.count(),attributes.item(0).toAttr().name().toLocal8Bit().constData());
[67]299 write_elem.removeAttribute(attributes.item(0).toAttr().name());
300 }
301 SetAttribute("name", name);
302}
303
[15]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");
[9]311}
312
[15]313ConnectionLayout *XmlWidget::connectionLayout(void) {
314 if (parent_widget != NULL) {
315 return (ConnectionLayout *)(parent_widget->connectionLayout());
316 } else {
317 return (ConnectionLayout *)this;
318 }
[9]319}
Note: See TracBrowser for help on using the repository browser.