Salome HOME
Fix problems of 'make distcheck'
[modules/homard.git] / src / HOMARDGUI / MonCreateCase.cxx
1 using namespace std;
2
3 #include "MonCreateCase.h"
4 #include "MonCreateBoundaryAn.h"
5 #include "MonEditBoundaryAn.h"
6 #include "MonCreateBoundaryDi.h"
7 #include "MonEditBoundaryDi.h"
8
9 #include <QFileDialog>
10 #include <QMessageBox>
11
12 #include "SalomeApp_Tools.h"
13 #include "HOMARDGUI_Utils.h"
14 #include "HomardQtCommun.h"
15 #include <utilities.h>
16
17
18 // -----------------------------------------------------------------------------------------
19 MonCreateCase::MonCreateCase(QWidget* parent, bool modal, HOMARD::HOMARD_Gen_var myHomardGen)
20 // -----------------------------------------------------------------------------------------
21 /* Constructs a MonCreateCase
22  * Inherits from CasHomard
23  * Sets attributes to default values
24  */
25     :
26     Ui_CreateCase(),
27     _aCaseName(""),_aDirName(""), _ConfType(1)
28     {
29       _myHomardGen=HOMARD::HOMARD_Gen::_duplicate(myHomardGen);
30       setupUi(this);
31       setModal(modal);
32       InitConnect();
33
34       SetNewCaseName() ;
35       GBBoundaryA->setVisible(0);
36       GBBoundaryD->setVisible(0);
37       GBTypeNoConf->setVisible(0);
38       adjustSize();
39       GetBoundarys();
40
41     }
42
43 // ------------------------------------------------------------------------
44 MonCreateCase::~MonCreateCase()
45 // ------------------------------------------------------------------------
46 {
47     // no need to delete child widgets, Qt does it all for us
48 }
49 // ------------------------------------------------------------------------
50 void MonCreateCase::InitConnect()
51 // ------------------------------------------------------------------------
52 {
53     connect( PushDir,        SIGNAL(pressed()), this, SLOT(SetDirName()));
54     connect( PushFichier,    SIGNAL(pressed()), this, SLOT(SetFileName()));
55
56     connect( RBConforme,     SIGNAL(clicked()), this, SLOT(SetConforme()));
57     connect( RBNonConforme,  SIGNAL(clicked()), this, SLOT(SetNonConforme()));
58     connect( RB1NpM,         SIGNAL(clicked()), this, SLOT(Set1NpM()));
59     connect( RB1NpA,         SIGNAL(clicked()), this, SLOT(Set1NpA()));
60     connect( RBQuelconque,   SIGNAL(clicked()), this, SLOT(SetQuelconque()));
61
62     connect( CBBoundaryD,      SIGNAL(stateChanged(int)), this, SLOT(SetBoundaryD()));
63     connect( PBBoundaryDiNew,  SIGNAL(pressed()), this, SLOT(PushBoundaryDiNew()));
64     connect( PBBoundaryDiEdit, SIGNAL(pressed()), this, SLOT(PushBoundaryDiEdit()) );
65     connect( CBBoundaryA,      SIGNAL(stateChanged(int)), this, SLOT(SetBoundaryA()));
66     connect( PBBoundaryAnNew,  SIGNAL(pressed()), this, SLOT(PushBoundaryAnNew()));
67     connect( PBBoundaryAnHelp, SIGNAL(pressed()), this, SLOT(PushBoundaryAnHelp()) );
68
69     connect( buttonOk,       SIGNAL(pressed()), this, SLOT(PushOnOK()));
70     connect( buttonApply,    SIGNAL(pressed()), this, SLOT(PushOnApply()));
71     connect( buttonCancel,   SIGNAL(pressed()), this, SLOT(close()));
72     connect( buttonHelp,     SIGNAL(pressed()), this, SLOT(PushOnHelp()));
73     connect( LECaseName,     SIGNAL(textChanged(QString)), this, SLOT(CaseNameChanged()));
74 }
75 // ------------------------------------------------------------------------
76 void MonCreateCase::GetBoundarys()
77 // ------------------------------------------------------------------------
78 {
79      HOMARD::HOMARD_Boundary_var myBoundary ;
80      HOMARD::listeBoundarys_var  mesBoundarys = _myHomardGen->GetAllBoundarys();
81      for (int i=0; i<mesBoundarys->length(); i++)
82      {
83          myBoundary = _myHomardGen->GetBoundary(mesBoundarys[i]);
84          int type_obj = myBoundary->GetBoundaryType() ;
85          if ( type_obj==0 ) { CBBoundaryDi->addItem(QString(mesBoundarys[i])); }
86      }
87 }
88
89 // -------------------------------
90 bool MonCreateCase::PushOnApply()
91 // --------------------------------
92 {
93   MESSAGE("MonCreateCase::PushOnApply");
94   QString aCaseName=LECaseName->text().trimmed();
95   if ( aCaseName == "" )
96   {
97     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
98                               QObject::tr("HOM_CASE_NAME") );
99     return false;
100   }
101
102   QString aDirName=LEDirName->text().trimmed();
103   if (aDirName == QString(""))
104   {
105     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
106                               QObject::tr("HOM_CASE_DIRECTORY_1") );
107     return false;
108   }
109   if ((aDirName != _aDirName) and (_myHomardGen->VerifieDir( aDirName.toStdString().c_str()) == false))
110   {
111     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
112                               QObject::tr("HOM_CASE_DIRECTORY_2") );
113     return false;
114   }
115   if (chdir(aDirName.toStdString().c_str()) != 0)
116   {
117     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
118                               QObject::tr("HOM_CASE_DIRECTORY_3") );
119     return false;
120   }
121
122   QString aFileName=LEFileName->text().trimmed();
123   if (aFileName ==QString(""))
124   {
125     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
126                               QObject::tr("HOM_CASE_MESH") );
127     return false;
128   }
129
130   QString aMeshName = HOMARD_QT_COMMUN::LireNomMaillage(aFileName);
131   if (aMeshName == "" )
132   {
133     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
134                               QObject::tr("HOM_MED_FILE_2") );
135     return false;
136   }
137
138   if (CBBoundaryA->isChecked())
139   {
140     QStringList ListeGroup ;
141     QString NomGroup ;
142     int nbcol = TWBoundary->columnCount();
143     int nbrow = TWBoundary->rowCount();
144     for ( int col=1; col< nbcol; col++)
145     {
146       for ( int row=0; row< nbrow; row++)
147       {
148         if ( TWBoundary->item( row, col )->checkState() ==  Qt::Checked )
149         {
150 //        Nom du groupe
151           NomGroup = QString(TWBoundary->item(row, 0)->text()) ;
152 //           MESSAGE("NomGroup "<<NomGroup.toStdString().c_str());
153           for ( int nugr = 0 ; nugr<ListeGroup.size(); nugr++)
154           {
155 //             MESSAGE("....... "<<ListeGroup[nugr].toStdString().c_str());
156             if ( NomGroup == ListeGroup[nugr] )
157             {
158               QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
159                                         QObject::tr("HOM_CASE_GROUP").arg(NomGroup) );
160               return false;
161             }
162           }
163           ListeGroup.insert(0, NomGroup );
164         }
165       }
166     }
167   }
168
169   if (aCaseName != _aCaseName )
170   {
171     _aCaseName = aCaseName;
172     try
173     {
174     aCase = _myHomardGen->CreateCase( \
175             CORBA::string_dup(_aCaseName.toStdString().c_str()),  \
176             CORBA::string_dup(aMeshName.toStdString().c_str()),  \
177             CORBA::string_dup(aFileName.toStdString().c_str()) );
178     }
179     catch( SALOME::SALOME_Exception& S_ex )
180     {
181       QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
182                                 QString(CORBA::string_dup(S_ex.details.text)) );
183       try
184       {
185           aCase = _myHomardGen->GetCas(_aCaseName.toStdString().c_str());
186           string iter0 = aCase->GetIter0Name();
187           HOMARD::HOMARD_Iteration_var aIter =  _myHomardGen->GetIteration(iter0.c_str());
188           QString aFileName = aIter->GetMeshFile();
189           LEFileName->setText(aFileName);
190           LEFileName->setReadOnly(true);
191           PushFichier->hide();
192       }
193       catch( SALOME::SALOME_Exception& S_ex )  {};
194       return false;
195     }
196     LEFileName->setReadOnly(true);
197     PushFichier->hide();
198   }
199
200   aCase->SetDirName(aDirName.toStdString().c_str());
201   _aDirName=aDirName;
202   aCase->SetConfType(_ConfType);
203
204 // Enregistrement de la frontiere discrete
205   if (CBBoundaryD->isChecked())
206   {
207     QString monBoundaryDiName=CBBoundaryDi->currentText();
208     if (monBoundaryDiName != "" )
209     {
210       aCase->AddBoundaryGroup(monBoundaryDiName.toStdString().c_str(), "");
211     }
212   }
213
214 // Enregistrement des liens (groupe,frontiere analytique)
215   if (CBBoundaryA->isChecked())
216   {
217     QString NomGroup ;
218     int nbcol = TWBoundary->columnCount();
219     int nbrow = TWBoundary->rowCount();
220     for ( int col=1; col< nbcol; col++)
221     {
222       for ( int row=0; row< nbrow; row++)
223       {
224         if ( TWBoundary->item( row, col )->checkState() ==  Qt::Checked )
225         {
226 //        Nom du groupe
227           NomGroup = QString(TWBoundary->item(row, 0)->text()) ;
228 //        Nom de la frontiere
229           QTableWidgetItem *__colItem = new QTableWidgetItem();
230           __colItem = TWBoundary->horizontalHeaderItem(col);
231           aCase->AddBoundaryGroup(QString(__colItem->text()).toStdString().c_str(), NomGroup.toStdString().c_str());
232         }
233       }
234     }
235   }
236
237   HOMARD_UTILS::updateObjBrowser();
238   return true;
239 }
240 // ---------------------------
241 void MonCreateCase::PushOnOK()
242 // ---------------------------
243 {
244      bool bOK = PushOnApply();
245      if ( bOK )  this->close();
246 }
247 //------------------------------
248 void MonCreateCase::PushOnHelp()
249 //-------------------------------
250 {
251   HOMARD_UTILS::PushOnHelp(QString("gui_create_case.html"));
252 }
253 // ---------------------------------
254 void MonCreateCase::SetNewCaseName()
255 // ------------------------------
256 {
257   HOMARD::listeCases_var  MyCases = _myHomardGen->GetAllCases();
258   int num = 0; QString aCaseName="";
259   while (aCaseName=="" )
260   {
261     aCaseName.setNum(num+1) ;
262     aCaseName.insert(0, QString("Case_")) ;
263     for ( int i=0; i<MyCases->length(); i++)
264     {
265       if ( aCaseName ==  QString((MyCases)[i]))
266       {
267           num=num+1;
268           aCaseName="";
269           break;
270       }
271    }
272   }
273   LECaseName->clear() ;
274   LECaseName->insert(aCaseName);
275 }
276
277 // ------------------------------------------------------------------------
278 void MonCreateCase::SetDirName()
279 // ------------------------------------------------------------------------
280 {
281   QString aDirName=QFileDialog::getExistingDirectory ();
282   if (!(aDirName.isEmpty()))LEDirName->setText(aDirName);
283 }
284 // ------------------------------------------------------------------------
285 void MonCreateCase::SetFileName()
286 // ------------------------------------------------------------------------
287 {
288   QString fileName0 = LEFileName->text().trimmed();
289   QString fileName = HOMARD_QT_COMMUN::PushNomFichier();
290   if (fileName.isEmpty()) fileName = fileName0 ;
291   LEFileName->setText(fileName);
292 }
293 // ------------------------------------------------------------------------
294 void MonCreateCase::SetConforme()
295 // ------------------------------------------------------------------------
296 {
297   GBTypeNoConf->setVisible(0);
298   adjustSize();
299   _ConfType=1;
300 }
301 // ------------------------------------------------------------------------
302 void MonCreateCase::SetNonConforme()
303 // ------------------------------------------------------------------------
304 {
305   GBTypeNoConf->setVisible(1);
306   RB1NpM->setChecked(true);
307   adjustSize();
308   _ConfType=2;
309 }
310 // ------------------------------------------------------------------------
311 void MonCreateCase::Set1NpM()
312 // ------------------------------------------------------------------------
313 {
314   _ConfType=2;
315 }
316 // ------------------------------------------------------------------------
317 void MonCreateCase::Set1NpA()
318 // ------------------------------------------------------------------------
319 {
320   _ConfType=3;
321 }
322 // ------------------------------------------------------------------------
323 void MonCreateCase::SetQuelconque()
324 // ------------------------------------------------------------------------
325 {
326   _ConfType=4;
327 }
328 // ------------------------------------------------------------------------
329 void MonCreateCase::SetBoundaryD()
330 // ------------------------------------------------------------------------
331 {
332   if    (CBBoundaryD->isChecked())
333   {
334     GBBoundaryD->setVisible(1);
335     bool bOK = PushOnApply();
336     if ( ! bOK) {
337       GBBoundaryD->setVisible(0);
338       CBBoundaryD->setChecked(0);
339       CBBoundaryD->setCheckState(Qt::Unchecked);
340     }
341   }
342   else { GBBoundaryD->setVisible(0); }
343   adjustSize();
344 }
345 // ------------------------------------------------------------------------
346 void MonCreateCase::addBoundaryDi(QString newBoundary)
347 // ------------------------------------------------------------------------
348 {
349   CBBoundaryDi->insertItem(0,newBoundary);
350   CBBoundaryDi->setCurrentIndex(0);
351 }
352 // ------------------------------------------------------------------------
353 void MonCreateCase::PushBoundaryDiNew()
354 // ------------------------------------------------------------------------
355 {
356    MonCreateBoundaryDi *BoundaryDlg = new MonCreateBoundaryDi(this, true,
357                 HOMARD::HOMARD_Gen::_duplicate(_myHomardGen), _aCaseName, "") ;
358    BoundaryDlg->show();
359 }
360 // ------------------------------------------------------------------------
361 void MonCreateCase::PushBoundaryDiEdit()
362 // ------------------------------------------------------------------------
363 {
364   if (CBBoundaryDi->currentText() == QString(""))  return;
365   MonEditBoundaryDi *BoundaryDlg = new MonEditBoundaryDi(this, true,
366        HOMARD::HOMARD_Gen::_duplicate(_myHomardGen), _aCaseName, CBBoundaryDi->currentText() ) ;
367   BoundaryDlg->show();
368 }
369 // ------------------------------------------------------------------------
370 void MonCreateCase::SetBoundaryA()
371 // ------------------------------------------------------------------------
372 {
373   MESSAGE("Debut de MonCreateCase::SetBoundaryA ");
374   if (CBBoundaryA->isChecked())
375   {
376     bool bOK = PushOnApply();
377     if (bOK) {
378       GBBoundaryA->setVisible(1);
379       HOMARD::ListGroupType_var _listeGroupesCas = aCase->GetGroups();
380       QTableWidgetItem *__colItem = new QTableWidgetItem();
381       __colItem->setText(QApplication::translate("CreateCase", "", 0, QApplication::UnicodeUTF8));
382       TWBoundary->setHorizontalHeaderItem(0, __colItem);
383       for ( int i = 0; i < _listeGroupesCas->length(); i++ )
384       {
385         TWBoundary->insertRow(i);
386         TWBoundary->setItem( i, 0, new QTableWidgetItem(QString((_listeGroupesCas)[i]).trimmed()));
387         TWBoundary->item( i, 0 )->setFlags(Qt::ItemIsEnabled |Qt::ItemIsSelectable );
388       }
389       TWBoundary->resizeColumnsToContents();
390       TWBoundary->resizeRowsToContents();
391       TWBoundary->clearSelection();
392     }
393     else{
394       GBBoundaryA->setVisible(0);
395       CBBoundaryA->setChecked(0);
396       CBBoundaryA->setCheckState(Qt::Unchecked); }
397   }
398   else { GBBoundaryA->setVisible(0); }
399   adjustSize();
400 //
401 //   MESSAGE("Fin de MonCreateCase::SetBoundaryA ");
402 }
403 // ------------------------------------------------------------------------
404 void MonCreateCase::addBoundaryAn(QString newBoundary)
405 // ------------------------------------------------------------------------
406 {
407   MESSAGE("Debut de MonCreateCase::addBoundaryAn ");
408 // Ajout d'une nouvelle colonne
409   int nbcol = TWBoundary->columnCount();
410   MESSAGE("nbcol " <<  nbcol);
411   nbcol += 1 ;
412   TWBoundary->setColumnCount ( nbcol ) ;
413   QTableWidgetItem *__colItem = new QTableWidgetItem();
414   __colItem->setText(QApplication::translate("CreateCase", newBoundary.toStdString().c_str(), 0, QApplication::UnicodeUTF8));
415   TWBoundary->setHorizontalHeaderItem(nbcol-1, __colItem);
416 /*  TWBoundary->horizontalHeaderItem(nbcol-1)->setFlags( Qt::ItemIsSelectable|Qt::ItemIsEnabled );*/
417 // Chaque case est a cocher
418   int nbrow = TWBoundary->rowCount();
419   MESSAGE("nbrow " <<  nbrow);
420   for ( int i = 0; i < nbrow; i++ )
421   {
422     TWBoundary->setItem( i, nbcol-1, new QTableWidgetItem( QString ("") ) );
423     TWBoundary->item( i, nbcol-1 )->setFlags( 0 );
424     TWBoundary->item( i, nbcol-1 )->setFlags( Qt::ItemIsUserCheckable|Qt::ItemIsEnabled  );
425     TWBoundary->item( i, nbcol-1 )->setCheckState( Qt::Unchecked );
426   }
427   TWBoundary->resizeColumnToContents(nbcol-1);
428 //   TWBoundary->resizeRowsToContents();
429 //   MESSAGE("Fin de addBoundaryAn ");
430 }
431 // ------------------------------------------------------------------------
432 void MonCreateCase::PushBoundaryAnNew()
433 // ------------------------------------------------------------------------
434 {
435    MonCreateBoundaryAn *BoundaryDlg = new MonCreateBoundaryAn(this, true,
436                 HOMARD::HOMARD_Gen::_duplicate(_myHomardGen), _aCaseName) ;
437    BoundaryDlg->show();
438 }
439 // ------------------------------------------------------------------------
440 void MonCreateCase::PushBoundaryAnHelp()
441 // ------------------------------------------------------------------------
442 {
443   HOMARD_UTILS::PushOnHelp(QString("gui_create_boundary.html#frontiere-analytique") ) ;
444 }
445 // ------------------------------------------------------------------------
446 void MonCreateCase::CaseNameChanged()
447 // ------------------------------------------------------------------------
448 {
449     if (_aCaseName != LECaseName->text().trimmed())
450     {
451        LEFileName->setReadOnly(false);
452        PushFichier->show();
453     }
454 }