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