2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2014-2014 - Scilab Enterprises - Clement DAVID
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
17 #include "internal.hxx"
23 #include "Controller.hxx"
24 #include "LinkAdapter.hxx"
26 namespace org_scilab_modules_scicos
36 static types::InternalType* get(const LinkAdapter& adaptor, const Controller& controller)
38 model::Link* adaptee = adaptor.getAdaptee();
40 std::vector<double> controlPoints;
41 controller.getObjectProperty(adaptee->id(), adaptee->kind(), CONTROL_POINTS, controlPoints);
44 types::Double* o = new types::Double(controlPoints.size() / 2, 1, &data);
46 std::copy(controlPoints.begin(), controlPoints.begin() + controlPoints.size() / 2, data);
50 static bool set(LinkAdapter& adaptor, types::InternalType* v, Controller& controller)
52 model::Link* adaptee = adaptor.getAdaptee();
54 if (v->getType() != types::InternalType::ScilabDouble)
59 types::Double* current = v->getAs<types::Double>();
61 std::vector<double> controlPoints;
62 controller.getObjectProperty(adaptee->id(), adaptee->kind(), CONTROL_POINTS, controlPoints);
64 if (current->getSize() != static_cast<int>(controlPoints.size() / 2))
69 std::copy(current->getReal(), current->getReal() + current->getSize(), controlPoints.begin());
70 controller.setObjectProperty(adaptee->id(), adaptee->kind(), CONTROL_POINTS, controlPoints);
79 static types::InternalType* get(const LinkAdapter& adaptor, const Controller& controller)
81 model::Link* adaptee = adaptor.getAdaptee();
83 std::vector<double> controlPoints;
84 controller.getObjectProperty(adaptee->id(), adaptee->kind(), CONTROL_POINTS, controlPoints);
87 types::Double* o = new types::Double(controlPoints.size() / 2, 1, &data);
89 std::copy(controlPoints.begin() + controlPoints.size() / 2, controlPoints.end(), data);
93 static bool set(LinkAdapter& adaptor, types::InternalType* v, Controller& controller)
95 model::Link* adaptee = adaptor.getAdaptee();
97 if (v->getType() != types::InternalType::ScilabDouble)
102 types::Double* current = v->getAs<types::Double>();
104 std::vector<double> controlPoints;
105 controller.getObjectProperty(adaptee->id(), adaptee->kind(), CONTROL_POINTS, controlPoints);
107 if (current->getSize() != static_cast<int>(controlPoints.size() / 2))
112 std::copy(current->getReal(), current->getReal() + current->getSize(), controlPoints.begin() + controlPoints.size() / 2);
113 controller.setObjectProperty(adaptee->id(), adaptee->kind(), CONTROL_POINTS, controlPoints);
122 static types::InternalType* get(const LinkAdapter& adaptor, const Controller& controller)
124 model::Link* adaptee = adaptor.getAdaptee();
127 controller.getObjectProperty(adaptee->id(), adaptee->kind(), LABEL, id);
129 types::String* o = new types::String(1, 1);
130 o->set(0, id.data());
135 static bool set(LinkAdapter& adaptor, types::InternalType* v, Controller& controller)
137 if (v->getType() != types::InternalType::ScilabString)
142 types::String* current = v->getAs<types::String>();
143 if (current->getSize() != 1)
148 model::Link* adaptee = adaptor.getAdaptee();
151 char* c_str = wide_string_to_UTF8(current->get(0));
152 id = std::string(c_str);
155 controller.setObjectProperty(adaptee->id(), adaptee->kind(), LABEL, id);
163 static types::InternalType* get(const LinkAdapter& adaptor, const Controller& controller)
165 model::Link* adaptee = adaptor.getAdaptee();
167 std::vector<double> thick;
168 controller.getObjectProperty(adaptee->id(), adaptee->kind(), THICK, thick);
171 types::Double* o = new types::Double(1, 2, &data);
178 static bool set(LinkAdapter& adaptor, types::InternalType* v, Controller& controller)
180 model::Link* adaptee = adaptor.getAdaptee();
182 if (v->getType() != types::InternalType::ScilabDouble)
187 types::Double* current = v->getAs<types::Double>();
188 if (current->getRows() != 1 || current->getCols() != 2)
193 std::vector<double> thick (2);
194 thick[0] = current->get(0);
195 thick[1] = current->get(1);
197 controller.setObjectProperty(adaptee->id(), adaptee->kind(), THICK, thick);
205 static types::InternalType* get(const LinkAdapter& adaptor, const Controller& controller)
207 model::Link* adaptee = adaptor.getAdaptee();
211 controller.getObjectProperty(adaptee->id(), adaptee->kind(), COLOR, color);
212 controller.getObjectProperty(adaptee->id(), adaptee->kind(), KIND, kind);
215 types::Double* o = new types::Double(1, 2, &data);
217 data[0] = static_cast<double>(color);
218 data[1] = static_cast<double>(kind);
222 static bool set(LinkAdapter& adaptor, types::InternalType* v, Controller& controller)
224 model::Link* adaptee = adaptor.getAdaptee();
226 if (v->getType() != types::InternalType::ScilabDouble)
231 types::Double* current = v->getAs<types::Double>();
232 if (current->getRows() != 1 || current->getCols() != 2)
236 if (floor(current->get(0)) != current->get(0) || floor(current->get(1)) != current->get(1))
241 int color = static_cast<int>(current->get(0));
242 int kind = static_cast<int>(current->get(1));
244 controller.setObjectProperty(adaptee->id(), adaptee->kind(), COLOR, color);
245 controller.setObjectProperty(adaptee->id(), adaptee->kind(), KIND, kind);
250 static types::Double* getLinkEnd(const LinkAdapter& adaptor, const Controller& controller, object_properties_t end)
252 model::Link* adaptee = adaptor.getAdaptee();
255 types::Double* o = new types::Double(1, 3, &data);
261 controller.getObjectProperty(adaptee->id(), adaptee->kind(), end, endID);
264 ScicosID sourceBlock;
265 controller.getObjectProperty(endID, PORT, SOURCE_BLOCK, sourceBlock);
267 controller.getObjectProperty(endID, PORT, PORT_KIND, kind);
269 data[0] = static_cast<double>(sourceBlock);
270 data[1] = static_cast<double>(endID);
271 data[2] = static_cast<double>(kind);
273 // Default case, the property was initialized at [].
277 static bool setLinkEnd(LinkAdapter& adaptor, Controller& controller, object_properties_t end, types::InternalType* v)
279 model::Link* adaptee = adaptor.getAdaptee();
281 if (v->getType() != types::InternalType::ScilabDouble)
286 types::Double* current = v->getAs<types::Double>();
288 if ((current->getRows() != 1 || current->getCols() != 3) && current->getSize() != 0)
290 return false; // Must be [] or [x y z]
294 controller.getObjectProperty(adaptee->id(), adaptee->kind(), SOURCE_PORT, from);
296 controller.getObjectProperty(adaptee->id(), adaptee->kind(), DESTINATION_PORT, to);
297 ScicosID concernedPort;
298 object_properties_t otherEnd;
299 object_properties_t portType;
303 concernedPort = from;
304 otherEnd = DESTINATION_PORT;
307 case DESTINATION_PORT:
309 otherEnd = SOURCE_PORT;
315 ScicosID unconnected = 0;
317 if (current->getSize() == 0 || (current->get(0) == 0 || current->get(1) == 0))
319 // We want to set an empty link
320 if (concernedPort == 0)
322 // In this case, the link was already empty, do a dummy call to display the console status.
323 controller.setObjectProperty(adaptee->id(), adaptee->kind(), end, concernedPort);
327 // Untie the old link on both ends and set the 2 concerned ports as unconnected
328 controller.setObjectProperty(from, PORT, CONNECTED_SIGNALS, unconnected);
329 controller.setObjectProperty(to, PORT, CONNECTED_SIGNALS, unconnected);
331 controller.setObjectProperty(adaptee->id(), adaptee->kind(), SOURCE_PORT, unconnected);
332 controller.setObjectProperty(adaptee->id(), adaptee->kind(), DESTINATION_PORT, unconnected);
337 if (current->get(2) != 0 && current->get(2) != 1)
339 return false; // "From" port must be output type or implicit.
342 if (floor(current->get(0)) != current->get(0) || floor(current->get(1)) != current->get(1))
344 return false; // Must be an integer value
347 // Disconnect the old port if it was connected
350 controller.setObjectProperty(concernedPort, PORT, CONNECTED_SIGNALS, unconnected);
353 // Connect the new one
354 int blk = static_cast<int>(current->get(0));
355 int port = static_cast<int>(current->get(1));
356 int kind = static_cast<int>(current->get(2));
357 std::vector<ScicosID> sourceBlockPorts;
358 controller.getObjectProperty(blk, BLOCK, portType, sourceBlockPorts);
359 int nBlockPorts = sourceBlockPorts.size();
360 // Create as many ports as necessary
361 while (nBlockPorts < port)
363 ScicosID createdPort = controller.createObject(PORT);
364 controller.setObjectProperty(createdPort, PORT, SOURCE_BLOCK, blk);
365 controller.setObjectProperty(createdPort, PORT, CONNECTED_SIGNALS, unconnected);
368 controller.getObjectProperty(blk, BLOCK, portType, sourceBlockPorts);
369 ScicosID newPort = sourceBlockPorts[port - 1];
371 controller.getObjectProperty(newPort, PORT, CONNECTED_SIGNALS, oldLink);
374 // Disconnect the old other end port and delete the old link
376 controller.getObjectProperty(oldLink, LINK, otherEnd, oldPort);
377 controller.setObjectProperty(oldPort, PORT, CONNECTED_SIGNALS, unconnected);
378 controller.deleteObject(oldLink);
381 // Connect the new source and destination ports together
382 controller.setObjectProperty(newPort, PORT, PORT_KIND, kind);
383 controller.setObjectProperty(newPort, PORT, CONNECTED_SIGNALS, adaptee->id());
384 controller.setObjectProperty(concernedPort, PORT, CONNECTED_SIGNALS, adaptee->id());
385 controller.setObjectProperty(adaptee->id(), adaptee->kind(), end, newPort);
392 static types::InternalType* get(const LinkAdapter& adaptor, const Controller& controller)
394 return getLinkEnd(adaptor, controller, SOURCE_PORT);
397 static bool set(LinkAdapter& adaptor, types::InternalType* v, Controller& controller)
399 return setLinkEnd(adaptor, controller, SOURCE_PORT, v);
406 static types::InternalType* get(const LinkAdapter& adaptor, const Controller& controller)
408 return getLinkEnd(adaptor, controller, DESTINATION_PORT);
411 static bool set(LinkAdapter& adaptor, types::InternalType* v, Controller& controller)
413 return setLinkEnd(adaptor, controller, DESTINATION_PORT, v);
419 template<> property<LinkAdapter>::props_t property<LinkAdapter>::fields = property<LinkAdapter>::props_t();
421 LinkAdapter::LinkAdapter(const LinkAdapter& o) :
422 BaseAdapter<LinkAdapter, org_scilab_modules_scicos::model::Link>(o) {}
424 LinkAdapter::LinkAdapter(org_scilab_modules_scicos::model::Link* o) :
425 BaseAdapter<LinkAdapter, org_scilab_modules_scicos::model::Link>(o)
427 if (property<LinkAdapter>::properties_has_not_been_set())
429 property<LinkAdapter>::fields.reserve(7);
430 property<LinkAdapter>::add_property(L"xx", &xx::get, &xx::set);
431 property<LinkAdapter>::add_property(L"yy", &yy::get, &yy::set);
432 property<LinkAdapter>::add_property(L"id", &id::get, &id::set);
433 property<LinkAdapter>::add_property(L"thick", &thick::get, &thick::set);
434 property<LinkAdapter>::add_property(L"ct", &ct::get, &ct::set);
435 property<LinkAdapter>::add_property(L"from", &from::get, &from::set);
436 property<LinkAdapter>::add_property(L"to", &to::get, &to::set);
440 LinkAdapter::~LinkAdapter()
444 std::wstring LinkAdapter::getTypeStr()
446 return getSharedTypeStr();
448 std::wstring LinkAdapter::getShortTypeStr()
450 return getSharedTypeStr();
453 } /* namespace view_scilab */
454 } /* namespace org_scilab_modules_scicos */