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