X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fstyling%2Fstyle-manager-impl.cpp;h=df885d9fc1174a2e261811c4eb4fef16a3df5b6c;hb=26d183fc49ba2b62bbd844d668f15d141efcd326;hp=1ee0e9ed315bcb01133cf3989383aa667c8aea36;hpb=2ddfbb9e23a7c3fc30e604236c41e0ef6d2ed6a2;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..df885d9 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) 2016 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,27 +18,31 @@ #include "style-manager-impl.h" // EXTERNAL INCLUDES -#include -#include -#include -#include +#include #include +#include #include +#include // INTERNAL INCLUDES +#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 = "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* APPLICATION_RESOURCE_PATH_KEY = "APPLICATION_RESOURCE_PATH"; + const char* DEFAULT_PACKAGE_PATH = DALI_DATA_READ_ONLY_DIR "/toolkit/"; } // namespace @@ -72,7 +76,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,58 +102,43 @@ 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; + mThemeBuilderConstants[ APPLICATION_RESOURCE_PATH_KEY ] = Application::GetResourcePath(); - 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::SetOrientationValue( int orientation ) +void StyleManager::ApplyTheme( const std::string& themeFile ) { - if( orientation != mOrientationDegrees ) - { - 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(); - } + SetTheme( themeFile ); } -int StyleManager::GetOrientationValue() +void StyleManager::ApplyDefaultTheme() { - return mOrientationDegrees; + SetTheme( DEFAULT_THEME ); } -void StyleManager::SetOrientation( Orientation orientation ) +const std::string& StyleManager::GetDefaultFontFamily() const { - if( mOrientation ) - { - mOrientation.ChangedSignal().Disconnect( this, &StyleManager::OnOrientationChanged ); - } - - OnOrientationChanged( orientation ); - - if( mOrientation ) - { - mOrientation.ChangedSignal().Connect( this, &StyleManager::OnOrientationChanged ); - } -} - -Orientation StyleManager::GetOrientation() -{ - return mOrientation; + return mDefaultFontFamily; } void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value ) @@ -167,198 +158,286 @@ bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& va return false; } -void StyleManager::OnOrientationChanged( Orientation orientation ) +void StyleManager::ApplyThemeStyle( Toolkit::Control control ) { - 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(); + if( !mThemeBuilder ) + { + ApplyDefaultTheme(); + } + + if( mThemeBuilder ) + { + ApplyStyle( mThemeBuilder, control ); + } } -Toolkit::Builder StyleManager::CreateBuilder( const Property::Map& constants ) +void StyleManager::ApplyThemeStyleAtInit( Toolkit::Control control ) { - Toolkit::Builder builder = Toolkit::Builder::New(); - builder.AddConstants( constants ); + ApplyThemeStyle( control ); - return builder; + if(mFeedbackStyle) + { + mFeedbackStyle->ObjectCreated( control ); + } } -bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath ) +void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName ) { - std::string fileString; - if( LoadFile( jsonFilePath, fileString ) ) + bool builderReady = false; + + // First look in the cache + Toolkit::Builder builder = FindCachedBuilder( jsonFileName ); + if( builder ) { - builder.LoadFromString( fileString ); - return true; + builderReady = true; } else { - DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str()); - return false; - } -} + // Merge theme and style constants + Property::Map constants( mThemeBuilderConstants ); + constants.Merge( mStyleBuilderConstants ); -void StyleManager::CollectQualifiers( StringList& qualifiersOut ) -{ - // Append the relevant qualifier for orientation - int orientation = mOrientationDegrees; + // Create it + builder = CreateBuilder( constants ); - if( mOrientation ) - { - orientation = mOrientation.GetDegrees(); + if( LoadJSON( builder, jsonFileName ) ) + { + CacheBuilder( builder, jsonFileName ); + builderReady = true; + } } - switch( orientation ) + // Apply the style to the control + if( builderReady ) { - 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; - } + builder.ApplyStyle( styleName, control ); } } -void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const StringList& qualifiers, std::string& qualifiedStyleOut ) +Toolkit::StyleManager::StyleChangedSignalType& StyleManager::StyleChangedSignal() { - qualifiedStyleOut.append( styleName ); - - for( StringList::const_iterator it = qualifiers.begin(), itEnd = qualifiers.end(); it != itEnd; ++it ) - { - const std::string& str = *it; + return mStyleChangedSignal; +} - qualifiedStyleOut.append( "-" ); - qualifiedStyleOut.append( str ); - } +Toolkit::StyleManager::StyleChangedSignalType& StyleManager::ControlStyleChangeSignal() +{ + return mControlStyleChangeSignal; } -void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control ) +void StyleManager::SetTheme( const std::string& themeFile ) { - // Convert control name to lower case - std::string styleName = control.GetTypeName(); - std::transform( styleName.begin(), styleName.end(), styleName.begin(), ::tolower ); + bool themeLoaded = false; - // Apply the style after choosing the correct actual style (e.g. landscape or portrait) - StringList qualifiers; - CollectQualifiers( qualifiers ); + if( mThemeFile.compare(DEFAULT_THEME) == 0 && mThemeBuilder ) + { + // We have already loaded the default theme into mThemeBuilder + } + else + { + // Reload the default theme + mThemeBuilder = CreateBuilder( mThemeBuilderConstants ); + themeLoaded = LoadJSON( mThemeBuilder, DEFAULT_THEME ); + } - while( true ) + if( themeFile.compare(DEFAULT_THEME) != 0 ) { - std::string qualifiedStyleName; - BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName ); + // The theme is different to the default: Merge it + themeLoaded = LoadJSON( mThemeBuilder, themeFile ); + mThemeFile = themeFile; + } - // Break if style found or we have tried the root style name (qualifiers is empty) - if( builder.ApplyStyle( qualifiedStyleName, control ) || qualifiers.size() == 0 ) + if( themeLoaded ) + { + if(mFeedbackStyle) { - break; + mFeedbackStyle->StyleChanged( mThemeFile, StyleChange::THEME_CHANGE ); } - // Remove the last qualifier in an attempt to find a style that is valid - qualifiers.pop_back(); + EmitStyleChangeSignals(StyleChange::THEME_CHANGE); } -} - -void StyleManager::ApplyThemeStyle( Toolkit::Control control ) -{ - if( mThemeBuilder ) + else { - ApplyStyle( mThemeBuilder, control ); + mThemeBuilder.Reset(); } } -void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName ) +const Property::Map StyleManager::GetConfigurations() { - bool builderReady = false; - - // First look in the cache - Toolkit::Builder builder = FindCachedBuilder( jsonFileName ); - if( builder ) + Property::Map result; + if( mThemeBuilder ) { - builderReady = true; + result = mThemeBuilder.GetConfigurations(); } else { - // Merge theme and style constants - Property::Map constants( mThemeBuilderConstants ); - constants.Merge( mStyleBuilderConstants ); + bool themeLoaded = false; - // Create it - builder = CreateBuilder( constants ); + mThemeBuilder = CreateBuilder( mThemeBuilderConstants ); - if( LoadJSON( builder, jsonFileName ) ) + // Load default theme because this is first try to load stylesheet. + themeLoaded = LoadJSON( mThemeBuilder, DEFAULT_THEME ); + mThemeFile = DEFAULT_THEME; + + if( themeLoaded ) { - CacheBuilder( builder, jsonFileName ); - builderReady = true; + result = mThemeBuilder.GetConfigurations(); } } - // Apply the style to the control - if( builderReady ) - { - builder.ApplyStyle( styleName, control ); - } + return result; } 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 ) + // as toolkit is platform agnostic, it cannot load files from filesystem + // ask style monitor to load the style sheet + if( mStyleMonitor ) { - std::stringstream buffer; - buffer << in.rdbuf(); + return mStyleMonitor.LoadThemeFile( filename, stringOut ); + } - stringOut = buffer.str(); + return false; +} - in.close(); +Toolkit::Builder StyleManager::CreateBuilder( const Property::Map& constants ) +{ + Toolkit::Builder builder = Toolkit::Builder::New(); + builder.AddConstants( constants ); + + return builder; +} +bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath ) +{ + std::string fileString; + if( LoadFile( jsonFilePath, fileString ) ) + { + builder.LoadFromString( fileString ); return true; } - - return false; + else + { + DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str()); + return false; + } } -Toolkit::StyleManager::StyleChangeSignalType& StyleManager::StyleChangeSignal() +static void CollectQualifiers( std::vector& qualifiersOut ) { - return mStyleChangeSignal; + // Append the relevant qualifier for orientation + // 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 + */ + + qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) ); + } -void StyleManager::RequestThemeChange( const std::string& themeFile ) +/** + * @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 ) { - mThemeFile = themeFile; + qualifiedStyleOut.append( styleName ); - // need to do style change synchronously as app might create a UI control on the next line - SetTheme(); + for( std::vector::const_iterator it = qualifiers.begin(), + itEnd = qualifiers.end(); it != itEnd; ++it ) + { + const std::string& str = *it; + + qualifiedStyleOut.append( "-" ); + qualifiedStyleOut.append( str ); + } } -void StyleManager::RequestDefaultTheme() +static bool GetStyleNameForControl( Toolkit::Builder builder, Toolkit::Control control, std::string& styleName) { - RequestThemeChange( DEFAULT_THEME ); + styleName = control.GetStyleName(); + + if( styleName.empty() ) + { + styleName = control.GetTypeName(); + } + + // Apply the style after choosing the correct actual style (e.g. landscape or portrait) + std::vector qualifiers; + CollectQualifiers( qualifiers ); + + bool found = 0; + std::string qualifiedStyleName; + do + { + qualifiedStyleName.clear(); + BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName ); + + // Break if style found or we have tried the root style name (qualifiers is empty) + 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(found) + { + styleName = qualifiedStyleName; + } + return found; } -void StyleManager::SetTheme() +void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control ) { - mThemeBuilder = CreateBuilder( mThemeBuilderConstants ); - if ( LoadJSON( mThemeBuilder, mThemeFile ) ) + std::string styleName = control.GetStyleName(); + if( GetStyleNameForControl( builder, control, styleName ) ) { - StyleChange change; - change.themeChange = true; - mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), change ); + builder.ApplyStyle( styleName, control ); } - else + + if( mDefaultFontSize >= 0 ) { - mThemeBuilder.Reset(); + // Apply the style for logical font size + std::stringstream fontSizeQualifier; + fontSizeQualifier << styleName << FONT_SIZE_QUALIFIER << mDefaultFontSize; + builder.ApplyStyle( fontSizeQualifier.str(), control ); } } +const StylePtr StyleManager::GetRecordedStyle( Toolkit::Control control ) +{ + if( mThemeBuilder ) + { + 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 ) { BuilderMap::iterator builderIt = mBuilderCache.find( key ); @@ -375,11 +454,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 ) +{ + switch ( styleChange ) + { + case StyleChange::DEFAULT_FONT_CHANGE: + { + mDefaultFontFamily = styleMonitor.GetDefaultFontFamily(); + break; + } + + case StyleChange::DEFAULT_FONT_SIZE_CHANGE: + { + mDefaultFontSize = styleMonitor.GetDefaultFontSize(); + break; + } + + case StyleChange::THEME_CHANGE: + { + SetTheme( styleMonitor.GetTheme() ); + break; + } + } + EmitStyleChangeSignals( styleChange ); +} + +void StyleManager::EmitStyleChangeSignals( StyleChange::Type styleChange ) { - mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), styleChange ); + Toolkit::StyleManager styleManager = StyleManager::Get(); + + // Update Controls first + mControlStyleChangeSignal.Emit( styleManager, styleChange ); + + // Inform application last + mStyleChangedSignal.Emit( styleManager, styleChange ); } + } // namespace Internal } // namespace Toolkit