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