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