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 | |
---|
10 | XmlWidget::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 | |
---|
51 | XmlWidget::~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 | |
---|
68 | QString XmlWidget::Name(void) { return write_elem.attribute("name"); } |
---|
69 | |
---|
70 | void XmlWidget::SetIsContainer(bool status) { isContainer = status; } |
---|
71 | |
---|
72 | void XmlWidget::SetIsExpandable(bool status) { isExpandable = status; } |
---|
73 | |
---|
74 | XmlWidget *XmlWidget::GetXmlWidget(QString name, QString type) { |
---|
75 | // fprintf(stderr,"recherche %s |
---|
76 | // %s\n",name.toLocal8Bit().constData(),type.toLocal8Bit().constData()); |
---|
77 | |
---|
78 | for (int i = 0; i < childs->count(); i++) { |
---|
79 | // fprintf(stderr,"child name |
---|
80 | // %s\n",childs->at(i)->write_elem.attribute("name").toLocal8Bit().constData()); |
---|
81 | // fprintf(stderr,"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 | |
---|
90 | void 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 | // fprintf(stderr,"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 | // fprintf(stderr,"not match\n"); |
---|
105 | XmlEvent(to_parse); |
---|
106 | } else { |
---|
107 | // fprintf(stderr,"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 | // fprintf(stderr,"delete flag\n"); |
---|
123 | if (match->isContainer == true && match->childs->count() != 0) { |
---|
124 | // fprintf(stderr,"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 | fprintf(stderr,"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 | fprintf(stderr,"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 | QDomElement dom=to_parse->firstChild().toElement(); |
---|
162 | match->ParseXml(&dom); |
---|
163 | } |
---|
164 | } |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | void XmlWidget::LoadXml(QDomElement *to_parse) { |
---|
169 | if (to_parse->isNull()) |
---|
170 | return; |
---|
171 | |
---|
172 | LoadEvent(to_parse); |
---|
173 | |
---|
174 | QDomElement elem = to_parse->firstChild().toElement(); |
---|
175 | |
---|
176 | while (!elem.isNull()) { |
---|
177 | |
---|
178 | QString type = elem.tagName(); |
---|
179 | QString name = elem.attribute("name"); |
---|
180 | // fprintf(stderr,"%s |
---|
181 | // %s\n",type.toLocal8Bit().constData(),name.toLocal8Bit().constData()); |
---|
182 | XmlWidget *match; |
---|
183 | match = GetXmlWidget(name, type); |
---|
184 | |
---|
185 | if (match != NULL) { |
---|
186 | // fprintf(stderr,"match\n"); |
---|
187 | match->LoadXml(&elem); |
---|
188 | } |
---|
189 | elem = elem.nextSibling().toElement(); |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | void XmlWidget::GetFullXml(QDomElement *doc) { |
---|
194 | QDomDocument tmp_doc = XmlDoc(); |
---|
195 | merge((QDomElement *)&tmp_doc, doc); |
---|
196 | |
---|
197 | for (int i = 0; i < childs->count(); i++) { |
---|
198 | childs->at(i)->GetFullXml(doc); |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | void XmlWidget::GetUpdateXml(QDomElement *doc) { |
---|
203 | if (IsUptodate() == false && isContainer == false) { |
---|
204 | SetUptodate(); |
---|
205 | QDomDocument tmp_doc = XmlDoc(); |
---|
206 | merge((QDomElement *)&tmp_doc, doc); |
---|
207 | } |
---|
208 | |
---|
209 | for (int i = 0; i < childs->count(); i++) { |
---|
210 | childs->at(i)->GetUpdateXml(doc); |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | void XmlWidget::ResetAllChilds(void) { |
---|
215 | Reset(); |
---|
216 | for (int i = 0; i < childs->count(); i++) { |
---|
217 | childs->at(i)->ResetAllChilds(); |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | void XmlWidget::merge(QDomElement *from, QDomElement *into) { |
---|
222 | QDomElement tmp_into, tmp_from; |
---|
223 | tmp_from = from->firstChildElement(); |
---|
224 | |
---|
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 | } |
---|
237 | |
---|
238 | if (match == false) { |
---|
239 | into->appendChild(tmp_from.cloneNode()); |
---|
240 | } |
---|
241 | |
---|
242 | tmp_from = tmp_from.nextSiblingElement(); |
---|
243 | } |
---|
244 | } |
---|
245 | |
---|
246 | QDomDocument XmlWidget::XmlDoc(void) { |
---|
247 | return document.cloneNode(true).toDocument(); |
---|
248 | } |
---|
249 | |
---|
250 | QDomElement *XmlWidget::AddXmlChild(QString type) { |
---|
251 | QDomElement *elem; |
---|
252 | |
---|
253 | elem = new QDomElement(document.createElement(type)); |
---|
254 | write_elem.appendChild(*elem); |
---|
255 | |
---|
256 | return elem; |
---|
257 | } |
---|
258 | |
---|
259 | void XmlWidget::RemoveXmlChild(QDomElement *element) { |
---|
260 | write_elem.removeChild(*element); |
---|
261 | delete element; |
---|
262 | } |
---|
263 | |
---|
264 | void XmlWidget::ClearDoc(void) { document.clear(); } |
---|
265 | |
---|
266 | void XmlWidget::SetValue(QString value) { |
---|
267 | write_elem.setAttribute("value", value); |
---|
268 | } |
---|
269 | |
---|
270 | void XmlWidget::SetAttribute(const QString &name, const QString &value) { |
---|
271 | write_elem.setAttribute(name, value); |
---|
272 | } |
---|
273 | |
---|
274 | void XmlWidget::SetAttribute(const QString &name, qlonglong value) { |
---|
275 | write_elem.setAttribute(name, value); |
---|
276 | } |
---|
277 | |
---|
278 | void XmlWidget::SetAttribute(const QString &name, qulonglong value) { |
---|
279 | write_elem.setAttribute(name, value); |
---|
280 | } |
---|
281 | |
---|
282 | void XmlWidget::SetAttribute(const QString &name, float value) { |
---|
283 | write_elem.setAttribute(name, value); |
---|
284 | } |
---|
285 | |
---|
286 | void XmlWidget::SetAttribute(const QString &name, double value) { |
---|
287 | write_elem.setAttribute(name, value); |
---|
288 | } |
---|
289 | |
---|
290 | void XmlWidget::RemoveAttribute(const QString &name) { |
---|
291 | write_elem.removeAttribute(name); |
---|
292 | } |
---|
293 | |
---|
294 | void XmlWidget::RemoveAllAttributes() { |
---|
295 | QString name = write_elem.attribute("name"); |
---|
296 | |
---|
297 | QDomNamedNodeMap attributes=write_elem.attributes(); |
---|
298 | while(attributes.count()!=0) { |
---|
299 | //fprintf(stderr,"%i %s\n",attributes.count(),attributes.item(0).toAttr().name().toLocal8Bit().constData()); |
---|
300 | write_elem.removeAttribute(attributes.item(0).toAttr().name()); |
---|
301 | } |
---|
302 | SetAttribute("name", name); |
---|
303 | } |
---|
304 | |
---|
305 | void 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"); |
---|
312 | } |
---|
313 | |
---|
314 | ConnectionLayout *XmlWidget::connectionLayout(void) { |
---|
315 | if (parent_widget != NULL) { |
---|
316 | return (ConnectionLayout *)(parent_widget->connectionLayout()); |
---|
317 | } else { |
---|
318 | return (ConnectionLayout *)this; |
---|
319 | } |
---|
320 | } |
---|