Salome HOME
Catalogue pour YACS ; liberation de structures allouees ; documentation
[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   std::string IterName = aIter->GetName() ;
164
165   if ( LEFieldFile->text().trimmed() != QString(""))
166   {
167     QString FieldFile=LEFieldFile->text().trimmed();
168     aIter->SetFieldFile(CORBA::string_dup(FieldFile.toStdString().c_str()));
169     int rank = SpinBox_Rank->value();
170     int step = SpinBox_TimeStep->value();
171     if ( step == -2 ) { aIter->SetTimeStepRankLast(); }
172     else              { aIter->SetTimeStepRank(step,rank); }
173   }
174   _myHomardGen->AssociateIterHypo (IterName.c_str(), monHypoName.toStdString().c_str());
175   aIter->SetMeshName(CORBA::string_dup(aMeshName_np1.toStdString().c_str()));
176
177   HOMARD_UTILS::updateObjBrowser() ;
178
179   return true;
180 }
181 // ------------------------------------------------------------------------
182 void MonCreateIteration::PushOnOK()
183 // ------------------------------------------------------------------------
184 {
185      bool bOK = PushOnApply();
186      if ( bOK )  this->close();
187 }
188 // ------------------------------------------------------------------------
189 void MonCreateIteration::PushOnHelp()
190 // ------------------------------------------------------------------------
191 {
192   HOMARD_UTILS::PushOnHelp(QString("gui_create_iteration.html"));
193 }
194 // ------------------------------------------------------------------------
195 void MonCreateIteration::SetIterParentName()
196 // ------------------------------------------------------------------------
197 {
198   if (_IterParentName == QString("")) {
199     _IterParentName=HOMARD_QT_COMMUN::SelectionArbreEtude(QString("IterationHomard"), 1);
200     if (_IterParentName == QString("")) { raise();return;};
201   }
202   _CaseName=HOMARD_QT_COMMUN::SelectionCasEtude();
203   HOMARD::HOMARD_Iteration_var aIterParent = _myHomardGen->GetIteration(_IterParentName.toStdString().c_str()) ;
204   QString MeshName = aIterParent->GetMeshName();
205
206   LEMeshName_n->setText(MeshName);
207   LEMeshName_n->setReadOnly(1);
208   LEMeshName_np1->setText(MeshName);
209
210   LEIterationParentName->setText(_IterParentName);
211 }
212 // -------------------------------------------------
213 void MonCreateIteration::SetNewIterationName()
214 // --------------------------------------------------
215 {
216 // Recherche d'un nom par defaut qui n'existe pas encore
217
218   HOMARD::listeIterations_var  myIters=_myHomardGen->GetAllIterationsName();
219   int num = 0;//
220   QString aIterationName="";
221   while (aIterationName=="" )
222   {
223     aIterationName.setNum(num+1) ;
224     aIterationName.insert(0, QString("Iter_"));
225     for ( int i=0; i< myIters->length(); i++)
226     {
227       if ( aIterationName ==  QString((myIters)[i]))
228       {
229           num=num+1;
230           aIterationName="";
231           break;
232       }
233    }
234   }
235   LEIterationName->setText(aIterationName);
236 }
237 // ------------------------------------------------------------------------
238 void MonCreateIteration::PushHypoEdit()
239 // ------------------------------------------------------------------------
240 {
241   if (CBHypothese->currentText() == QString(""))  return;
242   if (_IterParentName == QString(""))
243   {
244     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
245                               QObject::tr("HOM_ITER_STARTING_POINT") );
246     raise();
247     return;
248   }
249   QString aFieldFile=LEFieldFile->text().trimmed();
250   MonEditHypothesis *HypoDlg = new MonEditHypothesis(this,TRUE, HOMARD::HOMARD_Gen::_duplicate(_myHomardGen),CBHypothese->currentText(), _CaseName, aFieldFile) ;
251   HypoDlg->show();
252 }
253
254 // ------------------------------------------------------------------------
255 void MonCreateIteration::addHypothese(QString newHypothese)
256 // ------------------------------------------------------------------------
257 {
258   CBHypothese->insertItem(0,newHypothese);
259   CBHypothese->setCurrentIndex(0);
260 }
261 // ------------------------------------------------------------------------
262 void MonCreateIteration::PushHypoNew()
263 // ------------------------------------------------------------------------
264 {
265   if (_IterParentName == QString(""))
266   {
267     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
268                               QObject::tr("HOM_ITER_STARTING_POINT") );
269     raise();
270     return;
271   }
272   if ( _CaseName == QString(""))
273   {
274     HOMARD::HOMARD_Iteration_var aIterParent = _myHomardGen->GetIteration(_IterParentName.toStdString().c_str()) ;
275     _CaseName = aIterParent->GetCaseName();
276   }
277   QString aFieldFile=LEFieldFile->text().trimmed();
278   MonCreateHypothesis *HypoDlg = new MonCreateHypothesis(this,TRUE,HOMARD::HOMARD_Gen::_duplicate(_myHomardGen),QString(""),_CaseName, aFieldFile) ;
279   HypoDlg->show();
280 }
281 // ------------------------------------------------------------------------
282 void MonCreateIteration::SetFieldFile()
283 // ------------------------------------------------------------------------
284 {
285   QString fileName0 = LEFieldFile->text().trimmed();
286   QString fileName = HOMARD_QT_COMMUN::PushNomFichier(false);
287   if (fileName.isEmpty()) fileName = fileName0 ;
288   LEFieldFile->setText(fileName);
289   raise();
290 }
291 // ------------------------------------------------------------------------
292 void MonCreateIteration::SetTSNo()
293 // ------------------------------------------------------------------------
294 // Si on ne tient pas compte du pas de temps, on declare que le pas de temps
295 // vaut -1, valeur par defaut de med
296 {
297   Rank->setVisible(0);
298   SpinBox_Rank->setVisible(0);
299   SpinBox_Rank->setValue(-1);
300
301   TimeStep->setVisible(0);
302   SpinBox_TimeStep->setVisible(0);
303   SpinBox_TimeStep->setValue(-1);
304
305   adjustSize();
306 }
307 // ------------------------------------------------------------------------
308 void MonCreateIteration::SetTSLast()
309 // ------------------------------------------------------------------------
310 // Si on choisit le dernier instant, on declare que le pas de temps vaut -2
311 {
312   Rank->setVisible(0);
313   SpinBox_Rank->setVisible(0);
314   SpinBox_Rank->setValue(-2);
315
316   TimeStep->setVisible(0);
317   SpinBox_TimeStep->setVisible(0);
318   SpinBox_TimeStep->setValue(-2);
319
320   adjustSize();
321 }
322 // ------------------------------------------------------------------------
323 void MonCreateIteration::SetTSChosen()
324 // ------------------------------------------------------------------------
325 // Si choisit un instant, on prepositionne a 0
326 {
327   Rank->setVisible(1);
328   SpinBox_Rank->setVisible(1);
329   SpinBox_Rank->setValue(0);
330
331   TimeStep->setVisible(1);
332   SpinBox_TimeStep->setVisible(1);
333   SpinBox_TimeStep->setValue(0);
334
335   adjustSize();
336 }
337