4240ccbb0f0c303e708a5cd97cdab39217ce5e4f
[scilab.git] / scilab / modules / scicos / src / cpp / view_scilab / LinkAdapter.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 "internal.hxx"
18 #include "list.hxx"
19 #include "types.hxx"
20 #include "user.hxx"
21 #include "double.hxx"
22
23 #include "Controller.hxx"
24 #include "LinkAdapter.hxx"
25
26 namespace org_scilab_modules_scicos
27 {
28 namespace view_scilab
29 {
30 namespace
31 {
32
33 struct xx
34 {
35
36     static types::InternalType* get(const LinkAdapter& adaptor, const Controller& controller)
37     {
38         model::Link* adaptee = adaptor.getAdaptee();
39
40         std::vector<double> controlPoints;
41         controller.getObjectProperty(adaptee->id(), adaptee->kind(), CONTROL_POINTS, controlPoints);
42
43         double* data;
44         types::Double* o = new types::Double(controlPoints.size() / 2, 1, &data);
45
46         std::copy(controlPoints.begin(), controlPoints.begin() + controlPoints.size() / 2, data);
47         return o;
48     }
49
50     static bool set(LinkAdapter& adaptor, types::InternalType* v, Controller& controller)
51     {
52         model::Link* adaptee = adaptor.getAdaptee();
53
54         if (v->getType() != types::InternalType::ScilabDouble)
55         {
56             return false;
57         }
58
59         types::Double* current = v->getAs<types::Double>();
60
61         std::vector<double> controlPoints;
62         controller.getObjectProperty(adaptee->id(), adaptee->kind(), CONTROL_POINTS, controlPoints);
63
64         if (current->getSize() != static_cast<int>(controlPoints.size() / 2))
65         {
66             return false;
67         }
68
69         std::copy(current->getReal(), current->getReal() + current->getSize(), controlPoints.begin());
70         controller.setObjectProperty(adaptee->id(), adaptee->kind(), CONTROL_POINTS, controlPoints);
71
72         return true;
73     }
74 };
75
76 struct yy
77 {
78
79     static types::InternalType* get(const LinkAdapter& adaptor, const Controller& controller)
80     {
81         model::Link* adaptee = adaptor.getAdaptee();
82
83         std::vector<double> controlPoints;
84         controller.getObjectProperty(adaptee->id(), adaptee->kind(), CONTROL_POINTS, controlPoints);
85
86         double* data;
87         types::Double* o = new types::Double(controlPoints.size() / 2, 1, &data);
88
89         std::copy(controlPoints.begin() + controlPoints.size() / 2, controlPoints.end(), data);
90         return o;
91     }
92
93     static bool set(LinkAdapter& adaptor, types::InternalType* v, Controller& controller)
94     {
95         model::Link* adaptee = adaptor.getAdaptee();
96
97         if (v->getType() != types::InternalType::ScilabDouble)
98         {
99             return false;
100         }
101
102         types::Double* current = v->getAs<types::Double>();
103
104         std::vector<double> controlPoints;
105         controller.getObjectProperty(adaptee->id(), adaptee->kind(), CONTROL_POINTS, controlPoints);
106
107         if (current->getSize() != static_cast<int>(controlPoints.size() / 2))
108         {
109             return false;
110         }
111
112         std::copy(current->getReal(), current->getReal() + current->getSize(), controlPoints.begin() + controlPoints.size() / 2);
113         controller.setObjectProperty(adaptee->id(), adaptee->kind(), CONTROL_POINTS, controlPoints);
114
115         return true;
116     }
117 };
118
119 struct id
120 {
121
122     static types::InternalType* get(const LinkAdapter& adaptor, const Controller& controller)
123     {
124         model::Link* adaptee = adaptor.getAdaptee();
125
126         std::string id;
127         controller.getObjectProperty(adaptee->id(), adaptee->kind(), LABEL, id);
128
129         types::String* o = new types::String(1, 1);
130         o->set(0, id.data());
131
132         return o;
133     }
134
135     static bool set(LinkAdapter& adaptor, types::InternalType* v, Controller& controller)
136     {
137         if (v->getType() != types::InternalType::ScilabString)
138         {
139             return false;
140         }
141
142         types::String* current = v->getAs<types::String>();
143         if (current->getSize() != 1)
144         {
145             return false;
146         }
147
148         model::Link* adaptee = adaptor.getAdaptee();
149
150         std::string id;
151         char* c_str = wide_string_to_UTF8(current->get(0));
152         id = std::string(c_str);
153         FREE(c_str);
154
155         controller.setObjectProperty(adaptee->id(), adaptee->kind(), LABEL, id);
156         return true;
157     }
158 };
159
160 struct thick
161 {
162
163     static types::InternalType* get(const LinkAdapter& adaptor, const Controller& controller)
164     {
165         model::Link* adaptee = adaptor.getAdaptee();
166
167         std::vector<double> thick;
168         controller.getObjectProperty(adaptee->id(), adaptee->kind(), THICK, thick);
169
170         double* data;
171         types::Double* o = new types::Double(1, 2, &data);
172
173         data[0] = thick[0];
174         data[1] = thick[1];
175         return o;
176     }
177
178     static bool set(LinkAdapter& adaptor, types::InternalType* v, Controller& controller)
179     {
180         model::Link* adaptee = adaptor.getAdaptee();
181
182         if (v->getType() != types::InternalType::ScilabDouble)
183         {
184             return false;
185         }
186
187         types::Double* current = v->getAs<types::Double>();
188         if (current->getRows() != 1 || current->getCols() != 2)
189         {
190             return false;
191         }
192
193         std::vector<double> thick (2);
194         thick[0] = current->get(0);
195         thick[1] = current->get(1);
196
197         controller.setObjectProperty(adaptee->id(), adaptee->kind(), THICK, thick);
198         return true;
199     }
200 };
201
202 struct ct
203 {
204
205     static types::InternalType* get(const LinkAdapter& adaptor, const Controller& controller)
206     {
207         model::Link* adaptee = adaptor.getAdaptee();
208
209         int color;
210         int kind;
211         controller.getObjectProperty(adaptee->id(), adaptee->kind(), COLOR, color);
212         controller.getObjectProperty(adaptee->id(), adaptee->kind(), KIND, kind);
213
214         double* data;
215         types::Double* o = new types::Double(1, 2, &data);
216
217         data[0] = static_cast<double>(color);
218         data[1] = static_cast<double>(kind);
219         return o;
220     }
221
222     static bool set(LinkAdapter& adaptor, types::InternalType* v, Controller& controller)
223     {
224         model::Link* adaptee = adaptor.getAdaptee();
225
226         if (v->getType() != types::InternalType::ScilabDouble)
227         {
228             return false;
229         }
230
231         types::Double* current = v->getAs<types::Double>();
232         if (current->getRows() != 1 || current->getCols() != 2)
233         {
234             return false;
235         }
236         if (floor(current->get(0)) != current->get(0) || floor(current->get(1)) != current->get(1))
237         {
238             return false;
239         }
240
241         int color = static_cast<int>(current->get(0));
242         int kind  = static_cast<int>(current->get(1));
243
244         controller.setObjectProperty(adaptee->id(), adaptee->kind(), COLOR, color);
245         controller.setObjectProperty(adaptee->id(), adaptee->kind(), KIND, kind);
246         return true;
247     }
248 };
249
250 } /* namespace */
251
252 template<> property<LinkAdapter>::props_t property<LinkAdapter>::fields = property<LinkAdapter>::props_t();
253
254 LinkAdapter::LinkAdapter(const LinkAdapter& o) :
255     BaseAdapter<LinkAdapter, org_scilab_modules_scicos::model::Link>(o) {}
256
257 LinkAdapter::LinkAdapter(org_scilab_modules_scicos::model::Link* o) :
258     BaseAdapter<LinkAdapter, org_scilab_modules_scicos::model::Link>(o)
259 {
260     if (property<LinkAdapter>::properties_has_not_been_set())
261     {
262         property<LinkAdapter>::fields.reserve(5);
263         property<LinkAdapter>::add_property(L"xx", &xx::get, &xx::set);
264         property<LinkAdapter>::add_property(L"yy", &yy::get, &yy::set);
265         property<LinkAdapter>::add_property(L"id", &id::get, &id::set);
266         property<LinkAdapter>::add_property(L"thick", &thick::get, &thick::set);
267         property<LinkAdapter>::add_property(L"ct", &ct::get, &ct::set);
268     }
269 }
270
271 LinkAdapter::~LinkAdapter()
272 {
273 }
274
275 std::wstring LinkAdapter::getTypeStr()
276 {
277     return getSharedTypeStr();
278 }
279 std::wstring LinkAdapter::getShortTypeStr()
280 {
281     return getSharedTypeStr();
282 }
283
284 } /* namespace view_scilab */
285 } /* namespace org_scilab_modules_scicos */