Salome HOME
b8ee3c879dc7afdb90ccc7456503fb8161c7937d
[modules/smesh.git] / src / SMESHGUI / MG_ADAPTGUI.cxx
1 // Copyright (C) 2007-2020  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  file : MG_ADAPTGUI.cxx
23
24 #include "MG_ADAPTGUI.hxx"
25
26 #include "MEDFileData.hxx"
27 #include "MEDLoader.hxx"
28
29 #include "SUIT_Desktop.h"
30 #include "SUIT_Application.h"
31 #include "SUIT_Session.h"
32
33 #include "SalomeApp_Application.h"
34 #include "SalomeApp_Module.h"
35 #include "SalomeApp_Study.h"
36 #include <SUIT_MessageBox.h>
37 #include <LightApp_SelectionMgr.h>
38 #include <SUIT_OverrideCursor.h>
39 #include <SUIT_ResourceMgr.h>
40 #include <SVTK_ViewWindow.h>
41 #include <SALOME_ListIO.hxx>
42 #include <SUIT_FileDlg.h>
43 #include <QApplication>
44 #include <QButtonGroup>
45 #include <QGridLayout>
46 #include <QGroupBox>
47 #include <QHBoxLayout>
48 #include <QKeyEvent>
49 #include <QLabel>
50 #include <QLineEdit>
51 #include <QCheckBox>
52 #include <QPushButton>
53 #include <QRadioButton>
54 #include <QTabWidget>
55 #include <QVBoxLayout>
56 #include <QDoubleSpinBox>
57 #include <QSpinBox>
58 #include <QTreeWidget>
59 #include <QTreeWidgetItem>
60 #include <QSpacerItem>
61 #include <QString>
62 #include <QHeaderView>
63 #include <QItemDelegate>
64 #include <QFileDialog>
65 #include <QMessageBox>
66 #include <QComboBox>
67
68 #include <vtkPoints.h>
69 #include <vtkUnstructuredGrid.h>
70 #include <vtkIdList.h>
71 #include <vtkCellArray.h>
72 #include <vtkUnsignedCharArray.h>
73 #include <vtkDataSetMapper.h>
74 #include <VTKViewer_CellLocationsArray.h>
75 #include <vtkProperty.h>
76
77 #include <ElCLib.hxx>
78 // SALOME KERNEL includes
79 #include <SALOMEDS_SComponent.hxx>
80 #include <SALOMEDS_SObject.hxx>
81 #include <SALOMEDS_Study.hxx>
82 #include <SALOMEDS_wrap.hxx>
83 #include "SalomeApp_Tools.h"
84 #include <SALOMEconfig.h>
85 #include <med.h>
86 #include <utilities.h>
87
88 #include <TCollection_AsciiString.hxx>
89
90 const int SPACING = 6;            // layout spacing
91 const int MARGIN  = 9;            // layout margin
92
93 //=================================================================================
94 // function : SMESHGUI_MgAdaptDlg()
95 // purpose  :
96 //=================================================================================
97 SMESHGUI_MgAdaptDlg::SMESHGUI_MgAdaptDlg( SalomeApp_Module* theModule, SMESH::MG_ADAPT_ptr myModel, QWidget* parent, bool isCreation )
98     : mySMESHGUI( theModule ), QDialog(parent)
99 {
100   //~model = new MgAdapt(*myModel);
101   model = SMESH::MG_ADAPT::_duplicate(myModel);
102   myData = model->getData();
103   buildDlg();
104   if (!isCreation) readParamsFromHypo();
105 }
106
107 void SMESHGUI_MgAdaptDlg::buildDlg()
108 {
109   setModal( false );
110   setAttribute( Qt::WA_DeleteOnClose, true );
111   setWindowTitle( tr( "ADAPT_PREF_MG_ADAPT" ) );
112   setSizeGripEnabled( true );
113
114   myTabWidget = new QTabWidget( this );
115
116   // Arguments
117
118   myArgs = new SMESHGUI_MgAdaptArguments( myTabWidget );
119   SMESH::str_array* str = model->getOptionValuesStrVec();
120   SMESH::str_array* str2 = model->getCustomOptionValuesStrVec();
121   std::vector<std::string> s;
122   for (int i = 0; i< str->length(); i++) s.push_back( (*str)[i].in());
123   for (int j = str->length(); j< str2->length(); j++) s.push_back((*str2)[ j - str->length() ].in() );
124   //~str.insert( str.end(), str2.begin(), str2.end() );
125
126   myAdvOpt = new MgAdaptAdvWidget(myTabWidget, &s);
127
128   int argsTab = myTabWidget->addTab( myArgs, tr( "Args" ) );
129   int advTab = myTabWidget->addTab( myAdvOpt, tr( "ADVOP" ) );
130
131   myAdvOpt->workingDirectoryLabel         ->setText (tr( "WORKING_DIR" ));
132   myAdvOpt->workingDirectoryPushButton    ->setText (tr( "SELECT_DIR" ));
133   myAdvOpt->keepWorkingFilesCheck         ->setText (tr( "KEEP_WORKING_FILES" ));
134   myAdvOpt->verboseLevelLabel             ->setText (tr( "VERBOSE_LEVEL" ));
135   myAdvOpt->removeLogOnSuccessCheck       ->setText (tr( "REMOVE_LOG_ON_SUCCESS" ));
136   myAdvOpt->logInFileCheck                ->setText (tr( "LOG_IN_FILE" ));
137   myAdvOpt->logGroupBox                   ->setTitle(tr( "LOG_GROUP_TITLE" ));
138
139   // buttons
140   QPushButton* buttonOk = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), this);
141   buttonOk->setAutoDefault(false);
142   QPushButton* buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), this);
143   buttonApply->setAutoDefault(false);
144   QPushButton* buttonCancel = new QPushButton( tr( "SMESH_BUT_CANCEL" ), this );
145   buttonCancel->setAutoDefault( false );
146   QPushButton* buttonHelp = new QPushButton( tr( "SMESH_BUT_HELP" ), this );
147   buttonHelp->setAutoDefault( false );
148
149   QHBoxLayout* btnLayout = new QHBoxLayout;
150   btnLayout->setSpacing( SPACING );
151   btnLayout->setMargin( 0 );
152   btnLayout->addWidget( buttonOk );
153   btnLayout->addStretch( 10 );
154   btnLayout->addWidget( buttonApply );
155   btnLayout->addStretch( 10 );
156   btnLayout->addWidget( buttonCancel );
157   btnLayout->addStretch( 10 );
158   btnLayout->addWidget( buttonHelp );
159
160   QVBoxLayout* l = new QVBoxLayout ( this );
161   l->setMargin( MARGIN );
162   l->setSpacing( SPACING );
163   l->addWidget( myTabWidget );
164   l->addStretch();
165   l->addLayout( btnLayout );
166
167   connect( buttonOk,       SIGNAL(pressed()), this, SLOT(PushOnOK()));
168   connect( buttonApply,    SIGNAL(pressed()), this, SLOT(PushOnApply()));
169   connect( buttonCancel,   SIGNAL(pressed()), this, SLOT(close()));
170   connect( buttonHelp,     SIGNAL(pressed()), this, SLOT(PushOnHelp()));
171
172   connect( myArgs, SIGNAL(meshDimSignal(ADAPTATION_MODE)), myAdvOpt, SLOT( onMeshDimChanged(ADAPTATION_MODE))  );
173 }
174
175
176 //=================================================================================
177 // function : ~SMESHGUI_MgAdaptDlg()
178 // purpose  : Destroys the object and frees any allocated resources
179 //=================================================================================
180 SMESHGUI_MgAdaptDlg::~SMESHGUI_MgAdaptDlg()
181 {
182     //~delete model;
183 }
184
185 //~void SMESHGUI_MgAdaptDlg::setModel(MgAdapt* mg)
186 //~{
187     //~model = mg;
188 //~}
189 SMESH::MG_ADAPT_ptr SMESHGUI_MgAdaptDlg::getModel() const
190 {
191   return model;
192 }
193 /*!
194 \brief Perform clean-up actions on the dialog box closing.
195 */
196 bool SMESHGUI_MgAdaptDlg::PushOnApply()
197 {
198   bool ret = readParamsFromWidgets();
199   return ret;
200 }
201 void SMESHGUI_MgAdaptDlg::PushOnOK()
202 {
203   bool ret = PushOnApply();
204   if ( ret ) reject();
205 }
206 void SMESHGUI_MgAdaptDlg::reject()
207 {
208   QDialog::reject();
209 }
210 bool SMESHGUI_MgAdaptDlg::readParamsFromHypo( ) const
211 {
212   bool ret = true;
213   myArgs->aMedfile->setChecked(myData->fromMedFile) ;
214   if (myData->fromMedFile)
215   {
216
217     *(myArgs->myFileInDir) = QString(myData->myFileInDir) ;
218     myArgs->selectMedFileLineEdit->setText(QString(myData->myMeshFileIn)) ;
219     // myData->myInMeshName = // TODO
220
221   }
222   else
223   {
224     myArgs->aBrowserObject->setText(QString(myData->myInMeshName));
225     //~ myArgs->myFileInDir =""; // TODO
226     //~ myArgs->selectMedFileLineEdit->setText(); // TODO
227   }
228   myArgs->meshNameLineEdit->setText(QString(myData->myOutMeshName));
229   myArgs->medFileCheckBox->setChecked(myData->myMeshOutMed);
230
231   if(myData->myMeshOutMed)
232   {
233     *(myArgs->myFileOutDir) = QString(myData->myFileOutDir);
234     myArgs->selectOutMedFileLineEdit->setText(QString(myData->myMeshFileOut));
235   }
236   else
237   {
238     *(myArgs->myFileOutDir) = QString(""); //TODO
239   }
240
241   myArgs->publishOut->setChecked(myData->myPublish);
242
243   myArgs->localButton->setChecked(myData->myUseLocalMap);
244   myArgs->backgroundButton->setChecked(myData->myUseBackgroundMap);
245   myArgs->constantButton->setChecked(myData->myUseConstantValue);
246
247   if (myData->myUseConstantValue)
248   {
249     myArgs->dvalue->setValue(myData->myConstantValue);
250   }
251   else
252   {
253     myArgs->dvalue->setValue(0.0);
254   }
255
256   if (myData->myUseBackgroundMap)
257   {
258
259     *(myArgs->myFileSizeMapDir) = QString(myData->myFileSizeMapDir) ;
260     myArgs->selectMedFileBackgroundLineEdit->setText(QString(myData->myMeshFileBackground));
261   }
262   else
263   {
264     *(myArgs->myFileSizeMapDir) = QString("") ;  //TODO
265     myArgs->selectMedFileBackgroundLineEdit->setText(""); //TODO
266   }
267
268   myArgs->fieldNameCmb->setCurrentText(QString(myData->myFieldName));
269   myArgs->noTimeStep->setChecked(myData->myUseNoTimeStep);
270   myArgs->lastTimeStep->setChecked( myData->myUseLastTimeStep);
271   myArgs->chosenTimeStep->setChecked(myData->myUseChosenTimeStep);
272   if (myData->myUseChosenTimeStep)
273   {
274     myArgs->rankSpinBox->setValue(myData->myRank);
275     myArgs->timeStep->setValue(myData->myTimeStep);
276   }
277
278   myAdvOpt->workingDirectoryLineEdit->setText(QString(myData->myWorkingDir));
279   myAdvOpt->logInFileCheck->setChecked(myData->myPrintLogInFile);
280
281   myAdvOpt->verboseLevelSpin->setValue(myData->myVerboseLevel);
282   myAdvOpt->removeLogOnSuccessCheck->setChecked(myData->myRemoveLogOnSuccess);
283   myAdvOpt->keepWorkingFilesCheck->setChecked(myData->myKeepFiles);
284
285   return ret;
286
287 }
288
289 bool SMESHGUI_MgAdaptDlg::readParamsFromWidgets()
290 {
291   MESSAGE ("readParamsFromWidgets") ;
292   bool ret = true ;
293   SMESH::MgAdaptHypothesisData* aData = new SMESH::MgAdaptHypothesisData();
294   while ( ret )
295   {
296     // 1. Fichier du maillage de départ
297     aData->fromMedFile = myArgs->aMedfile->isChecked();
298     if (aData->fromMedFile)
299     {
300       aData->myFileInDir = CORBA::string_dup(myArgs->myFileInDir->toStdString().c_str());
301       aData->myMeshFileIn = CORBA::string_dup(myArgs->selectMedFileLineEdit->text().toStdString().c_str());
302         // aData->myInMeshName = // TODO
303     }
304     else // TODO browser
305     {
306       QMessageBox::critical( 0, QObject::tr("MG_ADAPT_ERROR"),
307                                 QObject::tr("MG_ADAPT_MED_FILE_4") );
308       ret = false ;
309       break ;
310   //     aData->myInMeshName = CORBA::string_dup(myArgs->aBrowserObject->text().toStdString().c_str());
311   //     aData->myFileInDir = CORBA::string_dup(myAdvOpt->workingDirectoryLineEdit->text().toStdString().c_str());
312   //
313   //     TCollection_AsciiString aGenericName = (char*)aData->myFileInDir;
314   //     TCollection_AsciiString aGenericName2 = "MgAdapt_";
315   //     aGenericName2 += getpid();
316   //     aGenericName2 += "_";
317   //     aGenericName2 += Abs((Standard_Integer)(long) aGenericName.ToCString());
318   //     aGenericName2 += ".med";
319   //     aGenericName+=aGenericName2;
320   //     emit myArgs->toExportMED(aGenericName.ToCString());
321   //     aData->myMeshFileIn = aGenericName2.ToCString();
322     }
323     // 2. Fichier du maillage de sortie
324     aData->myOutMeshName = CORBA::string_dup(myArgs->meshNameLineEdit->text().toStdString().c_str());
325     aData->myMeshOutMed = myArgs->medFileCheckBox->isChecked();
326     if(aData->myMeshOutMed)
327     {
328       aData->myFileOutDir = CORBA::string_dup(myArgs->myFileOutDir->toStdString().c_str());
329       aData->myMeshFileOut = CORBA::string_dup(myArgs->selectOutMedFileLineEdit->text().toStdString().c_str());
330     }
331     else
332     {
333       aData->myMeshFileOut = "";
334     }
335     aData->myPublish = myArgs->publishOut->isChecked();
336
337     // 3. Type de carte de tailles
338     aData->myUseLocalMap = myArgs->localButton->isChecked();
339     aData->myUseBackgroundMap = myArgs->backgroundButton->isChecked();
340     aData->myUseConstantValue = myArgs->constantButton->isChecked();
341     // 3.1. Constante
342     if (aData->myUseConstantValue)
343     {
344       aData->myConstantValue = myArgs->dvalue->value();
345     }
346     else
347     {
348       aData->myConstantValue = 0.0;
349     }
350     // 3.2. Arrière-plan
351     if (aData->myUseBackgroundMap)
352     {
353       aData->myFileSizeMapDir = CORBA::string_dup(myArgs->myFileSizeMapDir->toStdString().c_str());
354       aData->myMeshFileBackground = CORBA::string_dup(myArgs->selectMedFileBackgroundLineEdit->text().toStdString().c_str());
355     }
356     else
357     {
358       aData->myMeshFileBackground = "";
359     }
360
361   // 4. Le champ
362     if ( ! aData->myUseConstantValue )
363     {
364       if ( strlen(myArgs->fieldNameCmb->currentText().toStdString().c_str()) == 0 )
365       {
366         QMessageBox::critical( 0, QObject::tr("MG_ADAPT_ERROR"),
367                                   QObject::tr("MG_ADAPT_MED_FILE_5") );
368         ret = false ;
369         break ;
370       }
371       {
372         aData->myFieldName = CORBA::string_dup(myArgs->fieldNameCmb->currentText().toStdString().c_str());
373         aData->myUseNoTimeStep = myArgs->noTimeStep->isChecked();
374         aData->myUseLastTimeStep = myArgs->lastTimeStep->isChecked();
375         aData->myUseChosenTimeStep = myArgs->chosenTimeStep->isChecked();
376         if (aData->myUseChosenTimeStep)
377         {
378           aData->myRank = myArgs->rankSpinBox->value();
379           aData->myTimeStep = myArgs->timeStep->value();
380         }
381       }
382     }
383
384     // 5. Options avancées
385     aData->myWorkingDir = CORBA::string_dup(myAdvOpt->workingDirectoryLineEdit->text().toStdString().c_str());
386     aData->myPrintLogInFile = myAdvOpt->logInFileCheck->isChecked();
387     aData->myVerboseLevel = myAdvOpt->verboseLevelSpin->value();
388     aData->myRemoveLogOnSuccess = myAdvOpt->removeLogOnSuccessCheck->isChecked();
389     aData->myKeepFiles = myAdvOpt->keepWorkingFilesCheck->isChecked();
390     model->setData(*aData);
391
392     QString msg;
393     checkParams(msg);
394     break ;
395   }
396
397   delete aData;
398
399   return ret;
400 }
401 bool SMESHGUI_MgAdaptDlg::storeParamsToHypo( const SMESH::MgAdaptHypothesisData& ) const
402 {
403 }
404 /*!
405   \brief Show help page
406 */
407 void SMESHGUI_MgAdaptDlg::PushOnHelp()
408 {
409 //   QString aHelpFile;
410   // if ( myTabWidget->currentIndex() == MinDistance ) {
411   //   aHelpFile = "measurements.html#min-distance-anchor";
412   // } else if ( myTabWidget->currentIndex() == BoundingBox ) {
413   //   aHelpFile = "measurements.html#bounding-box-anchor";
414   // } else if ( myTabWidget->currentWidget() == myAngle ) {
415   //   aHelpFile = "measurements.html#angle-anchor";
416   // } else {
417   //   aHelpFile = "measurements.html#basic-properties-anchor";
418   // }
419
420 //   SMESH::ShowHelpFile( aHelpFile );
421 }
422 bool SMESHGUI_MgAdaptDlg::checkParams(QString& msg)
423 {
424   if ( !QFileInfo( myAdvOpt->workingDirectoryLineEdit->text().trimmed() ).isWritable() )
425   {
426     SUIT_MessageBox::warning( this,
427                               tr( "SMESH_WRN_WARNING" ),
428                               tr( "NO_PERMISSION" ) );
429     return false;
430   }
431
432
433   myAdvOpt->myOptionTable->setFocus();
434   QApplication::instance()->processEvents();
435
436   QString name, value;
437   bool isDefault, ok = true;
438   int iRow = 0, nbRows = myAdvOpt->myOptionTable->topLevelItemCount();
439   for ( ; iRow < nbRows; ++iRow )
440   {
441     QTreeWidgetItem* row = myAdvOpt->myOptionTable->topLevelItem( iRow );
442     myAdvOpt->GetOptionAndValue( row, name, value, isDefault );
443
444     if ( name.simplified().isEmpty() )
445         continue; // invalid custom option
446
447     if ( isDefault ) // not selected option
448         value.clear();
449
450     try
451     {
452       model->setOptionValue( name.toLatin1().constData(), value.toLatin1().constData() );
453     }
454     catch ( const SALOME::SALOME_Exception& ex )
455     {
456       msg = ex.details.text.in();
457       ok = false;
458       break;
459     }
460   }
461
462   return ok;
463 }
464
465 //=================================================================================
466 // function : SMESHGUI_MgAdaptArguments()
467 // purpose  :
468 //=================================================================================
469 SMESHGUI_MgAdaptArguments::SMESHGUI_MgAdaptArguments( QWidget* parent )
470     :QWidget(parent)
471 {
472
473   myFileInDir = new QString("");
474   myFileOutDir = new QString("");
475   myFileSizeMapDir = new QString("");
476   if ( SUIT_FileDlg::getLastVisitedPath().isEmpty() )
477   {
478     *myFileInDir = QDir::currentPath();
479     *myFileOutDir = QDir::currentPath();
480     *myFileSizeMapDir = QDir::currentPath();
481   }
482   else
483   {
484     *myFileInDir = SUIT_FileDlg::getLastVisitedPath();
485     *myFileOutDir = SUIT_FileDlg::getLastVisitedPath();
486     *myFileSizeMapDir = SUIT_FileDlg::getLastVisitedPath();
487   }
488
489   meshDim = 0;
490   meshDimBG = 0;
491   // Mesh in
492   aMeshIn = new QGroupBox( tr( "MeshIn" ), this );
493   aMedfile       = new QRadioButton( tr( "MEDFile" ),    aMeshIn );
494   aBrowser       = new QRadioButton( tr( "Browser" ), aMeshIn );
495   aBrowserObject = new QLineEdit(  aMeshIn );
496   selectMedFilebutton = new QPushButton("...", aMeshIn);
497   selectMedFileLineEdit = new QLineEdit(  aMeshIn );
498
499   meshIn = new QGridLayout( aMeshIn );
500
501   meshIn->setMargin( MARGIN );
502   meshIn->setSpacing( SPACING );
503   meshIn->addWidget( aMedfile,     0, 0, 1,1 );
504   meshIn->addWidget( aBrowser,     0, 1,1,1);
505   meshIn->addWidget( aBrowserObject,     0, 2, 1, 1 );
506   meshIn->addWidget( selectMedFilebutton,  1, 0,1, 1);
507   meshIn->addWidget( selectMedFileLineEdit,  1, 1, 1, 1 );
508   hspacer = new QSpacerItem(188, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
509
510   meshInGroup = new QButtonGroup( this );
511   meshInGroup->addButton( aMedfile,   0 );
512   meshInGroup->addButton( aBrowser,   1 );
513
514   //Mesh out
515
516   aMeshOut = new QGroupBox( tr( "MeshOut" ), this );
517   meshName = new QLabel(tr("MeshName"), aMeshOut);
518   secondHspacer = new QSpacerItem(100, 30);
519   meshNameLineEdit = new QLineEdit(aMeshOut) ;
520   medFileCheckBox = new QCheckBox(tr("MEDFile"), aMeshOut);
521   selectOutMedFilebutton = new QPushButton("...", aMeshOut);
522   thirdHspacer = new QSpacerItem(188, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
523   selectOutMedFileLineEdit = new QLineEdit(aMeshOut) ;
524   publishOut = new  QCheckBox(tr("Publish_MG_ADAPT"), aMeshOut);
525
526   meshOut = new QGridLayout( aMeshOut );
527
528   meshOut->setMargin( MARGIN );
529   meshOut->setSpacing( SPACING );
530   meshOut->addWidget( meshName,  0, 0, 1,1 );
531   meshOut->addItem( secondHspacer,  0, 1, 1, 1 );
532   meshOut->addWidget( meshNameLineEdit, 0, 2,1,1);
533   meshOut->addWidget( medFileCheckBox,  1, 0,1,1 );
534   meshOut->addWidget( selectOutMedFilebutton,  1, 1,1,1 );
535   meshOut->addWidget( selectOutMedFileLineEdit,  1, 2,1,1);
536   meshOut->addWidget( publishOut,  2, 0,1,1 );
537
538   //size map definition
539
540   sizeMapDefinition  = new QGroupBox(tr("SIZE_MAP_DEF"), this);
541   localButton = new QRadioButton(tr("LOCAL_MG_ADAPT"), sizeMapDefinition);
542   backgroundButton = new QRadioButton(tr("BACKGRND_MG_ADAPT"), sizeMapDefinition);
543   constantButton = new QRadioButton(tr("CNST_MG_ADAPT"), sizeMapDefinition);
544   medFileBackground = new QLabel(tr("MED_FILE_BCKG"), sizeMapDefinition);
545   selectMedFileBackgroundbutton = new QPushButton("...", sizeMapDefinition);
546   selectMedFileBackgroundLineEdit = new QLineEdit(sizeMapDefinition);
547   valueLabel = new QLabel(tr("VALUE_MG_ADAPT"), sizeMapDefinition);
548   dvalue = new QDoubleSpinBox(sizeMapDefinition);
549   sizeMapDefGroup = new QButtonGroup( this );
550   sizeMapDefGroup->addButton( localButton,   0 );
551   sizeMapDefGroup->addButton( backgroundButton,   1 );
552   sizeMapDefGroup->addButton( constantButton,   2 );
553
554   sizeMapDefGroupLayout = new QGridLayout(sizeMapDefinition);
555   sizeMapDefGroupLayout->addWidget(localButton, 0,0);
556   sizeMapDefGroupLayout->addWidget(backgroundButton, 0,1);
557   sizeMapDefGroupLayout->addWidget(constantButton, 0,2);
558   sizeMapDefGroupLayout->addWidget(medFileBackground, 1,0);
559   sizeMapDefGroupLayout->addWidget(selectMedFileBackgroundbutton, 1,1);
560   sizeMapDefGroupLayout->addWidget(selectMedFileBackgroundLineEdit, 1,2);
561   sizeMapDefGroupLayout->addWidget(valueLabel, 2,0);
562   sizeMapDefGroupLayout->addWidget(dvalue, 2,1);
563
564   // size Map field
565   sizeMapField = new QGroupBox(tr("SIZE_MAP_FIELD"), this);
566   fieldName = new QLabel(tr("MG_ADAPT_FIELD_NAME"), sizeMapField);
567   fieldNameCmb = new QComboBox(sizeMapField);
568   noTimeStep = new QRadioButton(tr("MG_ADAPT_NO_T_ST"), sizeMapField);
569   lastTimeStep = new QRadioButton(tr("MG_ADAPT_L_ST"), sizeMapField);
570   chosenTimeStep = new QRadioButton(tr("MG_ADAPT_CH_ST"), sizeMapField);
571   timeStepLabel = new QLabel(tr("MG_ADAPT_TSTP"), sizeMapField);
572   timeStep = new QSpinBox(sizeMapField);
573   timeStep->setMinimum(-1);
574   rankLabel = new QLabel(tr("MG_ADAPT_RANK"), sizeMapField);
575   rankSpinBox = new QSpinBox(sizeMapField);
576   rankSpinBox->setMinimum(-1);
577
578   timeStepGroup = new QButtonGroup(this);
579   timeStepGroup->addButton(noTimeStep, 0);
580   timeStepGroup->addButton(lastTimeStep, 1);
581   timeStepGroup->addButton(chosenTimeStep, 2);
582
583   sizeMapFieldGroupLayout = new QGridLayout(sizeMapField);
584
585   sizeMapFieldGroupLayout->addWidget(fieldName, 0,0);
586   sizeMapFieldGroupLayout->addWidget(fieldNameCmb, 0,1);
587   sizeMapFieldGroupLayout->addWidget(noTimeStep, 1,0);
588   sizeMapFieldGroupLayout->addWidget(lastTimeStep, 1,1);
589   sizeMapFieldGroupLayout->addWidget(chosenTimeStep, 1,2);
590   sizeMapFieldGroupLayout->addWidget(timeStepLabel, 2,0);
591   sizeMapFieldGroupLayout->addWidget(timeStep, 2,1);
592   sizeMapFieldGroupLayout->addWidget(rankLabel, 2,2);
593   sizeMapFieldGroupLayout->addWidget(rankSpinBox, 2,3);
594
595   QGridLayout* argumentsLayout = new QGridLayout( this );
596   argumentsLayout->setMargin( MARGIN );
597   argumentsLayout->setSpacing( SPACING );
598
599   argumentsLayout->addWidget( aMeshIn,  0, 0, 1, 3 );
600   argumentsLayout->addWidget( aMeshOut, 1, 0, 1, 3 );
601   argumentsLayout->addWidget( sizeMapDefinition, 2, 0, 1, 3 );
602   argumentsLayout->addWidget( sizeMapField, 3, 0, 1, 3 );
603   argumentsLayout->setColumnStretch( 1, 5 );
604   argumentsLayout->setRowStretch( 4, 5 );
605
606   // Initial state
607   setMode( Mesh, Local);
608   medFileCheckBox->setChecked(true);
609   visibleTimeStepRankLabel (false);
610
611   // Connections
612   connect( meshInGroup,            SIGNAL( buttonClicked( int ) ),  this, SLOT( modeChanged( int ) ) );
613   connect( sizeMapDefGroup,        SIGNAL( buttonClicked( int ) ),  this, SLOT( sizeMapDefChanged( int ) ) );
614   connect( selectMedFilebutton,    SIGNAL( pressed(  ) ),           this, SLOT( onSelectMedFilebuttonClicked(  ) ) );
615   connect( medFileCheckBox,        SIGNAL (stateChanged(int)),      this, SLOT(onMedFileCheckBox(int) ) );
616   connect( publishOut,             SIGNAL (stateChanged(int)),      this, SLOT(onPublishOut(int) ) );
617   connect( selectOutMedFilebutton, SIGNAL( pressed()),              this, SLOT(onSelectOutMedFilebutton()));
618   connect( selectMedFileBackgroundbutton, SIGNAL(pressed()),        this, SLOT(onSelectMedFileBackgroundbutton()) );
619   connect( timeStepGroup,          SIGNAL( buttonClicked( int ) ),  this, SLOT( timeStepGroupChanged( int ) ) );
620   emit updateSelection();
621 }
622
623 //=================================================================================
624 // function : ~SMESHGUI_MgAdaptArguments()
625 // purpose  : Destroys the object and frees any allocated resources
626 //=================================================================================
627 SMESHGUI_MgAdaptArguments::~SMESHGUI_MgAdaptArguments()
628 {
629 }
630
631 void SMESHGUI_MgAdaptArguments::onNoTimeStep(bool disableOther)
632 {
633   noTimeStep->setChecked(true);
634
635   visibleTimeStepRankLabel (false);
636   rankSpinBox->setValue(-2);
637   timeStep->setValue(-2);
638
639   lastTimeStep->setDisabled(disableOther);
640   chosenTimeStep->setDisabled(disableOther);
641 }
642 void SMESHGUI_MgAdaptArguments::onLastTimeStep(bool disableOther)
643 {
644   lastTimeStep->setChecked(true);
645
646   visibleTimeStepRankLabel (false);
647   rankSpinBox->setValue(-1);
648   timeStep->setValue(-1);
649   noTimeStep->setDisabled(disableOther);
650 }
651 void SMESHGUI_MgAdaptArguments::onChosenTimeStep(bool disableOther, int vmax)
652 {
653   chosenTimeStep->setChecked(true);
654
655   visibleTimeStepRankLabel (true);
656   rankSpinBox->setValue(-1);
657   timeStep->setValue(-1);
658   if (vmax) timeStep->setMaximum(vmax);
659 }
660
661 void SMESHGUI_MgAdaptArguments::visibleTimeStepRankLabel(bool visible)
662 {
663   rankLabel->setVisible(visible);
664   rankSpinBox->setVisible(visible);
665
666   timeStepLabel->setVisible(visible);
667   timeStep->setVisible(visible);
668 }
669
670 void SMESHGUI_MgAdaptArguments::onSelectOutMedFilebutton()
671 {
672
673   QString filtre = QString("Med") ;
674   filtre += QString(" files (*.") + QString("med") + QString(");;");
675   QString fileName = QFileDialog::getSaveFileName(this, tr("SAVE_MED"), QString(""), filtre);
676   QFileInfo myFileInfo(fileName);
677   selectOutMedFileLineEdit->setText(myFileInfo.fileName());
678   *myFileOutDir = myFileInfo.path();
679
680 }
681 void SMESHGUI_MgAdaptArguments::onSelectMedFileBackgroundbutton()
682 {
683   QString fileName0 = selectMedFileBackgroundbutton->text().trimmed();
684
685   QString fileName = getMedFileName(false);
686   if (fileName != QString::null)
687   {
688     myFieldList = GetListeChamps(fileName);
689     if (myFieldList.empty())
690     {
691       fileName = fileName0;
692       fieldNameCmb->clear();
693     }
694     else
695     {
696       // fill field name Combobox
697       fieldNameCmb->clear();
698       std::map<QString, int>::const_iterator it;
699       for ( it=myFieldList.begin() ; it != myFieldList.end(); it++)
700       {
701         fieldNameCmb->insertItem(0,QString(it->first));
702         int typeStepInField = it->second > 2 ?  2 : it->second ;
703         timeStepGroupChanged(typeStepInField, false);
704       }
705       // Dimension du maillage de fonds
706       MEDCoupling::MCAuto<MEDCoupling::MEDFileData> mfd = MEDCoupling::MEDFileData::New(fileName.toStdString());
707       meshDimBG = mfd->getMeshes()->getMeshAtPos(0)->getMeshDimension() ;
708       valueAdaptation ();
709     }
710   }
711   else
712   {
713     fileName = fileName0;
714     fieldNameCmb->clear();
715   }
716
717   QFileInfo myFileInfo(fileName);
718   *myFileSizeMapDir = myFileInfo.path();
719   selectMedFileBackgroundLineEdit->setText(myFileInfo.fileName());
720
721 }
722 void SMESHGUI_MgAdaptArguments::onMedFileCheckBox(int state)
723 {
724   if (state == Qt::Checked)
725   {
726     selectOutMedFilebutton->show();
727     selectOutMedFileLineEdit->show();
728     selectOutMedFilebutton->setEnabled(true);
729     selectOutMedFileLineEdit->setEnabled(true);
730   }
731   else
732   {
733     selectOutMedFilebutton->setEnabled(false);
734     selectOutMedFileLineEdit->setEnabled(false);
735     publishOut->setChecked(true);
736   }
737 }
738 void SMESHGUI_MgAdaptArguments::onPublishOut(int state)
739 {
740   if (state == Qt::Unchecked)
741   {
742     medFileCheckBox->setChecked(true);
743   }
744 }
745
746 void SMESHGUI_MgAdaptArguments::onSelectMedFilebuttonClicked()
747 {
748   // bool keep = false;
749   QString fileName0 = selectMedFileLineEdit->text().trimmed();
750
751   QString fileName = getMedFileName(false);
752   if(fileName != QString::null)
753   {
754     QString aMeshName = lireNomMaillage(fileName.trimmed(), meshDim);
755     if (aMeshName == QString::null )
756     {
757       QMessageBox::critical( 0, QObject::tr("MG_ADAPT_ERROR"),
758                                 QObject::tr("MG_ADAPT_MED_FILE_2") );
759       fileName = fileName0;
760     }
761     else
762     {
763       meshNameLineEdit->setText(aMeshName);
764       valueAdaptation ();
765 //       ADAPTATION_MODE aMode = meshDim == 3 ? ADAPTATION_MODE::BOTH : ADAPTATION_MODE::SURFACE; // and when dimesh 3 without 2D mesh?
766 //       emit meshDimSignal(aMode);
767     }
768   }
769   else
770   {
771       return;
772   }
773
774   QFileInfo myFileInfo(fileName);
775   *myFileInDir = myFileInfo.path();
776   *myFileOutDir = myFileInfo.path();
777   selectMedFileLineEdit->setText(myFileInfo.fileName());
778   QString outF = fileName == QString::null ? myFileInfo.fileName() :
779   QString( remove_extension(myFileInfo.fileName().toStdString() ).c_str() )+ QString(".adapt.med");
780   selectOutMedFileLineEdit->setText(outF);
781   onLocalSelected(myFileInfo.filePath());
782
783 }
784
785 void SMESHGUI_MgAdaptArguments::valueAdaptation()
786 {
787   ADAPTATION_MODE aMode ;
788   if ( meshDimBG < 3 )
789   {
790     aMode = meshDim == 3 ? ADAPTATION_MODE::BOTH : ADAPTATION_MODE::SURFACE;
791   }
792   else
793   {
794     aMode = ADAPTATION_MODE::BOTH;
795   }
796   emit meshDimSignal(aMode);
797 }
798
799 void SMESHGUI_MgAdaptArguments::onLocalSelected(QString filePath)
800 {
801   myFieldList = GetListeChamps(filePath, false);
802   if (myFieldList.empty())
803   {
804     if (localButton->isChecked())
805     {
806       fieldNameCmb->clear();
807     }
808   }
809   else
810   {
811     // fill field name Combobox
812     fieldNameCmb->clear();
813     std::map<QString, int>::const_iterator it;
814     for ( it = myFieldList.begin() ; it != myFieldList.end(); it++)
815     {
816       fieldNameCmb->insertItem(0,QString(it->first));
817       // Je ne comprends pas le rapport entre pas de temps et apparition d'un nouveau champ... GN
818       int typeStepInField = it->second > 2 ?  2 : it->second ;
819 //             std::cout << "SMESHGUI_MgAdaptArguments::onLocalSelected typeStepInField : " << typeStepInField << std::endl;
820       timeStepGroupChanged(typeStepInField, false);
821     }
822   }
823 }
824 // =======================================================================
825 // Gestion les boutons qui permettent  de
826 // 1) retourne le nom d'un fichier par une fenetre de dialogue si aucun
827 //    objet est selectionne dans l arbre d etude
828 // 2) retourne le nom du fichier asocie a l objet
829 //    selectionne dans l arbre d etude
830 // =======================================================================
831 QString SMESHGUI_MgAdaptArguments::getMedFileName(bool avertir)
832 {
833
834   QString aFile = QString::null;
835   QString filtre = QString("Med") ;
836   filtre += QString(" files (*.") + QString("med") + QString(");;");
837   aFile = SUIT_FileDlg::getOpenFileName(0, QObject::tr("MG_ADAPT_SELECT_FILE_0"), QString(""), filtre );
838
839   return aFile;
840
841 }
842 void SMESHGUI_MgAdaptArguments::setMode(const Mode theMode, const SIZEMAP theSizeMap )
843 {
844   QRadioButton* aButton = qobject_cast<QRadioButton*>( meshInGroup->button( theMode ) );
845   QRadioButton* bButton = qobject_cast<QRadioButton*>( sizeMapDefGroup->button( theSizeMap ) );
846   if ( aButton )
847   {
848     aButton->setChecked( true );
849     modeChanged( theMode );
850   }
851   if ( bButton )
852   {
853     bButton->setChecked( true );
854     sizeMapDefChanged( theSizeMap );
855   }
856 }
857
858 void SMESHGUI_MgAdaptArguments::modeChanged( int theMode )
859 {
860   clear();
861   if(theMode == Mesh)
862   {
863     aBrowserObject->hide();
864     selectMedFileLineEdit->show();
865     selectMedFilebutton->show();
866     localButton->setEnabled(true);
867   }
868   else
869   {
870     selectMedFileLineEdit->hide();
871     selectMedFilebutton->hide();
872     localButton->setEnabled(false);
873     aBrowserObject->show();
874     sizeMapDefChanged(Background);
875     emit updateSelection();
876   }
877 }
878
879 void SMESHGUI_MgAdaptArguments::sizeMapDefChanged( int  theSizeMap )
880 {
881   fieldNameCmb->clear();
882   if(theSizeMap == Local)
883   {
884     localButton->setEnabled(true);
885     localButton->setChecked(true);
886     medFileBackground->hide();
887     selectMedFileBackgroundbutton->hide();
888     selectMedFileBackgroundLineEdit->hide();
889     selectMedFileBackgroundLineEdit->clear();
890     valueLabel->hide();
891     dvalue->hide();
892
893     sizeMapField->setEnabled(true);
894     if (!selectMedFileLineEdit->text().isEmpty())
895     {
896       QFileInfo myFileInfo(QDir(*myFileInDir), selectMedFileLineEdit->text());
897       onLocalSelected(myFileInfo.filePath());
898     }
899   }
900   else if (theSizeMap == Background)
901   {
902     medFileBackground->show();
903     backgroundButton->setChecked(true);
904     selectMedFileBackgroundbutton->show();
905     selectMedFileBackgroundLineEdit->show();
906     valueLabel->hide();
907     dvalue->hide();
908     sizeMapField->setEnabled(true);
909   }
910   else
911   {
912     medFileBackground->hide();
913     constantButton->setChecked(true);
914     selectMedFileBackgroundbutton->hide();
915     selectMedFileBackgroundLineEdit->clear();
916     selectMedFileBackgroundLineEdit->hide();
917     valueLabel->show();
918     dvalue->show();
919     sizeMapField->setEnabled(false);
920   }
921   meshDimBG = 0;
922   valueAdaptation();
923 }
924 void SMESHGUI_MgAdaptArguments::timeStepGroupChanged(int timeStepType, bool disableOther, int vmax)
925 {
926   switch (timeStepType)
927   {
928   case 0 :
929     onNoTimeStep(disableOther);
930     break;
931   case 1 :
932     onLastTimeStep(disableOther);
933     break;
934   case 2 :
935     onChosenTimeStep(disableOther, vmax);
936   default:
937     break;
938   }
939 }
940
941 void SMESHGUI_MgAdaptArguments::clear()
942 {
943   selectMedFileLineEdit->clear();
944   aBrowserObject->clear();
945
946   meshNameLineEdit->clear();
947   selectOutMedFileLineEdit->clear();
948 }
949 // med_int SMESHGUI_MgAdaptArguments::getMeshDim() const
950 // {
951 //   return meshDim;
952 // }
953 QWidget* ItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &o, const QModelIndex &index) const
954 {
955   bool editable = index.data( EDITABLE_ROLE ).toInt();
956   return editable ? QItemDelegate::createEditor( parent, o, index ) : 0;
957 }
958
959 //////////////////////////////////////////
960 // MgAdaptAdvWidget
961 //////////////////////////////////////////
962
963 MgAdaptAdvWidget::MgAdaptAdvWidget( QWidget* parent, std::vector <std::string>* options, Qt::WindowFlags f )
964   : QWidget( parent, f ), myOptions(options)
965 {
966   setupWidget();
967   myOptionTable->header()->setSectionResizeMode( QHeaderView::ResizeToContents );
968   myOptionTable->setItemDelegate( new ItemDelegate( myOptionTable ) );
969
970   for ( int i = 0, nb = myOptions->size(); i < nb; ++i )
971   {
972     AddOption( (*myOptions)[i].c_str() );
973   }
974
975   connect( myOptionTable, SIGNAL( itemChanged(QTreeWidgetItem *, int)), SLOT( itemChanged(QTreeWidgetItem *, int )));
976   connect( addBtn,        SIGNAL(clicked()),       this, SLOT( onAddOption() ) );
977   connect(workingDirectoryPushButton, SIGNAL(pressed()),  this, SLOT(_onWorkingDirectoryPushButton()));
978 }
979
980 MgAdaptAdvWidget::~MgAdaptAdvWidget()
981 {
982 }
983
984 void MgAdaptAdvWidget::AddOption( const char* option, bool isCustom )
985 {
986   QString name, value;
987   bool isDefault = false;
988   if ( option )
989   {
990     QStringList name_value_type = QString(option).split( ":", QString::KeepEmptyParts );
991     if ( name_value_type.size() > 0 )
992         name = name_value_type[0];
993     if ( name_value_type.size() > 1 )
994         value = name_value_type[1];
995     if ( name_value_type.size() > 2 )
996         isDefault = !name_value_type[2].toInt();
997   }
998   QTreeWidget* table = myOptionTable;
999   //table->setExpanded( true );
1000
1001   QTreeWidgetItem* row;
1002   if (optionTreeWidgetItem.size())
1003   {
1004     std::map<QString, QTreeWidgetItem *>::iterator it = optionTreeWidgetItem.find(name);
1005     if(it != optionTreeWidgetItem.end()) return; // option exist
1006     else
1007     {
1008       row = getNewQTreeWidgetItem(table, option, name, isCustom);
1009     }
1010   }
1011   else
1012   {
1013     row = getNewQTreeWidgetItem(table, option, name, isCustom);
1014   }
1015   row->setText( 0, tr( name.toLatin1().constData() ));
1016   row->setText( 1, tr( value.toLatin1().constData() ));
1017   row->setCheckState( 0, isDefault ? Qt::Unchecked : Qt::Checked);
1018   row->setData( NAME_COL, PARAM_NAME, name );
1019
1020   if ( isCustom )
1021   {
1022     myOptionTable->scrollToItem( row );
1023     myOptionTable->setCurrentItem( row );
1024     myOptionTable->editItem( row, NAME_COL );
1025   }
1026 }
1027
1028 QTreeWidgetItem* MgAdaptAdvWidget::getNewQTreeWidgetItem(QTreeWidget* table, const char* option, QString& name, bool isCustom)
1029 {
1030   QTreeWidgetItem* row = new QTreeWidgetItem( table );
1031   row->setData( NAME_COL, EDITABLE_ROLE, int( isCustom && !option ));
1032   row->setFlags( row->flags() | Qt::ItemIsEditable );
1033   optionTreeWidgetItem.insert(std::pair <QString, QTreeWidgetItem*> (name, row));
1034
1035   return row;
1036 }
1037
1038 void MgAdaptAdvWidget::onAddOption()
1039 {
1040   AddOption( NULL, true );
1041 }
1042 void MgAdaptAdvWidget::GetOptionAndValue( QTreeWidgetItem * tblRow,
1043         QString&          option,
1044         QString&          value,
1045         bool&             isDefault)
1046 {
1047   option    = tblRow->data( NAME_COL, PARAM_NAME ).toString();
1048   value     = tblRow->text( VALUE_COL );
1049   isDefault = ! tblRow->checkState( NAME_COL );
1050
1051 }
1052
1053 void MgAdaptAdvWidget::itemChanged(QTreeWidgetItem* tblRow, int column)
1054 {
1055   if ( tblRow )
1056   {
1057     myOptionTable->blockSignals( true );
1058
1059     tblRow->setData( VALUE_COL, EDITABLE_ROLE, int( tblRow->checkState( NAME_COL )));
1060
1061     int c = tblRow->checkState( NAME_COL ) ? 0 : 150;
1062     tblRow->setForeground( VALUE_COL, QBrush( QColor( c, c, c )));
1063
1064     if ( column == NAME_COL && tblRow->data( NAME_COL, EDITABLE_ROLE ).toInt() ) // custom table
1065     {
1066       tblRow->setData( NAME_COL, PARAM_NAME, tblRow->text( NAME_COL ));
1067     }
1068
1069     myOptionTable->blockSignals( false );
1070   }
1071 }
1072 void MgAdaptAdvWidget::setupWidget()
1073 {
1074   if (this->objectName().isEmpty())
1075       this->setObjectName(QString(tr("MG-ADAPT-ADV")));
1076   this->resize(337, 369);
1077   gridLayout_4 = new QGridLayout(this);
1078   gridLayout_4->setObjectName(QString("gridLayout_4"));
1079   myOptionTable = new MgAdaptAdvWidgetTreeWidget(this);
1080   QFont font;
1081   font.setBold(false);
1082   font.setWeight(50);
1083   QTreeWidgetItem *__qtreewidgetitem = new QTreeWidgetItem();
1084   __qtreewidgetitem->setFont(1, font);
1085   __qtreewidgetitem->setFont(0, font);
1086   __qtreewidgetitem->setText(1, tr("OPTION_VALUE_COLUMN"));
1087   __qtreewidgetitem->setText(0, tr("OPTION_NAME_COLUMN"));
1088   myOptionTable->setHeaderItem(__qtreewidgetitem);
1089   myOptionTable->setObjectName(QString("myOptionTable"));
1090   myOptionTable->setEditTriggers(QAbstractItemView::DoubleClicked|QAbstractItemView::EditKeyPressed);
1091   myOptionTable->setTabKeyNavigation(true);
1092
1093   gridLayout_4->addWidget(myOptionTable, 0, 0, 1, 2);
1094
1095   addBtn = new QPushButton(this);
1096   addBtn->setText(QApplication::translate("SMESH_AdvOptionsWdg", "ADD_OPTION_BTN", Q_NULLPTR));
1097   addBtn->setObjectName(QString("addBtn"));
1098
1099   gridLayout_4->addWidget(addBtn, 1, 0, 1, 1);
1100
1101   horizontalSpacer = new QSpacerItem(188, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
1102
1103   gridLayout_4->addItem(horizontalSpacer, 1, 1, 1, 1);
1104
1105   logGroupBox = new QGroupBox(this);
1106   logGroupBox->setObjectName(QString("logGroupBox"));
1107   gridLayout_2 = new QGridLayout(logGroupBox);
1108   gridLayout_2->setObjectName(QString("gridLayout_2"));
1109   gridLayout = new QGridLayout();
1110   gridLayout->setObjectName(QString("gridLayout"));
1111   workingDirectoryLabel = new QLabel(logGroupBox);
1112   workingDirectoryLabel->setObjectName(QString("workingDirectoryLabel"));
1113
1114   gridLayout->addWidget(workingDirectoryLabel, 0, 0, 1, 1);
1115
1116   workingDirectoryLineEdit = new QLineEdit(logGroupBox);
1117   workingDirectoryLineEdit->setObjectName(QString("workingDirectoryLineEdit"));
1118
1119   gridLayout->addWidget(workingDirectoryLineEdit, 0, 1, 1, 1);
1120
1121   workingDirectoryPushButton = new QPushButton(logGroupBox);
1122   workingDirectoryPushButton->setObjectName(QString("workingDirectoryPushButton"));
1123
1124   gridLayout->addWidget(workingDirectoryPushButton, 0, 2, 1, 1);
1125
1126   verboseLevelLabel = new QLabel(logGroupBox);
1127   verboseLevelLabel->setObjectName(QString("verboseLevelLabel"));
1128
1129   gridLayout->addWidget(verboseLevelLabel, 1, 0, 1, 1);
1130
1131   verboseLevelSpin = new QSpinBox(logGroupBox);
1132   verboseLevelSpin->setObjectName(QString("verboseLevelSpin"));
1133
1134   gridLayout->addWidget(verboseLevelSpin, 1, 1, 1, 1);
1135
1136
1137   gridLayout_2->addLayout(gridLayout, 0, 0, 1, 1);
1138
1139   horizontalLayout = new QHBoxLayout();
1140   horizontalLayout->setObjectName(QString("horizontalLayout"));
1141   logInFileCheck = new QCheckBox(logGroupBox);
1142   logInFileCheck->setObjectName(QString("logInFileCheck"));
1143   logInFileCheck->setChecked(true);
1144
1145   horizontalLayout->addWidget(logInFileCheck);
1146
1147   removeLogOnSuccessCheck = new QCheckBox(logGroupBox);
1148   removeLogOnSuccessCheck->setObjectName(QString("removeLogOnSuccessCheck"));
1149   removeLogOnSuccessCheck->setChecked(true);
1150
1151   horizontalLayout->addWidget(removeLogOnSuccessCheck);
1152
1153
1154   gridLayout_2->addLayout(horizontalLayout, 1, 0, 1, 1);
1155
1156   keepWorkingFilesCheck = new QCheckBox(logGroupBox);
1157   keepWorkingFilesCheck->setObjectName(QString("keepWorkingFilesCheck"));
1158   keepWorkingFilesCheck->setAutoExclusive(false);
1159
1160   gridLayout_2->addWidget(keepWorkingFilesCheck, 2, 0, 1, 1);
1161
1162
1163   gridLayout_4->addWidget(logGroupBox, 3, 0, 1, 2);
1164
1165 }
1166 void MgAdaptAdvWidget::_onWorkingDirectoryPushButton()
1167 {
1168   QString aDirName=QFileDialog::getExistingDirectory ();
1169   if (!(aDirName.isEmpty()))workingDirectoryLineEdit->setText(aDirName);
1170 }
1171 void MgAdaptAdvWidget::onMeshDimChanged(ADAPTATION_MODE aMode)
1172 {
1173 /* default adaptation mode
1174   * assume that if meshDim == 2 and no 3D backgrounmesh-->adaptation surface
1175   * if meshDim == 3 and  if there is not 2D mesh -->VOLUME
1176   * else BOTH
1177   */
1178
1179   QString adaptation("adaptation"), value;
1180   switch(aMode)
1181   {
1182     case ADAPTATION_MODE::SURFACE:
1183     {
1184       value ="surface";
1185       setOptionValue(adaptation, value);
1186       break;
1187     }
1188     case ADAPTATION_MODE::BOTH :
1189     {
1190       value = "both";
1191       setOptionValue(adaptation, value);
1192       break;
1193     }
1194     case ADAPTATION_MODE::VOLUME :
1195     {
1196       value = "volume";
1197       setOptionValue(adaptation, value);
1198       break;
1199     }
1200   }
1201 }
1202 void MgAdaptAdvWidget::setOptionValue(QString& option, QString& value)
1203 {
1204   std::map<QString, QTreeWidgetItem *>::iterator it = optionTreeWidgetItem.find(option);
1205   if (it != optionTreeWidgetItem.end())
1206   {
1207     it->second->setText( 0, tr( option.toLatin1().constData() ));
1208     it->second->setText( 1, tr( value.toLatin1().constData() ));
1209     it->second->setCheckState( 0,  Qt::Checked );
1210     it->second->setData( NAME_COL, PARAM_NAME, option );
1211     myOptionTable->editItem( it->second, NAME_COL );
1212   }
1213 }
1214 namespace
1215 {
1216 bool isEditable( const QModelIndex& index )
1217 {
1218   return index.isValid() &&
1219           index.flags() & Qt::ItemIsEditable &&
1220           index.flags() & Qt::ItemIsEnabled &&
1221           ( !index.data( Qt::UserRole + 1 ).isValid() || index.data( Qt::UserRole + 1 ).toInt() != 0 );
1222 }
1223 }
1224
1225 MgAdaptAdvWidgetTreeWidget::MgAdaptAdvWidgetTreeWidget( QWidget* parent )
1226     : QTreeWidget( parent )
1227 {
1228 }
1229
1230 QModelIndex MgAdaptAdvWidgetTreeWidget::moveCursor( CursorAction action, Qt::KeyboardModifiers modifiers )
1231 {
1232   QModelIndex current = currentIndex();
1233   int column = current.column();
1234   if ( action == MoveNext )
1235   {
1236     if ( column < columnCount()-1 )
1237     {
1238       QModelIndex next = current.sibling( current.row(), column+1 );
1239       if ( isEditable( next ) ) return next;
1240     }
1241     else
1242     {
1243       QModelIndex next = current.sibling( current.row()+1, 0 );
1244       if ( isEditable( next ) ) return next;
1245     }
1246   }
1247   else if ( action == MovePrevious )
1248   {
1249     if ( column == 0 ) {
1250       QModelIndex next = current.sibling( current.row()-1, columnCount()-1 );
1251       if ( isEditable( next ) ) return next;
1252     }
1253     else {
1254       QModelIndex next = current.sibling( current.row(), column-1 );
1255        if ( isEditable( next ) ) return next;
1256     }
1257   }
1258   return QTreeWidget::moveCursor( action, modifiers );
1259 }
1260
1261 void MgAdaptAdvWidgetTreeWidget::keyPressEvent( QKeyEvent* e )
1262 {
1263   switch ( e->key() )
1264   {
1265     case Qt::Key_F2:
1266     {
1267       QModelIndex index = currentIndex();
1268       if ( !isEditable( index ) ) {
1269         for ( int i = 0; i < columnCount(); i++ ) {
1270           QModelIndex sibling = index.sibling( index.row(), i );
1271           if ( isEditable( sibling ) ) {
1272             if ( !edit( sibling, EditKeyPressed, e ) ) e->ignore();
1273           }
1274         }
1275       }
1276     }
1277     break;
1278     default:
1279       break;
1280   }
1281   QTreeWidget::keyPressEvent( e );
1282 }
1283
1284 // ======================================================
1285 // ========================================================
1286 QString lireNomMaillage(QString aFile, med_int& meshdim)
1287 {
1288   QString nomMaillage = QString::null ;
1289
1290   while ( true )
1291   {
1292     std::vector<std::string> listMeshesNames = MEDCoupling::GetMeshNames(aFile.toStdString());
1293
1294     std::size_t numberOfMeshes(listMeshesNames.size());
1295   //   std::cout << "numberOfMeshes:" << numberOfMeshes << std::endl;
1296     if (numberOfMeshes == 0 )
1297     {
1298       QMessageBox::critical( 0, QObject::tr("MG_ADAPT_ERROR"),
1299                                 QObject::tr("MG_ADAPT_MED_FILE_2") );
1300       break ;
1301     }
1302     if (numberOfMeshes > 1 )
1303     {
1304       QMessageBox::critical( 0, QObject::tr("MG_ADAPT_ERROR"),
1305                                 QObject::tr("MG_ADAPT_MED_FILE_3") );
1306       break ;
1307     }
1308
1309 //     std::cout << "nomMaillage:" << listMeshesNames[0] << std::endl;
1310     nomMaillage = QString(listMeshesNames[0].c_str());
1311
1312     // Dimension du maillage
1313     MEDCoupling::MCAuto<MEDCoupling::MEDFileData> mfd = MEDCoupling::MEDFileData::New(aFile.toStdString());
1314     meshdim = mfd->getMeshes()->getMeshAtPos(0)->getMeshDimension() ;
1315 //     std::cout << "meshdim:" << meshdim << std::endl;
1316
1317     break ;
1318   }
1319
1320   return nomMaillage;
1321 }
1322
1323 // =======================================================================
1324 std::map<QString, int> GetListeChamps(QString aFile, bool errorMessage)
1325 // =======================================================================
1326 {
1327 // Il faut voir si plusieurs maillages
1328
1329   std::map<QString, int> ListeChamp ;
1330
1331   while ( true )
1332   {
1333     MEDCoupling::MCAuto<MEDCoupling::MEDFileData> mfd = MEDCoupling::MEDFileData::New(aFile.toStdString());
1334     std::vector<std::string> listFieldsNames(mfd->getFields()->getFieldsNames());
1335     std::size_t jaux(listFieldsNames.size());
1336     if (jaux < 1 )
1337     {
1338       if(errorMessage)
1339       {
1340         QMessageBox::critical( 0, QObject::tr("_ERROR"),
1341                                   QObject::tr("HOM_MED_FILE_5") );
1342       }
1343       break ;
1344     }
1345     // nbofcstp inutile pour le moment
1346     med_int nbofcstp = 1;
1347     for(std::size_t j=0;j<jaux;j++)
1348     {
1349 //       std::cout << listFieldsNames[j] << std::endl;
1350       ListeChamp.insert(std::pair<QString, int> (QString(listFieldsNames[j].c_str()), nbofcstp));
1351     }
1352     break ;
1353   }
1354
1355   return ListeChamp;
1356 }
1357
1358 // =======================================================================
1359 std::string remove_extension(const std::string& filename)
1360 // =======================================================================
1361 {
1362   size_t lastdot = filename.find_last_of(".");
1363   if (lastdot == std::string::npos) return filename;
1364   return filename.substr(0, lastdot);
1365 }