From d0ff1447539c28ac644a923f6f47abe886da94fe Mon Sep 17 00:00:00 2001 From: Thomas Geymayer Date: Thu, 4 Jul 2013 14:11:54 +0200 Subject: [PATCH] Canvas: Fix removing text elements. Removing text elements failed due to prefering removing the style property with the same name. Add a flag to each style indicating whether it can be inherited. The text property is only applicable to text elements and can not be inherited. Now groups can not have a text property, avoiding the name clash with the text element. --- simgear/canvas/elements/CanvasElement.cxx | 37 +++++++----- simgear/canvas/elements/CanvasElement.hxx | 74 +++++++++++++++++------ simgear/canvas/elements/CanvasGroup.cxx | 25 +++----- simgear/canvas/elements/CanvasGroup.hxx | 2 +- simgear/canvas/elements/CanvasText.cxx | 2 +- 5 files changed, 87 insertions(+), 53 deletions(-) diff --git a/simgear/canvas/elements/CanvasElement.cxx b/simgear/canvas/elements/CanvasElement.cxx index 85327b2a..8d33eff2 100644 --- a/simgear/canvas/elements/CanvasElement.cxx +++ b/simgear/canvas/elements/CanvasElement.cxx @@ -342,11 +342,10 @@ namespace canvas _transform_dirty = true; return; } - else if( StyleSetter const* setter = - getStyleSetter(child->getNameString()) ) + else if( StyleInfo const* style = getStyleInfo(child->getNameString()) ) { - setStyle(getParentStyle(child), setter); - return; + if( setStyle(getParentStyle(child), style) ) + return; } } @@ -360,7 +359,7 @@ namespace canvas if( parent == _node ) { const std::string& name = child->getNameString(); - if( StyleSetter const* setter = getStyleSetter(name) ) + if( StyleInfo const* style_info = getStyleInfo(name) ) { SGPropertyNode const* style = child; if( isStyleEmpty(child) ) @@ -368,7 +367,7 @@ namespace canvas child->clearValue(); style = getParentStyle(child); } - setStyle(style, setter); + setStyle(style, style_info); return; } else if( name == "update" ) @@ -391,9 +390,9 @@ namespace canvas //---------------------------------------------------------------------------- bool Element::setStyle( const SGPropertyNode* child, - const StyleSetter* setter ) + const StyleInfo* style_info ) { - return canApplyStyle(child) && setStyleImpl(child, setter); + return canApplyStyle(child) && setStyleImpl(child, style_info); } //---------------------------------------------------------------------------- @@ -520,7 +519,7 @@ namespace canvas if( !isInit() ) { - addStyle("clip", "", &Element::setClip); + addStyle("clip", "", &Element::setClip, false); } } @@ -543,10 +542,10 @@ namespace canvas //---------------------------------------------------------------------------- bool Element::setStyleImpl( const SGPropertyNode* child, - const StyleSetter* setter ) + const StyleInfo* style_info ) { - const StyleSetter* style_setter = setter - ? setter + const StyleSetter* style_setter = style_info + ? &style_info->setter : getStyleSetter(child->getNameString()); while( style_setter ) { @@ -558,14 +557,22 @@ namespace canvas } //---------------------------------------------------------------------------- - const Element::StyleSetter* - Element::getStyleSetter(const std::string& name) const + const Element::StyleInfo* + Element::getStyleInfo(const std::string& name) const { StyleSetters::const_iterator setter = _style_setters.find(name); if( setter == _style_setters.end() ) return 0; - return &setter->second.setter; + return &setter->second; + } + + //---------------------------------------------------------------------------- + const Element::StyleSetter* + Element::getStyleSetter(const std::string& name) const + { + const StyleInfo* info = getStyleInfo(name); + return info ? &info->setter : 0; } //---------------------------------------------------------------------------- diff --git a/simgear/canvas/elements/CanvasElement.hxx b/simgear/canvas/elements/CanvasElement.hxx index a624bb22..07e87691 100644 --- a/simgear/canvas/elements/CanvasElement.hxx +++ b/simgear/canvas/elements/CanvasElement.hxx @@ -71,6 +71,8 @@ namespace canvas { StyleSetter setter; ///!< Function(s) for setting this style std::string type; ///!< Interpolation type + bool inheritable; ///!< Whether children can inherit this style from + /// their parents }; /** @@ -118,7 +120,7 @@ namespace canvas virtual void valueChanged(SGPropertyNode * child); virtual bool setStyle( const SGPropertyNode* child, - const StyleSetter* setter = 0 ); + const StyleInfo* style_info = 0 ); /** * Set clipping shape @@ -240,7 +242,8 @@ namespace canvas StyleSetter addStyle( const std::string& name, const std::string& type, - const boost::function& setter ) + const boost::function& setter, + bool inheritable = true ) { StyleInfo& style_info = _style_setters[ name ]; if( !type.empty() ) @@ -256,6 +259,8 @@ namespace canvas style_info.type = type; } + // TODO check if changed? + style_info.inheritable = inheritable; StyleSetter* style = &style_info.setter; while( style->next ) @@ -280,9 +285,10 @@ namespace canvas StyleSetter addStyle( const std::string& name, const std::string& type, - const boost::function& setter ) + const boost::function& setter, + bool inheritable = true ) { - return addStyle(name, type, setter); + return addStyle(name, type, setter, inheritable); } template< @@ -292,13 +298,15 @@ namespace canvas StyleSetter addStyle( const std::string& name, const std::string& type, - void (Derived::*setter)(T) ) + void (Derived::*setter)(T), + bool inheritable = true ) { return addStyle ( name, type, - boost::function(setter) + boost::function(setter), + inheritable ); } @@ -310,13 +318,15 @@ namespace canvas StyleSetterFunc addStyle( const std::string& name, const std::string& type, - void (Derived::*setter)(T2) ) + void (Derived::*setter)(T2), + bool inheritable = true ) { return addStyle ( name, type, - boost::function(setter) + boost::function(setter), + inheritable ); } @@ -326,13 +336,15 @@ namespace canvas StyleSetter addStyle( const std::string& name, const std::string& type, - void (Derived::*setter)(const std::string&) ) + void (Derived::*setter)(const std::string&), + bool inheritable = true ) { return addStyle ( name, type, - boost::function(setter) + boost::function(setter), + inheritable ); } @@ -346,9 +358,16 @@ namespace canvas addStyle( const std::string& name, const std::string& type, void (Other::*setter)(T), - OtherRef Derived::*instance_ref ) + OtherRef Derived::*instance_ref, + bool inheritable = true ) { - return addStyle(name, type, bindOther(setter, instance_ref)); + return addStyle + ( + name, + type, + bindOther(setter, instance_ref), + inheritable + ); } template< @@ -362,9 +381,16 @@ namespace canvas addStyle( const std::string& name, const std::string& type, void (Other::*setter)(T2), - OtherRef Derived::*instance_ref ) + OtherRef Derived::*instance_ref, + bool inheritable = true ) { - return addStyle(name, type, bindOther(setter, instance_ref)); + return addStyle + ( + name, + type, + bindOther(setter, instance_ref), + inheritable + ); } template< @@ -378,9 +404,16 @@ namespace canvas addStyle( const std::string& name, const std::string& type, const boost::function& setter, - OtherRef Derived::*instance_ref ) + OtherRef Derived::*instance_ref, + bool inheritable = true ) { - return addStyle(name, type, bindOther(setter, instance_ref)); + return addStyle + ( + name, + type, + bindOther(setter, instance_ref), + inheritable + ); } template< @@ -392,14 +425,16 @@ namespace canvas addStyle( const std::string& name, const std::string& type, void (Other::*setter)(const std::string&), - OtherRef Derived::*instance_ref ) + OtherRef Derived::*instance_ref, + bool inheritable = true ) { return addStyle ( name, type, boost::function(setter), - instance_ref + instance_ref, + inheritable ); } @@ -445,8 +480,9 @@ namespace canvas bool isStyleEmpty(const SGPropertyNode* child) const; bool canApplyStyle(const SGPropertyNode* child) const; bool setStyleImpl( const SGPropertyNode* child, - const StyleSetter* setter = 0 ); + const StyleInfo* style_info = 0 ); + const StyleInfo* getStyleInfo(const std::string& name) const; const StyleSetter* getStyleSetter(const std::string& name) const; const SGPropertyNode* getParentStyle(const SGPropertyNode* child) const; diff --git a/simgear/canvas/elements/CanvasGroup.cxx b/simgear/canvas/elements/CanvasGroup.cxx index 7199a976..4b60ca45 100644 --- a/simgear/canvas/elements/CanvasGroup.cxx +++ b/simgear/canvas/elements/CanvasGroup.cxx @@ -175,20 +175,16 @@ namespace canvas //---------------------------------------------------------------------------- bool Group::setStyle( const SGPropertyNode* style, - const StyleSetter* setter ) + const StyleInfo* style_info ) { if( !canApplyStyle(style) ) return false; - // Don't propagate styles directly applicable to this group - if( setStyleImpl(style, setter) ) - return true; - - bool handled = false; - for(size_t i = 0; i < _transform->getNumChildren(); ++i) + bool handled = setStyleImpl(style, style_info); + if( style_info->inheritable ) { - if( getChildByIndex(i)->setStyle(style, setter) ) - handled = true; + for(size_t i = 0; i < _transform->getNumChildren(); ++i) + handled |= getChildByIndex(i)->setStyle(style, style_info); } return handled; @@ -247,12 +243,9 @@ namespace canvas return; } - if( !Element::setStyle(child) ) - { - // Only add style if not applicable to group itself + StyleInfo const* style = getStyleInfo(child->getNameString()); + if( style && style->inheritable ) _style[ child->getNameString() ] = child; - setStyle(child); - } } //---------------------------------------------------------------------------- @@ -285,9 +278,7 @@ namespace canvas } else { - Style::iterator style = _style.find(node->getNameString()); - if( style != _style.end() ) - _style.erase(style); + _style.erase( node->getNameString() ); } } diff --git a/simgear/canvas/elements/CanvasGroup.hxx b/simgear/canvas/elements/CanvasGroup.hxx index cf40e8ef..5ee417ea 100644 --- a/simgear/canvas/elements/CanvasGroup.hxx +++ b/simgear/canvas/elements/CanvasGroup.hxx @@ -93,7 +93,7 @@ namespace canvas virtual bool traverse(EventVisitor& visitor); virtual bool setStyle( const SGPropertyNode* child, - const StyleSetter* setter = 0 ); + const StyleInfo* style_info = 0 ); virtual osg::BoundingBox getTransformedBounds(const osg::Matrix& m) const; diff --git a/simgear/canvas/elements/CanvasText.cxx b/simgear/canvas/elements/CanvasText.cxx index 1ed7be49..1aca55db 100644 --- a/simgear/canvas/elements/CanvasText.cxx +++ b/simgear/canvas/elements/CanvasText.cxx @@ -292,7 +292,7 @@ namespace canvas addStyle("max-width", "numeric", &TextOSG::setMaximumWidth, text); addStyle("font", "", &Text::setFont); addStyle("alignment", "", &Text::setAlignment); - addStyle("text", "", &Text::setText); + addStyle("text", "", &Text::setText, false); } setupStyle();