1 // Copyright (C) 2016-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "Py2YacsModel.hxx"
26 Py2YacsModel::Py2YacsModel()
34 Py2YacsModel::~Py2YacsModel()
38 void Py2YacsModel::loadFile(const std::string& path)
40 std::ifstream file_stream(path);
41 std::stringstream buffer;
44 buffer << "Error when opening file " << path
45 << ": " << strerror(errno)
47 _lastError = buffer.str();
48 emit errorChanged(_lastError.c_str());
52 buffer << file_stream.rdbuf();
54 setScript(buffer.str());
58 void Py2YacsModel::save()
66 void Py2YacsModel::saveAs(const std::string& path)
68 std::ofstream file_stream(path);
69 file_stream << _script;
72 std::stringstream buffer;
73 buffer << "Error when writing to file " << path
74 << ": " << strerror(errno)
76 _lastError = buffer.str();
77 emit errorChanged(_lastError.c_str());
83 void Py2YacsModel::setScript(const std::string& texte)
85 std::stringstream buffer;
87 emit scriptChanged(_script.c_str());
95 const std::list<std::string>& globalErrors = _data.getGlobalErrors();
96 if(! globalErrors.empty())
98 buffer << "Global errors:" << std::endl;
99 std::list<std::string>::const_iterator it;
100 for(it=globalErrors.begin(); it!=globalErrors.end(); it++)
102 buffer << *it << std::endl;
104 buffer << "-----------------------------------------" << std::endl;
107 std::list<FunctionProperties>::const_iterator it_fp;
108 const std::list<FunctionProperties>& functions = _data.getFunctionProperties();
109 for(it_fp=functions.begin();it_fp!=functions.end();it_fp++)
111 if(! it_fp->_errors.empty())
113 buffer << "Function " << it_fp->_name << " has errors:" << std::endl;
114 std::list<std::string>::const_iterator it;
115 buffer << "Errors :" ;
116 for(it=it_fp->_errors.begin();it!=it_fp->_errors.end();it++)
117 buffer << *it << std::endl;
118 buffer << "-----------------------------------------" << std::endl;
121 _lastError = buffer.str();
123 catch(Py2yacsException& e)
125 _lastError = e.what();
128 emit errorChanged(_lastError.c_str());
129 emit functionsChanged(getValidFunctionNames());
132 const std::string& Py2YacsModel::getScript()
137 const std::string& Py2YacsModel::getLastError()
142 bool Py2YacsModel::exportToXml(const std::string& path)
145 if(schemaAvailable())
149 _data.save(path, _functionName);
151 catch(Py2yacsException& e)
153 _lastError = e.what();
158 emit errorChanged(_lastError.c_str());
162 void Py2YacsModel::setFunctionName(const QString& functionName)
164 _functionName = functionName.toStdString();
167 void Py2YacsModel::setFunctionName(const std::string& functionName)
169 std::list<std::string> validFunctionNames = getValidFunctionNames();
170 if(std::find(validFunctionNames.begin(),
171 validFunctionNames.end(),
172 functionName) != validFunctionNames.end())
174 _functionName = functionName;
178 _lastError = "Invalid function name!";
179 emit errorChanged(_lastError.c_str());
183 std::list<std::string> Py2YacsModel::getValidFunctionNames()
185 std::list<std::string> result;
186 const std::list<std::string>& globalErrors = _data.getGlobalErrors();
187 if(globalErrors.empty())
189 std::list<FunctionProperties>::const_iterator it_fp;
190 const std::list<FunctionProperties>& functions = _data.getFunctionProperties();
191 for(it_fp=functions.begin();it_fp!=functions.end();it_fp++)
193 if(it_fp->_errors.empty())
195 result.push_back(it_fp->_name);
202 bool Py2YacsModel::savePossible()
204 return !_loadedFile.empty();
207 bool Py2YacsModel::schemaAvailable()
209 return !_functionName.empty();
212 YACS::ENGINE::Proc* Py2YacsModel::getProc()
214 YACS::ENGINE::Proc* result = NULL;
215 if(schemaAvailable())
219 result = _data.createProc(_functionName);
221 catch(Py2yacsException& e)
223 _lastError = e.what();