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