138e24a3b500c34641a6e79602a1361582aac679
[scilab.git] / scilab / modules / scicos / src / cpp / view_scilab / DiagramAdapter.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 "internal.hxx"
18 #include "double.hxx"
19 #include "list.hxx"
20 #include "tlist.hxx"
21 #include "string.hxx"
22 #include "types.hxx"
23 #include "user.hxx"
24
25 #include "utilities.hxx"
26 #include "Controller.hxx"
27 #include "DiagramAdapter.hxx"
28 #include "ParamsAdapter.hxx"
29 #include "TextAdapter.hxx"
30 #include "BlockAdapter.hxx"
31 #include "LinkAdapter.hxx"
32 #include "model/BaseObject.hxx"
33
34 extern "C" {
35 #include "sci_malloc.h"
36 #include "charEncoding.h"
37 }
38
39 namespace org_scilab_modules_scicos
40 {
41 namespace view_scilab
42 {
43 namespace
44 {
45
46 const std::wstring AnnotationStr (L"Text");
47 const std::wstring BlockStr (L"Block");
48 const std::wstring LinkStr (L"Link");
49
50 struct props
51 {
52
53     static types::InternalType* get(const DiagramAdapter& adaptor, const Controller& controller)
54     {
55         ParamsAdapter localAdaptor = ParamsAdapter(adaptor.getAdaptee());
56         return localAdaptor.getAsTList(new types::TList(), controller);
57     }
58
59     static bool set(DiagramAdapter& adaptor, types::InternalType* v, Controller& controller)
60     {
61         ParamsAdapter localAdaptor = ParamsAdapter(adaptor.getAdaptee());
62         return localAdaptor.setAsTList(v, controller);
63     }
64 };
65
66 struct objs
67 {
68
69     static types::InternalType* get(const DiagramAdapter& adaptor, const Controller& controller)
70     {
71         // FIXME: get all children of the Diagram and return them as a list
72         model::Diagram* adaptee = adaptor.getAdaptee();
73
74         std::vector<ScicosID> children;
75         controller.getObjectProperty(adaptee->id(), adaptee->kind(), CHILDREN, children);
76
77         types::List* o = new types::List();
78
79         for (size_t i = 0; i < children.size(); ++i)
80         {
81             model::BaseObject* item = Controller().getObject(children[i]);
82             switch (item->kind())
83             {
84                 case ANNOTATION:
85                 {
86                     model::Annotation* annotation = static_cast<model::Annotation*>(item);
87                     TextAdapter localAdaptor = TextAdapter(annotation);
88                     o->set(i, localAdaptor.getAsTList(new types::MList(), controller));
89                     break;
90                 }
91                 case BLOCK:
92                 {
93                     model::Block* block = static_cast<model::Block*>(item);
94                     BlockAdapter localAdaptor = BlockAdapter(block);
95                     o->set(i, localAdaptor.getAsTList(new types::MList(), controller));
96                     break;
97                 }
98                 case LINK:
99                 {
100                     model::Link* link = static_cast<model::Link*>(item);
101                     LinkAdapter localAdaptor = LinkAdapter(link);
102                     o->set(i, localAdaptor.getAsTList(new types::MList(), controller));
103                     break;
104                 }
105                 default:
106                     return 0;
107             }
108         }
109         return o;
110     }
111
112     static bool set(DiagramAdapter& adaptor, types::InternalType* v, Controller& controller)
113     {
114         // FIXME implement, decode the list and set all children of the Diagram
115         if (v->getType() != types::InternalType::ScilabList)
116         {
117             return false;
118         }
119
120         model::Diagram* adaptee = adaptor.getAdaptee();
121
122         types::List* list = v->getAs<types::List>();
123         std::vector<ScicosID> diagramChildren (list->getSize());
124         for (int i = 0; i < list->getSize(); ++i)
125         {
126             if (list->get(i)->getType() != types::InternalType::ScilabMList)
127             {
128                 return false;
129             }
130             types::MList* modelElement = list->get(i)->getAs<types::MList>();
131
132             std::wstring modelElementType = modelElement->getTypeStr();
133             ScicosID id;
134
135             if (modelElementType == AnnotationStr)
136             {
137                 id = controller.createObject(ANNOTATION);
138
139                 TextAdapter localAdaptor = TextAdapter(static_cast<model::Annotation*>(controller.getObject(id)));
140                 if (!localAdaptor.setAsTList(modelElement, controller))
141                 {
142                     return false;
143                 }
144             }
145             else if (modelElementType == BlockStr)
146             {
147                 id = controller.createObject(BLOCK);
148
149                 BlockAdapter localAdaptor = BlockAdapter(static_cast<model::Block*>(controller.getObject(id)));
150                 if (!localAdaptor.setAsTList(modelElement, controller))
151                 {
152                     return false;
153                 }
154             }
155             else if (modelElementType == LinkStr)
156             {
157                 id = controller.createObject(LINK);
158
159                 LinkAdapter localAdaptor = LinkAdapter(static_cast<model::Link*>(controller.getObject(id)));
160                 if (!localAdaptor.setAsTList(modelElement, controller))
161                 {
162                     return false;
163                 }
164             }
165             else
166             {
167                 return false;
168             }
169
170             diagramChildren[i] = id;
171         }
172
173         controller.setObjectProperty(adaptee->id(), adaptee->kind(), CHILDREN, diagramChildren);
174         return true;
175     }
176 };
177
178 struct version
179 {
180
181     static types::InternalType* get(const DiagramAdapter& adaptor, const Controller& controller)
182     {
183         model::Diagram* adaptee = adaptor.getAdaptee();
184
185         std::string version;
186         controller.getObjectProperty(adaptee->id(), adaptee->kind(), VERSION_NUMBER, version);
187
188         return new types::String(version.data());
189     }
190
191     static bool set(DiagramAdapter& adaptor, types::InternalType* v, Controller& controller)
192     {
193         if (v->getType() == types::InternalType::ScilabString)
194         {
195             types::String* current = v->getAs<types::String>();
196             if (current->getSize() != 1)
197             {
198                 return false;
199             }
200
201             model::Diagram* adaptee = adaptor.getAdaptee();
202
203             char* c_str = wide_string_to_UTF8(current->get(0));
204             std::string version (c_str);
205             FREE(c_str);
206
207             controller.setObjectProperty(adaptee->id(), adaptee->kind(), VERSION_NUMBER, version);
208             return true;
209         }
210         else if (v->getType() == types::InternalType::ScilabDouble)
211         {
212             types::Double* current = v->getAs<types::Double>();
213             if (current->getSize() != 0)
214             {
215                 return false;
216             }
217
218             model::Diagram* adaptee = adaptor.getAdaptee();
219
220             std::string version;
221             controller.setObjectProperty(adaptee->id(), adaptee->kind(), VERSION_NUMBER, version);
222             return true;
223         }
224
225         return false;
226     }
227 };
228
229 struct contrib
230 {
231
232     static types::InternalType* get(const DiagramAdapter& adaptor, const Controller& controller)
233     {
234         // silent unused parameter warnings
235         (void) controller;
236
237         return adaptor.getContribContent();
238     }
239
240     static bool set(DiagramAdapter& adaptor, types::InternalType* v, Controller& controller)
241     {
242         // silent unused parameter warnings
243         (void) v;
244         (void) controller;
245
246         adaptor.setContribContent(v->clone());
247         return true;
248     }
249 };
250
251 } /* namespace */
252
253 template<> property<DiagramAdapter>::props_t property<DiagramAdapter>::fields = property<DiagramAdapter>::props_t();
254
255 DiagramAdapter::DiagramAdapter(const DiagramAdapter& o) :
256     BaseAdapter<DiagramAdapter, org_scilab_modules_scicos::model::Diagram>(o)
257 {
258     contrib_content = new types::List();
259 }
260
261 DiagramAdapter::DiagramAdapter(org_scilab_modules_scicos::model::Diagram* o) :
262     BaseAdapter<DiagramAdapter, org_scilab_modules_scicos::model::Diagram>(o)
263 {
264     if (property<DiagramAdapter>::properties_have_not_been_set())
265     {
266         property<DiagramAdapter>::fields.reserve(4);
267         property<DiagramAdapter>::add_property(L"props", &props::get, &props::set);
268         property<DiagramAdapter>::add_property(L"objs", &objs::get, &objs::set);
269         property<DiagramAdapter>::add_property(L"version", &version::get, &version::set);
270         property<DiagramAdapter>::add_property(L"contrib", &contrib::get, &contrib::set);
271     }
272
273     contrib_content = new types::List();
274 }
275
276 DiagramAdapter::~DiagramAdapter()
277 {
278     delete contrib_content;
279 }
280
281 std::wstring DiagramAdapter::getTypeStr()
282 {
283     return getSharedTypeStr();
284 }
285 std::wstring DiagramAdapter::getShortTypeStr()
286 {
287     return getSharedTypeStr();
288 }
289
290 types::InternalType* DiagramAdapter::getContribContent() const
291 {
292     return contrib_content;
293 }
294
295 void DiagramAdapter::setContribContent(types::InternalType* v)
296 {
297     delete contrib_content;
298     contrib_content = v->clone();
299 }
300
301 } /* namespace view_scilab */
302 } /* namespace org_scilab_modules_scicos */