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