Salome HOME
6d98b287a08ed44f9d69738daf551492ca129613
[modules/homard.git] / src / HOMARDGUI / MonPursueIteration.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 "MonPursueIteration.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
34 using namespace std;
35
36 // -----------------------------------------------------------------------------------------
37 /* Constructs a MonPursueIteration
38  * Sets attributes to default values
39  */
40 // -----------------------------------------------------------------------------------------
41 MonPursueIteration::MonPursueIteration ( bool modal, HOMARD::HOMARD_Gen_var myHomardGen0 )
42     :
43     Ui_PursueIteration(),
44     _aCaseName(""), _aDirName(""), _aDirNameStart("")
45 {
46   MESSAGE("Debut du constructeur de MonPursueIteration");
47   myHomardGen=HOMARD::HOMARD_Gen::_duplicate(myHomardGen0);
48   setupUi(this);
49   setModal(modal);
50
51   InitConnect();
52
53   SetNewCaseName() ;
54   _Type = 1 ;
55   GBIterationintoCase->setVisible(0);
56   spinBoxNumber->setVisible(0);
57
58   adjustSize();
59   MESSAGE("Fin du constructeur de MonPursueIteration");
60 }
61
62 // ------------------------------------------------------------------------
63 MonPursueIteration::~MonPursueIteration()
64 // ------------------------------------------------------------------------
65 {
66     // no need to delete child widgets, Qt does it all for us
67 }
68 // ------------------------------------------------------------------------
69 void MonPursueIteration::InitConnect()
70 // ------------------------------------------------------------------------
71 {
72     connect( PushDir,        SIGNAL(pressed()), this, SLOT(SetDirName()));
73
74     connect( RBIteration,    SIGNAL(clicked()), this, SLOT(FromIteration()));
75     connect( RBCase,         SIGNAL(clicked()), this, SLOT(FromCase()));
76     connect( PushDirStart,   SIGNAL(pressed()), this, SLOT(SetDirNameStart()));
77
78     connect( RBCaseLastIteration, SIGNAL(clicked()), this, SLOT(CaseLastIteration()));
79     connect( RBCaseNIteration,    SIGNAL(clicked()), this, SLOT(CaseNIteration()));
80
81     connect( buttonOk,       SIGNAL(pressed()), this, SLOT(PushOnOK()));
82     connect( buttonApply,    SIGNAL(pressed()), this, SLOT(PushOnApply()));
83     connect( buttonCancel,   SIGNAL(pressed()), this, SLOT(close()));
84     connect( buttonHelp,     SIGNAL(pressed()), this, SLOT(PushOnHelp()));
85 }
86 // -------------------------------
87 bool MonPursueIteration::PushOnApply()
88 // --------------------------------
89 {
90   MESSAGE("PushOnApply");
91 // 1. Enregistrement du repertoire du cas
92   QString aDirName=LEDirName->text().trimmed();
93   if (aDirName == QString(""))
94   {
95     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
96                               QObject::tr("HOM_CASE_DIRECTORY_1") );
97     return false;
98   }
99   if ( aDirName != _aDirName)
100   { QString CaseNameDir = myHomardGen->VerifieDir( aDirName.toStdString().c_str()) ;
101     if ( CaseNameDir != "" )
102     {
103       QString texte ;
104       texte = QObject::tr("HOM_CASE_DIRECTORY_2") + CaseNameDir ;
105       QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
106                                 texte );
107       return false;
108     }
109   }
110 #ifndef WIN32
111   if (chdir(aDirName.toStdString().c_str()) != 0)
112 #else
113   if (_chdir(aDirName.toStdString().c_str()) != 0)
114 #endif
115   {
116     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
117                               QObject::tr("HOM_CASE_DIRECTORY_3") );
118     return false;
119   }
120 // 2. Enregistrement du repertoire de depart
121   QString aDirNameStart=LEDirNameStart->text().trimmed();
122   if (aDirNameStart == QString(""))
123   {
124     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
125                               QObject::tr("HOM_START_DIRECTORY_1") );
126     return false;
127   }
128 #ifndef WIN32
129   if (chdir(aDirNameStart.toStdString().c_str()) != 0)
130 #else
131   if (_chdir(aDirNameStart.toStdString().c_str()) != 0)
132 #endif
133   {
134     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
135                               QObject::tr("HOM_START_DIRECTORY_3") );
136     return false;
137   }
138
139 // 3. Enregistrement du numero d'iteration
140   int Number ;
141   if ( _Type == 3 ) { Number = spinBoxNumber->value() ; }
142
143 // 4. Creation du cas
144   QString _aCaseName=LECaseName->text().trimmed();
145   _aDirNameStart=aDirNameStart;
146
147   MESSAGE("_aCaseName = "<<_aCaseName.toStdString().c_str());
148   MESSAGE("_aDirNameStart = "<<_aDirNameStart.toStdString().c_str());
149   MESSAGE("_Type = "<<_Type);
150   switch (_Type)
151   {
152     case 1 : // Poursuite a partir d'une iteration
153     {
154       try
155       {
156         MESSAGE("Poursuite a partir d'une iteration");
157         aCase = myHomardGen->CreateCaseFromIteration( \
158                   CORBA::string_dup(_aCaseName.toStdString().c_str()),  \
159                   CORBA::string_dup(_aDirNameStart.toStdString().c_str()) );
160       }
161       catch( SALOME::SALOME_Exception& S_ex )
162       {
163         QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
164                                   QObject::tr(CORBA::string_dup(S_ex.details.text)) );
165         return false ;
166       }
167       break;
168     }
169     case 2 : // Poursuite a partir de la derniere iteration dans un cas
170     {
171       try
172       {
173         MESSAGE("Poursuite a partir de la derniere iteration dans un cas");
174         aCase = myHomardGen->CreateCaseFromCaseLastIteration( \
175                   CORBA::string_dup(_aCaseName.toStdString().c_str()),  \
176                   CORBA::string_dup(_aDirNameStart.toStdString().c_str()) );
177       }
178       catch( SALOME::SALOME_Exception& S_ex )
179       {
180         QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
181                                   QObject::tr(CORBA::string_dup(S_ex.details.text)) );
182         return false ;
183       }
184       break;
185     }
186     case 3 : // Poursuite a partir d'une iteration dans un cas
187     {
188       try
189       {
190         MESSAGE("Poursuite a partir d'une iteration dans un cas");
191         aCase = myHomardGen->CreateCaseFromCaseIteration( \
192                   CORBA::string_dup(_aCaseName.toStdString().c_str()),  \
193                   CORBA::string_dup(_aDirNameStart.toStdString().c_str()),  \
194                   Number );
195       }
196       catch( SALOME::SALOME_Exception& S_ex )
197       {
198         QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
199                                   QObject::tr(CORBA::string_dup(S_ex.details.text)) );
200         return false ;
201       }
202       break;
203     }
204   }
205
206   aCase->SetDirName(aDirName.toStdString().c_str());
207   _aDirName=aDirName;
208
209   HOMARD_UTILS::updateObjBrowser();
210
211   return true;
212 }
213 // ---------------------------
214 void MonPursueIteration::PushOnOK()
215 // ---------------------------
216 {
217   bool bOK = PushOnApply();
218   if ( bOK )  this->close();
219 }
220 //------------------------------
221 void MonPursueIteration::PushOnHelp()
222 //-------------------------------
223 {
224   char* LanguageShort = myHomardGen->GetLanguageShort();
225   HOMARD_UTILS::PushOnHelp(QString("gui_create_case.html"), QString(""), QString(LanguageShort));
226 }
227 // ---------------------------------
228 void MonPursueIteration::SetNewCaseName()
229 // ------------------------------
230 {
231   HOMARD::listeCases_var  MyCases = myHomardGen->GetAllCasesName();
232   int num = 0; QString aCaseName="";
233   while (aCaseName=="" )
234   {
235     aCaseName.setNum(num+1) ;
236     aCaseName.insert(0, QString("Case_")) ;
237     for ( int i=0; i<MyCases->length(); i++)
238     {
239       if ( aCaseName == QString((MyCases)[i]))
240       {
241           num += 1;
242           aCaseName = "";
243           break;
244       }
245    }
246   }
247   LECaseName->clear() ;
248   LECaseName->insert(aCaseName);
249 }
250 // ------------------------------------------------------------------------
251 void MonPursueIteration::SetDirName()
252 // ------------------------------------------------------------------------
253 {
254   QString aDirName=QFileDialog::getExistingDirectory ();
255   if (!(aDirName.isEmpty()))LEDirName->setText(aDirName);
256 }
257 // ------------------------------------------------------------------------
258 void MonPursueIteration::SetDirNameStart()
259 // ------------------------------------------------------------------------
260 {
261   QString aDirName=QFileDialog::getExistingDirectory ();
262   if (!(aDirName.isEmpty()))LEDirNameStart->setText(aDirName);
263 }
264 // ------------------------------------------------------------------------
265 void MonPursueIteration::FromIteration()
266 // ------------------------------------------------------------------------
267 {
268   GBIterationintoCase->setVisible(0);
269   spinBoxNumber->setVisible(0);
270   _Type = 1 ;
271   adjustSize();
272 }
273 // ------------------------------------------------------------------------
274 void MonPursueIteration::FromCase()
275 // ------------------------------------------------------------------------
276 {
277   GBIterationintoCase->setVisible(1);
278   CaseLastIteration();
279 }
280 // ------------------------------------------------------------------------
281 void MonPursueIteration::CaseLastIteration()
282 // ------------------------------------------------------------------------
283 {
284   spinBoxNumber->setVisible(0);
285   _Type = 2 ;
286   adjustSize();
287 }
288 // ------------------------------------------------------------------------
289 void MonPursueIteration::CaseNIteration()
290 // ------------------------------------------------------------------------
291 {
292   spinBoxNumber->setVisible(1);
293   _Type = 3 ;
294   adjustSize();
295 }