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