Xcos MVC: implement XCOS / ZCOS file saving
[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-2014 - Scilab Enterprises - Clement DAVID
4  *
5  *  This file must be used under the terms of the CeCILL.
6  *  This source file is licensed as described in the file COPYING, which
7  *  you should have received as part of this distribution.  The terms
8  *  are also available at
9  *  http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
10  *
11  */
12
13 #include "../../../includes/view_scilab/Adapters.hxx"
14
15 #include <string>
16 #include <algorithm>
17
18 #include "BlockAdapter.hxx"
19 #include "CprAdapter.hxx"
20 #include "DiagramAdapter.hxx"
21 #include "GraphicsAdapter.hxx"
22 #include "LinkAdapter.hxx"
23 #include "ModelAdapter.hxx"
24 #include "ParamsAdapter.hxx"
25 #include "ScsAdapter.hxx"
26 #include "StateAdapter.hxx"
27 #include "TextAdapter.hxx"
28
29 namespace org_scilab_modules_scicos
30 {
31 namespace view_scilab
32 {
33
34 Adapters Adapters::m_instance = Adapters();
35
36 Adapters& Adapters::instance()
37 {
38     return m_instance;
39 }
40
41 Adapters::Adapters()
42 {
43     /*
44      * Create the supported data types list
45      */
46     adapters = adapters_t();
47     // we assume that the INVALID_ADAPTER is set at end of the enum
48     adapters.reserve(INVALID_ADAPTER);
49
50     adapters.push_back(adapter_t(view_scilab::BlockAdapter::getSharedTypeStr(), BLOCK_ADAPTER));
51     adapters.push_back(adapter_t(view_scilab::CprAdapter::getSharedTypeStr(), CPR_ADAPTER));
52     adapters.push_back(adapter_t(view_scilab::DiagramAdapter::getSharedTypeStr(), DIAGRAM_ADAPTER));
53     adapters.push_back(adapter_t(view_scilab::GraphicsAdapter::getSharedTypeStr(), GRAPHIC_ADAPTER));
54     adapters.push_back(adapter_t(view_scilab::LinkAdapter::getSharedTypeStr(), LINK_ADAPTER));
55     adapters.push_back(adapter_t(view_scilab::ModelAdapter::getSharedTypeStr(), MODEL_ADAPTER));
56     adapters.push_back(adapter_t(view_scilab::ParamsAdapter::getSharedTypeStr(), PARAMS_ADAPTER));
57     adapters.push_back(adapter_t(view_scilab::ScsAdapter::getSharedTypeStr(), SCS_ADAPTER));
58     adapters.push_back(adapter_t(view_scilab::StateAdapter::getSharedTypeStr(), STATE_ADAPTER));
59     adapters.push_back(adapter_t(view_scilab::TextAdapter::getSharedTypeStr(), TEXT_ADAPTER));
60
61     std::sort(adapters.begin(), adapters.end());
62 }
63
64 Adapters::~Adapters()
65 {
66     adapters.clear();
67 }
68
69 Adapters::adapters_index_t Adapters::lookup_by_typename(const std::wstring& name)
70 {
71     adapters_t::iterator it = std::lower_bound(adapters.begin(), adapters.end(), name);
72     if (it != adapters.end() && !(name < it->name))
73     {
74         return it->kind;
75     }
76     return INVALID_ADAPTER;
77 }
78
79
80 std::wstring Adapters::get_typename(Adapters::adapters_index_t kind)
81 {
82     for (auto it : adapters)
83     {
84         if (it.kind == kind)
85         {
86             return it.name;
87         }
88     }
89
90     return L"";
91 }
92
93
94 const model::BaseObject* Adapters::descriptor(types::InternalType* v)
95 {
96     const std::wstring& name = v->getShortTypeStr();
97     adapters_t::iterator it = std::lower_bound(adapters.begin(), adapters.end(), name);
98     if (v->isUserType() && it != adapters.end() && !(name < it->name))
99     {
100         switch (it->kind)
101         {
102             case BLOCK_ADAPTER:
103                 return v->getAs<view_scilab::BlockAdapter>()->getAdaptee();
104             case CPR_ADAPTER:
105                 return v->getAs<view_scilab::CprAdapter>()->getAdaptee();
106             case DIAGRAM_ADAPTER:
107                 return v->getAs<view_scilab::DiagramAdapter>()->getAdaptee();
108             case GRAPHIC_ADAPTER:
109                 return v->getAs<view_scilab::GraphicsAdapter>()->getAdaptee();
110             case LINK_ADAPTER:
111                 return v->getAs<view_scilab::LinkAdapter>()->getAdaptee();
112             case MODEL_ADAPTER:
113                 return v->getAs<view_scilab::ModelAdapter>()->getAdaptee();
114             case PARAMS_ADAPTER:
115                 return v->getAs<view_scilab::ParamsAdapter>()->getAdaptee();
116             case SCS_ADAPTER:
117                 return v->getAs<view_scilab::ScsAdapter>()->getAdaptee();
118             case STATE_ADAPTER:
119                 return v->getAs<view_scilab::StateAdapter>()->getAdaptee();
120             case TEXT_ADAPTER:
121                 return v->getAs<view_scilab::TextAdapter>()->getAdaptee();
122             default:
123                 return nullptr;
124         }
125     }
126
127     return nullptr;
128 }
129
130 types::InternalType* Adapters::allocate_view(ScicosID id, kind_t kind)
131 {
132     Controller controller;
133
134     switch (kind)
135     {
136         case BLOCK:
137             return new view_scilab::BlockAdapter(controller, static_cast<model::Block*>(controller.getObject(id)));
138         case LINK:
139             return new view_scilab::LinkAdapter(controller, static_cast<model::Link*>(controller.getObject(id)));
140         case DIAGRAM:
141             return new view_scilab::DiagramAdapter(controller, static_cast<model::Diagram*>(controller.getObject(id)));
142         default:
143             return nullptr;
144     }
145 }
146
147 } /* namespace view_scilab */
148 } /* namespace org_scilab_modules_scicos */