Salome HOME
fb43a48788639b3c0da5d09b9e607a3dc1fb95e2
[modules/homard.git] / src / HOMARDGUI / MonCreateYACS.cxx
1 // Copyright (C) 2011-2014  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
20 #include "MonCreateYACS.h"
21
22 #include <QFileDialog>
23 #include <QMessageBox>
24
25 #include "SalomeApp_Tools.h"
26 #include "HOMARDGUI_Utils.h"
27 #include "HomardQtCommun.h"
28 #include <utilities.h>
29
30 #ifdef WIN32
31 #include <direct.h>
32 #endif
33 using namespace std;
34
35 // ----------------------------------------------------------------------
36 MonCreateYACS::MonCreateYACS (bool modal, HOMARD::HOMARD_Gen_var myHomardGen0, QString CaseName ):
37 // ----------------------------------------------------------------------
38 /* Constructs a MonCreateYACS
39  * Sets attributes to default values
40  */
41 // ----------------------------------------------------------------------
42   Ui_CreateYACS(),
43   _aCaseName(CaseName),
44   _aScriptFile(""),
45   _aDirName(""),
46   _aMeshFile(""),
47   _Type(1)
48   // Les valeurs de _Type, _MaxIter, _MaxNode, _MaxElem doivent etre les memes que celles dans HOMARD_Gen_i::CreateYACSSchema
49   // et doivent correspondre aux defauts des boutons
50   {
51 //     MESSAGE("Debut du constructeur de MonCreateYACS");
52     myHomardGen=HOMARD::HOMARD_Gen::_duplicate(myHomardGen0);
53     setupUi(this);
54     if ( modal ) { setWindowModality(Qt::WindowModal); }
55     else         { setWindowModality(Qt::NonModal); }
56
57     InitConnect();
58
59     SetNewName() ;
60
61     if (_aCaseName != QString("")) { SetCaseName(); }
62     else                           { setWindowModality(Qt::NonModal); /* permet selection du cas dans l arbre d etude */}
63 //
64 //  Les valeurs definies dans les preferences
65     _MaxIter = myHomardGen->GetYACSMaxIter();
66     _MaxNode = myHomardGen->GetYACSMaxNode();
67     _MaxElem = myHomardGen->GetYACSMaxElem();
68     MESSAGE ("Valeur par defaut de MaxIter = " << _MaxIter<<", MaxNode = "<< _MaxNode<<", MaxElem = "<< _MaxElem);
69     SpinBoxMaxIter->setValue(_MaxIter) ;
70     SpinBoxMaxNode->setValue(_MaxNode) ;
71     SpinBoxMaxElem->setValue(_MaxElem) ;
72 //
73     adjustSize();
74   }
75
76 // ----------------------------------------------------------------------
77 MonCreateYACS::MonCreateYACS(HOMARD::HOMARD_Gen_var myHomardGen0,
78                              QString caseName):
79 // ----------------------------------------------------------------------
80 // Constructeur appele par MonEditYACS
81 //
82 myHomardGen(myHomardGen0),
83 _Name (""),
84 Chgt (false)
85 {
86 //       MESSAGE("Debut du constructeur de MonCreateYACS appele par MonEditYACS");
87   setupUi(this) ;
88
89   setWindowModality(Qt::WindowModal);
90   InitConnect() ;
91 //
92   adjustSize();
93 }
94 // ------------------------------------------------------------------------
95 MonCreateYACS::~MonCreateYACS()
96 // ------------------------------------------------------------------------
97 {
98     // no need to delete child widgets, Qt does it all for us
99 }
100 // ------------------------------------------------------------------------
101 void MonCreateYACS::InitConnect()
102 // ------------------------------------------------------------------------
103 {
104     connect( PBCaseName,     SIGNAL(pressed()), this, SLOT(SetCaseName()));
105     connect( PBScriptFile,   SIGNAL(pressed()), this, SLOT(SetScriptFile()));
106     connect( PBDir,          SIGNAL(pressed()), this, SLOT(SetDirName()));
107     connect( PBMeshFile,     SIGNAL(pressed()), this, SLOT(SetMeshFile()));
108
109     connect( RBConstant,     SIGNAL(clicked()), this, SLOT(SetConstant()));
110     connect( RBVariable,     SIGNAL(clicked()), this, SLOT(SetVariable()));
111
112     connect( buttonOk,       SIGNAL(pressed()), this, SLOT(PushOnOK()));
113     connect( buttonApply,    SIGNAL(pressed()), this, SLOT(PushOnApply()));
114     connect( buttonCancel,   SIGNAL(pressed()), this, SLOT(close()));
115     connect( buttonHelp,     SIGNAL(pressed()), this, SLOT(PushOnHelp()));
116 }
117 // -------------------------------
118 bool MonCreateYACS::PushOnApply()
119 // --------------------------------
120 {
121   MESSAGE("PushOnApply");
122
123 // Le fichier du script
124   QString aFileName=LEScriptFile->text().trimmed();
125   if (aFileName ==QString(""))
126   {
127     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
128                               QObject::tr("HOM_SCRIPT_FILE") );
129     return false;
130   }
131
132 // Le repertoire de calcul
133   QString aDirName=LEDirName->text().trimmed();
134   if (aDirName == QString(""))
135   {
136     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
137                               QObject::tr("HOM_CASE_DIRECTORY_4") );
138     return false;
139   }
140   if ( aDirName != _aDirName)
141   { QString CaseNameDir = myHomardGen->VerifieDir( aDirName.toStdString().c_str()) ;
142     if ( CaseNameDir != "" )
143     {
144       QString texte ;
145       texte = QObject::tr("HOM_CASE_DIRECTORY_2") + CaseNameDir ;
146       QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
147                                 texte );
148       return false;
149     }
150   }
151 #ifndef WIN32
152   if (chdir(aDirName.toStdString().c_str()) != 0)
153 #else
154   if (_chdir(aDirName.toStdString().c_str()) != 0)
155 #endif
156   {
157     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
158                               QObject::tr("HOM_CASE_DIRECTORY_3") );
159     return false;
160   }
161
162 // Le fichier du tout premier maillage
163   aFileName=LEMeshFile->text().trimmed();
164   if (aFileName ==QString(""))
165   {
166     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
167                               QObject::tr("HOM_CASE_MESH") );
168     return false;
169   }
170   QString aMeshName = HOMARD_QT_COMMUN::LireNomMaillage(aFileName);
171   if (aMeshName == "" )
172   {
173     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
174                               QObject::tr("HOM_MED_FILE_2") );
175     return false;
176   }
177
178   bool bOK = CreateOrUpdate() ;
179
180   if ( bOK ) { HOMARD_UTILS::updateObjBrowser() ; }
181
182   return bOK;
183 }
184 // ---------------------------------------------------
185 bool MonCreateYACS:: CreateOrUpdate()
186 //----------------------------------------------------
187 //  Creation ou modification du schema
188 {
189   MESSAGE("CreateOrUpdate");
190   bool bOK = true ;
191
192   // 1. Verification des donnees
193   // 1.1. Le cas
194   if ( _aCaseName == QString (""))
195   {
196     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
197                               QObject::tr("HOM_CASE_NAME") );
198     return false;
199   }
200   // 1.2. Les donnees
201   QString aScriptFile=LEScriptFile->text().trimmed();
202   if ( aScriptFile != _aScriptFile )
203   {
204     _aScriptFile = aScriptFile ;
205     Chgt = true ;
206   }
207   QString aDirName=LEDirName->text().trimmed();
208   if ( aDirName != _aDirName )
209   {
210     _aDirName = aDirName ;
211     Chgt = true ;
212   }
213   QString aMeshFile=LEMeshFile->text().trimmed();
214   if ( aMeshFile != _aMeshFile )
215   {
216     _aMeshFile = aMeshFile ;
217     Chgt = true ;
218   }
219
220   // 2. Creation de l'objet CORBA
221   try
222   {
223     _Name=LEName->text().trimmed();
224     aYACS=myHomardGen->CreateYACSSchema(CORBA::string_dup(_Name.toStdString().c_str()), CORBA::string_dup(_aCaseName.toStdString().c_str()), CORBA::string_dup(_aScriptFile.toStdString().c_str()), CORBA::string_dup(_aDirName.toStdString().c_str()), CORBA::string_dup(_aMeshFile.toStdString().c_str()));
225   }
226   catch( SALOME::SALOME_Exception& S_ex )
227   {
228     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
229                               QObject::tr(CORBA::string_dup(S_ex.details.text)) );
230     bOK = false;
231   }
232
233   // 3. Options
234   if ( bOK )
235   {
236   // 3.1. Le type du schema
237     aYACS->SetType(_Type) ;
238
239   // 3.2. Les maximums
240     _MaxIter = SpinBoxMaxIter->value() ;
241     aYACS->SetMaxIter(_MaxIter) ;
242
243     _MaxNode = SpinBoxMaxNode->value() ;
244     aYACS->SetMaxNode(_MaxNode) ;
245
246     _MaxElem = SpinBoxMaxElem->value() ;
247     aYACS->SetMaxElem(_MaxElem) ;
248
249   }
250
251   // 4. Ecriture du fichier
252   if ( bOK )
253   {
254     int codret = aYACS->Write() ;
255     if ( codret != 0 ) { bOK = false ; }
256   }
257
258   return bOK;
259 }
260 // ---------------------------
261 void MonCreateYACS::PushOnOK()
262 // ---------------------------
263 {
264   bool bOK = PushOnApply();
265   if ( bOK ) this->close();
266 }
267 //------------------------------
268 void MonCreateYACS::PushOnHelp()
269 //-------------------------------
270 {
271   std::string LanguageShort = myHomardGen->GetLanguageShort();
272   HOMARD_UTILS::PushOnHelp(QString("yacs.html"), QString(""), QString(LanguageShort.c_str()));
273 }
274 // -------------------------------------------------
275 void MonCreateYACS::SetNewName()
276 // --------------------------------------------------
277 {
278
279   HOMARD::listeYACSs_var MyObjects = myHomardGen->GetAllYACSsName();
280   int num = 0; QString aName="";
281   while (aName == QString("") )
282   {
283     aName.setNum(num+1) ;
284     aName.insert(0, QString("YACS_")) ;
285     for ( int i=0; i<MyObjects->length(); i++)
286     {
287       if ( aName ==  QString(MyObjects[i]))
288       {
289         num ++ ;
290         aName = "" ;
291         break ;
292       }
293    }
294   }
295   LEName->setText(aName);
296 }
297 // ------------------------------------------------------------------------
298 void MonCreateYACS::SetCaseName()
299 // ------------------------------------------------------------------------
300 {
301   MESSAGE ("SetCaseName avec _aCaseName = " << _aCaseName.toStdString() );
302   if (_aCaseName == QString(""))
303   {
304     _aCaseName=HOMARD_QT_COMMUN::SelectionArbreEtude(QString("CasHomard"), 1);
305     if (_aCaseName == QString("")) { raise();return;};
306   }
307   LECaseName->setText(_aCaseName);
308 }
309 // ------------------------------------------------------------------------
310 void MonCreateYACS::SetDirName()
311 // ------------------------------------------------------------------------
312 {
313   QString aDirName=QFileDialog::getExistingDirectory ();
314   if (!(aDirName.isEmpty()))LEDirName->setText(aDirName);
315 }
316 // ------------------------------------------------------------------------
317 void MonCreateYACS::SetScriptFile()
318 // ------------------------------------------------------------------------
319 {
320   QString fileName0 = LEScriptFile->text().trimmed();
321   QString fileName = HOMARD_QT_COMMUN::PushNomFichier( false, QString("py") ) ;
322   if (fileName.isEmpty()) fileName = fileName0 ;
323   LEScriptFile->setText(fileName);
324 }
325 // ------------------------------------------------------------------------
326 void MonCreateYACS::SetMeshFile()
327 // ------------------------------------------------------------------------
328 {
329   QString fileName0 = LEMeshFile->text().trimmed();
330   QString fileName = HOMARD_QT_COMMUN::PushNomFichier( false, QString("med") ) ;
331   if (fileName.isEmpty()) fileName = fileName0 ;
332   LEMeshFile->setText(fileName);
333 }
334 // ------------------------------------------------------------------------
335 void MonCreateYACS::SetConstant()
336 // ------------------------------------------------------------------------
337 {
338   _Type = 1 ;
339 }
340 // ------------------------------------------------------------------------
341 void MonCreateYACS::SetVariable()
342 // ------------------------------------------------------------------------
343 {
344   _Type = 2 ;
345 }