osgEarth 2.1.1
Public Member Functions | Protected Attributes

osgEarth::Symbology::StyleSheet Class Reference

Inheritance diagram for osgEarth::Symbology::StyleSheet:
Collaboration diagram for osgEarth::Symbology::StyleSheet:

List of all members.

Public Member Functions

 StyleSheet (const Config &conf=Config())
const URIContexturiContext () const
void addStyle (const Style &style)
void removeStyle (const std::string &name)
StylegetStyle (const std::string &name, bool fallBackOnDefault=true)
const StylegetStyle (const std::string &name, bool fallBackOnDefault=true) const
StylegetDefaultStyle ()
const StylegetDefaultStyle () const
StyleSelectorListselectors ()
const StyleSelectorListselectors () const
void addResourceLibrary (const std::string &name, ResourceLibrary *library)
ResourceLibrarygetResourceLibrary (const std::string &name) const
const ResourceLibraryMapresourceLibraries () const
virtual Config getConfig () const
virtual void mergeConfig (const Config &conf)

Protected Attributes

URIContext _uriContext
ResourceLibraryMap _libraries
StyleSelectorList _selectors
StyleMap _styles
Style _emptyStyle

Detailed Description

A complete definition of style information.

Definition at line 184 of file Style.


Constructor & Destructor Documentation

StyleSheet::StyleSheet ( const Config conf = Config())

Constructs a new style sheet, optionally from a Config serialization.

Definition at line 218 of file Style.cpp.

{
    mergeConfig( conf );
}

Member Function Documentation

void StyleSheet::addResourceLibrary ( const std::string &  name,
ResourceLibrary library 
)

Adds a resource library.

Definition at line 310 of file Style.cpp.

{
    _libraries[name] = lib;
}
void StyleSheet::addStyle ( const Style style)

Adds a style to this sheet.

Definition at line 224 of file Style.cpp.

{
    _styles[ style.getName() ] = style;
}

Here is the call graph for this function:

Here is the caller graph for this function:

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;
}

Here is the call graph for this function:

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;
    }
}

Here is the caller graph for this function:

const Style * StyleSheet::getDefaultStyle ( ) const

Definition at line 293 of file Style.cpp.

{
    if ( _styles.size() == 1 ) {
        return &_styles.begin()->second;
    }
    else if ( _styles.find( "default" ) != _styles.end() ) {
        return &_styles.find( "default" )->second;
    }
    else if ( _styles.find( "" ) != _styles.end() ) {
        return &_styles.find( "" )->second;
    }
    else {
        return &_emptyStyle;
    }
}
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;
}

Here is the caller graph for this function:

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;
    }
}

Here is the caller graph for this function:

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;
        }
    }
}

Here is the call graph for this function:

void StyleSheet::removeStyle ( const std::string &  name)

Removes a style from this sheet.

Definition at line 230 of file Style.cpp.

{
    _styles.erase( name );
}
const ResourceLibraryMap& osgEarth::Symbology::StyleSheet::resourceLibraries ( ) const [inline]

Resource libraries in this sheet.

Definition at line 220 of file Style.

{ return _libraries; }
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; }

Here is the caller graph for this function:

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; }

Member Data Documentation

Definition at line 232 of file Style.

Definition at line 229 of file Style.

Definition at line 230 of file Style.

Definition at line 231 of file Style.

Definition at line 228 of file Style.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines