]> SALOME platform Git repositories - plugins/ghs3dplugin.git/blob - src/GUI/GHS3DPluginGUI_HypothesisCreator.cxx
Salome HOME
4212f79facb3235f3c80cd23cc9e6057007b0897
[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 <QtxComboBox.h>
36 #include <SUIT_MessageBox.h>
37 #include <SUIT_Session.h>
38 #include <SUIT_FileDlg.h>
39 #include <SalomeApp_Tools.h>
40
41 #include <qlabel.h>
42 #include <qgroupbox.h>
43 #include <qframe.h>
44 #include <qlayout.h>
45 #include <qlineedit.h>
46 #include <qcheckbox.h>
47 #include <qpixmap.h>
48 #include <qtabbar.h>
49 #include <qspinbox.h>
50 #include <qpushbutton.h>
51 #include <qfiledialog.h>
52 #include <qfileinfo.h>
53 //#include <qapplication.h>
54
55 enum {
56   STD_TAB = 0,
57   ADV_TAB
58 };
59
60 namespace {
61
62 #ifndef WIN32
63 #include <sys/sysinfo.h>
64 #endif
65
66   int maxAvailableMemory()
67   {
68 #ifndef WIN32
69     struct sysinfo si;
70     int err = sysinfo( &si );
71     if ( err == 0 ) {
72       int totMB =
73         si.totalram * si.mem_unit / 1024 / 1024 +
74         si.totalswap * si.mem_unit / 1024 / 1024 ;
75       return (int) ( 0.7 * totMB );
76     }
77 #endif
78     return 100000;
79   }
80 }
81
82 GHS3DPluginGUI_HypothesisCreator::GHS3DPluginGUI_HypothesisCreator( const QString& theHypType )
83 : SMESHGUI_GenericHypothesisCreator( theHypType )
84 {
85 }
86
87 GHS3DPluginGUI_HypothesisCreator::~GHS3DPluginGUI_HypothesisCreator()
88 {
89 }
90
91 QFrame* GHS3DPluginGUI_HypothesisCreator::buildFrame()
92 {
93   QFrame* fr = new QFrame( 0, "myframe" );
94   QVBoxLayout* lay = new QVBoxLayout( fr, 5, 0 );
95
96   // tab
97   QTabBar* tab = new QTabBar( fr, "QTabBar");
98   tab->setShape( QTabBar::RoundedAbove );
99   tab->insertTab( new QTab( tr( "SMESH_ARGUMENTS")), STD_TAB);
100   tab->insertTab( new QTab( tr( "GHS3D_ADV_ARGS")), ADV_TAB);
101   lay->addWidget( tab );
102
103   // basic parameters
104   myStdGroup = new QGroupBox( 2, Qt::Horizontal, fr, "myStdGroup" );
105   myStdGroup->layout()->setSpacing( 6 );
106   myStdGroup->layout()->setMargin( 11 );
107   lay->addWidget( myStdGroup );
108
109   myName = 0;
110   if( isCreation() )
111   {
112     new QLabel( tr( "SMESH_NAME" ), myStdGroup );
113     myName = new QLineEdit( myStdGroup );
114   }
115
116   myToMeshHolesCheck = new QCheckBox( tr( "GHS3D_TO_MESH_HOLES" ), myStdGroup );
117   myStdGroup->addSpace(0);
118
119   new QLabel( tr( "GHS3D_OPTIMIZATIOL_LEVEL" ), myStdGroup );
120   myOptimizationLevelCombo = new QtxComboBox( myStdGroup );
121   QStringList types;
122   types.append( QObject::tr( "LEVEL_NONE" ) );
123   types.append( QObject::tr( "LEVEL_LIGHT" ) );
124   types.append( QObject::tr( "LEVEL_MEDIUM" ) );
125   types.append( QObject::tr( "LEVEL_STRONG" ) );
126   myOptimizationLevelCombo->insertStringList( types );
127
128   // advanced parameters
129   myAdvGroup = new QGroupBox( 3, Qt::Horizontal, fr, "myAdvGroup" );
130   lay->addWidget( myAdvGroup );
131   
132   myMaximumMemoryCheck = new QCheckBox( tr("MAX_MEMORY_SIZE"), myAdvGroup );
133
134   myMaximumMemorySpin = new QSpinBox( myAdvGroup );
135   myMaximumMemorySpin->setMinValue( 20 );
136   myMaximumMemorySpin->setMaxValue( maxAvailableMemory() );
137   myMaximumMemorySpin->setLineStep( 10 );
138   new QLabel( tr("MEGABYTE"), myAdvGroup);
139
140   myInitialMemoryCheck = new QCheckBox( tr("INIT_MEMORY_SIZE"), myAdvGroup );
141
142   myInitialMemorySpin = new QSpinBox( myAdvGroup );
143   myInitialMemorySpin->setMinValue( 20 );
144   myInitialMemorySpin->setMaxValue( maxAvailableMemory() );
145   myInitialMemorySpin->setLineStep( 10 );
146   new QLabel( tr("MEGABYTE"), myAdvGroup);
147
148   new QLabel( tr( "WORKING_DIR" ), myAdvGroup );
149   QPushButton* dirBtn = new QPushButton( tr( "SELECT_DIR"), myAdvGroup, "dirBtn");
150   myWorkingDir = new QLineEdit( myAdvGroup, "myWorkingDir");
151   myWorkingDir->setReadOnly( true );
152   
153   myKeepFiles = new QCheckBox( tr( "KEEP_WORKING_FILES" ), myAdvGroup );
154   
155   connect( tab,                  SIGNAL( selected(int) ), this, SLOT( onTabSelected(int) ) );
156   connect( myMaximumMemoryCheck, SIGNAL( toggled(bool) ), this, SLOT( onMemCheckToggled(bool) ));
157   connect( myInitialMemoryCheck, SIGNAL( toggled(bool) ), this, SLOT( onMemCheckToggled(bool) ));
158   connect( dirBtn,               SIGNAL( clicked() ),     this, SLOT( onDirBtnClicked() ) );
159   
160   return fr;
161 }
162
163 void GHS3DPluginGUI_HypothesisCreator::onTabSelected(int tab)
164 {
165   if ( tab == STD_TAB ) {
166     myAdvGroup->hide();
167     myStdGroup->show();
168   }
169   else {
170     myStdGroup->hide();
171     myAdvGroup->show();
172   }
173 //   qApp->processEvents();
174   dlg()->adjustSize();
175 }
176
177 void GHS3DPluginGUI_HypothesisCreator::onMemCheckToggled(bool on)
178 {
179   if ( sender() == myMaximumMemoryCheck )
180     myMaximumMemorySpin->setEnabled( on );
181   else
182     myInitialMemorySpin->setEnabled( on );
183 }
184
185 void GHS3DPluginGUI_HypothesisCreator::onDirBtnClicked()
186 {
187   QString dir = SUIT_FileDlg::getExistingDirectory(dlg(),QString::null, QString::null );
188   if ( dir )
189     myWorkingDir->setText( dir );
190 }
191
192
193 bool GHS3DPluginGUI_HypothesisCreator::checkParams() const
194 {
195   if ( !QFileInfo( myWorkingDir->text() ).isWritable() ) {
196     SUIT_MessageBox::warn1(dlg(),
197                            QObject::tr("SMESH_WRN_WARNING"),
198                            QObject::tr("GHS3D_PERMISSION_DENIED"),
199                            QObject::tr("SMESH_BUT_OK"));
200     return false;
201   }
202   return true;
203 }
204
205 void GHS3DPluginGUI_HypothesisCreator::retrieveParams() const
206 {
207   GHS3DHypothesisData data;
208   readParamsFromHypo( data );
209
210   if( myName )
211     myName->setText( data.myName );
212   
213   myToMeshHolesCheck      ->setChecked    ( data.myToMeshHoles );
214   myOptimizationLevelCombo->setCurrentItem( data.myOptimizationLevel );
215   myMaximumMemoryCheck    ->setChecked    ( data.myMaximumMemory > 0 );
216   myMaximumMemorySpin     ->setValue      ( std::max( data.myMaximumMemory, myMaximumMemorySpin->minValue() ));
217   myInitialMemoryCheck    ->setChecked    ( data.myInitialMemory > 0 );
218   myInitialMemorySpin     ->setValue      ( std::max( data.myInitialMemory, myInitialMemorySpin->minValue() ));
219   myWorkingDir            ->setText       ( data.myWorkingDir );
220   myKeepFiles             ->setChecked    ( data.myKeepFiles );
221
222   myMaximumMemorySpin->setEnabled( myMaximumMemoryCheck->isChecked() );
223   myInitialMemorySpin->setEnabled( myInitialMemoryCheck->isChecked() );
224 }
225
226 QString GHS3DPluginGUI_HypothesisCreator::storeParams() const
227 {
228   GHS3DHypothesisData data;
229   readParamsFromWidgets( data );
230   storeParamsToHypo( data );
231   
232   QString valStr = " -c " + QString::number( !data.myToMeshHoles );
233   if ( data.myOptimizationLevel >= 0 && data.myOptimizationLevel < 4 ) {
234     char* level[] = { "none" , "light" , "standard" , "strong" };
235     valStr += " -o ";
236     valStr += level[ data.myOptimizationLevel ];
237   }
238   
239   return valStr;
240 }
241
242 bool GHS3DPluginGUI_HypothesisCreator::readParamsFromHypo( GHS3DHypothesisData& h_data ) const
243 {
244   GHS3DPlugin::GHS3DPlugin_Hypothesis_var h =
245     GHS3DPlugin::GHS3DPlugin_Hypothesis::_narrow( initParamsHypothesis() );
246
247   HypothesisData* data = SMESH::GetHypothesisData( hypType() );
248   h_data.myName = isCreation() && data ? hypName() : "";
249
250   h_data.myToMeshHoles       = h->GetToMeshHoles();
251   h_data.myMaximumMemory     = h->GetMaximumMemory();
252   h_data.myInitialMemory     = h->GetInitialMemory();
253   h_data.myInitialMemory     = h->GetInitialMemory();
254   h_data.myOptimizationLevel = h->GetOptimizationLevel();
255   h_data.myKeepFiles         = h->GetKeepFiles();
256   h_data.myWorkingDir        = h->GetWorkingDirectory();
257   
258   return true;
259 }
260
261 bool GHS3DPluginGUI_HypothesisCreator::storeParamsToHypo( const GHS3DHypothesisData& h_data ) const
262 {
263   GHS3DPlugin::GHS3DPlugin_Hypothesis_var h =
264     GHS3DPlugin::GHS3DPlugin_Hypothesis::_narrow( hypothesis() );
265
266   bool ok = true;
267   try
268   {
269     if( isCreation() )
270       SMESH::SetName( SMESH::FindSObject( h ), h_data.myName.latin1() );
271
272     if ( h->GetToMeshHoles() != h_data.myToMeshHoles ) // avoid duplication of DumpPython commands
273       h->SetToMeshHoles      ( h_data.myToMeshHoles       );
274     if ( h->GetMaximumMemory() != h_data.myMaximumMemory )
275       h->SetMaximumMemory    ( h_data.myMaximumMemory     );
276     if ( h->GetInitialMemory() != h_data.myInitialMemory )
277       h->SetInitialMemory    ( h_data.myInitialMemory     );
278     if ( h->GetInitialMemory() != h_data.myInitialMemory )
279       h->SetInitialMemory    ( h_data.myInitialMemory     );
280     if ( h->GetOptimizationLevel() != h_data.myOptimizationLevel )
281       h->SetOptimizationLevel( h_data.myOptimizationLevel );
282     if ( h->GetKeepFiles() != h_data.myKeepFiles )
283       h->SetKeepFiles        ( h_data.myKeepFiles         );
284     if (h->GetWorkingDirectory() != h_data.myWorkingDir )
285       h->SetWorkingDirectory ( h_data.myWorkingDir        );
286   }
287   catch(const SALOME::SALOME_Exception& ex)
288   {
289     SalomeApp_Tools::QtCatchCorbaException(ex);
290     ok = false;
291   }
292   return ok;
293 }
294
295 bool GHS3DPluginGUI_HypothesisCreator::readParamsFromWidgets( GHS3DHypothesisData& h_data ) const
296 {
297   h_data.myName              = myName ? myName->text() : "";
298   h_data.myToMeshHoles       = myToMeshHolesCheck->isChecked();
299   h_data.myMaximumMemory     = myMaximumMemoryCheck->isChecked() ? myMaximumMemorySpin->value() : -1;
300   h_data.myInitialMemory     = myInitialMemoryCheck->isChecked() ? myInitialMemorySpin->value() : -1;
301   h_data.myOptimizationLevel = myOptimizationLevelCombo->currentItem();
302   h_data.myKeepFiles         = myKeepFiles->isChecked();
303   h_data.myWorkingDir        = myWorkingDir->text();
304
305   return true;
306 }
307
308 QString GHS3DPluginGUI_HypothesisCreator::caption() const
309 {
310   return tr( "GHS3D_TITLE" );
311 }
312
313 QPixmap GHS3DPluginGUI_HypothesisCreator::icon() const
314 {
315   QString hypIconName = tr( QString("ICON_DLG_GHS3D_PARAMETERS" ));
316   return SUIT_Session::session()->resourceMgr()->loadPixmap( "GHS3DPlugin", hypIconName );
317 }
318
319 QString GHS3DPluginGUI_HypothesisCreator::type() const
320 {
321   return tr( "GHS3D_HYPOTHESIS" );
322 }
323
324 QString GHS3DPluginGUI_HypothesisCreator::helpPage() const
325 {
326   return "ghs3d_hypo_page.html";
327 }
328
329 //=============================================================================
330 /*! GetHypothesisCreator
331  *
332  */
333 //=============================================================================
334 extern "C"
335 {
336   GHS3DPLUGIN_EXPORT
337   SMESHGUI_GenericHypothesisCreator* GetHypothesisCreator( const QString& aHypType )
338   {
339     if( aHypType=="GHS3D_Parameters" )
340       return new GHS3DPluginGUI_HypothesisCreator( aHypType );
341     return 0;
342   }
343 }