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