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