]> SALOME platform Git repositories - modules/yacs.git/blob - src/py2yacsgui/Py2YacsModel.cxx
Salome HOME
Merge branch 'omu/py2yacsgui'
[modules/yacs.git] / src / py2yacsgui / Py2YacsModel.cxx
1 // Copyright (C) 2016-2017  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   std::stringstream buffer;
86   _script = texte;
87   emit scriptChanged(_script.c_str());
88   _functionName = "";
89   try
90   {
91     _data.load(_script);
92     
93     // get the errors
94     buffer.clear();
95     const std::list<std::string>& globalErrors = _data.getGlobalErrors();
96     if(! globalErrors.empty())
97     {
98       buffer << "Global errors:" << std::endl;
99       std::list<std::string>::const_iterator it;
100       for(it=globalErrors.begin(); it!=globalErrors.end(); it++)
101       {
102         buffer << *it << std::endl;
103       }
104       buffer << "-----------------------------------------" << std::endl;
105     }
106     
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++)
110     {
111       if(! it_fp->_errors.empty())
112       {
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;
119       }
120     }
121     _lastError = buffer.str();
122   }
123   catch(Py2yacsException& e)
124   {
125     _lastError = e.what();
126     _lastError += "\n";
127   }
128   emit errorChanged(_lastError.c_str());
129   emit functionsChanged(getValidFunctionNames());
130 }
131
132 const std::string& Py2YacsModel::getScript()
133 {
134   return _script;
135 }
136
137 const std::string& Py2YacsModel::getLastError()
138 {
139   return _lastError;
140 }
141
142 bool Py2YacsModel::exportToXml(const std::string& path)
143 {
144   bool ok = true;
145   if(schemaAvailable())
146   {
147     try
148     {
149     _data.save(path, _functionName);
150     }
151     catch(Py2yacsException& e)
152     {
153       _lastError = e.what();
154       _lastError += "\n";
155       ok = false;
156     }
157   }
158   emit errorChanged(_lastError.c_str());
159   return ok;
160 }
161
162 void Py2YacsModel::setFunctionName(const QString& functionName)
163 {
164   _functionName = functionName.toStdString();
165 }
166
167 void Py2YacsModel::setFunctionName(const std::string& functionName)
168 {
169   std::list<std::string> validFunctionNames = getValidFunctionNames();
170   if(std::find(validFunctionNames.begin(),
171                validFunctionNames.end(),
172                functionName)               != validFunctionNames.end())
173   {
174     _functionName = functionName;
175   }
176   else
177   {
178     _lastError = "Invalid function name!";
179     emit errorChanged(_lastError.c_str());
180   }
181 }
182
183 std::list<std::string> Py2YacsModel::getValidFunctionNames()
184 {
185   std::list<std::string> result;
186   const std::list<std::string>& globalErrors = _data.getGlobalErrors();
187   if(globalErrors.empty())
188   {
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++)
192     {
193       if(it_fp->_errors.empty())
194       {
195         result.push_back(it_fp->_name);
196       }
197     }
198   }
199   return result;
200 }
201
202 bool Py2YacsModel::savePossible()
203 {
204   return !_loadedFile.empty();
205 }
206
207 bool Py2YacsModel::schemaAvailable()
208 {
209   return !_functionName.empty();
210 }
211
212 YACS::ENGINE::Proc* Py2YacsModel::getProc()
213 {
214   YACS::ENGINE::Proc* result = NULL;
215   if(schemaAvailable())
216   {
217     try
218     {
219       result = _data.createProc(_functionName);
220     }
221     catch(Py2yacsException& e)
222     {
223       _lastError = e.what();
224       _lastError += "\n";
225     }
226   }
227   return result;
228 }