Salome HOME
Update copyright information
[plugins/netgenplugin.git] / src / GUI / NETGENPluginGUI_HypothesisCreator.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 //  NETGENPlugin GUI: GUI for plugged-in mesher NETGENPlugin
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   const double VALUE_MAX = 1.0e+6;
127
128   new QLabel( tr( "NETGEN_SEG_PER_EDGE" ), GroupC1 );
129   myNbSegPerEdge = new QtxDblSpinBox( GroupC1 );
130   myNbSegPerEdge->setMinValue( 0.2 );
131   myNbSegPerEdge->setMaxValue( VALUE_MAX ); // (PAL14890) max value in native netgen gui is 5
132   
133   new QLabel( tr( "NETGEN_SEG_PER_RADIUS" ), GroupC1 );
134   myNbSegPerRadius = new QtxDblSpinBox( GroupC1 );
135   myNbSegPerRadius->setMinValue( 0.2 );
136   myNbSegPerRadius->setMaxValue( VALUE_MAX ); // (PAL14890) max value in native netgen gui is 5
137
138   if ( hypType()=="NETGEN_Parameters_2D" )
139   {
140     myAllowQuadrangles = new QCheckBox( tr( "NETGEN_ALLOW_QUADRANGLES" ), GroupC1 );
141     GroupC1->addSpace(0);
142     myIs2D = true;
143   }
144
145   myOptimize = new QCheckBox( tr( "NETGEN_OPTIMIZE" ), GroupC1 );
146   GroupC1->addSpace(0);
147   
148   connect( myFineness, SIGNAL( activated( int ) ), this, SLOT( onFinenessChanged() ) );
149   
150   return fr;
151 }
152
153 void NETGENPluginGUI_HypothesisCreator::retrieveParams() const
154 {
155   NetgenHypothesisData data;
156   readParamsFromHypo( data );
157
158   if( myName )
159     myName->setText( data.myName );
160   myMaxSize->setValue( data.myMaxSize );
161   mySecondOrder->setChecked( data.mySecondOrder );
162   myOptimize->setChecked( data.myOptimize );
163   myFineness->setCurrentItem( data.myFineness );
164   myGrowthRate->setValue( data.myGrowthRate );
165   myNbSegPerEdge->setValue( data.myNbSegPerEdge );
166   myNbSegPerRadius->setValue( data.myNbSegPerRadius );
167   if (myIs2D)
168     myAllowQuadrangles->setChecked( data.myAllowQuadrangles );
169
170   // update widgets
171   bool isCustom = (myFineness->currentItem() == UserDefined);
172   myGrowthRate->setEnabled(isCustom);
173   myNbSegPerEdge->setEnabled(isCustom);
174   myNbSegPerRadius->setEnabled(isCustom);
175 }
176
177 QString NETGENPluginGUI_HypothesisCreator::storeParams() const
178 {
179   NetgenHypothesisData data;
180   readParamsFromWidgets( data );
181   storeParamsToHypo( data );
182   
183   QString valStr = tr("NETGEN_MAX_SIZE") + " = " + QString::number( data.myMaxSize ) + "; ";
184   if ( data.mySecondOrder )
185     valStr +=  tr("NETGEN_SECOND_ORDER") + "; ";
186   if ( data.myOptimize )
187     valStr +=  tr("NETGEN_OPTIMIZE") + "; ";
188   valStr += myFineness->currentText() + "(" +  QString::number( data.myGrowthRate )     + ", " +
189                                                QString::number( data.myNbSegPerEdge )   + ", " +
190                                                QString::number( data.myNbSegPerRadius ) + ")";
191
192   if ( myIs2D && data.myAllowQuadrangles )
193     valStr += "; " + tr("NETGEN_ALLOW_QUADRANGLES");
194   
195   return valStr;
196 }
197
198 bool NETGENPluginGUI_HypothesisCreator::readParamsFromHypo( NetgenHypothesisData& h_data ) const
199 {
200   NETGENPlugin::NETGENPlugin_Hypothesis_var h =
201     NETGENPlugin::NETGENPlugin_Hypothesis::_narrow( initParamsHypothesis() );
202
203   HypothesisData* data = SMESH::GetHypothesisData( hypType() );
204   h_data.myName = isCreation() && data ? data->Label : "";
205
206   h_data.myMaxSize = h->GetMaxSize();
207   h_data.mySecondOrder = h->GetSecondOrder();
208   h_data.myOptimize = h->GetOptimize();
209
210   h_data.myFineness = (int) h->GetFineness();
211   h_data.myGrowthRate = h->GetGrowthRate();
212   h_data.myNbSegPerEdge = h->GetNbSegPerEdge();
213   h_data.myNbSegPerRadius = h->GetNbSegPerRadius();
214
215   if ( myIs2D )
216     {
217       NETGENPlugin::NETGENPlugin_Hypothesis_2D_var h_2d =
218         NETGENPlugin::NETGENPlugin_Hypothesis_2D::_narrow( initParamsHypothesis() );
219
220       if ( !h_2d->_is_nil() )
221         h_data.myAllowQuadrangles = h_2d->GetQuadAllowed();
222     }
223   
224   return true;
225 }
226
227 bool NETGENPluginGUI_HypothesisCreator::storeParamsToHypo( const NetgenHypothesisData& h_data ) const
228 {
229   NETGENPlugin::NETGENPlugin_Hypothesis_var h =
230     NETGENPlugin::NETGENPlugin_Hypothesis::_narrow( hypothesis() );
231
232   bool ok = true;
233   try
234   {
235     if( isCreation() )
236       SMESH::SetName( SMESH::FindSObject( h ), h_data.myName.latin1() );
237
238     h->SetMaxSize( h_data.myMaxSize );
239     h->SetSecondOrder( h_data.mySecondOrder );
240     h->SetOptimize( h_data.myOptimize );
241     int fineness = h_data.myFineness;
242     h->SetFineness( fineness );
243
244     if( fineness==UserDefined )
245       {
246         h->SetGrowthRate( h_data.myGrowthRate );
247         h->SetNbSegPerEdge( h_data.myNbSegPerEdge );
248         h->SetNbSegPerRadius( h_data.myNbSegPerRadius );
249       }
250     
251     if ( myIs2D )
252       {
253         NETGENPlugin::NETGENPlugin_Hypothesis_2D_var h_2d =
254           NETGENPlugin::NETGENPlugin_Hypothesis_2D::_narrow( h );
255         
256         if ( !h_2d->_is_nil() )
257           h_2d->SetQuadAllowed( h_data.myAllowQuadrangles );
258       }
259   }
260   catch(const SALOME::SALOME_Exception& ex)
261   {
262     SalomeApp_Tools::QtCatchCorbaException(ex);
263     ok = false;
264   }
265   return ok;
266 }
267
268 bool NETGENPluginGUI_HypothesisCreator::readParamsFromWidgets( NetgenHypothesisData& h_data ) const
269 {
270   h_data.myName           = myName ? myName->text() : "";
271   h_data.myMaxSize        = myMaxSize->value();
272   h_data.mySecondOrder    = mySecondOrder->isChecked();
273   h_data.myOptimize       = myOptimize->isChecked();
274   h_data.myFineness       = myFineness->currentItem();
275   h_data.myGrowthRate     = myGrowthRate->value();
276   h_data.myNbSegPerEdge   = myNbSegPerEdge->value();
277   h_data.myNbSegPerRadius = myNbSegPerRadius->value();
278   
279   if ( myIs2D )
280     h_data.myAllowQuadrangles = myAllowQuadrangles->isChecked();
281   
282   return true;
283 }
284
285 void NETGENPluginGUI_HypothesisCreator::onFinenessChanged()
286 {
287   bool isCustom = (myFineness->currentItem() == UserDefined);
288   
289   myGrowthRate->setEnabled(isCustom);
290   myNbSegPerEdge->setEnabled(isCustom);
291   myNbSegPerRadius->setEnabled(isCustom);
292
293   if (!isCustom)
294     {
295       double aGrowthRate, aNbSegPerEdge, aNbSegPerRadius;
296       
297       switch ( myFineness->currentItem() )
298         {
299         case VeryCoarse:
300           aGrowthRate = 0.7;
301           aNbSegPerEdge = 0.3;
302           aNbSegPerRadius = 1;
303           break;
304         case Coarse:
305           aGrowthRate = 0.5;
306           aNbSegPerEdge = 0.5;
307           aNbSegPerRadius = 1.5;
308           break;
309         case Fine:
310           aGrowthRate = 0.2;
311           aNbSegPerEdge = 2;
312           aNbSegPerRadius = 3;
313           break;
314         case VeryFine:
315           aGrowthRate = 0.1;
316           aNbSegPerEdge = 3;
317           aNbSegPerRadius = 5;
318           break;
319         case Moderate:
320         default:
321           aGrowthRate = 0.3;
322           aNbSegPerEdge = 1;
323           aNbSegPerRadius = 2;
324           break;
325         }
326       
327       myGrowthRate->setValue( aGrowthRate );
328       myNbSegPerEdge->setValue( aNbSegPerEdge );
329       myNbSegPerRadius->setValue( aNbSegPerRadius );
330     }
331 }
332
333 QString NETGENPluginGUI_HypothesisCreator::caption() const
334 {
335   return tr( QString( "NETGEN_%1_TITLE" ).arg(myIs2D?QString("2D"):QString("3D")) );
336 }
337
338 QPixmap NETGENPluginGUI_HypothesisCreator::icon() const
339 {
340   QString hypIconName = tr( QString("ICON_DLG_NETGEN_PARAMETERS%1").arg(myIs2D?QString("_2D"):QString("")) );
341   return SUIT_Session::session()->resourceMgr()->loadPixmap( "NETGENPlugin", hypIconName );
342 }
343
344 QString NETGENPluginGUI_HypothesisCreator::type() const
345 {
346   return tr( QString( "NETGEN_%1_HYPOTHESIS" ).arg(myIs2D?QString("2D"):QString("3D")) );
347 }
348
349 QString NETGENPluginGUI_HypothesisCreator::helpPage() const
350 {
351   return "netgen_2d_3d_hypo_page.html";
352 }