Xcos MVC: implement diagram objs / version / contrib 31/15131/11
Paul Bignier [Fri, 29 Aug 2014 14:58:25 +0000 (16:58 +0200)]
Change-Id: I0bf8ff0166d7efa9d8636c86f82e413f8282efd2

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/Diagram.hxx
scilab/modules/scicos/src/cpp/view_scilab/BaseAdapter.hxx
scilab/modules/scicos/src/cpp/view_scilab/DiagramAdapter.cpp
scilab/modules/scicos/src/cpp/view_scilab/DiagramAdapter.hxx
scilab/modules/scicos/src/cpp/view_scilab/ParamsAdapter.cpp

index 07196ad..47fab89 100644 (file)
@@ -106,6 +106,7 @@ enum object_properties_t
     PATH,               //!< model::Diagram::title file path value
     PROPERTIES,         //!< model::Diagram::tol & tf values
     DIAGRAM_CONTEXT,    //!< model::Diagram::context value
+    VERSION_NUMBER,     //!< model::Diagram::version value
 };
 
 #endif /* UTILITIES_HXX_ */
index b21d9e1..5b3616a 100644 (file)
@@ -235,6 +235,9 @@ std::ostream& operator<<(std::ostream& os, object_properties_t p)
         case DIAGRAM_CONTEXT:
             os << "CONTEXT";
             break;
+        case VERSION_NUMBER:
+            os << "VERSION_NUMBER";
+            break;
     }
     return os;
 }
index bcc0744..1b1ec46 100644 (file)
@@ -242,6 +242,9 @@ bool Model::getObjectProperty(ScicosID uid, kind_t k, object_properties_t p, std
             case PATH:
                 o->getPath(v);
                 return true;
+            case VERSION_NUMBER:
+                o->getVersionNumber(v);
+                return true;
             default:
                 break;
         }
@@ -576,7 +579,7 @@ bool Model::getObjectProperty(ScicosID uid, kind_t k, object_properties_t p, std
         switch (p)
         {
             case CHILDREN:
-                v = o->getChildren();
+                o->getChildren(v);
                 return true;
             default:
                 break;
index 9b1d5a5..5ee3c91 100644 (file)
@@ -282,6 +282,8 @@ update_status_t Model::setObjectProperty(ScicosID uid, kind_t k, object_properti
                 return o->setTitle(v);
             case PATH:
                 return o->setPath(v);
+            case VERSION_NUMBER:
+                return o->setVersionNumber(v);
             default:
                 break;
         }
@@ -517,8 +519,11 @@ update_status_t Model::setObjectProperty(ScicosID uid, kind_t k, object_properti
     }
     else if (k == DIAGRAM)
     {
+        model::Diagram* o = static_cast<model::Diagram*>(getObject(uid));
         switch (p)
         {
+            case CHILDREN:
+                return o->setChildren(v);
             default:
                 break;
         }
index a1e8cea..04e0c23 100644 (file)
@@ -70,18 +70,25 @@ class Diagram: public BaseObject
 private:
     friend class ::org_scilab_modules_scicos::Model;
 
-    Diagram() : BaseObject(DIAGRAM), title("Untitled"), path(), properties(), context() {};
-    Diagram(const Diagram& o)  : BaseObject(DIAGRAM), title(o.title), path(o.path), properties(o.properties), context(o.context) {};
+    Diagram() : BaseObject(DIAGRAM), title("Untitled"), path(), properties(), context(), children(), version() {};
+    Diagram(const Diagram& o) : BaseObject(DIAGRAM), title(o.title), path(o.path), properties(o.properties),
+        context(o.context), children(o.children), version(o.version) {};
     ~Diagram() {}
 
-    const std::vector<ScicosID>& getChildren() const
+    void getChildren(std::vector<ScicosID>& c) const
     {
-        return children;
+        c = children;
     }
 
-    void setChildren(const std::vector<ScicosID>& children)
+    update_status_t setChildren(const std::vector<ScicosID>& c)
     {
-        this->children = children;
+        if (c == children)
+        {
+            return NO_CHANGES;
+        }
+
+        children = c;
+        return SUCCESS;
     }
 
     void getContext(std::vector<std::string>& data) const
@@ -164,14 +171,20 @@ private:
         return SUCCESS;
     }
 
-    const std::string& getVersion() const
+    void getVersionNumber(std::string& data) const
     {
-        return version;
+        data = version;
     }
 
-    void setVersion(const std::string& version)
+    update_status_t setVersionNumber(const std::string& data)
     {
-        this->version = version;
+        if (data == version)
+        {
+            return NO_CHANGES;
+        }
+
+        version = data;
+        return SUCCESS;
     }
 
 private:
index a11a299..b427ab7 100644 (file)
@@ -22,7 +22,6 @@
 #include "internal.hxx"
 #include "tlist.hxx"
 #include "mlist.hxx"
-#include "tlist.hxx"
 #include "string.hxx"
 
 #include "Controller.hxx"
@@ -168,7 +167,7 @@ public:
         typename property<Adaptor>::props_t properties = property<Adaptor>::fields;
         std::sort(properties.begin(), properties.end(), property<Adaptor>::original_index_cmp);
 
-        if (v->getType() != types::InternalType::ScilabTList)
+        if (v->getType() != types::InternalType::ScilabTList && v->getType() != types::InternalType::ScilabMList)
         {
             return false;
         }
index 2988a78..14e5473 100644 (file)
  */
 
 #include <string>
+#include <vector>
 #include <sstream>
 
 #include "internal.hxx"
+#include "double.hxx"
 #include "list.hxx"
 #include "tlist.hxx"
 #include "string.hxx"
 #include "types.hxx"
 #include "user.hxx"
 
+#include "utilities.hxx"
 #include "Controller.hxx"
 #include "DiagramAdapter.hxx"
 #include "ParamsAdapter.hxx"
+#include "TextAdapter.hxx"
+#include "BlockAdapter.hxx"
+#include "LinkAdapter.hxx"
+#include "model/BaseObject.hxx"
 
 extern "C" {
 #include "version.h"
@@ -35,6 +42,10 @@ namespace view_scilab
 namespace
 {
 
+static const std::wstring AnnotationStr = L"Text";
+static const std::wstring BlockStr = L"Block";
+static const std::wstring LinkStr = L"Link";
+
 struct props
 {
 
@@ -56,25 +67,110 @@ struct objs
 
     static types::InternalType* get(const DiagramAdapter& adaptor, const Controller& controller)
     {
-        // silent unused parameter warnings
-        (void) adaptor;
-        (void) controller;
-
-        // FIXME implement, get all children of the Diagram and return them as a list
-
-        return 0;
+        // FIXME: get all children of the Diagram and return them as a list
+        model::Diagram* adaptee = adaptor.getAdaptee();
+
+        std::vector<ScicosID> children;
+        controller.getObjectProperty(adaptee->id(), adaptee->kind(), CHILDREN, children);
+
+        types::List* o = new types::List();
+
+        for (size_t i = 0; i < children.size(); ++i)
+        {
+            model::BaseObject* item = Controller().getObject(children[i]);
+            switch (item->kind())
+            {
+                case ANNOTATION:
+                {
+                    model::Annotation* annotation = static_cast<model::Annotation*>(item);
+                    TextAdapter localAdaptor = TextAdapter(annotation);
+                    o->set(i, localAdaptor.getAsTList(new types::MList(), controller));
+                    break;
+                }
+                case BLOCK:
+                {
+                    model::Block* block = static_cast<model::Block*>(item);
+                    BlockAdapter localAdaptor = BlockAdapter(block);
+                    o->set(i, localAdaptor.getAsTList(new types::MList(), controller));
+                    break;
+                }
+                case LINK:
+                {
+                    model::Link* link = static_cast<model::Link*>(item);
+                    LinkAdapter localAdaptor = LinkAdapter(link);
+                    o->set(i, localAdaptor.getAsTList(new types::MList(), controller));
+                    break;
+                }
+                default:
+                    return 0;
+            }
+        }
+        return o;
     }
 
     static bool set(DiagramAdapter& adaptor, types::InternalType* v, Controller& controller)
     {
-        // silent unused parameter warnings
-        (void) adaptor;
-        (void) v;
-        (void) controller;
-
         // FIXME implement, decode the list and set all children of the Diagram
-
-        return false;
+        if (v->getType() != types::InternalType::ScilabList)
+        {
+            return false;
+        }
+
+        model::Diagram* adaptee = adaptor.getAdaptee();
+
+        types::List* list = v->getAs<types::List>();
+        std::vector<ScicosID> diagramChildren (list->getSize());
+        for (int i = 0; i < list->getSize(); ++i)
+        {
+            if (list->get(i)->getType() != types::InternalType::ScilabMList)
+            {
+                return false;
+            }
+            types::MList* modelElement = list->get(i)->getAs<types::MList>();
+
+            std::wstring modelElementType = modelElement->getTypeStr();
+            ScicosID id;
+
+            if (modelElementType == AnnotationStr)
+            {
+                id = controller.createObject(ANNOTATION);
+
+                TextAdapter localAdaptor = TextAdapter(static_cast<model::Annotation*>(controller.getObject(id)));
+                if (!localAdaptor.setAsTList(modelElement, controller))
+                {
+                    return false;
+                }
+            }
+            else if (modelElementType == BlockStr)
+            {
+                id = controller.createObject(BLOCK);
+
+                BlockAdapter localAdaptor = BlockAdapter(static_cast<model::Block*>(controller.getObject(id)));
+                if (!localAdaptor.setAsTList(modelElement, controller))
+                {
+                    return false;
+                }
+            }
+            else if (modelElementType == LinkStr)
+            {
+                id = controller.createObject(LINK);
+
+                LinkAdapter localAdaptor = LinkAdapter(static_cast<model::Link*>(controller.getObject(id)));
+                if (!localAdaptor.setAsTList(modelElement, controller))
+                {
+                    return false;
+                }
+            }
+            else
+            {
+                return false;
+            }
+
+            diagramChildren[i] = id;
+        }
+
+        controller.setObjectProperty(adaptee->id(), adaptee->kind(), CHILDREN, diagramChildren);
+        return true;
     }
 };
 
@@ -83,25 +179,49 @@ struct version
 
     static types::InternalType* get(const DiagramAdapter& adaptor, const Controller& controller)
     {
-        // silent unused parameter warnings
-        (void) adaptor;
-        (void) controller;
+        model::Diagram* adaptee = adaptor.getAdaptee();
 
-        std::stringstream str;
-        str << SCI_VERSION_MAJOR << '.' << SCI_VERSION_MINOR << '.' << SCI_VERSION_MAINTENANCE;
+        std::string version;
+        controller.getObjectProperty(adaptee->id(), adaptee->kind(), VERSION_NUMBER, version);
 
-        return new types::String(str.str().c_str());
+        return new types::String(version.data());
     }
 
     static bool set(DiagramAdapter& adaptor, types::InternalType* v, Controller& controller)
     {
-        // silent unused parameter warnings
-        (void) adaptor;
-        (void) v;
-        (void) controller;
+        if (v->getType() == types::InternalType::ScilabString)
+        {
+            types::String* current = v->getAs<types::String>();
+            if (current->getSize() != 1)
+            {
+                return false;
+            }
+
+            model::Diagram* adaptee = adaptor.getAdaptee();
+
+            char* c_str = wide_string_to_UTF8(current->get(0));
+            std::string version (c_str);
+            FREE(c_str);
+
+            controller.setObjectProperty(adaptee->id(), adaptee->kind(), VERSION_NUMBER, version);
+            return true;
+        }
+        else if (v->getType() == types::InternalType::ScilabDouble)
+        {
+            types::Double* current = v->getAs<types::Double>();
+            if (current->getSize() != 0)
+            {
+                return false;
+            }
+
+            model::Diagram* adaptee = adaptor.getAdaptee();
+
+            std::string version;
+            controller.setObjectProperty(adaptee->id(), adaptee->kind(), VERSION_NUMBER, version);
+            return true;
+        }
 
-        //FIXME: handle version upgrade of the whole model
-        return true;
+        return false;
     }
 };
 
@@ -111,24 +231,19 @@ struct contrib
     static types::InternalType* get(const DiagramAdapter& adaptor, const Controller& controller)
     {
         // silent unused parameter warnings
-        (void) adaptor;
         (void) controller;
 
-        // FIXME implement, user defined data stored in the view
-
-        return 0;
+        return adaptor.getContribContent();
     }
 
     static bool set(DiagramAdapter& adaptor, types::InternalType* v, Controller& controller)
     {
         // silent unused parameter warnings
-        (void) adaptor;
         (void) v;
         (void) controller;
 
-        // FIXME implement, user defined data stored in the view
-
-        return false;
+        adaptor.setContribContent(v->clone());
+        return true;
     }
 };
 
@@ -137,10 +252,13 @@ struct contrib
 template<> property<DiagramAdapter>::props_t property<DiagramAdapter>::fields = property<DiagramAdapter>::props_t();
 
 DiagramAdapter::DiagramAdapter(const DiagramAdapter& o) :
-    BaseAdapter<DiagramAdapter, org_scilab_modules_scicos::model::Diagram>(o), contrib(o.contrib) {}
+    BaseAdapter<DiagramAdapter, org_scilab_modules_scicos::model::Diagram>(o)
+{
+    contrib_content = new types::List();
+}
 
 DiagramAdapter::DiagramAdapter(org_scilab_modules_scicos::model::Diagram* o) :
-    BaseAdapter<DiagramAdapter, org_scilab_modules_scicos::model::Diagram>(o), contrib(0)
+    BaseAdapter<DiagramAdapter, org_scilab_modules_scicos::model::Diagram>(o)
 {
     if (property<DiagramAdapter>::properties_have_not_been_set())
     {
@@ -150,10 +268,13 @@ DiagramAdapter::DiagramAdapter(org_scilab_modules_scicos::model::Diagram* o) :
         property<DiagramAdapter>::add_property(L"version", &version::get, &version::set);
         property<DiagramAdapter>::add_property(L"contrib", &contrib::get, &contrib::set);
     }
+
+    contrib_content = new types::List();
 }
 
 DiagramAdapter::~DiagramAdapter()
 {
+    delete contrib_content;
 }
 
 std::wstring DiagramAdapter::getTypeStr()
@@ -165,14 +286,15 @@ std::wstring DiagramAdapter::getShortTypeStr()
     return getSharedTypeStr();
 }
 
-types::InternalType* DiagramAdapter::getContrib() const
+types::InternalType* DiagramAdapter::getContribContent() const
 {
-    return contrib;
+    return contrib_content;
 }
 
-void DiagramAdapter::setContrib(types::InternalType* contrib)
+void DiagramAdapter::setContribContent(types::InternalType* v)
 {
-    this->contrib = contrib;
+    delete contrib_content;
+    contrib_content = v->clone();
 }
 
 } /* namespace view_scilab */
index 7bf4ebc..fd40c85 100644 (file)
@@ -39,11 +39,11 @@ public:
     std::wstring getTypeStr();
     std::wstring getShortTypeStr();
 
-    types::InternalType* getContrib() const;
-    void setContrib(types::InternalType* contrib);
+    types::InternalType* getContribContent() const;
+    void setContribContent(types::InternalType* v);
 
 private:
-    types::InternalType* contrib;
+    types::InternalType* contrib_content;
 };
 
 } /* namespace view_scilab */
index b86333f..baad6ef 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "double.hxx"
 #include "string.hxx"
+#include "list.hxx"
 #include "tlist.hxx"
 
 #include "Controller.hxx"
@@ -44,7 +45,8 @@ struct dummy_property
         (void) adaptor;
         (void) controller;
 
-        return 0;
+        // Return a default empty matrix.
+        return types::Double::Empty();
     }
 
     static bool set(ParamsAdapter& adaptor, types::InternalType* v, Controller& controller)