Salome HOME
version 5_1_4 HOMARD_SRC
[modules/homard.git] / src / HOMARDGUI / MonCreateCase.cxx
1 using namespace std;
2
3 #include "MonCreateCase.h"
4 #include "MonCreateBoundaryDi.h"
5 #include "MonEditBoundaryDi.h"
6
7 #include <QFileDialog>
8 #include <QMessageBox>
9
10 #include "SalomeApp_Tools.h"
11 #include "HOMARDGUI_Utils.h"
12 #include "HomardQtCommun.h"
13 #include <utilities.h>
14
15
16 // -----------------------------------------------------------------------------------------
17 MonCreateCase::MonCreateCase(QWidget* parent, bool modal, HOMARD::HOMARD_Gen_var myHomardGen)
18 // -----------------------------------------------------------------------------------------
19 /* Constructs a MonCreateCase
20  * Inherits from CasHomard
21  * Sets attributes to default values
22  */
23     :
24     Ui_CreateCase(), 
25     _aCaseName(""),_aDirName(""), _ConfType(1)
26     {
27       _myHomardGen=HOMARD::HOMARD_Gen::_duplicate(myHomardGen);
28       setupUi(this);
29       setModal(modal);
30       InitConnect();
31
32       SetNewCaseName() ;
33       GBBoundaryA->setVisible(0);
34       GBBoundaryD->setVisible(0);
35       GBTypeNoConf->setVisible(0);
36       adjustSize();
37       GetBoundarys();
38
39     }
40
41 // ------------------------------------------------------------------------
42 MonCreateCase::~MonCreateCase()
43 // ------------------------------------------------------------------------
44 {
45     // no need to delete child widgets, Qt does it all for us
46 }
47 // ------------------------------------------------------------------------
48 void MonCreateCase::InitConnect()
49 // ------------------------------------------------------------------------
50 {
51     connect( PushDir,        SIGNAL(pressed()), this, SLOT(SetDirName()));
52     connect( PushFichier,    SIGNAL(pressed()), this, SLOT(SetFileName()));
53
54     connect( RBConforme,     SIGNAL(clicked()), this, SLOT(SetConforme()));
55     connect( RBNonConforme,  SIGNAL(clicked()), this, SLOT(SetNonConforme()));
56     connect( RB1NpM,         SIGNAL(clicked()), this, SLOT(Set1NpM()));
57     connect( RB1NpA,         SIGNAL(clicked()), this, SLOT(Set1NpA()));
58     connect( RBQuelconque,   SIGNAL(clicked()), this, SLOT(SetQuelconque()));
59
60     connect( CBBoundaryD,      SIGNAL(stateChanged(int)), this, SLOT(SetBoundaryD()));
61     connect( PBBoundaryDiNew,  SIGNAL(pressed()), this, SLOT(PushBoundaryDiNew()));
62     connect( PBBoundaryDiEdit, SIGNAL(pressed()), this, SLOT(PushBoundaryDiEdit()) );
63     connect( CBBoundaryA,      SIGNAL(stateChanged(int)), this, SLOT(SetBoundaryA()));
64
65     connect( buttonOk,       SIGNAL(pressed()), this, SLOT(PushOnOK()));
66     connect( buttonApply,    SIGNAL(pressed()), this, SLOT(PushOnApply()));
67     connect( buttonCancel,   SIGNAL(pressed()), this, SLOT(close()));
68     connect( buttonHelp,     SIGNAL(pressed()), this, SLOT(PushOnHelp()));
69     connect( LECaseName,     SIGNAL(textChanged(QString)), this, SLOT(CaseNameChanged()));
70 }
71 // ------------------------------------------------------------------------
72 void MonCreateCase::GetBoundarys()
73 // ------------------------------------------------------------------------
74 {
75      HOMARD::HOMARD_Boundary_var myBoundary ;
76      HOMARD::listeBoundarys_var  mesBoundarys = _myHomardGen->GetAllBoundarys();
77      for (int i=0; i<mesBoundarys->length(); i++)
78      {
79          myBoundary = _myHomardGen->GetBoundary(mesBoundarys[i]);
80          int type_obj = myBoundary->GetBoundaryType() ;
81          if ( type_obj==0 ) { CBBoundaryDi->addItem(QString(mesBoundarys[i])); }
82      }
83 }
84
85 // -------------------------------
86 bool MonCreateCase::PushOnApply()
87 // --------------------------------
88 {
89   MESSAGE("PushOnApply");
90   QString aCaseName=LECaseName->text().trimmed();
91   if ( aCaseName == "" )
92   {
93     QMessageBox::information( 0, "Error",
94             QString("The case must be named"),
95             QMessageBox::Ok + QMessageBox::Default );
96     return false;
97   }
98
99   QString aDirName=LEDirName->text().trimmed();
100   if (aDirName == QString(""))
101   {
102     QMessageBox::information( 0, "Error",
103               QString("A directory for the case must be selected."),
104               QMessageBox::Ok + QMessageBox::Default );
105     return false;
106   }
107   if ((aDirName != _aDirName) and (_myHomardGen->VerifieDir( aDirName.toStdString().c_str()) == false))
108   {
109     QMessageBox::information( 0, "Error",
110               QString("This directory is already used"),
111               QMessageBox::Ok + QMessageBox::Default );
112     return false;
113   }
114   if (chdir(aDirName.toStdString().c_str()) != 0)
115   {
116     QMessageBox::information( 0, "Error",
117               QString("A valid directory for the case must be selected."),
118               QMessageBox::Ok + QMessageBox::Default );
119     return false;
120   }
121
122   QString aFileName=LEFileName->text().trimmed();
123   if (aFileName ==QString(""))
124   {
125     QMessageBox::information( 0, "Error",
126               QString("The initial mesh must be selected."),
127               QMessageBox::Ok + QMessageBox::Default );
128     return false;
129   }
130
131   QString aMeshName = HOMARD_QT_COMMUN::LireNomMaillage(aFileName);
132   if (aMeshName == "" )
133   {
134     QMessageBox::information( 0, "Error",
135               QString("no mesh in mesh file"),
136               QMessageBox::Ok + QMessageBox::Default );
137     return false;
138   }
139
140   if (aCaseName != _aCaseName )
141   {
142     _aCaseName = aCaseName;
143     try
144     {
145     aCase = _myHomardGen->CreateCase( \
146             CORBA::string_dup(_aCaseName.toStdString().c_str()),  \
147             CORBA::string_dup(aMeshName.toStdString().c_str()),  \
148             CORBA::string_dup(aFileName.toStdString().c_str()) );
149     }
150     catch( SALOME::SALOME_Exception& S_ex ) 
151     {
152      QMessageBox::information( 0, "Error",
153             QString(CORBA::string_dup(S_ex.details.text)),
154             QMessageBox::Ok + QMessageBox::Default );
155       try
156       {
157           aCase = _myHomardGen->GetCas(_aCaseName.toStdString().c_str());
158           string iter0 = aCase->GetIter0Name();
159           HOMARD::HOMARD_Iteration_var aIter =  _myHomardGen->GetIteration(iter0.c_str());
160           QString aFileName = aIter->GetMeshFile();
161           LEFileName->setText(aFileName);
162           LEFileName->setReadOnly(true);
163           PushFichier->hide();
164       }
165       catch( SALOME::SALOME_Exception& S_ex )  {};
166       return false;
167     }
168     LEFileName->setReadOnly(true);
169     PushFichier->hide();
170   }
171
172   aCase->SetDirName(aDirName.toStdString().c_str());
173   _aDirName=aDirName;
174   aCase->SetConfType(_ConfType);
175
176   if (CBBoundaryD->isChecked())
177   {
178     QString monBoundaryDiName=CBBoundaryDi->currentText();
179     if (monBoundaryDiName != "" )
180     {
181       aCase->AddBoundaryGroup(monBoundaryDiName.toStdString().c_str(), " ");
182     }
183   }
184
185   HOMARD_UTILS::updateObjBrowser();
186   return true;
187 }
188 // ---------------------------
189 void MonCreateCase::PushOnOK()
190 // ---------------------------
191 {
192      bool bOK = PushOnApply();
193      if ( bOK )  this->close();
194 }
195 //------------------------------
196 void MonCreateCase::PushOnHelp()
197 //-------------------------------
198 {
199   HOMARD_UTILS::PushOnHelp(QString("gui_create_case.html"));
200 }
201 // ---------------------------------
202 void MonCreateCase::SetNewCaseName()
203 // ------------------------------
204 {
205   HOMARD::listeCases_var  MyCases = _myHomardGen->GetAllCases();
206   int num = 0; QString aCaseName="";
207   while (aCaseName=="" )
208   {
209     aCaseName.setNum(num+1) ;
210     aCaseName.insert(0, QString("Case_")) ;
211     for ( int i=0; i<MyCases->length(); i++)
212     {
213       if ( aCaseName ==  QString((MyCases)[i]))
214       {
215           num=num+1;
216           aCaseName="";
217           break;
218       }
219    }
220   }
221   LECaseName->clear() ;
222   LECaseName->insert(aCaseName);
223 }
224
225 // ------------------------------------------------------------------------
226 void MonCreateCase::SetDirName()
227 // ------------------------------------------------------------------------
228 {
229   QString aDirName=QFileDialog::getExistingDirectory ();
230   if (!(aDirName.isEmpty()))LEDirName->setText(aDirName);
231 }
232 // ------------------------------------------------------------------------
233 void MonCreateCase::SetFileName()
234 // ------------------------------------------------------------------------
235 {
236   QString fileName = HOMARD_QT_COMMUN::PushNomFichier();
237   if (!(fileName.isEmpty())) LEFileName->setText(fileName);
238 }
239 // ------------------------------------------------------------------------
240 void MonCreateCase::SetConforme()
241 // ------------------------------------------------------------------------
242 {
243   GBTypeNoConf->setVisible(0);
244   adjustSize();
245   _ConfType=1;
246 }
247 // ------------------------------------------------------------------------
248 void MonCreateCase::SetNonConforme()
249 // ------------------------------------------------------------------------
250 {
251   GBTypeNoConf->setVisible(1);
252   RB1NpM->setChecked(true);
253   adjustSize();
254   _ConfType=2;
255 }
256 // ------------------------------------------------------------------------
257 void MonCreateCase::Set1NpM()
258 // ------------------------------------------------------------------------
259 {
260   _ConfType=2;
261 }
262 // ------------------------------------------------------------------------
263 void MonCreateCase::Set1NpA()
264 // ------------------------------------------------------------------------
265 {
266   _ConfType=3;
267 }
268 // ------------------------------------------------------------------------
269 void MonCreateCase::SetQuelconque()
270 // ------------------------------------------------------------------------
271 {
272   _ConfType=4;
273 }
274 // ------------------------------------------------------------------------
275 void MonCreateCase::SetBoundaryD()
276 // ------------------------------------------------------------------------
277 {
278   if (CBBoundaryD->isChecked())
279   { GBBoundaryD->setVisible(1); }
280   else
281   { GBBoundaryD->setVisible(0); }
282   adjustSize();
283 }
284 // ------------------------------------------------------------------------
285 void MonCreateCase::addBoundaryDi(QString newBoundary)
286 // ------------------------------------------------------------------------
287 {
288   CBBoundaryDi->insertItem(0,newBoundary);
289   CBBoundaryDi->setCurrentIndex(0);
290 }
291 // ------------------------------------------------------------------------
292 void MonCreateCase::PushBoundaryDiNew()
293 // ------------------------------------------------------------------------
294 {
295    MonCreateBoundaryDi *BoundaryDlg = new MonCreateBoundaryDi(this, true, 
296                 HOMARD::HOMARD_Gen::_duplicate(_myHomardGen), _aCaseName, "") ;
297    BoundaryDlg->show();
298 }
299 // ------------------------------------------------------------------------
300 void MonCreateCase::PushBoundaryDiEdit()
301 // ------------------------------------------------------------------------
302 {
303   if (CBBoundaryDi->currentText() == QString(""))  return;
304   MonEditBoundaryDi *BoundaryDlg = new MonEditBoundaryDi(this, true, 
305        HOMARD::HOMARD_Gen::_duplicate(_myHomardGen), _aCaseName, CBBoundaryDi->currentText() ) ;
306   BoundaryDlg->show();
307 }
308 // ------------------------------------------------------------------------
309 void MonCreateCase::SetBoundaryA()
310 // ------------------------------------------------------------------------
311 {
312   if    (CBBoundaryA->isChecked()) { GBBoundaryA->setVisible(1); }
313   else { GBBoundaryA->setVisible(0); }
314   adjustSize();
315 }
316 // ------------------------------------------------------------------------
317 void MonCreateCase::CaseNameChanged()
318 // ------------------------------------------------------------------------
319 {
320     if (_aCaseName != LECaseName->text().trimmed())
321     {
322        LEFileName->setReadOnly(false);
323        PushFichier->show();
324     }
325 }