Salome HOME
Merge netgen with BR_V5_DEV branch.
[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 // copied from StdMeshersGUI_StdHypothesisCreator.cxx
59 const double VALUE_MAX = 1.0e+15, // COORD_MAX
60              VALUE_MAX_2  = VALUE_MAX * VALUE_MAX,
61              VALUE_MAX_3  = VALUE_MAX_2 * VALUE_MAX,
62              VALUE_SMALL = 1.0e-15,
63              VALUE_SMALL_2 = VALUE_SMALL * VALUE_SMALL,
64              VALUE_SMALL_3 = VALUE_SMALL_2 * VALUE_SMALL;
65
66 NETGENPluginGUI_SimpleCreator::NETGENPluginGUI_SimpleCreator(const QString& theHypType)
67 : SMESHGUI_GenericHypothesisCreator( theHypType ),
68   myName(0),
69   myNbSeg(0),
70   myLength(0),
71   myNbSegRadioBut(0),
72   myLengthRadioBut(0),
73   myLenFromEdgesCheckBox(0),
74   myArea(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   result = myNbSeg->isValid(msg,true) && result;
88   result = myLength->isValid(msg,true) && result;
89   result = myArea->isValid(msg,true) && result;
90   if (myVolume)
91     result = myVolume->isValid(msg,true) && result;
92   
93   return result;
94 }
95
96 QFrame* NETGENPluginGUI_SimpleCreator::buildFrame()
97 {
98   QFrame* fr = new QFrame();
99
100   QVBoxLayout* lay = new QVBoxLayout( fr );
101   lay->setMargin( 0 );
102   lay->setSpacing( 0 );
103
104   QGroupBox* argGroup = new QGroupBox( tr( "SMESH_ARGUMENTS" ), fr );
105   lay->addWidget( argGroup );
106
107   QGridLayout* argLay = new QGridLayout( argGroup );
108   argLay->setSpacing( SPACING );
109   argLay->setMargin( MARGIN );
110   argLay->setColumnStretch( 0, 0 );
111   argLay->setColumnStretch( 1, 1 );
112   int argRow = 0;
113
114   // Name
115   if( isCreation() ) {
116     myName = new QLineEdit( argGroup );
117     argLay->addWidget( new QLabel( tr( "SMESH_NAME" ), argGroup ), argRow, 0 );
118     argLay->addWidget( myName, argRow, 1 );
119     argRow++;
120   }
121
122   QGroupBox* dimGroup;
123   QGridLayout* dimLay;
124   int dimRow;
125
126   // 1D params group
127
128   dimGroup = new QGroupBox( tr( "NG_1D" ), argGroup );
129   argLay->addWidget( dimGroup, argRow, 0, 1, 2 );
130   argRow++;
131
132   dimLay = new QGridLayout( dimGroup );
133   dimLay->setSpacing( SPACING );
134   dimLay->setMargin( MARGIN );
135   dimLay->setColumnStretch( 0, 0 );
136   dimLay->setColumnStretch( 1, 1 );
137   dimRow = 0;
138
139   // *  number of segments
140   myNbSegRadioBut  = new QRadioButton( tr( "SMESH_NB_SEGMENTS_HYPOTHESIS"  ), dimGroup );
141   myNbSeg = new SalomeApp_IntSpinBox( dimGroup );
142   myNbSeg->setMinimum( 1 );
143   myNbSeg->setMaximum( 9999 );
144   myNbSeg->setValue( 1 );
145   dimLay->addWidget( myNbSegRadioBut, dimRow, 0 );
146   dimLay->addWidget( myNbSeg, dimRow, 1 );
147   dimRow++;
148
149   // * local length
150   myLengthRadioBut = new QRadioButton( tr( "SMESH_LOCAL_LENGTH_HYPOTHESIS" ), dimGroup );
151   myLength = new SMESHGUI_SpinBox( dimGroup );
152   myLength->RangeStepAndValidator( VALUE_SMALL, VALUE_MAX, 0.1, 6 );
153   myLength->setValue( 1. );
154   dimLay->addWidget( myLengthRadioBut, dimRow, 0 );
155   dimLay->addWidget( myLength, dimRow, 1 );
156   dimRow++;
157
158   // 2D params group
159
160   dimGroup = new QGroupBox( tr( "NG_2D" ), argGroup );
161   argLay->addWidget( dimGroup, argRow, 0, 1, 2 );
162   argRow++;
163
164   dimLay = new QGridLayout( dimGroup );
165   dimLay->setSpacing( SPACING );
166   dimLay->setMargin( MARGIN );
167   dimLay->setColumnStretch( 0, 0 );
168   dimLay->setColumnStretch( 1, 1 );
169   dimRow = 0;
170
171   // *  Length from edges
172   myLenFromEdgesCheckBox = new QCheckBox( tr( "NG_LENGTH_FROM_EDGES" ), dimGroup );
173   dimLay->addWidget( myLenFromEdgesCheckBox, dimRow, 0, 1, 2 );
174   dimRow++;
175
176   // * max area
177   dimLay->addWidget( new QLabel( tr( "SMESH_MAX_ELEMENT_AREA_HYPOTHESIS" ), dimGroup), dimRow, 0);
178   myArea = new SMESHGUI_SpinBox( dimGroup );
179   myArea->RangeStepAndValidator( VALUE_SMALL_2, VALUE_MAX_2, 0.1, 6 );
180   myArea->setValue( 1. );
181   dimLay->addWidget( myArea, dimRow, 1 );
182   dimRow++;
183
184   // 3D params group
185   if ( hypType()=="NETGEN_SimpleParameters_3D" )
186   {
187     dimGroup = new QGroupBox( tr( "NG_3D" ), argGroup );
188     argLay->addWidget( dimGroup, argRow, 0, 1, 2 );
189     argRow++;
190
191     dimLay = new QGridLayout( dimGroup );
192     dimLay->setSpacing( SPACING );
193     dimLay->setMargin( MARGIN );
194     dimLay->setColumnStretch( 0, 0 );
195     dimLay->setColumnStretch( 1, 1 );
196     dimRow = 0;
197
198     // *  Length from faces
199     myLenFromFacesCheckBox = new QCheckBox( tr( "NG_LENGTH_FROM_FACES" ), dimGroup );
200     dimLay->addWidget( myLenFromFacesCheckBox, dimRow, 0, 1, 2 );
201     dimRow++;
202
203     // * max volume
204     dimLay->addWidget(new QLabel( tr("SMESH_MAX_ELEMENT_VOLUME_HYPOTHESIS"), dimGroup), dimRow, 0);
205     myVolume = new SMESHGUI_SpinBox( dimGroup );
206     myVolume->RangeStepAndValidator( VALUE_SMALL_3, VALUE_MAX_3, 0.1, 6 );
207     myVolume->setValue( 1. );
208     dimLay->addWidget( myVolume, dimRow, 1 );
209     dimRow++;
210   }
211
212   connect( myNbSegRadioBut,  SIGNAL( clicked(bool) ), this, SLOT( onValueChanged() ));
213   connect( myLengthRadioBut, SIGNAL( clicked(bool) ), this, SLOT( onValueChanged() ));
214   connect( myLenFromEdgesCheckBox, SIGNAL( stateChanged(int)), this, SLOT( onValueChanged() ));
215   if ( myLenFromFacesCheckBox )
216     connect( myLenFromFacesCheckBox, SIGNAL( stateChanged(int)), this, SLOT( onValueChanged() ));
217
218   return fr;
219 }
220
221 void NETGENPluginGUI_SimpleCreator::retrieveParams() const
222 {
223   if ( isCreation() )
224     myName->setText( hypName() );
225
226   NETGENPlugin::NETGENPlugin_SimpleHypothesis_2D_var h =
227     NETGENPlugin::NETGENPlugin_SimpleHypothesis_2D::_narrow( initParamsHypothesis() );
228
229   SMESH::ListOfParameters_var aParameters = h->GetLastParameters();
230
231   // 1D
232   int nbSeg = (int) h->GetNumberOfSegments();
233   myNbSegRadioBut->setChecked( nbSeg );
234   myLengthRadioBut->setChecked( !nbSeg );
235   QString aPrm;
236   if ( nbSeg ) {
237     myLength->setEnabled( false );
238     myNbSeg->setEnabled( true );
239     aPrm = (aParameters->length() > 0) ? QString(aParameters[0].in()) : QString("");
240     if(aPrm.isEmpty())
241       myNbSeg->setValue( nbSeg );
242     else
243       myNbSeg->setText(aPrm);
244   }
245   else {
246     myNbSeg->setEnabled( false );
247     myLength->setEnabled( true );
248     aPrm = (aParameters->length() > 0) ? QString(aParameters[0].in()) : QString("");
249     if(aPrm.isEmpty())
250       myLength->setValue( h->GetLocalLength() );
251     else
252       myLength->setText(aPrm);
253   }
254
255   // 2D
256   if ( double area = h->GetMaxElementArea() ) {
257     myLenFromEdgesCheckBox->setChecked( false );
258     myArea->setEnabled( true );
259     aPrm = (aParameters->length() > 1) ? QString(aParameters[1].in()) : QString("");
260     if(aPrm.isEmpty()) 
261       myArea->setValue( area );
262     else
263       myArea->setText( aPrm );
264   }
265   else {
266     myLenFromEdgesCheckBox->setChecked( true );
267     myArea->setEnabled( false );
268   }
269
270   // 3D
271   if ( myVolume ) {
272     NETGENPlugin::NETGENPlugin_SimpleHypothesis_3D_var h =
273       NETGENPlugin::NETGENPlugin_SimpleHypothesis_3D::_narrow( initParamsHypothesis() );
274     if ( double volume = (double) h->GetMaxElementVolume() ) {
275       myLenFromFacesCheckBox->setChecked( false );
276       myVolume->setEnabled( true );
277       aPrm = (aParameters->length() > 2) ? QString(aParameters[2].in()) : QString("");
278       if(aPrm.isEmpty())
279         myVolume->setValue( volume );
280       else
281         myVolume->setText( aPrm );
282     }
283     else {
284       myLenFromFacesCheckBox->setChecked( true );
285       myVolume->setEnabled( false );
286     }
287   }
288 }
289
290 QString NETGENPluginGUI_SimpleCreator::storeParams() const
291 {
292   QString valStr;
293   try
294   {
295     NETGENPlugin::NETGENPlugin_SimpleHypothesis_2D_var h =
296       NETGENPlugin::NETGENPlugin_SimpleHypothesis_2D::_narrow( initParamsHypothesis() );
297
298     if( isCreation() )
299       SMESH::SetName( SMESH::FindSObject( h ), myName->text().toLatin1().data() );
300
301     
302
303     // 1D
304     QStringList aVariablesList;
305     if ( myNbSeg->isEnabled() ) {
306       h->SetNumberOfSegments( myNbSeg->value() );
307       valStr += "nbSeg=" + myNbSeg->text();
308       aVariablesList.append(myNbSeg->text());
309     }
310     else {
311       h->SetLocalLength( myLength->value() );
312       valStr += "len=" + myLength->text();
313       aVariablesList.append(myLength->text());
314     }
315     
316     h->SetParameters(SMESHGUI::JoinObjectParameters(aVariablesList));
317
318     // 2D
319     if ( myArea->isEnabled() ) {
320       h->SetMaxElementArea( myArea->value() );
321       valStr += "; area=" + myArea->text();
322       aVariablesList.append(myArea->text());
323     }
324     else {
325       h->LengthFromEdges();
326       valStr += "; lenFromEdges";
327       aVariablesList.append(QString());
328     }
329
330     h->SetParameters(SMESHGUI::JoinObjectParameters(aVariablesList));
331
332     // 3D
333     if ( myVolume ) {
334       NETGENPlugin::NETGENPlugin_SimpleHypothesis_3D_var h =
335         NETGENPlugin::NETGENPlugin_SimpleHypothesis_3D::_narrow( initParamsHypothesis() );
336       if ( myVolume->isEnabled() ) {
337         h->SetMaxElementVolume( myVolume->value() );
338         valStr += "; vol=" + myVolume->text();
339         aVariablesList.append( myVolume->text());
340       }
341       else {
342         h->LengthFromFaces();
343         valStr += "; lenFromFaces";
344         aVariablesList.append(QString());
345       }
346       h->SetParameters(SMESHGUI::JoinObjectParameters(aVariablesList));
347     }
348   }
349   catch(const SALOME::SALOME_Exception& ex)
350   {
351     SalomeApp_Tools::QtCatchCorbaException(ex);
352   }
353
354   return valStr;
355 }
356
357 void NETGENPluginGUI_SimpleCreator::onValueChanged()
358 {
359   QObject* changed = sender();
360
361   if ( myNbSegRadioBut == changed )
362   {
363     myLengthRadioBut->setChecked( !myNbSegRadioBut->isChecked() );
364   }
365   else if ( myLengthRadioBut == changed )
366   {
367     myNbSegRadioBut->setChecked( !myLengthRadioBut->isChecked() );
368   }
369   else if ( myLenFromEdgesCheckBox == changed )
370   {
371     myArea->setEnabled( !myLenFromEdgesCheckBox->isChecked() );
372   }
373   else if ( myLenFromFacesCheckBox == changed )
374   {
375     myVolume->setEnabled( !myLenFromFacesCheckBox->isChecked() );
376   }
377   myLength->setEnabled( myLengthRadioBut->isChecked() );
378   myNbSeg->setEnabled( myNbSegRadioBut->isChecked() );
379 }
380
381 QString NETGENPluginGUI_SimpleCreator::caption() const
382 {
383   return tr( (hypType() + "_TITLE").toLatin1().data() );
384 }
385
386 QPixmap NETGENPluginGUI_SimpleCreator::icon() const
387 {
388   QString hypIconName = tr( ("ICON_DLG_" + hypType()).toLatin1().data() );
389   return SUIT_Session::session()->resourceMgr()->loadPixmap( "NETGENPlugin", hypIconName );
390 }
391
392 QString NETGENPluginGUI_SimpleCreator::type() const
393 {
394   return tr( (hypType() + "_HYPOTHESIS").toLatin1().data() );
395 }
396
397 QString NETGENPluginGUI_SimpleCreator::helpPage() const
398 {
399   return "netgen_2d_3d_hypo_page.html";
400 }