Xcos MVC: implement xx / yy / id / thick / ct 04/15004/9
Paul Bignier [Thu, 7 Aug 2014 08:40:05 +0000 (10:40 +0200)]
In the model, split ct into 'color' and 'kind'.

Also, fixed 'set' action for 'in', 'in2' and 'intyp' properties.

Change-Id: Id892eadcaef7020e4355292a66e7324344965fa6

scilab/modules/scicos/includes/utilities.hxx
scilab/modules/scicos/src/cpp/LoggerView.cpp
scilab/modules/scicos/src/cpp/Model_getObjectProperties.cpp
scilab/modules/scicos/src/cpp/Model_setObjectProperties.cpp
scilab/modules/scicos/src/cpp/model/Link.hxx
scilab/modules/scicos/src/cpp/model/Port.hxx
scilab/modules/scicos/src/cpp/view_scilab/LinkAdapter.cpp
scilab/modules/scicos/src/cpp/view_scilab/LinkAdapter.hxx
scilab/modules/scicos/src/cpp/view_scilab/ports_management.hxx

index 3ee3e88..544633b 100644 (file)
@@ -83,10 +83,13 @@ enum object_properties_t
     CHILDREN,           //!< model::Block::children for superblocks or model::Diagram::children value
     PORT_REFERENCE,     //!< model::Block::portReference value
     STYLE,              //!< model::Block & Port::style value
-    LABEL,              //!< model::Block & Port::label value
+    LABEL,              //!< model::Block & Port & Link::label or id value
     DESTINATION_PORT,   //!< model::Link::destinationPort value
     SOURCE_PORT,        //!< model::Link::sourcePort value
     CONTROL_POINTS,     //!< model::Link::controlPoints value
+    THICK,              //!< model::Link::thick value
+    COLOR,              //!< model::Link::color value
+    KIND,               //!< model::Link::kind value
     DATATYPE,           //!< model::Port::dataType value
     DATATYPE_ROWS,      //!< model::Port::dataType adapter helper
     DATATYPE_COLS,      //!< model::Port::dataType adapter helper
index dee6c8a..a625cf8 100644 (file)
@@ -178,6 +178,15 @@ std::ostream& operator<<(std::ostream& os, object_properties_t p)
         case CONTROL_POINTS:
             os << "CONTROL_POINTS";
             break;
+        case THICK:
+            os << "THICK";
+            break;
+        case COLOR:
+            os << "COLOR";
+            break;
+        case KIND:
+            os << "KIND";
+            break;
         case DATATYPE:
             os << "DATATYPE";
             break;
index 046860b..0ac6221 100644 (file)
@@ -127,6 +127,12 @@ bool Model::getObjectProperty(ScicosID uid, kind_t k, object_properties_t p, int
         model::Link* o = static_cast<model::Link*>(getObject(uid));
         switch (p)
         {
+            case COLOR:
+                o->getColor(v);
+                return true;
+            case KIND:
+                o->getKind(v);
+                return true;
             default:
                 break;
         }
@@ -244,6 +250,9 @@ bool Model::getObjectProperty(ScicosID uid, kind_t k, object_properties_t p, std
         model::Link* o = static_cast<model::Link*>(getObject(uid));
         switch (p)
         {
+            case LABEL:
+                o->getLabel(v);
+                return true;
             default:
                 break;
         }
@@ -383,6 +392,17 @@ bool Model::getObjectProperty(ScicosID uid, kind_t k, object_properties_t p, std
     else if (k == LINK)
     {
         model::Link* o = static_cast<model::Link*>(getObject(uid));
+        switch (p)
+        {
+            case CONTROL_POINTS:
+                o->getControlPoints(v);
+                return true;
+            case THICK:
+                o->getThick(v);
+                return true;
+            default:
+                break;
+        }
     }
     else if (k == PORT)
     {
index 7c78411..d30e4d8 100644 (file)
@@ -122,6 +122,10 @@ update_status_t Model::setObjectProperty(ScicosID uid, kind_t k, object_properti
         model::Link* o = static_cast<model::Link*>(getObject(uid));
         switch (p)
         {
+            case COLOR:
+                return o->setColor(v);
+            case KIND:
+                return o->setKind(v);
             default:
                 break;
         }
@@ -236,6 +240,8 @@ update_status_t Model::setObjectProperty(ScicosID uid, kind_t k, object_properti
         model::Port* o = static_cast<model::Port*>(getObject(uid));
         switch (p)
         {
+            case SOURCE_BLOCK:
+                return o->setSourceBlock(v);
             default:
                 break;
         }
@@ -287,6 +293,8 @@ update_status_t Model::setObjectProperty(ScicosID uid, kind_t k, object_properti
         model::Link* o = static_cast<model::Link*>(getObject(uid));
         switch (p)
         {
+            case LABEL:
+                return o->setLabel(v);
             default:
                 break;
         }
@@ -354,6 +362,10 @@ update_status_t Model::setObjectProperty(ScicosID uid, kind_t k, object_properti
         model::Link* o = static_cast<model::Link*>(getObject(uid));
         switch (p)
         {
+            case CONTROL_POINTS:
+                return o->setControlPoints(v);
+            case THICK:
+                return o->setThick(v);
             default:
                 break;
         }
index 6db27cd..d2d47ba 100644 (file)
@@ -15,7 +15,6 @@
 
 #include <string>
 #include <vector>
-#include <utility>
 
 #include "Model.hxx"
 #include "model/BaseObject.hxx"
@@ -25,13 +24,25 @@ namespace org_scilab_modules_scicos
 namespace model
 {
 
+/*
+ * List of all possible values for a Link.
+ */
+enum link_kind_t
+{
+    activation = -1,
+    regular = 1,
+    implicit = 2
+};
+
 class Link: public BaseObject
 {
     friend class ::org_scilab_modules_scicos::Model;
 
 private:
-    Link() : BaseObject(LINK), parentDiagram(0), sourcePort(0), destinationPort(0), controlPoints() {};
-    Link(const Link& o) : BaseObject(LINK), parentDiagram(o.parentDiagram), sourcePort(o.sourcePort), destinationPort(o.destinationPort), controlPoints(o.controlPoints) {};
+    Link() : BaseObject(LINK), parentDiagram(0), sourcePort(0), destinationPort(0), controlPoints(),
+        label(), thick(), color(1), linkKind(regular) {};
+    Link(const Link& o) : BaseObject(LINK), parentDiagram(o.parentDiagram), sourcePort(o.sourcePort), destinationPort(o.destinationPort),
+        controlPoints(o.controlPoints), label(o.label), thick(o.thick), color(o.color), linkKind(o.linkKind) {};
     ~Link() {}
 
     ScicosID getParentDiagram() const
@@ -44,14 +55,103 @@ private:
         this->parentDiagram = parentDiagram;
     }
 
-    const std::vector<std::pair<double, double> >& getControlPoints() const
+    void getControlPoints(std::vector<double>& v) const
+    {
+        v = controlPoints;
+    }
+
+    update_status_t setControlPoints(const std::vector<double>& v)
+    {
+        if (v == controlPoints)
+        {
+            return NO_CHANGES;
+        }
+
+        controlPoints = v;
+        return SUCCESS;
+    }
+
+    void getLabel(std::string& data) const
+    {
+        data = label;
+    }
+
+    update_status_t setLabel(const std::string& data)
+    {
+        if (data == label)
+        {
+            return NO_CHANGES;
+        }
+
+        label = data;
+        return SUCCESS;
+    }
+
+    void getThick(std::vector<double>& v) const
+    {
+        v = thick;
+    }
+
+    update_status_t setThick(const std::vector<double>& v)
     {
-        return controlPoints;
+        if (v.size() != 2)
+        {
+            return FAIL;
+        }
+
+        if (v == thick)
+        {
+            return NO_CHANGES;
+        }
+
+        thick = v;
+        return SUCCESS;
     }
 
-    void setControlPoints(const std::vector<std::pair<double, double> >& controlPoints)
+    void getColor(int& c) const
     {
-        this->controlPoints = controlPoints;
+        c = color;
+    }
+
+    update_status_t setColor(const int c)
+    {
+        if (c == color)
+        {
+            return NO_CHANGES;
+        }
+
+        color = c;
+        return SUCCESS;
+    }
+
+    void getKind(int& k) const
+    {
+        k = linkKind;
+    }
+
+    update_status_t setKind(const int k)
+    {
+        if (k == linkKind)
+        {
+            return NO_CHANGES;
+        }
+
+        // Only accepting Scilab Link kind values
+        switch (k)
+        {
+            case activation:
+                linkKind = activation;
+                break;
+            case regular:
+                linkKind = regular;
+                break;
+            case implicit:
+                linkKind = implicit;
+                break;
+            default:
+                return FAIL;
+        }
+        return SUCCESS;
     }
 
     ScicosID getDestinationPort() const
@@ -81,7 +181,13 @@ private:
     ScicosID destinationPort;
 
     // used to store, user-defined control points
-    std::vector<std::pair<double, double> > controlPoints;
+    std::vector<double> controlPoints;
+
+    std::string label;
+    std::vector<double> thick;
+    int color;
+    link_kind_t linkKind;
+
 };
 
 } /* namespace model */
index 6c1fb4d..bea267a 100644 (file)
@@ -98,14 +98,19 @@ private:
         this->kind = kind;
     }
 
-    ScicosID getSourceBlock() const
+    void getSourceBlock(ScicosID& sb) const
     {
-        return sourceBlock;
+        sb = sourceBlock;
     }
 
-    void setSourceBlock(ScicosID sourceBlock)
+    update_status_t setSourceBlock(const ScicosID sb)
     {
-        this->sourceBlock = sourceBlock;
+        if (sb == this->sourceBlock)
+        {
+            return NO_CHANGES;
+        }
+        this->sourceBlock = sb;
+        return SUCCESS;
     }
 
     void getImplicit(bool& v) const
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);
     }
 }
 
index 18c2313..c21af92 100644 (file)
@@ -343,7 +343,32 @@ bool addNewPort(ScicosID newPortID, int newPort, const std::vector<ScicosID>& ch
     else
     {
         // set the requested property, using newPort as a value
-        status = controller.setObjectProperty(newPortID, PORT, p, newPort);
+        switch (p)
+        {
+            case DATATYPE_ROWS:
+            {
+                std::vector<int> datatype (3);
+                controller.getObjectProperty(newPortID, PORT, DATATYPE, datatype);
+                datatype[0] = newPort;
+                return controller.setObjectProperty(newPortID, PORT, DATATYPE, datatype);
+            }
+            case DATATYPE_COLS:
+            {
+                std::vector<int> datatype (3);
+                controller.getObjectProperty(newPortID, PORT, DATATYPE, datatype);
+                datatype[1] = newPort;
+                return controller.setObjectProperty(newPortID, PORT, DATATYPE, datatype);
+            }
+            case DATATYPE_TYPE:
+            {
+                std::vector<int> datatype (3);
+                controller.getObjectProperty(newPortID, PORT, DATATYPE, datatype);
+                datatype[2] = newPort;
+                return controller.setObjectProperty(newPortID, PORT, DATATYPE, datatype);
+            }
+            default:
+                return controller.setObjectProperty(newPortID, PORT, p, newPort);
+        }
     }
 
     return status;
@@ -446,7 +471,7 @@ bool update_ports_property(const Adaptor& adaptor, object_properties_t port_kind
         while (!newPorts.empty())
         {
             int newPort = newPorts.back();
-            oldPorts.pop_back();
+            newPorts.pop_back();
 
             ScicosID id = controller.createObject(PORT);
             controller.setObjectProperty(id, PORT, SOURCE_BLOCK, adaptee->id());