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