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)
86 emit scriptChanged(_script.c_str());
91 _lastError = _data.getAllErrors();
93 catch(Py2yacsException& e)
95 _lastError = e.what();
98 emit errorChanged(_lastError.c_str());
99 emit functionsChanged(getValidFunctionNames());
102 const std::string& Py2YacsModel::getScript()
107 const std::string& Py2YacsModel::getLastError()
112 bool Py2YacsModel::exportToXml(const std::string& path)
115 if(schemaAvailable())
119 _data.save(path, _functionName);
121 catch(Py2yacsException& e)
123 _lastError = e.what();
128 emit errorChanged(_lastError.c_str());
132 void Py2YacsModel::setFunctionName(const QString& functionName)
134 _functionName = functionName.toStdString();
137 void Py2YacsModel::setFunctionName(const std::string& functionName)
139 std::list<std::string> validFunctionNames = getValidFunctionNames();
140 if(std::find(validFunctionNames.begin(),
141 validFunctionNames.end(),
142 functionName) != validFunctionNames.end())
144 _functionName = functionName;
148 _lastError = "Invalid function name!";
149 emit errorChanged(_lastError.c_str());
153 std::list<std::string> Py2YacsModel::getValidFunctionNames()
155 std::list<std::string> result;
156 const std::list<std::string>& globalErrors = _data.getGlobalErrors();
157 if(globalErrors.empty())
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++)
163 if(it_fp->_errors.empty())
165 result.push_back(it_fp->_name);
172 bool Py2YacsModel::savePossible()
174 return !_loadedFile.empty();
177 bool Py2YacsModel::schemaAvailable()
179 return !_functionName.empty();
182 YACS::ENGINE::Proc* Py2YacsModel::getProc()
184 YACS::ENGINE::Proc* result = NULL;
185 if(schemaAvailable())
189 result = _data.createProc(_functionName);
191 catch(Py2yacsException& e)
193 _lastError = e.what();