Salome HOME
Suivi de frontières 2D sur des cônes (suite)
[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->GetAllBoundarysName();
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->GetType() ;
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                                 QObject::tr(CORBA::string_dup(S_ex.details.text)) );
228       try
229       {
230           aCase = _myHomardGen->GetCase(_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
292   return true;
293 }
294 // ---------------------------
295 void MonCreateCase::PushOnOK()
296 // ---------------------------
297 {
298      bool bOK = PushOnApply();
299      if ( bOK )  this->close();
300 }
301 //------------------------------
302 void MonCreateCase::PushOnHelp()
303 //-------------------------------
304 {
305   HOMARD_UTILS::PushOnHelp(QString("gui_create_case.html"));
306 }
307 // ---------------------------------
308 void MonCreateCase::SetNewCaseName()
309 // ------------------------------
310 {
311   HOMARD::listeCases_var  MyCases = _myHomardGen->GetAllCasesName();
312   int num = 0; QString aCaseName="";
313   while (aCaseName=="" )
314   {
315     aCaseName.setNum(num+1) ;
316     aCaseName.insert(0, QString("Case_")) ;
317     for ( int i=0; i<MyCases->length(); i++)
318     {
319       if ( aCaseName ==  QString((MyCases)[i]))
320       {
321           num=num+1;
322           aCaseName="";
323           break;
324       }
325    }
326   }
327   LECaseName->clear() ;
328   LECaseName->insert(aCaseName);
329 }
330
331 // ------------------------------------------------------------------------
332 void MonCreateCase::SetDirName()
333 // ------------------------------------------------------------------------
334 {
335   QString aDirName=QFileDialog::getExistingDirectory ();
336   if (!(aDirName.isEmpty()))LEDirName->setText(aDirName);
337 }
338 // ------------------------------------------------------------------------
339 void MonCreateCase::SetFileName()
340 // ------------------------------------------------------------------------
341 {
342   QString fileName0 = LEFileName->text().trimmed();
343   QString fileName = HOMARD_QT_COMMUN::PushNomFichier();
344   if (fileName.isEmpty()) fileName = fileName0 ;
345   LEFileName->setText(fileName);
346 }
347 // ------------------------------------------------------------------------
348 void MonCreateCase::SetConforme()
349 // ------------------------------------------------------------------------
350 {
351   GBTypeNoConf->setVisible(0);
352   adjustSize();
353   _ConfType=1;
354 }
355 // ------------------------------------------------------------------------
356 void MonCreateCase::SetNonConforme()
357 // ------------------------------------------------------------------------
358 {
359   GBTypeNoConf->setVisible(1);
360   RB1NpM->setChecked(true);
361   adjustSize();
362   _ConfType=2;
363 }
364 // ------------------------------------------------------------------------
365 void MonCreateCase::Set1NpM()
366 // ------------------------------------------------------------------------
367 {
368   _ConfType=2;
369 }
370 // ------------------------------------------------------------------------
371 void MonCreateCase::Set1NpA()
372 // ------------------------------------------------------------------------
373 {
374   _ConfType=3;
375 }
376 // ------------------------------------------------------------------------
377 void MonCreateCase::SetQuelconque()
378 // ------------------------------------------------------------------------
379 {
380   _ConfType=4;
381 }
382 // ------------------------------------------------------------------------
383 void MonCreateCase::SetBoundaryD()
384 // ------------------------------------------------------------------------
385 {
386   MESSAGE("Debut de SetBoundaryD ");
387   if (CBBoundaryD->isChecked())
388   {
389     bool bOK = PushOnApply();
390     if (bOK) { GBBoundaryD->setVisible(1); }
391     else     { GBBoundaryD->setVisible(0);
392                CBBoundaryD->setChecked(0);
393                CBBoundaryD->setCheckState(Qt::Unchecked); }
394   }
395   else { GBBoundaryD->setVisible(0); }
396   adjustSize();
397 }
398 // ------------------------------------------------------------------------
399 void MonCreateCase::AddBoundaryDi(QString newBoundary)
400 // ------------------------------------------------------------------------
401 {
402   CBBoundaryDi->insertItem(0,newBoundary);
403   CBBoundaryDi->setCurrentIndex(0);
404 }
405 // ------------------------------------------------------------------------
406 void MonCreateCase::PushBoundaryDiNew()
407 // ------------------------------------------------------------------------
408 {
409    MonCreateBoundaryDi *BoundaryDlg = new MonCreateBoundaryDi(this, true,
410                 HOMARD::HOMARD_Gen::_duplicate(_myHomardGen), _aCaseName, "") ;
411    BoundaryDlg->show();
412 }
413 // ------------------------------------------------------------------------
414 void MonCreateCase::PushBoundaryDiEdit()
415 // ------------------------------------------------------------------------
416 {
417   if (CBBoundaryDi->currentText() == QString(""))  return;
418   MonEditBoundaryDi *BoundaryDlg = new MonEditBoundaryDi(this, true,
419        HOMARD::HOMARD_Gen::_duplicate(_myHomardGen), _aCaseName, CBBoundaryDi->currentText() ) ;
420   BoundaryDlg->show();
421 }
422 // ------------------------------------------------------------------------
423 void MonCreateCase::PushBoundaryDiHelp()
424 // ------------------------------------------------------------------------
425 {
426   HOMARD_UTILS::PushOnHelp(QString("gui_create_boundary.html#frontiere-discrete") ) ;
427 }
428 // ------------------------------------------------------------------------
429 void MonCreateCase::SetBoundaryA()
430 // ------------------------------------------------------------------------
431 {
432   MESSAGE("Debut de SetBoundaryA ");
433   if (CBBoundaryA->isChecked())
434   {
435     bool bOK = PushOnApply();
436     if (bOK) { GBBoundaryA->setVisible(1); }
437     else     { GBBoundaryA->setVisible(0);
438                CBBoundaryA->setChecked(0);
439                CBBoundaryA->setCheckState(Qt::Unchecked); }
440   }
441   else { GBBoundaryA->setVisible(0); }
442   adjustSize();
443 //
444 //   MESSAGE("Fin de SetBoundaryA ");
445 }
446 // ------------------------------------------------------------------------
447 void MonCreateCase::AddBoundaryAn(QString newBoundary)
448 // ------------------------------------------------------------------------
449 {
450   MESSAGE("Debut de AddBoundaryAn ");
451 // Ajout d'une nouvelle colonne
452   int nbcol = TWBoundary->columnCount();
453 //   MESSAGE("nbcol " <<  nbcol);
454   nbcol += 1 ;
455   TWBoundary->setColumnCount ( nbcol ) ;
456   QTableWidgetItem *__colItem = new QTableWidgetItem();
457   __colItem->setText(QApplication::translate("CreateCase", newBoundary.toStdString().c_str(), 0, QApplication::UnicodeUTF8));
458   TWBoundary->setHorizontalHeaderItem(nbcol-1, __colItem);
459 /*  TWBoundary->horizontalHeaderItem(nbcol-1)->setFlags( Qt::ItemIsSelectable|Qt::ItemIsEnabled );*/
460 // Chaque case est a cocher
461   int nbrow = TWBoundary->rowCount();
462 //   MESSAGE("nbrow " <<  nbrow);
463   for ( int i = 0; i < nbrow; i++ )
464   {
465     TWBoundary->setItem( i, nbcol-1, new QTableWidgetItem( QString ("") ) );
466     TWBoundary->item( i, nbcol-1 )->setFlags( 0 );
467     TWBoundary->item( i, nbcol-1 )->setFlags( Qt::ItemIsUserCheckable|Qt::ItemIsEnabled  );
468     TWBoundary->item( i, nbcol-1 )->setCheckState( Qt::Unchecked );
469   }
470   TWBoundary->resizeColumnToContents(nbcol-1);
471 //   TWBoundary->resizeRowsToContents();
472 //   MESSAGE("Fin de AddBoundaryAn ");
473 }
474 // ------------------------------------------------------------------------
475 void MonCreateCase::PushBoundaryAnNew()
476 // ------------------------------------------------------------------------
477 {
478    MonCreateBoundaryAn *BoundaryDlg = new MonCreateBoundaryAn(this, true,
479                 HOMARD::HOMARD_Gen::_duplicate(_myHomardGen), _aCaseName) ;
480    BoundaryDlg->show();
481 }
482 // ------------------------------------------------------------------------
483 void MonCreateCase::PushBoundaryAnEdit()
484 // ------------------------------------------------------------------------
485 {
486   QString nom="";
487   int nbcol = TWBoundary->columnCount();
488   for ( int i = 1; i < nbcol; i++ )
489   {
490     QTableWidgetItem *__colItem = new QTableWidgetItem();
491     __colItem = TWBoundary->horizontalHeaderItem(i);
492     nom = QString(__colItem->text()) ;
493     MESSAGE("nom "<<nom.toStdString().c_str());
494     if (nom != QString(""))
495     { MonEditBoundaryAn *BoundaryDlg = new MonEditBoundaryAn(this, true,
496         HOMARD::HOMARD_Gen::_duplicate(_myHomardGen), _aCaseName, nom ) ;
497       BoundaryDlg->show(); }
498   }
499 }
500 // ------------------------------------------------------------------------
501 void MonCreateCase::PushBoundaryAnHelp()
502 // ------------------------------------------------------------------------
503 {
504   HOMARD_UTILS::PushOnHelp(QString("gui_create_boundary.html#frontiere-analytique") ) ;
505 }
506 // ------------------------------------------------------------------------
507 void MonCreateCase::CaseNameChanged()
508 // ------------------------------------------------------------------------
509 {
510     if (_aCaseName != LECaseName->text().trimmed())
511     {
512        LEFileName->setReadOnly(false);
513        PushFichier->show();
514     }
515 }
516 // ------------------------------------------------------------------------
517 void MonCreateCase::SetAdvanced()
518 // ------------------------------------------------------------------------
519 {
520   MESSAGE("Debut de SetAdvanced ");
521   if (CBAdvanced->isChecked()) { GBAdvancedOptions->setVisible(1); }
522   else
523   { GBAdvancedOptions->setVisible(0);
524     CBPyramid->setChecked(false);
525     _Pyram = 0 ;
526  }
527   adjustSize();
528 }