Moved StyleManager to the public API
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / toolkit-style-monitor.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
18 #include "toolkit-style-monitor.h"
19
20 #include <dali/public-api/common/dali-common.h>
21 #include <dali/public-api/object/base-object.h>
22 #include <dali/public-api/signals/dali-signal.h>
23
24 namespace Dali
25 {
26 const std::string Dali::StyleMonitor::DEFAULT_FONT_FAMILY("DefaultFont");
27 const std::string Dali::StyleMonitor::DEFAULT_FONT_STYLE("Regular");
28 const float       Dali::StyleMonitor::DEFAULT_FONT_SIZE(1.0f);
29 }
30
31 namespace
32 {
33 const char* DEFAULT_THEME=
34   "{\"styles\":{\n"
35   "  \"textlabel\":\n"
36   "    {\n"
37   "      \"fontStyle\":\"Regular\",\n"
38   "      \"pointSize\":18\n"
39   "    }\n"
40   "  }\n"
41   "}\n";
42
43 struct NamedTheme
44 {
45   NamedTheme( const std::string& name, const std::string& theme )
46   : name(name), theme(theme)
47   {
48   }
49
50   std::string name;
51   std::string theme;
52 };
53 typedef std::vector< NamedTheme > NamedThemes;
54 NamedThemes gThemes;
55
56 std::string gTheme;
57 std::string gFontFamily = Dali::StyleMonitor::DEFAULT_FONT_FAMILY;
58 std::string gFontStyle  = Dali::StyleMonitor::DEFAULT_FONT_STYLE;
59 float       gFontSize   = Dali::StyleMonitor::DEFAULT_FONT_SIZE;
60 }
61
62 namespace Dali
63 {
64 namespace Internal
65 {
66 namespace Adaptor
67 {
68 /**
69  * Stub for the StyleMonitor
70  */
71 class StyleMonitor : public BaseObject
72 {
73 public: // Creation & Destruction
74   static Dali::StyleMonitor Get();
75   StyleMonitor();
76   ~StyleMonitor();
77
78 public: // Style Information
79   std::string GetDefaultFontFamily() const;
80   std::string GetDefaultFontStyle() const;
81   float GetDefaultFontSize() const;
82   const std::string& GetTheme() const;
83   void SetTheme(std::string theme);
84   bool LoadThemeFile( const std::string& filename, std::string& output );
85
86 public: // Signals
87   Dali::StyleMonitor::StyleChangeSignalType& StyleChangeSignal();
88
89   void EmitStyleChangeSignal(StyleChange::Type styleChange)
90   {
91     mStyleChangeSignal.Emit(Dali::StyleMonitor(this), styleChange);
92   }
93
94 private:
95   Dali::StyleMonitor::StyleChangeSignalType mStyleChangeSignal;
96   static Dali::StyleMonitor mToolkitStyleMonitor;
97
98   std::string mTheme;  ///<< Current theme name
99 };
100
101 Dali::StyleMonitor StyleMonitor::mToolkitStyleMonitor;
102
103 Dali::StyleMonitor StyleMonitor::Get()
104 {
105   if( ! mToolkitStyleMonitor )
106   {
107     mToolkitStyleMonitor = Dali::StyleMonitor( new Dali::Internal::Adaptor::StyleMonitor() );
108   }
109   return mToolkitStyleMonitor;
110 }
111
112 StyleMonitor::StyleMonitor()
113 : mTheme("default")
114 {
115 }
116
117 StyleMonitor::~StyleMonitor()
118 {
119 }
120
121 std::string StyleMonitor::GetDefaultFontFamily() const
122 {
123   return gFontFamily;
124 }
125
126 std::string StyleMonitor::GetDefaultFontStyle() const
127 {
128   return gFontStyle;
129 }
130
131 float StyleMonitor::GetDefaultFontSize() const
132 {
133   return gFontSize;
134
135 }
136
137 const std::string& StyleMonitor::GetTheme() const
138 {
139   return mTheme;
140 }
141
142 void StyleMonitor::SetTheme(std::string path)
143 {
144   mTheme = path;
145   EmitStyleChangeSignal( StyleChange::THEME_CHANGE );
146 }
147
148 bool StyleMonitor::LoadThemeFile( const std::string& filename, std::string& output )
149 {
150   for( NamedThemes::iterator iter = gThemes.begin(); iter != gThemes.end(); ++iter )
151   {
152     NamedTheme& theme = *iter;
153     if( theme.name == filename )
154     {
155       output = theme.theme;
156       return true;
157     }
158   }
159
160   if( !gTheme.empty() )
161   {
162     output = gTheme;
163   }
164   else
165   {
166     output = DEFAULT_THEME;
167   }
168   return true;
169 }
170
171 Dali::StyleMonitor::StyleChangeSignalType& StyleMonitor::StyleChangeSignal()
172 {
173   return mStyleChangeSignal;
174 }
175
176 } // namespace Adaptor
177 } // namespace Internal
178
179 ////////////////////////////////////////////////////////////////////////////////////////////////////
180
181 Internal::Adaptor::StyleMonitor& GetImplementation(Dali::StyleMonitor& monitor)
182 {
183   BaseObject& object = monitor.GetBaseObject();
184   return static_cast<Internal::Adaptor::StyleMonitor&>(object);
185 }
186 const Internal::Adaptor::StyleMonitor& GetImplementation(const Dali::StyleMonitor& monitor)
187 {
188   const BaseObject& object = monitor.GetBaseObject();
189   return static_cast<const Internal::Adaptor::StyleMonitor&>(object);
190 }
191
192 StyleMonitor::StyleMonitor()
193 {
194 }
195
196 StyleMonitor::StyleMonitor(const StyleMonitor& monitor)
197 : BaseHandle(monitor)
198 {
199 }
200
201 StyleMonitor StyleMonitor::StyleMonitor::Get()
202 {
203   return Internal::Adaptor::StyleMonitor::Get();
204 }
205
206 StyleMonitor::~StyleMonitor()
207 {
208 }
209
210 std::string StyleMonitor::GetDefaultFontFamily() const
211 {
212   return GetImplementation(*this).GetDefaultFontFamily();
213 }
214
215 std::string StyleMonitor::GetDefaultFontStyle() const
216 {
217   return GetImplementation(*this).GetDefaultFontStyle();
218 }
219
220 float StyleMonitor::GetDefaultFontSize() const
221 {
222   return GetImplementation(*this).GetDefaultFontSize();
223 }
224
225 const std::string& StyleMonitor::GetTheme() const
226 {
227   return GetImplementation(*this).GetTheme();
228 }
229
230 void StyleMonitor::SetTheme(std::string themeFilePath)
231 {
232   GetImplementation(*this).SetTheme(themeFilePath);
233 }
234
235 StyleMonitor::StyleChangeSignalType& StyleMonitor::StyleChangeSignal()
236 {
237   return GetImplementation(*this).StyleChangeSignal();
238 }
239
240 void StyleMonitor::EmitStyleChangeSignal(StyleChange::Type styleChange)
241 {
242   GetImplementation(*this).EmitStyleChangeSignal(styleChange);
243 }
244
245 bool StyleMonitor::LoadThemeFile( const std::string& filename, std::string& output )
246 {
247   return GetImplementation(*this).LoadThemeFile(filename, output);
248 }
249
250 StyleMonitor& StyleMonitor::operator=(const StyleMonitor& monitor)
251 {
252   if( *this != monitor )
253   {
254     BaseHandle::operator=(monitor);
255   }
256   return *this;
257 }
258
259 StyleMonitor::StyleMonitor(Internal::Adaptor::StyleMonitor* internal)
260 : BaseHandle(internal)
261 {
262 }
263
264 } // namespace Dali
265
266 namespace Test
267 {
268 namespace StyleMonitor
269 {
270
271 void SetThemeFileOutput( const std::string& name, const std::string& output )
272 {
273   for( NamedThemes::iterator iter = gThemes.begin(); iter != gThemes.end(); ++iter )
274   {
275     NamedTheme& theme = *iter;
276     if( theme.name == name )
277     {
278       theme.theme = output;
279       return;
280     }
281   }
282
283   gThemes.push_back( NamedTheme( name, output ) );
284 }
285
286 void SetDefaultFontFamily(const std::string& family)
287 {
288   gFontFamily = family;
289 }
290
291 void SetDefaultFontStyle(const std::string& style)
292 {
293   gFontStyle = style;
294 }
295
296 void SetDefaultFontSize( float size )
297 {
298   gFontSize = size;
299 }
300
301 } // StyleMonitor
302 } // Test