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