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