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

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

gcs

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