53bc9870f220a27e584b94ca5b83553ed6f105af
[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 }
27
28 namespace org_scilab_modules_scicos
29 {
30 namespace view_scilab
31 {
32 namespace
33 {
34
35 static const wchar_t* scsopt = L"scsopt";
36
37 struct wpar
38 {
39
40     static types::InternalType* get(const ParamsAdapter& adaptor, const Controller& controller)
41     {
42         return 0;
43     }
44
45     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& controller)
46     {
47         // The model does not stock wpar.
48         return true;
49     }
50 };
51
52 struct title
53 {
54
55     static types::InternalType* get(const ParamsAdapter& adaptor, const Controller& controller)
56     {
57         model::Diagram* adaptee = adaptor.getAdaptee();
58
59         std::string title;
60         controller.getObjectProperty(adaptee->id(), adaptee->kind(), TITLE, title);
61         std::string path;
62         controller.getObjectProperty(adaptee->id(), adaptee->kind(), PATH, path);
63
64         types::String* o = new types::String(2, 1);
65         o->set(0, title.data());
66         o->set(1, path.data());
67
68         return o;
69     }
70
71     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& controller)
72     {
73         if (v->getType() != types::InternalType::ScilabString)
74         {
75             return false;
76         }
77
78         model::Diagram* adaptee = adaptor.getAdaptee();
79
80         std::string path;
81         std::string title;
82         types::String* current = v->getAs<types::String>();
83         if (current->getSize() == 1)
84         {
85             // Pass an empty path
86         }
87         else if (current->getSize() == 2)
88         {
89             char* Path = wide_string_to_UTF8(current->get(1));
90             title = std::string(Path);
91             FREE(Path);
92         }
93         else
94         {
95             return false;
96         }
97
98         char* Title = wide_string_to_UTF8(current->get(0));
99         title = std::string(Title);
100         FREE(Title);
101
102         controller.setObjectProperty(adaptee->id(), adaptee->kind(), TITLE, title);
103         controller.setObjectProperty(adaptee->id(), adaptee->kind(), PATH, path);
104         return true;
105     }
106 };
107
108 struct tol
109 {
110
111     static types::InternalType* get(const ParamsAdapter& adaptor, const Controller& controller)
112     {
113         model::Diagram* adaptee = adaptor.getAdaptee();
114
115         double* data;
116         types::Double* o = new types::Double(7, 1, &data);
117
118         std::vector<double> tol;
119         controller.getObjectProperty(adaptee->id(), adaptee->kind(), PROPERTIES, tol);
120
121         std::copy(tol.begin() + 1, tol.end(), data);
122
123         return o;
124     }
125
126     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& controller)
127     {
128
129         if (v->getType() != types::InternalType::ScilabDouble)
130         {
131             return false;
132         }
133
134         types::Double* current = v->getAs<types::Double>();
135         if (current->getSize() != 7)
136         {
137             return false;
138         }
139
140         model::Diagram* adaptee = adaptor.getAdaptee();
141
142         std::vector<double> tol;
143         controller.getObjectProperty(adaptee->id(), adaptee->kind(), PROPERTIES, tol);
144
145         std::copy(current->getReal(), current->getReal() + current->getSize(), tol.begin() + 1);
146
147         controller.setObjectProperty(adaptee->id(), adaptee->kind(), PROPERTIES, tol);
148         return true;
149     }
150 };
151
152 struct tf
153 {
154
155     static types::InternalType* get(const ParamsAdapter& adaptor, const Controller& controller)
156     {
157         model::Diagram* adaptee = adaptor.getAdaptee();
158
159         std::vector<double> tf;
160         controller.getObjectProperty(adaptee->id(), adaptee->kind(), PROPERTIES, tf);
161
162         return new types::Double(tf[0]);
163     }
164
165     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& controller)
166     {
167
168         if (v->getType() != types::InternalType::ScilabDouble)
169         {
170             return false;
171         }
172
173         types::Double* current = v->getAs<types::Double>();
174         if (current->getSize() != 1)
175         {
176             return false;
177         }
178
179         model::Diagram* adaptee = adaptor.getAdaptee();
180
181         std::vector<double> tol;
182         controller.getObjectProperty(adaptee->id(), adaptee->kind(), PROPERTIES, tol);
183
184         tol[0] = current->get(0);
185
186         controller.setObjectProperty(adaptee->id(), adaptee->kind(), PROPERTIES, tol);
187         return true;
188     }
189 };
190
191 struct context
192 {
193
194     static types::InternalType* get(const ParamsAdapter& adaptor, const Controller& controller)
195     {
196         model::Diagram* adaptee = adaptor.getAdaptee();
197
198         std::vector<std::string> context;
199         controller.getObjectProperty(adaptee->id(), adaptee->kind(), DIAGRAM_CONTEXT, context);
200
201         types::String* o = new types::String((int)context.size(), 1);
202         for (int i = 0; i < (int)context.size(); ++i)
203         {
204             o->set(i, context[i].data());
205         }
206
207         return o;
208     }
209
210     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& controller)
211     {
212         if (v->getType() == types::InternalType::ScilabString)
213         {
214             types::String* current = v->getAs<types::String>();
215             if (current->getCols() != 0 && current->getCols() != 1)
216             {
217                 return false;
218             }
219
220             model::Diagram* adaptee = adaptor.getAdaptee();
221
222             std::vector<std::string> context (current->getSize());
223             for (int i = 0; i < (int)context.size(); ++i)
224             {
225                 char* c_str = wide_string_to_UTF8(current->get(i));
226                 context[i] = std::string(c_str);
227                 FREE(c_str);
228             }
229
230             controller.setObjectProperty(adaptee->id(), adaptee->kind(), DIAGRAM_CONTEXT, context);
231             return true;
232         }
233         else if (v->getType() == types::InternalType::ScilabDouble)
234         {
235             types::Double* current = v->getAs<types::Double>();
236             if (current->getRows() != 0 || current->getCols() != 0)
237             {
238                 return false;
239             }
240
241             model::Diagram* adaptee = adaptor.getAdaptee();
242
243             std::vector<std::string> context;
244             controller.setObjectProperty(adaptee->id(), adaptee->kind(), DIAGRAM_CONTEXT, context);
245             return true;
246         }
247         return false;
248     }
249 };
250
251 struct options
252 {
253
254     static types::InternalType* get(const ParamsAdapter& adaptor, const Controller& controller)
255     {
256         model::Diagram* adaptee = adaptor.getAdaptee();
257
258         // Return a dummy 'scsopt'-typed tlist.
259         types::String* header = new types::String(scsopt);
260
261         types::TList* Scsopt = new types::TList();
262         Scsopt->set(0, header);
263         return Scsopt;
264     }
265
266     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& controller)
267     {
268         // The model does not store 'options'.
269         return true;
270     }
271 };
272
273 struct doc
274 {
275     static types::InternalType* get(const ParamsAdapter& adaptor, const Controller& controller)
276     {
277         return adaptor.getDocContent();
278     }
279
280     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& controller)
281     {
282         adaptor.setDocContent(v->clone());
283         return true;
284     }
285 };
286
287 } /* namespace */
288
289 template<> property<ParamsAdapter>::props_t property<ParamsAdapter>::fields = property<ParamsAdapter>::props_t();
290
291 ParamsAdapter::ParamsAdapter(const ParamsAdapter& o) :
292     BaseAdapter<ParamsAdapter, org_scilab_modules_scicos::model::Diagram>(o) {}
293
294 ParamsAdapter::ParamsAdapter(org_scilab_modules_scicos::model::Diagram* o) :
295     BaseAdapter<ParamsAdapter, org_scilab_modules_scicos::model::Diagram>(o)
296 {
297     if (property<ParamsAdapter>::properties_have_not_been_set())
298     {
299         property<ParamsAdapter>::fields.reserve(10);
300         property<ParamsAdapter>::add_property(L"wpar", &wpar::get, &wpar::set);
301         property<ParamsAdapter>::add_property(L"title", &title::get, &title::set);
302         property<ParamsAdapter>::add_property(L"tol", &tol::get, &tol::set);
303         property<ParamsAdapter>::add_property(L"tf", &tf::get, &tf::set);
304         property<ParamsAdapter>::add_property(L"context", &context::get, &context::set);
305         property<ParamsAdapter>::add_property(L"void1", &wpar::get, &wpar::set);
306         property<ParamsAdapter>::add_property(L"options", &options::get, &options::set);
307         property<ParamsAdapter>::add_property(L"void2", &wpar::get, &wpar::set);
308         property<ParamsAdapter>::add_property(L"void3", &wpar::get, &wpar::set);
309         property<ParamsAdapter>::add_property(L"doc", &doc::get, &doc::set);
310     }
311
312     doc_content = new types::List();
313 }
314
315 ParamsAdapter::~ParamsAdapter()
316 {
317     delete doc_content;
318 }
319
320 std::wstring ParamsAdapter::getTypeStr()
321 {
322     return getSharedTypeStr();
323 }
324 std::wstring ParamsAdapter::getShortTypeStr()
325 {
326     return getSharedTypeStr();
327 }
328
329 types::InternalType* ParamsAdapter::getDocContent() const
330 {
331     return doc_content;
332 }
333
334 void ParamsAdapter::setDocContent(types::InternalType* v)
335 {
336     delete doc_content;
337     doc_content = v->clone();
338 }
339
340 } /* namespace view_scilab */
341 } /* namespace org_scilab_modules_scicos */