X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fstyling%2Fstyle-manager-impl.cpp;h=a5a02dd10cc212c11ccfc5494a2da3c49d09fab8;hp=bc249b3de8b35e7682c553d64a7fbe322a5783f2;hb=666b4b2f13d2f417557180a8440c6686c5dbfe83;hpb=dd846cfb29b79dcc270562a4d46de1e28f545254 diff --git a/dali-toolkit/internal/styling/style-manager-impl.cpp b/dali-toolkit/internal/styling/style-manager-impl.cpp index bc249b3..a5a02dd 100644 --- a/dali-toolkit/internal/styling/style-manager-impl.cpp +++ b/dali-toolkit/internal/styling/style-manager-impl.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Samsung Electronics Co., Ltd. + * Copyright (c) 2018 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,30 +18,37 @@ #include "style-manager-impl.h" // EXTERNAL INCLUDES -#include -#include -#include -#include +#include #include -#include +#include #include +#include // INTERNAL INCLUDES +#include +#include #include #include -#include +#include +#include namespace { -const char* LANDSCAPE_QUALIFIER = "landscape"; +//const char* LANDSCAPE_QUALIFIER = "landscape"; const char* PORTRAIT_QUALIFIER = "portrait"; -const char* FONT_SIZE_QUALIFIER = "font-size-"; +const char* FONT_SIZE_QUALIFIER = "fontsize"; -const char* DEFAULT_THEME = DALI_STYLE_DIR "tizen-default-theme.json"; +const char* DEFAULT_THEME_FILE_NAME = "dali-toolkit-default-theme.json"; const char* PACKAGE_PATH_KEY = "PACKAGE_PATH"; -const char* DEFAULT_PACKAGE_PATH = DALI_DATA_READ_ONLY_DIR "/toolkit/"; +const char* APPLICATION_RESOURCE_PATH_KEY = "APPLICATION_RESOURCE_PATH"; + +const char* DEFAULT_TOOLKIT_PACKAGE_PATH = "/toolkit/"; + +#if defined(DEBUG_ENABLED) +Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_STYLE"); +#endif } // namespace @@ -100,85 +107,225 @@ Toolkit::StyleManager StyleManager::Get() } StyleManager::StyleManager() -: mOrientationDegrees( 0 ), // Portrait - mDefaultFontSize( -1 ), - mThemeFile( DEFAULT_THEME ) +: mDefaultFontSize( -1 ), + mDefaultFontFamily(""), + mDefaultThemeFilePath(), + mFeedbackStyle( nullptr ) { // Add theme builder constants - mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = DEFAULT_PACKAGE_PATH; + const std::string dataReadOnlyDir = AssetManager::GetDaliDataReadOnlyPath(); + mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = dataReadOnlyDir + DEFAULT_TOOLKIT_PACKAGE_PATH; + mThemeBuilderConstants[ APPLICATION_RESOURCE_PATH_KEY ] = Application::GetResourcePath(); - StyleMonitor styleMonitor( StyleMonitor::Get() ); - if( styleMonitor ) + mStyleMonitor = StyleMonitor::Get(); + if( mStyleMonitor ) { - styleMonitor.StyleChangeSignal().Connect( this, &StyleManager::StyleMonitorChange ); - - mDefaultFontSize = styleMonitor.GetDefaultFontSize(); + mStyleMonitor.StyleChangeSignal().Connect( this, &StyleManager::StyleMonitorChange ); + mDefaultFontSize = mStyleMonitor.GetDefaultFontSize(); } + + // Set the full path for the default style theme. + const std::string styleDirPath = AssetManager::GetDaliStylePath(); + mDefaultThemeFilePath = styleDirPath + DEFAULT_THEME_FILE_NAME; + + // Sound & haptic style + mFeedbackStyle = new FeedbackStyle(); } StyleManager::~StyleManager() { + delete mFeedbackStyle; +} + +void StyleManager::ApplyTheme( const std::string& themeFile ) +{ + SetTheme( themeFile ); +} + +void StyleManager::ApplyDefaultTheme() +{ + SetTheme(mDefaultThemeFilePath); +} + +const std::string& StyleManager::GetDefaultFontFamily() const +{ + return mDefaultFontFamily; } -void StyleManager::SetOrientationValue( int orientation ) +void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value ) { - if( orientation != mOrientationDegrees ) + mStyleBuilderConstants[ key ] = value; +} + +bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& valueOut ) +{ + 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 ); } +} + +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 ); - OnOrientationChanged( orientation ); + if( LoadJSON( builder, jsonFileName ) ) + { + CacheBuilder( builder, jsonFileName ); + builderReady = true; + } + } - if( mOrientation ) + // Apply the style to the control + if( builderReady ) { - mOrientation.ChangedSignal().Connect( this, &StyleManager::OnOrientationChanged ); + 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; + bool loading = false; + + // If we haven't loaded a theme, or the stored theme file is empty, or + // the previously loaded theme is different to the requested theme, + // first reset the builder and load the default theme. + if( ! mThemeBuilder || mThemeFile.empty() || mThemeFile.compare( themeFile ) != 0 ) { - valueOut = *value; - return true; + loading = true; + mThemeBuilder = CreateBuilder( mThemeBuilderConstants ); + themeLoaded = LoadJSON( mThemeBuilder, mDefaultThemeFilePath ); // Sets themeLoaded to true if theme exists } - return false; + if( themeFile.compare(mDefaultThemeFilePath) != 0 ) + { + // The theme is different to the default: Merge it + loading = true; + themeLoaded |= LoadJSON( mThemeBuilder, themeFile ); + } + + if( loading ) + { + mThemeFile = themeFile; + + if( themeLoaded ) + { + // We've successfully loaded the theme file + if(mFeedbackStyle) + { + mFeedbackStyle->StyleChanged( mThemeFile, StyleChange::THEME_CHANGE ); + } + + EmitStyleChangeSignals(StyleChange::THEME_CHANGE); + } + else + { + // We tried to load a theme, but it failed. Ensure the builder is reset + mThemeBuilder.Reset(); + mThemeFile.clear(); + } + } } -void StyleManager::OnOrientationChanged( Orientation orientation ) +const Property::Map StyleManager::GetConfigurations() { - 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_LOG_STREAM( gLogFilter, Debug::Concise, "GetConfigurations()\n On entry, mThemeBuilder: " << (bool(mThemeBuilder)?"Created":"Empty") << " mThemeFile: " << mThemeFile); + + Property::Map result; + if( mThemeBuilder ) + { + result = mThemeBuilder.GetConfigurations(); + } + else + { + DALI_LOG_STREAM( gLogFilter, Debug::Concise, "GetConfigurations() Loading default theme" ); + + bool themeLoaded = false; + + mThemeBuilder = CreateBuilder( mThemeBuilderConstants ); + + // Load default theme because this is first try to load stylesheet. + themeLoaded = LoadJSON( mThemeBuilder, mDefaultThemeFilePath ); + mThemeFile = mDefaultThemeFilePath; + + if( themeLoaded ) + { + result = mThemeBuilder.GetConfigurations(); + } + DALI_LOG_STREAM( gLogFilter, Debug::Concise, " themeLoaded" << (themeLoaded?"success":"failure") ); + } + + DALI_LOG_STREAM( gLogFilter, Debug::Concise, "GetConfigurations()\n On exit, result Count: " << (result.Count() != 0) ); + DALI_LOG_STREAM( gLogFilter, Debug::Verbose, " result: " << result ); + + return result; +} + +bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut ) +{ + 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 ) @@ -204,39 +351,39 @@ bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFi } } -void StyleManager::CollectQualifiers( StringList& qualifiersOut ) +static void CollectQualifiers( std::vector& qualifiersOut ) { // Append the relevant qualifier for orientation - int orientation = mOrientationDegrees; + // int orientation = 0; // Get the orientation from the system + /* + //// To Do ///// + Getting orientation from the system, and determine Qualifie LANDSCAPE or PORTRAIT + orientation 0, 180 : PORTRAIT_QUALIFIER (default) + orientation 90, 270 : LANDSCAPE_QUALIFIER + */ - if( mOrientation ) - { - orientation = mOrientation.GetDegrees(); - } + qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) ); - switch( orientation ) - { - case 90: - case 270: - { - qualifiersOut.push_back( std::string( LANDSCAPE_QUALIFIER ) ); - break; - } - case 180: - case 0: // fall through - default: - { - qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) ); - break; - } - } } -void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const StringList& qualifiers, std::string& qualifiedStyleOut ) +/** + * @brief Construct a qualified style name out of qualifiers + * + * A qualifed style name will be in the format: style-qualifier0-qualifier1-qualifierN + * + * @param[in] styleName The root name of the style + * @param[in] qualifiers List of qualifier names + * @param[out] qualifiedStyleOut The qualified style name + */ +static void BuildQualifiedStyleName( + const std::string& styleName, + const std::vector& qualifiers, + std::string& qualifiedStyleOut ) { qualifiedStyleOut.append( styleName ); - for( StringList::const_iterator it = qualifiers.begin(), itEnd = qualifiers.end(); it != itEnd; ++it ) + for( std::vector::const_iterator it = qualifiers.begin(), + itEnd = qualifiers.end(); it != itEnd; ++it ) { const std::string& str = *it; @@ -245,136 +392,77 @@ void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const } } -void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control ) +static bool GetStyleNameForControl( Toolkit::Builder builder, Toolkit::Control control, std::string& styleName) { - std::string styleName = control.GetStyleName(); + 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; + std::vector qualifiers; CollectQualifiers( qualifiers ); - while( true ) + bool found = 0; + std::string qualifiedStyleName; + do { - std::string qualifiedStyleName; + qualifiedStyleName.clear(); BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName ); // Break if style found or we have tried the root style name (qualifiers is empty) - if( builder.ApplyStyle( qualifiedStyleName, control ) || qualifiers.size() == 0 ) + if( GetImpl(builder).LookupStyleName( qualifiedStyleName ) ) + { + found = true; + break; + } + if( qualifiers.size() == 0 ) { break; } - // Remove the last qualifier in an attempt to find a style that is valid qualifiers.pop_back(); - } + } while (!found); - if( mDefaultFontSize >= 0 ) + if(found) { - // Apply the style for logical font size - std::stringstream fontSizeQualifier; - fontSizeQualifier << styleName << "-" << FONT_SIZE_QUALIFIER << mDefaultFontSize; - builder.ApplyStyle( fontSizeQualifier.str(), control ); + styleName = qualifiedStyleName; } + return found; } -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 ) +void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control ) { - 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 ) + std::string styleName = control.GetStyleName(); + if( GetStyleNameForControl( builder, control, styleName ) ) { builder.ApplyStyle( styleName, control ); } -} -bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut ) -{ - DALI_ASSERT_DEBUG( 0 != filename.length()); - - std::ifstream in( filename.c_str(), std::ios::in ); - if( in ) + if( mDefaultFontSize >= 0 ) { - std::stringstream buffer; - buffer << in.rdbuf(); - - stringOut = buffer.str(); - - in.close(); - - return true; + // Apply the style for logical font size + std::stringstream fontSizeQualifier; + fontSizeQualifier << styleName << FONT_SIZE_QUALIFIER << mDefaultFontSize; + builder.ApplyStyle( fontSizeQualifier.str(), control ); } - - return false; -} - -Toolkit::StyleManager::StyleChangeSignalType& StyleManager::StyleChangeSignal() -{ - return mStyleChangeSignal; -} - -void StyleManager::RequestThemeChange( const std::string& themeFile ) -{ - mThemeFile = themeFile; - - // need to do style change synchronously as app might create a UI control on the next line - SetTheme(); } -void StyleManager::RequestDefaultTheme() +const StylePtr StyleManager::GetRecordedStyle( Toolkit::Control control ) { - RequestThemeChange( DEFAULT_THEME ); -} - -void StyleManager::SetTheme() -{ - mThemeBuilder = CreateBuilder( mThemeBuilderConstants ); - if ( LoadJSON( mThemeBuilder, mThemeFile ) ) - { - StyleChange change; - change.themeChange = true; - mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), change ); - } - else + if( mThemeBuilder ) { - mThemeBuilder.Reset(); + std::string styleName = control.GetStyleName(); + + if( GetStyleNameForControl( mThemeBuilder, control, styleName ) ) + { + const StylePtr style = GetImpl(mThemeBuilder).GetStyle( styleName ); + return style; + } } + return StylePtr(NULL); } Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key ) @@ -393,30 +481,43 @@ void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& ke mBuilderCache[ key ] = builder; } -void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange styleChange ) +void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange::Type styleChange ) { - if( styleChange.defaultFontSizeChange ) + switch ( styleChange ) { - mDefaultFontSize = styleMonitor.GetDefaultFontSize(); - } - - if( styleChange.themeChange ) - { - if( ! styleChange.themeFilePath.empty() ) + case StyleChange::DEFAULT_FONT_CHANGE: { - mThemeFile = styleChange.themeFilePath; + mDefaultFontFamily = styleMonitor.GetDefaultFontFamily(); + break; } - else + + case StyleChange::DEFAULT_FONT_SIZE_CHANGE: { - mThemeFile = DEFAULT_THEME; + mDefaultFontSize = styleMonitor.GetDefaultFontSize(); + break; } - SetTheme(); + case StyleChange::THEME_CHANGE: + { + SetTheme( styleMonitor.GetTheme() ); + break; + } } + EmitStyleChangeSignals( styleChange ); +} + +void StyleManager::EmitStyleChangeSignals( StyleChange::Type styleChange ) +{ + Toolkit::StyleManager styleManager = StyleManager::Get(); + + // Update Controls first + mControlStyleChangeSignal.Emit( styleManager, styleChange ); - mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), styleChange ); + // Inform application last + mStyleChangedSignal.Emit( styleManager, styleChange ); } + } // namespace Internal } // namespace Toolkit