0e43c192ce4a182111a3ef6b2cc91c4926b8937a
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / styling / style-manager-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 // CLASS HEADER
18 #include "style-manager-impl.h"
19
20 // INTERNAL INCLUDES
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/integration-api/debug.h>
25
26 // EXTERNAL INCLUDES
27 #include <fstream>
28 #include <iostream>
29 #include <sstream>
30
31
32 namespace
33 {
34
35 const char* LANDSCAPE_QUALIFIER = "landscape";
36 const char* PORTRAIT_QUALIFIER  = "portrait";
37
38 const char* DEFAULT_THEME = DALI_STYLE_DIR "tizen-default-theme.json";
39
40 const char* PACKAGE_PATH_KEY = "PACKAGE_PATH";
41 const char* DEFAULT_PACKAGE_PATH = DALI_DATA_READ_ONLY_DIR "/toolkit/";
42
43 } // namespace
44
45 namespace Dali
46 {
47
48 namespace Toolkit
49 {
50
51 namespace Internal
52 {
53
54 namespace
55 {
56
57 BaseHandle Create()
58 {
59   BaseHandle handle = StyleManager::Get();
60
61   if ( !handle )
62   {
63     SingletonService singletonService( SingletonService::Get() );
64     if ( singletonService )
65     {
66       Toolkit::StyleManager manager = Toolkit::StyleManager( new Internal::StyleManager() );
67       singletonService.Register( typeid( manager ), manager );
68       handle = manager;
69     }
70   }
71
72   return handle;
73 }
74 TypeRegistration STYLE_MANAGER_TYPE( typeid(Dali::Toolkit::StyleManager), typeid(Dali::BaseHandle), Create, true /* Create instance at startup */ );
75
76 /**
77  * Merge two maps into one
78  */
79 void MergeMaps( const PropertyValueMap& a, const PropertyValueMap& b, PropertyValueMap& out )
80 {
81   out = a;
82   for( PropertyValueMap::const_iterator it = b.begin(), itEnd = b.end(); it != itEnd; ++it )
83   {
84     out[ it->first ] = it->second;
85   }
86 }
87
88 } // namespace
89
90 Toolkit::StyleManager StyleManager::Get()
91 {
92   Toolkit::StyleManager manager;
93
94   SingletonService singletonService( SingletonService::Get() );
95   if ( singletonService )
96   {
97     // Check whether the style manager is already created
98     Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::StyleManager ) );
99     if( handle )
100     {
101       // If so, downcast the handle of singleton
102       manager = Toolkit::StyleManager( dynamic_cast< StyleManager* >( handle.GetObjectPtr() ) );
103     }
104   }
105
106   return manager;
107 }
108
109 StyleManager::StyleManager()
110   : mOrientationDegrees( 0 )  // Portrait
111 {
112   // Add theme builder constants
113   mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = DEFAULT_PACKAGE_PATH;
114
115   RequestDefaultTheme();
116
117   StyleMonitor styleMonitor( StyleMonitor::Get() );
118   if( styleMonitor )
119   {
120     styleMonitor.StyleChangeSignal().Connect( this, &StyleManager::StyleMonitorChange );
121   }
122 }
123
124 StyleManager::~StyleManager()
125 {
126 }
127
128 void StyleManager::SetOrientationValue( int orientation )
129 {
130   if( orientation !=  mOrientationDegrees )
131   {
132     mOrientationDegrees = orientation;
133     // TODO: if orientation changed, apply the new style to all controls
134     // dont want to really do the whole load from file again if the bundle contains both portrait & landscape
135     SetTheme();
136   }
137 }
138
139 int StyleManager::GetOrientationValue()
140 {
141   return mOrientationDegrees;
142 }
143
144 void StyleManager::SetOrientation( Orientation orientation )
145 {
146   if( mOrientation )
147   {
148     mOrientation.ChangedSignal().Disconnect( this, &StyleManager::OnOrientationChanged );
149   }
150
151   OnOrientationChanged( orientation );
152
153   if( mOrientation )
154   {
155     mOrientation.ChangedSignal().Connect( this, &StyleManager::OnOrientationChanged );
156   }
157 }
158
159 Orientation StyleManager::GetOrientation()
160 {
161   return mOrientation;
162 }
163
164 void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value )
165 {
166   mStyleBuilderConstants[ key ] = value;
167 }
168
169 bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& valueOut )
170 {
171   Toolkit::PropertyValueMap::iterator valueIt = mStyleBuilderConstants.find( key );
172   if( valueIt != mStyleBuilderConstants.end() )
173   {
174     valueOut = valueIt->second;
175     return true;
176   }
177
178   return false;
179 }
180
181 void StyleManager::OnOrientationChanged( Orientation orientation )
182 {
183   mOrientation = orientation;
184   // TODO: if orientation changed, apply the new style to all controls
185   // dont want to really do the whole load from file again if the bundle contains both portrait & landscape
186   SetTheme();
187 }
188
189 Toolkit::Builder StyleManager::CreateBuilder( const PropertyValueMap& constants )
190 {
191   Toolkit::Builder builder = Toolkit::Builder::New();
192   builder.AddConstants( constants );
193
194   return builder;
195 }
196
197 bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath )
198 {
199   std::string fileString;
200   if( LoadFile( jsonFilePath, fileString ) )
201   {
202     builder.LoadFromString( fileString );
203     return true;
204   }
205   else
206   {
207     DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
208     return false;
209   }
210 }
211
212 void StyleManager::CollectQualifiers( StringList& qualifiersOut )
213 {
214   // Append the relevant qualifier for orientation
215   int orientation = mOrientationDegrees;
216
217   if( mOrientation )
218   {
219     orientation = mOrientation.GetDegrees();
220   }
221
222   switch( orientation )
223   {
224     case 90:
225     case 270:
226     {
227       qualifiersOut.push_back( std::string( LANDSCAPE_QUALIFIER ) );
228       break;
229     }
230     case 180:
231     case 0: // fall through
232     default:
233     {
234       qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) );
235       break;
236     }
237   }
238 }
239
240 void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const StringList& qualifiers, std::string& qualifiedStyleOut )
241 {
242   qualifiedStyleOut.append( styleName );
243
244   for( StringList::const_iterator it = qualifiers.begin(), itEnd = qualifiers.end(); it != itEnd; ++it )
245   {
246     const std::string& str = *it;
247
248     qualifiedStyleOut.append( "-" );
249     qualifiedStyleOut.append( str );
250   }
251 }
252
253 void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
254 {
255   // Convert control name to lower case
256   std::string styleName = control.GetTypeName();
257   std::transform( styleName.begin(), styleName.end(), styleName.begin(), ::tolower );
258
259   // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
260   StringList qualifiers;
261   CollectQualifiers( qualifiers );
262
263   while( true )
264   {
265     std::string qualifiedStyleName;
266     BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
267
268     // Break if style found or we have tried the root style name (qualifiers is empty)
269     if( builder.ApplyStyle( qualifiedStyleName, control ) || qualifiers.size() == 0 )
270     {
271       break;
272     }
273
274     // Remove the last qualifier in an attempt to find a style that is valid
275     qualifiers.pop_back();
276   }
277 }
278
279 void StyleManager::ApplyThemeStyle( Toolkit::Control control )
280 {
281   if( mThemeBuilder )
282   {
283     ApplyStyle( mThemeBuilder, control );
284   }
285 }
286
287 void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName )
288 {
289   bool builderReady = false;
290
291   // First look in the cache
292   Toolkit::Builder builder = FindCachedBuilder( jsonFileName );
293   if( builder )
294   {
295     builderReady = true;
296   }
297   else
298   {
299     // Merge theme and style constants
300     PropertyValueMap constants;
301     MergeMaps( mThemeBuilderConstants, mStyleBuilderConstants, constants );
302
303     // Create it
304     builder = CreateBuilder( constants );
305
306     if( LoadJSON( builder, jsonFileName ) )
307     {
308       CacheBuilder( builder, jsonFileName );
309       builderReady = true;
310     }
311   }
312
313   // Apply the style to the control
314   if( builderReady )
315   {
316     builder.ApplyStyle( styleName, control );
317   }
318 }
319
320 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
321 {
322   DALI_ASSERT_DEBUG( 0 != filename.length());
323
324   std::ifstream in( filename.c_str(), std::ios::in );
325   if( in )
326   {
327     std::stringstream buffer;
328     buffer << in.rdbuf();
329
330     stringOut = buffer.str();
331
332     in.close();
333
334     return true;
335   }
336
337   return false;
338 }
339
340 Toolkit::StyleManager::StyleChangeSignalType& StyleManager::StyleChangeSignal()
341 {
342   return mStyleChangeSignal;
343 }
344
345 void StyleManager::RequestThemeChange( const std::string& themeFile )
346 {
347   mThemeFile = themeFile;
348
349   // need to do style change synchronously as app might create a UI control on the next line
350   SetTheme();
351 }
352
353 void StyleManager::RequestDefaultTheme()
354 {
355   RequestThemeChange( DEFAULT_THEME );
356 }
357
358 void StyleManager::SetTheme()
359 {
360   mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
361   LoadJSON( mThemeBuilder, mThemeFile );
362
363   StyleChange change;
364   change.themeChange = true;
365   mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), change );
366 }
367
368 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
369 {
370   BuilderMap::iterator builderIt = mBuilderCache.find( key );
371   if( builderIt != mBuilderCache.end() )
372   {
373     return builderIt->second;
374   }
375
376   return Toolkit::Builder();
377 }
378
379 void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key )
380 {
381   mBuilderCache[ key ] = builder;
382 }
383
384 void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange styleChange )
385 {
386   mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), styleChange );
387 }
388
389 } // namespace Internal
390
391 } // namespace Toolkit
392
393 } // namespace Dali