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