Salome HOME
Update mail address
[modules/smesh.git] / src / StdMeshersGUI / StdMeshersGUI_StdHypothesisCreator.cxx
1 //  SMESH StdMeshersGUI : GUI for plugged-in meshers
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   : StdMeshersGUI_StdHypothesisCreator.cxx
24 //  Author : Alexander SOLOVYOV
25 //  Module : SMESH
26 //  $Header: /home/server/cvs/SMESH/SMESH_SRC/src/StdMeshersGUI/StdMeshersGUI_StdHypothesisCreator.cxx
27
28 #include "StdMeshersGUI_StdHypothesisCreator.h"
29
30 #include <SMESHGUI.h>
31 #include <SMESHGUI_SpinBox.h>
32 #include <SMESHGUI_HypothesesUtils.h>
33 #include <SMESHGUI_Utils.h>
34
35 #include <SUIT_ResourceMgr.h>
36
37 #include <SALOMEconfig.h>
38 #include CORBA_SERVER_HEADER(SMESH_BasicHypothesis)
39 #include CORBA_SERVER_HEADER(SMESH_Mesh)
40
41 #include <qpixmap.h>
42 #include <qhbox.h>
43 #include <qslider.h>
44 #include <qlabel.h>
45
46
47 const double VALUE_MAX = 1.0e+15, // COORD_MAX
48              VALUE_MAX_2  = VALUE_MAX * VALUE_MAX,
49              VALUE_MAX_3  = VALUE_MAX_2 * VALUE_MAX,
50              VALUE_SMALL = 1.0e-15,
51              VALUE_SMALL_2 = VALUE_SMALL * VALUE_SMALL,
52              VALUE_SMALL_3 = VALUE_SMALL_2 * VALUE_SMALL;
53
54 StdMeshersGUI_StdHypothesisCreator::StdMeshersGUI_StdHypothesisCreator( const QString& type )
55 : SMESHGUI_GenericHypothesisCreator( type )
56 {
57 }
58
59 StdMeshersGUI_StdHypothesisCreator::~StdMeshersGUI_StdHypothesisCreator()
60 {
61 }
62
63 QFrame* StdMeshersGUI_StdHypothesisCreator::buildFrame()
64 {
65   return buildStdFrame();
66 }
67
68 bool StdMeshersGUI_StdHypothesisCreator::checkParams() const
69 {
70   return true;
71 }
72
73 void StdMeshersGUI_StdHypothesisCreator::retrieveParams() const
74 {
75   //here this method must be empty because buildStdParam sets values itself
76 }
77
78 QString StdMeshersGUI_StdHypothesisCreator::storeParams() const
79 {
80   ListOfStdParams params;
81   bool res = getStdParamFromDlg( params );
82   if( isCreation() )
83   {
84     SMESH::SetName( SMESH::FindSObject( hypothesis() ), params[0].myValue.toString().latin1() );
85     params.remove( params.begin() );
86   }
87
88   QString valueStr = stdParamValues( params );
89
90   if( res && !params.isEmpty() )
91   {
92     if( hypType()=="LocalLength" )
93     {
94       StdMeshers::StdMeshers_LocalLength_var h =
95         StdMeshers::StdMeshers_LocalLength::_narrow( hypothesis() );
96
97       h->SetLength( params[0].myValue.toDouble() );
98     }
99     else if( hypType()=="Arithmetic1D" )
100     {
101       StdMeshers::StdMeshers_Arithmetic1D_var h =
102         StdMeshers::StdMeshers_Arithmetic1D::_narrow( hypothesis() );
103
104       h->SetLength( params[0].myValue.toDouble(), true );
105       h->SetLength( params[1].myValue.toDouble(), false );
106     }
107     else if( hypType()=="MaxElementArea" )
108     {
109       StdMeshers::StdMeshers_MaxElementArea_var h =
110         StdMeshers::StdMeshers_MaxElementArea::_narrow( hypothesis() );
111
112       h->SetMaxElementArea( params[0].myValue.toDouble() );
113     }
114     else if( hypType()=="MaxElementVolume" )
115     {
116       StdMeshers::StdMeshers_MaxElementVolume_var h =
117         StdMeshers::StdMeshers_MaxElementVolume::_narrow( hypothesis() );
118
119       h->SetMaxElementVolume( params[0].myValue.toDouble() );
120     }
121     else if( hypType()=="StartEndLength" )
122     {
123       StdMeshers::StdMeshers_StartEndLength_var h =
124         StdMeshers::StdMeshers_StartEndLength::_narrow( hypothesis() );
125
126       h->SetLength( params[0].myValue.toDouble(), true );
127       h->SetLength( params[1].myValue.toDouble(), false );
128     }
129     else if( hypType()=="Deflection1D" )
130     {
131       StdMeshers::StdMeshers_Deflection1D_var h =
132         StdMeshers::StdMeshers_Deflection1D::_narrow( hypothesis() );
133
134       h->SetDeflection( params[0].myValue.toDouble() );
135     }
136     else if( hypType()=="AutomaticLength" )
137     {
138       StdMeshers::StdMeshers_AutomaticLength_var h =
139         StdMeshers::StdMeshers_AutomaticLength::_narrow( hypothesis() );
140
141       h->SetFineness( params[0].myValue.toDouble() );
142     }
143   }
144   return valueStr;
145 }
146
147 bool StdMeshersGUI_StdHypothesisCreator::stdParams( ListOfStdParams& p ) const
148 {
149   bool res = true;
150   SMESHGUI_GenericHypothesisCreator::StdParam item;
151
152   p.clear();
153   if( isCreation() )
154   {
155     HypothesisData* data = SMESH::GetHypothesisData( hypType() );
156     item.myName = tr( "SMESH_NAME" );
157     item.myValue = data ? data->Label : QString();
158     p.append( item );
159   }
160
161   SMESH::SMESH_Hypothesis_var hyp = initParamsHypothesis();
162
163   if( hypType()=="LocalLength" )
164   {
165     StdMeshers::StdMeshers_LocalLength_var h =
166       StdMeshers::StdMeshers_LocalLength::_narrow( hyp );
167
168     item.myName = tr("SMESH_LOCAL_LENGTH_PARAM");
169     item.myValue = h->GetLength();
170     p.append( item );
171   }
172   else if( hypType()=="Arithmetic1D" )
173   {
174     StdMeshers::StdMeshers_Arithmetic1D_var h =
175       StdMeshers::StdMeshers_Arithmetic1D::_narrow( hyp );
176
177     item.myName = tr( "SMESH_START_LENGTH_PARAM" );
178     item.myValue = h->GetLength( true );
179     p.append( item );
180     item.myName = tr( "SMESH_END_LENGTH_PARAM" );
181     item.myValue = h->GetLength( false );
182     p.append( item );
183   }
184   else if( hypType()=="MaxElementArea" )
185   {
186     StdMeshers::StdMeshers_MaxElementArea_var h =
187       StdMeshers::StdMeshers_MaxElementArea::_narrow( hyp );
188
189     item.myName = tr( "SMESH_MAX_ELEMENT_AREA_PARAM" );
190     item.myValue = h->GetMaxElementArea();
191     p.append( item );
192   }
193   else if( hypType()=="MaxElementVolume" )
194   {
195     StdMeshers::StdMeshers_MaxElementVolume_var h =
196       StdMeshers::StdMeshers_MaxElementVolume::_narrow( hyp );
197
198     item.myName = tr( "SMESH_MAX_ELEMENT_VOLUME_PARAM" );
199     item.myValue = h->GetMaxElementVolume();
200     p.append( item );
201   }
202   else if( hypType()=="StartEndLength" )
203   {
204     StdMeshers::StdMeshers_StartEndLength_var h =
205       StdMeshers::StdMeshers_StartEndLength::_narrow( hyp );
206
207     item.myName = tr( "SMESH_START_LENGTH_PARAM" );
208     item.myValue = h->GetLength( true );
209     p.append( item );
210     item.myName = tr( "SMESH_END_LENGTH_PARAM" );
211     item.myValue = h->GetLength( false );
212     p.append( item );
213   }
214   else if( hypType()=="Deflection1D" )
215   {
216     StdMeshers::StdMeshers_Deflection1D_var h =
217       StdMeshers::StdMeshers_Deflection1D::_narrow( hyp );
218
219     item.myName = tr( "SMESH_DEFLECTION1D_PARAM" );
220     item.myValue = h->GetDeflection();
221     p.append( item );
222   }
223   else if( hypType()=="AutomaticLength" )
224   {
225     StdMeshers::StdMeshers_AutomaticLength_var h =
226       StdMeshers::StdMeshers_AutomaticLength::_narrow( hyp );
227
228     item.myName = tr( "SMESH_FINENESS_PARAM" );
229     item.myValue = h->GetFineness();
230     p.append( item );
231   }
232   else
233     res = false;
234   return res;
235 }
236
237 void StdMeshersGUI_StdHypothesisCreator::attuneStdWidget( QWidget* w, const int ) const
238 {
239   SMESHGUI_SpinBox* sb = w->inherits( "SMESHGUI_SpinBox" ) ? ( SMESHGUI_SpinBox* )w : 0;
240   if( hypType()=="LocalLength" &&  sb )
241   {
242     sb->RangeStepAndValidator( VALUE_SMALL, VALUE_MAX, 1.0, 6 );
243   }
244   else if( hypType()=="Arithmetic1D" && sb )
245   {
246     sb->RangeStepAndValidator( VALUE_SMALL, VALUE_MAX, 1.0, 6 );
247   }
248   else if( hypType()=="MaxElementArea" && sb )
249   {
250     sb->RangeStepAndValidator( VALUE_SMALL_2, VALUE_MAX_2, 1.0, 6 );
251   }
252   else if( hypType()=="MaxElementVolume" && sb )
253   {
254     sb->RangeStepAndValidator( VALUE_SMALL_3, VALUE_MAX_3, 1.0, 6 );
255   }
256   else if( hypType()=="StartEndLength" && sb )
257   {
258     sb->RangeStepAndValidator( VALUE_SMALL, VALUE_MAX, 1.0, 6 );
259   }
260   else if( hypType()=="Deflection1D" && sb )
261   {
262     sb->RangeStepAndValidator( VALUE_SMALL, VALUE_MAX, 1.0, 6 );
263   }
264   else if ( sb ) // default validator for possible ancestors
265   {
266     sb->RangeStepAndValidator( VALUE_SMALL, VALUE_MAX, 1.0, 6 );
267   }
268 }
269
270 QString StdMeshersGUI_StdHypothesisCreator::caption() const
271 {
272   return tr( QString( "SMESH_%1_TITLE" ).arg( hypTypeName( hypType() ) ) );
273 }
274
275 QPixmap StdMeshersGUI_StdHypothesisCreator::icon() const
276 {
277   QString hypIconName = tr( QString( "ICON_DLG_%1" ).arg( hypTypeName( hypType() ) ) );
278   return SMESHGUI::resourceMgr()->loadPixmap( "SMESH", hypIconName );
279 }
280
281 QString StdMeshersGUI_StdHypothesisCreator::type() const
282 {
283   return tr( QString( "SMESH_%1_HYPOTHESIS" ).arg( hypTypeName( hypType() ) ) );
284 }
285
286 QString StdMeshersGUI_StdHypothesisCreator::hypTypeName( const QString& t ) const
287 {
288   static QMap<QString,QString>  types;
289   if( types.isEmpty() )
290   {
291     types.insert( "LocalLength", "LOCAL_LENGTH" );
292     types.insert( "NumberOfSegments", "NB_SEGMENTS" );
293     types.insert( "MaxElementArea", "MAX_ELEMENT_AREA" );
294     types.insert( "MaxElementVolume", "MAX_ELEMENT_VOLUME" );
295     types.insert( "StartEndLength", "START_END_LENGTH" );
296     types.insert( "Deflection1D", "DEFLECTION1D" );
297     types.insert( "Arithmetic1D", "ARITHMETIC_1D" );
298     types.insert( "AutomaticLength", "AUTOMATIC_LENGTH" );
299   }
300
301   QString res;
302   if( types.contains( t ) )
303     res = types[ t ];
304
305   return res;
306 }
307
308 //================================================================================
309 /*!
310  * \brief Widget: slider with left and right labels
311  */
312 //================================================================================
313
314 class TDoubleSliderWith2Lables: public QHBox
315 {
316 public:
317   TDoubleSliderWith2Lables( const QString& leftLabel, const QString& rightLabel,
318                             const double   initValue, const double   bottom,
319                             const double   top      , const double   precision,
320                             QWidget *      parent=0 , const char *   name=0 )
321     :QHBox(parent,name), _bottom(bottom), _precision(precision)
322   {
323     if ( !leftLabel.isEmpty() ) (new QLabel( this ))->setText( leftLabel );
324     _slider = new QSlider( Horizontal, this );
325     _slider->setRange( 0, toInt( top ));
326     _slider->setValue( toInt( initValue ));
327     if ( !rightLabel.isEmpty() ) (new QLabel( this ))->setText( rightLabel );
328   }
329   double value() const { return _bottom + _slider->value() * _precision; }
330   QSlider * getSlider() const { return _slider; }
331   int toInt( double val ) const { return (int) ceil(( val - _bottom ) / _precision ); }
332 private:
333   double _bottom, _precision;
334   QSlider * _slider;
335 };
336
337 //=======================================================================
338 //function : getCustomWidget
339 //purpose  : 
340 //=======================================================================
341
342 QWidget* StdMeshersGUI_StdHypothesisCreator::getCustomWidget( const StdParam & param,
343                                                               QWidget*         parent) const
344 {
345   if ( hypType()=="AutomaticLength" && param.myValue.type() == QVariant::Double )
346     return new TDoubleSliderWith2Lables( "0 ", " 1", param.myValue.toDouble(),
347                                          0, 1, 0.01, parent );
348
349   return 0;
350 }
351
352 //=======================================================================
353 //function : getParamFromCustomWidget
354 //purpose  : 
355 //=======================================================================
356
357 bool StdMeshersGUI_StdHypothesisCreator::getParamFromCustomWidget( StdParam & param,
358                                                                    QWidget*   widget) const
359 {
360   if ( hypType()=="AutomaticLength" ) {
361     TDoubleSliderWith2Lables* w = dynamic_cast<TDoubleSliderWith2Lables*>( widget );
362     if ( w ) {
363       param.myValue = w->value();
364       return true;
365     }
366   }
367   return false;
368 }