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