Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 2 // See the file tools.license for terms. 3 4 #ifndef tools_xml_styles 5 #define tools_xml_styles 6 7 #include "../vpair" 8 #include "../sg/style_colormap" 9 #include "../smatch" 10 #include "../sout" 11 #include "../forit" 12 #include "../sbeg" 13 14 namespace tools { 15 namespace xml { 16 17 class styles { 18 public: 19 styles(std::ostream& a_out):m_out(a_out){} 20 virtual ~styles(){} 21 public: 22 styles(const styles& a_from) 23 :m_out(a_from.m_out) 24 ,m_named_styles(a_from.m_named_styles) 25 ,m_aliases(a_from.m_aliases) 26 ,m_cmaps(a_from.m_cmaps) 27 {} 28 styles& operator=(const styles& a_from){ 29 m_named_styles = a_from.m_named_styles; 30 m_aliases = a_from.m_aliases; 31 m_cmaps = a_from.m_cmaps; 32 return *this; 33 } 34 public: 35 std::ostream& out() const {return m_out;} 36 37 const sg::cmaps_t& cmaps() const {return m_c 38 sg::cmaps_t& cmaps() {return m_cmaps;} 39 40 typedef std::pair<std::string,std::string> s 41 typedef std::vector<style_item_t> style_t; 42 typedef std::pair<std::string,style_t> named 43 44 const std::vector<named_style_t>& named_styl 45 std::vector<named_style_t>& named_styles() { 46 47 typedef std::pair<std::string,std::string> a 48 const std::vector<alias_t>& aliases() const 49 std::vector<alias_t>& aliases() {return m_al 50 51 const style_t* find_style(const std::string& 52 tools_vforcit(named_style_t,m_named_styles 53 if((*it).first==a_name) return &((*it).s 54 } 55 return 0; 56 } 57 58 /* 59 style_t* find_style(const std::string& a_nam 60 tools_vforit(named_style_t,m_named_styles, 61 if((*it).first==a_name) return &((*it).s 62 } 63 return 0; 64 } 65 void remove_item_in_style(style_t& a_style,c 66 remove<std::string,std::string>(a_style,a_ 67 } 68 */ 69 70 void find_styles(const std::string& a_patter 71 a_v.clear(); 72 tools_vforcit(named_style_t,m_named_styles 73 if(match((*it).first,a_pattern)) a_v.pus 74 } 75 } 76 77 void add_style(const std::string& a_name,con 78 tools_vforit(named_style_t,m_named_styles, 79 if((*it).first==a_name) { //override 80 //::printf("debug : styles::add_style 81 (*it).second = a_style; 82 return; 83 } 84 } 85 //::printf("debug : styles::add_style : pu 86 m_named_styles.push_back(named_style_t(a_n 87 } 88 89 void remove_styles(const std::string& a_patt 90 std::vector<named_style_t>::iterator it; 91 for(it=m_named_styles.begin();it!=m_named_ 92 if(match((*it).first,a_pattern)) { 93 it = m_named_styles.erase(it); 94 } else { 95 it++; 96 } 97 } 98 } 99 100 void append(const styles& a_from) { 101 //::printf("debug : styles::append ...\n") 102 tools_vforcit(named_style_t,a_from.m_named 103 //::printf("debug : styles::append \"%s\ 104 m_named_styles.push_back(*it); 105 } 106 } 107 108 bool is_alias(const std::string& a_wanted,st 109 if(find(m_aliases,a_wanted,a_style)) retur 110 a_style = a_wanted; 111 return false; 112 } 113 114 /* 115 bool res_string(const std::string& a_style, 116 const std::string& a_ 117 std::string& a_v) con 118 std::map<std::string,style_t>::const_itera 119 if(it==m_styles.end()) {a_v.clear();return 120 const style_t& sty = (*it).second; 121 if(!find(sty,a_key,a_v)) {a_v.clear();retu 122 return true; 123 } 124 */ 125 template <class T> 126 bool res_value(const style_t& a_sty,const st 127 //NOTE : if ret false, we do not set a_v t 128 129 std::string _s; 130 if(!find(a_sty,a_key,_s)) { 131 if(a_msg.size()) { 132 m_out << "tools::xml::styles::res_valu 133 << " key " << sout(a_key) << " n 134 << " in style " << sout(a_msg) < 135 << std::endl; 136 } 137 return false; 138 } 139 T v; 140 if(!to(_s,v)) { 141 m_out << "tools::xml::styles::res_value 142 << " for key " << sout(a_key) << " 143 << " in style " << sout(a_msg) << 144 << " can't convert " << sout(_s) < 145 << std::endl; 146 return false; 147 } 148 a_v = v; 149 return true; 150 } 151 152 template <class T> 153 bool res_value(const std::string& a_style,co 154 //NOTE : if ret false, we do not set a_v t 155 156 //look if a_style is an alias : 157 std::string style; 158 is_alias(a_style,style); 159 160 const style_t* sty = find_style(style); 161 if(!sty) { 162 //m_out << "tools::xml::styles::res_valu 163 // << " style " << sout(style) << " 164 // << std::endl; 165 return false; 166 } 167 T v; 168 if(!res_value<T>(*sty,a_key,v,style)) retu 169 a_v = v; 170 return true; 171 } 172 173 bool res_color(const style_t& a_sty,const st 174 //NOTE : if ret false, we do not set a_col 175 std::string _s; 176 if(!find(a_sty,a_key,_s)) { 177 if(a_msg.size()) { 178 m_out << "tools::xml::styles::res_colo 179 << " key " << sout(a_key) << " n 180 << " in style " << sout(a_msg) < 181 << std::endl; 182 } 183 return false; 184 } 185 if(!sg::find_color(m_cmaps,_s,a_color)) { 186 m_out << "tools::xml::styles::res_color 187 << " key " << sout(a_key) << " is 188 << " in style " << sout(a_msg) << 189 << std::endl; 190 return false; 191 } 192 return true; 193 } 194 195 bool res_color(const std::string& a_style,co 196 //NOTE : if ret false, we do not set a_col 197 198 //look if a_style is an alias : 199 std::string style; 200 is_alias(a_style,style); 201 202 const style_t* sty = find_style(style); 203 if(!sty) { 204 //m_out << "tools::xml::styles::res_colo 205 // << " style " << sout(style) << " 206 // << std::endl; 207 return false; 208 } 209 210 return res_color(*sty,a_key,a_color,style) 211 } 212 213 bool res_bool(const style_t& a_sty,const std 214 //NOTE : if ret false, we do not set a_v t 215 std::string _s; 216 if(!find(a_sty,a_key,_s)) { 217 if(a_msg.size()) { 218 m_out << "tools::xml::styles::res_bool 219 << " key " << sout(a_key) << " n 220 << " in style " << sout(a_msg) < 221 << std::endl; 222 } 223 return false; 224 } 225 bool v; 226 if(!to(_s,v)) { 227 m_out << "tools::xml::styles::res_bool : 228 << " for key " << sout(a_key) << " 229 << " in style " << sout(a_msg) << 230 << " can't convert " << sout(_s) < 231 << std::endl; 232 return false; 233 } 234 a_v = v; 235 return true; 236 } 237 238 bool res_bool(const std::string& a_style,con 239 //NOTE : if ret false, we do not set a_col 240 241 //look if a_style is an alias : 242 std::string style; 243 is_alias(a_style,style); 244 245 const style_t* sty = find_style(style); 246 if(!sty) { 247 //m_out << "tools::xml::styles::res_colo 248 // << " style " << sout(style) << " 249 // << std::endl; 250 return false; 251 } 252 253 return res_bool(*sty,a_key,a_v,style); 254 } 255 256 // for plotter : 257 template <class T> //T = [style,text_style,l 258 bool res_sg_style(const std::string& a_style 259 //NOTE : a_sg_style is changed according t 260 // Then it is not fully reset by thi 261 const style_t* sty = find_style(a_style); 262 if(!sty) { 263 //could be ok to not find a plotter sub 264 //m_out << "tools::sg::gui_viewer::res_s 265 // << " style " << sout(a_style) << 266 // << std::endl; 267 return false; 268 } 269 270 std::string _s; 271 tools_vforcit(style_item_t,*sty,vit) { 272 if(vit!=(*sty).begin()) _s += "\n"; 273 _s += (*vit).first; 274 _s += " "; 275 _s += (*vit).second; 276 } 277 return a_sg_style.from_string(m_out,m_cmap 278 } 279 280 typedef sg::style_colormap cmap_t; 281 bool find_colormap(const std::string& a_name 282 std::map<std::string,cmap_t>::const_iterat 283 if(it==m_cmaps.end()) return false; 284 a_cmap = (*it).second; 285 return true; 286 } 287 void add_colormap(const std::string& a_name, 288 m_cmaps[a_name] = a_cmap; 289 } 290 //void clear_colormaps() {m_cmaps.clear();/* 291 292 public: 293 static bool is_plotter_style(const style_t& 294 tools_vforcit(style_item_t,a_sty,it) { 295 const std::string& key = (*it).first; 296 const std::string& sv = (*it).second; 297 if((key=="tag")&&(sv=="plotter_style")) 298 } 299 return false; 300 } 301 302 public: 303 void find_plotter_styles(std::vector<std::st 304 a_v.clear(); 305 tools_vforcit(named_style_t,m_named_styles 306 if(is_plotter_style((*it).second)) a_v.p 307 } 308 } 309 310 void dump() { 311 tools_vforcit(named_style_t,m_named_styles 312 313 m_out << "////////////////////////////// 314 m_out << "/// " << (*it).first << std::e 315 m_out << "////////////////////////////// 316 317 const style_t& sty = (*it).second; 318 319 tools_vforcit(style_item_t,sty,vit) { 320 m_out << " " << (*vit).first << " " << 321 } 322 323 } 324 } 325 326 bool print_plotter_style(std::ostream& a_out 327 std::string _beg = a_style+"."; 328 bool found = false; 329 tools_vforcit(named_style_t,m_named_styles 330 const std::string& _name = (*it).first; 331 if((_name==a_style)||is_beg(_name,_beg)) 332 found = true; 333 a_out << (*it).first << " :" << std::e 334 const style_t& sty = (*it).second; 335 tools_vforcit(style_item_t,sty,vit) { 336 if((*vit).first=="tag") continue; 337 a_out << " " << (*vit).first << " " 338 } 339 } 340 } 341 return found; 342 } 343 344 void list_plotter_styles(std::ostream& a_out 345 tools_vforcit(named_style_t,m_named_styles 346 const style_t& sty = (*it).second; 347 if(!styles::is_plotter_style(sty)) conti 348 a_out << (*it).first << std::endl; 349 } 350 } 351 protected: 352 std::ostream& m_out; 353 //styles : 354 std::vector<named_style_t> m_named_styles; 355 std::vector<alias_t> m_aliases; 356 //cmaps : 357 sg::cmaps_t m_cmaps; 358 }; 359 360 }} 361 362 #include "tree" 363 #include "../vmanip" 364 365 namespace tools { 366 namespace xml { 367 368 inline void load_style(styles& a_styles,const 369 std::string name; 370 a_tree.attribute_value("name",name); 371 if(name.empty()) { 372 a_styles.out() << "tools::sg::gui_viewer:: 373 << " <style> without name." 374 << std::endl; 375 return; 376 } 377 378 styles::style_t sty; 379 380 {looper _for(a_tree); 381 while(element* _elem = _for.next_element()) 382 383 if(_elem->name()=="copy") { 384 385 std::string from; 386 _elem->attribute_value("from",from); / 387 if(from.empty()) { 388 a_styles.out() << "tools::sg::gui_vi 389 << " <copy> without f 390 << std::endl; 391 return; 392 } 393 394 const styles::style_t* csty = a_styles 395 if(!csty) { 396 a_styles.out() << "tools::sg::gui_vi 397 << " <copy> : from " << s 398 << std::endl; 399 return; 400 } 401 402 append(sty,*csty); 403 404 } else { 405 sty.push_back(styles::style_item_t(_el 406 } 407 408 }} 409 410 if(sty.size()) a_styles.add_style(name,sty); 411 } 412 413 inline void load_plotter_style(styles& a_style 414 std::string pname; 415 a_tree.attribute_value("name",pname); 416 if(pname.empty()) { 417 a_styles.out() << "tools::sg::gui_viewer:: 418 << " <plotter_style> without name." 419 << std::endl; 420 return; 421 } 422 423 {styles::style_t sty; 424 sty.push_back(styles::style_item_t("tag","pl 425 426 {looper _for(a_tree); 427 while(element* _elem = _for.next_element()) 428 429 if(_elem->name()=="copy") { 430 431 std::string from; 432 _elem->attribute_value("from",from); / 433 if(from.empty()) { 434 a_styles.out() << "tools::sg::gui_vi 435 << " <copy> without from." 436 << std::endl; 437 return; 438 } 439 440 const styles::style_t* csty = a_styles 441 if(!csty) { 442 a_styles.out() << "tools::sg::gui_vi 443 << " <copy> : from " << sout(f 444 << std::endl; 445 return; 446 } 447 if(csty->size()) a_styles.add_style(pn 448 449 if(styles::is_plotter_style(*csty)) { 450 //search all <from>.<sub_style> styl 451 std::string head = from+"."; 452 std::string::size_type l = head.size 453 454 styles sts(a_styles.out()); 455 456 const std::vector<styles::named_styl 457 tools_vforcit(styles::named_style_t, 458 const std::string& name = (*it).fi 459 if(name.substr(0,l)==head) { 460 std::string tail = name.substr(l 461 const styles::style_t& ssty = (* 462 if(ssty.size()) sts.add_style(pn 463 } 464 } 465 466 a_styles.append(sts); 467 468 } 469 470 471 } else { 472 sty.push_back(styles::style_item_t(_el 473 } 474 475 }} 476 477 if(sty.size()) a_styles.add_style(pname,sty) 478 479 {looper _for(a_tree); 480 while(tree* _tree = _for.next_tree()) { 481 482 const std::string& tag = _tree->tag_name 483 if(tag=="style") { 484 485 std::string name; 486 _tree->attribute_value("name",name); 487 if(name.empty()) { 488 a_styles.out() << "tools::sg::gui_vi 489 << " <style> without name." 490 << std::endl; 491 return; 492 } 493 494 {styles::style_t sty; 495 496 {looper _for2(*_tree); 497 while(element* _elem = _for2.next_elem 498 if(_elem->name()=="copy") { 499 std::string from; 500 _elem->attribute_value("from",from 501 if(from.empty()) { 502 a_styles.out() << "tools::sg::gu 503 << " <copy> witho 504 << std::endl; 505 return; 506 } 507 const styles::style_t* csty = a_st 508 if(!csty) { 509 a_styles.out() << "tools::sg::gu 510 << " <copy> : fro 511 << std::endl; 512 return; 513 } 514 append(sty,*csty); 515 } else { 516 sty.push_back(styles::style_item_t 517 } 518 }} 519 520 if(sty.size()) { 521 std::string path = pname+"."+name; 522 a_styles.add_style(path,sty); 523 }} 524 525 } else { 526 a_styles.out() << "tools::sg::gui_view 527 << " unexpected tag " << sout(ta 528 << std::endl; 529 } 530 531 }} 532 533 } 534 535 inline bool scan_style_tree(styles& a_styles,c 536 537 if(a_tree.tag_name()!="styles") return false 538 539 // look for aliases : 540 {looper _for(a_tree); 541 while(element* _elem = _for.next_element()) 542 543 std::string name; 544 _elem->attribute_value("name",name); 545 if(name.empty()) { 546 a_styles.out() << "tools::sg::gui_view 547 << " <alias> without name." 548 << std::endl; 549 continue; 550 } 551 add<std::string,std::string>(a_styles.al 552 }} 553 554 // scan children : 555 {looper _for(a_tree); 556 while(tree* _tree = _for.next_tree()) { 557 558 const std::string& tag = _tree->tag_name 559 if(tag=="style") { 560 load_style(a_styles,*_tree); 561 } else if(tag=="plotter_style") { 562 load_plotter_style(a_styles,*_tree); 563 } 564 565 }} 566 567 return true; 568 } 569 }} 570 571 #endif