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