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