Changeset 15 in flair-src for trunk/tools/FlairGCS/src/XmlWidget.cpp
- Timestamp:
- 04/08/16 15:40:57 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/FlairGCS/src/XmlWidget.cpp
r10 r15 8 8 #include <stdio.h> 9 9 10 XmlWidget::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(); 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 // printf("recherche %s 76 // %s\n",name.toLocal8Bit().constData(),type.toLocal8Bit().constData()); 77 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; 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 // printf("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 // 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 } 119 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 } 128 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 } 43 145 } 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 55 XmlWidget::~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 71 QString XmlWidget::Name(void) 72 { 73 return write_elem.attribute("name"); 74 } 75 76 void XmlWidget::SetIsContainer(bool status) { 77 isContainer=status; 78 } 79 80 void XmlWidget::SetIsExpandable(bool status) { 81 isExpandable=status; 82 } 83 84 XmlWidget* 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 99 void 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); 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); 152 153 XmlEvent(to_parse); 154 155 // return -1;//ou retourner le xml a renvoyer pour chager de nom 113 156 } 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 } 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 } 169 165 } 170 166 171 167 void 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 194 void 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 203 void 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 215 void XmlWidget::ResetAllChilds(void) 216 { 217 Reset(); 218 for(int i=0;i<childs->count();i++) 219 { 220 childs->at(i)->ResetAllChilds(); 221 } 222 } 223 224 void 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 254 QDomDocument XmlWidget::XmlDoc(void) 255 { 256 return document.cloneNode(true).toDocument(); 257 } 258 259 QDomElement* 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 269 void XmlWidget::RemoveXmlChild(QDomElement* element) 270 { 271 write_elem.removeChild(*element); 272 delete element; 273 } 274 275 void XmlWidget::ClearDoc(void) 276 { 277 document.clear(); 278 } 279 280 void XmlWidget::SetValue(QString value) 281 { 282 write_elem.setAttribute("value",value); 283 } 284 285 void XmlWidget::SetAttribute(const QString& name, const QString& value) 286 { 287 write_elem.setAttribute(name,value); 288 } 289 290 void XmlWidget::SetAttribute(const QString& name, qlonglong value) 291 { 292 write_elem.setAttribute(name,value); 293 } 294 295 void XmlWidget::SetAttribute(const QString& name, qulonglong value) 296 { 297 write_elem.setAttribute(name,value); 298 } 299 300 void XmlWidget::SetAttribute(const QString& name, float value) 301 { 302 write_elem.setAttribute(name,value); 303 } 304 305 void XmlWidget::SetAttribute(const QString& name, double value) 306 { 307 write_elem.setAttribute(name,value); 308 } 309 310 void XmlWidget::RemoveAttribute(const QString& name) { 311 write_elem.removeAttribute(name); 312 } 313 314 void 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 325 ConnectionLayout* XmlWidget::connectionLayout(void) 326 { 327 if(parent_widget!=NULL) { 328 return (ConnectionLayout*)(parent_widget->connectionLayout()); 329 } else { 330 return (ConnectionLayout*)this; 331 } 332 } 333 168 if (to_parse.isNull()) 169 return; 170 171 LoadEvent(to_parse); 172 173 QDomElement elem = to_parse.firstChild().toElement(); 174 175 while (!elem.isNull()) { 176 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); 183 184 if (match != NULL) { 185 // printf("match\n"); 186 match->LoadXml(elem); 187 } 188 elem = elem.nextSibling().toElement(); 189 } 190 } 191 192 void XmlWidget::GetFullXml(QDomElement *doc) { 193 QDomDocument tmp_doc = XmlDoc(); 194 merge((QDomElement *)&tmp_doc, doc); 195 196 for (int i = 0; i < childs->count(); i++) { 197 childs->at(i)->GetFullXml(doc); 198 } 199 } 200 201 void XmlWidget::GetUpdateXml(QDomElement *doc) { 202 if (IsUptodate() == false && isContainer == false) { 203 SetUptodate(); 204 QDomDocument tmp_doc = XmlDoc(); 205 merge((QDomElement *)&tmp_doc, doc); 206 } 207 208 for (int i = 0; i < childs->count(); i++) { 209 childs->at(i)->GetUpdateXml(doc); 210 } 211 } 212 213 void XmlWidget::ResetAllChilds(void) { 214 Reset(); 215 for (int i = 0; i < childs->count(); i++) { 216 childs->at(i)->ResetAllChilds(); 217 } 218 } 219 220 void XmlWidget::merge(QDomElement *from, QDomElement *into) { 221 QDomElement tmp_into, tmp_from; 222 tmp_from = from->firstChildElement(); 223 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 } 236 237 if (match == false) { 238 into->appendChild(tmp_from.cloneNode()); 239 } 240 241 tmp_from = tmp_from.nextSiblingElement(); 242 } 243 } 244 245 QDomDocument XmlWidget::XmlDoc(void) { 246 return document.cloneNode(true).toDocument(); 247 } 248 249 QDomElement *XmlWidget::AddXmlChild(QString type) { 250 QDomElement *elem; 251 252 elem = new QDomElement(document.createElement(type)); 253 write_elem.appendChild(*elem); 254 255 return elem; 256 } 257 258 void XmlWidget::RemoveXmlChild(QDomElement *element) { 259 write_elem.removeChild(*element); 260 delete element; 261 } 262 263 void XmlWidget::ClearDoc(void) { document.clear(); } 264 265 void XmlWidget::SetValue(QString value) { 266 write_elem.setAttribute("value", value); 267 } 268 269 void XmlWidget::SetAttribute(const QString &name, const QString &value) { 270 write_elem.setAttribute(name, value); 271 } 272 273 void XmlWidget::SetAttribute(const QString &name, qlonglong value) { 274 write_elem.setAttribute(name, value); 275 } 276 277 void XmlWidget::SetAttribute(const QString &name, qulonglong value) { 278 write_elem.setAttribute(name, value); 279 } 280 281 void XmlWidget::SetAttribute(const QString &name, float value) { 282 write_elem.setAttribute(name, value); 283 } 284 285 void XmlWidget::SetAttribute(const QString &name, double value) { 286 write_elem.setAttribute(name, value); 287 } 288 289 void XmlWidget::RemoveAttribute(const QString &name) { 290 write_elem.removeAttribute(name); 291 } 292 293 void XmlWidget::RenamedFrom(QString old_name) { 294 QString name = write_elem.attribute("name"); 295 SetAttribute("name", old_name); 296 SetAttribute("new_name", name); 297 connectionLayout()->XmlToSend(XmlDoc()); 298 SetAttribute("name", name); 299 write_elem.removeAttribute("new_name"); 300 } 301 302 ConnectionLayout *XmlWidget::connectionLayout(void) { 303 if (parent_widget != NULL) { 304 return (ConnectionLayout *)(parent_widget->connectionLayout()); 305 } else { 306 return (ConnectionLayout *)this; 307 } 308 }
Note:
See TracChangeset
for help on using the changeset viewer.