Salome HOME
Merge from BR_V5_DEV 16Feb09
[plugins/netgenplugin.git] / src / GUI / NETGENPluginGUI_SimpleCreator.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File   : NETGENPluginGUI_SimpleCreator.cxx
23 // Author : Open CASCADE S.A.S.
24 // SMESH includes
25 //
26 #include "NETGENPluginGUI_SimpleCreator.h"
27
28 #include <SMESHGUI_Utils.h>
29 #include <SMESHGUI_HypothesesUtils.h>
30 #include <SMESHGUI_SpinBox.h>
31 #include <SMESHGUI.h>
32
33 // IDL includes
34 #include CORBA_SERVER_HEADER(NETGENPlugin_Algorithm)
35
36 #include <SUIT_Session.h>
37 #include <SUIT_ResourceMgr.h>
38
39 // SALOME GUI includes
40 #include <SalomeApp_Tools.h>
41 #include <SalomeApp_IntSpinBox.h>
42
43 // Qt includes
44 #include <QLabel>
45 #include <QGroupBox>
46 #include <QFrame>
47 #include <QLineEdit>
48 //#include <QButtonGroup>
49 #include <QRadioButton>
50 #include <QGridLayout>
51 #include <QVBoxLayout>
52 #include <QApplication>
53 #include <QCheckBox>
54
55 #define SPACING 6
56 #define MARGIN  11
57
58 using namespace NETGENPlugin;
59
60 // copied from StdMeshersGUI_StdHypothesisCreator.cxx
61 const double VALUE_MAX = 1.0e+15, // COORD_MAX
62              VALUE_MAX_2  = VALUE_MAX * VALUE_MAX,
63              VALUE_MAX_3  = VALUE_MAX_2 * VALUE_MAX,
64              VALUE_SMALL = 1.0e-15,
65              VALUE_SMALL_2 = VALUE_SMALL * VALUE_SMALL,
66              VALUE_SMALL_3 = VALUE_SMALL_2 * VALUE_SMALL;
67
68 NETGENPluginGUI_SimpleCreator::NETGENPluginGUI_SimpleCreator(const QString& theHypType)
69 : SMESHGUI_GenericHypothesisCreator( theHypType ),
70   myName(0),
71   myNbSeg(0),
72   myLength(0),
73   myNbSegRadioBut(0),
74   myLengthRadioBut(0),
75   myLenFromEdgesCheckBox(0),
76   myArea(0),
77   myLenFromFacesCheckBox(0),
78   myVolume(0)
79 {
80 }
81
82 NETGENPluginGUI_SimpleCreator::~NETGENPluginGUI_SimpleCreator()
83 {
84 }
85
86 bool NETGENPluginGUI_SimpleCreator::checkParams(QString& msg) const
87 {
88   bool result = true;
89   result = myNbSeg->isValid(msg,true) && result;
90   result = myLength->isValid(msg,true) && result;
91   result = myArea->isValid(msg,true) && result;
92   if (myVolume)
93     result = myVolume->isValid(msg,true) && result;
94   
95   return result;
96 }
97
98 QFrame* NETGENPluginGUI_SimpleCreator::buildFrame()
99 {
100   QFrame* fr = new QFrame();
101
102   QVBoxLayout* lay = new QVBoxLayout( fr );
103   lay->setMargin( 0 );
104   lay->setSpacing( 0 );
105
106   QGroupBox* argGroup = new QGroupBox( tr( "SMESH_ARGUMENTS" ), fr );
107   lay->addWidget( argGroup );
108
109   QGridLayout* argLay = new QGridLayout( argGroup );
110   argLay->setSpacing( SPACING );
111   argLay->setMargin( MARGIN );
112   argLay->setColumnStretch( 0, 0 );
113   argLay->setColumnStretch( 1, 1 );
114   int argRow = 0;
115
116   // Name
117   if( isCreation() ) {
118     myName = new QLineEdit( argGroup );
119     argLay->addWidget( new QLabel( tr( "SMESH_NAME" ), argGroup ), argRow, 0 );
120     argLay->addWidget( myName, argRow, 1 );
121     argRow++;
122   }
123
124   QGroupBox* dimGroup;
125   QGridLayout* dimLay;
126   int dimRow;
127
128   // 1D params group
129
130   dimGroup = new QGroupBox( tr( "NG_1D" ), argGroup );
131   argLay->addWidget( dimGroup, argRow, 0, 1, 2 );
132   argRow++;
133
134   dimLay = new QGridLayout( dimGroup );
135   dimLay->setSpacing( SPACING );
136   dimLay->setMargin( MARGIN );
137   dimLay->setColumnStretch( 0, 0 );
138   dimLay->setColumnStretch( 1, 1 );
139   dimRow = 0;
140
141   // *  number of segments
142   myNbSegRadioBut  = new QRadioButton( tr( "SMESH_NB_SEGMENTS_HYPOTHESIS"  ), dimGroup );
143   myNbSeg = new SalomeApp_IntSpinBox( dimGroup );
144   myNbSeg->setMinimum( 1 );
145   myNbSeg->setMaximum( 9999 );
146   myNbSeg->setValue( 1 );
147   dimLay->addWidget( myNbSegRadioBut, dimRow, 0 );
148   dimLay->addWidget( myNbSeg, dimRow, 1 );
149   dimRow++;
150
151   // * local length
152   myLengthRadioBut = new QRadioButton( tr( "SMESH_LOCAL_LENGTH_HYPOTHESIS" ), dimGroup );
153   myLength = new SMESHGUI_SpinBox( dimGroup );
154   myLength->RangeStepAndValidator( VALUE_SMALL, VALUE_MAX, 0.1, 6 );
155   myLength->setValue( 1. );
156   dimLay->addWidget( myLengthRadioBut, dimRow, 0 );
157   dimLay->addWidget( myLength, dimRow, 1 );
158   dimRow++;
159
160   // 2D params group
161
162   dimGroup = new QGroupBox( tr( "NG_2D" ), argGroup );
163   argLay->addWidget( dimGroup, argRow, 0, 1, 2 );
164   argRow++;
165
166   dimLay = new QGridLayout( dimGroup );
167   dimLay->setSpacing( SPACING );
168   dimLay->setMargin( MARGIN );
169   dimLay->setColumnStretch( 0, 0 );
170   dimLay->setColumnStretch( 1, 1 );
171   dimRow = 0;
172
173   // *  Length from edges
174   myLenFromEdgesCheckBox = new QCheckBox( tr( "NG_LENGTH_FROM_EDGES" ), dimGroup );
175   dimLay->addWidget( myLenFromEdgesCheckBox, dimRow, 0, 1, 2 );
176   dimRow++;
177
178   // * max area
179   dimLay->addWidget( new QLabel( tr( "SMESH_MAX_ELEMENT_AREA_HYPOTHESIS" ), dimGroup), dimRow, 0);
180   myArea = new SMESHGUI_SpinBox( dimGroup );
181   myArea->RangeStepAndValidator( VALUE_SMALL_2, VALUE_MAX_2, 0.1, 6 );
182   myArea->setValue( 1. );
183   dimLay->addWidget( myArea, dimRow, 1 );
184   dimRow++;
185
186   // 3D params group
187   if ( hypType()=="NETGEN_SimpleParameters_3D" )
188   {
189     dimGroup = new QGroupBox( tr( "NG_3D" ), argGroup );
190     argLay->addWidget( dimGroup, argRow, 0, 1, 2 );
191     argRow++;
192
193     dimLay = new QGridLayout( dimGroup );
194     dimLay->setSpacing( SPACING );
195     dimLay->setMargin( MARGIN );
196     dimLay->setColumnStretch( 0, 0 );
197     dimLay->setColumnStretch( 1, 1 );
198     dimRow = 0;
199
200     // *  Length from faces
201     myLenFromFacesCheckBox = new QCheckBox( tr( "NG_LENGTH_FROM_FACES" ), dimGroup );
202     dimLay->addWidget( myLenFromFacesCheckBox, dimRow, 0, 1, 2 );
203     dimRow++;
204
205     // * max volume
206     dimLay->addWidget(new QLabel( tr("SMESH_MAX_ELEMENT_VOLUME_HYPOTHESIS"), dimGroup), dimRow, 0);
207     myVolume = new SMESHGUI_SpinBox( dimGroup );
208     myVolume->RangeStepAndValidator( VALUE_SMALL_3, VALUE_MAX_3, 0.1, 6 );
209     myVolume->setValue( 1. );
210     dimLay->addWidget( myVolume, dimRow, 1 );
211     dimRow++;
212   }
213
214   connect( myNbSegRadioBut,  SIGNAL( clicked(bool) ), this, SLOT( onValueChanged() ));
215   connect( myLengthRadioBut, SIGNAL( clicked(bool) ), this, SLOT( onValueChanged() ));
216   connect( myLenFromEdgesCheckBox, SIGNAL( stateChanged(int)), this, SLOT( onValueChanged() ));
217   if ( myLenFromFacesCheckBox )
218     connect( myLenFromFacesCheckBox, SIGNAL( stateChanged(int)), this, SLOT( onValueChanged() ));
219
220   return fr;
221 }
222
223 void NETGENPluginGUI_SimpleCreator::retrieveParams() const
224 {
225   if ( isCreation() )
226     myName->setText( hypName() );
227
228   // set default real values
229
230   NETGENPlugin_SimpleHypothesis_2D_var h =
231     NETGENPlugin_SimpleHypothesis_2D::_narrow( initParamsHypothesis( hasInitParamsHypothesis() ));
232
233   if ( double len = h->GetLocalLength() )
234     myLength->setValue( len );
235   if ( double area = h->GetMaxElementArea() )
236     myArea->setValue( area );
237   if ( myVolume ) {
238     NETGENPlugin_SimpleHypothesis_3D_var h3d =
239       NETGENPlugin_SimpleHypothesis_3D::_narrow( initParamsHypothesis( hasInitParamsHypothesis()) );
240     if ( double volume = (double) h3d->GetMaxElementVolume() )
241       myVolume->setValue( volume );
242   }
243
244   h = NETGENPlugin_SimpleHypothesis_2D::_narrow( hypothesis() );
245
246   // set values of hypothesis
247
248   SMESH::ListOfParameters_var aParameters = h->GetLastParameters();
249
250   // 1D
251   int nbSeg = (int) h->GetNumberOfSegments();
252   myNbSegRadioBut->setChecked( nbSeg );
253   myLengthRadioBut->setChecked( !nbSeg );
254   QString aPrm;
255   if ( nbSeg ) {
256     myLength->setEnabled( false );
257     myNbSeg->setEnabled( true );
258     aPrm = (aParameters->length() > 0) ? QString(aParameters[0].in()) : QString("");
259     if(aPrm.isEmpty())
260       myNbSeg->setValue( nbSeg );
261     else
262       myNbSeg->setText(aPrm);
263   }
264   else {
265     myNbSeg->setEnabled( false );
266     myLength->setEnabled( true );
267     aPrm = (aParameters->length() > 0) ? QString(aParameters[0].in()) : QString("");
268     if(aPrm.isEmpty())
269       myLength->setValue( h->GetLocalLength() );
270     else
271       myLength->setText(aPrm);
272   }
273
274   // 2D
275   if ( double area = h->GetMaxElementArea() ) {
276     myLenFromEdgesCheckBox->setChecked( false );
277     myArea->setEnabled( true );
278     aPrm = (aParameters->length() > 1) ? QString(aParameters[1].in()) : QString("");
279     if(aPrm.isEmpty()) 
280       myArea->setValue( area );
281     else
282       myArea->setText( aPrm );
283   }
284   else {
285     myLenFromEdgesCheckBox->setChecked( true );
286     myArea->setEnabled( false );
287   }
288
289   // 3D
290   if ( myVolume ) {
291     NETGENPlugin_SimpleHypothesis_3D_var h = NETGENPlugin_SimpleHypothesis_3D::_narrow( hypothesis() );
292     if ( double volume = (double) h->GetMaxElementVolume() ) {
293       myLenFromFacesCheckBox->setChecked( false );
294       myVolume->setEnabled( true );
295       aPrm = (aParameters->length() > 2) ? QString(aParameters[2].in()) : QString("");
296       if(aPrm.isEmpty())
297         myVolume->setValue( volume );
298       else
299         myVolume->setText( aPrm );
300     }
301     else {
302       myLenFromFacesCheckBox->setChecked( true );
303       myVolume->setEnabled( false );
304     }
305   }
306 }
307
308 QString NETGENPluginGUI_SimpleCreator::storeParams() const
309 {
310   QString valStr;
311   try
312   {
313     NETGENPlugin_SimpleHypothesis_2D_var h =
314       NETGENPlugin_SimpleHypothesis_2D::_narrow( hypothesis() );
315
316     if( isCreation() )
317       SMESH::SetName( SMESH::FindSObject( h ), myName->text().toLatin1().data() );
318
319     
320
321     // 1D
322     QStringList aVariablesList;
323     if ( myNbSeg->isEnabled() ) {
324       h->SetNumberOfSegments( myNbSeg->value() );
325       valStr += "nbSeg=" + myNbSeg->text();
326       aVariablesList.append(myNbSeg->text());
327     }
328     else {
329       h->SetLocalLength( myLength->value() );
330       valStr += "len=" + myLength->text();
331       aVariablesList.append(myLength->text());
332     }
333     
334     h->SetParameters(SMESHGUI::JoinObjectParameters(aVariablesList));
335
336     // 2D
337     if ( myArea->isEnabled() ) {
338       h->SetMaxElementArea( myArea->value() );
339       valStr += "; area=" + myArea->text();
340       aVariablesList.append(myArea->text());
341     }
342     else {
343       h->LengthFromEdges();
344       valStr += "; lenFromEdges";
345       aVariablesList.append(QString());
346     }
347
348     h->SetParameters(SMESHGUI::JoinObjectParameters(aVariablesList));
349
350     // 3D
351     if ( myVolume ) {
352       NETGENPlugin_SimpleHypothesis_3D_var h =
353         NETGENPlugin_SimpleHypothesis_3D::_narrow( hypothesis() );
354       if ( myVolume->isEnabled() ) {
355         h->SetMaxElementVolume( myVolume->value() );
356         valStr += "; vol=" + myVolume->text();
357         aVariablesList.append( myVolume->text());
358       }
359       else {
360         h->LengthFromFaces();
361         valStr += "; lenFromFaces";
362         aVariablesList.append(QString());
363       }
364       h->SetParameters(SMESHGUI::JoinObjectParameters(aVariablesList));
365     }
366   }
367   catch(const SALOME::SALOME_Exception& ex)
368   {
369     SalomeApp_Tools::QtCatchCorbaException(ex);
370   }
371
372   return valStr;
373 }
374
375 void NETGENPluginGUI_SimpleCreator::onValueChanged()
376 {
377   QObject* changed = sender();
378
379   if ( myNbSegRadioBut == changed )
380   {
381     myLengthRadioBut->setChecked( !myNbSegRadioBut->isChecked() );
382   }
383   else if ( myLengthRadioBut == changed )
384   {
385     myNbSegRadioBut->setChecked( !myLengthRadioBut->isChecked() );
386   }
387   else if ( myLenFromEdgesCheckBox == changed )
388   {
389     myArea->setEnabled( !myLenFromEdgesCheckBox->isChecked() );
390   }
391   else if ( myLenFromFacesCheckBox == changed )
392   {
393     myVolume->setEnabled( !myLenFromFacesCheckBox->isChecked() );
394   }
395   myLength->setEnabled( myLengthRadioBut->isChecked() );
396   myNbSeg->setEnabled( myNbSegRadioBut->isChecked() );
397 }
398
399 QString NETGENPluginGUI_SimpleCreator::caption() const
400 {
401   return tr( (hypType() + "_TITLE").toLatin1().data() );
402 }
403
404 QPixmap NETGENPluginGUI_SimpleCreator::icon() const
405 {
406   QString hypIconName = tr( ("ICON_DLG_" + hypType()).toLatin1().data() );
407   return SUIT_Session::session()->resourceMgr()->loadPixmap( "NETGENPlugin", hypIconName );
408 }
409
410 QString NETGENPluginGUI_SimpleCreator::type() const
411 {
412   return tr( (hypType() + "_HYPOTHESIS").toLatin1().data() );
413 }
414
415 QString NETGENPluginGUI_SimpleCreator::helpPage() const
416 {
417   return "netgen_2d_3d_hypo_page.html";
418 }