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