Salome HOME
merge from branch V5_1_sizemap tag mergeto_V5_1_main_8oct09
[plugins/ghs3dplugin.git] / src / GUI / GHS3DPluginGUI_HypothesisCreator.cxx
1 //  Copyright (C) 2004-2008  CEA/DEN, EDF R&D
2 //
3 //  This library is free software; you can redistribute it and/or
4 //  modify it under the terms of the GNU Lesser General Public
5 //  License as published by the Free Software Foundation; either
6 //  version 2.1 of the License.
7 //
8 //  This library is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 //  Lesser General Public License for more details.
12 //
13 //  You should have received a copy of the GNU Lesser General Public
14 //  License along with this library; if not, write to the Free Software
15 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 //  GHS3DPlugin GUI: GUI for plugged-in mesher GHS3DPlugin
20 //  File   : GHS3DPluginGUI_HypothesisCreator.cxx
21 //  Author : Michael Zorin
22 //  Module : GHS3DPlugin
23 //  $Header: 
24 //
25 #include "GHS3DPluginGUI_HypothesisCreator.h"
26
27 #include <SMESHGUI_Utils.h>
28 #include <SMESHGUI_HypothesesUtils.h>
29
30 #include <SUIT_Session.h>
31 #include <SUIT_MessageBox.h>
32 #include <SUIT_ResourceMgr.h>
33 #include <SUIT_FileDlg.h>
34 #include <SalomeApp_Tools.h>
35 #include <SalomeApp_TypeFilter.h>
36
37 #include <QComboBox>
38 #include <QLabel>
39 #include <QFrame>
40 #include <QVBoxLayout>
41 #include <QGridLayout>
42 #include <QLineEdit>
43 #include <QCheckBox>
44 #include <QTabWidget>
45 #include <QSpinBox>
46 #include <QPushButton>
47 #include <QFileInfo>
48
49 #include <QTableWidget>
50 #include <QStandardItemModel>
51 #include <QStandardItem>
52 #include <QHeaderView>
53 #include <QModelIndexList>
54
55 #include <stdexcept>
56 #include <utilities.h>
57
58 // tabs
59 enum {
60   STD_TAB = 0,
61   ADV_TAB,
62   ENF_VER_TAB
63 };
64
65 // Enforced vertices array columns
66 enum {
67   ENF_VER_X_COLUMN = 0,
68   ENF_VER_Y_COLUMN,
69   ENF_VER_Z_COLUMN,
70   ENF_VER_SIZE_COLUMN,
71   ENF_VER_NB_COLUMNS
72 };
73
74 // Enforced vertices inputs
75 enum {
76   ENF_VER_BTNS = 0,
77   ENF_VER_X_COORD,
78   ENF_VER_Y_COORD,
79   ENF_VER_Z_COORD,
80   ENF_VER_SIZE,
81   ENF_VER_VERTEX_BTN,
82   ENF_VER_SEPARATOR,
83   ENF_VER_REMOVE_BTN,
84 };
85
86 namespace {
87
88 #ifdef WIN32
89 #include <windows.h>
90 #else
91 #include <sys/sysinfo.h>
92 #endif
93
94   int maxAvailableMemory()
95   {
96 #ifdef WIN32
97     // See http://msdn.microsoft.com/en-us/library/aa366589.aspx
98     MEMORYSTATUSEX statex;
99     statex.dwLength = sizeof (statex);
100     int err = GlobalMemoryStatusEx (&statex);
101     if (err != 0) {
102       int totMB = 
103         statex.ullTotalPhys / 1024 / 1024 +
104         statex.ullTotalPageFile / 1024 / 1024 +
105         statex.ullTotalVirtual / 1024 / 1024;
106       return (int) ( 0.7 * totMB );
107     }
108 #else
109     struct sysinfo si;
110     int err = sysinfo( &si );
111     if ( err == 0 ) {
112       int totMB =
113         si.totalram * si.mem_unit / 1024 / 1024 +
114         si.totalswap * si.mem_unit / 1024 / 1024 ;
115       return (int) ( 0.7 * totMB );
116     }
117 #endif
118   }
119 }
120
121 class QDoubleValidator;
122
123 //
124 // BEGIN DoubleLineEditDelegate
125 //
126
127 DoubleLineEditDelegate::DoubleLineEditDelegate(QObject *parent)
128     : QItemDelegate(parent)
129 {
130 }
131
132 QWidget *DoubleLineEditDelegate::createEditor(QWidget *parent,
133     const QStyleOptionViewItem &/* option */,
134     const QModelIndex &/* index */) const
135 {
136     QLineEdit *editor = new QLineEdit(parent);
137     editor->setValidator(new QDoubleValidator(parent));
138
139     return editor;
140 }
141
142 void DoubleLineEditDelegate::setEditorData(QWidget *editor,
143                                     const QModelIndex &index) const
144 {
145     QString value = index.model()->data(index, Qt::EditRole).toString();
146
147     QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
148     lineEdit->setText(value);
149 }
150
151 void DoubleLineEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
152                                     const QModelIndex &index) const
153 {
154     QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
155     bool ok;
156     double value = lineEdit->text().toDouble(&ok);
157
158     if (ok) {
159         model->setData(index, value, Qt::EditRole);
160         MESSAGE("Value " << value << " was set at index(" << index.row() << "," << index.column() << ")");
161     }
162 }
163
164 void DoubleLineEditDelegate::updateEditorGeometry(QWidget *editor,
165     const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
166 {
167     editor->setGeometry(option.rect);
168 }
169
170 //
171 // END DoubleLineEditDelegate
172 //
173
174 GHS3DPluginGUI_HypothesisCreator::GHS3DPluginGUI_HypothesisCreator( const QString& theHypType )
175 : SMESHGUI_GenericHypothesisCreator( theHypType )
176 {
177 }
178
179 GHS3DPluginGUI_HypothesisCreator::~GHS3DPluginGUI_HypothesisCreator()
180 {
181 }
182
183 QFrame* GHS3DPluginGUI_HypothesisCreator::buildFrame()
184 {
185   QFrame* fr = new QFrame( 0 );
186   QVBoxLayout* lay = new QVBoxLayout( fr );
187   lay->setMargin( 5 );
188   lay->setSpacing( 0 );
189
190   // tab
191   QTabWidget* tab = new QTabWidget( fr );
192   tab->setTabShape( QTabWidget::Rounded );
193   tab->setTabPosition( QTabWidget::North );
194   lay->addWidget( tab );
195
196   // basic parameters
197   myStdGroup = new QWidget();
198   QGridLayout* aStdLayout = new QGridLayout( myStdGroup );
199   aStdLayout->setSpacing( 6 );
200   aStdLayout->setMargin( 11 );
201
202   int row = 0;
203   myName = 0;
204   if( isCreation() )
205   {
206     aStdLayout->addWidget( new QLabel( tr( "SMESH_NAME" ), myStdGroup ), row, 0, 1, 1 );
207     myName = new QLineEdit( myStdGroup );
208     aStdLayout->addWidget( myName, row++, 1, 1, 1 );
209   }
210
211   myToMeshHolesCheck = new QCheckBox( tr( "GHS3D_TO_MESH_HOLES" ), myStdGroup );
212   aStdLayout->addWidget( myToMeshHolesCheck, row++, 0, 1, 2 );
213
214   aStdLayout->addWidget( new QLabel( tr( "GHS3D_OPTIMIZATIOL_LEVEL" ), myStdGroup ), row, 0 );
215   myOptimizationLevelCombo = new QComboBox( myStdGroup );
216   aStdLayout->addWidget( myOptimizationLevelCombo, row++, 1, 1, 1 );
217
218   QStringList types;
219   types << tr( "LEVEL_NONE" ) << tr( "LEVEL_LIGHT" ) << tr( "LEVEL_MEDIUM" ) << tr( "LEVEL_STANDARDPLUS" ) << tr( "LEVEL_STRONG" );
220   myOptimizationLevelCombo->addItems( types );
221
222   aStdLayout->setRowStretch( row, 5 );
223
224   // advanced parameters
225   myAdvGroup = new QWidget();
226   QGridLayout* anAdvLayout = new QGridLayout( myAdvGroup );
227   anAdvLayout->setSpacing( 6 );
228   anAdvLayout->setMargin( 11 );
229   
230   myMaximumMemoryCheck = new QCheckBox( tr( "MAX_MEMORY_SIZE" ), myAdvGroup );
231   myMaximumMemorySpin = new QSpinBox( myAdvGroup );
232   myMaximumMemorySpin->setMinimum( 1 );
233   myMaximumMemorySpin->setMaximum( maxAvailableMemory() );
234   myMaximumMemorySpin->setSingleStep( 10 );
235   QLabel* aMegabyteLabel = new QLabel( tr( "MEGABYTE" ), myAdvGroup );
236
237   myInitialMemoryCheck = new QCheckBox( tr( "INIT_MEMORY_SIZE" ), myAdvGroup );
238   myInitialMemorySpin = new QSpinBox( myAdvGroup );
239   myInitialMemorySpin->setMinimum( 1 );
240   myInitialMemorySpin->setMaximum( maxAvailableMemory() );
241   myInitialMemorySpin->setSingleStep( 10 );
242   QLabel* aMegabyteLabel2 = new QLabel( tr( "MEGABYTE" ), myAdvGroup );
243
244   QLabel* aWorkinDirLabel = new QLabel( tr( "WORKING_DIR" ), myAdvGroup );
245   myWorkingDir = new QLineEdit( myAdvGroup );
246   //myWorkingDir->setReadOnly( true );
247   QPushButton* dirBtn = new QPushButton( tr( "SELECT_DIR" ), myAdvGroup );
248   dirBtn->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
249   
250   myKeepFiles = new QCheckBox( tr( "KEEP_WORKING_FILES" ), myAdvGroup );
251
252   QLabel* aVerboseLevelLabel = new QLabel( tr( "VERBOSE_LEVEL" ), myAdvGroup );
253   myVerboseLevelSpin = new QSpinBox( myAdvGroup );
254   myVerboseLevelSpin->setMinimum( 0 );
255   myVerboseLevelSpin->setMaximum( 10 );
256   myVerboseLevelSpin->setSingleStep( 1 );
257
258   myToCreateNewNodesCheck = new QCheckBox( tr( "TO_ADD_NODES" ), myAdvGroup );
259   
260   myRemoveInitialCentralPointCheck = new QCheckBox( tr( "NO_INITIAL_CENTRAL_POINT" ), myAdvGroup );
261   
262   myBoundaryRecoveryCheck = new QCheckBox( tr( "RECOVERY_VERSION" ), myAdvGroup );
263   
264   myFEMCorrectionCheck = new QCheckBox( tr( "FEM_CORRECTION" ), myAdvGroup );
265
266   QLabel* aTextOptionLabel = new QLabel( tr( "TEXT_OPTION" ), myAdvGroup );
267   myTextOption = new QLineEdit( myAdvGroup );
268
269   anAdvLayout->addWidget( myMaximumMemoryCheck,             0, 0, 1, 1 );
270   anAdvLayout->addWidget( myMaximumMemorySpin,              0, 1, 1, 1 );
271   anAdvLayout->addWidget( aMegabyteLabel,                   0, 2, 1, 1 );
272   anAdvLayout->addWidget( myInitialMemoryCheck,             1, 0, 1, 1 );
273   anAdvLayout->addWidget( myInitialMemorySpin,              1, 1, 1, 1 );
274   anAdvLayout->addWidget( aMegabyteLabel2,                  1, 2, 1, 1 );
275   anAdvLayout->addWidget( aWorkinDirLabel,                  2, 0, 1, 1 );
276   anAdvLayout->addWidget( myWorkingDir,                     2, 1, 1, 2 );
277   anAdvLayout->addWidget( dirBtn,                           2, 3, 1, 1 );
278   anAdvLayout->addWidget( myKeepFiles,                      3, 0, 1, 4 );
279   anAdvLayout->addWidget( aVerboseLevelLabel,               4, 0, 1, 1 );
280   anAdvLayout->addWidget( myVerboseLevelSpin,               4, 1, 1, 1 );
281   anAdvLayout->addWidget( myToCreateNewNodesCheck,          5, 0, 1, 4 );
282   anAdvLayout->addWidget( myRemoveInitialCentralPointCheck, 6, 0, 1, 4 );
283   anAdvLayout->addWidget( myBoundaryRecoveryCheck,          7, 0, 1, 4 );
284   anAdvLayout->addWidget( myFEMCorrectionCheck,             8, 0, 1, 4 );
285   anAdvLayout->addWidget( aTextOptionLabel,                 9, 0, 1, 1 );
286   anAdvLayout->addWidget( myTextOption,                     9, 1, 1, 2 );
287
288   // Size Maps parameters
289   myEnfGroup = new QWidget();
290   QGridLayout* anSmpLayout = new QGridLayout(myEnfGroup);
291   
292   mySmpModel = new QStandardItemModel(0, ENF_VER_NB_COLUMNS);
293   myEnforcedTableView = new QTableView(myEnfGroup);
294   myEnforcedTableView->setModel(mySmpModel);
295   myEnforcedTableView->setSortingEnabled(true);
296   myEnforcedTableView->setItemDelegateForColumn(ENF_VER_SIZE_COLUMN,new DoubleLineEditDelegate(this));
297   anSmpLayout->addWidget(myEnforcedTableView, 1, 0, 9, 1);
298   QStringList enforcedHeaders;
299   enforcedHeaders << tr( "GHS3D_ENF_VER_X_COLUMN" )<< tr( "GHS3D_ENF_VER_Y_COLUMN" ) << tr( "GHS3D_ENF_VER_Z_COLUMN" ) << tr( "GHS3D_ENF_VER_SIZE_COLUMN" ); 
300   mySmpModel->setHorizontalHeaderLabels(enforcedHeaders);
301   myEnforcedTableView->setAlternatingRowColors(true);
302   myEnforcedTableView->verticalHeader()->hide();
303   myEnforcedTableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
304   
305   QLabel* myXCoordLabel = new QLabel( tr( "GHS3D_ENF_VER_X_LABEL" ), myEnfGroup );
306   anSmpLayout->addWidget(myXCoordLabel, ENF_VER_X_COORD, 1, 1, 1);
307   myXCoord = new QLineEdit(myEnfGroup);
308   myXCoord->setValidator(new QDoubleValidator(myEnfGroup));
309   anSmpLayout->addWidget(myXCoord, ENF_VER_X_COORD, 2, 1, 1);
310   QLabel* myYCoordLabel = new QLabel( tr( "GHS3D_ENF_VER_Y_LABEL" ), myEnfGroup );
311   anSmpLayout->addWidget(myYCoordLabel, ENF_VER_Y_COORD, 1, 1, 1);
312   myYCoord = new QLineEdit(myEnfGroup);
313   myYCoord->setValidator(new QDoubleValidator(myEnfGroup));
314   anSmpLayout->addWidget(myYCoord, ENF_VER_Y_COORD, 2, 1, 1);
315   QLabel* myZCoordLabel = new QLabel( tr( "GHS3D_ENF_VER_Z_LABEL" ), myEnfGroup );
316   anSmpLayout->addWidget(myZCoordLabel, ENF_VER_Z_COORD, 1, 1, 1);
317   myZCoord = new QLineEdit(myEnfGroup);
318   myZCoord->setValidator(new QDoubleValidator(myEnfGroup));
319   anSmpLayout->addWidget(myZCoord, ENF_VER_Z_COORD, 2, 1, 1);
320   QLabel* mySizeLabel = new QLabel( tr( "GHS3D_ENF_VER_SIZE_LABEL" ), myEnfGroup );
321   anSmpLayout->addWidget(mySizeLabel, ENF_VER_SIZE, 1, 1, 1);
322   mySizeValue = new QLineEdit(myEnfGroup);
323   mySizeValue->setValidator(new QDoubleValidator(myEnfGroup));
324   anSmpLayout->addWidget(mySizeValue, ENF_VER_SIZE, 2, 1, 1);
325
326   addVertexButton = new QPushButton(tr("GHS3D_ENF_VER_VERTEX"),myEnfGroup);
327   anSmpLayout->addWidget(addVertexButton, ENF_VER_VERTEX_BTN, 1, 1, 2);
328   addVertexButton->setEnabled(false);
329
330   QFrame *line = new QFrame(myEnfGroup);
331   line->setFrameShape(QFrame::HLine);
332   line->setFrameShadow(QFrame::Sunken);
333   anSmpLayout->addWidget(line, ENF_VER_SEPARATOR, 1, 1, 2);
334
335   removeVertexButton = new QPushButton(tr("GHS3D_ENF_VER_REMOVE"),myEnfGroup);
336   anSmpLayout->addWidget(removeVertexButton, ENF_VER_REMOVE_BTN, 1, 1, 2);
337           
338   // add tabs
339   tab->insertTab( STD_TAB, myStdGroup, tr( "SMESH_ARGUMENTS" ) );
340   tab->insertTab( ADV_TAB, myAdvGroup, tr( "GHS3D_ADV_ARGS" ) );
341   tab->insertTab( ENF_VER_TAB, myEnfGroup, tr( "GHS3D_ENFORCED_VERTICES" ) );
342   tab->setCurrentIndex( STD_TAB );
343
344   // connections
345   connect( myMaximumMemoryCheck,    SIGNAL( toggled( bool ) ), this, SLOT( updateWidgets() ) );
346   connect( myInitialMemoryCheck,    SIGNAL( toggled( bool ) ), this, SLOT( updateWidgets() ) );
347   connect( myBoundaryRecoveryCheck, SIGNAL( toggled( bool ) ), this, SLOT( updateWidgets() ) );
348   connect( dirBtn,                  SIGNAL( clicked() ),       this, SLOT( onDirBtnClicked() ) );
349   connect( myXCoord,                SIGNAL( textChanged(const QString&) ),   this, SLOT( checkVertexIsDefined() ) );
350   connect( myYCoord,                SIGNAL( textChanged(const QString&) ),   this, SLOT( checkVertexIsDefined() ) );
351   connect( myZCoord,                SIGNAL( textChanged(const QString&) ),   this, SLOT( checkVertexIsDefined() ) );
352   connect( mySizeValue,             SIGNAL( textChanged(const QString&) ),   this, SLOT( checkVertexIsDefined() ) );
353   connect( this,                    SIGNAL( vertexDefined(bool) ), addVertexButton, SLOT( setEnabled(bool) ) );
354   connect( addVertexButton,         SIGNAL( clicked() ),       this, SLOT( onVertexBtnClicked() ) );
355   connect( removeVertexButton,      SIGNAL( clicked() ),       this, SLOT( onRemoveVertexBtnClicked() ) );
356   
357
358   
359   return fr;
360 }
361
362 bool GHS3DPluginGUI_HypothesisCreator::smpVertexExists(double x, double y, double z) const
363 {
364     const int rowCount = mySmpModel->rowCount();
365     for (int i=0 ; i < rowCount ; i++) {
366       double myX = mySmpModel->data(mySmpModel->index(i, ENF_VER_X_COLUMN)).toDouble();
367       if (myX == x) {
368 //         MESSAGE("Found x value " << x << " at row " << i);
369         double myY = mySmpModel->data(mySmpModel->index(i, ENF_VER_Y_COLUMN)).toDouble();
370         if (myY == y) {
371 //           MESSAGE("Found y value " << y << " at row " << i);
372           double myZ = mySmpModel->data(mySmpModel->index(i, ENF_VER_Z_COLUMN)).toDouble();
373           if (myZ == z) {
374             MESSAGE("Found x value " << x << " at row " << i);
375             MESSAGE("Found y value " << y << " at row " << i);
376             MESSAGE("Found z value " << z << " at row " << i);
377             return true;
378           }
379         }
380       }
381     }
382 //     MESSAGE("Not found x,y,z values: " << x << " " << y << " " << z);
383     return false;
384 }
385
386 bool GHS3DPluginGUI_HypothesisCreator::checkVertexIsDefined()
387 {
388   bool val = (!myXCoord->text().isEmpty())&&(!myYCoord->text().isEmpty())&&(!myZCoord->text().isEmpty())&&(!mySizeValue->text().isEmpty());
389   bool isDefined = val;
390   if (val)
391     isDefined = not smpVertexExists(myXCoord->text().toDouble(),myYCoord->text().toDouble(),myZCoord->text().toDouble());
392
393   emit vertexDefined(isDefined);
394   return isDefined;
395 }
396
397 void GHS3DPluginGUI_HypothesisCreator::onVertexBtnClicked()
398 {
399     MESSAGE("GHS3DPluginGUI_HypothesisCreator::onVertexBtnClicked()");
400     const int row = mySmpModel->rowCount() ;
401     double x = myXCoord->text().toDouble();
402     double y = myYCoord->text().toDouble();
403     double z = myZCoord->text().toDouble();
404     double size = mySizeValue->text().toDouble();
405     
406     if (smpVertexExists(x,y,z)) return;
407     
408 //     double size = 10.0;
409     // ENF_VER_X_COLUMN
410     mySmpModel->setData(mySmpModel->index(row, ENF_VER_X_COLUMN),x);
411     mySmpModel->setItem( row, ENF_VER_X_COLUMN, new QStandardItem(QString::number(x)) );
412     mySmpModel->item( row, ENF_VER_X_COLUMN )->setFlags( Qt::ItemIsSelectable );
413     // ENF_VER_Y_COLUMN
414     mySmpModel->setData(mySmpModel->index(row, ENF_VER_Y_COLUMN),y);
415     mySmpModel->setItem( row, ENF_VER_Y_COLUMN, new QStandardItem(QString::number(y)) );
416     mySmpModel->item( row, ENF_VER_Y_COLUMN )->setFlags( Qt::ItemIsSelectable );
417     // ENF_VER_Z_COLUMN
418     mySmpModel->setData(mySmpModel->index(row, ENF_VER_Z_COLUMN),z);
419     mySmpModel->setItem( row, ENF_VER_Z_COLUMN, new QStandardItem(QString::number(z)) );
420     mySmpModel->item( row, ENF_VER_Z_COLUMN )->setFlags( Qt::ItemIsSelectable );
421     // ENF_VER_SIZE_COLUMN
422     mySmpModel->setData(mySmpModel->index(row, ENF_VER_SIZE_COLUMN),size);
423     mySmpModel->setItem( row, ENF_VER_SIZE_COLUMN, new QStandardItem(QString::number(size,'f')) );
424
425     myEnforcedTableView->clearSelection();
426     myEnforcedTableView->scrollTo( mySmpModel->item( row, ENF_VER_SIZE_COLUMN )->index() );
427     checkVertexIsDefined();
428 }
429
430 void GHS3DPluginGUI_HypothesisCreator::onRemoveVertexBtnClicked()
431 {
432     QList<int> selectedRows;
433     QList<QModelIndex> selectedIndex = myEnforcedTableView->selectionModel()->selectedIndexes();
434     int row;
435     QModelIndex index;
436     foreach( index, selectedIndex ) {
437         row = index.row();
438         if ( !selectedRows.contains( row ) ) 
439         selectedRows.append( row );
440     }
441     qSort( selectedRows );
442     QListIterator<int> it( selectedRows );
443     it.toBack();
444     while ( it.hasPrevious() ) {
445         row = it.previous();
446         MESSAGE("delete row #"<< row);
447         mySmpModel->removeRow(row );
448     }
449     myEnforcedTableView->clearSelection();
450 }
451 void GHS3DPluginGUI_HypothesisCreator::onDirBtnClicked()
452 {
453   QString dir = SUIT_FileDlg::getExistingDirectory( dlg(), myWorkingDir->text(), QString() );
454   if ( !dir.isEmpty() )
455     myWorkingDir->setText( dir );
456 }
457
458 void GHS3DPluginGUI_HypothesisCreator::updateWidgets()
459 {
460   myMaximumMemorySpin->setEnabled( myMaximumMemoryCheck->isChecked() );
461   myInitialMemoryCheck->setEnabled( !myBoundaryRecoveryCheck->isChecked() );
462   myInitialMemorySpin->setEnabled( myInitialMemoryCheck->isChecked() && !myBoundaryRecoveryCheck->isChecked() );
463   myOptimizationLevelCombo->setEnabled( !myBoundaryRecoveryCheck->isChecked() );
464 }
465
466 bool GHS3DPluginGUI_HypothesisCreator::checkParams(QString& msg) const
467 {
468   MESSAGE("GHS3DPluginGUI_HypothesisCreator::checkParams");
469
470   if ( !QFileInfo( myWorkingDir->text().trimmed() ).isWritable() ) {
471     SUIT_MessageBox::warning( dlg(),
472                               tr( "SMESH_WRN_WARNING" ),
473                               tr( "GHS3D_PERMISSION_DENIED" ) );
474     return false;
475   }
476
477   return true;
478 }
479
480 void GHS3DPluginGUI_HypothesisCreator::retrieveParams() const
481 {
482   MESSAGE("GHS3DPluginGUI_HypothesisCreator::retrieveParams");
483   GHS3DHypothesisData data;
484   readParamsFromHypo( data );
485
486   if ( myName )
487     myName->setText( data.myName );
488   
489   myToMeshHolesCheck               ->setChecked    ( data.myToMeshHoles );
490   myOptimizationLevelCombo         ->setCurrentIndex( data.myOptimizationLevel );
491   myMaximumMemoryCheck             ->setChecked    ( data.myMaximumMemory > 0 );
492   myMaximumMemorySpin              ->setValue      ( qMax( data.myMaximumMemory,
493                                                                                          myMaximumMemorySpin->minimum() ));
494   myInitialMemoryCheck             ->setChecked    ( data.myInitialMemory > 0 );
495   myInitialMemorySpin              ->setValue      ( qMax( data.myInitialMemory,
496                                                                                          myInitialMemorySpin->minimum() ));
497   myWorkingDir                     ->setText       ( data.myWorkingDir );
498   myKeepFiles                      ->setChecked    ( data.myKeepFiles );
499   myVerboseLevelSpin               ->setValue      ( data.myVerboseLevel );
500   myToCreateNewNodesCheck          ->setChecked    ( data.myToCreateNewNodes );
501   myRemoveInitialCentralPointCheck ->setChecked    ( data.myRemoveInitialCentralPoint );
502   myBoundaryRecoveryCheck          ->setChecked    ( data.myBoundaryRecovery );
503   myFEMCorrectionCheck             ->setChecked    ( data.myFEMCorrection );
504   myTextOption                     ->setText       ( data.myTextOption );
505
506   TEnforcedVertexValues::const_iterator it;
507   int row = 0;
508   for(it = data.myEnforcedVertices.begin() ; it != data.myEnforcedVertices.end(); it++ )
509   {
510     double x = it->at(0);
511     double y = it->at(1);
512     double z = it->at(2);
513     double size = it->at(3);
514     // ENF_VER_X_COLUMN
515     mySmpModel->setData(mySmpModel->index(row, ENF_VER_X_COLUMN),x);
516     mySmpModel->setItem( row, ENF_VER_X_COLUMN, new QStandardItem(QString::number(x)) );
517     mySmpModel->item( row, ENF_VER_X_COLUMN )->setFlags( Qt::ItemIsSelectable );
518     // ENF_VER_Y_COLUMN
519     mySmpModel->setData(mySmpModel->index(row, ENF_VER_Y_COLUMN),y);
520     mySmpModel->setItem( row, ENF_VER_Y_COLUMN, new QStandardItem(QString::number(y)) );
521     mySmpModel->item( row, ENF_VER_Y_COLUMN )->setFlags( Qt::ItemIsSelectable );
522     // ENF_VER_Z_COLUMN
523     mySmpModel->setData(mySmpModel->index(row, ENF_VER_Z_COLUMN),z);
524     mySmpModel->setItem( row, ENF_VER_Z_COLUMN, new QStandardItem(QString::number(z)) );
525     mySmpModel->item( row, ENF_VER_Z_COLUMN )->setFlags( Qt::ItemIsSelectable );
526     // ENF_VER_SIZE_COLUMN
527     mySmpModel->setData(mySmpModel->index(row, ENF_VER_SIZE_COLUMN),size);
528     mySmpModel->setItem( row, ENF_VER_SIZE_COLUMN, new QStandardItem(QString::number(size)) );
529
530     MESSAGE("Row " << row << ": (" << x << ","<< y << ","<< z << ") ="<< size);
531     row++;
532   }
533   
534   GHS3DPluginGUI_HypothesisCreator* that = (GHS3DPluginGUI_HypothesisCreator*)this;
535   that->updateWidgets();
536 }
537
538 QString GHS3DPluginGUI_HypothesisCreator::storeParams() const
539 {
540     MESSAGE("GHS3DPluginGUI_HypothesisCreator::storeParams");
541     GHS3DHypothesisData data;
542     readParamsFromWidgets( data );
543     storeParamsToHypo( data );
544     
545     QString valStr = "";
546     
547     if ( !data.myBoundaryRecovery )
548         valStr = "-c " + QString::number( !data.myToMeshHoles );
549     
550     if ( data.myOptimizationLevel >= 0 && data.myOptimizationLevel < 5 && !data.myBoundaryRecovery) {
551         char* level[] = { "none" , "light" , "standard" , "standard+" , "strong" };
552         valStr += " -o ";
553         valStr += level[ data.myOptimizationLevel ];
554     }
555     if ( data.myMaximumMemory > 0 ) {
556         valStr += " -m ";
557         valStr += QString::number( data.myMaximumMemory );
558     }
559     if ( data.myInitialMemory > 0 && !data.myBoundaryRecovery ) {
560         valStr += " -M ";
561         valStr += QString::number( data.myInitialMemory );
562     }
563     valStr += " -v ";
564     valStr += QString::number( data.myVerboseLevel );
565     
566     if ( !data.myToCreateNewNodes )
567         valStr += " -p0";
568     
569     if ( data.myRemoveInitialCentralPoint )
570         valStr += " -no_initial_central_point";
571     
572     if ( data.myBoundaryRecovery )
573         valStr += " -C";
574     
575     if ( data.myFEMCorrection )
576         valStr += " -FEM";
577     
578     valStr += " ";
579     valStr += data.myTextOption;
580     
581     valStr += " #BEGIN ENFORCED VERTICES#";
582     // Add size map parameters storage
583     for (int i=0 ; i<mySmpModel->rowCount() ; i++) {
584         valStr += " (";
585         double x = mySmpModel->data(mySmpModel->index(i,ENF_VER_X_COLUMN)).toDouble();
586         double y = mySmpModel->data(mySmpModel->index(i,ENF_VER_Y_COLUMN)).toDouble();
587         double z = mySmpModel->data(mySmpModel->index(i,ENF_VER_Z_COLUMN)).toDouble();
588         double size = mySmpModel->data(mySmpModel->index(i,ENF_VER_SIZE_COLUMN)).toDouble();
589         valStr += QString::number( x );
590         valStr += ",";
591         valStr += QString::number( y );
592         valStr += ",";
593         valStr += QString::number( z );
594         valStr += ")=";
595         valStr += QString::number( size );
596         if (i!=mySmpModel->rowCount()-1)
597             valStr += ";";
598     }
599     valStr += " #END ENFORCED VERTICES#";
600     MESSAGE(valStr.toStdString());
601   return valStr;
602 }
603
604 bool GHS3DPluginGUI_HypothesisCreator::readParamsFromHypo( GHS3DHypothesisData& h_data ) const
605 {
606   MESSAGE("GHS3DPluginGUI_HypothesisCreator::readParamsFromHypo");
607   GHS3DPlugin::GHS3DPlugin_Hypothesis_var h =
608     GHS3DPlugin::GHS3DPlugin_Hypothesis::_narrow( initParamsHypothesis() );
609
610   HypothesisData* data = SMESH::GetHypothesisData( hypType() );
611   h_data.myName = isCreation() && data ? hypName() : "";
612
613   h_data.myToMeshHoles                = h->GetToMeshHoles();
614   h_data.myMaximumMemory              = h->GetMaximumMemory();
615   h_data.myInitialMemory              = h->GetInitialMemory();
616   h_data.myInitialMemory              = h->GetInitialMemory();
617   h_data.myOptimizationLevel          = h->GetOptimizationLevel();
618   h_data.myKeepFiles                  = h->GetKeepFiles();
619   h_data.myWorkingDir                 = h->GetWorkingDirectory();
620   h_data.myVerboseLevel               = h->GetVerboseLevel();
621   h_data.myToCreateNewNodes           = h->GetToCreateNewNodes();
622   h_data.myRemoveInitialCentralPoint  = h->GetToRemoveCentralPoint();
623   h_data.myBoundaryRecovery           = h->GetToUseBoundaryRecoveryVersion();
624   h_data.myFEMCorrection              = h->GetFEMCorrection();
625   h_data.myTextOption                 = h->GetTextOption();
626   
627   GHS3DPlugin::GHS3DEnforcedVertexList_var vertices = h->GetEnforcedVertices();
628   MESSAGE("vertices->length(): " << vertices->length());
629   h_data.myEnforcedVertices.clear();
630   for (int i=0 ; i<vertices->length() ; i++) {
631     GHS3DEnforcedVertex myVertex;
632     myVertex.push_back(vertices[i].x);
633     myVertex.push_back(vertices[i].y);
634     myVertex.push_back(vertices[i].z);
635     myVertex.push_back(vertices[i].size);
636     MESSAGE("Add enforced vertex ("<< myVertex[0] << ","<< myVertex[1] << ","<< myVertex[2] << ") ="<< myVertex[3]);
637     h_data.myEnforcedVertices.push_back(myVertex);
638   }
639   return true;
640 }
641
642 bool GHS3DPluginGUI_HypothesisCreator::storeParamsToHypo( const GHS3DHypothesisData& h_data ) const
643 {
644   MESSAGE("GHS3DPluginGUI_HypothesisCreator::storeParamsToHypo");
645   GHS3DPlugin::GHS3DPlugin_Hypothesis_var h =
646     GHS3DPlugin::GHS3DPlugin_Hypothesis::_narrow( hypothesis() );
647
648   bool ok = true;
649   try
650   {
651     if( isCreation() )
652       SMESH::SetName( SMESH::FindSObject( h ), h_data.myName.toLatin1().constData() );
653
654     if ( h->GetToMeshHoles() != h_data.myToMeshHoles ) // avoid duplication of DumpPython commands
655       h->SetToMeshHoles      ( h_data.myToMeshHoles       );
656     if ( h->GetMaximumMemory() != h_data.myMaximumMemory )
657       h->SetMaximumMemory    ( h_data.myMaximumMemory     );
658     if ( h->GetInitialMemory() != h_data.myInitialMemory )
659       h->SetInitialMemory    ( h_data.myInitialMemory     );
660     if ( h->GetInitialMemory() != h_data.myInitialMemory )
661       h->SetInitialMemory    ( h_data.myInitialMemory     );
662     if ( h->GetOptimizationLevel() != h_data.myOptimizationLevel )
663       h->SetOptimizationLevel( h_data.myOptimizationLevel );
664     if ( h->GetKeepFiles() != h_data.myKeepFiles )
665       h->SetKeepFiles        ( h_data.myKeepFiles         );
666     if ( h->GetWorkingDirectory() != h_data.myWorkingDir )
667       h->SetWorkingDirectory ( h_data.myWorkingDir.toLatin1().constData() );
668     if ( h->GetVerboseLevel() != h_data.myVerboseLevel )
669       h->SetVerboseLevel     ( h_data.myVerboseLevel );
670     if ( h->GetToCreateNewNodes() != h_data.myToCreateNewNodes )
671       h->SetToCreateNewNodes( h_data.myToCreateNewNodes );
672     if ( h->GetToRemoveCentralPoint() != h_data.myRemoveInitialCentralPoint )
673       h->SetToRemoveCentralPoint( h_data.myRemoveInitialCentralPoint );
674     if ( h->GetToUseBoundaryRecoveryVersion() != h_data.myBoundaryRecovery )
675       h->SetToUseBoundaryRecoveryVersion( h_data.myBoundaryRecovery );
676     if ( h->GetFEMCorrection() != h_data.myFEMCorrection )
677       h->SetFEMCorrection( h_data.myFEMCorrection );
678     if ( h->GetTextOption() != h_data.myTextOption )
679       h->SetTextOption       ( h_data.myTextOption.toLatin1().constData() );
680     
681     int nbVertex = (int) h_data.myEnforcedVertices.size();
682     GHS3DPlugin::GHS3DEnforcedVertexList_var vertexHyp = h->GetEnforcedVertices();
683     int nbVertexHyp = vertexHyp->length();
684     
685     MESSAGE("Store params for size maps: " << nbVertex << " enforced vertices");
686     MESSAGE("h->GetEnforcedVertices()->length(): " << nbVertexHyp);
687     
688     // Some vertices were removed
689     if (nbVertex < nbVertexHyp) {
690 //        if (nbVertex == 0)
691 //            h->ClearEnforcedVertices();
692 //        else {
693             // iterate over vertices of hypo
694             for(int i = 0 ; i <nbVertexHyp ; i++) {
695                 double x = vertexHyp[i].x;
696                 double y = vertexHyp[i].y;
697                 double z = vertexHyp[i].z;
698                 // vertex is removed
699                 if (!smpVertexExists(x,y,z))
700                     h->RemoveEnforcedVertex(x,y,z);
701             }
702 //        }
703     }
704     
705     TEnforcedVertexValues::const_iterator it;
706     for(it = h_data.myEnforcedVertices.begin() ; it != h_data.myEnforcedVertices.end(); it++ ) {
707       double x = it->at(0);
708       double y = it->at(1);
709       double z = it->at(2);
710       double size = it->at(3);
711       MESSAGE("(" << x   << ", "
712                        << y   << ", "
713                        << z   << ") = "
714                        << size  );
715       double mySize;
716       try {
717         mySize = h->GetEnforcedVertex(x,y,z);
718         MESSAGE("Old size: " << mySize);
719         if (mySize != size) {
720           MESSAGE("Setting new size: " << size);
721           h->SetEnforcedVertex(x,y,z,size);
722         }
723       }
724       catch (...) {
725         MESSAGE("Setting new size: " << size);
726         h->SetEnforcedVertex(x,y,z,size);
727       }
728     }
729   }
730   catch ( const SALOME::SALOME_Exception& ex )
731   {
732     SalomeApp_Tools::QtCatchCorbaException( ex );
733     ok = false;
734   }
735   return ok;
736 }
737
738 bool GHS3DPluginGUI_HypothesisCreator::readParamsFromWidgets( GHS3DHypothesisData& h_data ) const
739 {
740   MESSAGE("GHS3DPluginGUI_HypothesisCreator::readParamsFromWidgets");
741   h_data.myName                       = myName ? myName->text() : "";
742   h_data.myToMeshHoles                = myToMeshHolesCheck->isChecked();
743   h_data.myMaximumMemory              = myMaximumMemoryCheck->isChecked() ? myMaximumMemorySpin->value() : -1;
744   h_data.myInitialMemory              = myInitialMemoryCheck->isChecked() ? myInitialMemorySpin->value() : -1;
745   h_data.myOptimizationLevel          = myOptimizationLevelCombo->currentIndex();
746   h_data.myKeepFiles                  = myKeepFiles->isChecked();
747   h_data.myWorkingDir                 = myWorkingDir->text().trimmed();
748   h_data.myVerboseLevel               = myVerboseLevelSpin->value();
749   h_data.myToCreateNewNodes           = myToCreateNewNodesCheck->isChecked();
750   h_data.myRemoveInitialCentralPoint  = myRemoveInitialCentralPointCheck->isChecked();
751   h_data.myBoundaryRecovery           = myBoundaryRecoveryCheck->isChecked();
752   h_data.myFEMCorrection              = myFEMCorrectionCheck->isChecked();
753   h_data.myTextOption                 = myTextOption->text();
754   h_data.myEnforcedVertices.clear();
755
756   for (int i=0 ; i<mySmpModel->rowCount() ; i++) {
757     GHS3DEnforcedVertex myVertex;
758     myVertex.push_back(mySmpModel->data(mySmpModel->index(i,ENF_VER_X_COLUMN)).toDouble());
759     myVertex.push_back(mySmpModel->data(mySmpModel->index(i,ENF_VER_Y_COLUMN)).toDouble());
760     myVertex.push_back(mySmpModel->data(mySmpModel->index(i,ENF_VER_Z_COLUMN)).toDouble());
761     myVertex.push_back(mySmpModel->data(mySmpModel->index(i,ENF_VER_SIZE_COLUMN)).toDouble());
762     MESSAGE("Add new enforced vertex (" << myVertex[0] << ", "
763                                              << myVertex[1] << ", "
764                                              << myVertex[2] << ") = "
765                                              << myVertex[3]);
766     h_data.myEnforcedVertices.push_back(myVertex);
767   }
768
769   return true;
770 }
771
772 QString GHS3DPluginGUI_HypothesisCreator::caption() const
773 {
774   return tr( "GHS3D_TITLE" );
775 }
776
777 QPixmap GHS3DPluginGUI_HypothesisCreator::icon() const
778 {
779   return SUIT_Session::session()->resourceMgr()->loadPixmap( "GHS3DPlugin", tr( "ICON_DLG_GHS3D_PARAMETERS" ) );
780 }
781
782 QString GHS3DPluginGUI_HypothesisCreator::type() const
783 {
784   return tr( "GHS3D_HYPOTHESIS" );
785 }
786
787 QString GHS3DPluginGUI_HypothesisCreator::helpPage() const
788 {
789   return "ghs3d_hypo_page.html";
790 }
791
792 //=============================================================================
793 /*! GetHypothesisCreator
794  *
795  */
796 //=============================================================================
797 extern "C"
798 {
799   GHS3DPLUGINGUI_EXPORT
800   SMESHGUI_GenericHypothesisCreator* GetHypothesisCreator( const QString& aHypType )
801   {
802     if ( aHypType == "GHS3D_Parameters" )
803       return new GHS3DPluginGUI_HypothesisCreator( aHypType );
804     return 0;
805   }
806 }