Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[plugins/netgenplugin.git] / src / GUI / NETGENPluginGUI_HypothesisCreator.cxx
1 //  NETGENPlugin GUI: GUI for plugged-in mesher NETGENPlugin
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   : NETGENPluginGUI_HypothesisCreator.cxx
24 //  Author : Michael Zorin
25 //  Module : NETGENPlugin
26 //  $Header: 
27
28 #include "NETGENPluginGUI_HypothesisCreator.h"
29
30 #include <SMESHGUI_Utils.h>
31 #include <SMESHGUI_HypothesesUtils.h>
32
33 #include CORBA_SERVER_HEADER(NETGENPlugin_Algorithm)
34
35 #include <SUIT_Session.h>
36
37 #include <SalomeApp_Tools.h>
38
39 #include <QtxDblSpinBox.h>
40 #include <QtxComboBox.h>
41
42 #include <qlabel.h>
43 #include <qgroupbox.h>
44 #include <qframe.h>
45 #include <qlayout.h>
46 #include <qlineedit.h>
47 #include <qcheckbox.h>
48 #include <qpixmap.h>
49
50  enum Fineness
51    {
52      VeryCoarse,
53      Coarse,
54      Moderate,
55      Fine,
56      VeryFine,
57      UserDefined
58    };
59
60 NETGENPluginGUI_HypothesisCreator::NETGENPluginGUI_HypothesisCreator( const QString& theHypType )
61 : SMESHGUI_GenericHypothesisCreator( theHypType ),
62   myIs2D(false)
63 {
64 }
65
66 NETGENPluginGUI_HypothesisCreator::~NETGENPluginGUI_HypothesisCreator()
67 {
68 }
69
70 bool NETGENPluginGUI_HypothesisCreator::checkParams() const
71 {
72   NetgenHypothesisData data_old, data_new;
73   readParamsFromHypo( data_old );
74   readParamsFromWidgets( data_new );
75   bool res = storeParamsToHypo( data_new );
76   storeParamsToHypo( data_old );
77   return res;
78 }
79
80 QFrame* NETGENPluginGUI_HypothesisCreator::buildFrame()
81 {
82   QFrame* fr = new QFrame( 0, "myframe" );
83   QVBoxLayout* lay = new QVBoxLayout( fr, 5, 0 );
84
85   QGroupBox* GroupC1 = new QGroupBox( 2, Qt::Horizontal, fr, "GroupC1" );
86   lay->addWidget( GroupC1 );
87   
88   GroupC1->setTitle( tr( "SMESH_ARGUMENTS"  ) );
89   GroupC1->layout()->setSpacing( 6 );
90   GroupC1->layout()->setMargin( 11 );
91   
92   myName = 0;
93   if( isCreation() )
94   {
95     new QLabel( tr( "SMESH_NAME" ), GroupC1 );
96     myName = new QLineEdit( GroupC1 );
97   }
98
99   new QLabel( tr( "NETGEN_MAX_SIZE" ), GroupC1 );
100   myMaxSize = new QtxDblSpinBox( GroupC1 );
101   myMaxSize->setPrecision( 7 );
102   myMaxSize->setMinValue( 1e-07 );
103   myMaxSize->setMaxValue( 1e+06 );
104   myMaxSize->setLineStep( 10 );
105   
106   mySecondOrder = new QCheckBox( tr( "NETGEN_SECOND_ORDER" ), GroupC1 );
107   GroupC1->addSpace(0);
108   
109   new QLabel( tr( "NETGEN_FINENESS" ), GroupC1 );
110   myFineness = new QtxComboBox( GroupC1 );
111   QStringList types;
112   types.append( QObject::tr( "NETGEN_VERYCOARSE" ) );
113   types.append( QObject::tr( "NETGEN_COARSE" ) );
114   types.append( QObject::tr( "NETGEN_MODERATE" ) );
115   types.append( QObject::tr( "NETGEN_FINE" ) );
116   types.append( QObject::tr( "NETGEN_VERYFINE" ) );
117   types.append( QObject::tr( "NETGEN_CUSTOM" ) );
118   myFineness->insertStringList( types );
119
120   new QLabel( tr( "NETGEN_GROWTH_RATE" ), GroupC1 );
121   myGrowthRate = new QtxDblSpinBox( GroupC1 );
122   myGrowthRate->setMinValue( 0.1 );
123   myGrowthRate->setMaxValue( 10 );
124   myGrowthRate->setLineStep( 0.1 );
125   
126   new QLabel( tr( "NETGEN_SEG_PER_EDGE" ), GroupC1 );
127   myNbSegPerEdge = new QtxDblSpinBox( GroupC1 );
128   myNbSegPerEdge->setMinValue( 0.2 );
129   myNbSegPerEdge->setMaxValue( 5.0 );
130   
131   new QLabel( tr( "NETGEN_SEG_PER_RADIUS" ), GroupC1 );
132   myNbSegPerRadius = new QtxDblSpinBox( GroupC1 );
133   myNbSegPerRadius->setMinValue( 0.2 );
134   myNbSegPerRadius->setMaxValue( 5.0 );
135
136   if ( hypType()=="NETGEN_Parameters_2D" )
137   {
138     myAllowQuadrangles = new QCheckBox( tr( "NETGEN_ALLOW_QUADRANGLES" ), GroupC1 );
139     GroupC1->addSpace(0);
140     myIs2D = true;
141   }
142
143   myOptimize = new QCheckBox( tr( "NETGEN_OPTIMIZE" ), GroupC1 );
144   GroupC1->addSpace(0);
145   
146   connect( myFineness, SIGNAL( activated( int ) ), this, SLOT( onFinenessChanged() ) );
147   
148   return fr;
149 }
150
151 void NETGENPluginGUI_HypothesisCreator::retrieveParams() const
152 {
153   NetgenHypothesisData data;
154   readParamsFromHypo( data );
155
156   if( myName )
157     myName->setText( data.myName );
158   myMaxSize->setValue( data.myMaxSize );
159   mySecondOrder->setChecked( data.mySecondOrder );
160   myOptimize->setChecked( data.myOptimize );
161   myFineness->setCurrentItem( data.myFineness );
162   myGrowthRate->setValue( data.myGrowthRate );
163   myNbSegPerEdge->setValue( data.myNbSegPerEdge );
164   myNbSegPerRadius->setValue( data.myNbSegPerRadius );
165   if (myIs2D)
166     myAllowQuadrangles->setChecked( data.myAllowQuadrangles );
167
168   // update widgets
169   bool isCustom = (myFineness->currentItem() == UserDefined);
170   myGrowthRate->setEnabled(isCustom);
171   myNbSegPerEdge->setEnabled(isCustom);
172   myNbSegPerRadius->setEnabled(isCustom);
173 }
174
175 QString NETGENPluginGUI_HypothesisCreator::storeParams() const
176 {
177   NetgenHypothesisData data;
178   readParamsFromWidgets( data );
179   storeParamsToHypo( data );
180   
181   QString valStr = tr("NETGEN_MAX_SIZE") + " = " + QString::number( data.myMaxSize ) + "; ";
182   if ( data.mySecondOrder )
183     valStr +=  tr("NETGEN_SECOND_ORDER") + "; ";
184   if ( data.myOptimize )
185     valStr +=  tr("NETGEN_OPTIMIZE") + "; ";
186   valStr += myFineness->currentText() + "(" +  QString::number( data.myGrowthRate )     + ", " +
187                                                QString::number( data.myNbSegPerEdge )   + ", " +
188                                                QString::number( data.myNbSegPerRadius ) + ")";
189
190   if ( myIs2D && data.myAllowQuadrangles )
191     valStr += "; " + tr("NETGEN_ALLOW_QUADRANGLES");
192   
193   return valStr;
194 }
195
196 bool NETGENPluginGUI_HypothesisCreator::readParamsFromHypo( NetgenHypothesisData& h_data ) const
197 {
198   NETGENPlugin::NETGENPlugin_Hypothesis_var h =
199     NETGENPlugin::NETGENPlugin_Hypothesis::_narrow( initParamsHypothesis() );
200
201   HypothesisData* data = SMESH::GetHypothesisData( hypType() );
202   h_data.myName = isCreation() && data ? data->Label : "";
203
204   h_data.myMaxSize = h->GetMaxSize();
205   h_data.mySecondOrder = h->GetSecondOrder();
206   h_data.myOptimize = h->GetOptimize();
207
208   h_data.myFineness = (int) h->GetFineness();
209   h_data.myGrowthRate = h->GetGrowthRate();
210   h_data.myNbSegPerEdge = h->GetNbSegPerEdge();
211   h_data.myNbSegPerRadius = h->GetNbSegPerRadius();
212
213   if ( myIs2D )
214     {
215       NETGENPlugin::NETGENPlugin_Hypothesis_2D_var h_2d =
216         NETGENPlugin::NETGENPlugin_Hypothesis_2D::_narrow( initParamsHypothesis() );
217
218       if ( !h_2d->_is_nil() )
219         h_data.myAllowQuadrangles = h_2d->GetQuadAllowed();
220     }
221   
222   return true;
223 }
224
225 bool NETGENPluginGUI_HypothesisCreator::storeParamsToHypo( const NetgenHypothesisData& h_data ) const
226 {
227   NETGENPlugin::NETGENPlugin_Hypothesis_var h =
228     NETGENPlugin::NETGENPlugin_Hypothesis::_narrow( hypothesis() );
229
230   bool ok = true;
231   try
232   {
233     if( isCreation() )
234       SMESH::SetName( SMESH::FindSObject( h ), h_data.myName.latin1() );
235
236     h->SetMaxSize( h_data.myMaxSize );
237     h->SetSecondOrder( h_data.mySecondOrder );
238     h->SetOptimize( h_data.myOptimize );
239     int fineness = h_data.myFineness;
240     h->SetFineness( fineness );
241
242     if( fineness==UserDefined )
243       {
244         h->SetGrowthRate( h_data.myGrowthRate );
245         h->SetNbSegPerEdge( h_data.myNbSegPerEdge );
246         h->SetNbSegPerRadius( h_data.myNbSegPerRadius );
247       }
248     
249     if ( myIs2D )
250       {
251         NETGENPlugin::NETGENPlugin_Hypothesis_2D_var h_2d =
252           NETGENPlugin::NETGENPlugin_Hypothesis_2D::_narrow( h );
253         
254         if ( !h_2d->_is_nil() )
255           h_2d->SetQuadAllowed( h_data.myAllowQuadrangles );
256       }
257   }
258   catch(const SALOME::SALOME_Exception& ex)
259   {
260     SalomeApp_Tools::QtCatchCorbaException(ex);
261     ok = false;
262   }
263   return ok;
264 }
265
266 bool NETGENPluginGUI_HypothesisCreator::readParamsFromWidgets( NetgenHypothesisData& h_data ) const
267 {
268   h_data.myName           = myName ? myName->text() : "";
269   h_data.myMaxSize        = myMaxSize->value();
270   h_data.mySecondOrder    = mySecondOrder->isChecked();
271   h_data.myOptimize       = myOptimize->isChecked();
272   h_data.myFineness       = myFineness->currentItem();
273   h_data.myGrowthRate     = myGrowthRate->value();
274   h_data.myNbSegPerEdge   = myNbSegPerEdge->value();
275   h_data.myNbSegPerRadius = myNbSegPerRadius->value();
276   
277   if ( myIs2D )
278     h_data.myAllowQuadrangles = myAllowQuadrangles->isChecked();
279   
280   return true;
281 }
282
283 void NETGENPluginGUI_HypothesisCreator::onFinenessChanged()
284 {
285   bool isCustom = (myFineness->currentItem() == UserDefined);
286   
287   myGrowthRate->setEnabled(isCustom);
288   myNbSegPerEdge->setEnabled(isCustom);
289   myNbSegPerRadius->setEnabled(isCustom);
290
291   if (!isCustom)
292     {
293       double aGrowthRate, aNbSegPerEdge, aNbSegPerRadius;
294       
295       switch ( myFineness->currentItem() )
296         {
297         case VeryCoarse:
298           aGrowthRate = 0.7;
299           aNbSegPerEdge = 0.3;
300           aNbSegPerRadius = 1;
301           break;
302         case Coarse:
303           aGrowthRate = 0.5;
304           aNbSegPerEdge = 0.5;
305           aNbSegPerRadius = 1.5;
306           break;
307         case Fine:
308           aGrowthRate = 0.2;
309           aNbSegPerEdge = 2;
310           aNbSegPerRadius = 3;
311           break;
312         case VeryFine:
313           aGrowthRate = 0.1;
314           aNbSegPerEdge = 3;
315           aNbSegPerRadius = 5;
316           break;
317         case Moderate:
318         default:
319           aGrowthRate = 0.3;
320           aNbSegPerEdge = 1;
321           aNbSegPerRadius = 2;
322           break;
323         }
324       
325       myGrowthRate->setValue( aGrowthRate );
326       myNbSegPerEdge->setValue( aNbSegPerEdge );
327       myNbSegPerRadius->setValue( aNbSegPerRadius );
328     }
329 }
330
331 QString NETGENPluginGUI_HypothesisCreator::caption() const
332 {
333   return tr( QString( "NETGEN_%1_TITLE" ).arg(myIs2D?QString("2D"):QString("3D")) );
334 }
335
336 QPixmap NETGENPluginGUI_HypothesisCreator::icon() const
337 {
338   QString hypIconName = tr( QString("ICON_DLG_NETGEN_PARAMETERS%1").arg(myIs2D?QString("_2D"):QString("")) );
339   return SUIT_Session::session()->resourceMgr()->loadPixmap( "NETGENPlugin", hypIconName );
340 }
341
342 QString NETGENPluginGUI_HypothesisCreator::type() const
343 {
344   return tr( QString( "NETGEN_%1_HYPOTHESIS" ).arg(myIs2D?QString("2D"):QString("3D")) );
345 }