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