Xcos MVC: handle graphics and model port creation
[scilab.git] / scilab / modules / scicos / src / cpp / view_scilab / ModelAdapter.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 "list.hxx"
14 #include "double.hxx"
15 #include "string.hxx"
16
17 #include "Controller.hxx"
18 #include "ModelAdapter.hxx"
19 #include "ports_management.hxx"
20
21 namespace org_scilab_modules_scicos
22 {
23 namespace view_scilab
24 {
25
26 struct sim
27 {
28
29     static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
30     {
31         model::Block* adaptee = adaptor.getAdaptee();
32
33         // First, extact the function Name
34         std::string name;
35         controller.getObjectProperty(adaptee->id(), adaptee->kind(), SIM_FUNCTION_NAME, name);
36         types::String* Name = new types::String(1, 1);
37         Name->set(0, name.data());
38
39         // Then the Api. If it is zero, then just return the Name. Otherwise, return a list containing both.
40         int api;
41         controller.getObjectProperty(adaptee->id(), adaptee->kind(), SIM_FUNCTION_API, api);
42
43         if (api == 0)
44         {
45             return Name;
46         }
47         else
48         {
49             types::Double* Api = new types::Double(1, 1, (double) api);
50             types::List* o = new types::List();
51             o->set(0, Name);
52             o->set(1, Api);
53             return o;
54         }
55     }
56
57     static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
58     {
59         model::Block* adaptee = adaptor.getAdaptee();
60
61         if (v->getType() == types::InternalType::ScilabString)
62         {
63             types::String* current = v->getAs<types::String>();
64             if (current->getSize() != 1)
65             {
66                 return false;
67             }
68
69             char* c_str = wide_string_to_UTF8(current->get(0));
70             std::string name = std::string(c_str);
71             FREE(c_str);
72
73             // If the input is a scalar string, then the functionApi is 0.
74             int api = 0;
75
76             controller.setObjectProperty(adaptee->id(), adaptee->kind(), SIM_FUNCTION_NAME, name);
77             controller.setObjectProperty(adaptee->id(), adaptee->kind(), SIM_FUNCTION_API, api);
78         }
79         else if (v->getType() == types::InternalType::ScilabList)
80         {
81             // If the input is a 2-sized list, then it must be string and positive integer.
82             types::List* current = v->getAs<types::List>();
83             if (current->getSize() != 2)
84             {
85                 return false;
86             }
87             if (current->get(0)->getType() != types::InternalType::ScilabString || current->get(1)->getType() != types::InternalType::ScilabDouble)
88             {
89                 return false;
90             }
91
92             types::String* Name = current->get(0)->getAs<types::String>();
93             if (Name->getSize() != 1)
94             {
95                 return false;
96             }
97             char* c_str = wide_string_to_UTF8(Name->get(0));
98             std::string name = std::string(c_str);
99             FREE(c_str);
100
101             types::Double* Api = current->get(1)->getAs<types::Double>();
102             if (Api->getSize() != 1)
103             {
104                 return false;
105             }
106             double api = Api->get(0);
107             if (floor(api) != api)
108             {
109                 return false;
110             }
111             int api_int = (int) api;
112
113             controller.setObjectProperty(adaptee->id(), adaptee->kind(), SIM_FUNCTION_NAME, name);
114             controller.setObjectProperty(adaptee->id(), adaptee->kind(), SIM_FUNCTION_API, api_int);
115         }
116         else
117         {
118             return false;
119         }
120         return true;
121     }
122 };
123
124
125 struct in
126 {
127
128     static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
129     {
130         return get_ports_property<ModelAdapter, DATATYPE_ROWS>(adaptor, INPUTS, controller);
131     }
132
133     static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
134     {
135         return update_ports_property<ModelAdapter, DATATYPE_ROWS>(adaptor, INPUTS, controller, v);
136     }
137 };
138
139 struct in2
140 {
141
142     static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
143     {
144         return get_ports_property<ModelAdapter, DATATYPE_COLS>(adaptor, INPUTS, controller);
145     }
146
147     static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
148     {
149         return set_ports_property<ModelAdapter, DATATYPE_COLS>(adaptor, INPUTS, controller, v);
150     }
151 };
152
153 struct intyp
154 {
155
156     static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
157     {
158         return get_ports_property<ModelAdapter, DATATYPE_TYPE>(adaptor, INPUTS, controller);
159     }
160
161     static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
162     {
163         return set_ports_property<ModelAdapter, DATATYPE_TYPE>(adaptor, INPUTS, controller, v);
164     }
165 };
166
167
168 struct out
169 {
170
171     static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
172     {
173         return get_ports_property<ModelAdapter, DATATYPE_ROWS>(adaptor, OUTPUTS, controller);
174     }
175
176     static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
177     {
178         return update_ports_property<ModelAdapter, DATATYPE_ROWS>(adaptor, OUTPUTS, controller, v);
179     }
180 };
181
182 struct out2
183 {
184
185     static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
186     {
187         return get_ports_property<ModelAdapter, DATATYPE_COLS>(adaptor, OUTPUTS, controller);
188     }
189
190     static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
191     {
192         return set_ports_property<ModelAdapter, DATATYPE_COLS>(adaptor, OUTPUTS, controller, v);
193     }
194 };
195
196 struct outtyp
197 {
198
199     static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
200     {
201         return get_ports_property<ModelAdapter, DATATYPE_TYPE>(adaptor, OUTPUTS, controller);
202     }
203
204     static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
205     {
206         return set_ports_property<ModelAdapter, DATATYPE_TYPE>(adaptor, OUTPUTS, controller, v);
207     }
208 };
209
210 struct evtin
211 {
212
213     static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
214     {
215         return get_ports_property<ModelAdapter, DATATYPE_ROWS>(adaptor, EVENT_INPUTS, controller);
216     }
217
218     static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
219     {
220         return update_ports_property<ModelAdapter, DATATYPE_ROWS>(adaptor, EVENT_INPUTS, controller, v);
221     }
222 };
223
224 struct evtout
225 {
226
227     static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
228     {
229         return get_ports_property<ModelAdapter, DATATYPE_ROWS>(adaptor, EVENT_OUTPUTS, controller);
230     }
231
232     static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
233     {
234         return update_ports_property<ModelAdapter, DATATYPE_ROWS>(adaptor, EVENT_OUTPUTS, controller, v);
235     }
236 };
237
238 template<> property<ModelAdapter>::props_t property<ModelAdapter>::fields = property<ModelAdapter>::props_t();
239
240 ModelAdapter::ModelAdapter(const ModelAdapter& o) :
241     BaseAdapter<ModelAdapter, org_scilab_modules_scicos::model::Block>(o) { }
242
243 ModelAdapter::ModelAdapter(org_scilab_modules_scicos::model::Block* o) :
244     BaseAdapter<ModelAdapter, org_scilab_modules_scicos::model::Block>(o)
245 {
246     if (property<ModelAdapter>::properties_has_not_been_set())
247     {
248         property<ModelAdapter>::fields.reserve(7);
249         property<ModelAdapter>::add_property(L"sim", &sim::get, &sim::set);
250         property<ModelAdapter>::add_property(L"in", &in::get, &in::set);
251         property<ModelAdapter>::add_property(L"in2", &in2::get, &in2::set);
252         property<ModelAdapter>::add_property(L"intyp", &intyp::get, &intyp::set);
253         property<ModelAdapter>::add_property(L"out", &out::get, &out::set);
254         property<ModelAdapter>::add_property(L"out2", &out2::get, &out2::set);
255         property<ModelAdapter>::add_property(L"outtyp", &outtyp::get, &outtyp::set);
256         property<ModelAdapter>::add_property(L"evtin", &evtin::get, &evtin::set);
257         property<ModelAdapter>::add_property(L"evtout", &evtout::get, &evtout::set);
258     }
259 }
260
261 ModelAdapter::~ModelAdapter()
262 {
263 }
264
265 bool ModelAdapter::toString(std::wostringstream& ostr)
266 {
267     ostr << L"ModelAdapter.hxx: Dunno what to display there" << std::endl;
268     return true;
269 }
270
271 std::wstring ModelAdapter::getTypeStr()
272 {
273     return getSharedTypeStr();
274 }
275
276 std::wstring ModelAdapter::getShortTypeStr()
277 {
278     return getSharedTypeStr();
279 }
280
281 } /* view_scilab */
282 } /* namespace org_scilab_modules_scicos */