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