Salome HOME
09df846a9a08f6b3fc81ba5851e2c509849c051e
[modules/homard.git] / src / HOMARDGUI / MonCreateIteration.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 using namespace std;
21
22 #include "MonCreateIteration.h"
23 #include "MonCreateHypothesis.h"
24 #include "MonEditHypothesis.h"
25
26 #include <QFileDialog>
27 #include <QMessageBox>
28
29 #include "SalomeApp_Tools.h"
30 #include "HOMARDGUI_Utils.h"
31 #include "HomardQtCommun.h"
32 #include <utilities.h>
33
34
35 // -----------------------------------------------------------------------------------------------------
36 MonCreateIteration::MonCreateIteration(QWidget* parent, bool modal,
37                                        HOMARD::HOMARD_Gen_var myHomardGen, QString IterParentName ):
38 // -----------------------------------------------------------------------------------------------------
39 /* Constructs a MonCreateIteration
40  * Inherits from CasHomard
41  * Sets attributes to default values
42  */
43     QDialog(0),
44     Ui_CreateIteration(),
45     _IterationName(""),
46     _IterParentName(IterParentName),
47     _CaseName("")
48     {
49       MESSAGE("Constructeur");
50       _myHomardGen=HOMARD::HOMARD_Gen::_duplicate(myHomardGen);
51       setupUi(this);
52       setModal(modal);
53       InitConnect();
54
55       SetNewIterationName();
56       GetHypotheses();
57       if (_IterParentName != QString(""))
58          { SetIterParentName(); }
59       else
60          {setModal(false); /* permet selection de l iteration dans l arbre d etude */}
61       SetTSNo();
62     }
63
64 // ------------------------------------------------------------------------
65 MonCreateIteration::~MonCreateIteration()
66 // ------------------------------------------------------------------------
67 {
68     // no need to delete child widgets, Qt does it all for us
69 }
70 // ------------------------------------------------------------------------
71 void MonCreateIteration::InitConnect()
72 // ------------------------------------------------------------------------
73 {
74     connect( PBIterParent,  SIGNAL(pressed()), this, SLOT( SetIterParentName()));
75     connect( PBHypoEdit,    SIGNAL(pressed()), this, SLOT( PushHypoEdit() ) );
76     connect( PBHypoNew,     SIGNAL(pressed()), this, SLOT( PushHypoNew() ) );
77     connect( PushFieldFile, SIGNAL(pressed()), this, SLOT( SetFieldFile() ) );
78     connect( RBNo,          SIGNAL(clicked()), this, SLOT( SetTSNo()));
79     connect( RBLast,        SIGNAL(clicked()), this, SLOT( SetTSLast()));
80     connect( RBChosen,      SIGNAL(clicked()), this, SLOT( SetTSChosen()));
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 void MonCreateIteration::GetHypotheses()
89 // ------------------------------------------------------------------------
90 {
91      HOMARD::listeHypotheses_var  mesHypotheses = _myHomardGen->GetAllHypothesesName();
92      for (int i=0; i<mesHypotheses->length(); i++)
93      {
94          CBHypothese->addItem(QString(mesHypotheses[i]));
95      }
96 }
97
98 // ------------------------------------------------------------------------
99 bool MonCreateIteration::PushOnApply()
100 // ------------------------------------------------------------------------
101 // Appele lorsque l'un des boutons Ok ou Apply est presse
102 {
103   MESSAGE("MonCreateIteration::PushOnApply");
104 //
105   QString aIterationName = LEIterationName->text().trimmed();
106   if ( aIterationName == QString (""))
107   {
108     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
109                               QObject::tr("HOM_ITER_NAME") );
110     return false;
111   }
112
113   if ( _IterParentName == QString (""))
114   {
115     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
116                               QObject::tr("HOM_ITER_STARTING_POINT") );
117     return false;
118   }
119   QString aMeshName_np1=LEMeshName_np1->text().trimmed();
120   if (aMeshName_np1 == "" )
121   {
122     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
123                               QObject::tr("HOM_ITER_MESH") );
124     return false;
125   }
126   QString monHypoName=CBHypothese->currentText();
127   if (monHypoName == "" )
128   {
129     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
130                               QObject::tr("HOM_ITER_HYPO") );
131     return false;
132   }
133   HOMARD::HOMARD_Hypothesis_var _myHypothesis = _myHomardGen->GetHypothesis(monHypoName.toStdString().c_str());
134   HOMARD::listeTypes_var ListTypes (_myHypothesis->GetAdapRefinUnRef());
135   int TypeAdap = ListTypes[0];
136   if ( TypeAdap == 1 and LEFieldFile->text().trimmed() == QString("") )
137   {
138     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
139                               QObject::tr("HOM_ITER_FIELD_FILE") );
140     return false;
141   }
142
143   MESSAGE ("aMeshName_np1.toStdString " << aMeshName_np1.toStdString());
144   MESSAGE ("_CaseName.toStdString " << _CaseName.toStdString() );
145   MESSAGE ("_IterParentName.toStdString " << _IterParentName.toStdString() );
146
147 // Creation de l'objet CORBA si ce n'est pas deja fait sous le meme nom
148   if (_IterationName != aIterationName)
149   {
150     try
151     {
152       _IterationName = aIterationName;
153       std::cerr << _IterationName.toStdString() << std::endl;
154       aIter = _myHomardGen->CreateIteration( \
155                CORBA::string_dup(_IterationName.toStdString().c_str()),
156                CORBA::string_dup(_IterParentName.toStdString().c_str()));
157     }
158     catch( SALOME::SALOME_Exception& S_ex )
159     {
160       QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
161                                 QString(CORBA::string_dup(S_ex.details.text)) );
162        return false;
163     }
164   }
165
166 // Mise en place des attributs
167   const char* IterName = aIter->GetName() ;
168   std::cerr << IterName << std::endl;
169
170   if ( LEFieldFile->text().trimmed() != QString(""))
171   {
172     QString FieldFile=LEFieldFile->text().trimmed();
173     int rank = SpinBox_Rank->value();
174     int step = SpinBox_TimeStep->value();
175     aIter->SetFieldFile(CORBA::string_dup(FieldFile.toStdString().c_str()));
176     aIter->SetTimeStepRank(step,rank);
177   }
178   _myHomardGen->AssociateIterHypo (IterName, monHypoName.toStdString().c_str());
179   aIter->SetMeshName(CORBA::string_dup(aMeshName_np1.toStdString().c_str()));
180
181   HOMARD_UTILS::updateObjBrowser();
182   return true;
183 }
184
185 // ------------------------------------------------------------------------
186 void MonCreateIteration::PushOnOK()
187 // ------------------------------------------------------------------------
188 {
189      bool bOK = PushOnApply();
190      if ( bOK )  this->close();
191 }
192
193 // ------------------------------------------------------------------------
194 void MonCreateIteration::PushOnHelp()
195 // ------------------------------------------------------------------------
196 {
197   HOMARD_UTILS::PushOnHelp(QString("gui_create_iteration.html"));
198 }
199
200 // ------------------------------------------------------------------------
201 void MonCreateIteration::SetIterParentName()
202 // ------------------------------------------------------------------------
203 {
204   if (_IterParentName == QString("")) {
205     _IterParentName=HOMARD_QT_COMMUN::SelectionArbreEtude(QString("IterationHomard"), 1);
206     if (_IterParentName == QString("")) { raise();return;};
207   }
208   _CaseName=HOMARD_QT_COMMUN::SelectionCasEtude();
209   HOMARD::HOMARD_Iteration_var aIterParent = _myHomardGen->GetIteration(_IterParentName.toStdString().c_str()) ;
210   QString MeshName = aIterParent->GetMeshName();
211
212   LEMeshName_n->setText(MeshName);
213   LEMeshName_n->setReadOnly(1);
214   LEMeshName_np1->setText(MeshName);
215
216   LEIterationParentName->setText(_IterParentName);
217 }
218
219 // -------------------------------------------------
220 void MonCreateIteration::SetNewIterationName()
221 // --------------------------------------------------
222 {
223 // Recherche d'un nom par defaut qui n'existe pas encore
224
225   HOMARD::listeIterations_var  myIters=_myHomardGen->GetAllIterationsName();
226   int num = 0;//
227   QString aIterationName="";
228   while (aIterationName=="" )
229   {
230     aIterationName.setNum(num+1) ;
231     aIterationName.insert(0, QString("Iter_"));
232     for ( int i=0; i< myIters->length(); i++)
233     {
234       if ( aIterationName ==  QString((myIters)[i]))
235       {
236           num=num+1;
237           aIterationName="";
238           break;
239       }
240    }
241   }
242   LEIterationName->setText(aIterationName);
243 }
244
245
246 // ------------------------------------------------------------------------
247 void MonCreateIteration::PushHypoEdit()
248 // ------------------------------------------------------------------------
249 {
250   if (CBHypothese->currentText() == QString(""))  return;
251   if (_IterParentName == QString(""))
252   {
253     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
254                               QObject::tr("HOM_ITER_STARTING_POINT") );
255     raise();
256     return;
257   }
258   QString aFieldFile=LEFieldFile->text().trimmed();
259   MonEditHypothesis *HypoDlg = new MonEditHypothesis(this,TRUE, HOMARD::HOMARD_Gen::_duplicate(_myHomardGen),CBHypothese->currentText(), _CaseName, aFieldFile) ;
260   HypoDlg->show();
261 }
262
263 // ------------------------------------------------------------------------
264 void MonCreateIteration::addHypothese(QString newHypothese)
265 // ------------------------------------------------------------------------
266 {
267   CBHypothese->insertItem(0,newHypothese);
268   CBHypothese->setCurrentIndex(0);
269 }
270 // ------------------------------------------------------------------------
271 void MonCreateIteration::PushHypoNew()
272 // ------------------------------------------------------------------------
273 {
274   if (_IterParentName == QString(""))
275   {
276     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
277                               QObject::tr("HOM_ITER_STARTING_POINT") );
278     raise();
279     return;
280   }
281   if ( _CaseName == QString(""))
282   {
283          HOMARD::HOMARD_Iteration_var aIterParent = _myHomardGen->GetIteration(_IterParentName.toStdString().c_str()) ;
284         _CaseName = aIterParent->GetCaseName();
285   }
286   QString aFieldFile=LEFieldFile->text().trimmed();
287   MonCreateHypothesis *HypoDlg = new MonCreateHypothesis(this,TRUE,HOMARD::HOMARD_Gen::_duplicate(_myHomardGen),QString(""),_CaseName, aFieldFile) ;
288   HypoDlg->show();
289 }
290
291 // ------------------------------------------------------------------------
292 void MonCreateIteration::SetFieldFile()
293 // ------------------------------------------------------------------------
294
295 {
296   QString fileName0 = LEFieldFile->text().trimmed();
297   QString fileName = HOMARD_QT_COMMUN::PushNomFichier(false);
298   if (fileName.isEmpty()) fileName = fileName0 ;
299   LEFieldFile->setText(fileName);
300   raise();
301 }
302 // ------------------------------------------------------------------------
303 void MonCreateIteration::SetTSNo()
304 // ------------------------------------------------------------------------
305 // Si on ne tient pas compte du pas de temps, on declare que le pas de temps
306 // vaut -1, valeur par defaut de med
307 {
308   Rank->setVisible(0);
309   SpinBox_Rank->setVisible(0);
310   SpinBox_Rank->setValue(-1);
311
312   TimeStep->setVisible(0);
313   SpinBox_TimeStep->setVisible(0);
314   SpinBox_TimeStep->setValue(-1);
315
316   adjustSize();
317 }
318 // ------------------------------------------------------------------------
319 void MonCreateIteration::SetTSLast()
320 // ------------------------------------------------------------------------
321 // Si on choisit le dernier instant, on declare que le pas de temps vaut -2
322 {
323   Rank->setVisible(0);
324   SpinBox_Rank->setVisible(0);
325   SpinBox_Rank->setValue(-2);
326
327   TimeStep->setVisible(0);
328   SpinBox_TimeStep->setVisible(0);
329   SpinBox_TimeStep->setValue(-2);
330
331   adjustSize();
332 }
333 // ------------------------------------------------------------------------
334 void MonCreateIteration::SetTSChosen()
335 // ------------------------------------------------------------------------
336 // Si choisit un instant, on prepositionne a 0
337 {
338   Rank->setVisible(1);
339   SpinBox_Rank->setVisible(1);
340   SpinBox_Rank->setValue(0);
341
342   TimeStep->setVisible(1);
343   SpinBox_TimeStep->setVisible(1);
344   SpinBox_TimeStep->setValue(0);
345
346   adjustSize();
347 }
348