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