X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fstyling%2Fstyle-manager-impl.cpp;h=71baa9622bdbffe428ec61d6703307c10a05b5fe;hb=36ac338ff7e9c299ce45818d77aa8f71c63a45aa;hp=1ee0e9ed315bcb01133cf3989383aa667c8aea36;hpb=57869973578f6a0b0f836d396c7232ddb8302c6b;p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git diff --git a/dali-toolkit/internal/styling/style-manager-impl.cpp b/dali-toolkit/internal/styling/style-manager-impl.cpp index 1ee0e9e..71baa96 100644 --- a/dali-toolkit/internal/styling/style-manager-impl.cpp +++ b/dali-toolkit/internal/styling/style-manager-impl.cpp @@ -18,25 +18,25 @@ #include "style-manager-impl.h" // EXTERNAL INCLUDES -#include -#include -#include -#include +#include #include +#include #include // INTERNAL INCLUDES #include #include #include +#include namespace { const char* LANDSCAPE_QUALIFIER = "landscape"; const char* PORTRAIT_QUALIFIER = "portrait"; +const char* FONT_SIZE_QUALIFIER = "FontSize"; -const char* DEFAULT_THEME = DALI_STYLE_DIR "tizen-default-theme.json"; +const char* DEFAULT_THEME = DALI_STYLE_DIR "dali-toolkit-default-theme.json"; const char* PACKAGE_PATH_KEY = "PACKAGE_PATH"; const char* DEFAULT_PACKAGE_PATH = DALI_DATA_READ_ONLY_DIR "/toolkit/"; @@ -72,7 +72,9 @@ BaseHandle Create() return handle; } -TypeRegistration STYLE_MANAGER_TYPE( typeid(Dali::Toolkit::StyleManager), typeid(Dali::BaseHandle), Create, true /* Create instance at startup */ ); + +DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::StyleManager, Dali::BaseHandle, Create, true ) +DALI_TYPE_REGISTRATION_END() } // namespace @@ -96,83 +98,171 @@ Toolkit::StyleManager StyleManager::Get() } StyleManager::StyleManager() - : mOrientationDegrees( 0 ) // Portrait +: mDefaultFontSize( -1 ), + mDefaultFontFamily(""), + mFeedbackStyle( NULL ) { // Add theme builder constants mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = DEFAULT_PACKAGE_PATH; - RequestDefaultTheme(); - - StyleMonitor styleMonitor( StyleMonitor::Get() ); - if( styleMonitor ) + mStyleMonitor = StyleMonitor::Get(); + if( mStyleMonitor ) { - styleMonitor.StyleChangeSignal().Connect( this, &StyleManager::StyleMonitorChange ); + mStyleMonitor.StyleChangeSignal().Connect( this, &StyleManager::StyleMonitorChange ); + + mDefaultFontSize = mStyleMonitor.GetDefaultFontSize(); } + + // Sound & haptic style + mFeedbackStyle = new FeedbackStyle(); } StyleManager::~StyleManager() { + delete mFeedbackStyle; +} + +void StyleManager::ApplyTheme( const std::string& themeFile ) +{ + SetTheme( themeFile ); +} + +void StyleManager::ApplyDefaultTheme() +{ + std::string empty; + SetTheme( empty ); +} + +const std::string& StyleManager::GetDefaultFontFamily() const +{ + return mDefaultFontFamily; +} + +void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value ) +{ + mStyleBuilderConstants[ key ] = value; } -void StyleManager::SetOrientationValue( int orientation ) +bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& valueOut ) { - if( orientation != mOrientationDegrees ) + Property::Value* value = mStyleBuilderConstants.Find( key ); + if( value ) { - mOrientationDegrees = orientation; - // TODO: if orientation changed, apply the new style to all controls - // dont want to really do the whole load from file again if the bundle contains both portrait & landscape - SetTheme(); + valueOut = *value; + return true; } + + return false; } -int StyleManager::GetOrientationValue() +void StyleManager::ApplyThemeStyle( Toolkit::Control control ) { - return mOrientationDegrees; + if( !mThemeBuilder ) + { + ApplyDefaultTheme(); + } + + if( mThemeBuilder ) + { + ApplyStyle( mThemeBuilder, control ); + } } -void StyleManager::SetOrientation( Orientation orientation ) +void StyleManager::ApplyThemeStyleAtInit( Toolkit::Control control ) { - if( mOrientation ) + ApplyThemeStyle( control ); + + if(mFeedbackStyle) { - mOrientation.ChangedSignal().Disconnect( this, &StyleManager::OnOrientationChanged ); + mFeedbackStyle->ObjectCreated( control ); } +} - OnOrientationChanged( orientation ); +void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName ) +{ + bool builderReady = false; - if( mOrientation ) + // First look in the cache + Toolkit::Builder builder = FindCachedBuilder( jsonFileName ); + if( builder ) { - mOrientation.ChangedSignal().Connect( this, &StyleManager::OnOrientationChanged ); + builderReady = true; + } + else + { + // Merge theme and style constants + Property::Map constants( mThemeBuilderConstants ); + constants.Merge( mStyleBuilderConstants ); + + // Create it + builder = CreateBuilder( constants ); + + if( LoadJSON( builder, jsonFileName ) ) + { + CacheBuilder( builder, jsonFileName ); + builderReady = true; + } + } + + // Apply the style to the control + if( builderReady ) + { + builder.ApplyStyle( styleName, control ); } } -Orientation StyleManager::GetOrientation() +Toolkit::StyleManager::StyleChangedSignalType& StyleManager::StyleChangedSignal() { - return mOrientation; + return mStyleChangedSignal; } -void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value ) +Toolkit::StyleManager::StyleChangedSignalType& StyleManager::ControlStyleChangeSignal() { - mStyleBuilderConstants[ key ] = value; + return mControlStyleChangeSignal; } -bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& valueOut ) +void StyleManager::SetTheme( const std::string& themeFile ) { - Property::Value* value = mStyleBuilderConstants.Find( key ); - if( value ) + bool themeLoaded = false; + + mThemeBuilder = CreateBuilder( mThemeBuilderConstants ); + + // Always load the default theme first, then merge in the custom theme if present + themeLoaded = LoadJSON( mThemeBuilder, DEFAULT_THEME ); + + if( ! themeFile.empty() ) { - valueOut = *value; - return true; + mThemeFile = themeFile; + themeLoaded = LoadJSON( mThemeBuilder, mThemeFile ); } - return false; + if( themeLoaded ) + { + if(mFeedbackStyle) + { + mFeedbackStyle->StyleChanged( mThemeFile, StyleChange::THEME_CHANGE ); + } + + EmitStyleChangeSignals(StyleChange::THEME_CHANGE); + } + else + { + mThemeBuilder.Reset(); + } } -void StyleManager::OnOrientationChanged( Orientation orientation ) +bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut ) { - mOrientation = orientation; - // TODO: if orientation changed, apply the new style to all controls - // dont want to really do the whole load from file again if the bundle contains both portrait & landscape - SetTheme(); + DALI_ASSERT_DEBUG( 0 != filename.length()); + + // as toolkit is platform agnostic, it cannot load files from filesystem + // ask style monitor to load the style sheet + if( mStyleMonitor ) + { + return mStyleMonitor.LoadThemeFile( filename, stringOut ); + } + + return false; } Toolkit::Builder StyleManager::CreateBuilder( const Property::Map& constants ) @@ -201,13 +291,7 @@ bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFi void StyleManager::CollectQualifiers( StringList& qualifiersOut ) { // Append the relevant qualifier for orientation - int orientation = mOrientationDegrees; - - if( mOrientation ) - { - orientation = mOrientation.GetDegrees(); - } - + int orientation = 0; // Get the orientation from the system switch( orientation ) { case 90: @@ -241,9 +325,14 @@ void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control ) { - // Convert control name to lower case - std::string styleName = control.GetTypeName(); - std::transform( styleName.begin(), styleName.end(), styleName.begin(), ::tolower ); + std::string styleName = control.GetStyleName(); + + if( styleName.empty() ) + { + // Convert control name to lower case + styleName = control.GetTypeName(); + std::transform( styleName.begin(), styleName.end(), styleName.begin(), ::tolower ); + } // Apply the style after choosing the correct actual style (e.g. landscape or portrait) StringList qualifiers; @@ -263,122 +352,68 @@ void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control contro // Remove the last qualifier in an attempt to find a style that is valid qualifiers.pop_back(); } -} - -void StyleManager::ApplyThemeStyle( Toolkit::Control control ) -{ - if( mThemeBuilder ) - { - ApplyStyle( mThemeBuilder, control ); - } -} - -void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName ) -{ - bool builderReady = false; - - // First look in the cache - Toolkit::Builder builder = FindCachedBuilder( jsonFileName ); - if( builder ) - { - builderReady = true; - } - else - { - // Merge theme and style constants - Property::Map constants( mThemeBuilderConstants ); - constants.Merge( mStyleBuilderConstants ); - - // Create it - builder = CreateBuilder( constants ); - if( LoadJSON( builder, jsonFileName ) ) - { - CacheBuilder( builder, jsonFileName ); - builderReady = true; - } - } - - // Apply the style to the control - if( builderReady ) + if( mDefaultFontSize >= 0 ) { - builder.ApplyStyle( styleName, control ); + // Apply the style for logical font size + std::stringstream fontSizeQualifier; + fontSizeQualifier << styleName << FONT_SIZE_QUALIFIER << mDefaultFontSize; + builder.ApplyStyle( fontSizeQualifier.str(), control ); } } -bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut ) +Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key ) { - DALI_ASSERT_DEBUG( 0 != filename.length()); - - std::ifstream in( filename.c_str(), std::ios::in ); - if( in ) + BuilderMap::iterator builderIt = mBuilderCache.find( key ); + if( builderIt != mBuilderCache.end() ) { - std::stringstream buffer; - buffer << in.rdbuf(); - - stringOut = buffer.str(); - - in.close(); - - return true; + return builderIt->second; } - return false; + return Toolkit::Builder(); } -Toolkit::StyleManager::StyleChangeSignalType& StyleManager::StyleChangeSignal() +void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key ) { - return mStyleChangeSignal; + mBuilderCache[ key ] = builder; } -void StyleManager::RequestThemeChange( const std::string& themeFile ) +void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange::Type styleChange ) { - mThemeFile = themeFile; - - // need to do style change synchronously as app might create a UI control on the next line - SetTheme(); -} + switch ( styleChange ) + { + case StyleChange::DEFAULT_FONT_CHANGE: + { + mDefaultFontFamily = styleMonitor.GetDefaultFontFamily(); + break; + } -void StyleManager::RequestDefaultTheme() -{ - RequestThemeChange( DEFAULT_THEME ); -} + case StyleChange::DEFAULT_FONT_SIZE_CHANGE: + { + mDefaultFontSize = styleMonitor.GetDefaultFontSize(); + break; + } -void StyleManager::SetTheme() -{ - mThemeBuilder = CreateBuilder( mThemeBuilderConstants ); - if ( LoadJSON( mThemeBuilder, mThemeFile ) ) - { - StyleChange change; - change.themeChange = true; - mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), change ); - } - else - { - mThemeBuilder.Reset(); + case StyleChange::THEME_CHANGE: + { + SetTheme( styleMonitor.GetTheme() ); + break; + } } + EmitStyleChangeSignals( styleChange ); } -Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key ) +void StyleManager::EmitStyleChangeSignals( StyleChange::Type styleChange ) { - BuilderMap::iterator builderIt = mBuilderCache.find( key ); - if( builderIt != mBuilderCache.end() ) - { - return builderIt->second; - } + Toolkit::StyleManager styleManager = StyleManager::Get(); - return Toolkit::Builder(); -} + // Update Controls first + mControlStyleChangeSignal.Emit( styleManager, styleChange ); -void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key ) -{ - mBuilderCache[ key ] = builder; + // Inform application last + mStyleChangedSignal.Emit( styleManager, styleChange ); } -void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange styleChange ) -{ - mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), styleChange ); -} } // namespace Internal