Salome HOME
5f3e0a9c90fbf0b968be3802e6fa031727a99fb0
[plugins/blsurfplugin.git] / src / GUI / BLSURFPluginGUI_HypothesisCreator.cxx
1 //  BLSURFPlugin GUI: GUI for plugged-in mesher BLSURFPlugin
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.org
20 //
21 //
22 //
23 //  File   : BLSURFPluginGUI_HypothesisCreator.cxx
24 // Authors : Francis KLOSS (OCC) & Patrick LAUG (INRIA) & Lioka RAZAFINDRAZAKA (CEA)
25 //  Module : BLSURFPlugin
26 //  $Header: 
27
28 #include "BLSURFPluginGUI_HypothesisCreator.h"
29
30 #include <SMESHGUI_Utils.h>
31 #include <SMESHGUI_HypothesesUtils.h>
32
33 #include CORBA_SERVER_HEADER(BLSURFPlugin_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 PhysicalMesh
51   {
52     DefaultSize,
53     PhysicalUserDefined
54   };
55
56 enum GeometricMesh
57   {
58     DefaultGeom,
59     UserDefined
60   };
61
62 BLSURFPluginGUI_HypothesisCreator::BLSURFPluginGUI_HypothesisCreator( const QString& theHypType )
63 : SMESHGUI_GenericHypothesisCreator( theHypType ),
64   myIs2D(true)
65 {
66 }
67
68 BLSURFPluginGUI_HypothesisCreator::~BLSURFPluginGUI_HypothesisCreator()
69 {
70 }
71
72 bool BLSURFPluginGUI_HypothesisCreator::checkParams() const
73 {
74   BlsurfHypothesisData data_old, data_new;
75   readParamsFromHypo( data_old );
76   readParamsFromWidgets( data_new );
77   bool res = storeParamsToHypo( data_new );
78   return res;
79 }
80
81 QFrame* BLSURFPluginGUI_HypothesisCreator::buildFrame()
82 {
83   QFrame* fr = new QFrame( 0, "myframe" );
84   QVBoxLayout* lay = new QVBoxLayout( fr, 5, 0 );
85
86   QGroupBox* GroupC1 = new QGroupBox( 2, Qt::Horizontal, fr, "GroupC1" );
87   lay->addWidget( GroupC1 );
88   
89   GroupC1->setTitle( tr( "SMESH_ARGUMENTS"  ) );
90   GroupC1->layout()->setSpacing( 6 );
91   GroupC1->layout()->setMargin( 11 );
92   
93   myName = 0;
94   if( isCreation() ) {
95     new QLabel( tr( "SMESH_NAME" ), GroupC1 );
96     myName = new QLineEdit( GroupC1 );
97   }
98
99   new QLabel( tr( "BLSURF_PHY_MESH" ), GroupC1 );
100   myPhysicalMesh = new QtxComboBox( GroupC1 );
101   QStringList physicalTypes;
102   physicalTypes.append( QObject::tr( "BLSURF_DEFAULT_USER" ) );
103   physicalTypes.append( QObject::tr( "BLSURF_CUSTOM_USER" ) );
104   myPhysicalMesh->insertStringList( physicalTypes );
105
106   new QLabel( tr( "BLSURF_HPHYDEF" ), GroupC1 );
107   myPhySize = new QtxDblSpinBox( GroupC1 );
108   myPhySize->setMinValue( 1e-02 );
109   myPhySize->setMaxValue( 1e+02 );
110   myPhySize->setLineStep( 1 );
111
112   new QLabel( tr( "BLSURF_GEOM_MESH" ), GroupC1 );
113   myGeometricMesh = new QtxComboBox( GroupC1 );
114   QStringList types;
115   types.append( QObject::tr( "BLSURF_DEFAULT_GEOM" ) );
116   types.append( QObject::tr( "BLSURF_CUSTOM_GEOM" ) );
117   myGeometricMesh->insertStringList( types );
118
119 //   new QLabel( tr( "BLSURF_GROWTH_RATE" ), GroupC1 );
120 //   myGrowthRate = new QtxDblSpinBox( GroupC1 );
121 //   myGrowthRate->setMinValue( 0.1 );
122 //   myGrowthRate->setMaxValue( 10 );
123 //   myGrowthRate->setLineStep( 0.1 );
124
125   new QLabel( tr( "BLSURF_ANGLE_MESH_S" ), GroupC1 );
126   myAngleMeshS = new QtxDblSpinBox( GroupC1 );
127   myAngleMeshS->setMinValue( 0 );
128   myAngleMeshS->setMaxValue( 16 );
129   myAngleMeshS->setLineStep( 0.5 );
130   
131   new QLabel( tr( "BLSURF_GRADATION" ), GroupC1 );
132   myGradation = new QtxDblSpinBox( GroupC1 );
133   myGradation->setMinValue( 1.1 );
134   myGradation->setMaxValue( 1.5 );
135   myGradation->setLineStep( 0.1 );
136
137 //   new QLabel( tr( "BLSURF_SEG_PER_RADIUS" ), GroupC1 );
138 //   myNbSegPerRadius = new QtxDblSpinBox( GroupC1 );
139 //   myNbSegPerRadius->setMinValue( 0.2 );
140 //   myNbSegPerRadius->setMaxValue( 5.0 );
141
142   myAllowQuadrangles = new QCheckBox( tr( "BLSURF_ALLOW_QUADRANGLES" ), GroupC1 );
143   GroupC1->addSpace(0);
144 //   myIs2D = true;
145
146   myDecimesh = new QCheckBox( tr( "BLSURF_DECIMESH" ), GroupC1 );
147   GroupC1->addSpace(0);
148
149   connect( myGeometricMesh, SIGNAL( activated( int ) ), this, SLOT( onGeometricMeshChanged() ) );
150   connect( myPhysicalMesh,  SIGNAL( activated( int ) ), this, SLOT( onPhysicalMeshChanged() ) );
151
152   return fr;
153 }
154
155 void BLSURFPluginGUI_HypothesisCreator::retrieveParams() const
156 {
157   BlsurfHypothesisData data;
158   readParamsFromHypo( data );
159
160   if( myName )
161     myName->setText( data.myName );
162   myPhysicalMesh->setCurrentItem( data.myPhysicalMesh );
163   myPhySize->setValue( data.myPhySize );
164   myGeometricMesh->setCurrentItem( data.myGeometricMesh );
165   myAngleMeshS->setValue( data.myAngleMeshS);
166   myGradation->setValue( data.myGradation);
167   myAllowQuadrangles->setChecked( data.myAllowQuadrangles );
168   myDecimesh->setChecked( data.myDecimesh );
169
170   // update widgets
171
172   bool isPhysicalCustom = (myPhysicalMesh->currentItem() == PhysicalUserDefined);
173   myPhySize->setEnabled(isPhysicalCustom);
174
175   bool isCustom = (myGeometricMesh->currentItem() == UserDefined);
176   myAngleMeshS->setEnabled(isCustom);
177   myGradation->setEnabled(isCustom);
178 }
179
180 QString BLSURFPluginGUI_HypothesisCreator::storeParams() const
181 {
182   BlsurfHypothesisData data;
183   readParamsFromWidgets( data );
184   storeParamsToHypo( data );
185
186   QString guiHyp;
187   guiHyp += tr("BLSURF_PHY_MESH") + " = " + QString::number( data.myPhysicalMesh ) + "; ";
188   guiHyp += tr("BLSURF_HPHYDEF") + " = " + QString::number( data.myPhySize ) + "; ";
189   guiHyp += tr("BLSURF_GEOM_MESH") + " = " + QString::number( data.myGeometricMesh ) + "; ";
190   guiHyp += tr("BLSURF_ANGLE_MESH_S") + " = " + QString::number( data.myAngleMeshS ) + "; ";
191   guiHyp += tr("BLSURF_GRADATION") + " = " + QString::number( data.myGradation ) + "; ";
192   guiHyp += tr("BLSURF_ALLOW_QUADRANGLES") + " = " + QString(data.myAllowQuadrangles ? "yes" : "no") + "; ";
193   guiHyp += tr("BLSURF_DECIMESH") + " = " + QString(data.myDecimesh ? "yes" : "no") + "; ";
194
195   cout << "guiHyp : " << guiHyp << endl;
196
197   return guiHyp;
198 }
199
200 bool BLSURFPluginGUI_HypothesisCreator::readParamsFromHypo( BlsurfHypothesisData& h_data ) const
201 {
202   BLSURFPlugin::BLSURFPlugin_Hypothesis_var h =
203     BLSURFPlugin::BLSURFPlugin_Hypothesis::_narrow( initParamsHypothesis() );
204
205   HypothesisData* data = SMESH::GetHypothesisData( hypType() );
206   h_data.myName = isCreation() && data ? data->Label : "";
207
208   h_data.myPhysicalMesh = (int) h->GetPhysicalMesh();
209   h_data.myPhySize = h->GetPhySize();
210   h_data.myGeometricMesh = (int) h->GetGeometricMesh();
211   h_data.myAngleMeshS = h->GetAngleMeshS();
212   h_data.myGradation = h->GetGradation();
213   h_data.myAllowQuadrangles = h->GetQuadAllowed();
214   h_data.myDecimesh = h->GetDecimesh();
215
216   return true;
217 }
218
219 bool BLSURFPluginGUI_HypothesisCreator::storeParamsToHypo( const BlsurfHypothesisData& h_data ) const
220 {
221   BLSURFPlugin::BLSURFPlugin_Hypothesis_var h =
222     BLSURFPlugin::BLSURFPlugin_Hypothesis::_narrow( hypothesis() );
223
224   bool ok = true;
225   try
226   {
227     if( isCreation() )
228       SMESH::SetName( SMESH::FindSObject( h ), h_data.myName.latin1() );
229
230     h->SetPhysicalMesh( (int) h_data.myPhysicalMesh );
231     h->SetPhySize( h_data.myPhySize );
232     h->SetGeometricMesh( (int) h_data.myGeometricMesh );
233     h->SetGradation( h_data.myGradation );
234     h->SetQuadAllowed( h_data.myAllowQuadrangles );
235     h->SetDecimesh( h_data.myDecimesh );
236
237     if( (int) h_data.myPhysicalMesh == PhysicalUserDefined )
238       h->SetPhySize( h_data.myPhySize );
239
240     if( (int) h_data.myGeometricMesh == UserDefined )
241       h->SetAngleMeshS( h_data.myAngleMeshS );
242   }
243   catch(const SALOME::SALOME_Exception& ex)
244   {
245     SalomeApp_Tools::QtCatchCorbaException(ex);
246     ok = false;
247   }
248   return ok;
249 }
250
251 bool BLSURFPluginGUI_HypothesisCreator::readParamsFromWidgets( BlsurfHypothesisData& h_data ) const
252 {
253   h_data.myName             = myName ? myName->text() : "";
254   h_data.myPhysicalMesh     = myPhysicalMesh->currentItem();
255   h_data.myPhySize          = myPhySize->value();
256   h_data.myGeometricMesh    = myGeometricMesh->currentItem();
257   h_data.myAngleMeshS       = myAngleMeshS->value();
258   h_data.myGradation        = myGradation->value();
259   h_data.myAllowQuadrangles = myAllowQuadrangles->isChecked();
260   h_data.myDecimesh         = myDecimesh->isChecked();
261
262   return true;
263 }
264
265 void BLSURFPluginGUI_HypothesisCreator::onPhysicalMeshChanged() {
266   bool isCustom = (myPhysicalMesh->currentItem() == PhysicalUserDefined);
267   myPhySize->setEnabled(isCustom);
268
269   if ( ! isCustom ) {
270     double aPhySize;
271     switch( myPhysicalMesh->currentItem() ) {
272       case DefaultSize:
273       default:
274         aPhySize = 10;
275         break;
276       }
277     myPhySize->setValue( aPhySize );
278   }
279 }
280
281 void BLSURFPluginGUI_HypothesisCreator::onGeometricMeshChanged() {
282   bool isCustom = (myGeometricMesh->currentItem() == UserDefined);
283   myAngleMeshS->setEnabled(isCustom);
284   myGradation->setEnabled(isCustom);
285
286   if ( ! isCustom ) {
287     double aAngleMeshS, aGradation;
288     switch( myGeometricMesh->currentItem() ) {
289       case DefaultGeom:
290       default:
291         aAngleMeshS = 8;
292         aGradation = 1.1;
293         break;
294       }
295     myAngleMeshS->setValue( aAngleMeshS );
296     myGradation->setValue( aGradation );
297   }
298 }
299
300 QString BLSURFPluginGUI_HypothesisCreator::caption() const
301 {
302   return tr( QString( "BLSURF_%1_TITLE" ).arg(myIs2D?QString("2D"):QString("3D")) );
303 }
304
305 QPixmap BLSURFPluginGUI_HypothesisCreator::icon() const
306 {
307   QString hypIconName = tr( QString("ICON_DLG_BLSURF_PARAMETERS%1").arg(myIs2D?QString(""):QString("")) );
308   return SUIT_Session::session()->resourceMgr()->loadPixmap( "BLSURFPlugin", hypIconName );
309 }
310
311 QString BLSURFPluginGUI_HypothesisCreator::type() const
312 {
313   return tr( QString( "BLSURF_%1_HYPOTHESIS" ).arg(myIs2D?QString("2D"):QString("3D")) );
314 }