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