Salome HOME
c749d0618ba7687edc20fe180961fd2b39d619e3
[modules/yacs.git] / src / py2yacsgui / Py2YacsModel.cxx
1 // Copyright (C) 2016-2022  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #include "Py2YacsModel.hxx"
20 #include <fstream>
21 #include <sstream>
22 #include <cerrno>
23 #include <algorithm>
24 #include <cstring>
25
26 Py2YacsModel::Py2YacsModel()
27 : _data(),
28   _loadedFile(),
29   _script(),
30   _lastError()
31 {
32 }
33
34 Py2YacsModel::~Py2YacsModel()
35 {
36 }
37
38 void Py2YacsModel::loadFile(const std::string& path)
39 {
40   std::ifstream file_stream(path);
41   std::stringstream buffer;
42   if(!file_stream)
43   {
44     buffer << "Error when opening file " << path
45              << ": " << strerror(errno)
46              << std::endl;
47     _lastError = buffer.str();
48     emit errorChanged(_lastError.c_str());
49   }
50   else
51   {
52     buffer << file_stream.rdbuf();
53     _loadedFile = path;
54     setScript(buffer.str());
55   }
56 }
57
58 void Py2YacsModel::save()
59 {
60   if(savePossible())
61   {
62     saveAs(_loadedFile);
63   }
64 }
65
66 void Py2YacsModel::saveAs(const std::string& path)
67 {
68   std::ofstream file_stream(path);
69   file_stream << _script;
70   if(!file_stream)
71   {
72     std::stringstream buffer;
73     buffer << "Error when writing to file " << path
74            << ": " << strerror(errno)
75            << std::endl;
76     _lastError = buffer.str();
77     emit errorChanged(_lastError.c_str());
78   }
79   else
80     _loadedFile = path;
81 }
82
83 void Py2YacsModel::setScript(const std::string& texte)
84 {
85   _script = texte;
86   emit scriptChanged(_script.c_str());
87   _functionName = "";
88   try
89   {
90     _data.load(_script);
91     _lastError = _data.getAllErrors();
92   }
93   catch(Py2yacsException& e)
94   {
95     _lastError = e.what();
96     _lastError += "\n";
97   }
98   emit errorChanged(_lastError.c_str());
99   emit functionsChanged(getValidFunctionNames());
100 }
101
102 const std::string& Py2YacsModel::getScript()
103 {
104   return _script;
105 }
106
107 const std::string& Py2YacsModel::getLastError()
108 {
109   return _lastError;
110 }
111
112 bool Py2YacsModel::exportToXml(const std::string& path)
113 {
114   bool ok = true;
115   if(schemaAvailable())
116   {
117     try
118     {
119     _data.save(path, _functionName);
120     }
121     catch(Py2yacsException& e)
122     {
123       _lastError = e.what();
124       _lastError += "\n";
125       ok = false;
126     }
127   }
128   emit errorChanged(_lastError.c_str());
129   return ok;
130 }
131
132 void Py2YacsModel::setFunctionName(const QString& functionName)
133 {
134   _functionName = functionName.toStdString();
135 }
136
137 void Py2YacsModel::setFunctionName(const std::string& functionName)
138 {
139   std::list<std::string> validFunctionNames = getValidFunctionNames();
140   if(std::find(validFunctionNames.begin(),
141                validFunctionNames.end(),
142                functionName)               != validFunctionNames.end())
143   {
144     _functionName = functionName;
145   }
146   else
147   {
148     _lastError = "Invalid function name!";
149     emit errorChanged(_lastError.c_str());
150   }
151 }
152
153 std::list<std::string> Py2YacsModel::getValidFunctionNames()
154 {
155   std::list<std::string> result;
156   const std::list<std::string>& globalErrors = _data.getGlobalErrors();
157   if(globalErrors.empty())
158   {
159     std::list<FunctionProperties>::const_iterator it_fp;
160     const std::list<FunctionProperties>& functions = _data.getFunctionProperties();
161     for(it_fp=functions.begin();it_fp!=functions.end();it_fp++)
162     {
163       if(it_fp->_errors.empty())
164       {
165         result.push_back(it_fp->_name);
166       }
167     }
168   }
169   return result;
170 }
171
172 bool Py2YacsModel::savePossible()
173 {
174   return !_loadedFile.empty();
175 }
176
177 bool Py2YacsModel::schemaAvailable()
178 {
179   return !_functionName.empty();
180 }
181
182 YACS::ENGINE::Proc* Py2YacsModel::getProc()
183 {
184   YACS::ENGINE::Proc* result = NULL;
185   if(schemaAvailable())
186   {
187     try
188     {
189       result = _data.createProc(_functionName);
190     }
191     catch(Py2yacsException& e)
192     {
193       _lastError = e.what();
194       _lastError += "\n";
195     }
196   }
197   return result;
198 }