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