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