Xcos: "Region to superblock" fixed
[scilab.git] / scilab / modules / scicos / src / cpp / view_scilab / Adapters.cpp
1 /*
2  *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  *  Copyright (C) 2014-2016 - Scilab Enterprises - Clement DAVID
4  *  Copyright (C) 2017-2018 - ESI Group - Clement DAVID
5  *
6  * Copyright (C) 2012 - 2016 - Scilab Enterprises
7  *
8  * This file is hereby licensed under the terms of the GNU GPL v2.0,
9  * pursuant to article 5.3.4 of the CeCILL v.2.1.
10  * This file was originally licensed under the terms of the CeCILL v2.1,
11  * and continues to be available under such terms.
12  * For more information, see the COPYING file which you should have received
13  * along with this program.
14  *
15  */
16
17 #include "../../../includes/view_scilab/Adapters.hxx"
18
19 #include <string>
20 #include <algorithm>
21
22 #include "View.hxx"
23 #include "BlockAdapter.hxx"
24 #include "CprAdapter.hxx"
25 #include "DiagramAdapter.hxx"
26 #include "GraphicsAdapter.hxx"
27 #include "LinkAdapter.hxx"
28 #include "ModelAdapter.hxx"
29 #include "ParamsAdapter.hxx"
30 #include "ScsAdapter.hxx"
31 #include "StateAdapter.hxx"
32 #include "TextAdapter.hxx"
33
34 namespace org_scilab_modules_scicos
35 {
36 namespace view_scilab
37 {
38
39 Adapters Adapters::m_instance;
40
41 Adapters& Adapters::instance()
42 {
43     return m_instance;
44 }
45
46 Adapters::Adapters() : adapters()
47 {
48     /*
49      * Create the supported data types list
50      */
51     // we assume that the INVALID_ADAPTER is set at end of the enum
52     adapters.reserve(INVALID_ADAPTER);
53
54     adapters.push_back(adapter_t(view_scilab::BlockAdapter::getSharedTypeStr(), BLOCK_ADAPTER));
55     adapters.push_back(adapter_t(view_scilab::CprAdapter::getSharedTypeStr(), CPR_ADAPTER));
56     adapters.push_back(adapter_t(view_scilab::DiagramAdapter::getSharedTypeStr(), DIAGRAM_ADAPTER));
57     adapters.push_back(adapter_t(view_scilab::GraphicsAdapter::getSharedTypeStr(), GRAPHIC_ADAPTER));
58     adapters.push_back(adapter_t(view_scilab::LinkAdapter::getSharedTypeStr(), LINK_ADAPTER));
59     adapters.push_back(adapter_t(view_scilab::ModelAdapter::getSharedTypeStr(), MODEL_ADAPTER));
60     adapters.push_back(adapter_t(view_scilab::ParamsAdapter::getSharedTypeStr(), PARAMS_ADAPTER));
61     adapters.push_back(adapter_t(view_scilab::ScsAdapter::getSharedTypeStr(), SCS_ADAPTER));
62     adapters.push_back(adapter_t(view_scilab::StateAdapter::getSharedTypeStr(), STATE_ADAPTER));
63     adapters.push_back(adapter_t(view_scilab::TextAdapter::getSharedTypeStr(), TEXT_ADAPTER));
64
65     std::sort(adapters.begin(), adapters.end());
66 }
67
68 Adapters::~Adapters()
69 {
70     adapters.clear();
71 }
72
73 Adapters::adapters_index_t Adapters::lookup_by_typename(const std::wstring& name)
74 {
75     adapters_t::iterator it = std::lower_bound(adapters.begin(), adapters.end(), name);
76     if (it != adapters.end() && !(name < it->name))
77     {
78         return it->kind;
79     }
80     return INVALID_ADAPTER;
81 }
82
83 std::wstring Adapters::get_typename(Adapters::adapters_index_t kind)
84 {
85     for (auto it : adapters)
86     {
87         if (it.kind == kind)
88         {
89             return it.name;
90         }
91     }
92
93     return L"";
94 }
95
96 model::BaseObject* Adapters::descriptor(types::InternalType* v)
97 {
98     const std::wstring& name = v->getShortTypeStr();
99     adapters_t::iterator it = std::lower_bound(adapters.begin(), adapters.end(), name);
100     if (v->isUserType() && it != adapters.end() && !(name < it->name))
101     {
102         return descriptor(it->kind, v);
103     }
104     return nullptr;
105 }
106
107 model::BaseObject* Adapters::descriptor(adapters_index_t index, types::InternalType* v)
108 {
109     switch (index)
110     {
111         case BLOCK_ADAPTER:
112             return v->getAs<view_scilab::BlockAdapter>()->getAdaptee();
113         case CPR_ADAPTER:
114             return v->getAs<view_scilab::CprAdapter>()->getAdaptee();
115         case DIAGRAM_ADAPTER:
116             return v->getAs<view_scilab::DiagramAdapter>()->getAdaptee();
117         case GRAPHIC_ADAPTER:
118             return v->getAs<view_scilab::GraphicsAdapter>()->getAdaptee();
119         case LINK_ADAPTER:
120             return v->getAs<view_scilab::LinkAdapter>()->getAdaptee();
121         case MODEL_ADAPTER:
122             return v->getAs<view_scilab::ModelAdapter>()->getAdaptee();
123         case PARAMS_ADAPTER:
124             return v->getAs<view_scilab::ParamsAdapter>()->getAdaptee();
125         case SCS_ADAPTER:
126             return v->getAs<view_scilab::ScsAdapter>()->getAdaptee();
127         case STATE_ADAPTER:
128             return v->getAs<view_scilab::StateAdapter>()->getAdaptee();
129         case TEXT_ADAPTER:
130             return v->getAs<view_scilab::TextAdapter>()->getAdaptee();
131         default:
132             return nullptr;
133     }
134 }
135
136 types::InternalType* Adapters::allocate_view(ScicosID id, kind_t kind)
137 {
138     Controller controller;
139
140     return allocate_view(controller, controller.getBaseObject(id));
141 }
142
143 types::InternalType* Adapters::allocate_view(Controller& controller, model::BaseObject* o)
144 {
145     switch (o->kind())
146     {
147         case BLOCK:
148                 return new view_scilab::BlockAdapter(controller, static_cast<model::Block*>(o));
149         case DIAGRAM:
150                 return new view_scilab::DiagramAdapter(controller, static_cast<model::Diagram*>(o));
151         case ANNOTATION:
152                 return new view_scilab::TextAdapter(controller, static_cast<model::Annotation*>(o));
153         case LINK:
154             return new view_scilab::LinkAdapter(controller, static_cast<model::Link*>(o));
155         default:
156             return nullptr;
157     }
158 }
159
160 } /* namespace view_scilab */
161 } /* namespace org_scilab_modules_scicos */