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