view_scilab: relax graphics.exprs checking
[scilab.git] / scilab / modules / scicos / src / cpp / view_scilab / GraphicsAdapter.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  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13  *
14  */
15
16 #include <cwchar>
17
18 #include <string>
19 #include <vector>
20 #include <iostream>
21 #include <iomanip>
22
23 #include "list.hxx"
24 #include "tlist.hxx"
25 #include "double.hxx"
26 #include "string.hxx"
27
28 #include "utilities.hxx"
29 #include "Controller.hxx"
30 #include "GraphicsAdapter.hxx"
31 #include "ports_management.hxx"
32 #include "controller_helpers.hxx"
33
34 #include "var2vec.hxx"
35 #include "vec2var.hxx"
36
37 extern "C" {
38 #include "localization.h"
39 #include "sci_malloc.h"
40 #include "charEncoding.h"
41 }
42
43 namespace org_scilab_modules_scicos
44 {
45 namespace view_scilab
46 {
47 namespace
48 {
49
50 const std::wstring MBLOCK (L"MBLOCK");
51 const std::wstring MPBLOCK (L"MPBLOCK");
52 const std::wstring in (L"in");
53 const std::wstring intype (L"intype");
54 const std::wstring out (L"out");
55 const std::wstring outtype (L"outtype");
56 const std::wstring param (L"param");
57 const std::wstring paramv (L"paramv");
58 const std::wstring pprop (L"pprop");
59 const std::wstring nameF (L"nameF");
60 const std::wstring funtxt (L"funtxt");
61
62 struct orig
63 {
64
65     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
66     {
67         double* data;
68         types::Double* o = new types::Double(1, 2, &data);
69         ScicosID adaptee = adaptor.getAdaptee()->id();
70
71         std::vector<double> geom;
72         controller.getObjectProperty(adaptee, BLOCK, GEOMETRY, geom);
73
74         data[0] = geom[0];
75         data[1] = geom[1];
76
77         return o;
78     }
79
80     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
81     {
82
83         if (v->getType() != types::InternalType::ScilabDouble)
84         {
85             get_or_allocate_logger()->log(LOG_ERROR, _("Wrong type for field %s.%s: Real matrix expected.\n"), "graphics", "orig");
86             return false;
87         }
88
89         types::Double* current = v->getAs<types::Double>();
90         if (current->getSize() != 2)
91         {
92             get_or_allocate_logger()->log(LOG_ERROR, _("Wrong dimension for field %s.%s: %d-by-%d expected.\n"), "graphics", "orig", 1, 2);
93             return false;
94         }
95
96         ScicosID adaptee = adaptor.getAdaptee()->id();
97         std::vector<double> geom;
98         controller.getObjectProperty(adaptee, BLOCK, GEOMETRY, geom);
99
100         geom[0] = current->get(0);
101         geom[1] = current->get(1);
102
103         controller.setObjectProperty(adaptee, BLOCK, GEOMETRY, geom);
104         return true;
105     }
106 };
107
108 struct sz
109 {
110
111     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
112     {
113         double* data;
114         types::Double* o = new types::Double(1, 2, &data);
115         ScicosID adaptee = adaptor.getAdaptee()->id();
116
117         std::vector<double> geom;
118         controller.getObjectProperty(adaptee, BLOCK, GEOMETRY, geom);
119
120         data[0] = geom[2];
121         data[1] = geom[3];
122         return o;
123     }
124
125     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
126     {
127         if (v->getType() != types::InternalType::ScilabDouble)
128         {
129             get_or_allocate_logger()->log(LOG_ERROR, _("Wrong type for field %s.%s: Real matrix expected.\n"), "graphics", "sz");
130             return false;
131         }
132
133         types::Double* current = v->getAs<types::Double>();
134         if (current->getSize() != 2)
135         {
136             get_or_allocate_logger()->log(LOG_ERROR, _("Wrong dimension for field %s.%s: %d-by-%d expected.\n"), "graphics", "sz", 1, 2);
137             return false;
138         }
139
140         ScicosID adaptee = adaptor.getAdaptee()->id();
141         std::vector<double> geom;
142         controller.getObjectProperty(adaptee, BLOCK, GEOMETRY, geom);
143
144         geom[2] = current->get(0);
145         geom[3] = current->get(1);
146
147         controller.setObjectProperty(adaptee, BLOCK, GEOMETRY, geom);
148         return true;
149     }
150 };
151
152 struct exprs
153 {
154
155     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
156     {
157         ScicosID adaptee = adaptor.getAdaptee()->id();
158
159         std::vector<double> exprs;
160         controller.getObjectProperty(adaptee, BLOCK, EXPRS, exprs);
161
162         types::InternalType* res;
163         if (!vec2var(exprs, res))
164         {
165             return 0;
166         }
167         return res;
168     }
169
170     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
171     {
172         ScicosID adaptee = adaptor.getAdaptee()->id();
173
174         // Corner-case the content is an empty matrix
175         if (v->getType() == types::InternalType::ScilabDouble)
176         {
177             types::Double* current = v->getAs<types::Double>();
178             if (!current->isEmpty())
179             {
180                 return false;
181             }
182         }
183
184         std::vector<double> exprs;
185         if (!var2vec(v, exprs))
186         {
187             return false;
188         }
189         controller.setObjectProperty(adaptee, BLOCK, EXPRS, exprs);
190         return true;
191     }
192 };
193
194 struct pin
195 {
196
197     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
198     {
199         return get_ports_property<GraphicsAdapter, CONNECTED_SIGNALS>(adaptor, INPUTS, controller);
200     }
201
202     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
203     {
204         return update_ports_property<GraphicsAdapter, CONNECTED_SIGNALS>(adaptor, INPUTS, controller, v);
205     }
206 };
207
208 struct pout
209 {
210
211     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
212     {
213         return get_ports_property<GraphicsAdapter, CONNECTED_SIGNALS>(adaptor, OUTPUTS, controller);
214     }
215
216     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
217     {
218         return update_ports_property<GraphicsAdapter, CONNECTED_SIGNALS>(adaptor, OUTPUTS, controller, v);
219     }
220 };
221
222 struct pein
223 {
224
225     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
226     {
227         return get_ports_property<GraphicsAdapter, CONNECTED_SIGNALS>(adaptor, EVENT_INPUTS, controller);
228     }
229
230     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
231     {
232         return update_ports_property<GraphicsAdapter, CONNECTED_SIGNALS>(adaptor, EVENT_INPUTS, controller, v);
233     }
234 };
235
236 struct peout
237 {
238
239     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
240     {
241         return get_ports_property<GraphicsAdapter, CONNECTED_SIGNALS>(adaptor, EVENT_OUTPUTS, controller);
242     }
243
244     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
245     {
246         return update_ports_property<GraphicsAdapter, CONNECTED_SIGNALS>(adaptor, EVENT_OUTPUTS, controller, v);
247     }
248 };
249
250 struct gr_i
251 {
252
253     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& /*controller*/)
254     {
255         return adaptor.getGrIContent();
256     }
257
258     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& /*controller*/)
259     {
260         adaptor.setGrIContent(v->clone());
261         return true;
262     }
263 };
264
265 struct id
266 {
267
268     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
269     {
270         ScicosID adaptee = adaptor.getAdaptee()->id();
271
272         std::string id;
273         controller.getObjectProperty(adaptee, BLOCK, DESCRIPTION, id);
274
275         types::String* o = new types::String(1, 1);
276         o->set(0, id.data());
277
278         return o;
279     }
280
281     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
282     {
283         if (v->getType() != types::InternalType::ScilabString)
284         {
285             get_or_allocate_logger()->log(LOG_ERROR, _("Wrong type for field %s.%s: string expected.\n"), "graphics", "id");
286             return false;
287         }
288
289         types::String* current = v->getAs<types::String>();
290         if (!current->isScalar())
291         {
292             get_or_allocate_logger()->log(LOG_ERROR, _("Wrong dimension for field %s.%s: %d-by-%d expected.\n"), "graphics", "id", 1, 1);
293             return false;
294         }
295
296         ScicosID adaptee = adaptor.getAdaptee()->id();
297
298         char* c_str = wide_string_to_UTF8(current->get(0));
299         std::string id(c_str);
300         FREE(c_str);
301
302         controller.setObjectProperty(adaptee, BLOCK, DESCRIPTION, id);
303         return true;
304     }
305 };
306
307 struct in_implicit
308 {
309
310     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
311     {
312         return get_ports_property<GraphicsAdapter, IMPLICIT>(adaptor, INPUTS, controller);
313     }
314
315     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
316     {
317         return set_ports_property<GraphicsAdapter, IMPLICIT>(adaptor, INPUTS, controller, v);
318     }
319 };
320
321 struct out_implicit
322 {
323
324     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
325     {
326         return get_ports_property<GraphicsAdapter, IMPLICIT>(adaptor, OUTPUTS, controller);
327     }
328
329     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
330     {
331         return set_ports_property<GraphicsAdapter, IMPLICIT>(adaptor, OUTPUTS, controller, v);
332     }
333 };
334
335 struct in_style
336 {
337
338     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
339     {
340         return get_ports_property<GraphicsAdapter, STYLE>(adaptor, INPUTS, controller);
341     }
342
343     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
344     {
345         return set_ports_property<GraphicsAdapter, STYLE>(adaptor, INPUTS, controller, v);
346     }
347 };
348
349 struct out_style
350 {
351
352     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
353     {
354         return get_ports_property<GraphicsAdapter, STYLE>(adaptor, OUTPUTS, controller);
355     }
356
357     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
358     {
359         return set_ports_property<GraphicsAdapter, STYLE>(adaptor, OUTPUTS, controller, v);
360     }
361 };
362
363 struct in_label
364 {
365
366     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
367     {
368         return get_ports_property<GraphicsAdapter, LABEL>(adaptor, INPUTS, controller);
369     }
370
371     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
372     {
373         return set_ports_property<GraphicsAdapter, LABEL>(adaptor, INPUTS, controller, v);
374     }
375 };
376
377 struct out_label
378 {
379
380     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
381     {
382         return get_ports_property<GraphicsAdapter, LABEL>(adaptor, OUTPUTS, controller);
383     }
384
385     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
386     {
387         return set_ports_property<GraphicsAdapter, LABEL>(adaptor, OUTPUTS, controller, v);
388     }
389 };
390
391 struct style
392 {
393
394     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
395     {
396         ScicosID adaptee = adaptor.getAdaptee()->id();
397
398         std::string style;
399         controller.getObjectProperty(adaptee, BLOCK, STYLE, style);
400
401         return new types::String(style.c_str());
402     }
403
404     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
405     {
406         ScicosID adaptee = adaptor.getAdaptee()->id();
407         if (v->getType() == types::InternalType::ScilabString)
408         {
409             types::String* current = v->getAs<types::String>();
410             if (!current->isScalar())
411             {
412                 get_or_allocate_logger()->log(LOG_ERROR, _("Wrong dimension for field %s.%s: %d-by-%d expected.\n"), "graphics", "style", 1, 1);
413                 return false;
414             }
415
416             char* c_str = wide_string_to_UTF8(current->get(0));
417             std::string style(c_str);
418             FREE(c_str);
419
420             controller.setObjectProperty(adaptee, BLOCK, STYLE, style);
421             return true;
422         }
423         else if (v->getType() == types::InternalType::ScilabDouble)
424         {
425             types::Double* current = v->getAs<types::Double>();
426             if (current->getSize() != 0)
427             {
428                 get_or_allocate_logger()->log(LOG_ERROR, _("Wrong type for field %s.%s: string expected.\n"), "graphics", "style");
429                 return false;
430             }
431
432             std::string style;
433             controller.setObjectProperty(adaptee, BLOCK, STYLE, style);
434             return true;
435         }
436
437         get_or_allocate_logger()->log(LOG_ERROR, _("Wrong type for field %s.%s: string expected.\n"), "graphics", "style");
438         return false;
439     }
440 };
441
442 } /* namespace */
443
444 template<> property<GraphicsAdapter>::props_t property<GraphicsAdapter>::fields = property<GraphicsAdapter>::props_t();
445 static void initialize_fields()
446 {
447     if (property<GraphicsAdapter>::properties_have_not_been_set())
448     {
449         property<GraphicsAdapter>::fields.reserve(16);
450         property<GraphicsAdapter>::add_property(L"orig", &orig::get, &orig::set);
451         property<GraphicsAdapter>::add_property(L"sz", &sz::get, &sz::set);
452         property<GraphicsAdapter>::add_property(L"exprs", &exprs::get, &exprs::set);
453         property<GraphicsAdapter>::add_property(L"pin", &pin::get, &pin::set);
454         property<GraphicsAdapter>::add_property(L"pout", &pout::get, &pout::set);
455         property<GraphicsAdapter>::add_property(L"pein", &pein::get, &pein::set);
456         property<GraphicsAdapter>::add_property(L"peout", &peout::get, &peout::set);
457         property<GraphicsAdapter>::add_property(L"gr_i", &gr_i::get, &gr_i::set);
458         property<GraphicsAdapter>::add_property(L"id", &id::get, &id::set);
459         property<GraphicsAdapter>::add_property(L"in_implicit", &in_implicit::get, &in_implicit::set);
460         property<GraphicsAdapter>::add_property(L"out_implicit", &out_implicit::get, &out_implicit::set);
461         property<GraphicsAdapter>::add_property(L"in_style", &in_style::get, &in_style::set);
462         property<GraphicsAdapter>::add_property(L"out_style", &out_style::get, &out_style::set);
463         property<GraphicsAdapter>::add_property(L"in_label", &in_label::get, &in_label::set);
464         property<GraphicsAdapter>::add_property(L"out_label", &out_label::get, &out_label::set);
465         property<GraphicsAdapter>::add_property(L"style", &style::get, &style::set);
466     }
467 }
468
469 GraphicsAdapter::GraphicsAdapter() :
470     BaseAdapter<GraphicsAdapter, org_scilab_modules_scicos::model::Block>(),
471     gr_i_content(types::Double::Empty())
472 {
473     initialize_fields();
474 }
475
476 GraphicsAdapter::GraphicsAdapter(const Controller& c, model::Block* adaptee) :
477     BaseAdapter<GraphicsAdapter, org_scilab_modules_scicos::model::Block>(c, adaptee),
478     gr_i_content(types::Double::Empty())
479 {
480     initialize_fields();
481 }
482
483 GraphicsAdapter::~GraphicsAdapter()
484 {
485     gr_i_content->DecreaseRef();
486     gr_i_content->killMe();
487 }
488
489 std::wstring GraphicsAdapter::getTypeStr()
490 {
491     return getSharedTypeStr();
492 }
493
494 std::wstring GraphicsAdapter::getShortTypeStr()
495 {
496     return getSharedTypeStr();
497 }
498
499 types::InternalType* GraphicsAdapter::getGrIContent() const
500 {
501     gr_i_content->IncreaseRef();
502     return gr_i_content;
503 }
504
505 void GraphicsAdapter::setGrIContent(types::InternalType* v)
506 {
507     gr_i_content->DecreaseRef();
508     gr_i_content->killMe();
509
510     v->IncreaseRef();
511     gr_i_content = v;
512 }
513
514 } /* namespace view_scilab */
515 } /* namespace org_scilab_modules_scicos */