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