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