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