Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/homard.git] / src / HOMARDGUI / MonCreateCase.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 "MonCreateCase.h"
23 #include "MonCreateBoundaryAn.h"
24 #include "MonEditBoundaryAn.h"
25 #include "MonCreateBoundaryDi.h"
26 #include "MonEditBoundaryDi.h"
27
28 #include <QFileDialog>
29 #include <QMessageBox>
30
31 #include "SalomeApp_Tools.h"
32 #include "HOMARDGUI_Utils.h"
33 #include "HomardQtCommun.h"
34 #include <utilities.h>
35
36
37 // -----------------------------------------------------------------------------------------
38 MonCreateCase::MonCreateCase(QWidget* parent, bool modal, HOMARD::HOMARD_Gen_var myHomardGen)
39 // -----------------------------------------------------------------------------------------
40 /* Constructs a MonCreateCase
41  * Inherits from CasHomard
42  * Sets attributes to default values
43  */
44     :
45     Ui_CreateCase(),
46     _aCaseName(""),_aDirName(""),
47     _ConfType(1),
48     _Pyram(0)
49 {
50       _myHomardGen=HOMARD::HOMARD_Gen::_duplicate(myHomardGen);
51       setupUi(this);
52       setModal(modal);
53       InitConnect();
54
55       SetNewCaseName() ;
56       GBBoundaryA->setVisible(0);
57       GBBoundaryD->setVisible(0);
58       GBTypeNoConf->setVisible(0);
59       adjustSize();
60       GBAdvancedOptions->setVisible(0);
61       CBPyramid->setChecked(false);
62 }
63
64 // ------------------------------------------------------------------------
65 MonCreateCase::~MonCreateCase()
66 // ------------------------------------------------------------------------
67 {
68     // no need to delete child widgets, Qt does it all for us
69 }
70 // ------------------------------------------------------------------------
71 void MonCreateCase::InitConnect()
72 // ------------------------------------------------------------------------
73 {
74     connect( PushDir,        SIGNAL(pressed()), this, SLOT(SetDirName()));
75     connect( PushFichier,    SIGNAL(pressed()), this, SLOT(SetFileName()));
76
77     connect( RBConforme,     SIGNAL(clicked()), this, SLOT(SetConforme()));
78     connect( RBNonConforme,  SIGNAL(clicked()), this, SLOT(SetNonConforme()));
79     connect( RB1NpM,         SIGNAL(clicked()), this, SLOT(Set1NpM()));
80     connect( RB1NpA,         SIGNAL(clicked()), this, SLOT(Set1NpA()));
81     connect( RBQuelconque,   SIGNAL(clicked()), this, SLOT(SetQuelconque()));
82
83     connect( CBBoundaryD,      SIGNAL(stateChanged(int)), this, SLOT(SetBoundaryD()));
84     connect( PBBoundaryDiNew,  SIGNAL(pressed()), this, SLOT(PushBoundaryDiNew()));
85     connect( PBBoundaryDiEdit, SIGNAL(pressed()), this, SLOT(PushBoundaryDiEdit()) );
86     connect( PBBoundaryDiHelp, SIGNAL(pressed()), this, SLOT(PushBoundaryDiHelp()) );
87     connect( CBBoundaryA,      SIGNAL(stateChanged(int)), this, SLOT(SetBoundaryA()));
88     connect( PBBoundaryAnNew,  SIGNAL(pressed()), this, SLOT(PushBoundaryAnNew()));
89     connect( PBBoundaryAnEdit, SIGNAL(pressed()), this, SLOT(PushBoundaryAnEdit()) );
90     connect( PBBoundaryAnHelp, SIGNAL(pressed()), this, SLOT(PushBoundaryAnHelp()) );
91
92     connect( CBAdvanced,     SIGNAL(stateChanged(int)), this, SLOT(SetAdvanced()));
93
94     connect( buttonOk,       SIGNAL(pressed()), this, SLOT(PushOnOK()));
95     connect( buttonApply,    SIGNAL(pressed()), this, SLOT(PushOnApply()));
96     connect( buttonCancel,   SIGNAL(pressed()), this, SLOT(close()));
97     connect( buttonHelp,     SIGNAL(pressed()), this, SLOT(PushOnHelp()));
98     connect( LECaseName,     SIGNAL(textChanged(QString)), this, SLOT(CaseNameChanged()));
99 }
100 // ------------------------------------------------------------------------
101 void MonCreateCase::InitBoundarys()
102 // ------------------------------------------------------------------------
103 // Initialisation des menus avec les frontieres deja enregistrees
104 {
105   MESSAGE("InitBoundarys");
106 // Pour les frontieres analytiques : la colonne des groupes
107   HOMARD::ListGroupType_var _listeGroupesCas = aCase->GetGroups();
108   QTableWidgetItem *__colItem = new QTableWidgetItem();
109   __colItem->setText(QApplication::translate("CreateCase", "", 0, QApplication::UnicodeUTF8));
110   TWBoundary->setHorizontalHeaderItem(0, __colItem);
111   for ( int i = 0; i < _listeGroupesCas->length(); i++ )
112   {
113     TWBoundary->insertRow(i);
114     TWBoundary->setItem( i, 0, new QTableWidgetItem(QString((_listeGroupesCas)[i]).trimmed()));
115     TWBoundary->item( i, 0 )->setFlags(Qt::ItemIsEnabled |Qt::ItemIsSelectable );
116   }
117 // Pour les frontieres discretes : la liste a saisir
118 // Pour les frontieres analytiques : les colonnes de chaque frontiere
119   HOMARD::HOMARD_Boundary_var myBoundary ;
120   HOMARD::listeBoundarys_var  mesBoundarys = _myHomardGen->GetAllBoundarys();
121 //   MESSAGE("Nombre de frontieres enregistrees : "<<mesBoundarys->length());
122   for (int i=0; i<mesBoundarys->length(); i++)
123   {
124     myBoundary = _myHomardGen->GetBoundary(mesBoundarys[i]);
125     int type_obj = myBoundary->GetBoundaryType() ;
126     if ( type_obj==0 ) { CBBoundaryDi->addItem(QString(mesBoundarys[i])); }
127     else               { addBoundaryAn(QString(mesBoundarys[i])); }
128   }
129 // Ajustement
130   TWBoundary->resizeColumnsToContents();
131   TWBoundary->resizeRowsToContents();
132   TWBoundary->clearSelection();
133 }
134 // -------------------------------
135 bool MonCreateCase::PushOnApply()
136 // --------------------------------
137 {
138   MESSAGE("PushOnApply");
139   QString aCaseName=LECaseName->text().trimmed();
140   if ( aCaseName == "" )
141   {
142     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
143                               QObject::tr("HOM_CASE_NAME") );
144     return false;
145   }
146
147   QString aDirName=LEDirName->text().trimmed();
148   if (aDirName == QString(""))
149   {
150     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
151                               QObject::tr("HOM_CASE_DIRECTORY_1") );
152     return false;
153   }
154   if ((aDirName != _aDirName) and (_myHomardGen->VerifieDir( aDirName.toStdString().c_str()) == false))
155   {
156     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
157                               QObject::tr("HOM_CASE_DIRECTORY_2") );
158     return false;
159   }
160   if (chdir(aDirName.toStdString().c_str()) != 0)
161   {
162     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
163                               QObject::tr("HOM_CASE_DIRECTORY_3") );
164     return false;
165   }
166
167   QString aFileName=LEFileName->text().trimmed();
168   if (aFileName ==QString(""))
169   {
170     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
171                               QObject::tr("HOM_CASE_MESH") );
172     return false;
173   }
174
175   QString aMeshName = HOMARD_QT_COMMUN::LireNomMaillage(aFileName);
176   if (aMeshName == "" )
177   {
178     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
179                               QObject::tr("HOM_MED_FILE_2") );
180     return false;
181   }
182
183   if (CBBoundaryA->isChecked())
184   {
185     QStringList ListeGroup ;
186     QString NomGroup ;
187     int nbcol = TWBoundary->columnCount();
188     int nbrow = TWBoundary->rowCount();
189     for ( int col=1; col< nbcol; col++)
190     {
191       for ( int row=0; row< nbrow; row++)
192       {
193         if ( TWBoundary->item( row, col )->checkState() ==  Qt::Checked )
194         {
195 //        Nom du groupe
196           NomGroup = QString(TWBoundary->item(row, 0)->text()) ;
197 //           MESSAGE("NomGroup "<<NomGroup.toStdString().c_str());
198           for ( int nugr = 0 ; nugr<ListeGroup.size(); nugr++)
199           {
200 //             MESSAGE("....... "<<ListeGroup[nugr].toStdString().c_str());
201             if ( NomGroup == ListeGroup[nugr] )
202             {
203               QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
204                                         QObject::tr("HOM_CASE_GROUP").arg(NomGroup) );
205               return false;
206             }
207           }
208           ListeGroup.insert(0, NomGroup );
209         }
210       }
211     }
212   }
213
214   if (aCaseName != _aCaseName )
215   {
216     _aCaseName = aCaseName;
217     try
218     {
219     aCase = _myHomardGen->CreateCase( \
220             CORBA::string_dup(_aCaseName.toStdString().c_str()),  \
221             CORBA::string_dup(aMeshName.toStdString().c_str()),  \
222             CORBA::string_dup(aFileName.toStdString().c_str()) );
223     }
224     catch( SALOME::SALOME_Exception& S_ex )
225     {
226       QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
227                                 QString(CORBA::string_dup(S_ex.details.text)) );
228       try
229       {
230           aCase = _myHomardGen->GetCas(_aCaseName.toStdString().c_str());
231           string iter0 = aCase->GetIter0Name();
232           HOMARD::HOMARD_Iteration_var aIter =  _myHomardGen->GetIteration(iter0.c_str());
233           QString aFileName = aIter->GetMeshFile();
234           LEFileName->setText(aFileName);
235           LEFileName->setReadOnly(true);
236           PushFichier->hide();
237       }
238       catch( SALOME::SALOME_Exception& S_ex )  {};
239       return false;
240     }
241     LEFileName->setReadOnly(true);
242     PushFichier->hide();
243     InitBoundarys();
244   }
245
246   aCase->SetDirName(aDirName.toStdString().c_str());
247   _aDirName=aDirName;
248   aCase->SetConfType(_ConfType);
249
250 // Enregistrement de la frontiere discrete
251   if (CBBoundaryD->isChecked())
252   {
253     QString monBoundaryDiName=CBBoundaryDi->currentText();
254     if (monBoundaryDiName != "" )
255     {
256       aCase->AddBoundaryGroup(monBoundaryDiName.toStdString().c_str(), "");
257     }
258   }
259
260 // Enregistrement des liens (groupe,frontiere analytique)
261   if (CBBoundaryA->isChecked())
262   {
263     QString NomGroup ;
264     int nbcol = TWBoundary->columnCount();
265     int nbrow = TWBoundary->rowCount();
266     for ( int col=1; col< nbcol; col++)
267     {
268       for ( int row=0; row< nbrow; row++)
269       {
270         if ( TWBoundary->item( row, col )->checkState() ==  Qt::Checked )
271         {
272 //        Nom du groupe
273           NomGroup = QString(TWBoundary->item(row, 0)->text()) ;
274 //        Nom de la frontiere
275           QTableWidgetItem *__colItem = new QTableWidgetItem();
276           __colItem = TWBoundary->horizontalHeaderItem(col);
277           aCase->AddBoundaryGroup(QString(__colItem->text()).toStdString().c_str(), NomGroup.toStdString().c_str());
278         }
279       }
280     }
281   }
282
283 // Options avancees
284   if (CBAdvanced->isChecked())
285   {
286 // Autorisation des pyramides
287     if (CBPyramid->isChecked()) { _Pyram = 1 ; }
288   }
289
290   HOMARD_UTILS::updateObjBrowser();
291   return true;
292 }
293 // ---------------------------
294 void MonCreateCase::PushOnOK()
295 // ---------------------------
296 {
297      bool bOK = PushOnApply();
298      if ( bOK )  this->close();
299 }
300 //------------------------------
301 void MonCreateCase::PushOnHelp()
302 //-------------------------------
303 {
304   HOMARD_UTILS::PushOnHelp(QString("gui_create_case.html"));
305 }
306 // ---------------------------------
307 void MonCreateCase::SetNewCaseName()
308 // ------------------------------
309 {
310   HOMARD::listeCases_var  MyCases = _myHomardGen->GetAllCases();
311   int num = 0; QString aCaseName="";
312   while (aCaseName=="" )
313   {
314     aCaseName.setNum(num+1) ;
315     aCaseName.insert(0, QString("Case_")) ;
316     for ( int i=0; i<MyCases->length(); i++)
317     {
318       if ( aCaseName ==  QString((MyCases)[i]))
319       {
320           num=num+1;
321           aCaseName="";
322           break;
323       }
324    }
325   }
326   LECaseName->clear() ;
327   LECaseName->insert(aCaseName);
328 }
329
330 // ------------------------------------------------------------------------
331 void MonCreateCase::SetDirName()
332 // ------------------------------------------------------------------------
333 {
334   QString aDirName=QFileDialog::getExistingDirectory ();
335   if (!(aDirName.isEmpty()))LEDirName->setText(aDirName);
336 }
337 // ------------------------------------------------------------------------
338 void MonCreateCase::SetFileName()
339 // ------------------------------------------------------------------------
340 {
341   QString fileName0 = LEFileName->text().trimmed();
342   QString fileName = HOMARD_QT_COMMUN::PushNomFichier();
343   if (fileName.isEmpty()) fileName = fileName0 ;
344   LEFileName->setText(fileName);
345 }
346 // ------------------------------------------------------------------------
347 void MonCreateCase::SetConforme()
348 // ------------------------------------------------------------------------
349 {
350   GBTypeNoConf->setVisible(0);
351   adjustSize();
352   _ConfType=1;
353 }
354 // ------------------------------------------------------------------------
355 void MonCreateCase::SetNonConforme()
356 // ------------------------------------------------------------------------
357 {
358   GBTypeNoConf->setVisible(1);
359   RB1NpM->setChecked(true);
360   adjustSize();
361   _ConfType=2;
362 }
363 // ------------------------------------------------------------------------
364 void MonCreateCase::Set1NpM()
365 // ------------------------------------------------------------------------
366 {
367   _ConfType=2;
368 }
369 // ------------------------------------------------------------------------
370 void MonCreateCase::Set1NpA()
371 // ------------------------------------------------------------------------
372 {
373   _ConfType=3;
374 }
375 // ------------------------------------------------------------------------
376 void MonCreateCase::SetQuelconque()
377 // ------------------------------------------------------------------------
378 {
379   _ConfType=4;
380 }
381 // ------------------------------------------------------------------------
382 void MonCreateCase::SetBoundaryD()
383 // ------------------------------------------------------------------------
384 {
385   MESSAGE("Debut de SetBoundaryD ");
386   if (CBBoundaryD->isChecked())
387   {
388     bool bOK = PushOnApply();
389     if (bOK) { GBBoundaryD->setVisible(1); }
390     else     { GBBoundaryD->setVisible(0);
391                CBBoundaryD->setChecked(0);
392                CBBoundaryD->setCheckState(Qt::Unchecked); }
393   }
394   else { GBBoundaryD->setVisible(0); }
395   adjustSize();
396 }
397 // ------------------------------------------------------------------------
398 void MonCreateCase::addBoundaryDi(QString newBoundary)
399 // ------------------------------------------------------------------------
400 {
401   CBBoundaryDi->insertItem(0,newBoundary);
402   CBBoundaryDi->setCurrentIndex(0);
403 }
404 // ------------------------------------------------------------------------
405 void MonCreateCase::PushBoundaryDiNew()
406 // ------------------------------------------------------------------------
407 {
408    MonCreateBoundaryDi *BoundaryDlg = new MonCreateBoundaryDi(this, true,
409                 HOMARD::HOMARD_Gen::_duplicate(_myHomardGen), _aCaseName, "") ;
410    BoundaryDlg->show();
411 }
412 // ------------------------------------------------------------------------
413 void MonCreateCase::PushBoundaryDiEdit()
414 // ------------------------------------------------------------------------
415 {
416   if (CBBoundaryDi->currentText() == QString(""))  return;
417   MonEditBoundaryDi *BoundaryDlg = new MonEditBoundaryDi(this, true,
418        HOMARD::HOMARD_Gen::_duplicate(_myHomardGen), _aCaseName, CBBoundaryDi->currentText() ) ;
419   BoundaryDlg->show();
420 }
421 // ------------------------------------------------------------------------
422 void MonCreateCase::PushBoundaryDiHelp()
423 // ------------------------------------------------------------------------
424 {
425   HOMARD_UTILS::PushOnHelp(QString("gui_create_boundary.html#frontiere-discrete") ) ;
426 }
427 // ------------------------------------------------------------------------
428 void MonCreateCase::SetBoundaryA()
429 // ------------------------------------------------------------------------
430 {
431   MESSAGE("Debut de SetBoundaryA ");
432   if (CBBoundaryA->isChecked())
433   {
434     bool bOK = PushOnApply();
435     if (bOK) { GBBoundaryA->setVisible(1); }
436     else     { GBBoundaryA->setVisible(0);
437                CBBoundaryA->setChecked(0);
438                CBBoundaryA->setCheckState(Qt::Unchecked); }
439   }
440   else { GBBoundaryA->setVisible(0); }
441   adjustSize();
442 //
443 //   MESSAGE("Fin de SetBoundaryA ");
444 }
445 // ------------------------------------------------------------------------
446 void MonCreateCase::addBoundaryAn(QString newBoundary)
447 // ------------------------------------------------------------------------
448 {
449   MESSAGE("Debut de addBoundaryAn ");
450 // Ajout d'une nouvelle colonne
451   int nbcol = TWBoundary->columnCount();
452 //   MESSAGE("nbcol " <<  nbcol);
453   nbcol += 1 ;
454   TWBoundary->setColumnCount ( nbcol ) ;
455   QTableWidgetItem *__colItem = new QTableWidgetItem();
456   __colItem->setText(QApplication::translate("CreateCase", newBoundary.toStdString().c_str(), 0, QApplication::UnicodeUTF8));
457   TWBoundary->setHorizontalHeaderItem(nbcol-1, __colItem);
458 /*  TWBoundary->horizontalHeaderItem(nbcol-1)->setFlags( Qt::ItemIsSelectable|Qt::ItemIsEnabled );*/
459 // Chaque case est a cocher
460   int nbrow = TWBoundary->rowCount();
461 //   MESSAGE("nbrow " <<  nbrow);
462   for ( int i = 0; i < nbrow; i++ )
463   {
464     TWBoundary->setItem( i, nbcol-1, new QTableWidgetItem( QString ("") ) );
465     TWBoundary->item( i, nbcol-1 )->setFlags( 0 );
466     TWBoundary->item( i, nbcol-1 )->setFlags( Qt::ItemIsUserCheckable|Qt::ItemIsEnabled  );
467     TWBoundary->item( i, nbcol-1 )->setCheckState( Qt::Unchecked );
468   }
469   TWBoundary->resizeColumnToContents(nbcol-1);
470 //   TWBoundary->resizeRowsToContents();
471 //   MESSAGE("Fin de addBoundaryAn ");
472 }
473 // ------------------------------------------------------------------------
474 void MonCreateCase::PushBoundaryAnNew()
475 // ------------------------------------------------------------------------
476 {
477    MonCreateBoundaryAn *BoundaryDlg = new MonCreateBoundaryAn(this, true,
478                 HOMARD::HOMARD_Gen::_duplicate(_myHomardGen), _aCaseName) ;
479    BoundaryDlg->show();
480 }
481 // ------------------------------------------------------------------------
482 void MonCreateCase::PushBoundaryAnEdit()
483 // ------------------------------------------------------------------------
484 {
485   QString nom="";
486   int nbcol = TWBoundary->columnCount();
487   for ( int i = 1; i < nbcol; i++ )
488   {
489     QTableWidgetItem *__colItem = new QTableWidgetItem();
490     __colItem = TWBoundary->horizontalHeaderItem(i);
491     nom = QString(__colItem->text()) ;
492     MESSAGE("nom "<<nom.toStdString().c_str());
493     if (nom != QString(""))
494     { MonEditBoundaryAn *BoundaryDlg = new MonEditBoundaryAn(this, true,
495         HOMARD::HOMARD_Gen::_duplicate(_myHomardGen), _aCaseName, nom ) ;
496       BoundaryDlg->show(); }
497   }
498 }
499 // ------------------------------------------------------------------------
500 void MonCreateCase::PushBoundaryAnHelp()
501 // ------------------------------------------------------------------------
502 {
503   HOMARD_UTILS::PushOnHelp(QString("gui_create_boundary.html#frontiere-analytique") ) ;
504 }
505 // ------------------------------------------------------------------------
506 void MonCreateCase::CaseNameChanged()
507 // ------------------------------------------------------------------------
508 {
509     if (_aCaseName != LECaseName->text().trimmed())
510     {
511        LEFileName->setReadOnly(false);
512        PushFichier->show();
513     }
514 }
515 // ------------------------------------------------------------------------
516 void MonCreateCase::SetAdvanced()
517 // ------------------------------------------------------------------------
518 {
519   MESSAGE("Debut de SetAdvanced ");
520   if (CBAdvanced->isChecked()) { GBAdvancedOptions->setVisible(1); }
521   else
522   { GBAdvancedOptions->setVisible(0);
523     CBPyramid->setChecked(false);
524     _Pyram = 0 ;
525  }
526   adjustSize();
527 }