Xcos MVC: add missing headers
[scilab.git] / scilab / modules / scicos / src / cpp / view_scilab / ParamsAdapter.cpp
1 /*
2  *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  *  Copyright (C) 2014-2014 - Scilab Enterprises - Clement DAVID
4  *
5  *  This file must be used under the terms of the CeCILL.
6  *  This source file is licensed as described in the file COPYING, which
7  *  you should have received as part of this distribution.  The terms
8  *  are also available at
9  *  http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
10  *
11  */
12
13 #include <string>
14 #include <vector>
15 #include <algorithm>
16
17 #include "double.hxx"
18 #include "string.hxx"
19 #include "tlist.hxx"
20
21 #include "Controller.hxx"
22 #include "ParamsAdapter.hxx"
23
24 extern "C" {
25 #include "sci_malloc.h"
26 #include "charEncoding.h"
27 }
28
29 namespace org_scilab_modules_scicos
30 {
31 namespace view_scilab
32 {
33 namespace
34 {
35
36 const std::wstring scsopt(L"scsopt");
37
38 struct dummy_property
39 {
40
41     static types::InternalType* get(const ParamsAdapter& adaptor, const Controller& controller)
42     {
43         // silent unused parameter warnings
44         (void) adaptor;
45         (void) controller;
46
47         return 0;
48     }
49
50     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& controller)
51     {
52         // silent unused parameter warnings
53         (void) adaptor;
54         (void) v;
55         (void) controller;
56
57         // everything should be right as the properties mapped using this adapter do not perform anything
58         return true;
59     }
60 };
61
62 struct title
63 {
64
65     static types::InternalType* get(const ParamsAdapter& adaptor, const Controller& controller)
66     {
67         model::Diagram* adaptee = adaptor.getAdaptee();
68
69         std::string title;
70         controller.getObjectProperty(adaptee->id(), adaptee->kind(), TITLE, title);
71         std::string path;
72         controller.getObjectProperty(adaptee->id(), adaptee->kind(), PATH, path);
73
74         types::String* o = new types::String(2, 1);
75         o->set(0, title.data());
76         o->set(1, path.data());
77
78         return o;
79     }
80
81     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& controller)
82     {
83         if (v->getType() != types::InternalType::ScilabString)
84         {
85             return false;
86         }
87
88         model::Diagram* adaptee = adaptor.getAdaptee();
89
90         std::string path;
91         std::string title;
92         types::String* current = v->getAs<types::String>();
93         if (current->getSize() == 1)
94         {
95             // Pass an empty path
96         }
97         else if (current->getSize() == 2)
98         {
99             char* Path = wide_string_to_UTF8(current->get(1));
100             title = std::string(Path);
101             FREE(Path);
102         }
103         else
104         {
105             return false;
106         }
107
108         char* Title = wide_string_to_UTF8(current->get(0));
109         title = std::string(Title);
110         FREE(Title);
111
112         controller.setObjectProperty(adaptee->id(), adaptee->kind(), TITLE, title);
113         controller.setObjectProperty(adaptee->id(), adaptee->kind(), PATH, path);
114         return true;
115     }
116 };
117
118 struct tol
119 {
120
121     static types::InternalType* get(const ParamsAdapter& adaptor, const Controller& controller)
122     {
123         model::Diagram* adaptee = adaptor.getAdaptee();
124
125         double* data;
126         types::Double* o = new types::Double(7, 1, &data);
127
128         std::vector<double> tol;
129         controller.getObjectProperty(adaptee->id(), adaptee->kind(), PROPERTIES, tol);
130
131         std::copy(tol.begin() + 1, tol.end(), data);
132
133         return o;
134     }
135
136     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& controller)
137     {
138
139         if (v->getType() != types::InternalType::ScilabDouble)
140         {
141             return false;
142         }
143
144         types::Double* current = v->getAs<types::Double>();
145         if (current->getSize() != 7)
146         {
147             return false;
148         }
149
150         model::Diagram* adaptee = adaptor.getAdaptee();
151
152         std::vector<double> tol;
153         controller.getObjectProperty(adaptee->id(), adaptee->kind(), PROPERTIES, tol);
154
155         std::copy(current->getReal(), current->getReal() + current->getSize(), tol.begin() + 1);
156
157         controller.setObjectProperty(adaptee->id(), adaptee->kind(), PROPERTIES, tol);
158         return true;
159     }
160 };
161
162 struct tf
163 {
164
165     static types::InternalType* get(const ParamsAdapter& adaptor, const Controller& controller)
166     {
167         model::Diagram* adaptee = adaptor.getAdaptee();
168
169         std::vector<double> tf;
170         controller.getObjectProperty(adaptee->id(), adaptee->kind(), PROPERTIES, tf);
171
172         return new types::Double(tf[0]);
173     }
174
175     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& controller)
176     {
177
178         if (v->getType() != types::InternalType::ScilabDouble)
179         {
180             return false;
181         }
182
183         types::Double* current = v->getAs<types::Double>();
184         if (current->getSize() != 1)
185         {
186             return false;
187         }
188
189         model::Diagram* adaptee = adaptor.getAdaptee();
190
191         std::vector<double> tol;
192         controller.getObjectProperty(adaptee->id(), adaptee->kind(), PROPERTIES, tol);
193
194         tol[0] = current->get(0);
195
196         controller.setObjectProperty(adaptee->id(), adaptee->kind(), PROPERTIES, tol);
197         return true;
198     }
199 };
200
201 struct context
202 {
203
204     static types::InternalType* get(const ParamsAdapter& adaptor, const Controller& controller)
205     {
206         model::Diagram* adaptee = adaptor.getAdaptee();
207
208         std::vector<std::string> context;
209         controller.getObjectProperty(adaptee->id(), adaptee->kind(), DIAGRAM_CONTEXT, context);
210
211         types::String* o = new types::String((int)context.size(), 1);
212         for (int i = 0; i < (int)context.size(); ++i)
213         {
214             o->set(i, context[i].data());
215         }
216
217         return o;
218     }
219
220     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& controller)
221     {
222         if (v->getType() == types::InternalType::ScilabString)
223         {
224             types::String* current = v->getAs<types::String>();
225             if (current->getCols() != 0 && current->getCols() != 1)
226             {
227                 return false;
228             }
229
230             model::Diagram* adaptee = adaptor.getAdaptee();
231
232             std::vector<std::string> context (current->getSize());
233             for (int i = 0; i < (int)context.size(); ++i)
234             {
235                 char* c_str = wide_string_to_UTF8(current->get(i));
236                 context[i] = std::string(c_str);
237                 FREE(c_str);
238             }
239
240             controller.setObjectProperty(adaptee->id(), adaptee->kind(), DIAGRAM_CONTEXT, context);
241             return true;
242         }
243         else if (v->getType() == types::InternalType::ScilabDouble)
244         {
245             types::Double* current = v->getAs<types::Double>();
246             if (current->getRows() != 0 || current->getCols() != 0)
247             {
248                 return false;
249             }
250
251             model::Diagram* adaptee = adaptor.getAdaptee();
252
253             std::vector<std::string> context;
254             controller.setObjectProperty(adaptee->id(), adaptee->kind(), DIAGRAM_CONTEXT, context);
255             return true;
256         }
257         return false;
258     }
259 };
260
261 struct options
262 {
263
264     static types::InternalType* get(const ParamsAdapter& adaptor, const Controller& controller)
265     {
266         // silent unused parameter warnings
267         (void) adaptor;
268         (void) controller;
269
270         // Return a dummy 'scsopt'-typed tlist.
271         types::String* header = new types::String(scsopt.c_str());
272
273         types::TList* Scsopt = new types::TList();
274         Scsopt->set(0, header);
275         return Scsopt;
276     }
277
278     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& controller)
279     {
280         // The model does not store 'options'.
281         return dummy_property::set(adaptor, v, controller);
282     }
283 };
284
285 struct doc
286 {
287     static types::InternalType* get(const ParamsAdapter& adaptor, const Controller& controller)
288     {
289         // silent unused parameter warnings
290         (void) controller;
291
292         return adaptor.getDocContent();
293     }
294
295     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& controller)
296     {
297         // silent unused parameter warnings
298         (void) controller;
299
300         adaptor.setDocContent(v->clone());
301         return true;
302     }
303 };
304
305 } /* namespace */
306
307 template<> property<ParamsAdapter>::props_t property<ParamsAdapter>::fields = property<ParamsAdapter>::props_t();
308
309 ParamsAdapter::ParamsAdapter(const ParamsAdapter& o) :
310     BaseAdapter<ParamsAdapter, org_scilab_modules_scicos::model::Diagram>(o)
311 {
312     doc_content = new types::List();
313 }
314
315 ParamsAdapter::ParamsAdapter(org_scilab_modules_scicos::model::Diagram* o) :
316     BaseAdapter<ParamsAdapter, org_scilab_modules_scicos::model::Diagram>(o)
317 {
318     if (property<ParamsAdapter>::properties_have_not_been_set())
319     {
320         property<ParamsAdapter>::fields.reserve(10);
321         property<ParamsAdapter>::add_property(L"wpar", &dummy_property::get, &dummy_property::set);
322         property<ParamsAdapter>::add_property(L"title", &title::get, &title::set);
323         property<ParamsAdapter>::add_property(L"tol", &tol::get, &tol::set);
324         property<ParamsAdapter>::add_property(L"tf", &tf::get, &tf::set);
325         property<ParamsAdapter>::add_property(L"context", &context::get, &context::set);
326         property<ParamsAdapter>::add_property(L"void1", &dummy_property::get, &dummy_property::set);
327         property<ParamsAdapter>::add_property(L"options", &options::get, &options::set);
328         property<ParamsAdapter>::add_property(L"void2", &dummy_property::get, &dummy_property::set);
329         property<ParamsAdapter>::add_property(L"void3", &dummy_property::get, &dummy_property::set);
330         property<ParamsAdapter>::add_property(L"doc", &doc::get, &doc::set);
331     }
332
333     doc_content = new types::List();
334 }
335
336 ParamsAdapter::~ParamsAdapter()
337 {
338     delete doc_content;
339 }
340
341 std::wstring ParamsAdapter::getTypeStr()
342 {
343     return getSharedTypeStr();
344 }
345 std::wstring ParamsAdapter::getShortTypeStr()
346 {
347     return getSharedTypeStr();
348 }
349
350 types::InternalType* ParamsAdapter::getDocContent() const
351 {
352     return doc_content;
353 }
354
355 void ParamsAdapter::setDocContent(types::InternalType* v)
356 {
357     delete doc_content;
358     doc_content = v->clone();
359 }
360
361 } /* namespace view_scilab */
362 } /* namespace org_scilab_modules_scicos */