Salome HOME
Merge branch 'gni/adaptation' of https://codev-tuleap.cea.fr/plugins/git/salome/smesh...
[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
601     // Connections
602     connect( meshInGroup, SIGNAL( buttonClicked( int ) ),  this, SLOT( modeChanged( int ) ) );
603     connect( sizeMapDefGroup, SIGNAL( buttonClicked( int ) ),  this, SLOT( sizeMapDefChanged( int ) ) );
604     connect( selectMedFilebutton, SIGNAL( pressed(  ) ),  this, SLOT( onSelectMedFilebuttonClicked(  ) ) );
605     connect(medFileCheckBox, SIGNAL (stateChanged(int)), this, SLOT(onMedFileCheckBox(int) ) );
606     connect(publishOut, SIGNAL (stateChanged(int)), this, SLOT(onPublishOut(int) ) );
607     connect(selectOutMedFilebutton, SIGNAL( pressed()), this, SLOT(onSelectOutMedFilebutton()));
608     connect(selectMedFileBackgroundbutton, SIGNAL(pressed()), this, SLOT(onSelectMedFileBackgroundbutton()) );
609     connect( timeStepGroup, SIGNAL( buttonClicked( int ) ),  this, SLOT( timeStepGroupChanged( int ) ) );
610     emit updateSelection();
611 }
612
613 //=================================================================================
614 // function : ~SMESHGUI_MgAdaptArguments()
615 // purpose  : Destroys the object and frees any allocated resources
616 //=================================================================================
617 SMESHGUI_MgAdaptArguments::~SMESHGUI_MgAdaptArguments()
618 {
619 }
620
621 void SMESHGUI_MgAdaptArguments::onNoTimeStep(bool disableOther)
622 {
623     noTimeStep->setChecked(true);
624
625     rankLabel->setVisible(0);
626     rankSpinBox->setVisible(0);
627     rankSpinBox->setValue(-2);
628
629     timeStepLabel->setVisible(0);
630     timeStep->setVisible(0);
631     timeStep->setValue(-2);
632
633     lastTimeStep->setDisabled(disableOther);
634     chosenTimeStep->setDisabled(disableOther);
635
636
637 }
638 void SMESHGUI_MgAdaptArguments::onLastTimeStep(bool disableOther)
639 {
640     lastTimeStep->setChecked(true);
641
642     rankLabel->setVisible(0);
643     rankSpinBox->setVisible(0);
644     rankSpinBox->setValue(-1);
645
646     timeStepLabel->setVisible(0);
647     timeStep->setVisible(0);
648     timeStep->setValue(-1);
649     noTimeStep->setDisabled(disableOther);
650 }
651
652 void SMESHGUI_MgAdaptArguments::onChosenTimeStep(bool disableOther, int max)
653 {
654     chosenTimeStep->setChecked(true);
655
656     rankLabel->setVisible(1);
657     rankSpinBox->setVisible(1);
658     rankSpinBox->setValue(0);
659
660     timeStepLabel->setVisible(1);
661     timeStep->setVisible(1);
662     timeStep->setValue(0);
663     if (max) timeStep->setMaximum(max);
664
665 }
666
667 void SMESHGUI_MgAdaptArguments::onSelectOutMedFilebutton()
668 {
669
670     QString filtre = QString("Med") ;
671     filtre += QString(" files (*.") + QString("med") + QString(");;");
672     QString fileName = QFileDialog::getSaveFileName(this, tr("SAVE_MED"),
673                        QString(""), filtre);
674     QFileInfo myFileInfo(fileName);
675     selectOutMedFileLineEdit->setText(myFileInfo.fileName());
676     *myFileOutDir = myFileInfo.path();
677
678 }
679 void SMESHGUI_MgAdaptArguments::onSelectMedFileBackgroundbutton()
680 {
681     QString fileName0 = selectMedFileBackgroundbutton->text().trimmed();
682
683     QString fileName = getMedFileName(false);
684     if (fileName != QString::null)
685     {
686         myFieldList = GetListeChamps(fileName);
687         if (myFieldList.empty())
688         {
689             fileName = fileName0;
690             fieldNameCmb->clear();
691         }
692         else
693         {
694             // fill field name Combobox
695             fieldNameCmb->clear();
696             std::map<QString, int>::const_iterator it;
697             for ( it=myFieldList.begin() ; it != myFieldList.end(); it++)
698             {
699                 fieldNameCmb->insertItem(0,QString(it->first));
700                 int typeStepInField = it->second > 2 ?  2 : it->second ;
701                 timeStepGroupChanged(typeStepInField, true);
702             }
703
704         }
705
706     }
707     else
708     {
709         fileName = fileName0;
710         fieldNameCmb->clear();
711     }
712
713     QFileInfo myFileInfo(fileName);
714     *myFileSizeMapDir = myFileInfo.path();
715     selectMedFileBackgroundLineEdit->setText(myFileInfo.fileName());
716
717 }
718 void SMESHGUI_MgAdaptArguments::onMedFileCheckBox(int state)
719 {
720     if (state == Qt::Checked)
721     {
722         selectOutMedFilebutton->show();
723         selectOutMedFileLineEdit->show();
724         selectOutMedFilebutton->setEnabled(true);
725         selectOutMedFileLineEdit->setEnabled(true);
726     }
727     else
728     {
729         selectOutMedFilebutton->setEnabled(false);
730         selectOutMedFileLineEdit->setEnabled(false);
731         publishOut->setChecked(true);
732     }
733 }
734 void SMESHGUI_MgAdaptArguments::onPublishOut(int state)
735 {
736     if (state == Qt::Unchecked)
737     {
738         medFileCheckBox->setChecked(true);
739     }
740 }
741
742 void SMESHGUI_MgAdaptArguments::onSelectMedFilebuttonClicked()
743 {
744     // bool keep = false;
745     QString fileName0 = selectMedFileLineEdit->text().trimmed();
746
747     QString fileName = getMedFileName(false);
748     if(fileName != QString::null)
749     {
750         QString aMeshName = lireNomMaillage(fileName.trimmed(), meshDim);
751         if (aMeshName == QString::null )
752         {
753             QMessageBox::critical( 0, QObject::tr("MG_ADAPT_ERROR"),
754                                    QObject::tr("MG_ADAPT_MED_FILE_2") );
755             fileName = fileName0;
756         }
757         else
758         {
759             meshNameLineEdit->setText(aMeshName);
760             ADAPTATION_MODE aMode = meshDim == 3 ? ADAPTATION_MODE::BOTH : ADAPTATION_MODE::SURFACE; // and when dimesh 3 without 2D mesh?
761             emit meshDimSignal(aMode);
762         }
763
764     }
765     else
766     {
767         return;
768     }
769
770
771     QFileInfo myFileInfo(fileName);
772     *myFileInDir = myFileInfo.path();
773     *myFileOutDir = myFileInfo.path();
774     selectMedFileLineEdit->setText(myFileInfo.fileName());
775     QString outF = fileName == QString::null ? myFileInfo.fileName() :
776     QString( remove_extension(myFileInfo.fileName().toStdString() ).c_str() )+ QString(".adapt.med");
777     selectOutMedFileLineEdit->setText(outF);
778     onLocalSelected(myFileInfo.filePath());
779
780 }
781
782 void SMESHGUI_MgAdaptArguments::onLocalSelected(QString filePath)
783 {
784     myFieldList = GetListeChamps(filePath, false);
785     if (myFieldList.empty())
786     {
787         if (localButton->isChecked())
788         {
789             fieldNameCmb->clear();
790         }
791
792     }
793     else
794     {
795         // fill field name Combobox
796         fieldNameCmb->clear();
797         std::map<QString, int>::const_iterator it;
798         for ( it = myFieldList.begin() ; it != myFieldList.end(); it++)
799         {
800             fieldNameCmb->insertItem(0,QString(it->first));
801             int typeStepInField = it->second > 2 ?  2 : it->second ;
802             timeStepGroupChanged(typeStepInField, true);
803         }
804
805     }
806 }
807 // =======================================================================
808 // Gestion les boutons qui permettent  de
809 // 1) retourne le nom d'un fichier par une fenetre de dialogue si aucun
810 //    objet est selectionne dans l arbre d etude
811 // 2) retourne le nom du fichier asocie a l objet
812 //    selectionne dans l arbre d etude
813 // =======================================================================
814 QString SMESHGUI_MgAdaptArguments::getMedFileName(bool avertir)
815 {
816
817     QString aFile = QString::null;
818     QString filtre = QString("Med") ;
819     filtre += QString(" files (*.") + QString("med") + QString(");;");
820     aFile = SUIT_FileDlg::getOpenFileName(0, QObject::tr("MG_ADAPT_SELECT_FILE_0"), QString(""), filtre );
821
822     return aFile;
823
824 }
825 void SMESHGUI_MgAdaptArguments::setMode(const Mode theMode, const SIZEMAP theSizeMap )
826 {
827     QRadioButton* aButton = qobject_cast<QRadioButton*>( meshInGroup->button( theMode ) );
828     QRadioButton* bButton = qobject_cast<QRadioButton*>( sizeMapDefGroup->button( theSizeMap ) );
829     if ( aButton ) {
830         aButton->setChecked( true );
831         modeChanged( theMode );
832     }
833     if ( bButton ) {
834         bButton->setChecked( true );
835         sizeMapDefChanged( theSizeMap );
836     }
837 }
838
839 void SMESHGUI_MgAdaptArguments::modeChanged( int theMode )
840 {
841     clear();
842     if(theMode == Mesh)
843     {
844         aBrowserObject->hide();
845         selectMedFileLineEdit->show();
846         selectMedFilebutton->show();
847         localButton->setEnabled(true);
848     }
849     else
850     {
851         selectMedFileLineEdit->hide();
852         selectMedFilebutton->hide();
853         localButton->setEnabled(false);
854         aBrowserObject->show();
855         sizeMapDefChanged(Background);
856         emit updateSelection();
857     }
858
859
860
861 }
862
863 void SMESHGUI_MgAdaptArguments::sizeMapDefChanged( int  theSizeMap )
864 {
865     fieldNameCmb->clear();
866     if(theSizeMap == Local)
867     {
868         localButton->setEnabled(true);
869         localButton->setChecked(true);
870         medFileBackground->hide();
871         selectMedFileBackgroundbutton->hide();
872         selectMedFileBackgroundLineEdit->hide();
873         selectMedFileBackgroundLineEdit->clear();
874         valueLabel->hide();
875         dvalue->hide();
876
877         sizeMapField->setEnabled(true);
878         if (!selectMedFileLineEdit->text().isEmpty())
879         {
880             QFileInfo myFileInfo(QDir(*myFileInDir), selectMedFileLineEdit->text());
881             onLocalSelected(myFileInfo.filePath());
882         }
883     }
884     else if (theSizeMap == Background)
885     {
886         medFileBackground->show();
887         backgroundButton->setChecked(true);
888         selectMedFileBackgroundbutton->show();
889         selectMedFileBackgroundLineEdit->show();
890         valueLabel->hide();
891         dvalue->hide();
892         sizeMapField->setEnabled(true);
893
894     }
895     else
896     {
897         medFileBackground->hide();
898         constantButton->setChecked(true);
899         selectMedFileBackgroundbutton->hide();
900         selectMedFileBackgroundLineEdit->clear();
901         selectMedFileBackgroundLineEdit->hide();
902         valueLabel->show();
903         dvalue->show();
904         sizeMapField->setEnabled(false);
905
906     }
907
908
909 }
910 void SMESHGUI_MgAdaptArguments::timeStepGroupChanged(int timeStepType, bool disableOther, int max)
911 {
912     switch (timeStepType)
913     {
914     case 0 :
915         onNoTimeStep(disableOther);
916         break;
917     case 1 :
918         onLastTimeStep(disableOther);
919         break;
920     case 2 :
921         onChosenTimeStep(disableOther, max);
922     default:
923         break;
924     }
925 }
926
927 void SMESHGUI_MgAdaptArguments::clear()
928 {
929     selectMedFileLineEdit->clear();
930     aBrowserObject->clear();
931
932     meshNameLineEdit->clear();
933     selectOutMedFileLineEdit->clear();
934 }
935 med_int SMESHGUI_MgAdaptArguments::getMeshDim() const
936 {
937         return meshDim;
938 }
939 QWidget* ItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &o, const QModelIndex &index) const
940 {
941     bool editable = index.data( EDITABLE_ROLE ).toInt();
942     return editable ? QItemDelegate::createEditor( parent, o, index ) : 0;
943 }
944
945 //////////////////////////////////////////
946 // MgAdaptAdvWidget
947 //////////////////////////////////////////
948
949 MgAdaptAdvWidget::MgAdaptAdvWidget( QWidget* parent, std::vector <std::string>* options, Qt::WindowFlags f )
950     : QWidget( parent, f ), myOptions(options)
951 {
952     setupWidget();
953     myOptionTable->header()->setSectionResizeMode( QHeaderView::ResizeToContents );
954     myOptionTable->setItemDelegate( new ItemDelegate( myOptionTable ) );
955
956     for ( int i = 0, nb = myOptions->size(); i < nb; ++i )
957     {
958         AddOption( (*myOptions)[i].c_str() );
959     }
960
961     connect( myOptionTable, SIGNAL( itemChanged(QTreeWidgetItem *, int)), SLOT( itemChanged(QTreeWidgetItem *, int )));
962     connect( addBtn,                     SIGNAL( clicked() ),       this, SLOT( onAddOption() ) );
963     connect(workingDirectoryPushButton, SIGNAL(pressed()),  this, SLOT(_onWorkingDirectoryPushButton()));
964 }
965
966 MgAdaptAdvWidget::~MgAdaptAdvWidget()
967 {
968 }
969
970 void MgAdaptAdvWidget::AddOption( const char* option, bool isCustom )
971 {
972
973
974     QString name, value;
975     bool isDefault = false;
976     if ( option )
977     {
978         QStringList name_value_type = QString(option).split( ":", QString::KeepEmptyParts );
979         if ( name_value_type.size() > 0 )
980             name = name_value_type[0];
981         if ( name_value_type.size() > 1 )
982             value = name_value_type[1];
983         if ( name_value_type.size() > 2 )
984             isDefault = !name_value_type[2].toInt();
985
986     }
987         QTreeWidget* table = myOptionTable;
988     //table->setExpanded( true );
989
990     QTreeWidgetItem* row;
991     if (optionTreeWidgetItem.size())
992     {
993                 std::map<QString, QTreeWidgetItem *>::iterator it = optionTreeWidgetItem.find(name);
994                 if(it != optionTreeWidgetItem.end()) return; // option exist
995                 else
996                 {
997                         row = getNewQTreeWidgetItem(table, option, name, isCustom); 
998                 } 
999         }
1000         else 
1001         {
1002                 row = getNewQTreeWidgetItem(table, option, name, isCustom); 
1003         }
1004     row->setText( 0, tr( name.toLatin1().constData() ));
1005     row->setText( 1, tr( value.toLatin1().constData() ));
1006     row->setCheckState( 0, isDefault ? Qt::Unchecked : Qt::Checked);
1007     row->setData( NAME_COL, PARAM_NAME, name );
1008
1009     if ( isCustom )
1010     {
1011         myOptionTable->scrollToItem( row );
1012         myOptionTable->setCurrentItem( row );
1013         myOptionTable->editItem( row, NAME_COL );
1014     }
1015 }
1016
1017 QTreeWidgetItem* MgAdaptAdvWidget::getNewQTreeWidgetItem(QTreeWidget* table, const char* option, QString& name, bool isCustom)
1018 {
1019         QTreeWidgetItem* row = new QTreeWidgetItem( table );
1020         row->setData( NAME_COL, EDITABLE_ROLE, int( isCustom && !option ));
1021         row->setFlags( row->flags() | Qt::ItemIsEditable );
1022         optionTreeWidgetItem.insert(std::pair <QString, QTreeWidgetItem*> (name, row)); 
1023         
1024         return row;
1025 }
1026
1027 void MgAdaptAdvWidget::onAddOption()
1028 {
1029     AddOption( NULL, true );
1030 }
1031 void MgAdaptAdvWidget::GetOptionAndValue( QTreeWidgetItem * tblRow,
1032         QString&          option,
1033         QString&          value,
1034         bool&             isDefault)
1035 {
1036     option    = tblRow->data( NAME_COL, PARAM_NAME ).toString();
1037     value     = tblRow->text( VALUE_COL );
1038     isDefault = ! tblRow->checkState( NAME_COL );
1039
1040 }
1041
1042
1043 void MgAdaptAdvWidget::itemChanged(QTreeWidgetItem* tblRow, int column)
1044 {
1045     if ( tblRow )
1046     {
1047         myOptionTable->blockSignals( true );
1048
1049         tblRow->setData( VALUE_COL, EDITABLE_ROLE, int( tblRow->checkState( NAME_COL )));
1050
1051         int c = tblRow->checkState( NAME_COL ) ? 0 : 150;
1052         tblRow->setForeground( VALUE_COL, QBrush( QColor( c, c, c )));
1053
1054         if ( column == NAME_COL && tblRow->data( NAME_COL, EDITABLE_ROLE ).toInt() ) // custom table
1055         {
1056             tblRow->setData( NAME_COL, PARAM_NAME, tblRow->text( NAME_COL ));
1057         }
1058
1059         myOptionTable->blockSignals( false );
1060     }
1061 }
1062 void MgAdaptAdvWidget::setupWidget()
1063 {
1064     if (this->objectName().isEmpty())
1065         this->setObjectName(QString(tr("MG-ADAPT-ADV")));
1066     this->resize(337, 369);
1067     gridLayout_4 = new QGridLayout(this);
1068     gridLayout_4->setObjectName(QString("gridLayout_4"));
1069     myOptionTable = new MgAdaptAdvWidgetTreeWidget(this);
1070     QFont font;
1071     font.setBold(false);
1072     font.setWeight(50);
1073     QTreeWidgetItem *__qtreewidgetitem = new QTreeWidgetItem();
1074     __qtreewidgetitem->setFont(1, font);
1075     __qtreewidgetitem->setFont(0, font);
1076     __qtreewidgetitem->setText(1, tr("OPTION_VALUE_COLUMN"));
1077     __qtreewidgetitem->setText(0, tr("OPTION_NAME_COLUMN"));
1078     myOptionTable->setHeaderItem(__qtreewidgetitem);
1079     myOptionTable->setObjectName(QString("myOptionTable"));
1080     myOptionTable->setEditTriggers(QAbstractItemView::DoubleClicked|QAbstractItemView::EditKeyPressed);
1081     myOptionTable->setTabKeyNavigation(true);
1082
1083     gridLayout_4->addWidget(myOptionTable, 0, 0, 1, 2);
1084
1085     addBtn = new QPushButton(this);
1086     addBtn->setObjectName(QString("addBtn"));
1087
1088     gridLayout_4->addWidget(addBtn, 1, 0, 1, 1);
1089
1090     horizontalSpacer = new QSpacerItem(188, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
1091
1092     gridLayout_4->addItem(horizontalSpacer, 1, 1, 1, 1);
1093
1094     logGroupBox = new QGroupBox(this);
1095     logGroupBox->setObjectName(QString("logGroupBox"));
1096     gridLayout_2 = new QGridLayout(logGroupBox);
1097     gridLayout_2->setObjectName(QString("gridLayout_2"));
1098     gridLayout = new QGridLayout();
1099     gridLayout->setObjectName(QString("gridLayout"));
1100     workingDirectoryLabel = new QLabel(logGroupBox);
1101     workingDirectoryLabel->setObjectName(QString("workingDirectoryLabel"));
1102
1103     gridLayout->addWidget(workingDirectoryLabel, 0, 0, 1, 1);
1104
1105     workingDirectoryLineEdit = new QLineEdit(logGroupBox);
1106     workingDirectoryLineEdit->setObjectName(QString("workingDirectoryLineEdit"));
1107
1108     gridLayout->addWidget(workingDirectoryLineEdit, 0, 1, 1, 1);
1109
1110     workingDirectoryPushButton = new QPushButton(logGroupBox);
1111     workingDirectoryPushButton->setObjectName(QString("workingDirectoryPushButton"));
1112
1113     gridLayout->addWidget(workingDirectoryPushButton, 0, 2, 1, 1);
1114
1115     verboseLevelLabel = new QLabel(logGroupBox);
1116     verboseLevelLabel->setObjectName(QString("verboseLevelLabel"));
1117
1118     gridLayout->addWidget(verboseLevelLabel, 1, 0, 1, 1);
1119
1120     verboseLevelSpin = new QSpinBox(logGroupBox);
1121     verboseLevelSpin->setObjectName(QString("verboseLevelSpin"));
1122
1123     gridLayout->addWidget(verboseLevelSpin, 1, 1, 1, 1);
1124
1125
1126     gridLayout_2->addLayout(gridLayout, 0, 0, 1, 1);
1127
1128     horizontalLayout = new QHBoxLayout();
1129     horizontalLayout->setObjectName(QString("horizontalLayout"));
1130     logInFileCheck = new QCheckBox(logGroupBox);
1131     logInFileCheck->setObjectName(QString("logInFileCheck"));
1132     logInFileCheck->setChecked(true);
1133
1134     horizontalLayout->addWidget(logInFileCheck);
1135
1136     removeLogOnSuccessCheck = new QCheckBox(logGroupBox);
1137     removeLogOnSuccessCheck->setObjectName(QString("removeLogOnSuccessCheck"));
1138     removeLogOnSuccessCheck->setChecked(true);
1139
1140     horizontalLayout->addWidget(removeLogOnSuccessCheck);
1141
1142
1143     gridLayout_2->addLayout(horizontalLayout, 1, 0, 1, 1);
1144
1145     keepWorkingFilesCheck = new QCheckBox(logGroupBox);
1146     keepWorkingFilesCheck->setObjectName(QString("keepWorkingFilesCheck"));
1147     keepWorkingFilesCheck->setAutoExclusive(false);
1148
1149     gridLayout_2->addWidget(keepWorkingFilesCheck, 2, 0, 1, 1);
1150
1151
1152     gridLayout_4->addWidget(logGroupBox, 3, 0, 1, 2);
1153
1154
1155     // retranslateUi(this);
1156
1157     // QMetaObject::connectSlotsByName(this);
1158
1159 }
1160 void MgAdaptAdvWidget::_onWorkingDirectoryPushButton()
1161 {
1162     QString aDirName=QFileDialog::getExistingDirectory ();
1163     if (!(aDirName.isEmpty()))workingDirectoryLineEdit->setText(aDirName);
1164 }
1165 void MgAdaptAdvWidget::onMeshDimChanged(ADAPTATION_MODE aMode)
1166 {
1167         /* default adaptation mode
1168          * assume that if meshDim == 2 -->adaptation surface
1169          * if meshDim == 3 and  if there is not 2D mesh -->VOLUME
1170          * else BOTH 
1171          */
1172          
1173          QString adaptation("adaptation"), value;
1174          switch(aMode)
1175          {
1176          case ADAPTATION_MODE::SURFACE:
1177          {      
1178              value ="surface";           
1179              setOptionValue(adaptation, value);
1180              break;
1181          }
1182          case ADAPTATION_MODE::BOTH :
1183          {
1184                  value = "both";         
1185                  setOptionValue(adaptation, value);
1186                  break;
1187          }
1188          case ADAPTATION_MODE::VOLUME :
1189          {
1190                  value = "volume"; 
1191                  setOptionValue(adaptation, value);
1192                  break;
1193          }
1194          } 
1195 }
1196 void MgAdaptAdvWidget::setOptionValue(QString& option, QString& value)
1197 {        
1198
1199         std::map<QString, QTreeWidgetItem *>::iterator it = optionTreeWidgetItem.find(option);
1200         if (it != optionTreeWidgetItem.end())
1201         {
1202                 it->second->setText( 0, tr( option.toLatin1().constData() ));
1203                 it->second->setText( 1, tr( value.toLatin1().constData() ));
1204                 it->second->setCheckState( 0,  Qt::Checked );
1205                 it->second->setData( NAME_COL, PARAM_NAME, option );
1206         myOptionTable->editItem( it->second, NAME_COL );
1207         }
1208 }
1209 namespace
1210 {
1211 bool isEditable( const QModelIndex& index )
1212 {
1213     return index.isValid() &&
1214            index.flags() & Qt::ItemIsEditable &&
1215            index.flags() & Qt::ItemIsEnabled &&
1216            ( !index.data( Qt::UserRole + 1 ).isValid() || index.data( Qt::UserRole + 1 ).toInt() != 0 );
1217 }
1218 }
1219
1220 MgAdaptAdvWidgetTreeWidget::MgAdaptAdvWidgetTreeWidget( QWidget* parent )
1221     : QTreeWidget( parent )
1222 {
1223 }
1224
1225 QModelIndex MgAdaptAdvWidgetTreeWidget::moveCursor( CursorAction action, Qt::KeyboardModifiers modifiers )
1226 {
1227     QModelIndex current = currentIndex();
1228     int column = current.column();
1229     if ( action == MoveNext ) {
1230         if ( column < columnCount()-1 ) {
1231             QModelIndex next = current.sibling( current.row(), column+1 );
1232             if ( isEditable( next ) )
1233                 return next;
1234         }
1235         else {
1236             QModelIndex next = current.sibling( current.row()+1, 0 );
1237             if ( isEditable( next ) )
1238                 return next;
1239         }
1240     }
1241     else if ( action == MovePrevious ) {
1242         if ( column == 0 ) {
1243             QModelIndex next = current.sibling( current.row()-1, columnCount()-1 );
1244             if ( isEditable( next ) )
1245                 return next;
1246         }
1247         else {
1248             QModelIndex next = current.sibling( current.row(), column-1 );
1249             if ( isEditable( next ) )
1250                 return next;
1251         }
1252     }
1253     return QTreeWidget::moveCursor( action, modifiers );
1254 }
1255
1256 void MgAdaptAdvWidgetTreeWidget::keyPressEvent( QKeyEvent* e )
1257 {
1258     switch ( e->key() ) {
1259     case Qt::Key_F2:
1260     {
1261         QModelIndex index = currentIndex();
1262         if ( !isEditable( index ) ) {
1263             for ( int i = 0; i < columnCount(); i++ ) {
1264                 QModelIndex sibling = index.sibling( index.row(), i );
1265                 if ( isEditable( sibling ) ) {
1266                     if ( !edit( sibling, EditKeyPressed, e ) )
1267                         e->ignore();
1268                 }
1269             }
1270         }
1271     }
1272     break;
1273     default:
1274         break;
1275     }
1276     QTreeWidget::keyPressEvent( e );
1277 }
1278
1279
1280 // =======================================================================
1281 // renvoie le medId associe au fichier Med apres ouverture
1282 // =======================================================================
1283 med_idt OuvrirFichier(QString aFile)
1284 {
1285     med_idt medIdt = MEDfileOpen(aFile.toStdString().c_str(),MED_ACC_RDONLY);
1286     if (medIdt <0)
1287     {
1288         QMessageBox::critical( 0, QObject::tr("MG_ADAPT_ERROR"),
1289                                QObject::tr("MG_ADAPT_MED_FILE_1") );
1290     }
1291     return medIdt;
1292 }
1293
1294 // ======================================================
1295 // ========================================================
1296 QString lireNomMaillage(QString aFile, med_int& meshdim)
1297 {
1298     QString nomMaillage = QString::null ;
1299     int erreur = 0 ;
1300     med_idt medIdt ;
1301
1302     //  Ouverture du fichier
1303     medIdt = OuvrirFichier(aFile);
1304     if ( medIdt < 0 )
1305     {
1306         erreur = 1 ;
1307         return nomMaillage;
1308     }
1309     med_int numberOfMeshes = MEDnMesh(medIdt) ;
1310     if (numberOfMeshes == 0 )
1311     {
1312         QMessageBox::critical( 0, QObject::tr("MG_ADAPT_ERROR"),
1313                                QObject::tr("MG_ADAPT_MED_FILE_2") );
1314         erreur = 2 ;
1315         return nomMaillage;
1316     }
1317     if (numberOfMeshes > 1 )
1318     {
1319         QMessageBox::critical( 0, QObject::tr("MG_ADAPT_ERROR"),
1320                                QObject::tr("MG_ADAPT_MED_FILE_3") );
1321         erreur = 3 ;
1322         return nomMaillage;
1323     }
1324
1325     nomMaillage = lireNomMaillage2(medIdt,1, meshdim);
1326     // Fermeture du fichier
1327     if ( medIdt > 0 ) MEDfileClose(medIdt);
1328
1329     return nomMaillage;
1330 }
1331
1332 // =======================================================================
1333 // =======================================================================
1334 QString lireNomMaillage2(med_idt medIdt,int meshId, med_int& meshdim )
1335 {
1336     QString NomMaillage=QString::null;
1337     char meshname[MED_NAME_SIZE+1];
1338     med_int spacedim;
1339     med_mesh_type meshtype;
1340     char descriptionription[MED_COMMENT_SIZE+1];
1341     char dtunit[MED_SNAME_SIZE+1];
1342     med_sorting_type sortingtype;
1343     med_int nstep;
1344     med_axis_type axistype;
1345     int naxis = MEDmeshnAxis(medIdt,1);
1346     char *axisname=new char[naxis*MED_SNAME_SIZE+1];
1347     char *axisunit=new char[naxis*MED_SNAME_SIZE+1];
1348     med_err aRet = MEDmeshInfo(medIdt,
1349                                meshId,
1350                                meshname,
1351                                &spacedim,
1352                                &meshdim,
1353                                &meshtype,
1354                                descriptionription,
1355                                dtunit,
1356                                &sortingtype,
1357                                &nstep,
1358                                &axistype,
1359                                axisname,
1360                                axisunit);
1361
1362     if ( aRet < 0 ) {
1363         QMessageBox::critical( 0, QObject::tr("MG_ADAPT_ERROR"), \
1364                                QObject::tr("MG_ADAPT_MED_FILE_4") );
1365     }
1366     else            {
1367         NomMaillage=QString(meshname);
1368     }
1369
1370     delete[] axisname ;
1371     delete[] axisunit ;
1372
1373     return NomMaillage;
1374 }
1375
1376
1377
1378 // =======================================================================
1379 std::map<QString, int> GetListeChamps(QString aFile, bool errorMessage)
1380 // =======================================================================
1381 {
1382 // Il faut voir si plusieurs maillages
1383
1384     MESSAGE("GetListeChamps");
1385     std::map<QString, int> ListeChamp ;
1386
1387     med_err erreur = 0 ;
1388     med_idt medIdt ;
1389
1390     while ( erreur == 0 )
1391     {
1392         // Ouverture du fichier
1393         SCRUTE(aFile.toStdString());
1394         medIdt = OuvrirFichier(aFile);
1395         if ( medIdt < 0 )
1396         {
1397             erreur = 1 ;
1398             break ;
1399         }
1400         // Lecture du nombre de champs
1401         med_int ncha = MEDnField(medIdt) ;
1402         if (ncha < 1 )
1403         {
1404             if(errorMessage)
1405             {
1406                 QMessageBox::critical( 0, QObject::tr("_ERROR"),
1407                                        QObject::tr("HOM_MED_FILE_5") );
1408             }
1409             erreur = 2 ;
1410             break ;
1411         }
1412         // Lecture des caracteristiques des champs
1413         for (int i=0; i< ncha; i++)
1414         {
1415 //       Lecture du nombre de composantes
1416             med_int ncomp = MEDfieldnComponent(medIdt,i+1);
1417 //       Lecture du type du champ, des noms des composantes et du nom de l'unite
1418             char nomcha  [MED_NAME_SIZE+1];
1419             char meshname[MED_NAME_SIZE+1];
1420             char * comp = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
1421             char * unit = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
1422             char dtunit[MED_SNAME_SIZE+1];
1423             med_bool local;
1424             med_field_type typcha;
1425             med_int nbofcstp;
1426             erreur = MEDfieldInfo(medIdt,i+1,nomcha,meshname,&local,&typcha,comp,unit,dtunit,&nbofcstp) ;
1427             free(comp);
1428             free(unit);
1429             if ( erreur < 0 )
1430             {
1431                 if(errorMessage)
1432                 {
1433                     QMessageBox::critical( 0, QObject::tr("MG_ADAPT_ERROR"),
1434                                            QObject::tr("MG_ADAPT_MED_FILE_6") );
1435                 }
1436                 break ;
1437             }
1438
1439             ListeChamp.insert(std::pair<QString, int> (QString(nomcha), nbofcstp));
1440
1441         }
1442         break ;
1443     }
1444     // Fermeture du fichier
1445     if ( medIdt > 0 ) MEDfileClose(medIdt);
1446
1447     return ListeChamp;
1448 }
1449
1450 std::string remove_extension(const std::string& filename) {
1451     size_t lastdot = filename.find_last_of(".");
1452     if (lastdot == std::string::npos) return filename;
1453     return filename.substr(0, lastdot); 
1454 }
1455
1456