Xcos MVC: implement Annotations 16/15116/18
Paul Bignier [Thu, 28 Aug 2014 09:22:18 +0000 (11:22 +0200)]
Change-Id: Iad8dd86eef130a7eb3910cfe9f5949916b30aaf9

scilab/modules/scicos/includes/utilities.hxx
scilab/modules/scicos/macros/scicos_scicos/%graphics_i_Text.sci [new file with mode: 0644]
scilab/modules/scicos/macros/scicos_scicos/%model_i_Text.sci [new file with mode: 0644]
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/Annotation.hxx
scilab/modules/scicos/src/cpp/view_scilab/TextAdapter.cpp
scilab/modules/scicos/tests/unit_tests/model/Annotation.dia.ref [new file with mode: 0644]
scilab/modules/scicos/tests/unit_tests/model/Annotation.tst [new file with mode: 0644]
scilab/modules/scicos/tests/unit_tests/scicos_new.tst

index 47fab89..290cf18 100644 (file)
@@ -55,7 +55,9 @@ enum object_properties_t
 {
     PARENT_DIAGRAM,     //!< model::Annotation::parentDiagram or model::Block::parentDiagram or model::Link::parentDiagram value
     GEOMETRY,           //!< model::Annotation::geometry or model::Block::geometry value
-    DESCRIPTION,        //!< model::Annotation::description
+    DESCRIPTION,        //!< model::Annotation::description text
+    FONT,               //!< model::Annotation::description font
+    FONT_SIZE,          //!< model::Annotation::description font size
     RELATED_TO,         //!< model::Annotation::relatedTo
     INTERFACE_FUNCTION, //!< model::Block::interfaceFunction value
     SIM_FUNCTION_NAME,  //!< model::Descriptor::functionName value (stored into model::Block::sim)
diff --git a/scilab/modules/scicos/macros/scicos_scicos/%graphics_i_Text.sci b/scilab/modules/scicos/macros/scicos_scicos/%graphics_i_Text.sci
new file mode 100644 (file)
index 0000000..3dbb1b9
--- /dev/null
@@ -0,0 +1,24 @@
+//  Scicos
+//
+//  Copyright (C) 2014 - Scilab Enterprises - Paul Bignier
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+//
+// See the file ../license.txt
+//
+
+function g=%graphics_i_Text(i,t,g)
+    scicos_setfield(i,t,g)
+endfunction
diff --git a/scilab/modules/scicos/macros/scicos_scicos/%model_i_Text.sci b/scilab/modules/scicos/macros/scicos_scicos/%model_i_Text.sci
new file mode 100644 (file)
index 0000000..fabeae6
--- /dev/null
@@ -0,0 +1,24 @@
+//  Scicos
+//
+//  Copyright (C) 2014 - Scilab Enterprises - Paul Bignier
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+//
+// See the file ../license.txt
+//
+
+function g=%model_i_Text(i,t,g)
+    scicos_setfield(i,t,g)
+endfunction
index 5b3616a..8f2b546 100644 (file)
@@ -85,6 +85,12 @@ std::ostream& operator<<(std::ostream& os, object_properties_t p)
         case DESCRIPTION:
             os << "DESCRIPTION";
             break;
+        case FONT:
+            os << "FONT";
+            break;
+        case FONT_SIZE:
+            os << "FONT_SIZE";
+            break;
         case RELATED_TO:
             os << "RELATED_TO";
             break;
index b26686e..693feb4 100644 (file)
@@ -198,8 +198,18 @@ bool Model::getObjectProperty(ScicosID uid, kind_t k, object_properties_t p, std
 
     if (k == ANNOTATION)
     {
+        model::Annotation* o = static_cast<model::Annotation*>(getObject(uid));
         switch (p)
         {
+            case DESCRIPTION:
+                o->getDescription(v);
+                return true;
+            case FONT:
+                o->getFont(v);
+                return true;
+            case FONT_SIZE:
+                o->getFontSize(v);
+                return true;
             default:
                 break;
         }
index 74d1e2b..450ad25 100644 (file)
@@ -246,8 +246,15 @@ update_status_t Model::setObjectProperty(ScicosID uid, kind_t k, object_properti
 
     if (k == ANNOTATION)
     {
+        model::Annotation* o = static_cast<model::Annotation*>(getObject(uid));
         switch (p)
         {
+            case DESCRIPTION:
+                return o->setDescription(v);
+            case FONT:
+                return o->setFont(v);
+            case FONT_SIZE:
+                return o->setFontSize(v);
             default:
                 break;
         }
index a6dd734..99cb8d4 100644 (file)
@@ -29,23 +29,63 @@ class Annotation: public BaseObject
     friend class ::org_scilab_modules_scicos::Model;
 
 private:
-    Annotation(): BaseObject(ANNOTATION), parentDiagram(0), geometry(), description(), relatedTo(0) {};
-    Annotation(const Annotation& o) : BaseObject(ANNOTATION), parentDiagram(o.parentDiagram), geometry(o.geometry), description(o.description), relatedTo(o.relatedTo) {};
+    Annotation(): BaseObject(ANNOTATION), parentDiagram(0),
+        description(std::string("Text")), font(std::string("2")), font_size(std::string("1")), relatedTo(0)
+    {
+        std::vector<double> geom (4, 0);
+        geom[2] = 2;
+        geom[3] = 1;
+        geometry = Geometry(geom);
+    };
+    Annotation(const Annotation& o) : BaseObject(ANNOTATION), parentDiagram(o.parentDiagram), geometry(o.geometry),
+        description(o.description), font(o.font), font_size(o.font_size), relatedTo(o.relatedTo) {};
     ~Annotation() {};
 
-    const std::string& getDescription() const
+    void getDescription(std::string& data) const
+    {
+        data = description;
+    }
+
+    update_status_t setDescription(const std::string& data)
+    {
+        if (data == description)
+        {
+            return NO_CHANGES;
+        }
+
+        description = data;
+        return SUCCESS;
+    }
+
+    void getFont(std::string& data) const
+    {
+        data = font;
+    }
+
+    update_status_t setFont(const std::string& data)
+    {
+        if (data == font)
+        {
+            return NO_CHANGES;
+        }
+
+        font = data;
+        return SUCCESS;
+    }
+
+    void getFontSize(std::string& data) const
     {
-        return description;
+        data = font_size;
     }
 
-    update_status_t setDescription(const std::string& description)
+    update_status_t setFontSize(const std::string& data)
     {
-        if (this->description == description)
+        if (data == font_size)
         {
             return NO_CHANGES;
         }
 
-        this->description = description;
+        font_size = data;
         return SUCCESS;
     }
 
@@ -107,6 +147,8 @@ private:
     ScicosID parentDiagram;
     Geometry geometry;
     std::string description;
+    std::string font;
+    std::string font_size;
     ScicosID relatedTo;
 };
 
index 482dc3b..a5121c5 100644 (file)
  */
 
 #include <string>
+#include <vector>
 
+#include "double.hxx"
+#include "string.hxx"
+#include "mlist.hxx"
 #include "internal.hxx"
 
 #include "Controller.hxx"
@@ -29,6 +33,225 @@ namespace view_scilab
 namespace
 {
 
+const std::wstring Graphics(L"graphics");
+const std::wstring orig(L"orig");
+const std::wstring sz(L"sz");
+const std::wstring exprs(L"exprs");
+
+const std::wstring Model(L"model");
+const std::wstring sim(L"sim");
+const std::wstring rpar(L"rpar");
+const std::wstring ipar(L"ipar");
+
+struct graphics
+{
+    static types::InternalType* get(const TextAdapter& adaptor, const Controller& controller)
+    {
+        model::Annotation* adaptee = adaptor.getAdaptee();
+
+        types::MList* o = new types::MList();
+        types::String* MListFields = new types::String(1, 4);
+        MListFields->set(0, Graphics.c_str());
+        MListFields->set(1, orig.c_str());
+        MListFields->set(2, sz.c_str());
+        MListFields->set(3, exprs.c_str());
+        o->set(0, MListFields);
+
+        // orig and sz
+        std::vector<double> geom;
+        controller.getObjectProperty(adaptee->id(), adaptee->kind(), GEOMETRY, geom);
+
+        double* dataOrig;
+        double* dataSz;
+        types::Double* origField = new types::Double(1, 2, &dataOrig);
+        types::Double* szField   = new types::Double(1, 2, &dataSz);
+        dataOrig[0] = geom[0];
+        dataOrig[1] = geom[1];
+        dataSz[0]   = geom[2];
+        dataSz[1]   = geom[3];
+        o->set(1, origField);
+        o->set(2, szField);
+
+        // exprs
+        std::vector<std::string> Exprs (3);
+        controller.getObjectProperty(adaptee->id(), adaptee->kind(), DESCRIPTION, Exprs[0]);
+        controller.getObjectProperty(adaptee->id(), adaptee->kind(), FONT, Exprs[1]);
+        controller.getObjectProperty(adaptee->id(), adaptee->kind(), FONT_SIZE, Exprs[2]);
+
+        types::String* exprsField = new types::String(3, 1);
+        for (int i = 0; i < (int) Exprs.size(); ++i)
+        {
+            exprsField->set(i, Exprs[i].data());
+        }
+        o->set(3, exprsField);
+
+        return o;
+    }
+
+    static bool set(TextAdapter& adaptor, types::InternalType* v, Controller& controller)
+    {
+        if (v->getType() != types::InternalType::ScilabMList)
+        {
+            return false;
+        }
+
+        types::MList* current = v->getAs<types::MList>();
+        if (current->getSize() < 4)
+        {
+            return false;
+        }
+
+        types::InternalType* currentField;
+        types::Double* currentFieldDouble;
+        types::String* currentFieldString;
+
+        model::Annotation* adaptee = adaptor.getAdaptee();
+
+        // orig
+        if ((currentField = current->getField(orig.c_str())) == NULL)
+        {
+            return false;
+        }
+        if (currentField->getType() != types::InternalType::ScilabDouble)
+        {
+            return false;
+        }
+        currentFieldDouble = currentField->getAs<types::Double>();
+        if (currentFieldDouble->getRows() != 1 || currentFieldDouble->getCols() != 2)
+        {
+            return false;
+        }
+        std::vector<double> origField;
+        controller.getObjectProperty(adaptee->id(), adaptee->kind(), GEOMETRY, origField);
+        origField[0] = currentFieldDouble->get(0);
+        origField[1] = currentFieldDouble->get(1);
+        controller.setObjectProperty(adaptee->id(), adaptee->kind(), GEOMETRY, origField);
+
+        // sz
+        if ((currentField = current->getField(sz.c_str())) == NULL)
+        {
+            return false;
+        }
+        if (currentField->getType() != types::InternalType::ScilabDouble)
+        {
+            return false;
+        }
+        currentFieldDouble = currentField->getAs<types::Double>();
+        if (currentFieldDouble->getRows() != 1 || currentFieldDouble->getCols() != 2)
+        {
+            return false;
+        }
+        std::vector<double> szField;
+        controller.getObjectProperty(adaptee->id(), adaptee->kind(), GEOMETRY, szField);
+        szField[2] = currentFieldDouble->get(0);
+        szField[3] = currentFieldDouble->get(1);
+        controller.setObjectProperty(adaptee->id(), adaptee->kind(), GEOMETRY, szField);
+
+        // exprs
+        if ((currentField = current->getField(exprs.c_str())) == NULL)
+        {
+            return false;
+        }
+        if (currentField->getType() == types::InternalType::ScilabString)
+        {
+            currentFieldString = currentField->getAs<types::String>();
+            if (currentFieldString->getCols() != 1 || currentFieldString->getSize() != 3)
+            {
+                return false;
+            }
+
+            std::vector<std::string> exprsField (3);
+            for (int i = 0; i < (int) exprsField.size(); ++i)
+            {
+                char* c_str = wide_string_to_UTF8(currentFieldString->get(i));
+                exprsField[i] = std::string(c_str);
+                FREE(c_str);
+            }
+            controller.setObjectProperty(adaptee->id(), adaptee->kind(), DESCRIPTION, exprsField[0]);
+            controller.setObjectProperty(adaptee->id(), adaptee->kind(), FONT, exprsField[1]);
+            controller.setObjectProperty(adaptee->id(), adaptee->kind(), FONT_SIZE, exprsField[2]);
+            return true;
+        }
+        else if (currentField->getType() == types::InternalType::ScilabDouble)
+        {
+            currentFieldDouble = currentField->getAs<types::Double>();
+            if (currentFieldDouble->getSize() != 0)
+            {
+                return false;
+            }
+
+            std::vector<std::string> exprsField (3);
+            controller.setObjectProperty(adaptee->id(), adaptee->kind(), DESCRIPTION, exprsField[0]);
+            controller.setObjectProperty(adaptee->id(), adaptee->kind(), FONT, exprsField[1]);
+            controller.setObjectProperty(adaptee->id(), adaptee->kind(), FONT_SIZE, exprsField[2]);
+            return true;
+        }
+
+        return false;
+    }
+};
+
+struct model
+{
+
+    static types::InternalType* get(const TextAdapter& adaptor, const Controller& controller)
+    {
+        // silent unused parameter warnings
+        (void) adaptor;
+        (void) controller;
+
+        // Return an empty "model"-typed mlist because this field isn't used.
+        types::MList* o = new types::MList();
+        types::String* MListFields = new types::String(4, 1);
+
+        MListFields->set(0, Model.c_str());
+        MListFields->set(1, sim.c_str());
+        MListFields->set(2, rpar.c_str());
+        MListFields->set(3, ipar.c_str());
+
+        o->set(0, MListFields);
+        // 'sim' field needs to be defined for the console display.
+        o->set(1, new types::Double(0, 0));
+        return o;
+    }
+
+    static bool set(TextAdapter& adaptor, types::InternalType* v, Controller& controller)
+    {
+        // silent unused parameter warnings
+        (void) adaptor;
+        (void) v;
+        (void) controller;
+
+        // everything should be right as the properties mapped using this adapter do not perform anything
+        return true;
+    }
+};
+
+struct dummy_property
+{
+
+    static types::InternalType* get(const TextAdapter& adaptor, const Controller& controller)
+    {
+        // silent unused parameter warnings
+        (void) adaptor;
+        (void) controller;
+
+        // Return an empty matrix because this field isn't used.
+        return new types::Double(0, 0);
+    }
+
+    static bool set(TextAdapter& adaptor, types::InternalType* v, Controller& controller)
+    {
+        // silent unused parameter warnings
+        (void) adaptor;
+        (void) v;
+        (void) controller;
+
+        // everything should be right as the properties mapped using this adapter do not perform anything
+        return true;
+    }
+};
+
 } /* namespace */
 
 template<> property<TextAdapter>::props_t property<TextAdapter>::fields = property<TextAdapter>::props_t();
@@ -41,7 +264,11 @@ TextAdapter::TextAdapter(org_scilab_modules_scicos::model::Annotation* o) :
 {
     if (property<TextAdapter>::properties_have_not_been_set())
     {
-        // FIXME: add some properties
+        property<TextAdapter>::fields.reserve(4);
+        property<TextAdapter>::add_property(Graphics, &graphics::get, &graphics::set);
+        property<TextAdapter>::add_property(Model, &model::get, &model::set);
+        property<TextAdapter>::add_property(L"void", &dummy_property::get, &dummy_property::set);
+        property<TextAdapter>::add_property(L"gui", &dummy_property::get, &dummy_property::set);
     }
 }
 
diff --git a/scilab/modules/scicos/tests/unit_tests/model/Annotation.dia.ref b/scilab/modules/scicos/tests/unit_tests/model/Annotation.dia.ref
new file mode 100644 (file)
index 0000000..33cc6b7
--- /dev/null
@@ -0,0 +1,80 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2014 - Scilab Enterprises - Paul Bignier
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+loadXcosLibs();
+p = funcprot();
+funcprot(0);
+// Overload scicos_getvalue and xstringl to customize parameters
+function [ok,txt,font,siz,exprs] = scicos_getvalue(title, fields, types, exprs)
+    ok = %t;
+    txt = "newText";
+    font = 3;
+    siz = 4;
+    exprs = [txt; string(font); string(siz)];
+endfunction
+function r = xstringl(a, b, c, d, e)
+    r = [0 0 3 4];
+endfunction
+funcprot(p);
+// Allocate an Annotation
+o = TEXT_f("define")
+objectCreated( 1 , ANNOTATION )
+propertyUpdated( 1 , ANNOTATION , GEOMETRY ) : NO_CHANGES
+propertyUpdated( 1 , ANNOTATION , GEOMETRY ) : NO_CHANGES
+propertyUpdated( 1 , ANNOTATION , DESCRIPTION ) : NO_CHANGES
+propertyUpdated( 1 , ANNOTATION , FONT ) : NO_CHANGES
+propertyUpdated( 1 , ANNOTATION , FONT_SIZE ) : NO_CHANGES
+o  = 
+scicos_Text type :
+  graphics
+  model
+  void
+  gui
+o.graphics
+ ans  =
+orig = [0,0]
+sz = [2,1]
+exprs = ["Text";"2";"1"]
+o.model
+ ans  =
+ type: 0
+"rpar"
+"ipar"
+o.void
+ ans  =
+    []
+o.gui
+ ans  =
+    []
+// Modify it
+o = TEXT_f("set", o)
+propertyUpdated( 1 , ANNOTATION , GEOMETRY ) : NO_CHANGES
+propertyUpdated( 1 , ANNOTATION , GEOMETRY ) : SUCCESS
+propertyUpdated( 1 , ANNOTATION , DESCRIPTION ) : SUCCESS
+propertyUpdated( 1 , ANNOTATION , FONT ) : SUCCESS
+propertyUpdated( 1 , ANNOTATION , FONT_SIZE ) : SUCCESS
+o  = 
+scicos_Text type :
+  graphics
+  model
+  void
+  gui
+o.graphics
+ ans  =
+orig = [0,0]
+sz = [3,4]
+exprs = ["newText";"3";"4"]
+o.model
+ ans  =
+ type: 0
+"rpar"
+"ipar"
+o.void
+ ans  =
+    []
+o.gui
+ ans  =
+    []
diff --git a/scilab/modules/scicos/tests/unit_tests/model/Annotation.tst b/scilab/modules/scicos/tests/unit_tests/model/Annotation.tst
new file mode 100644 (file)
index 0000000..f8bc84a
--- /dev/null
@@ -0,0 +1,37 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2014 - Scilab Enterprises - Paul Bignier
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+
+loadXcosLibs();
+
+p = funcprot();
+funcprot(0);
+// Overload scicos_getvalue and xstringl to customize parameters
+function [ok,txt,font,siz,exprs] = scicos_getvalue(title, fields, types, exprs)
+    ok = %t;
+    txt = "newText";
+    font = 3;
+    siz = 4;
+    exprs = [txt; string(font); string(siz)];
+endfunction
+function r = xstringl(a, b, c, d, e)
+    r = [0 0 3 4];
+endfunction
+funcprot(p);
+
+// Allocate an Annotation
+o = TEXT_f("define")
+o.graphics
+o.model
+o.void
+o.gui
+
+// Modify it
+o = TEXT_f("set", o)
+o.graphics
+o.model
+o.void
+o.gui
index 3817502..1217bd5 100644 (file)
@@ -3,7 +3,7 @@
 // Copyright (C) 2014 - Scilab Enterprises - Clement David
 //
 //  This file is distributed under the same license as the Scilab package.
-// =============================================================================+
+// =============================================================================
 
 loadXcosLibs();