Xcos MVC: implement xx / yy / id / thick / ct
[scilab.git] / scilab / modules / scicos / src / cpp / view_scilab / LinkAdapter.cpp
index 7428bdb..4240ccb 100644 (file)
  */
 
 #include <string>
+#include <vector>
+#include <algorithm>
 
 #include "internal.hxx"
 #include "list.hxx"
 #include "types.hxx"
 #include "user.hxx"
+#include "double.hxx"
 
 #include "Controller.hxx"
 #include "LinkAdapter.hxx"
@@ -27,6 +30,223 @@ namespace view_scilab
 namespace
 {
 
+struct xx
+{
+
+    static types::InternalType* get(const LinkAdapter& adaptor, const Controller& controller)
+    {
+        model::Link* adaptee = adaptor.getAdaptee();
+
+        std::vector<double> controlPoints;
+        controller.getObjectProperty(adaptee->id(), adaptee->kind(), CONTROL_POINTS, controlPoints);
+
+        double* data;
+        types::Double* o = new types::Double(controlPoints.size() / 2, 1, &data);
+
+        std::copy(controlPoints.begin(), controlPoints.begin() + controlPoints.size() / 2, data);
+        return o;
+    }
+
+    static bool set(LinkAdapter& adaptor, types::InternalType* v, Controller& controller)
+    {
+        model::Link* adaptee = adaptor.getAdaptee();
+
+        if (v->getType() != types::InternalType::ScilabDouble)
+        {
+            return false;
+        }
+
+        types::Double* current = v->getAs<types::Double>();
+
+        std::vector<double> controlPoints;
+        controller.getObjectProperty(adaptee->id(), adaptee->kind(), CONTROL_POINTS, controlPoints);
+
+        if (current->getSize() != static_cast<int>(controlPoints.size() / 2))
+        {
+            return false;
+        }
+
+        std::copy(current->getReal(), current->getReal() + current->getSize(), controlPoints.begin());
+        controller.setObjectProperty(adaptee->id(), adaptee->kind(), CONTROL_POINTS, controlPoints);
+
+        return true;
+    }
+};
+
+struct yy
+{
+
+    static types::InternalType* get(const LinkAdapter& adaptor, const Controller& controller)
+    {
+        model::Link* adaptee = adaptor.getAdaptee();
+
+        std::vector<double> controlPoints;
+        controller.getObjectProperty(adaptee->id(), adaptee->kind(), CONTROL_POINTS, controlPoints);
+
+        double* data;
+        types::Double* o = new types::Double(controlPoints.size() / 2, 1, &data);
+
+        std::copy(controlPoints.begin() + controlPoints.size() / 2, controlPoints.end(), data);
+        return o;
+    }
+
+    static bool set(LinkAdapter& adaptor, types::InternalType* v, Controller& controller)
+    {
+        model::Link* adaptee = adaptor.getAdaptee();
+
+        if (v->getType() != types::InternalType::ScilabDouble)
+        {
+            return false;
+        }
+
+        types::Double* current = v->getAs<types::Double>();
+
+        std::vector<double> controlPoints;
+        controller.getObjectProperty(adaptee->id(), adaptee->kind(), CONTROL_POINTS, controlPoints);
+
+        if (current->getSize() != static_cast<int>(controlPoints.size() / 2))
+        {
+            return false;
+        }
+
+        std::copy(current->getReal(), current->getReal() + current->getSize(), controlPoints.begin() + controlPoints.size() / 2);
+        controller.setObjectProperty(adaptee->id(), adaptee->kind(), CONTROL_POINTS, controlPoints);
+
+        return true;
+    }
+};
+
+struct id
+{
+
+    static types::InternalType* get(const LinkAdapter& adaptor, const Controller& controller)
+    {
+        model::Link* adaptee = adaptor.getAdaptee();
+
+        std::string id;
+        controller.getObjectProperty(adaptee->id(), adaptee->kind(), LABEL, id);
+
+        types::String* o = new types::String(1, 1);
+        o->set(0, id.data());
+
+        return o;
+    }
+
+    static bool set(LinkAdapter& adaptor, types::InternalType* v, Controller& controller)
+    {
+        if (v->getType() != types::InternalType::ScilabString)
+        {
+            return false;
+        }
+
+        types::String* current = v->getAs<types::String>();
+        if (current->getSize() != 1)
+        {
+            return false;
+        }
+
+        model::Link* adaptee = adaptor.getAdaptee();
+
+        std::string id;
+        char* c_str = wide_string_to_UTF8(current->get(0));
+        id = std::string(c_str);
+        FREE(c_str);
+
+        controller.setObjectProperty(adaptee->id(), adaptee->kind(), LABEL, id);
+        return true;
+    }
+};
+
+struct thick
+{
+
+    static types::InternalType* get(const LinkAdapter& adaptor, const Controller& controller)
+    {
+        model::Link* adaptee = adaptor.getAdaptee();
+
+        std::vector<double> thick;
+        controller.getObjectProperty(adaptee->id(), adaptee->kind(), THICK, thick);
+
+        double* data;
+        types::Double* o = new types::Double(1, 2, &data);
+
+        data[0] = thick[0];
+        data[1] = thick[1];
+        return o;
+    }
+
+    static bool set(LinkAdapter& adaptor, types::InternalType* v, Controller& controller)
+    {
+        model::Link* adaptee = adaptor.getAdaptee();
+
+        if (v->getType() != types::InternalType::ScilabDouble)
+        {
+            return false;
+        }
+
+        types::Double* current = v->getAs<types::Double>();
+        if (current->getRows() != 1 || current->getCols() != 2)
+        {
+            return false;
+        }
+
+        std::vector<double> thick (2);
+        thick[0] = current->get(0);
+        thick[1] = current->get(1);
+
+        controller.setObjectProperty(adaptee->id(), adaptee->kind(), THICK, thick);
+        return true;
+    }
+};
+
+struct ct
+{
+
+    static types::InternalType* get(const LinkAdapter& adaptor, const Controller& controller)
+    {
+        model::Link* adaptee = adaptor.getAdaptee();
+
+        int color;
+        int kind;
+        controller.getObjectProperty(adaptee->id(), adaptee->kind(), COLOR, color);
+        controller.getObjectProperty(adaptee->id(), adaptee->kind(), KIND, kind);
+
+        double* data;
+        types::Double* o = new types::Double(1, 2, &data);
+
+        data[0] = static_cast<double>(color);
+        data[1] = static_cast<double>(kind);
+        return o;
+    }
+
+    static bool set(LinkAdapter& adaptor, types::InternalType* v, Controller& controller)
+    {
+        model::Link* adaptee = adaptor.getAdaptee();
+
+        if (v->getType() != types::InternalType::ScilabDouble)
+        {
+            return false;
+        }
+
+        types::Double* current = v->getAs<types::Double>();
+        if (current->getRows() != 1 || current->getCols() != 2)
+        {
+            return false;
+        }
+        if (floor(current->get(0)) != current->get(0) || floor(current->get(1)) != current->get(1))
+        {
+            return false;
+        }
+
+        int color = static_cast<int>(current->get(0));
+        int kind  = static_cast<int>(current->get(1));
+
+        controller.setObjectProperty(adaptee->id(), adaptee->kind(), COLOR, color);
+        controller.setObjectProperty(adaptee->id(), adaptee->kind(), KIND, kind);
+        return true;
+    }
+};
+
 } /* namespace */
 
 template<> property<LinkAdapter>::props_t property<LinkAdapter>::fields = property<LinkAdapter>::props_t();
@@ -39,7 +259,12 @@ LinkAdapter::LinkAdapter(org_scilab_modules_scicos::model::Link* o) :
 {
     if (property<LinkAdapter>::properties_has_not_been_set())
     {
-        // FIXME: add some properties
+        property<LinkAdapter>::fields.reserve(5);
+        property<LinkAdapter>::add_property(L"xx", &xx::get, &xx::set);
+        property<LinkAdapter>::add_property(L"yy", &yy::get, &yy::set);
+        property<LinkAdapter>::add_property(L"id", &id::get, &id::set);
+        property<LinkAdapter>::add_property(L"thick", &thick::get, &thick::set);
+        property<LinkAdapter>::add_property(L"ct", &ct::get, &ct::set);
     }
 }