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