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

Last change on this file since 313 was 269, checked in by Sanahuja Guillaume, 6 years ago

flairgcs:
speed up processing time when receiving datas from uav
triger watchdog while receiving datas from uav
(avoids connection lost in uav)

File size: 8.9 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) {
[244]75 // fprintf(stderr,"recherche %s
[15]76 // %s\n",name.toLocal8Bit().constData(),type.toLocal8Bit().constData());
[9]77
[15]78 for (int i = 0; i < childs->count(); i++) {
[244]79 // fprintf(stderr,"child name
[15]80 // %s\n",childs->at(i)->write_elem.attribute("name").toLocal8Bit().constData());
[244]81 // fprintf(stderr,"child tag
[15]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
[269]90void XmlWidget::ParseXml(QDomElement *to_parse) {
[9]91
[269]92 if (to_parse->isNull())
[15]93 return;
[9]94
[269]95 QString type = to_parse->tagName();
96 QString name = to_parse->attribute("name");
[9]97
[244]98 // fprintf(stderr,"parse %s
[15]99 // %s\n",type.toLocal8Bit().constData(),name.toLocal8Bit().constData());
100 XmlWidget *match;
101 match = GetXmlWidget(name, type);
[9]102
[15]103 if (match == NULL) {
[244]104 // fprintf(stderr,"not match\n");
[15]105 XmlEvent(to_parse);
106 } else {
[244]107 // fprintf(stderr,"match\n");
[15]108 // si on a une balise IsEnabled, on ne traite que ca
109 if (match->visible_widget != NULL) {
[269]110 if (to_parse->attribute("IsEnabled") == "0") {
[15]111 match->visible_widget->setEnabled(false);
112 return;
113 }
[269]114 if (to_parse->attribute("IsEnabled") == "1") {
[15]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
[269]121 if (to_parse->attribute("Delete") == "1") {
[244]122 // fprintf(stderr,"delete flag\n");
[15]123 if (match->isContainer == true && match->childs->count() != 0) {
[244]124 // fprintf(stderr,"non vide
[15]125 // %s\n",match->objectName().toLocal8Bit().constData());
126 return;
127 }
[9]128
[15]129 delete match;
130 return;
131 }
132
[269]133 if (to_parse->firstChild().isNull() == true &&
[15]134 match->isExpandable == false) {
135 QString new_name;
[244]136 fprintf(stderr,"possible doublon\n");
[15]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 }
[244]149 fprintf(stderr,"new_name %s\n", new_name.toLocal8Bit().constData());
[269]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 {
[269]157 if (to_parse->firstChild().toElement().isNull()) {
[15]158 match->XmlEvent(to_parse);
159 return;
160 } else {
[269]161 QDomElement dom=to_parse->firstChild().toElement();
162 match->ParseXml(&dom);
[15]163 }
[9]164 }
[15]165 }
[9]166}
167
[269]168void XmlWidget::LoadXml(QDomElement *to_parse) {
169 if (to_parse->isNull())
[15]170 return;
[9]171
[15]172 LoadEvent(to_parse);
[9]173
[269]174 QDomElement elem = to_parse->firstChild().toElement();
[9]175
[15]176 while (!elem.isNull()) {
[9]177
[15]178 QString type = elem.tagName();
179 QString name = elem.attribute("name");
[244]180 // fprintf(stderr,"%s
[15]181 // %s\n",type.toLocal8Bit().constData(),name.toLocal8Bit().constData());
182 XmlWidget *match;
183 match = GetXmlWidget(name, type);
[9]184
[15]185 if (match != NULL) {
[244]186 // fprintf(stderr,"match\n");
[269]187 match->LoadXml(&elem);
[9]188 }
[15]189 elem = elem.nextSibling().toElement();
190 }
[9]191}
192
[15]193void XmlWidget::GetFullXml(QDomElement *doc) {
194 QDomDocument tmp_doc = XmlDoc();
195 merge((QDomElement *)&tmp_doc, doc);
[9]196
[15]197 for (int i = 0; i < childs->count(); i++) {
198 childs->at(i)->GetFullXml(doc);
199 }
[9]200}
201
[15]202void XmlWidget::GetUpdateXml(QDomElement *doc) {
203 if (IsUptodate() == false && isContainer == false) {
204 SetUptodate();
205 QDomDocument tmp_doc = XmlDoc();
206 merge((QDomElement *)&tmp_doc, doc);
207 }
[9]208
[15]209 for (int i = 0; i < childs->count(); i++) {
210 childs->at(i)->GetUpdateXml(doc);
211 }
[9]212}
213
[15]214void XmlWidget::ResetAllChilds(void) {
215 Reset();
216 for (int i = 0; i < childs->count(); i++) {
217 childs->at(i)->ResetAllChilds();
218 }
[9]219}
220
[15]221void XmlWidget::merge(QDomElement *from, QDomElement *into) {
222 QDomElement tmp_into, tmp_from;
223 tmp_from = from->firstChildElement();
[9]224
[15]225 while (tmp_from.isNull() == false) {
226 // search corresponding child
227 bool match = false;
228 tmp_into = into->firstChildElement(tmp_from.tagName());
229 while (tmp_into.isNull() == false) {
230 if (tmp_into.attribute("name") == tmp_from.attribute("name")) {
231 merge(&tmp_from, &tmp_into);
232 match = true;
233 break;
234 }
235 tmp_into = tmp_into.nextSiblingElement(tmp_from.tagName());
236 }
[9]237
[15]238 if (match == false) {
239 into->appendChild(tmp_from.cloneNode());
240 }
[9]241
[15]242 tmp_from = tmp_from.nextSiblingElement();
243 }
[9]244}
245
[15]246QDomDocument XmlWidget::XmlDoc(void) {
247 return document.cloneNode(true).toDocument();
[9]248}
249
[15]250QDomElement *XmlWidget::AddXmlChild(QString type) {
251 QDomElement *elem;
[9]252
[15]253 elem = new QDomElement(document.createElement(type));
254 write_elem.appendChild(*elem);
[9]255
[15]256 return elem;
[9]257}
258
[15]259void XmlWidget::RemoveXmlChild(QDomElement *element) {
260 write_elem.removeChild(*element);
261 delete element;
[9]262}
263
[15]264void XmlWidget::ClearDoc(void) { document.clear(); }
[9]265
[15]266void XmlWidget::SetValue(QString value) {
267 write_elem.setAttribute("value", value);
[9]268}
269
[15]270void XmlWidget::SetAttribute(const QString &name, const QString &value) {
271 write_elem.setAttribute(name, value);
[9]272}
273
[15]274void XmlWidget::SetAttribute(const QString &name, qlonglong value) {
275 write_elem.setAttribute(name, value);
[9]276}
277
[15]278void XmlWidget::SetAttribute(const QString &name, qulonglong value) {
279 write_elem.setAttribute(name, value);
[9]280}
281
[15]282void XmlWidget::SetAttribute(const QString &name, float value) {
283 write_elem.setAttribute(name, value);
[9]284}
285
[15]286void XmlWidget::SetAttribute(const QString &name, double value) {
287 write_elem.setAttribute(name, value);
[9]288}
289
[15]290void XmlWidget::RemoveAttribute(const QString &name) {
291 write_elem.removeAttribute(name);
[9]292}
293
[67]294void XmlWidget::RemoveAllAttributes() {
295 QString name = write_elem.attribute("name");
296
297 QDomNamedNodeMap attributes=write_elem.attributes();
298 while(attributes.count()!=0) {
[244]299 //fprintf(stderr,"%i %s\n",attributes.count(),attributes.item(0).toAttr().name().toLocal8Bit().constData());
[67]300 write_elem.removeAttribute(attributes.item(0).toAttr().name());
301 }
302 SetAttribute("name", name);
303}
304
[15]305void XmlWidget::RenamedFrom(QString old_name) {
306 QString name = write_elem.attribute("name");
307 SetAttribute("name", old_name);
308 SetAttribute("new_name", name);
309 connectionLayout()->XmlToSend(XmlDoc());
310 SetAttribute("name", name);
311 write_elem.removeAttribute("new_name");
[9]312}
313
[15]314ConnectionLayout *XmlWidget::connectionLayout(void) {
315 if (parent_widget != NULL) {
316 return (ConnectionLayout *)(parent_widget->connectionLayout());
317 } else {
318 return (ConnectionLayout *)this;
319 }
[9]320}
Note: See TracBrowser for help on using the repository browser.