Salome HOME
restoring fam with old id
[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     selectOutMedFileLineEdit->setText(myFileInfo.fileName());
768     onLocalSelected(myFileInfo.filePath());
769
770 }
771
772 void SMESHGUI_MgAdaptArguments::onLocalSelected(QString filePath)
773 {
774     myFieldList = GetListeChamps(filePath, false);
775     if (myFieldList.empty())
776     {
777         if (localButton->isChecked())
778         {
779             fieldNameCmb->clear();
780         }
781
782     }
783     else
784     {
785         // fill field name Combobox
786         fieldNameCmb->clear();
787         std::map<QString, int>::const_iterator it;
788         for ( it=myFieldList.begin() ; it != myFieldList.end(); it++)
789         {
790             fieldNameCmb->insertItem(0,QString(it->first));
791             int typeStepInField = it->second > 2 ?  2 : it->second ;
792             timeStepGroupChanged(typeStepInField, true, it->second-1);
793         }
794
795     }
796 }
797 // =======================================================================
798 // Gestion les boutons qui permettent  de
799 // 1) retourne le nom d'un fichier par une fenetre de dialogue si aucun
800 //    objet est selectionne dans l arbre d etude
801 // 2) retourne le nom du fichier asocie a l objet
802 //    selectionne dans l arbre d etude
803 // =======================================================================
804 QString SMESHGUI_MgAdaptArguments::getMedFileName(bool avertir)
805 {
806
807     QString aFile = QString::null;
808     QString filtre = QString("Med") ;
809     filtre += QString(" files (*.") + QString("med") + QString(");;");
810     aFile = SUIT_FileDlg::getOpenFileName(0, QObject::tr("MG_ADAPT_SELECT_FILE_0"), QString(""), filtre );
811
812     return aFile;
813
814 }
815 void SMESHGUI_MgAdaptArguments::setMode(const Mode theMode, const SIZEMAP theSizeMap )
816 {
817     QRadioButton* aButton = qobject_cast<QRadioButton*>( meshInGroup->button( theMode ) );
818     QRadioButton* bButton = qobject_cast<QRadioButton*>( sizeMapDefGroup->button( theSizeMap ) );
819     if ( aButton ) {
820         aButton->setChecked( true );
821         modeChanged( theMode );
822     }
823     if ( bButton ) {
824         bButton->setChecked( true );
825         sizeMapDefChanged( theSizeMap );
826     }
827 }
828
829 void SMESHGUI_MgAdaptArguments::modeChanged( int theMode )
830 {
831     clear();
832     if(theMode == Mesh)
833     {
834         aBrowserObject->hide();
835         selectMedFileLineEdit->show();
836         selectMedFilebutton->show();
837         localButton->setEnabled(true);
838     }
839     else
840     {
841         selectMedFileLineEdit->hide();
842         selectMedFilebutton->hide();
843         localButton->setEnabled(false);
844         aBrowserObject->show();
845         sizeMapDefChanged(Background);
846         emit updateSelection();
847     }
848
849
850
851 }
852
853 void SMESHGUI_MgAdaptArguments::sizeMapDefChanged( int  theSizeMap )
854 {
855     fieldNameCmb->clear();
856     if(theSizeMap == Local)
857     {
858         localButton->setEnabled(true);
859         localButton->setChecked(true);
860         medFileBackground->hide();
861         selectMedFileBackgroundbutton->hide();
862         selectMedFileBackgroundLineEdit->hide();
863         selectMedFileBackgroundLineEdit->clear();
864         valueLabel->hide();
865         dvalue->hide();
866
867         sizeMapField->setEnabled(true);
868         if (!selectMedFileLineEdit->text().isEmpty())
869         {
870             QFileInfo myFileInfo(QDir(*myFileInDir), selectMedFileLineEdit->text());
871             onLocalSelected(myFileInfo.filePath());
872         }
873     }
874     else if (theSizeMap == Background)
875     {
876         medFileBackground->show();
877         backgroundButton->setChecked(true);
878         selectMedFileBackgroundbutton->show();
879         selectMedFileBackgroundLineEdit->show();
880         valueLabel->hide();
881         dvalue->hide();
882         sizeMapField->setEnabled(true);
883
884     }
885     else
886     {
887         medFileBackground->hide();
888         constantButton->setChecked(true);
889         selectMedFileBackgroundbutton->hide();
890         selectMedFileBackgroundLineEdit->clear();
891         selectMedFileBackgroundLineEdit->hide();
892         valueLabel->show();
893         dvalue->show();
894         sizeMapField->setEnabled(false);
895
896     }
897
898
899 }
900 void SMESHGUI_MgAdaptArguments::timeStepGroupChanged(int timeStepType, bool disableOther, int max)
901 {
902     switch (timeStepType)
903     {
904     case 0 :
905         onNoTimeStep(disableOther);
906         break;
907     case 1 :
908         onLastTimeStep(disableOther);
909         break;
910     case 2 :
911         onChosenTimeStep(disableOther, max);
912     default:
913         break;
914     }
915 }
916
917 void SMESHGUI_MgAdaptArguments::clear()
918 {
919     selectMedFileLineEdit->clear();
920     aBrowserObject->clear();
921
922     meshNameLineEdit->clear();
923     selectOutMedFileLineEdit->clear();
924 }
925
926 QWidget* ItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &o, const QModelIndex &index) const
927 {
928     bool editable = index.data( EDITABLE_ROLE ).toInt();
929     return editable ? QItemDelegate::createEditor( parent, o, index ) : 0;
930 }
931
932 //////////////////////////////////////////
933 // MgAdaptAdvWidget
934 //////////////////////////////////////////
935
936 MgAdaptAdvWidget::MgAdaptAdvWidget( QWidget* parent, std::vector <std::string>* options, Qt::WindowFlags f )
937     : QWidget( parent, f ), myOptions(options)
938 {
939     setupWidget();
940     myOptionTable->header()->setSectionResizeMode( QHeaderView::ResizeToContents );
941     myOptionTable->setItemDelegate( new ItemDelegate( myOptionTable ) );
942
943     for ( int i = 0, nb = myOptions->size(); i < nb; ++i )
944     {
945         AddOption( (*myOptions)[i].c_str() );
946     }
947
948     connect( myOptionTable, SIGNAL( itemChanged(QTreeWidgetItem *, int)), SLOT( itemChanged(QTreeWidgetItem *, int )));
949     connect( addBtn,                     SIGNAL( clicked() ),       this, SLOT( onAddOption() ) );
950     connect(workingDirectoryPushButton, SIGNAL(pressed()),  this, SLOT(_onWorkingDirectoryPushButton()));
951 }
952
953 MgAdaptAdvWidget::~MgAdaptAdvWidget()
954 {
955 }
956
957 void MgAdaptAdvWidget::AddOption( const char* option, bool isCustom )
958 {
959     QTreeWidget * table = myOptionTable;
960     //table->setExpanded( true );
961
962     QTreeWidgetItem * row = new QTreeWidgetItem( table );
963     row->setData( NAME_COL, EDITABLE_ROLE, int( isCustom && !option ));
964     row->setFlags( row->flags() | Qt::ItemIsEditable );
965
966     QString name, value;
967     bool isDefault = false;
968     if ( option )
969     {
970         QStringList name_value_type = QString(option).split( ":", QString::KeepEmptyParts );
971         if ( name_value_type.size() > 0 )
972             name = name_value_type[0];
973         if ( name_value_type.size() > 1 )
974             value = name_value_type[1];
975         if ( name_value_type.size() > 2 )
976             isDefault = !name_value_type[2].toInt();
977
978     }
979     row->setText( 0, tr( name.toLatin1().constData() ));
980     row->setText( 1, tr( value.toLatin1().constData() ));
981     row->setCheckState( 0, isDefault ? Qt::Unchecked : Qt::Checked);
982     row->setData( NAME_COL, PARAM_NAME, name );
983
984     if ( isCustom )
985     {
986         myOptionTable->scrollToItem( row );
987         myOptionTable->setCurrentItem( row );
988         myOptionTable->editItem( row, NAME_COL );
989     }
990 }
991 void MgAdaptAdvWidget::onAddOption()
992 {
993     AddOption( NULL, true );
994 }
995 void MgAdaptAdvWidget::GetOptionAndValue( QTreeWidgetItem * tblRow,
996         QString&          option,
997         QString&          value,
998         bool&             isDefault)
999 {
1000     option    = tblRow->data( NAME_COL, PARAM_NAME ).toString();
1001     value     = tblRow->text( VALUE_COL );
1002     isDefault = ! tblRow->checkState( NAME_COL );
1003
1004 }
1005
1006
1007 void MgAdaptAdvWidget::itemChanged(QTreeWidgetItem * tblRow, int column)
1008 {
1009     if ( tblRow )
1010     {
1011         myOptionTable->blockSignals( true );
1012
1013         tblRow->setData( VALUE_COL, EDITABLE_ROLE, int( tblRow->checkState( NAME_COL )));
1014
1015         int c = tblRow->checkState( NAME_COL ) ? 0 : 150;
1016         tblRow->setForeground( VALUE_COL, QBrush( QColor( c, c, c )));
1017
1018         if ( column == NAME_COL && tblRow->data( NAME_COL, EDITABLE_ROLE ).toInt() ) // custom table
1019         {
1020             tblRow->setData( NAME_COL, PARAM_NAME, tblRow->text( NAME_COL ));
1021         }
1022
1023         myOptionTable->blockSignals( false );
1024     }
1025 }
1026 void MgAdaptAdvWidget::setupWidget()
1027 {
1028     if (this->objectName().isEmpty())
1029         this->setObjectName(QString(tr("MG-ADAPT-ADV")));
1030     this->resize(337, 369);
1031     gridLayout_4 = new QGridLayout(this);
1032     gridLayout_4->setObjectName(QString("gridLayout_4"));
1033     myOptionTable = new MgAdaptAdvWidgetTreeWidget(this);
1034     QFont font;
1035     font.setBold(false);
1036     font.setWeight(50);
1037     QTreeWidgetItem *__qtreewidgetitem = new QTreeWidgetItem();
1038     __qtreewidgetitem->setFont(1, font);
1039     __qtreewidgetitem->setFont(0, font);
1040     __qtreewidgetitem->setText(1, tr("OPTION_VALUE_COLUMN"));
1041     __qtreewidgetitem->setText(0, tr("OPTION_NAME_COLUMN"));
1042     myOptionTable->setHeaderItem(__qtreewidgetitem);
1043     myOptionTable->setObjectName(QString("myOptionTable"));
1044     myOptionTable->setEditTriggers(QAbstractItemView::DoubleClicked|QAbstractItemView::EditKeyPressed);
1045     myOptionTable->setTabKeyNavigation(true);
1046
1047     gridLayout_4->addWidget(myOptionTable, 0, 0, 1, 2);
1048
1049     addBtn = new QPushButton(this);
1050     addBtn->setObjectName(QString("addBtn"));
1051
1052     gridLayout_4->addWidget(addBtn, 1, 0, 1, 1);
1053
1054     horizontalSpacer = new QSpacerItem(188, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
1055
1056     gridLayout_4->addItem(horizontalSpacer, 1, 1, 1, 1);
1057
1058     logGroupBox = new QGroupBox(this);
1059     logGroupBox->setObjectName(QString("logGroupBox"));
1060     gridLayout_2 = new QGridLayout(logGroupBox);
1061     gridLayout_2->setObjectName(QString("gridLayout_2"));
1062     gridLayout = new QGridLayout();
1063     gridLayout->setObjectName(QString("gridLayout"));
1064     workingDirectoryLabel = new QLabel(logGroupBox);
1065     workingDirectoryLabel->setObjectName(QString("workingDirectoryLabel"));
1066
1067     gridLayout->addWidget(workingDirectoryLabel, 0, 0, 1, 1);
1068
1069     workingDirectoryLineEdit = new QLineEdit(logGroupBox);
1070     workingDirectoryLineEdit->setObjectName(QString("workingDirectoryLineEdit"));
1071
1072     gridLayout->addWidget(workingDirectoryLineEdit, 0, 1, 1, 1);
1073
1074     workingDirectoryPushButton = new QPushButton(logGroupBox);
1075     workingDirectoryPushButton->setObjectName(QString("workingDirectoryPushButton"));
1076
1077     gridLayout->addWidget(workingDirectoryPushButton, 0, 2, 1, 1);
1078
1079     verboseLevelLabel = new QLabel(logGroupBox);
1080     verboseLevelLabel->setObjectName(QString("verboseLevelLabel"));
1081
1082     gridLayout->addWidget(verboseLevelLabel, 1, 0, 1, 1);
1083
1084     verboseLevelSpin = new QSpinBox(logGroupBox);
1085     verboseLevelSpin->setObjectName(QString("verboseLevelSpin"));
1086
1087     gridLayout->addWidget(verboseLevelSpin, 1, 1, 1, 1);
1088
1089
1090     gridLayout_2->addLayout(gridLayout, 0, 0, 1, 1);
1091
1092     horizontalLayout = new QHBoxLayout();
1093     horizontalLayout->setObjectName(QString("horizontalLayout"));
1094     logInFileCheck = new QCheckBox(logGroupBox);
1095     logInFileCheck->setObjectName(QString("logInFileCheck"));
1096     logInFileCheck->setChecked(true);
1097
1098     horizontalLayout->addWidget(logInFileCheck);
1099
1100     removeLogOnSuccessCheck = new QCheckBox(logGroupBox);
1101     removeLogOnSuccessCheck->setObjectName(QString("removeLogOnSuccessCheck"));
1102     removeLogOnSuccessCheck->setChecked(true);
1103
1104     horizontalLayout->addWidget(removeLogOnSuccessCheck);
1105
1106
1107     gridLayout_2->addLayout(horizontalLayout, 1, 0, 1, 1);
1108
1109     keepWorkingFilesCheck = new QCheckBox(logGroupBox);
1110     keepWorkingFilesCheck->setObjectName(QString("keepWorkingFilesCheck"));
1111     keepWorkingFilesCheck->setAutoExclusive(false);
1112
1113     gridLayout_2->addWidget(keepWorkingFilesCheck, 2, 0, 1, 1);
1114
1115
1116     gridLayout_4->addWidget(logGroupBox, 3, 0, 1, 2);
1117
1118
1119     // retranslateUi(this);
1120
1121     // QMetaObject::connectSlotsByName(this);
1122
1123 }
1124 void MgAdaptAdvWidget::_onWorkingDirectoryPushButton()
1125 {
1126     QString aDirName=QFileDialog::getExistingDirectory ();
1127     if (!(aDirName.isEmpty()))workingDirectoryLineEdit->setText(aDirName);
1128 }
1129
1130 namespace
1131 {
1132 bool isEditable( const QModelIndex& index )
1133 {
1134     return index.isValid() &&
1135            index.flags() & Qt::ItemIsEditable &&
1136            index.flags() & Qt::ItemIsEnabled &&
1137            ( !index.data( Qt::UserRole + 1 ).isValid() || index.data( Qt::UserRole + 1 ).toInt() != 0 );
1138 }
1139 }
1140
1141 MgAdaptAdvWidgetTreeWidget::MgAdaptAdvWidgetTreeWidget( QWidget* parent )
1142     : QTreeWidget( parent )
1143 {
1144 }
1145
1146 QModelIndex MgAdaptAdvWidgetTreeWidget::moveCursor( CursorAction action, Qt::KeyboardModifiers modifiers )
1147 {
1148     QModelIndex current = currentIndex();
1149     int column = current.column();
1150     if ( action == MoveNext ) {
1151         if ( column < columnCount()-1 ) {
1152             QModelIndex next = current.sibling( current.row(), column+1 );
1153             if ( isEditable( next ) )
1154                 return next;
1155         }
1156         else {
1157             QModelIndex next = current.sibling( current.row()+1, 0 );
1158             if ( isEditable( next ) )
1159                 return next;
1160         }
1161     }
1162     else if ( action == MovePrevious ) {
1163         if ( column == 0 ) {
1164             QModelIndex next = current.sibling( current.row()-1, columnCount()-1 );
1165             if ( isEditable( next ) )
1166                 return next;
1167         }
1168         else {
1169             QModelIndex next = current.sibling( current.row(), column-1 );
1170             if ( isEditable( next ) )
1171                 return next;
1172         }
1173     }
1174     return QTreeWidget::moveCursor( action, modifiers );
1175 }
1176
1177 void MgAdaptAdvWidgetTreeWidget::keyPressEvent( QKeyEvent* e )
1178 {
1179     switch ( e->key() ) {
1180     case Qt::Key_F2:
1181     {
1182         QModelIndex index = currentIndex();
1183         if ( !isEditable( index ) ) {
1184             for ( int i = 0; i < columnCount(); i++ ) {
1185                 QModelIndex sibling = index.sibling( index.row(), i );
1186                 if ( isEditable( sibling ) ) {
1187                     if ( !edit( sibling, EditKeyPressed, e ) )
1188                         e->ignore();
1189                 }
1190             }
1191         }
1192     }
1193     break;
1194     default:
1195         break;
1196     }
1197     QTreeWidget::keyPressEvent( e );
1198 }
1199
1200
1201 // =======================================================================
1202 // renvoie le medId associe au fichier Med apres ouverture
1203 // =======================================================================
1204 med_idt OuvrirFichier(QString aFile)
1205 {
1206     med_idt medIdt = MEDfileOpen(aFile.toStdString().c_str(),MED_ACC_RDONLY);
1207     if (medIdt <0)
1208     {
1209         QMessageBox::critical( 0, QObject::tr("MG_ADAPT_ERROR"),
1210                                QObject::tr("MG_ADAPT_MED_FILE_1") );
1211     }
1212     return medIdt;
1213 }
1214
1215 // ======================================================
1216 // ========================================================
1217 QString lireNomMaillage(QString aFile)
1218 {
1219     QString nomMaillage = QString::null ;
1220     int erreur = 0 ;
1221     med_idt medIdt ;
1222
1223     //  Ouverture du fichier
1224     medIdt = OuvrirFichier(aFile);
1225     if ( medIdt < 0 )
1226     {
1227         erreur = 1 ;
1228         return nomMaillage;
1229     }
1230     med_int numberOfMeshes = MEDnMesh(medIdt) ;
1231     if (numberOfMeshes == 0 )
1232     {
1233         QMessageBox::critical( 0, QObject::tr("MG_ADAPT_ERROR"),
1234                                QObject::tr("MG_ADAPT_MED_FILE_2") );
1235         erreur = 2 ;
1236         return nomMaillage;
1237     }
1238     if (numberOfMeshes > 1 )
1239     {
1240         QMessageBox::critical( 0, QObject::tr("MG_ADAPT_ERROR"),
1241                                QObject::tr("MG_ADAPT_MED_FILE_3") );
1242         erreur = 3 ;
1243         return nomMaillage;
1244     }
1245
1246     nomMaillage = lireNomMaillage2(medIdt,1);
1247     // Fermeture du fichier
1248     if ( medIdt > 0 ) MEDfileClose(medIdt);
1249
1250     return nomMaillage;
1251 }
1252
1253 // =======================================================================
1254 // =======================================================================
1255 QString lireNomMaillage2(med_idt medIdt,int meshId)
1256 {
1257     QString NomMaillage=QString::null;
1258     char meshname[MED_NAME_SIZE+1];
1259     med_int spacedim,meshdim;
1260     med_mesh_type meshtype;
1261     char descriptionription[MED_COMMENT_SIZE+1];
1262     char dtunit[MED_SNAME_SIZE+1];
1263     med_sorting_type sortingtype;
1264     med_int nstep;
1265     med_axis_type axistype;
1266     int naxis = MEDmeshnAxis(medIdt,1);
1267     char *axisname=new char[naxis*MED_SNAME_SIZE+1];
1268     char *axisunit=new char[naxis*MED_SNAME_SIZE+1];
1269     med_err aRet = MEDmeshInfo(medIdt,
1270                                meshId,
1271                                meshname,
1272                                &spacedim,
1273                                &meshdim,
1274                                &meshtype,
1275                                descriptionription,
1276                                dtunit,
1277                                &sortingtype,
1278                                &nstep,
1279                                &axistype,
1280                                axisname,
1281                                axisunit);
1282
1283     if ( aRet < 0 ) {
1284         QMessageBox::critical( 0, QObject::tr("MG_ADAPT_ERROR"), \
1285                                QObject::tr("MG_ADAPT_MED_FILE_4") );
1286     }
1287     else            {
1288         NomMaillage=QString(meshname);
1289     }
1290
1291     delete[] axisname ;
1292     delete[] axisunit ;
1293
1294     return NomMaillage;
1295 }
1296
1297
1298
1299 // =======================================================================
1300 std::map<QString, int> GetListeChamps(QString aFile, bool errorMessage)
1301 // =======================================================================
1302 {
1303 // Il faut voir si plusieurs maillages
1304
1305     MESSAGE("GetListeChamps");
1306     std::map<QString, int> ListeChamp ;
1307
1308     med_err erreur = 0 ;
1309     med_idt medIdt ;
1310
1311     while ( erreur == 0 )
1312     {
1313         // Ouverture du fichier
1314         SCRUTE(aFile.toStdString());
1315         medIdt = OuvrirFichier(aFile);
1316         if ( medIdt < 0 )
1317         {
1318             erreur = 1 ;
1319             break ;
1320         }
1321         // Lecture du nombre de champs
1322         med_int ncha = MEDnField(medIdt) ;
1323         if (ncha < 1 )
1324         {
1325             if(errorMessage)
1326             {
1327                 QMessageBox::critical( 0, QObject::tr("_ERROR"),
1328                                        QObject::tr("HOM_MED_FILE_5") );
1329             }
1330             erreur = 2 ;
1331             break ;
1332         }
1333         // Lecture des caracteristiques des champs
1334         for (int i=0; i< ncha; i++)
1335         {
1336 //       Lecture du nombre de composantes
1337             med_int ncomp = MEDfieldnComponent(medIdt,i+1);
1338 //       Lecture du type du champ, des noms des composantes et du nom de l'unite
1339             char nomcha  [MED_NAME_SIZE+1];
1340             char meshname[MED_NAME_SIZE+1];
1341             char * comp = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
1342             char * unit = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
1343             char dtunit[MED_SNAME_SIZE+1];
1344             med_bool local;
1345             med_field_type typcha;
1346             med_int nbofcstp;
1347             erreur = MEDfieldInfo(medIdt,i+1,nomcha,meshname,&local,&typcha,comp,unit,dtunit,&nbofcstp) ;
1348             free(comp);
1349             free(unit);
1350             if ( erreur < 0 )
1351             {
1352                 if(errorMessage)
1353                 {
1354                     QMessageBox::critical( 0, QObject::tr("MG_ADAPT_ERROR"),
1355                                            QObject::tr("MG_ADAPT_MED_FILE_6") );
1356                 }
1357                 break ;
1358             }
1359
1360             ListeChamp.insert(std::pair<QString, int> (QString(nomcha), nbofcstp));
1361
1362         }
1363         break ;
1364     }
1365     // Fermeture du fichier
1366     if ( medIdt > 0 ) MEDfileClose(medIdt);
1367
1368     return ListeChamp;
1369 }
1370
1371
1372
1373