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