osgEarth 2.1.1
|
Public Member Functions | |
StyleSheet (const Config &conf=Config()) | |
const URIContext & | uriContext () const |
void | addStyle (const Style &style) |
void | removeStyle (const std::string &name) |
Style * | getStyle (const std::string &name, bool fallBackOnDefault=true) |
const Style * | getStyle (const std::string &name, bool fallBackOnDefault=true) const |
Style * | getDefaultStyle () |
const Style * | getDefaultStyle () const |
StyleSelectorList & | selectors () |
const StyleSelectorList & | selectors () const |
void | addResourceLibrary (const std::string &name, ResourceLibrary *library) |
ResourceLibrary * | getResourceLibrary (const std::string &name) const |
const ResourceLibraryMap & | resourceLibraries () const |
virtual Config | getConfig () const |
virtual void | mergeConfig (const Config &conf) |
Protected Attributes | |
URIContext | _uriContext |
ResourceLibraryMap | _libraries |
StyleSelectorList | _selectors |
StyleMap | _styles |
Style | _emptyStyle |
Constructs a new style sheet, optionally from a Config serialization.
Definition at line 218 of file Style.cpp.
{ mergeConfig( conf ); }
void StyleSheet::addResourceLibrary | ( | const std::string & | name, |
ResourceLibrary * | library | ||
) |
void StyleSheet::addStyle | ( | const Style & | style | ) |
Config StyleSheet::getConfig | ( | ) | const [virtual] |
Definition at line 323 of file Style.cpp.
{ Config conf; for( StyleSelectorList::const_iterator i = _selectors.begin(); i != _selectors.end(); ++i ) { conf.add( "selector", i->getConfig() ); } for( StyleMap::const_iterator i = _styles.begin(); i != _styles.end(); ++i ) { conf.add( "style", i->second.getConfig() ); } return conf; }
Style * StyleSheet::getDefaultStyle | ( | ) |
Gets the default style in this sheet.
Definition at line 274 of file Style.cpp.
{ if ( _styles.find( "default" ) != _styles.end() ) { return &_styles.find( "default" )->second; } else if ( _styles.find( "" ) != _styles.end() ) { return &_styles.find( "" )->second; } if ( _styles.size() > 0 ) { return &_styles.begin()->second; } else { // insert the empty style and return it. _styles["default"] = _emptyStyle; return &_styles.begin()->second; } }
const Style * StyleSheet::getDefaultStyle | ( | ) | const |
ResourceLibrary * StyleSheet::getResourceLibrary | ( | const std::string & | name | ) | const |
Gets a resource library by name.
Definition at line 316 of file Style.cpp.
{ ResourceLibraryMap::const_iterator i = _libraries.find( name ); return i != _libraries.end() ? i->second.get() : 0L; }
const Style * StyleSheet::getStyle | ( | const std::string & | name, |
bool | fallBackOnDefault = true |
||
) | const |
Definition at line 255 of file Style.cpp.
{ StyleMap::const_iterator i = _styles.find( name ); if ( i != _styles.end() ) { return &i->second; } else if ( name.length() > 1 && name.at(0) == '#' ) { std::string nameWithoutHash = name.substr( 1 ); return getStyle( nameWithoutHash, fallBackOnDefault ); } else if ( fallBackOnDefault ) { return getDefaultStyle(); } else { return 0L; } }
Style * StyleSheet::getStyle | ( | const std::string & | name, |
bool | fallBackOnDefault = true |
||
) |
Gets a named style from this sheet. If the name isn't found, optionally falls back on the "default" style. Note: if the name has a hashtag prefix (e.g., "#name") it will search for the name with and without the hash. (they are considered equivalent)
Definition at line 236 of file Style.cpp.
{ StyleMap::iterator i = _styles.find( name ); if ( i != _styles.end() ) { return &i->second; } else if ( name.length() > 1 && name.at(0) == '#' ) { std::string nameWithoutHash = name.substr( 1 ); return getStyle( nameWithoutHash, fallBackOnDefault ); } else if ( fallBackOnDefault ) { return getDefaultStyle(); } else { return 0L; } }
void StyleSheet::mergeConfig | ( | const Config & | conf | ) | [virtual] |
Definition at line 338 of file Style.cpp.
{ _uriContext = conf.uriContext(); // read in any resource library references ConfigSet libraries = conf.children( "library" ); for( ConfigSet::iterator i = libraries.begin(); i != libraries.end(); ++i ) { std::string name = i->value("name"); if ( name.empty() ) { OE_WARN << LC << "Resource library missing required 'name' attribute" << std::endl; continue; } URI uri( i->value("url"), i->uriContext() ); if ( uri.empty() ) { OE_WARN << LC << "Resource library missing required 'url' element" << std::endl; continue; } osg::ref_ptr<ResourceLibrary> reslib = ResourceLibrary::create( uri ); if ( !reslib.valid() ) { OE_WARN << LC << "Resource library creation failed" << std::endl; continue; } addResourceLibrary( name, reslib.get() ); } // read any style class definitions. either "class" or "selector" is allowed ConfigSet selectors = conf.children( "selector" ); if ( selectors.empty() ) selectors = conf.children( "class" ); for( ConfigSet::iterator i = selectors.begin(); i != selectors.end(); ++i ) { _selectors.push_back( StyleSelector( *i ) ); } // read in the actual styles ConfigSet styles = conf.children( "style" ); for( ConfigSet::iterator i = styles.begin(); i != styles.end(); ++i ) { const Config& styleConf = *i; if ( styleConf.value("type") == "text/css" ) { // read the inline data: std::string cssString = styleConf.value(); // if there's a URL, read the CSS from the URL: if ( styleConf.hasValue("url") ) { URI uri( styleConf.value("url"), styleConf.uriContext() ); HTTPClient::readString( uri.full(), cssString ); } // a CSS style definition can actually contain multiple styles. Read them // and create one style for each in the catalog. std::stringstream buf( cssString ); Config css = CssUtils::readConfig( buf ); css.setURIContext( styleConf.uriContext( ) ); const ConfigSet children = css.children(); for(ConfigSet::const_iterator j = children.begin(); j != children.end(); ++j ) { Style style( styleConf ); if ( SLDReader::readStyleFromCSSParams( *j, style ) ) _styles[ j->key() ] = style; } } else { Style style( styleConf ); _styles[ style.getName() ] = style; } } }
void StyleSheet::removeStyle | ( | const std::string & | name | ) |
const ResourceLibraryMap& osgEarth::Symbology::StyleSheet::resourceLibraries | ( | ) | const [inline] |
StyleSelectorList& osgEarth::Symbology::StyleSheet::selectors | ( | ) | [inline] |
Selectors pick a style from the sheet based on some criteria.
Definition at line 210 of file Style.
{ return _selectors; }
const StyleSelectorList& osgEarth::Symbology::StyleSheet::selectors | ( | ) | const [inline] |
Definition at line 211 of file Style.
{ return _selectors; }
const URIContext& osgEarth::Symbology::StyleSheet::uriContext | ( | ) | const [inline] |
Gets the context for relative path resolution
Definition at line 191 of file Style.
{ return _uriContext; }
Style osgEarth::Symbology::StyleSheet::_emptyStyle [protected] |
StyleMap osgEarth::Symbology::StyleSheet::_styles [protected] |