// Copyright (C) 2010, Guy Barrand. All rights reserved. // See the file tools.license for terms. #ifndef tools_xml_styles #define tools_xml_styles #include "../sg/style_colormap" #include "../sout" #include "../forit" #include #include namespace tools { namespace xml { class styles { public: styles(std::ostream& a_out):m_out(a_out){} virtual ~styles(){} public: styles(const styles& a_from) :m_out(a_from.m_out) ,m_named_styles(a_from.m_named_styles) ,m_aliases(a_from.m_aliases) ,m_cmaps(a_from.m_cmaps) {} styles& operator=(const styles& a_from){ m_named_styles = a_from.m_named_styles; m_aliases = a_from.m_aliases; m_cmaps = a_from.m_cmaps; return *this; } public: std::ostream& out() const {return m_out;} const sg::cmaps_t& cmaps() const {return m_cmaps;} sg::cmaps_t& cmaps() {return m_cmaps;} typedef std::pair style_item_t; typedef std::vector style_t; typedef std::pair named_style_t; const std::vector& named_styles() const {return m_named_styles;} std::vector& named_styles() {return m_named_styles;} typedef std::pair alias_t; const std::vector& aliases() const {return m_aliases;} std::vector& aliases() {return m_aliases;} const style_t* find_style(const std::string& a_name) const { tools_vforcit(named_style_t,m_named_styles,it){ if((*it).first==a_name) return &((*it).second); } return 0; } void add_style(const std::string& a_name,const style_t& a_style) { tools_vforit(named_style_t,m_named_styles,it){ if((*it).first==a_name) { //override (*it).second = a_style; return; } } m_named_styles.push_back(named_style_t(a_name,a_style)); } void append(const styles& a_from) { tools_vforcit(named_style_t,a_from.m_named_styles,it) { m_named_styles.push_back(*it); } } // for plotter : template //T = [style,text_style,line_style] bool res_sg_style(const std::string& a_style,T& a_sg_style) const { //NOTE : a_sg_style is changed according to what is found. // Then it is not fully reset by this method. const style_t* sty = find_style(a_style); if(!sty) { //could be ok to not find a plotter sub style. return false; } std::string _s; tools_vforcit(style_item_t,*sty,vit) { if(vit!=(*sty).begin()) _s += "\n"; _s += (*vit).first; _s += " "; _s += (*vit).second; } return a_sg_style.from_string(m_out,m_cmaps,_s); } typedef sg::style_colormap cmap_t; void add_colormap(const std::string& a_name,const cmap_t& a_cmap) { m_cmaps[a_name] = a_cmap; } protected: std::ostream& m_out; //styles : std::vector m_named_styles; std::vector m_aliases; //cmaps : sg::cmaps_t m_cmaps; }; }} #include "tree" #include "../vmanip" namespace tools { namespace xml { template inline void add_alias(std::vector< std::pair >& a_vec,const K& a_key,const V& a_value) { for(auto it = a_vec.begin(); it != a_vec.end(); ++it) { if((*it).first==a_key) { (*it).second = a_value; //override. return; } } //not found, add a new pair: a_vec.emplace_back(a_key, a_value); } inline bool is_plotter_style(const styles::style_t& a_sty) { tools_vforcit(style_item_t,a_sty,it) { const std::string& key = (*it).first; const std::string& sv = (*it).second; if((key=="tag")&&(sv=="plotter_style")) return true; } return false; } inline void load_style(styles& a_styles,const tree& a_tree) { std::string name; a_tree.attribute_value("name",name); if(name.empty()) { a_styles.out() << "tools::sg::gui_viewer::load_style :" << "