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();
62 if ( !handle && Adaptor::IsAvailable() )
64 Toolkit::StyleManager manager = Toolkit::StyleManager( new Internal::StyleManager() );
65 Adaptor::Get().RegisterSingleton( typeid( manager ), manager );
71 TypeRegistration STYLE_MANAGER_TYPE( typeid(Dali::Toolkit::StyleManager), typeid(Dali::BaseHandle), Create, true /* Create instance at startup */ );
74 * Merge two maps into one
76 void MergeMaps( const PropertyValueMap& a, const PropertyValueMap& b, PropertyValueMap& out )
79 for( PropertyValueMap::const_iterator it = b.begin(), itEnd = b.end(); it != itEnd; ++it )
81 out[ it->first ] = it->second;
87 Toolkit::StyleManager StyleManager::Get()
89 Toolkit::StyleManager manager;
91 if ( Adaptor::IsAvailable() )
93 // Check whether the style manager is already created
94 Dali::BaseHandle handle = Dali::Adaptor::Get().GetSingleton( typeid( Toolkit::StyleManager ) );
97 // If so, downcast the handle of singleton
98 manager = Toolkit::StyleManager( dynamic_cast< StyleManager* >( handle.GetObjectPtr() ) );
105 StyleManager::StyleManager()
106 : mOrientationDegrees( 0 ), // Portrait
107 mSetThemeConnection( false )
109 // Add theme builder constants
110 mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = DEFAULT_PACKAGE_PATH;
112 RequestDefaultTheme();
114 if( Adaptor::IsAvailable() )
116 StyleMonitor::Get().StyleChangeSignal().Connect( this, &StyleManager::StyleMonitorChange );
120 StyleManager::~StyleManager()
122 // Disconnect from signal
123 SetOrientation( Orientation() );
126 void StyleManager::SetOrientationValue( int orientation )
128 mOrientationDegrees = orientation;
130 Util::ConnectEventProcessingFinishedSignal();
131 mSetThemeConnection = true;
134 int StyleManager::GetOrientationValue()
136 return mOrientationDegrees;
139 void StyleManager::SetOrientation( Orientation orientation )
143 mOrientation.ChangedSignal().Disconnect( this, &StyleManager::OnOrientationChanged );
146 OnOrientationChanged( orientation );
150 mOrientation.ChangedSignal().Connect( this, &StyleManager::OnOrientationChanged );
154 Orientation StyleManager::GetOrientation()
159 void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value )
161 mStyleBuilderConstants[ key ] = value;
164 bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& valueOut )
166 Toolkit::PropertyValueMap::iterator valueIt = mStyleBuilderConstants.find( key );
167 if( valueIt != mStyleBuilderConstants.end() )
169 valueOut = valueIt->second;
176 void StyleManager::OnOrientationChanged( Orientation orientation )
178 mOrientation = orientation;
182 Util::ConnectEventProcessingFinishedSignal();
183 mSetThemeConnection = true;
187 Toolkit::Builder StyleManager::CreateBuilder( const PropertyValueMap& constants )
189 Toolkit::Builder builder = Toolkit::Builder::New();
190 builder.AddConstants( constants );
195 bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath )
197 std::string fileString;
198 if( LoadFile( jsonFilePath, fileString ) )
200 builder.LoadFromString( fileString );
205 DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
210 void StyleManager::CollectQualifiers( StringList& qualifiersOut )
212 // Append the relevant qualifier for orientation
213 int orientation = mOrientationDegrees;
217 orientation = mOrientation.GetDegrees();
220 switch( orientation )
225 qualifiersOut.push_back( std::string( LANDSCAPE_QUALIFIER ) );
229 case 0: // fall through
232 qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) );
238 void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const StringList& qualifiers, std::string& qualifiedStyleOut )
240 qualifiedStyleOut.append( styleName );
242 for( StringList::const_iterator it = qualifiers.begin(), itEnd = qualifiers.end(); it != itEnd; ++it )
244 const std::string& str = *it;
246 qualifiedStyleOut.append( "-" );
247 qualifiedStyleOut.append( str );
251 void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
253 // Convert control name to lower case
254 std::string styleName = control.GetTypeName();
255 std::transform( styleName.begin(), styleName.end(), styleName.begin(), ::tolower );
257 // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
258 StringList qualifiers;
259 CollectQualifiers( qualifiers );
263 std::string qualifiedStyleName;
264 BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
266 // Break if style found or we have tried the root style name (qualifiers is empty)
267 if( builder.ApplyStyle( qualifiedStyleName, control ) || qualifiers.size() == 0 )
272 // Remove the last qualifier in an attempt to find a style that is valid
273 qualifiers.pop_back();
277 void StyleManager::ApplyThemeStyle( Toolkit::Control control )
281 ApplyStyle( mThemeBuilder, control );
285 void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName )
287 bool builderReady = false;
289 // First look in the cache
290 Toolkit::Builder builder = FindCachedBuilder( jsonFileName );
297 // Merge theme and style constants
298 PropertyValueMap constants;
299 MergeMaps( mThemeBuilderConstants, mStyleBuilderConstants, constants );
302 builder = CreateBuilder( constants );
304 if( LoadJSON( builder, jsonFileName ) )
306 CacheBuilder( builder, jsonFileName );
311 // Apply the style to the control
314 builder.ApplyStyle( styleName, control );
318 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
320 DALI_ASSERT_DEBUG( 0 != filename.length());
322 std::ifstream in( filename.c_str(), std::ios::in );
325 std::stringstream buffer;
326 buffer << in.rdbuf();
328 stringOut = buffer.str();
338 Toolkit::StyleManager::StyleChangeSignalType& StyleManager::StyleChangeSignal()
340 return mStyleChangeSignal;
343 void StyleManager::RequestThemeChange( const std::string& themeFile )
345 mThemeFile = themeFile;
347 Util::ConnectEventProcessingFinishedSignal();
348 mSetThemeConnection = true;
351 void StyleManager::RequestDefaultTheme()
353 RequestThemeChange( DEFAULT_THEME );
356 bool StyleManager::IsThemeRequestPending()
358 return mSetThemeConnection;
361 void StyleManager::SetTheme()
363 mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
364 LoadJSON( mThemeBuilder, mThemeFile );
366 mSetThemeConnection = false;
369 change.themeChange = true;
370 mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), change );
373 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
375 BuilderMap::iterator builderIt = mBuilderCache.find( key );
376 if( builderIt != mBuilderCache.end() )
378 return builderIt->second;
381 return Toolkit::Builder();
384 void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key )
386 mBuilderCache[ key ] = builder;
389 void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange styleChange )
391 mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), styleChange );
394 } // namespace Internal
396 } // namespace Toolkit