Xcos MVC: implement firing / dep_ut / label / nzcross / nmode
[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  *  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-en.txt
10  *
11  */
12
13 #include <string>
14 #include <vector>
15 #include <cassert>
16
17 #include "list.hxx"
18 #include "double.hxx"
19 #include "string.hxx"
20
21 #include "Controller.hxx"
22 #include "GraphicsAdapter.hxx"
23 #include "ports_management.hxx"
24
25 extern "C" {
26 #include "sci_malloc.h"
27 }
28
29 namespace org_scilab_modules_scicos
30 {
31 namespace view_scilab
32 {
33 namespace
34 {
35
36 struct orig
37 {
38
39     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
40     {
41         double* data;
42         types::Double* o = new types::Double(1, 2, &data);
43         model::Block* adaptee = adaptor.getAdaptee();
44
45         std::vector<double> geom;
46         controller.getObjectProperty(adaptee->id(), adaptee->kind(), GEOMETRY, geom);
47
48         data[0] = geom[0];
49         data[1] = geom[1];
50
51         return o;
52     }
53
54     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
55     {
56
57         if (v->getType() != types::InternalType::ScilabDouble)
58         {
59             return false;
60         }
61
62         types::Double* current = v->getAs<types::Double>();
63         if (current->getRows() != 1 || current->getCols() != 2)
64         {
65             return false;
66         }
67
68         model::Block* adaptee = adaptor.getAdaptee();
69         std::vector<double> geom;
70         controller.getObjectProperty(adaptee->id(), adaptee->kind(), GEOMETRY, geom);
71
72         geom[0] = current->get(0);
73         geom[1] = current->get(1);
74
75         controller.setObjectProperty(adaptee->id(), adaptee->kind(), GEOMETRY, geom);
76         return true;
77     }
78 };
79
80 struct sz
81 {
82
83     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
84     {
85         double* data;
86         types::Double* o = new types::Double(1, 2, &data);
87         model::Block* adaptee = adaptor.getAdaptee();
88
89         std::vector<double> geom;
90         controller.getObjectProperty(adaptee->id(), adaptee->kind(), GEOMETRY, geom);
91
92         data[0] = geom[2];
93         data[1] = geom[3];
94         return o;
95     }
96
97     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
98     {
99         if (v->getType() != types::InternalType::ScilabDouble)
100         {
101             return false;
102         }
103
104         types::Double* current = v->getAs<types::Double>();
105         if (current->getRows() != 1 || current->getCols() != 2)
106         {
107             return false;
108         }
109
110         model::Block* adaptee = adaptor.getAdaptee();
111         std::vector<double> geom;
112         controller.getObjectProperty(adaptee->id(), adaptee->kind(), GEOMETRY, geom);
113
114         geom[2] = current->get(0);
115         geom[3] = current->get(1);
116
117         controller.setObjectProperty(adaptee->id(), adaptee->kind(), GEOMETRY, geom);
118         return true;
119     }
120 };
121
122 struct flip
123 {
124
125     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
126     {
127         int* data;
128         types::Bool* o = new types::Bool(1, 1, &data);
129         model::Block* adaptee = adaptor.getAdaptee();
130
131         std::vector<double> angle;
132         controller.getObjectProperty(adaptee->id(), adaptee->kind(), ANGLE, angle);
133
134         data[0] = static_cast<int>(angle[0]);
135         return o;
136     }
137
138     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
139     {
140         if (v->getType() != types::InternalType::ScilabBool)
141         {
142             return false;
143         }
144
145         types::Bool* current = v->getAs<types::Bool>();
146         if (current->isScalar() != true)
147         {
148             return false;
149         }
150
151         model::Block* adaptee = adaptor.getAdaptee();
152         std::vector<double> angle;
153         controller.getObjectProperty(adaptee->id(), adaptee->kind(), ANGLE, angle);
154
155         angle[0] = (current->get(0) == false) ? 0 : 1;
156
157         controller.setObjectProperty(adaptee->id(), adaptee->kind(), ANGLE, angle);
158         return true;
159     }
160 };
161
162 struct theta
163 {
164
165     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
166     {
167         double* data;
168         types::Double* o = new types::Double(1, 1, &data);
169         model::Block* adaptee = adaptor.getAdaptee();
170
171         std::vector<double> angle;
172         controller.getObjectProperty(adaptee->id(), adaptee->kind(), ANGLE, angle);
173
174         data[0] = angle[1];
175         return o;
176     }
177
178     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
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->isScalar() != true)
187         {
188             return false;
189         }
190
191         model::Block* adaptee = adaptor.getAdaptee();
192         std::vector<double> angle;
193         controller.getObjectProperty(adaptee->id(), adaptee->kind(), ANGLE, angle);
194
195         angle[1] = current->get(0);
196
197         controller.setObjectProperty(adaptee->id(), adaptee->kind(), ANGLE, angle);
198         return true;
199     }
200 };
201
202 struct exprs
203 {
204
205     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
206     {
207         model::Block* adaptee = adaptor.getAdaptee();
208
209         std::vector<std::string> exprs;
210         controller.getObjectProperty(adaptee->id(), adaptee->kind(), EXPRS, exprs);
211
212         types::String* o = new types::String(exprs.size(), 1);
213         for (size_t i = 0; i < exprs.size(); ++i)
214         {
215             o->set(i, exprs[i].data());
216         }
217
218         return o;
219     }
220
221     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
222     {
223         if (v->getType() == types::InternalType::ScilabString)
224         {
225             types::String* current = v->getAs<types::String>();
226             if (current->getCols() != 0 && current->getCols() != 1)
227             {
228                 return false;
229             }
230
231             model::Block* adaptee = adaptor.getAdaptee();
232             std::vector<std::string> exprs = std::vector<std::string>(current->getSize());
233             for (size_t i = 0; i < exprs.size(); ++i)
234             {
235                 char* c_str = wide_string_to_UTF8(current->get(i));
236                 exprs[i] = std::string(c_str);
237                 FREE(c_str);
238             }
239
240             controller.setObjectProperty(adaptee->id(), adaptee->kind(), EXPRS, exprs);
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::Block* adaptee = adaptor.getAdaptee();
252             std::vector<std::string> exprs;
253             controller.setObjectProperty(adaptee->id(), adaptee->kind(), EXPRS, exprs);
254             return true;
255         }
256         return false;
257     }
258 };
259
260 struct pin
261 {
262
263     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
264     {
265         return get_ports_property<GraphicsAdapter, CONNECTED_SIGNALS>(adaptor, INPUTS, controller);
266     }
267
268     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
269     {
270         return update_ports_property<GraphicsAdapter, CONNECTED_SIGNALS>(adaptor, INPUTS, controller, v);
271     }
272 };
273
274 struct pout
275 {
276
277     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
278     {
279         return get_ports_property<GraphicsAdapter, CONNECTED_SIGNALS>(adaptor, OUTPUTS, controller);
280     }
281
282     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
283     {
284         return update_ports_property<GraphicsAdapter, CONNECTED_SIGNALS>(adaptor, OUTPUTS, controller, v);
285     }
286 };
287
288 struct pein
289 {
290
291     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
292     {
293         return get_ports_property<GraphicsAdapter, CONNECTED_SIGNALS>(adaptor, EVENT_INPUTS, controller);
294     }
295
296     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
297     {
298         return update_ports_property<GraphicsAdapter, CONNECTED_SIGNALS>(adaptor, EVENT_INPUTS, controller, v);
299     }
300 };
301
302 struct peout
303 {
304
305     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
306     {
307         return get_ports_property<GraphicsAdapter, CONNECTED_SIGNALS>(adaptor, EVENT_OUTPUTS, controller);
308     }
309
310     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
311     {
312         return update_ports_property<GraphicsAdapter, CONNECTED_SIGNALS>(adaptor, EVENT_OUTPUTS, controller, v);
313     }
314 };
315
316 struct gr_i
317 {
318
319     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
320     {
321         return adaptor.getGrIContent();
322     }
323
324     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
325     {
326         adaptor.setGrIContent(v->clone());
327         return true;
328     }
329 };
330
331 struct id
332 {
333
334     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
335     {
336         model::Block* adaptee = adaptor.getAdaptee();
337
338         std::string id;
339         controller.getObjectProperty(adaptee->id(), adaptee->kind(), LABEL, id);
340
341         types::String* o = new types::String(1, 1);
342         o->set(0, id.data());
343
344         return o;
345     }
346
347     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
348     {
349         if (v->getType() != types::InternalType::ScilabString)
350         {
351             return false;
352         }
353
354         types::String* current = v->getAs<types::String>();
355         if (current->getSize() != 1)
356         {
357             return false;
358         }
359
360         model::Block* adaptee = adaptor.getAdaptee();
361
362         std::string id;
363         char* c_str = wide_string_to_UTF8(current->get(0));
364         id = std::string(c_str);
365         FREE(c_str);
366
367         controller.setObjectProperty(adaptee->id(), adaptee->kind(), LABEL, id);
368         return true;
369     }
370 };
371
372 struct in_implicit
373 {
374
375     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
376     {
377         return get_ports_property<GraphicsAdapter, IMPLICIT>(adaptor, INPUTS, controller);
378     }
379
380     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
381     {
382         return set_ports_property<GraphicsAdapter, IMPLICIT>(adaptor, INPUTS, controller, v);
383     }
384 };
385
386 struct out_implicit
387 {
388
389     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
390     {
391         return get_ports_property<GraphicsAdapter, IMPLICIT>(adaptor, OUTPUTS, controller);
392     }
393
394     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
395     {
396         return set_ports_property<GraphicsAdapter, IMPLICIT>(adaptor, OUTPUTS, controller, v);
397     }
398 };
399
400 struct in_style
401 {
402
403     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
404     {
405         return get_ports_property<GraphicsAdapter, STYLE>(adaptor, INPUTS, controller);
406     }
407
408     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
409     {
410         return set_ports_property<GraphicsAdapter, STYLE>(adaptor, INPUTS, controller, v);
411     }
412 };
413
414 struct out_style
415 {
416
417     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
418     {
419         return get_ports_property<GraphicsAdapter, STYLE>(adaptor, OUTPUTS, controller);
420     }
421
422     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
423     {
424         return set_ports_property<GraphicsAdapter, STYLE>(adaptor, OUTPUTS, controller, v);
425     }
426 };
427
428 struct in_label
429 {
430
431     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
432     {
433         return get_ports_property<GraphicsAdapter, LABEL>(adaptor, INPUTS, controller);
434     }
435
436     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
437     {
438         return set_ports_property<GraphicsAdapter, LABEL>(adaptor, INPUTS, controller, v);
439     }
440 };
441
442 struct out_label
443 {
444
445     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
446     {
447         return get_ports_property<GraphicsAdapter, LABEL>(adaptor, OUTPUTS, controller);
448     }
449
450     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
451     {
452         return set_ports_property<GraphicsAdapter, LABEL>(adaptor, OUTPUTS, controller, v);
453     }
454 };
455
456 struct style
457 {
458
459     static types::InternalType* get(const GraphicsAdapter& adaptor, const Controller& controller)
460     {
461         model::Block* adaptee = adaptor.getAdaptee();
462
463         std::string style;
464         controller.getObjectProperty(adaptee->id(), adaptee->kind(), STYLE, style);
465
466         types::String* o;
467         if (style.empty())
468         {
469             o = new types::String(0, 0);
470         }
471         else
472         {
473             o = new types::String(1, 1);
474             o->set(0, style.data());
475         }
476         return o;
477     }
478
479     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
480     {
481         model::Block* adaptee = adaptor.getAdaptee();
482         if (v->getType() == types::InternalType::ScilabString)
483         {
484             types::String* current = v->getAs<types::String>();
485             if (current->getSize() != 1)
486             {
487                 return false;
488             }
489
490             std::string style;
491             char* c_str = wide_string_to_UTF8(current->get(0));
492             style = std::string(c_str);
493             FREE(c_str);
494
495             controller.setObjectProperty(adaptee->id(), adaptee->kind(), STYLE, style);
496             return true;
497         }
498         else if (v->getType() == types::InternalType::ScilabDouble)
499         {
500             types::Double* current = v->getAs<types::Double>();
501             if (current->getSize() != 0)
502             {
503                 return false;
504             }
505
506             std::string style;
507             controller.setObjectProperty(adaptee->id(), adaptee->kind(), STYLE, style);
508             return true;
509         }
510         return false;
511     }
512 };
513
514 } /* namespace */
515
516 template<> property<GraphicsAdapter>::props_t property<GraphicsAdapter>::fields = property<GraphicsAdapter>::props_t();
517
518 GraphicsAdapter::GraphicsAdapter(const GraphicsAdapter& o) :
519     BaseAdapter<GraphicsAdapter, org_scilab_modules_scicos::model::Block>(o),
520     gr_i_content(o.gr_i_content->clone())
521 {}
522
523 GraphicsAdapter::GraphicsAdapter(org_scilab_modules_scicos::model::Block* o) :
524     BaseAdapter<GraphicsAdapter, org_scilab_modules_scicos::model::Block>(o)
525 {
526     if (property<GraphicsAdapter>::properties_has_not_been_set())
527     {
528         property<GraphicsAdapter>::fields.reserve(18);
529         property<GraphicsAdapter>::add_property(L"orig", &orig::get, &orig::set);
530         property<GraphicsAdapter>::add_property(L"sz", &sz::get, &sz::set);
531         property<GraphicsAdapter>::add_property(L"flip", &flip::get, &flip::set);
532         property<GraphicsAdapter>::add_property(L"theta", &theta::get, &theta::set);
533         property<GraphicsAdapter>::add_property(L"exprs", &exprs::get, &exprs::set);
534         property<GraphicsAdapter>::add_property(L"pin", &pin::get, &pin::set);
535         property<GraphicsAdapter>::add_property(L"pout", &pout::get, &pout::set);
536         property<GraphicsAdapter>::add_property(L"pein", &pein::get, &pein::set);
537         property<GraphicsAdapter>::add_property(L"peout", &peout::get, &peout::set);
538         property<GraphicsAdapter>::add_property(L"gr_i", &gr_i::get, &gr_i::set);
539         property<GraphicsAdapter>::add_property(L"id", &id::get, &id::set);
540         property<GraphicsAdapter>::add_property(L"in_implicit", &in_implicit::get, &in_implicit::set);
541         property<GraphicsAdapter>::add_property(L"out_implicit", &out_implicit::get, &out_implicit::set);
542         property<GraphicsAdapter>::add_property(L"in_style", &in_style::get, &in_style::set);
543         property<GraphicsAdapter>::add_property(L"out_style", &out_style::get, &out_style::set);
544         property<GraphicsAdapter>::add_property(L"in_label", &in_label::get, &in_label::set);
545         property<GraphicsAdapter>::add_property(L"out_label", &out_label::get, &out_label::set);
546         property<GraphicsAdapter>::add_property(L"style", &style::get, &style::set);
547     }
548
549     gr_i_content = new types::List();
550 }
551
552 GraphicsAdapter::~GraphicsAdapter()
553 {
554     delete gr_i_content;
555 }
556
557 std::wstring GraphicsAdapter::getTypeStr()
558 {
559     return getSharedTypeStr();
560 }
561
562 std::wstring GraphicsAdapter::getShortTypeStr()
563 {
564     return getSharedTypeStr();
565 }
566
567 types::InternalType* GraphicsAdapter::getGrIContent() const
568 {
569     return gr_i_content;
570 }
571
572 void GraphicsAdapter::setGrIContent(types::InternalType* v)
573 {
574     delete gr_i_content;
575     gr_i_content = v->clone();
576 }
577
578 } /* namespace view_scilab */
579 } /* namespace org_scilab_modules_scicos */