Xcos MVC: give default values to scs_m.props.options tlist for backward compatibility
[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 "bool.hxx"
20 #include "list.hxx"
21 #include "tlist.hxx"
22
23 #include "utilities.hxx"
24 #include "Controller.hxx"
25 #include "ParamsAdapter.hxx"
26
27 extern "C" {
28 #include "sci_malloc.h"
29 #include "charEncoding.h"
30 }
31
32 namespace org_scilab_modules_scicos
33 {
34 namespace view_scilab
35 {
36 namespace
37 {
38
39 const std::wstring scsopt(L"scsopt");
40 const std::wstring ThreeD(L"3D");
41 const std::wstring Background(L"Background");
42 const std::wstring Link(L"Link");
43 const std::wstring ID(L"ID");
44 const std::wstring Cmap(L"Cmap");
45
46 struct dummy_property
47 {
48
49     static types::InternalType* get(const ParamsAdapter& /*adaptor*/, const Controller& /*controller*/)
50     {
51         // Return a default empty matrix.
52         return types::Double::Empty();
53     }
54
55     static bool set(ParamsAdapter& /*adaptor*/, types::InternalType* /*v*/, Controller& /*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         ScicosID adaptee = adaptor.getAdaptee()->id();
68
69         std::string title;
70         controller.getObjectProperty(adaptee, DIAGRAM, TITLE, title);
71         std::string path;
72         controller.getObjectProperty(adaptee, DIAGRAM, 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         ScicosID adaptee = adaptor.getAdaptee()->id();
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             path = 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, DIAGRAM, TITLE, title);
113         controller.setObjectProperty(adaptee, DIAGRAM, 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         ScicosID adaptee = adaptor.getAdaptee()->id();
124
125         double* data;
126         types::Double* o = new types::Double(1, 7, &data);
127
128         std::vector<double> tol;
129         controller.getObjectProperty(adaptee, DIAGRAM, PROPERTIES, tol);
130 #ifdef _MSC_VER
131         std::copy(tol.begin() + 1, tol.end(), stdext::checked_array_iterator<double*>( data, 7 ));
132 #else
133         std::copy(tol.begin() + 1, tol.end(), data);
134 #endif
135
136         return o;
137     }
138
139     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& controller)
140     {
141
142         if (v->getType() != types::InternalType::ScilabDouble)
143         {
144             return false;
145         }
146
147         types::Double* current = v->getAs<types::Double>();
148         if (current->getSize() != 6 && current->getSize() != 7)
149         {
150             return false;
151         }
152
153         ScicosID adaptee = adaptor.getAdaptee()->id();
154
155         std::vector<double> tol;
156         controller.getObjectProperty(adaptee, DIAGRAM, PROPERTIES, tol);
157
158         std::copy(current->getReal(), current->getReal() + current->getSize(), tol.begin() + 1);
159
160         // In case the last parameter is missing
161         if (current->getSize() == 6)
162         {
163             tol[7] = 0;
164         }
165
166         controller.setObjectProperty(adaptee, DIAGRAM, PROPERTIES, tol);
167         return true;
168     }
169 };
170
171 struct tf
172 {
173
174     static types::InternalType* get(const ParamsAdapter& adaptor, const Controller& controller)
175     {
176         ScicosID adaptee = adaptor.getAdaptee()->id();
177
178         std::vector<double> tf;
179         controller.getObjectProperty(adaptee, DIAGRAM, PROPERTIES, tf);
180
181         return new types::Double(tf[0]);
182     }
183
184     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& controller)
185     {
186
187         if (v->getType() != types::InternalType::ScilabDouble)
188         {
189             return false;
190         }
191
192         types::Double* current = v->getAs<types::Double>();
193         if (current->getSize() != 1)
194         {
195             return false;
196         }
197
198         ScicosID adaptee = adaptor.getAdaptee()->id();
199
200         std::vector<double> tol;
201         controller.getObjectProperty(adaptee, DIAGRAM, PROPERTIES, tol);
202
203         tol[0] = current->get(0);
204
205         controller.setObjectProperty(adaptee, DIAGRAM, PROPERTIES, tol);
206         return true;
207     }
208 };
209
210 struct context
211 {
212
213     static types::InternalType* get(const ParamsAdapter& adaptor, const Controller& controller)
214     {
215         ScicosID adaptee = adaptor.getAdaptee()->id();
216
217         std::vector<std::string> context;
218         controller.getObjectProperty(adaptee, DIAGRAM, DIAGRAM_CONTEXT, context);
219
220         if (context.size() == 0)
221         {
222             // An empty context returns an empty matrix
223             return types::Double::Empty();
224         }
225
226         types::String* o = new types::String((int)context.size(), 1);
227         for (int i = 0; i < (int)context.size(); ++i)
228         {
229             o->set(i, context[i].data());
230         }
231
232         return o;
233     }
234
235     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& controller)
236     {
237         if (v->getType() == types::InternalType::ScilabString)
238         {
239             types::String* current = v->getAs<types::String>();
240             if (current->getCols() != 0 && current->getCols() != 1)
241             {
242                 return false;
243             }
244
245             ScicosID adaptee = adaptor.getAdaptee()->id();
246
247             std::vector<std::string> context (current->getSize());
248             for (int i = 0; i < (int)context.size(); ++i)
249             {
250                 char* c_str = wide_string_to_UTF8(current->get(i));
251                 context[i] = std::string(c_str);
252                 FREE(c_str);
253             }
254
255             controller.setObjectProperty(adaptee, DIAGRAM, DIAGRAM_CONTEXT, context);
256             return true;
257         }
258         else if (v->getType() == types::InternalType::ScilabDouble)
259         {
260             types::Double* current = v->getAs<types::Double>();
261             if (!current->isEmpty())
262             {
263                 return false;
264             }
265
266             ScicosID adaptee = adaptor.getAdaptee()->id();
267
268             std::vector<std::string> context;
269             controller.setObjectProperty(adaptee, DIAGRAM, DIAGRAM_CONTEXT, context);
270             return true;
271         }
272         return false;
273     }
274 };
275
276 struct options
277 {
278
279     static types::InternalType* get(const ParamsAdapter& /*adaptor*/, const Controller& /*controller*/)
280     {
281         // Return a default 'scsopt'-typed tlist.
282         types::String* header = new types::String(1, 6);
283         header->set(0, scsopt.c_str());
284         header->set(1, ThreeD.c_str());
285         header->set(2, Background.c_str());
286         header->set(3, Link.c_str());
287         header->set(4, ID.c_str());
288         header->set(5, Cmap.c_str());
289
290         types::TList* Scsopt = new types::TList();
291         Scsopt->append(header);
292
293         types::List* ThreeDField = new types::List();
294         types::Bool* ThreeD1 = new types::Bool(1);
295         ThreeDField->append(ThreeD1);
296         types::Double* ThreeD2 = new types::Double(33);
297         ThreeDField->append(ThreeD2);
298         Scsopt->append(ThreeDField);
299
300         types::Double* BackgroundField = new types::Double(1, 2);
301         BackgroundField->set(0, 8);
302         BackgroundField->set(1, 1);
303         Scsopt->append(BackgroundField);
304
305         types::Double* LinkField = new types::Double(1, 2);
306         LinkField->set(0, 1);
307         LinkField->set(1, 5);
308         Scsopt->append(LinkField);
309
310         types::List* IDField = new types::List();
311         types::Double* ID1 = new types::Double(1, 4);
312         ID1->set(0, 4);
313         ID1->set(1, 1);
314         ID1->set(2, 10);
315         ID1->set(3, 1);
316         IDField->append(ID1);
317         types::Double* ID2 = new types::Double(1, 4);
318         ID2->set(0, 4);
319         ID2->set(1, 1);
320         ID2->set(2, 2);
321         ID2->set(3, 1);
322         IDField->append(ID2);
323         Scsopt->append(IDField);
324
325         types::Double* CmapField = new types::Double(1, 3);
326         CmapField->set(0, 0.8);
327         CmapField->set(1, 0.8);
328         CmapField->set(2, 0.8);
329         Scsopt->append(CmapField);
330
331         return Scsopt;
332     }
333
334     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& controller)
335     {
336         // The model does not store 'options'.
337         return dummy_property::set(adaptor, v, controller);
338     }
339 };
340
341 struct doc
342 {
343     static types::InternalType* get(const ParamsAdapter& adaptor, const Controller& /*controller*/)
344     {
345         return adaptor.getDocContent();
346     }
347
348     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& /*controller*/)
349     {
350         adaptor.setDocContent(v->clone());
351         return true;
352     }
353 };
354
355 } /* namespace */
356
357 template<> property<ParamsAdapter>::props_t property<ParamsAdapter>::fields = property<ParamsAdapter>::props_t();
358 static void initialize_fields()
359 {
360     if (property<ParamsAdapter>::properties_have_not_been_set())
361     {
362         property<ParamsAdapter>::fields.reserve(10);
363         property<ParamsAdapter>::add_property(L"wpar", &dummy_property::get, &dummy_property::set);
364         property<ParamsAdapter>::add_property(L"title", &title::get, &title::set);
365         property<ParamsAdapter>::add_property(L"tol", &tol::get, &tol::set);
366         property<ParamsAdapter>::add_property(L"tf", &tf::get, &tf::set);
367         property<ParamsAdapter>::add_property(L"context", &context::get, &context::set);
368         property<ParamsAdapter>::add_property(L"void1", &dummy_property::get, &dummy_property::set);
369         property<ParamsAdapter>::add_property(L"options", &options::get, &options::set);
370         property<ParamsAdapter>::add_property(L"void2", &dummy_property::get, &dummy_property::set);
371         property<ParamsAdapter>::add_property(L"void3", &dummy_property::get, &dummy_property::set);
372         property<ParamsAdapter>::add_property(L"doc", &doc::get, &doc::set);
373     }
374 }
375
376 ParamsAdapter::ParamsAdapter() :
377     BaseAdapter<ParamsAdapter, org_scilab_modules_scicos::model::Diagram>(),
378     doc_content(new types::List())
379 {
380     initialize_fields();
381 }
382
383 ParamsAdapter::ParamsAdapter(const Controller& c, org_scilab_modules_scicos::model::Diagram* adaptee) :
384     BaseAdapter<ParamsAdapter, org_scilab_modules_scicos::model::Diagram>(c, adaptee),
385     doc_content(new types::List())
386 {
387     initialize_fields();
388 }
389
390 ParamsAdapter::~ParamsAdapter()
391 {
392     doc_content->DecreaseRef();
393     doc_content->killMe();
394 }
395
396 std::wstring ParamsAdapter::getTypeStr()
397 {
398     return getSharedTypeStr();
399 }
400 std::wstring ParamsAdapter::getShortTypeStr()
401 {
402     return getSharedTypeStr();
403 }
404
405 types::InternalType* ParamsAdapter::getDocContent() const
406 {
407     doc_content->IncreaseRef();
408     return doc_content;
409 }
410
411 void ParamsAdapter::setDocContent(types::InternalType* v)
412 {
413     doc_content->DecreaseRef();
414     doc_content->killMe();
415
416     v->IncreaseRef();
417     doc_content = v;
418 }
419
420 } /* namespace view_scilab */
421 } /* namespace org_scilab_modules_scicos */