g4tools  5.4.0
styles
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
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)
25  ,m_aliases(a_from.m_aliases)
26  ,m_cmaps(a_from.m_cmaps)
27  {}
28  styles& operator=(const styles& a_from){
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_cmaps;}
38  sg::cmaps_t& cmaps() {return m_cmaps;}
39 
40  typedef std::pair<std::string,std::string> style_item_t;
41  typedef std::vector<style_item_t> style_t;
42  typedef std::pair<std::string,style_t> named_style_t;
43 
44  const std::vector<named_style_t>& named_styles() const {return m_named_styles;}
45  std::vector<named_style_t>& named_styles() {return m_named_styles;}
46 
47  typedef std::pair<std::string,std::string> alias_t;
48  const std::vector<alias_t>& aliases() const {return m_aliases;}
49  std::vector<alias_t>& aliases() {return m_aliases;}
50 
51  const style_t* find_style(const std::string& a_name) const {
53  if((*it).first==a_name) return &((*it).second);
54  }
55  return 0;
56  }
57 
58 /*
59  style_t* find_style(const std::string& a_name) {
60  tools_vforit(named_style_t,m_named_styles,it){
61  if((*it).first==a_name) return &((*it).second);
62  }
63  return 0;
64  }
65  void remove_item_in_style(style_t& a_style,const std::string& a_item) {
66  remove<std::string,std::string>(a_style,a_item);
67  }
68 */
69 
70  void find_styles(const std::string& a_pattern,std::vector<std::string>& a_v) const {
71  a_v.clear();
73  if(match((*it).first,a_pattern)) a_v.push_back((*it).first);
74  }
75  }
76 
77  void add_style(const std::string& a_name,const style_t& a_style) {
79  if((*it).first==a_name) { //override
80  //::printf("debug : styles::add_style : override : \"%s\"\n",a_name.c_str());
81  (*it).second = a_style;
82  return;
83  }
84  }
85  //::printf("debug : styles::add_style : push_back \"%s\"\n",a_name.c_str());
86  m_named_styles.push_back(named_style_t(a_name,a_style));
87  }
88 
89  void remove_styles(const std::string& a_pattern) {
90  std::vector<named_style_t>::iterator it;
91  for(it=m_named_styles.begin();it!=m_named_styles.end();) {
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");
103  //::printf("debug : styles::append \"%s\"\n",(*it).first.c_str());
104  m_named_styles.push_back(*it);
105  }
106  }
107 
108  bool is_alias(const std::string& a_wanted,std::string& a_style) const {
109  if(find(m_aliases,a_wanted,a_style)) return true;
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_key,
117  std::string& a_v) const {
118  std::map<std::string,style_t>::const_iterator it = m_styles.find(a_style);
119  if(it==m_styles.end()) {a_v.clear();return false;}
120  const style_t& sty = (*it).second;
121  if(!find(sty,a_key,a_v)) {a_v.clear();return false;}
122  return true;
123  }
124 */
125  template <class T>
126  bool res_value(const style_t& a_sty,const std::string& a_key,T& a_v,const std::string& a_msg) const {
127  //NOTE : if ret false, we do not set a_v to something.
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_value :"
133  << " key " << sout(a_key) << " not found"
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) << " not found"
143  << " in style " << sout(a_msg) << "."
144  << " can't convert " << sout(_s) << " to value."
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,const std::string& a_key,T& a_v) const {
154  //NOTE : if ret false, we do not set a_v to something.
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_value :"
163  // << " style " << sout(style) << " not found."
164  // << std::endl;
165  return false;
166  }
167  T v;
168  if(!res_value<T>(*sty,a_key,v,style)) return false;
169  a_v = v;
170  return true;
171  }
172 
173  bool res_color(const style_t& a_sty,const std::string& a_key,colorf& a_color,const std::string& a_msg) const {
174  //NOTE : if ret false, we do not set a_color to something.
175  std::string s;
176  if(!find(a_sty,a_key,s)) {
177  if(a_msg.size()) {
178  m_out << "tools::xml::styles::res_color :"
179  << " key " << sout(a_key) << " not found"
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 not a color"
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,const std::string& a_key,colorf& a_color) const {
196  //NOTE : if ret false, we do not set a_color to something.
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_color :"
205  // << " style " << sout(style) << " not found."
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::string& a_key,bool& a_v,const std::string& a_msg) const {
214  //NOTE : if ret false, we do not set a_v to something.
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) << " not found"
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) << " not found"
229  << " in style " << sout(a_msg) << "."
230  << " can't convert " << sout(s) << " to bool."
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,const std::string& a_key,bool& a_v) const {
239  //NOTE : if ret false, we do not set a_color to something.
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_color :"
248  // << " style " << sout(style) << " not found."
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,line_style]
258  bool res_sg_style(const std::string& a_style,T& a_sg_style) const {
259  //NOTE : a_sg_style is changed according to what is found.
260  // Then it is not fully reset by this method.
261  const style_t* sty = find_style(a_style);
262  if(!sty) {
263  //could be ok to not find a plotter sub style.
264  //m_out << "tools::sg::gui_viewer::res_sg_style :"
265  // << " style " << sout(a_style) << " not found."
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_cmaps,_s);
278  }
279 
281  bool find_colormap(const std::string& a_name,cmap_t& a_cmap) const {
282  std::map<std::string,cmap_t>::const_iterator it = m_cmaps.find(a_name);
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,const cmap_t& a_cmap) {
288  m_cmaps[a_name] = a_cmap;
289  }
290  //void clear_colormaps() {m_cmaps.clear();/*add default*/}
291 
292 public:
293  static bool is_plotter_style(const style_t& a_sty) {
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")) return true;
298  }
299  return false;
300  }
301 
302 public:
303  void find_plotter_styles(std::vector<std::string>& a_v) const {
304  a_v.clear();
306  if(is_plotter_style((*it).second)) a_v.push_back((*it).first);
307  }
308  }
309 
310  void dump() {
312 
313  m_out << "/////////////////////////////////////" << std::endl;
314  m_out << "/// " << (*it).first << std::endl;
315  m_out << "/////////////////////////////////////" << std::endl;
316 
317  const style_t& sty = (*it).second;
318 
319  tools_vforcit(style_item_t,sty,vit) {
320  m_out << " " << (*vit).first << " " << (*vit).second << std::endl;
321  }
322 
323  }
324  }
325 
326  bool print_plotter_style(std::ostream& a_out,const std::string& a_style) {
327  std::string _beg = a_style+".";
328  bool found = false;
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::endl;
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 << " " << (*vit).second << std::endl;
338  }
339  }
340  }
341  return found;
342  }
343 
344  void list_plotter_styles(std::ostream& a_out) {
346  const style_t& sty = (*it).second;
347  if(!styles::is_plotter_style(sty)) continue;
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 :
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 tree& a_tree) {
369  std::string name;
370  a_tree.attribute_value("name",name);
371  if(name.empty()) {
372  a_styles.out() << "tools::sg::gui_viewer::load_style :"
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); //expect another style name.
387  if(from.empty()) {
388  a_styles.out() << "tools::sg::gui_viewer::load_style :"
389  << " <copy> without from."
390  << std::endl;
391  return;
392  }
393 
394  const styles::style_t* csty = a_styles.find_style(from);
395  if(!csty) {
396  a_styles.out() << "tools::sg::gui_viewer::load_style :"
397  << " <copy> : from " << sout(from) << " not found."
398  << std::endl;
399  return;
400  }
401 
402  append(sty,*csty);
403 
404  } else {
405  sty.push_back(styles::style_item_t(_elem->name(),_elem->value()));
406  }
407 
408  }}
409 
410  if(sty.size()) a_styles.add_style(name,sty);
411 }
412 
413 inline void load_plotter_style(styles& a_styles,const tree& a_tree) {
414  std::string pname;
415  a_tree.attribute_value("name",pname);
416  if(pname.empty()) {
417  a_styles.out() << "tools::sg::gui_viewer::load_plotter_style :"
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","plotter_style"));
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); //expect a plotter_style name.
433  if(from.empty()) {
434  a_styles.out() << "tools::sg::gui_viewer::load_plotter_style :"
435  << " <copy> without from."
436  << std::endl;
437  return;
438  }
439 
440  const styles::style_t* csty = a_styles.find_style(from);
441  if(!csty) {
442  a_styles.out() << "tools::sg::gui_viewer::load_plotter_style :"
443  << " <copy> : from " << sout(from) << " not found."
444  << std::endl;
445  return;
446  }
447  if(csty->size()) a_styles.add_style(pname,*csty);
448 
449  if(styles::is_plotter_style(*csty)) {
450  //search all <from>.<sub_style> styles :
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_style_t>& nss = a_styles.named_styles();
458  const std::string& name = (*it).first;
459  if(name.substr(0,l)==head) {
460  std::string tail = name.substr(l,name.size()-l);
461  const styles::style_t& ssty = (*it).second;
462  if(ssty.size()) sts.add_style(pname+"."+tail,ssty);
463  }
464  }
465 
466  a_styles.append(sts);
467 
468  }
469 
470 
471  } else {
472  sty.push_back(styles::style_item_t(_elem->name(),_elem->value()));
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_viewer::load_plotter_style :"
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_element()) {
498  if(_elem->name()=="copy") {
499  std::string from;
500  _elem->attribute_value("from",from); //expect another style name.
501  if(from.empty()) {
502  a_styles.out() << "tools::sg::gui_viewer::load_plotter_style : (2) :"
503  << " <copy> without from."
504  << std::endl;
505  return;
506  }
507  const styles::style_t* csty = a_styles.find_style(from);
508  if(!csty) {
509  a_styles.out() << "tools::sg::gui_viewer::load_plotter_style : (2) :"
510  << " <copy> : from " << sout(from) << " not found."
511  << std::endl;
512  return;
513  }
514  append(sty,*csty);
515  } else {
516  sty.push_back(styles::style_item_t(_elem->name(),_elem->value()));
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_viewer::load_plotter_style :"
527  << " unexpected tag " << sout(tag) << "."
528  << std::endl;
529  }
530 
531  }}
532 
533 }
534 
535 inline bool scan_style_tree(styles& a_styles,const tree& a_tree) {
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_viewer::load_style :"
547  << " <alias> without name."
548  << std::endl;
549  continue;
550  }
551  add<std::string,std::string>(a_styles.aliases(),name,_elem->value());
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
tools::xml::styles::res_value
bool res_value(const std::string &a_style, const std::string &a_key, T &a_v) const
Definition: styles:153
tools::xml::styles::res_value
bool res_value(const style_t &a_sty, const std::string &a_key, T &a_v, const std::string &a_msg) const
Definition: styles:126
tools::xml::styles
Definition: styles:17
tools::xml::element
Definition: element:26
tools::xml::styles::m_aliases
std::vector< alias_t > m_aliases
Definition: styles:355
tools::xml::styles::alias_t
std::pair< std::string, std::string > alias_t
Definition: styles:47
tools::xml::styles::is_alias
bool is_alias(const std::string &a_wanted, std::string &a_style) const
Definition: styles:108
tools::xml::styles::m_named_styles
std::vector< named_style_t > m_named_styles
Definition: styles:354
tools::xml::styles::m_cmaps
sg::cmaps_t m_cmaps
Definition: styles:357
tools::match
bool match(const std::string &a_string, const std::string &a_pattern, bool a_check_for_wilds=true)
Definition: smatch:12
tools::xml::styles::add_style
void add_style(const std::string &a_name, const style_t &a_style)
Definition: styles:77
tools::sg::find_color
bool find_color(const cmaps_t &a_cmaps, const std::string &a_s, colorf &a_col)
Definition: style_colormap:549
tools::xml::styles::style_item_t
std::pair< std::string, std::string > style_item_t
Definition: styles:40
tools::xml::styles::~styles
virtual ~styles()
Definition: styles:20
tools::xml::styles::aliases
const std::vector< alias_t > & aliases() const
Definition: styles:48
tools::xml::styles::aliases
std::vector< alias_t > & aliases()
Definition: styles:49
tools::xml::styles::styles
styles(const styles &a_from)
Definition: styles:22
tools::xml::styles::res_color
bool res_color(const std::string &a_style, const std::string &a_key, colorf &a_color) const
Definition: styles:195
tools::colorf
Definition: colorf:11
tools::xml::styles::style_t
std::vector< style_item_t > style_t
Definition: styles:41
tools::xml::styles::out
std::ostream & out() const
Definition: styles:35
tools::is_beg
bool is_beg(const std::string &a_s, const std::string &a_b, bool a_forward=true)
Definition: sbeg:11
tools::xml::styles::print_plotter_style
bool print_plotter_style(std::ostream &a_out, const std::string &a_style)
Definition: styles:326
tools::find
bool find(const std::map< K, V > &a_map, const K &a_key, V &a_value)
Definition: mapmanip:86
tools::xml::tree::attribute_value
bool attribute_value(const std::string &a_atb, std::string &a_value) const
Definition: tree:130
tools::xml::styles::res_color
bool res_color(const style_t &a_sty, const std::string &a_key, colorf &a_color, const std::string &a_msg) const
Definition: styles:173
tools::xml::styles::res_bool
bool res_bool(const style_t &a_sty, const std::string &a_key, bool &a_v, const std::string &a_msg) const
Definition: styles:213
tools::xml::styles::m_out
std::ostream & m_out
Definition: styles:352
tools::xml::styles::res_sg_style
bool res_sg_style(const std::string &a_style, T &a_sg_style) const
Definition: styles:258
tools::xml::styles::remove_styles
void remove_styles(const std::string &a_pattern)
Definition: styles:89
tools::xml::styles::add_colormap
void add_colormap(const std::string &a_name, const cmap_t &a_cmap)
Definition: styles:287
tree
tools::xml::load_plotter_style
void load_plotter_style(styles &a_styles, const tree &a_tree)
Definition: styles:413
tools::xml::looper
Definition: tree:956
tools::xml::styles::cmap_t
sg::style_colormap cmap_t
Definition: styles:280
tools::xml::looper::next_element
element * next_element()
Definition: tree:977
tools::xml::styles::dump
void dump()
Definition: styles:310
tools::xml::styles::find_plotter_styles
void find_plotter_styles(std::vector< std::string > &a_v) const
Definition: styles:303
tools::sout
Definition: sout:17
tools::append
void append(std::vector< T > &a_vec, const std::vector< T > &a_from)
Definition: vmanip:66
tools::sg::cmaps_t
std::map< std::string, style_colormap > cmaps_t
Definition: style_colormap:536
tools::sg::style_colormap
Definition: style_colormap:20
tools::xml::styles::append
void append(const styles &a_from)
Definition: styles:100
tools::xml::styles::cmaps
const sg::cmaps_t & cmaps() const
Definition: styles:37
tools::xml::tree
Definition: tree:47
tools::to
std::vector< std::string > to(int a_argc, char **a_argv)
Definition: args:507
tools_vforit
#define tools_vforit(a__T, a__v, a__it)
Definition: forit:13
tools::xml::styles::find_colormap
bool find_colormap(const std::string &a_name, cmap_t &a_cmap) const
Definition: styles:281
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::xml::styles::cmaps
sg::cmaps_t & cmaps()
Definition: styles:38
tools::xml::styles::named_styles
const std::vector< named_style_t > & named_styles() const
Definition: styles:44
tools::xml::styles::find_styles
void find_styles(const std::string &a_pattern, std::vector< std::string > &a_v) const
Definition: styles:70
tools::file::found
bool found(const std::string &a_file, const std::string &a_what, std::vector< std::string > &a_found)
Definition: file:507
tools::xml::scan_style_tree
bool scan_style_tree(styles &a_styles, const tree &a_tree)
Definition: styles:535
tools::xml::looper::next_tree
tree * next_tree()
Definition: tree:970
tools::xml::tree::tag_name
const std::string & tag_name() const
Definition: tree:128
tools::xml::styles::list_plotter_styles
void list_plotter_styles(std::ostream &a_out)
Definition: styles:344
tools::xml::styles::named_style_t
std::pair< std::string, style_t > named_style_t
Definition: styles:42
tools::xml::styles::named_styles
std::vector< named_style_t > & named_styles()
Definition: styles:45
tools::xml::styles::res_bool
bool res_bool(const std::string &a_style, const std::string &a_key, bool &a_v) const
Definition: styles:238
tools_vforcit
#define tools_vforcit(a__T, a__v, a__it)
Definition: forit:7
tools::xml::styles::styles
styles(std::ostream &a_out)
Definition: styles:19
tools::xml::styles::is_plotter_style
static bool is_plotter_style(const style_t &a_sty)
Definition: styles:293
tools::xml::styles::find_style
const style_t * find_style(const std::string &a_name) const
Definition: styles:51
tools::xml::styles::operator=
styles & operator=(const styles &a_from)
Definition: styles:28
tools::xml::load_style
void load_style(styles &a_styles, const tree &a_tree)
Definition: styles:368