2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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();
115 StyleManager::~StyleManager()
117 // Disconnect from signal
118 SetOrientation( Orientation() );
121 void StyleManager::SetOrientationValue( int orientation )
123 mOrientationDegrees = orientation;
125 Util::ConnectEventProcessingFinishedSignal();
126 mSetThemeConnection = true;
129 int StyleManager::GetOrientationValue()
131 return mOrientationDegrees;
134 void StyleManager::SetOrientation( Orientation orientation )
138 mOrientation.ChangedSignal().Disconnect( this, &StyleManager::OnOrientationChanged );
141 OnOrientationChanged( orientation );
145 mOrientation.ChangedSignal().Connect( this, &StyleManager::OnOrientationChanged );
149 Orientation StyleManager::GetOrientation()
154 void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value )
156 mStyleBuilderConstants[ key ] = value;
159 bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& valueOut )
161 Toolkit::PropertyValueMap::iterator valueIt = mStyleBuilderConstants.find( key );
162 if( valueIt != mStyleBuilderConstants.end() )
164 valueOut = valueIt->second;
171 void StyleManager::OnOrientationChanged( Orientation orientation )
173 mOrientation = orientation;
177 Util::ConnectEventProcessingFinishedSignal();
178 mSetThemeConnection = true;
182 Toolkit::Builder StyleManager::CreateBuilder( const PropertyValueMap& constants )
184 Toolkit::Builder builder = Toolkit::Builder::New();
185 builder.AddConstants( constants );
190 bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath )
192 std::string fileString;
193 if( LoadFile( jsonFilePath, fileString ) )
195 builder.LoadFromString( fileString );
200 DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
205 void StyleManager::CollectQualifiers( StringList& qualifiersOut )
207 // Append the relevant qualifier for orientation
208 int orientation = mOrientationDegrees;
212 orientation = mOrientation.GetDegrees();
215 switch( orientation )
220 qualifiersOut.push_back( std::string( LANDSCAPE_QUALIFIER ) );
224 case 0: // fall through
227 qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) );
233 void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const StringList& qualifiers, std::string& qualifiedStyleOut )
235 qualifiedStyleOut.append( styleName );
237 for( StringList::const_iterator it = qualifiers.begin(), itEnd = qualifiers.end(); it != itEnd; ++it )
239 const std::string& str = *it;
241 qualifiedStyleOut.append( "-" );
242 qualifiedStyleOut.append( str );
246 void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
248 // Convert control name to lower case
249 std::string styleName = control.GetTypeName();
250 std::transform( styleName.begin(), styleName.end(), styleName.begin(), ::tolower );
252 // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
253 StringList qualifiers;
254 CollectQualifiers( qualifiers );
258 std::string qualifiedStyleName;
259 BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
261 // Break if style found or we have tried the root style name (qualifiers is empty)
262 if( builder.ApplyStyle( qualifiedStyleName, control ) || qualifiers.size() == 0 )
267 // Remove the last qualifier in an attempt to find a style that is valid
268 qualifiers.pop_back();
272 void StyleManager::ApplyThemeStyle( Toolkit::Control control )
276 ApplyStyle( mThemeBuilder, control );
280 void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName )
282 bool builderReady = false;
284 // First look in the cache
285 Toolkit::Builder builder = FindCachedBuilder( jsonFileName );
292 // Merge theme and style constants
293 PropertyValueMap constants;
294 MergeMaps( mThemeBuilderConstants, mStyleBuilderConstants, constants );
297 builder = CreateBuilder( constants );
299 if( LoadJSON( builder, jsonFileName ) )
301 CacheBuilder( builder, jsonFileName );
306 // Apply the style to the control
309 builder.ApplyStyle( styleName, control );
313 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
315 DALI_ASSERT_DEBUG( 0 != filename.length());
317 std::ifstream in( filename.c_str(), std::ios::in );
320 std::stringstream buffer;
321 buffer << in.rdbuf();
323 stringOut = buffer.str();
333 Toolkit::StyleManager::ThemeChangeSignalType& StyleManager::ThemeChangeSignal()
335 return mThemeChangeSignal;
338 void StyleManager::EmitThemeChangeSignal()
340 if( !mThemeChangeSignal.Empty() )
342 mThemeChangeSignal.Emit( Toolkit::StyleManager::Get() );
346 void StyleManager::RequestThemeChange( const std::string& themeFile )
348 mThemeFile = themeFile;
350 Util::ConnectEventProcessingFinishedSignal();
351 mSetThemeConnection = true;
354 void StyleManager::RequestDefaultTheme()
356 RequestThemeChange( DEFAULT_THEME );
359 bool StyleManager::IsThemeRequestPending()
361 return mSetThemeConnection;
364 void StyleManager::SetTheme()
366 mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
367 LoadJSON( mThemeBuilder, mThemeFile );
369 mSetThemeConnection = false;
371 EmitThemeChangeSignal();
374 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
376 BuilderMap::iterator builderIt = mBuilderCache.find( key );
377 if( builderIt != mBuilderCache.end() )
379 return builderIt->second;
382 return Toolkit::Builder();
385 void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key )
387 mBuilderCache[ key ] = builder;
390 } // namespace Internal
392 } // namespace Toolkit