Salome HOME
Porting HOMARD SALOME module on WIN32 platform.
[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()));
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()
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 != "" )
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   if (CBBoundaryA->isChecked())
200   {
201     QStringList ListeGroup ;
202     QString NomGroup ;
203     int nbcol = TWBoundary->columnCount();
204     int nbrow = TWBoundary->rowCount();
205     for ( int col=1; col< nbcol; col++)
206     {
207       for ( int row=0; row< nbrow; row++)
208       {
209         if ( TWBoundary->item( row, col )->checkState() ==  Qt::Checked )
210         {
211 //        Nom du groupe
212           NomGroup = QString(TWBoundary->item(row, 0)->text()) ;
213 //           MESSAGE("NomGroup "<<NomGroup.toStdString().c_str());
214           for ( int nugr = 0 ; nugr<ListeGroup.size(); nugr++)
215           {
216 //             MESSAGE("....... "<<ListeGroup[nugr].toStdString().c_str());
217             if ( NomGroup == ListeGroup[nugr] )
218             {
219               QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
220                                         QObject::tr("HOM_CASE_GROUP").arg(NomGroup) );
221               return false;
222             }
223           }
224           ListeGroup.insert(0, NomGroup );
225         }
226       }
227     }
228   }
229
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       try
245       {
246           aCase = myHomardGen->GetCase(_aCaseName.toStdString().c_str());
247           string iter0 = aCase->GetIter0Name();
248           HOMARD::HOMARD_Iteration_var aIter =  myHomardGen->GetIteration(iter0.c_str());
249           QString aFileName = aIter->GetMeshFile();
250           LEFileName->setText(aFileName);
251           LEFileName->setReadOnly(true);
252           PushFichier->hide();
253       }
254       catch( SALOME::SALOME_Exception& S_ex )  {};
255       return false;
256     }
257     LEFileName->setReadOnly(true);
258     PushFichier->hide();
259     InitBoundarys();
260   }
261
262   aCase->SetDirName(aDirName.toStdString().c_str());
263   _aDirName=aDirName;
264   aCase->SetConfType(_ConfType);
265
266 // Enregistrement de la frontiere discrete
267   if (CBBoundaryD->isChecked())
268   {
269     QString monBoundaryDiName=CBBoundaryDi->currentText();
270     if (monBoundaryDiName != "" )
271     {
272       aCase->AddBoundaryGroup(monBoundaryDiName.toStdString().c_str(), "");
273     }
274   }
275
276 // Enregistrement des liens (groupe,frontiere analytique)
277   if (CBBoundaryA->isChecked())
278   {
279     QString NomGroup ;
280     int nbcol = TWBoundary->columnCount();
281     int nbrow = TWBoundary->rowCount();
282     for ( int col=1; col< nbcol; col++)
283     {
284       for ( int row=0; row< nbrow; row++)
285       {
286         if ( TWBoundary->item( row, col )->checkState() ==  Qt::Checked )
287         {
288 //        Nom du groupe
289           NomGroup = QString(TWBoundary->item(row, 0)->text()) ;
290 //        Nom de la frontiere
291           QTableWidgetItem *__colItem = new QTableWidgetItem();
292           __colItem = TWBoundary->horizontalHeaderItem(col);
293           aCase->AddBoundaryGroup(QString(__colItem->text()).toStdString().c_str(), NomGroup.toStdString().c_str());
294         }
295       }
296     }
297   }
298
299 // Options avancees
300   if (CBAdvanced->isChecked())
301   {
302 // Autorisation des pyramides
303     if (CBPyramid->isChecked()) { _Pyram = 1 ; }
304   }
305
306   HOMARD_UTILS::updateObjBrowser();
307
308   return true;
309 }
310 // ---------------------------
311 void MonCreateCase::PushOnOK()
312 // ---------------------------
313 {
314   bool bOK = PushOnApply();
315   if ( bOK ) this->close();
316 }
317 //------------------------------
318 void MonCreateCase::PushOnHelp()
319 //-------------------------------
320 {
321   char* LanguageShort = myHomardGen->GetLanguageShort();
322   HOMARD_UTILS::PushOnHelp(QString("gui_create_case.html"), QString(""), QString(LanguageShort));
323 }
324 // ---------------------------------
325 void MonCreateCase::SetNewName()
326 // ------------------------------
327 {
328   HOMARD::listeCases_var  MyCases = myHomardGen->GetAllCasesName();
329   int num = 0; QString aCaseName="";
330   while (aCaseName=="" )
331   {
332     aCaseName.setNum(num+1) ;
333     aCaseName.insert(0, QString("Case_")) ;
334     for ( int i=0; i<MyCases->length(); i++)
335     {
336       if ( aCaseName ==  QString((MyCases)[i]))
337       {
338           num=num+1;
339           aCaseName="";
340           break;
341       }
342    }
343   }
344   LEName->clear() ;
345   LEName->insert(aCaseName);
346 }
347
348 // ------------------------------------------------------------------------
349 void MonCreateCase::SetDirName()
350 // ------------------------------------------------------------------------
351 {
352   QString aDirName=QFileDialog::getExistingDirectory ();
353   if (!(aDirName.isEmpty()))LEDirName->setText(aDirName);
354 }
355 // ------------------------------------------------------------------------
356 void MonCreateCase::SetFileName()
357 // ------------------------------------------------------------------------
358 {
359   QString fileName0 = LEFileName->text().trimmed();
360   QString fileName = HOMARD_QT_COMMUN::PushNomFichier();
361   if (fileName.isEmpty()) fileName = fileName0 ;
362   LEFileName->setText(fileName);
363 }
364 // ------------------------------------------------------------------------
365 void MonCreateCase::SetConforme()
366 // ------------------------------------------------------------------------
367 {
368   GBTypeNoConf->setVisible(0);
369   adjustSize();
370   _ConfType=1;
371 }
372 // ------------------------------------------------------------------------
373 void MonCreateCase::SetNonConforme()
374 // ------------------------------------------------------------------------
375 {
376   GBTypeNoConf->setVisible(1);
377   RB1NpM->setChecked(true);
378   adjustSize();
379   _ConfType=2;
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();
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   adjustSize();
414 }
415 // ------------------------------------------------------------------------
416 void MonCreateCase::AddBoundaryDi(QString newBoundary)
417 // ------------------------------------------------------------------------
418 {
419   CBBoundaryDi->insertItem(0,newBoundary);
420   CBBoundaryDi->setCurrentIndex(0);
421 }
422 // ------------------------------------------------------------------------
423 void MonCreateCase::PushBoundaryDiNew()
424 // ------------------------------------------------------------------------
425 {
426    MonCreateBoundaryDi *BoundaryDlg = new MonCreateBoundaryDi(this, true,
427                 HOMARD::HOMARD_Gen::_duplicate(myHomardGen), _aCaseName, "") ;
428    BoundaryDlg->show();
429 }
430 // ------------------------------------------------------------------------
431 void MonCreateCase::PushBoundaryDiEdit()
432 // ------------------------------------------------------------------------
433 {
434   if (CBBoundaryDi->currentText() == QString(""))  return;
435   MonEditBoundaryDi *BoundaryDlg = new MonEditBoundaryDi(this, true,
436        HOMARD::HOMARD_Gen::_duplicate(myHomardGen), _aCaseName, CBBoundaryDi->currentText() ) ;
437   BoundaryDlg->show();
438 }
439 // ------------------------------------------------------------------------
440 void MonCreateCase::PushBoundaryDiHelp()
441 // ------------------------------------------------------------------------
442 {
443   char* LanguageShort = myHomardGen->GetLanguageShort();
444   HOMARD_UTILS::PushOnHelp(QString("gui_create_boundary.html"), QString("frontiere-discrete"), QString(LanguageShort));
445 }
446 // ------------------------------------------------------------------------
447 void MonCreateCase::SetBoundaryA()
448 // ------------------------------------------------------------------------
449 {
450   MESSAGE("Debut de SetBoundaryA ");
451   if (CBBoundaryA->isChecked())
452   {
453     bool bOK = PushOnApply();
454     if (bOK) { GBBoundaryA->setVisible(1); }
455     else     { GBBoundaryA->setVisible(0);
456                CBBoundaryA->setChecked(0);
457                CBBoundaryA->setCheckState(Qt::Unchecked); }
458   }
459   else { GBBoundaryA->setVisible(0); }
460   adjustSize();
461 //
462 //   MESSAGE("Fin de SetBoundaryA ");
463 }
464 // ------------------------------------------------------------------------
465 void MonCreateCase::AddBoundaryAn(QString newBoundary)
466 // ------------------------------------------------------------------------
467 {
468   MESSAGE("Debut de AddBoundaryAn ");
469 // Ajout d'une nouvelle colonne
470   int nbcol = TWBoundary->columnCount();
471 //   MESSAGE("nbcol " <<  nbcol);
472   nbcol += 1 ;
473   TWBoundary->setColumnCount ( nbcol ) ;
474   QTableWidgetItem *__colItem = new QTableWidgetItem();
475   __colItem->setText(QApplication::translate("CreateCase", newBoundary.toStdString().c_str(), 0, QApplication::UnicodeUTF8));
476   TWBoundary->setHorizontalHeaderItem(nbcol-1, __colItem);
477 /*  TWBoundary->horizontalHeaderItem(nbcol-1)->setFlags( Qt::ItemIsSelectable|Qt::ItemIsEnabled );*/
478 // Chaque case est a cocher
479   int nbrow = TWBoundary->rowCount();
480 //   MESSAGE("nbrow " <<  nbrow);
481   for ( int i = 0; i < nbrow; i++ )
482   {
483     TWBoundary->setItem( i, nbcol-1, new QTableWidgetItem( QString ("") ) );
484     TWBoundary->item( i, nbcol-1 )->setFlags( 0 );
485     TWBoundary->item( i, nbcol-1 )->setFlags( Qt::ItemIsUserCheckable|Qt::ItemIsEnabled  );
486     TWBoundary->item( i, nbcol-1 )->setCheckState( Qt::Unchecked );
487   }
488   TWBoundary->resizeColumnToContents(nbcol-1);
489 //   TWBoundary->resizeRowsToContents();
490 //   MESSAGE("Fin de AddBoundaryAn ");
491 }
492 // ------------------------------------------------------------------------
493 void MonCreateCase::PushBoundaryAnNew()
494 // ------------------------------------------------------------------------
495 {
496    MonCreateBoundaryAn *BoundaryDlg = new MonCreateBoundaryAn(this, true,
497                 HOMARD::HOMARD_Gen::_duplicate(myHomardGen), _aCaseName) ;
498    BoundaryDlg->show();
499 }
500 // ------------------------------------------------------------------------
501 void MonCreateCase::PushBoundaryAnEdit()
502 // ------------------------------------------------------------------------
503 {
504   QString nom="";
505   int nbcol = TWBoundary->columnCount();
506   for ( int i = 1; i < nbcol; i++ )
507   {
508     QTableWidgetItem *__colItem = new QTableWidgetItem();
509     __colItem = TWBoundary->horizontalHeaderItem(i);
510     nom = QString(__colItem->text()) ;
511     MESSAGE("nom "<<nom.toStdString().c_str());
512     if (nom != QString(""))
513     { MonEditBoundaryAn *BoundaryDlg = new MonEditBoundaryAn(this, true,
514         HOMARD::HOMARD_Gen::_duplicate(myHomardGen), _aCaseName, nom ) ;
515       BoundaryDlg->show(); }
516   }
517 }
518 // ------------------------------------------------------------------------
519 void MonCreateCase::PushBoundaryAnHelp()
520 // ------------------------------------------------------------------------
521 {
522   char* LanguageShort = myHomardGen->GetLanguageShort();
523   HOMARD_UTILS::PushOnHelp(QString("gui_create_boundary.html"), QString("frontiere-analytique"), QString(LanguageShort));
524 }
525 // ------------------------------------------------------------------------
526 void MonCreateCase::CaseNameChanged()
527 // ------------------------------------------------------------------------
528 {
529     if (_aCaseName != LEName->text().trimmed())
530     {
531        LEFileName->setReadOnly(false);
532        PushFichier->show();
533     }
534 }
535 // ------------------------------------------------------------------------
536 void MonCreateCase::SetAdvanced()
537 // ------------------------------------------------------------------------
538 {
539   MESSAGE("Debut de SetAdvanced ");
540   if (CBAdvanced->isChecked()) { GBAdvancedOptions->setVisible(1); }
541   else
542   { GBAdvancedOptions->setVisible(0);
543     CBPyramid->setChecked(false);
544     _Pyram = 0 ;
545  }
546   adjustSize();
547 }