Salome HOME
Fix for bug IPAL19909(Qt4 porting. Incorrect default value f(t) 1e+20 in Distribution...
[modules/smesh.git] / src / StdMeshersGUI / StdMeshersGUI_NbSegmentsCreator.cxx
1 // Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
3 //
4 // This library is free software; you can redistribute it and/or 
5 // modify it under the terms of the GNU Lesser General Public 
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License. 
8 //
9 // This library is distributed in the hope that it will be useful, 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details. 
13 //
14 // You should have received a copy of the GNU Lesser General Public 
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 // File   : StdMeshersGUI_NbSegmentsCreator.cxx
21 // Author : Open CASCADE S.A.S.
22 //
23
24 // SMESH includes
25 #include "StdMeshersGUI_NbSegmentsCreator.h"
26 #include "StdMeshersGUI_DistrTable.h"
27 #include "StdMeshersGUI_DistrPreview.h"
28
29 #include <SMESHGUI_Utils.h>
30 #include <SMESHGUI_HypothesesUtils.h>
31 #include <SMESHGUI_SpinBox.h>
32
33 // IDL includes
34 #include CORBA_SERVER_HEADER(SMESH_BasicHypothesis)
35
36 // SALOME GUI includes
37 #include <SalomeApp_Tools.h>
38 #include <QtxIntSpinBox.h>
39 #include <QtxComboBox.h>
40
41 // Qt includes
42 #include <QLabel>
43 #include <QGroupBox>
44 #include <QFrame>
45 #include <QLineEdit>
46 #include <QButtonGroup>
47 #include <QRadioButton>
48 #include <QGridLayout>
49 #include <QVBoxLayout>
50 #include <QHBoxLayout>
51 #include <QApplication>
52
53 #define SPACING 6
54 #define MARGIN  11
55
56 StdMeshersGUI_NbSegmentsCreator::StdMeshersGUI_NbSegmentsCreator()
57 : StdMeshersGUI_StdHypothesisCreator( "NumberOfSegments" ),
58   myNbSeg( 0 ),
59   myDistr( 0 ),
60   myScale( 0 ),
61   myTable( 0 ),
62   myPreview( 0 ),
63   myExpr( 0 ),
64   myConvBox( 0 ),
65   myConv( 0 ),
66   myLScale( 0 ),
67   myLTable( 0 ),
68   myLExpr( 0 ),
69   myInfo( 0 ),
70   myGroupLayout( 0 ),
71   myTableRow( 0 ),
72   myPreviewRow( 0 )
73 {
74 }
75
76 StdMeshersGUI_NbSegmentsCreator::~StdMeshersGUI_NbSegmentsCreator()
77 {
78 }
79
80 bool StdMeshersGUI_NbSegmentsCreator::checkParams() const
81 {
82   NbSegmentsHypothesisData data_old, data_new;
83   readParamsFromHypo( data_old );
84   readParamsFromWidgets( data_new );
85   bool res = storeParamsToHypo( data_new );
86   storeParamsToHypo( data_old );
87   return res;
88 }
89
90 QFrame* StdMeshersGUI_NbSegmentsCreator::buildFrame()
91 {
92   QFrame* fr = new QFrame();
93
94   QVBoxLayout* lay = new QVBoxLayout( fr );
95   lay->setMargin( 0 );
96   lay->setSpacing( 0 );
97
98   QGroupBox* GroupC1 = new QGroupBox( tr( "SMESH_ARGUMENTS" ), fr );
99   lay->addWidget( GroupC1 );
100
101   StdMeshers::StdMeshers_NumberOfSegments_var h =
102     StdMeshers::StdMeshers_NumberOfSegments::_narrow( hypothesis() );
103   myPreview = new StdMeshersGUI_DistrPreview( GroupC1, h.in() );
104
105   myGroupLayout = new QGridLayout( GroupC1 );
106   myGroupLayout->setSpacing( SPACING );
107   myGroupLayout->setMargin( MARGIN );
108   myGroupLayout->setColumnStretch( 0, 0 );
109   myGroupLayout->setColumnStretch( 1, 1 );
110
111   int row = 0;
112   // 0)  name
113   myName = 0;
114   if( isCreation() )
115   {
116     myName = new QLineEdit( GroupC1 );
117     myGroupLayout->addWidget( new QLabel( tr( "SMESH_NAME" ), GroupC1 ), row, 0 );
118     myGroupLayout->addWidget( myName, row, 1 );
119     row++;
120   }
121
122   // 1)  number of segments
123   myGroupLayout->addWidget( new QLabel( tr( "SMESH_NB_SEGMENTS_PARAM" ), GroupC1 ), row, 0 );
124   myNbSeg = new QtxIntSpinBox( GroupC1 );
125   myNbSeg->setMinimum( 1 );
126   myNbSeg->setMaximum( 9999 );
127   myGroupLayout->addWidget( myNbSeg, row, 1 );
128   row++;
129
130   // 2)  type of distribution
131   myGroupLayout->addWidget( new QLabel( tr( "SMESH_DISTR_TYPE" ), GroupC1 ), row, 0 );
132   myDistr = new QtxComboBox( GroupC1 );
133   QStringList types;
134   types.append( tr( "SMESH_DISTR_REGULAR" ) );
135   types.append( tr( "SMESH_DISTR_SCALE"   ) );
136   types.append( tr( "SMESH_DISTR_TAB"     ) );
137   types.append( tr( "SMESH_DISTR_EXPR"    ) );
138   myDistr->addItems( types );
139   myGroupLayout->addWidget( myDistr, row, 1 );
140   row++;
141
142   // 3)  scale
143   myGroupLayout->addWidget( myLScale = new QLabel( tr( "SMESH_NB_SEGMENTS_SCALE_PARAM" ), GroupC1 ), row, 0 );
144   myScale = new SMESHGUI_SpinBox( GroupC1 );
145   myScale->RangeStepAndValidator( 1E-5, 1E+5, 0.1, 6 );
146   myGroupLayout->addWidget( myScale, row, 1 );
147   row++;
148
149   myInfo = new QLabel( tr( "SMESH_FUNC_DOMAIN" ), GroupC1 );
150   myGroupLayout->addWidget( myInfo, row, 0, 1, 2 );
151   row++;
152   
153   // 4)  table
154   myGroupLayout->addWidget( myLTable = new QLabel( tr( "SMESH_TAB_FUNC" ), GroupC1 ), row, 0 );
155   myTable = new StdMeshersGUI_DistrTableFrame( GroupC1 );
156   myGroupLayout->addWidget( myTable, row, 1 );
157   myGroupLayout->setRowStretch( row, 1 );
158   myTableRow = row;
159   row++;
160
161   // 5)  expression
162   myGroupLayout->addWidget( myLExpr = new QLabel( tr( "SMESH_EXPR_FUNC" ), GroupC1 ), row, 0 );
163   myExpr = new QLineEdit( GroupC1 );
164   myGroupLayout->addWidget( myExpr, row, 1 );
165   row++;
166
167   // 6)  conversion (radiogroup)
168   myConvBox = new QGroupBox( tr( "SMESH_CONV_MODE" ), GroupC1 );
169   myConv = new QButtonGroup( GroupC1 );
170
171   QHBoxLayout* convLay = new QHBoxLayout( myConvBox );
172   convLay->setMargin( MARGIN );
173   convLay->setSpacing( SPACING );
174
175   QRadioButton* rbExp = new QRadioButton( tr( "SMESH_EXP_MODE" ), myConvBox );
176   QRadioButton* myCutNeg = new QRadioButton( tr( "SMESH_CUT_NEG_MODE" ), myConvBox );
177
178   convLay->addWidget( rbExp );
179   convLay->addWidget( myCutNeg );
180   myConv->addButton( rbExp, 0 );
181   myConv->addButton( myCutNeg, 1 );
182
183   myGroupLayout->addWidget( myConvBox, row, 0, 1, 2 );
184   row++;
185
186   // 7) distribution preview
187   myGroupLayout->addWidget( myPreview, row, 0, 1, 2 );
188   myGroupLayout->setRowStretch( row, 1 );
189   myPreviewRow = row;
190   row++;
191
192   connect( myNbSeg, SIGNAL( valueChanged( const QString& ) ), this, SLOT( onValueChanged() ) );
193   connect( myDistr, SIGNAL( activated( int ) ), this, SLOT( onValueChanged() ) );
194   connect( myTable, SIGNAL( valueChanged( int, int ) ), this, SLOT( onValueChanged() ) );
195   connect( myExpr,  SIGNAL( textChanged( const QString& ) ), this, SLOT( onValueChanged() ) );
196   connect( myConv,  SIGNAL( cuttonClicked( int ) ), this, SLOT( onValueChanged() ) );
197
198   return fr;
199 }
200
201 void StdMeshersGUI_NbSegmentsCreator::retrieveParams() const
202 {
203   NbSegmentsHypothesisData data;
204   readParamsFromHypo( data );
205
206   if( myName )
207     myName->setText( data.myName );
208   myNbSeg->setValue( data.myNbSeg );
209   myDistr->setCurrentIndex( data.myDistrType );
210   myScale->setValue( data.myScale );
211   myConv->button( data.myConv )->setChecked( true );
212   myTable->setFuncMinValue(myConv->checkedId()==0 ? -1E20 : 0);
213   myTable->setData( data.myTable );
214   myExpr->setText( data.myExpr );
215
216   if ( dlg() )
217     dlg()->setMinimumSize( dlg()->minimumSizeHint().width(), dlg()->minimumSizeHint().height() );
218 }
219
220 QString StdMeshersGUI_NbSegmentsCreator::storeParams() const
221 {
222   NbSegmentsHypothesisData data;
223   readParamsFromWidgets( data );
224   storeParamsToHypo( data );
225
226   QString valStr = QString::number( data.myNbSeg ) += "; ";
227
228   enum DistrType
229   {
230     Regular, //!< equidistant distribution
231     Scale,   //!< scale distribution
232     TabFunc, //!< distribution with density function presented by table
233     ExprFunc //!< distribution with density function presented by expression
234   };
235   bool hasConv = false;
236   switch ( data.myDistrType ) {
237   case Regular :
238     valStr += tr("SMESH_DISTR_REGULAR");
239     break;
240   case Scale   :
241     valStr += tr("SMESH_NB_SEGMENTS_SCALE_PARAM") + " = " + QString::number( data.myScale );
242     break;
243   case TabFunc : {
244     //valStr += tr("SMESH_TAB_FUNC");
245     bool param = true;
246     for( int i=0; i < data.myTable.length(); i++, param = !param ) {
247       if ( param )
248         valStr += "[";
249       valStr += QString::number( data.myTable[ i ]);
250       valStr += ( param ? "," : "]" );
251     }
252     hasConv = true;
253     break;
254   }
255   case ExprFunc:
256     valStr += data.myExpr;
257     hasConv = true;
258     break;
259   }
260   if ( hasConv )
261     if ( data.myConv )
262       valStr += "; " + tr("SMESH_CUT_NEG_MODE");
263     else
264       valStr += "; " + tr("SMESH_EXP_MODE");
265
266   return valStr;
267 }
268
269 bool StdMeshersGUI_NbSegmentsCreator::readParamsFromHypo( NbSegmentsHypothesisData& h_data ) const
270 {
271   StdMeshers::StdMeshers_NumberOfSegments_var h =
272     StdMeshers::StdMeshers_NumberOfSegments::_narrow( initParamsHypothesis() );
273
274   h_data.myName = hypName();
275
276   h_data.myNbSeg = (int) h->GetNumberOfSegments();
277   int distr = (int) h->GetDistrType();
278   h_data.myDistrType = distr;
279   h_data.myScale = distr==1 ? h->GetScaleFactor() : 1.0;
280   if( distr==2 )
281   {
282     SMESH::double_array* a = h->GetTableFunction();
283     h_data.myTable = *a;
284     delete a;
285   }
286   else
287   {
288     SMESH::double_array& a = h_data.myTable;
289     // by default, constant table function f(t)=1
290     a.length( 4 );
291     a[0] = 0.0; a[1] = 1.0;
292     a[2] = 1.0; a[3] = 1.0; 
293   }
294
295   h_data.myExpr = distr==3 ? h->GetExpressionFunction() : "1";
296   h_data.myConv = distr==2 || distr==3 ? h->ConversionMode() : 1; /*cut negative by default*/
297
298   return true;
299 }
300
301 bool StdMeshersGUI_NbSegmentsCreator::storeParamsToHypo( const NbSegmentsHypothesisData& h_data ) const
302 {
303   StdMeshers::StdMeshers_NumberOfSegments_var h =
304     StdMeshers::StdMeshers_NumberOfSegments::_narrow( hypothesis() );
305
306   bool ok = true;
307   try
308   {
309     if( isCreation() )
310       SMESH::SetName( SMESH::FindSObject( h ), h_data.myName.toLatin1().data() );
311
312     h->SetNumberOfSegments( h_data.myNbSeg );
313     int distr = h_data.myDistrType;
314     h->SetDistrType( distr );
315
316     if( distr==1 )
317       h->SetScaleFactor( h_data.myScale );
318
319     if( distr==2 || distr==3 )
320       h->SetConversionMode( h_data.myConv );
321
322     if( distr==2 )
323       h->SetTableFunction( h_data.myTable );
324
325     if( distr==3 )
326       h->SetExpressionFunction( h_data.myExpr.toLatin1().data() );
327     //setting of function must follow after setConversionMode, because otherwise
328     //the function will be checked with old conversion mode, so that it may occurs
329     //unexpected errors for user
330   }
331   catch(const SALOME::SALOME_Exception& ex)
332   {
333     SalomeApp_Tools::QtCatchCorbaException(ex);
334     ok = false;
335   }
336   return ok;
337 }
338
339 bool StdMeshersGUI_NbSegmentsCreator::readParamsFromWidgets( NbSegmentsHypothesisData& h_data ) const
340 {
341   h_data.myName      = myName ? myName->text() : "";
342   h_data.myNbSeg     = myNbSeg->value();
343   h_data.myDistrType = myDistr->currentIndex();
344   h_data.myConv      = myConv->checkedId();
345   h_data.myScale     = myScale->value();
346   myTable->data( h_data.myTable );
347   h_data.myExpr      = myExpr->text();
348   return true;
349 }
350
351 void StdMeshersGUI_NbSegmentsCreator::onValueChanged()
352 {
353   int distr = myDistr->currentIndex();
354
355 /*  if( distr==2 ) //table func
356     myCutNeg->setText( tr( "SMESH_NO_CONV" ) );
357   else if( distr==3 )
358     myCutNeg->setText( tr( "SMESH_CUT_NEG_MODE" ) );*/
359
360   if( distr==2 && sender()==myConv ) //table func
361   {
362     myTable->setFuncMinValue( myConv->checkedId()==0 ? -1E20 : 0 );
363     SMESH::double_array arr;
364     myTable->data( arr );
365     myTable->setData( arr ); //update data in table
366   }
367
368   myScale->setShown( distr==1 );
369   myLScale->setShown( distr==1 );
370
371   bool isFunc = distr==2 || distr==3;
372   myPreview->setShown( isFunc );
373   myGroupLayout->setRowStretch( myPreviewRow, isFunc ? 1 : 0 );
374
375   myConvBox->setShown( isFunc );
376
377   if( distr==2 )
378     myTable->show();
379   else
380     myTable->hide();
381   myLTable->setShown( distr==2 );
382   myGroupLayout->setRowStretch( myTableRow, distr==2 ? 1 : 0 );
383
384   myExpr->setShown( distr==3 );
385   myLExpr->setShown( distr==3 );
386   myInfo->setShown( isFunc );
387
388   //change of preview
389   int nbSeg = myNbSeg->value();
390   if( distr==2 ) //preview for table-described function
391   {
392     SMESH::double_array a;
393     myTable->data( a );
394     myPreview->setParams( a, nbSeg, false );
395   }
396   else if( distr==3 ) //preview for analytic-described function
397     myPreview->setParams( myExpr->text(), nbSeg, 100, false );
398
399   if( isFunc )
400     myPreview->setConversion( StdMeshersGUI_DistrPreview::Conversion( myConv->checkedId() ) );
401
402   if ( (QtxComboBox*)sender() == myDistr && dlg() ) {
403     QApplication::instance()->processEvents();
404     dlg()->updateGeometry();
405     dlg()->setMinimumSize( dlg()->minimumSizeHint().width(), dlg()->minimumSizeHint().height() );
406     dlg()->resize( dlg()->minimumSize() );
407   }
408 }