]> SALOME platform Git repositories - plugins/ghs3dplugin.git/blob - src/GUI/GHS3DPluginGUI_HypothesisCreator.cxx
Salome HOME
773aad964197614ba2171bb5ae45977a5bedb454
[plugins/ghs3dplugin.git] / src / GUI / GHS3DPluginGUI_HypothesisCreator.cxx
1 //  GHS3DPlugin GUI: GUI for plugged-in mesher GHS3DPlugin
2 //
3 //  Copyright (C) 2003  CEA
4 // 
5 //  This library is free software; you can redistribute it and/or 
6 //  modify it under the terms of the GNU Lesser General Public 
7 //  License as published by the Free Software Foundation; either 
8 //  version 2.1 of the License. 
9 // 
10 //  This library is distributed in the hope that it will be useful, 
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
13 //  Lesser General Public License for more details. 
14 // 
15 //  You should have received a copy of the GNU Lesser General Public 
16 //  License along with this library; if not, write to the Free Software 
17 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
18 // 
19 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 //
21 //
22 //
23 //  File   : GHS3DPluginGUI_HypothesisCreator.cxx
24 //  Author : Michael Zorin
25 //  Module : GHS3DPlugin
26 //  $Header: 
27
28 #include "GHS3DPluginGUI_HypothesisCreator.h"
29
30 #include <SMESHGUI_Utils.h>
31 #include <SMESHGUI_HypothesesUtils.h>
32
33 #include CORBA_SERVER_HEADER(GHS3DPlugin_Algorithm)
34
35 #include <SUIT_MessageBox.h>
36 #include <SUIT_Session.h>
37 #include <SUIT_FileDlg.h>
38 #include <SUIT_ResourceMgr.h>
39 #include <SalomeApp_Tools.h>
40
41 #include <QLabel>
42 #include <QComboBox>
43 #include <QFrame>
44 #include <QGridLayout>
45 #include <QVBoxLayout>
46 #include <QLineEdit>
47 #include <QCheckBox>
48 #include <QTabWidget>
49 #include <QSpinBox>
50 #include <QPushButton>
51 #include <QFileInfo>
52
53 enum {
54   STD_TAB = 0,
55   ADV_TAB
56 };
57
58 namespace {
59
60 #ifndef WIN32
61 #include <sys/sysinfo.h>
62 #endif
63
64   int maxAvailableMemory()
65   {
66 #ifndef WIN32
67     struct sysinfo si;
68     int err = sysinfo( &si );
69     if ( err == 0 ) {
70       int totMB =
71         si.totalram * si.mem_unit / 1024 / 1024 +
72         si.totalswap * si.mem_unit / 1024 / 1024 ;
73       return (int) ( 0.7 * totMB );
74     }
75 #endif
76     return 100000;
77   }
78 }
79
80 GHS3DPluginGUI_HypothesisCreator::GHS3DPluginGUI_HypothesisCreator( const QString& theHypType )
81 : SMESHGUI_GenericHypothesisCreator( theHypType )
82 {
83 }
84
85 GHS3DPluginGUI_HypothesisCreator::~GHS3DPluginGUI_HypothesisCreator()
86 {
87 }
88
89 QFrame* GHS3DPluginGUI_HypothesisCreator::buildFrame()
90 {
91   QFrame* fr = new QFrame( 0 );
92   QVBoxLayout* lay = new QVBoxLayout( fr );
93   lay->setMargin( 5 );
94   lay->setSpacing( 0 );
95
96   // tab
97   QTabWidget* tab = new QTabWidget( fr );
98   tab->setTabShape( QTabWidget::Rounded );
99   tab->setTabPosition( QTabWidget::North );
100   lay->addWidget( tab );
101
102   // basic parameters
103   myStdGroup = new QWidget();
104   QGridLayout* aStdLayout = new QGridLayout( myStdGroup );
105   aStdLayout->setSpacing( 6 );
106   aStdLayout->setMargin( 11 );
107
108   int row = 0;
109   myName = 0;
110   if( isCreation() )
111   {
112     aStdLayout->addWidget( new QLabel( tr( "SMESH_NAME" ), myStdGroup ), row, 0, 1, 1 );
113     myName = new QLineEdit( myStdGroup );
114     aStdLayout->addWidget( myName, row++, 1, 1, 1 );
115   }
116
117   myToMeshHolesCheck = new QCheckBox( tr( "GHS3D_TO_MESH_HOLES" ), myStdGroup );
118   aStdLayout->addWidget( myToMeshHolesCheck, row++, 0, 1, 2 );
119
120   aStdLayout->addWidget( new QLabel( tr( "GHS3D_OPTIMIZATIOL_LEVEL" ), myStdGroup ), row, 0 );
121   myOptimizationLevelCombo = new QComboBox( myStdGroup );
122   aStdLayout->addWidget( myOptimizationLevelCombo, row++, 1, 1, 1 );
123
124   QStringList types;
125   types << tr( "LEVEL_NONE" ) << tr( "LEVEL_LIGHT" ) << tr( "LEVEL_MEDIUM" ) << tr( "LEVEL_STRONG" );
126   myOptimizationLevelCombo->addItems( types );
127
128   aStdLayout->setRowStretch( row, 5 );
129
130   // advanced parameters
131   myAdvGroup = new QWidget();
132   QGridLayout* anAdvLayout = new QGridLayout( myAdvGroup );
133   anAdvLayout->setSpacing( 6 );
134   anAdvLayout->setMargin( 11 );
135   
136   myMaximumMemoryCheck = new QCheckBox( tr( "MAX_MEMORY_SIZE" ), myAdvGroup );
137   myMaximumMemorySpin = new QSpinBox( myAdvGroup );
138   myMaximumMemorySpin->setMinimum( 1 );
139   myMaximumMemorySpin->setMaximum( maxAvailableMemory() );
140   myMaximumMemorySpin->setSingleStep( 10 );
141   QLabel* aMegabyteLabel = new QLabel( tr( "MEGABYTE" ), myAdvGroup );
142
143   myInitialMemoryCheck = new QCheckBox( tr( "INIT_MEMORY_SIZE" ), myAdvGroup );
144   myInitialMemorySpin = new QSpinBox( myAdvGroup );
145   myInitialMemorySpin->setMinimum( 1 );
146   myInitialMemorySpin->setMaximum( maxAvailableMemory() );
147   myInitialMemorySpin->setSingleStep( 10 );
148   QLabel* aMegabyteLabel2 = new QLabel( tr( "MEGABYTE" ), myAdvGroup );
149
150   QLabel* aWorkinDirLabel = new QLabel( tr( "WORKING_DIR" ), myAdvGroup );
151   myWorkingDir = new QLineEdit( myAdvGroup );
152   //myWorkingDir->setReadOnly( true );
153   QPushButton* dirBtn = new QPushButton( tr( "SELECT_DIR" ), myAdvGroup );
154   dirBtn->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
155   
156   myKeepFiles = new QCheckBox( tr( "KEEP_WORKING_FILES" ), myAdvGroup );
157
158   QLabel* aVerboseLevelLabel = new QLabel( tr( "VERBOSE_LEVEL" ), myAdvGroup );
159   myVerboseLevelSpin = new QSpinBox( myAdvGroup );
160   myVerboseLevelSpin->setMinimum( 0 );
161   myVerboseLevelSpin->setMaximum( 10 );
162   myVerboseLevelSpin->setSingleStep( 1 );
163
164   myToCreateNewNodesCheck = new QCheckBox( tr( "TO_ADD_NODES" ), myAdvGroup );
165   
166   myBoundaryRecoveryCheck = new QCheckBox( tr( "RECOVERY_VERSION" ), myAdvGroup );
167
168   QLabel* aTextOptionLabel = new QLabel( tr( "TEXT_OPTION" ), myAdvGroup );
169   myTextOption = new QLineEdit( myAdvGroup );
170
171   anAdvLayout->addWidget( myMaximumMemoryCheck,    0, 0, 1, 1 );
172   anAdvLayout->addWidget( myMaximumMemorySpin,     0, 1, 1, 1 );
173   anAdvLayout->addWidget( aMegabyteLabel,          0, 2, 1, 1 );
174   anAdvLayout->addWidget( myInitialMemoryCheck,    1, 0, 1, 1 );
175   anAdvLayout->addWidget( myInitialMemorySpin,     1, 1, 1, 1 );
176   anAdvLayout->addWidget( aMegabyteLabel2,         1, 2, 1, 1 );
177   anAdvLayout->addWidget( aWorkinDirLabel,         2, 0, 1, 1 );
178   anAdvLayout->addWidget( myWorkingDir,            2, 1, 1, 2 );
179   anAdvLayout->addWidget( dirBtn,                  2, 3, 1, 1 );
180   anAdvLayout->addWidget( myKeepFiles,             3, 0, 1, 4 );
181   anAdvLayout->addWidget( aVerboseLevelLabel,      4, 0, 1, 1 );
182   anAdvLayout->addWidget( myVerboseLevelSpin,      4, 1, 1, 1 );
183   anAdvLayout->addWidget( myToCreateNewNodesCheck, 5, 0, 1, 4 );
184   anAdvLayout->addWidget( myBoundaryRecoveryCheck, 6, 0, 1, 4 );
185   anAdvLayout->addWidget( aTextOptionLabel,        7, 0, 1, 1 );
186   anAdvLayout->addWidget( myTextOption,            7, 1, 1, 2 );
187
188   // add tabs
189   tab->insertTab( STD_TAB, myStdGroup, tr( "SMESH_ARGUMENTS" ) );
190   tab->insertTab( ADV_TAB, myAdvGroup, tr( "GHS3D_ADV_ARGS" ) );
191   tab->setCurrentIndex( STD_TAB );
192
193   // connections
194   connect( myMaximumMemoryCheck,    SIGNAL( toggled( bool ) ), this, SLOT( updateWidgets() ) );
195   connect( myInitialMemoryCheck,    SIGNAL( toggled( bool ) ), this, SLOT( updateWidgets() ) );
196   connect( myBoundaryRecoveryCheck, SIGNAL( toggled( bool ) ), this, SLOT( updateWidgets() ) );
197   connect( dirBtn,                  SIGNAL( clicked() ),       this, SLOT( onDirBtnClicked() ) );
198   
199   return fr;
200 }
201
202 void GHS3DPluginGUI_HypothesisCreator::onDirBtnClicked()
203 {
204   QString dir = SUIT_FileDlg::getExistingDirectory( dlg(), myWorkingDir->text(), QString() );
205   if ( !dir.isEmpty() )
206     myWorkingDir->setText( dir );
207 }
208
209 void GHS3DPluginGUI_HypothesisCreator::updateWidgets()
210 {
211   myMaximumMemorySpin->setEnabled( myMaximumMemoryCheck->isChecked() );
212   myInitialMemoryCheck->setEnabled( !myBoundaryRecoveryCheck->isChecked() );
213   myInitialMemorySpin->setEnabled( myInitialMemoryCheck->isChecked() && !myBoundaryRecoveryCheck->isChecked() );
214   myOptimizationLevelCombo->setEnabled( !myBoundaryRecoveryCheck->isChecked() );
215 }
216
217 bool GHS3DPluginGUI_HypothesisCreator::checkParams() const
218 {
219   if ( !QFileInfo( myWorkingDir->text().trimmed() ).isWritable() ) {
220     SUIT_MessageBox::warning( dlg(),
221                               tr( "SMESH_WRN_WARNING" ),
222                               tr( "GHS3D_PERMISSION_DENIED" ) );
223     return false;
224   }
225   return true;
226 }
227
228 void GHS3DPluginGUI_HypothesisCreator::retrieveParams() const
229 {
230   GHS3DHypothesisData data;
231   readParamsFromHypo( data );
232
233   if ( myName )
234     myName->setText( data.myName );
235   
236   myToMeshHolesCheck      ->setChecked    ( data.myToMeshHoles );
237   myOptimizationLevelCombo->setCurrentIndex( data.myOptimizationLevel );
238   myMaximumMemoryCheck    ->setChecked    ( data.myMaximumMemory > 0 );
239   myMaximumMemorySpin     ->setValue      ( qMax( data.myMaximumMemory,
240                                                   myMaximumMemorySpin->minimum() ));
241   myInitialMemoryCheck    ->setChecked    ( data.myInitialMemory > 0 );
242   myInitialMemorySpin     ->setValue      ( qMax( data.myInitialMemory,
243                                                   myInitialMemorySpin->minimum() ));
244   myWorkingDir            ->setText       ( data.myWorkingDir );
245   myKeepFiles             ->setChecked    ( data.myKeepFiles );
246   myVerboseLevelSpin      ->setValue      ( data.myVerboseLevel );
247   myToCreateNewNodesCheck ->setChecked    ( data.myToCreateNewNodes );
248   myBoundaryRecoveryCheck ->setChecked    ( data.myBoundaryRecovery );
249   myTextOption            ->setText       ( data.myTextOption );
250   
251   GHS3DPluginGUI_HypothesisCreator* that = (GHS3DPluginGUI_HypothesisCreator*)this;
252   that->updateWidgets();
253 }
254
255 QString GHS3DPluginGUI_HypothesisCreator::storeParams() const
256 {
257   GHS3DHypothesisData data;
258   readParamsFromWidgets( data );
259   storeParamsToHypo( data );
260   
261   QString valStr = "";
262
263   if ( !data.myBoundaryRecovery )
264     valStr = "-c " + QString::number( !data.myToMeshHoles );
265
266   if ( data.myOptimizationLevel >= 0 && data.myOptimizationLevel < 4 && !data.myBoundaryRecovery) {
267     char* level[] = { "none" , "light" , "standard" , "strong" };
268     valStr += " -o ";
269     valStr += level[ data.myOptimizationLevel ];
270   }
271   if ( data.myMaximumMemory > 0 ) {
272     valStr += " -m ";
273     valStr += QString::number( data.myMaximumMemory );
274   }
275   if ( data.myInitialMemory > 0 && !data.myBoundaryRecovery ) {
276     valStr += " -M ";
277     valStr += QString::number( data.myInitialMemory );
278   }
279   valStr += " -v ";
280   valStr += QString::number( data.myVerboseLevel );
281
282   if ( !data.myToCreateNewNodes )
283     valStr += " -p0";
284
285   if ( data.myBoundaryRecovery )
286     valStr += " -C";
287
288   valStr += " ";
289   valStr += data.myTextOption;
290
291   return valStr;
292 }
293
294 bool GHS3DPluginGUI_HypothesisCreator::readParamsFromHypo( GHS3DHypothesisData& h_data ) const
295 {
296   GHS3DPlugin::GHS3DPlugin_Hypothesis_var h =
297     GHS3DPlugin::GHS3DPlugin_Hypothesis::_narrow( initParamsHypothesis() );
298
299   HypothesisData* data = SMESH::GetHypothesisData( hypType() );
300   h_data.myName = isCreation() && data ? hypName() : "";
301
302   h_data.myToMeshHoles       = h->GetToMeshHoles();
303   h_data.myMaximumMemory     = h->GetMaximumMemory();
304   h_data.myInitialMemory     = h->GetInitialMemory();
305   h_data.myInitialMemory     = h->GetInitialMemory();
306   h_data.myOptimizationLevel = h->GetOptimizationLevel();
307   h_data.myKeepFiles         = h->GetKeepFiles();
308   h_data.myWorkingDir        = h->GetWorkingDirectory();
309   h_data.myVerboseLevel      = h->GetVerboseLevel();
310   h_data.myToCreateNewNodes  = h->GetToCreateNewNodes();
311   h_data.myBoundaryRecovery  = h->GetToUseBoundaryRecoveryVersion();
312   h_data.myTextOption        = h->GetTextOption();
313   
314   return true;
315 }
316
317 bool GHS3DPluginGUI_HypothesisCreator::storeParamsToHypo( const GHS3DHypothesisData& h_data ) const
318 {
319   GHS3DPlugin::GHS3DPlugin_Hypothesis_var h =
320     GHS3DPlugin::GHS3DPlugin_Hypothesis::_narrow( hypothesis() );
321
322   bool ok = true;
323   try
324   {
325     if( isCreation() )
326       SMESH::SetName( SMESH::FindSObject( h ), h_data.myName.toLatin1().constData() );
327
328     if ( h->GetToMeshHoles() != h_data.myToMeshHoles ) // avoid duplication of DumpPython commands
329       h->SetToMeshHoles      ( h_data.myToMeshHoles       );
330     if ( h->GetMaximumMemory() != h_data.myMaximumMemory )
331       h->SetMaximumMemory    ( h_data.myMaximumMemory     );
332     if ( h->GetInitialMemory() != h_data.myInitialMemory )
333       h->SetInitialMemory    ( h_data.myInitialMemory     );
334     if ( h->GetInitialMemory() != h_data.myInitialMemory )
335       h->SetInitialMemory    ( h_data.myInitialMemory     );
336     if ( h->GetOptimizationLevel() != h_data.myOptimizationLevel )
337       h->SetOptimizationLevel( h_data.myOptimizationLevel );
338     if ( h->GetKeepFiles() != h_data.myKeepFiles )
339       h->SetKeepFiles        ( h_data.myKeepFiles         );
340     if ( h->GetWorkingDirectory() != h_data.myWorkingDir )
341       h->SetWorkingDirectory ( h_data.myWorkingDir.toLatin1().constData() );
342     if ( h->GetVerboseLevel() != h_data.myVerboseLevel )
343       h->SetVerboseLevel     ( h_data.myVerboseLevel );
344     if ( h->GetToCreateNewNodes() != h_data.myToCreateNewNodes )
345       h->SetToCreateNewNodes( h_data.myToCreateNewNodes );
346     if ( h->GetToUseBoundaryRecoveryVersion() != h_data.myBoundaryRecovery )
347       h->SetToUseBoundaryRecoveryVersion( h_data.myBoundaryRecovery );
348     if ( h->GetTextOption() != h_data.myTextOption )
349       h->SetTextOption       ( h_data.myTextOption.toLatin1().constData() );
350   }
351   catch ( const SALOME::SALOME_Exception& ex )
352   {
353     SalomeApp_Tools::QtCatchCorbaException( ex );
354     ok = false;
355   }
356   return ok;
357 }
358
359 bool GHS3DPluginGUI_HypothesisCreator::readParamsFromWidgets( GHS3DHypothesisData& h_data ) const
360 {
361   h_data.myName              = myName ? myName->text() : "";
362   h_data.myToMeshHoles       = myToMeshHolesCheck->isChecked();
363   h_data.myMaximumMemory     = myMaximumMemoryCheck->isChecked() ? myMaximumMemorySpin->value() : -1;
364   h_data.myInitialMemory     = myInitialMemoryCheck->isChecked() ? myInitialMemorySpin->value() : -1;
365   h_data.myOptimizationLevel = myOptimizationLevelCombo->currentIndex();
366   h_data.myKeepFiles         = myKeepFiles->isChecked();
367   h_data.myWorkingDir        = myWorkingDir->text().trimmed();
368   h_data.myVerboseLevel      = myVerboseLevelSpin->value();
369   h_data.myToCreateNewNodes  = myToCreateNewNodesCheck->isChecked();
370   h_data.myBoundaryRecovery  = myBoundaryRecoveryCheck->isChecked();
371   h_data.myTextOption        = myTextOption->text();
372
373   return true;
374 }
375
376 QString GHS3DPluginGUI_HypothesisCreator::caption() const
377 {
378   return tr( "GHS3D_TITLE" );
379 }
380
381 QPixmap GHS3DPluginGUI_HypothesisCreator::icon() const
382 {
383   return SUIT_Session::session()->resourceMgr()->loadPixmap( "GHS3DPlugin", tr( "ICON_DLG_GHS3D_PARAMETERS" ) );
384 }
385
386 QString GHS3DPluginGUI_HypothesisCreator::type() const
387 {
388   return tr( "GHS3D_HYPOTHESIS" );
389 }
390
391 QString GHS3DPluginGUI_HypothesisCreator::helpPage() const
392 {
393   return "ghs3d_hypo_page.html";
394 }
395
396 //=============================================================================
397 /*! GetHypothesisCreator
398  *
399  */
400 //=============================================================================
401 extern "C"
402 {
403   GHS3DPLUGIN_EXPORT
404   SMESHGUI_GenericHypothesisCreator* GetHypothesisCreator( const QString& aHypType )
405   {
406     if ( aHypType == "GHS3D_Parameters" )
407       return new GHS3DPluginGUI_HypothesisCreator( aHypType );
408     return 0;
409   }
410 }