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