2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "style-manager-impl.h"
21 #include <dali-toolkit/public-api/controls/control.h>
22 #include <dali-toolkit/public-api/controls/control-impl.h>
23 #include <dali-toolkit/public-api/styling/style-manager.h>
24 #include <dali-toolkit/internal/styling/util.h>
25 #include <dali/integration-api/debug.h>
36 const char* LANDSCAPE_QUALIFIER = "landscape";
37 const char* PORTRAIT_QUALIFIER = "portrait";
39 const char* DEFAULT_THEME = DALI_STYLE_DIR "tizen-default-theme.json";
41 const char* PACKAGE_PATH_KEY = "PACKAGE_PATH";
42 const char* DEFAULT_PACKAGE_PATH = DALI_DATA_READ_ONLY_DIR "/toolkit/";
60 BaseHandle handle = StyleManager::Get();
64 SingletonService singletonService( SingletonService::Get() );
65 if ( singletonService )
67 Toolkit::StyleManager manager = Toolkit::StyleManager( new Internal::StyleManager() );
68 singletonService.Register( typeid( manager ), manager );
75 TypeRegistration STYLE_MANAGER_TYPE( typeid(Dali::Toolkit::StyleManager), typeid(Dali::BaseHandle), Create, true /* Create instance at startup */ );
78 * Merge two maps into one
80 void MergeMaps( const PropertyValueMap& a, const PropertyValueMap& b, PropertyValueMap& out )
83 for( PropertyValueMap::const_iterator it = b.begin(), itEnd = b.end(); it != itEnd; ++it )
85 out[ it->first ] = it->second;
91 Toolkit::StyleManager StyleManager::Get()
93 Toolkit::StyleManager manager;
95 SingletonService singletonService( SingletonService::Get() );
96 if ( singletonService )
98 // Check whether the style manager is already created
99 Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::StyleManager ) );
102 // If so, downcast the handle of singleton
103 manager = Toolkit::StyleManager( dynamic_cast< StyleManager* >( handle.GetObjectPtr() ) );
110 StyleManager::StyleManager()
111 : mOrientationDegrees( 0 ), // Portrait
112 mSetThemeConnection( false )
114 // Add theme builder constants
115 mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = DEFAULT_PACKAGE_PATH;
117 RequestDefaultTheme();
119 if( Adaptor::IsAvailable() )
121 StyleMonitor::Get().StyleChangeSignal().Connect( this, &StyleManager::StyleMonitorChange );
125 StyleManager::~StyleManager()
127 // Disconnect from signal
128 SetOrientation( Orientation() );
131 void StyleManager::SetOrientationValue( int orientation )
133 mOrientationDegrees = orientation;
135 Util::ConnectEventProcessingFinishedSignal();
136 mSetThemeConnection = true;
139 int StyleManager::GetOrientationValue()
141 return mOrientationDegrees;
144 void StyleManager::SetOrientation( Orientation orientation )
148 mOrientation.ChangedSignal().Disconnect( this, &StyleManager::OnOrientationChanged );
151 OnOrientationChanged( orientation );
155 mOrientation.ChangedSignal().Connect( this, &StyleManager::OnOrientationChanged );
159 Orientation StyleManager::GetOrientation()
164 void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value )
166 mStyleBuilderConstants[ key ] = value;
169 bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& valueOut )
171 Toolkit::PropertyValueMap::iterator valueIt = mStyleBuilderConstants.find( key );
172 if( valueIt != mStyleBuilderConstants.end() )
174 valueOut = valueIt->second;
181 void StyleManager::OnOrientationChanged( Orientation orientation )
183 mOrientation = orientation;
187 Util::ConnectEventProcessingFinishedSignal();
188 mSetThemeConnection = true;
192 Toolkit::Builder StyleManager::CreateBuilder( const PropertyValueMap& constants )
194 Toolkit::Builder builder = Toolkit::Builder::New();
195 builder.AddConstants( constants );
200 bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath )
202 std::string fileString;
203 if( LoadFile( jsonFilePath, fileString ) )
205 builder.LoadFromString( fileString );
210 DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
215 void StyleManager::CollectQualifiers( StringList& qualifiersOut )
217 // Append the relevant qualifier for orientation
218 int orientation = mOrientationDegrees;
222 orientation = mOrientation.GetDegrees();
225 switch( orientation )
230 qualifiersOut.push_back( std::string( LANDSCAPE_QUALIFIER ) );
234 case 0: // fall through
237 qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) );
243 void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const StringList& qualifiers, std::string& qualifiedStyleOut )
245 qualifiedStyleOut.append( styleName );
247 for( StringList::const_iterator it = qualifiers.begin(), itEnd = qualifiers.end(); it != itEnd; ++it )
249 const std::string& str = *it;
251 qualifiedStyleOut.append( "-" );
252 qualifiedStyleOut.append( str );
256 void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
258 // Convert control name to lower case
259 std::string styleName = control.GetTypeName();
260 std::transform( styleName.begin(), styleName.end(), styleName.begin(), ::tolower );
262 // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
263 StringList qualifiers;
264 CollectQualifiers( qualifiers );
268 std::string qualifiedStyleName;
269 BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
271 // Break if style found or we have tried the root style name (qualifiers is empty)
272 if( builder.ApplyStyle( qualifiedStyleName, control ) || qualifiers.size() == 0 )
277 // Remove the last qualifier in an attempt to find a style that is valid
278 qualifiers.pop_back();
282 void StyleManager::ApplyThemeStyle( Toolkit::Control control )
286 ApplyStyle( mThemeBuilder, control );
290 void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName )
292 bool builderReady = false;
294 // First look in the cache
295 Toolkit::Builder builder = FindCachedBuilder( jsonFileName );
302 // Merge theme and style constants
303 PropertyValueMap constants;
304 MergeMaps( mThemeBuilderConstants, mStyleBuilderConstants, constants );
307 builder = CreateBuilder( constants );
309 if( LoadJSON( builder, jsonFileName ) )
311 CacheBuilder( builder, jsonFileName );
316 // Apply the style to the control
319 builder.ApplyStyle( styleName, control );
323 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
325 DALI_ASSERT_DEBUG( 0 != filename.length());
327 std::ifstream in( filename.c_str(), std::ios::in );
330 std::stringstream buffer;
331 buffer << in.rdbuf();
333 stringOut = buffer.str();
343 Toolkit::StyleManager::StyleChangeSignalType& StyleManager::StyleChangeSignal()
345 return mStyleChangeSignal;
348 void StyleManager::RequestThemeChange( const std::string& themeFile )
350 mThemeFile = themeFile;
352 Util::ConnectEventProcessingFinishedSignal();
353 mSetThemeConnection = true;
356 void StyleManager::RequestDefaultTheme()
358 RequestThemeChange( DEFAULT_THEME );
361 bool StyleManager::IsThemeRequestPending()
363 return mSetThemeConnection;
366 void StyleManager::SetTheme()
368 mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
369 LoadJSON( mThemeBuilder, mThemeFile );
371 mSetThemeConnection = false;
374 change.themeChange = true;
375 mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), change );
378 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
380 BuilderMap::iterator builderIt = mBuilderCache.find( key );
381 if( builderIt != mBuilderCache.end() )
383 return builderIt->second;
386 return Toolkit::Builder();
389 void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key )
391 mBuilderCache[ key ] = builder;
394 void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange styleChange )
396 mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), styleChange );
399 } // namespace Internal
401 } // namespace Toolkit