Fix feedback style error message.
[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   SetTheme( DEFAULT_THEME );
137 }
138
139 const std::string& StyleManager::GetDefaultFontFamily() const
140 {
141   return mDefaultFontFamily;
142 }
143
144 void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value )
145 {
146   mStyleBuilderConstants[ key ] = value;
147 }
148
149 bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& valueOut )
150 {
151   Property::Value* value = mStyleBuilderConstants.Find( key );
152   if( value )
153   {
154     valueOut = *value;
155     return true;
156   }
157
158   return false;
159 }
160
161 void StyleManager::ApplyThemeStyle( Toolkit::Control control )
162 {
163   if( !mThemeBuilder )
164   {
165     ApplyDefaultTheme();
166   }
167
168   if( mThemeBuilder )
169   {
170     ApplyStyle( mThemeBuilder, control );
171   }
172 }
173
174 void StyleManager::ApplyThemeStyleAtInit( Toolkit::Control control )
175 {
176   ApplyThemeStyle( control );
177
178   if(mFeedbackStyle)
179   {
180     mFeedbackStyle->ObjectCreated( control );
181   }
182 }
183
184 void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName )
185 {
186   bool builderReady = false;
187
188   // First look in the cache
189   Toolkit::Builder builder = FindCachedBuilder( jsonFileName );
190   if( builder )
191   {
192     builderReady = true;
193   }
194   else
195   {
196     // Merge theme and style constants
197     Property::Map constants( mThemeBuilderConstants );
198     constants.Merge( mStyleBuilderConstants );
199
200     // Create it
201     builder = CreateBuilder( constants );
202
203     if( LoadJSON( builder, jsonFileName ) )
204     {
205       CacheBuilder( builder, jsonFileName );
206       builderReady = true;
207     }
208   }
209
210   // Apply the style to the control
211   if( builderReady )
212   {
213     builder.ApplyStyle( styleName, control );
214   }
215 }
216
217 Toolkit::StyleManager::StyleChangedSignalType& StyleManager::StyleChangedSignal()
218 {
219   return mStyleChangedSignal;
220 }
221
222 Toolkit::StyleManager::StyleChangedSignalType& StyleManager::ControlStyleChangeSignal()
223 {
224   return mControlStyleChangeSignal;
225 }
226
227 void StyleManager::SetTheme( const std::string& themeFile )
228 {
229   bool themeLoaded = false;
230
231   mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
232
233   // Always load the default theme first, then merge in the custom theme if present
234   themeLoaded = LoadJSON( mThemeBuilder, DEFAULT_THEME );
235   mThemeFile = themeFile;
236
237   if( themeFile.compare(DEFAULT_THEME) != 0 )
238   {
239     themeLoaded = LoadJSON( mThemeBuilder, mThemeFile );
240   }
241
242   if( themeLoaded )
243   {
244     if(mFeedbackStyle)
245     {
246       mFeedbackStyle->StyleChanged( mThemeFile, StyleChange::THEME_CHANGE );
247     }
248
249     EmitStyleChangeSignals(StyleChange::THEME_CHANGE);
250   }
251   else
252   {
253     mThemeBuilder.Reset();
254   }
255 }
256
257 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
258 {
259   DALI_ASSERT_DEBUG( 0 != filename.length());
260
261   // as toolkit is platform agnostic, it cannot load files from filesystem
262   // ask style monitor to load the style sheet
263   if( mStyleMonitor )
264   {
265     return mStyleMonitor.LoadThemeFile( filename, stringOut );
266   }
267
268   return false;
269 }
270
271 Toolkit::Builder StyleManager::CreateBuilder( const Property::Map& constants )
272 {
273   Toolkit::Builder builder = Toolkit::Builder::New();
274   builder.AddConstants( constants );
275
276   return builder;
277 }
278
279 bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath )
280 {
281   std::string fileString;
282   if( LoadFile( jsonFilePath, fileString ) )
283   {
284     builder.LoadFromString( fileString );
285     return true;
286   }
287   else
288   {
289     DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
290     return false;
291   }
292 }
293
294 static void CollectQualifiers( std::vector<std::string>& qualifiersOut )
295 {
296   // Append the relevant qualifier for orientation
297   // int orientation = 0; // Get the orientation from the system
298   /*
299   //// To Do /////
300   Getting orientation from the system, and determine Qualifie LANDSCAPE or PORTRAIT
301   orientation  0, 180 : PORTRAIT_QUALIFIER (default)
302   orientation 90, 270 : LANDSCAPE_QUALIFIER
303   */
304
305   qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) );
306
307 }
308
309 /**
310  * @brief Construct a qualified style name out of qualifiers
311  *
312  * A qualifed style name will be in the format: style-qualifier0-qualifier1-qualifierN
313  *
314  * @param[in] styleName The root name of the style
315  * @param[in] qualifiers List of qualifier names
316  * @param[out] qualifiedStyleOut The qualified style name
317  */
318 static void BuildQualifiedStyleName(
319   const std::string& styleName,
320   const std::vector<std::string>& qualifiers,
321   std::string& qualifiedStyleOut )
322 {
323   qualifiedStyleOut.append( styleName );
324
325   for( std::vector<std::string>::const_iterator it = qualifiers.begin(),
326          itEnd = qualifiers.end(); it != itEnd; ++it )
327   {
328     const std::string& str = *it;
329
330     qualifiedStyleOut.append( "-" );
331     qualifiedStyleOut.append( str );
332   }
333 }
334
335 static bool GetStyleNameForControl( Toolkit::Builder builder, Toolkit::Control control, std::string& styleName)
336 {
337   styleName = control.GetStyleName();
338
339   if( styleName.empty() )
340   {
341     styleName = control.GetTypeName();
342   }
343
344   // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
345   std::vector<std::string> qualifiers;
346   CollectQualifiers( qualifiers );
347
348   bool found = 0;
349   std::string qualifiedStyleName;
350   do
351   {
352     qualifiedStyleName.clear();
353     BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
354
355     // Break if style found or we have tried the root style name (qualifiers is empty)
356     if( GetImpl(builder).LookupStyleName( qualifiedStyleName ) )
357     {
358       found = true;
359       break;
360     }
361     if( qualifiers.size() == 0 )
362     {
363       break;
364     }
365     // Remove the last qualifier in an attempt to find a style that is valid
366     qualifiers.pop_back();
367   } while (!found);
368
369   if(found)
370   {
371     styleName = qualifiedStyleName;
372   }
373   return found;
374 }
375
376 void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
377 {
378   std::string styleName = control.GetStyleName();
379   if( GetStyleNameForControl( builder, control, styleName ) )
380   {
381     builder.ApplyStyle( styleName, control );
382   }
383
384   if( mDefaultFontSize >= 0 )
385   {
386     // Apply the style for logical font size
387     std::stringstream fontSizeQualifier;
388     fontSizeQualifier << styleName << FONT_SIZE_QUALIFIER << mDefaultFontSize;
389     builder.ApplyStyle( fontSizeQualifier.str(), control );
390   }
391 }
392
393 const StylePtr StyleManager::GetRecordedStyle( Toolkit::Control control )
394 {
395   if( mThemeBuilder )
396   {
397     std::string styleName = control.GetStyleName();
398
399     if( GetStyleNameForControl( mThemeBuilder, control, styleName ) )
400     {
401       const StylePtr style = GetImpl(mThemeBuilder).GetStyle( styleName );
402       return style;
403     }
404   }
405   return StylePtr(NULL);
406 }
407
408 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
409 {
410   BuilderMap::iterator builderIt = mBuilderCache.find( key );
411   if( builderIt != mBuilderCache.end() )
412   {
413     return builderIt->second;
414   }
415
416   return Toolkit::Builder();
417 }
418
419 void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key )
420 {
421   mBuilderCache[ key ] = builder;
422 }
423
424 void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange::Type styleChange )
425 {
426   switch ( styleChange )
427   {
428     case StyleChange::DEFAULT_FONT_CHANGE:
429     {
430       mDefaultFontFamily = styleMonitor.GetDefaultFontFamily();
431       break;
432     }
433
434     case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
435     {
436       mDefaultFontSize = styleMonitor.GetDefaultFontSize();
437       break;
438     }
439
440     case StyleChange::THEME_CHANGE:
441     {
442       SetTheme( styleMonitor.GetTheme() );
443       break;
444     }
445   }
446   EmitStyleChangeSignals( styleChange );
447 }
448
449 void StyleManager::EmitStyleChangeSignals( StyleChange::Type styleChange )
450 {
451   Toolkit::StyleManager styleManager = StyleManager::Get();
452
453   // Update Controls first
454   mControlStyleChangeSignal.Emit( styleManager, styleChange );
455
456   // Inform application last
457   mStyleChangedSignal.Emit( styleManager, styleChange );
458 }
459
460
461 } // namespace Internal
462
463 } // namespace Toolkit
464
465 } // namespace Dali