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