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