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