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

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

lic

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