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