Salome HOME
cfb80164e4dc55852e8e69ba36b74a7640fc2745
[modules/smesh.git] / src / StdMeshersGUI / StdMeshersGUI_NbSegmentsCreator.cxx
1 // Copyright (C) 2007-2015  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, or (at your option) any later version.
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
23 // File   : StdMeshersGUI_NbSegmentsCreator.cxx
24 // Author : Open CASCADE S.A.S.
25 // SMESH includes
26 //
27 #include "StdMeshersGUI_NbSegmentsCreator.h"
28 #include "StdMeshersGUI_DistrPreview.h"
29 #include "StdMeshersGUI_DistrTable.h"
30 #include "StdMeshersGUI_PropagationHelperWdg.h"
31 #include "StdMeshersGUI_SubShapeSelectorWdg.h"
32
33 #include <SMESHGUI.h>
34 #include <SMESHGUI_Utils.h>
35 #include <SMESHGUI_HypothesesUtils.h>
36 #include <SMESHGUI_SpinBox.h>
37
38 // IDL includes
39 #include CORBA_SERVER_HEADER(SMESH_BasicHypothesis)
40
41 // SALOME GUI includes
42 #include <SalomeApp_Tools.h>
43 #include <SalomeApp_IntSpinBox.h>
44 #include <QtxComboBox.h>
45
46 // Qt includes
47 #include <QLabel>
48 #include <QGroupBox>
49 #include <QFrame>
50 #include <QLineEdit>
51 #include <QButtonGroup>
52 #include <QRadioButton>
53 #include <QGridLayout>
54 #include <QVBoxLayout>
55 #include <QHBoxLayout>
56 #include <QApplication>
57
58 #define SPACING 6
59 #define MARGIN  11
60
61 StdMeshersGUI_NbSegmentsCreator::StdMeshersGUI_NbSegmentsCreator()
62 : StdMeshersGUI_StdHypothesisCreator( "NumberOfSegments" ),
63   myNbSeg( 0 ),
64   myDistr( 0 ),
65   myScale( 0 ),
66   myTable( 0 ),
67   myPreview( 0 ),
68   myExpr( 0 ),
69   myConvBox( 0 ),
70   myConv( 0 ),
71   myLScale( 0 ),
72   myLTable( 0 ),
73   myLExpr( 0 ),
74   myInfo( 0 ),
75   myGroupLayout( 0 ),
76   myTableRow( 0 ),
77   myPreviewRow( 0 )
78 {
79 }
80
81 StdMeshersGUI_NbSegmentsCreator::~StdMeshersGUI_NbSegmentsCreator()
82 {
83 }
84
85 bool StdMeshersGUI_NbSegmentsCreator::checkParams( QString& msg ) const
86 {
87   if( !SMESHGUI_GenericHypothesisCreator::checkParams( msg ) )
88     return false;
89   NbSegmentsHypothesisData data_old, data_new;
90   readParamsFromHypo( data_old );
91   readParamsFromWidgets( data_new );
92   bool res = storeParamsToHypo( data_new );
93   storeParamsToHypo( data_old );
94   res = myNbSeg->isValid( msg, true ) && res;
95   res = myScale->isValid( msg, true ) && res;
96   return res;
97 }
98
99 QFrame* StdMeshersGUI_NbSegmentsCreator::buildFrame()
100 {
101   QFrame* fr = new QFrame();
102   fr->setMinimumWidth(460);
103
104   QVBoxLayout* lay = new QVBoxLayout( fr );
105   lay->setMargin( 0 );
106   lay->setSpacing( SPACING );
107
108   QGroupBox* GroupC1 = new QGroupBox( tr( "SMESH_ARGUMENTS" ), fr );
109   lay->addWidget( GroupC1 );
110
111   StdMeshers::StdMeshers_NumberOfSegments_var h =
112     StdMeshers::StdMeshers_NumberOfSegments::_narrow( hypothesis() );
113
114   myGroupLayout = new QGridLayout( GroupC1 );
115   myGroupLayout->setSpacing( SPACING );
116   myGroupLayout->setMargin( MARGIN );
117   myGroupLayout->setColumnStretch( 0, 0 );
118   myGroupLayout->setColumnStretch( 1, 1 );
119
120   int row = 0;
121   // 0)  name
122   myName = 0;
123   if( isCreation() )
124   {
125     myName = new QLineEdit( GroupC1 );
126     myGroupLayout->addWidget( new QLabel( tr( "SMESH_NAME" ), GroupC1 ), row, 0 );
127     myGroupLayout->addWidget( myName, row, 1 );
128     row++;
129   }
130
131
132   // 1)  number of segments
133   myGroupLayout->addWidget( new QLabel( tr( "SMESH_NB_SEGMENTS_PARAM" ), GroupC1 ), row, 0 );
134   myNbSeg = new SalomeApp_IntSpinBox( GroupC1 );
135   myNbSeg->setMinimum( 1 );
136   myNbSeg->setMaximum( 9999 );
137   myGroupLayout->addWidget( myNbSeg, row, 1 );
138   row++;
139
140   
141   // 2)  type of distribution
142   myGroupLayout->addWidget( new QLabel( tr( "SMESH_DISTR_TYPE" ), GroupC1 ), row, 0 );
143   myDistr = new QtxComboBox( GroupC1 );
144   QStringList types;
145   types.append( tr( "SMESH_DISTR_REGULAR" ) );
146   types.append( tr( "SMESH_DISTR_SCALE"   ) );
147   types.append( tr( "SMESH_DISTR_TAB"     ) );
148   types.append( tr( "SMESH_DISTR_EXPR"    ) );
149   myDistr->addItems( types );
150   myGroupLayout->addWidget( myDistr, row, 1 );
151   row++;
152
153   
154   // 3)  scale
155   myGroupLayout->addWidget( myLScale = new QLabel( tr( "SMESH_NB_SEGMENTS_SCALE_PARAM" ), GroupC1 ), row, 0 );
156   myScale = new SMESHGUI_SpinBox( GroupC1 );
157   myScale->RangeStepAndValidator( 1E-5, 1E+5, 0.1, "parametric_precision" );
158   myGroupLayout->addWidget( myScale, row, 1 );
159   row++;
160
161   
162   // 4) Distribution definition
163   QGridLayout* myDistLayout = new QGridLayout(GroupC1);
164   myGroupLayout->addLayout( myDistLayout, row, 0, 1, 2 );
165   myGroupLayout->setRowStretch( row, 1 );
166   row ++;
167
168        // a)  expression
169   QHBoxLayout* myExprLayout = new QHBoxLayout(GroupC1);
170   myExprLayout->addWidget( myLExpr = new QLabel( "f(t)=", GroupC1 ), 0);
171   myExpr = new QLineEdit( GroupC1 );
172   myExprLayout->addWidget( myExpr,1);
173   myDistLayout->addLayout(myExprLayout,1 ,0);
174   myDistLayout->setRowStretch(2, 1);
175
176        // b)  warning
177   myInfo = new QLabel( tr( "SMESH_FUNC_DOMAIN" ), GroupC1 );
178   myDistLayout->addWidget( myInfo, 0, 0, 1, 2);
179   
180        // c)  table
181   myTable = new StdMeshersGUI_DistrTableFrame( GroupC1 );
182   myDistLayout->addWidget( myTable, 1, 0, 2, 1 );
183
184        // d) preview
185   myPreview = new StdMeshersGUI_DistrPreview( GroupC1, h.in() );  
186   myPreview->setMinimumHeight(220);
187   myDistLayout->addWidget( myPreview, 1, 1, 2, 1 );
188   
189   // 5)  conversion (radiogroup)
190   myConvBox = new QGroupBox( tr( "SMESH_CONV_MODE" ), GroupC1 );
191   myConv = new QButtonGroup( GroupC1 );
192
193   QHBoxLayout* convLay = new QHBoxLayout( myConvBox );
194   convLay->setMargin( MARGIN );
195   convLay->setSpacing( SPACING );
196
197   QRadioButton* rbExp = new QRadioButton( tr( "SMESH_EXP_MODE" ), myConvBox );
198   QRadioButton* myCutNeg = new QRadioButton( tr( "SMESH_CUT_NEG_MODE" ), myConvBox );
199
200   convLay->addWidget( rbExp );
201   convLay->addWidget( myCutNeg );
202   myConv->addButton( rbExp, 0 );
203   myConv->addButton( myCutNeg, 1 );
204
205   myGroupLayout->addWidget( myConvBox, row, 0, 1, 2 );
206   row++;
207
208
209   // 6) reverse edge parameters
210   myReversedEdgesBox = new QGroupBox(tr( "SMESH_REVERSED_EDGES" ), fr);
211   QHBoxLayout* edgeLay = new QHBoxLayout( myReversedEdgesBox );
212
213   myDirectionWidget = new StdMeshersGUI_SubShapeSelectorWdg( myReversedEdgesBox );
214   QString aGeomEntry = getShapeEntry();
215   QString aMainEntry = getMainShapeEntry();
216   if ( aGeomEntry == "" )
217     aGeomEntry = h->GetObjectEntry();
218   myDirectionWidget->SetGeomShapeEntry( aGeomEntry, aMainEntry );
219   myDirectionWidget->SetListOfIDs( h->GetReversedEdges() );
220   edgeLay->addWidget( myDirectionWidget );
221
222   lay->addWidget( myReversedEdgesBox );
223   lay->setStretchFactor( GroupC1, 1);
224   lay->setStretchFactor( myReversedEdgesBox, 1);
225
226   if ( !aGeomEntry.isEmpty() || !aMainEntry.isEmpty() )
227   {
228     myReversedEdgesHelper = new StdMeshersGUI_PropagationHelperWdg( myDirectionWidget, fr, false );
229     lay->addWidget( myReversedEdgesHelper );
230     lay->setStretchFactor( myReversedEdgesHelper, 1 );
231   }
232
233   connect( myNbSeg, SIGNAL( valueChanged( const QString& ) ), this, SLOT( onValueChanged() ) );
234   connect( myDistr, SIGNAL( activated( int ) ), this, SLOT( onValueChanged() ) );
235   connect( myTable, SIGNAL( valueChanged( int, int ) ), this, SLOT( onValueChanged() ) );
236   connect( myExpr,  SIGNAL( textChanged( const QString& ) ), this, SLOT( onValueChanged() ) );
237   connect( myConv,  SIGNAL( buttonClicked( int ) ), this, SLOT( onValueChanged() ) );
238
239   onValueChanged();
240
241   return fr;
242 }
243
244 void StdMeshersGUI_NbSegmentsCreator::retrieveParams() const
245 {
246   NbSegmentsHypothesisData data;
247   readParamsFromHypo( data );
248
249   if( myName )
250     myName->setText( data.myName );
251   if(data.myNbSegVarName.isEmpty())
252     myNbSeg->setValue( data.myNbSeg );
253   else
254     myNbSeg->setText( data.myNbSegVarName );
255   
256   myDistr->setCurrentIndex( data.myDistrType );
257   if(data.myScaleVarName.isEmpty())
258     myScale->setValue( data.myScale );
259   else
260     myScale->setText( data.myScaleVarName );
261   myConv->button( data.myConv )->setChecked( true );
262   myTable->setFuncMinValue(myConv->checkedId()==0 ? -1E20 : 0);
263   myTable->setData( data.myTable );
264   myExpr->setText( data.myExpr );
265
266   if ( dlg() )
267     dlg()->setMinimumSize( dlg()->minimumSizeHint().width(), dlg()->minimumSizeHint().height() );
268 }
269
270 QString StdMeshersGUI_NbSegmentsCreator::storeParams() const
271 {
272   NbSegmentsHypothesisData data;
273   readParamsFromWidgets( data );
274   storeParamsToHypo( data );
275     
276   QString valStr = QString::number( data.myNbSeg ) += "; ";
277
278   enum DistrType
279   {
280     Regular, //!< equidistant distribution
281     Scale,   //!< scale distribution
282     TabFunc, //!< distribution with density function presented by table
283     ExprFunc //!< distribution with density function presented by expression
284   };
285   bool hasConv = false;
286   switch ( data.myDistrType ) {
287   case Regular :
288     valStr += tr("SMESH_DISTR_REGULAR");
289     break;
290   case Scale   : 
291     valStr += tr("SMESH_NB_SEGMENTS_SCALE_PARAM") + " = " + QString::number( data.myScale );\
292     break;
293   case TabFunc : {
294     //valStr += tr("SMESH_TAB_FUNC");
295     bool param = true;
296     for( int i=0; i < data.myTable.length(); i++, param = !param ) {
297       if ( param )
298         valStr += "[";
299       valStr += QString::number( data.myTable[ i ]);
300       valStr += ( param ? "," : "]" );
301     }
302     hasConv = true;
303     break;
304   }
305   case ExprFunc:
306     valStr += data.myExpr;
307     hasConv = true;
308     break;
309   }
310   if ( hasConv )
311   {
312     if ( data.myConv )
313       valStr += "; " + tr("SMESH_CUT_NEG_MODE");
314     else
315       valStr += "; " + tr("SMESH_EXP_MODE");
316   }
317
318   return valStr;
319 }
320
321 bool StdMeshersGUI_NbSegmentsCreator::readParamsFromHypo( NbSegmentsHypothesisData& h_data ) const
322 {
323   StdMeshers::StdMeshers_NumberOfSegments_var h =
324     StdMeshers::StdMeshers_NumberOfSegments::_narrow( initParamsHypothesis() );
325
326   h_data.myName = hypName();
327
328   h_data.myNbSeg = (int) h->GetNumberOfSegments();
329
330   CORBA::String_var aVaribaleName = h->GetVarParameter( "SetNumberOfSegments" );
331   h_data.myNbSegVarName = aVaribaleName.in();
332
333   int distr = (int) h->GetDistrType();
334   h_data.myDistrType = distr;
335   h_data.myScale = distr==1 ? h->GetScaleFactor() : 1.0;
336   
337   if(distr==1) {
338     aVaribaleName = h->GetVarParameter( "SetScaleFactor" );
339     h_data.myScaleVarName = aVaribaleName.in();
340   }
341   else 
342     h_data.myScaleVarName = QString("");
343
344   if( distr==2 )
345   {
346     SMESH::double_array* a = h->GetTableFunction();
347     h_data.myTable = *a;
348     delete a;
349   }
350   else
351   {
352     SMESH::double_array& a = h_data.myTable;
353     // by default, constant table function f(t)=1
354     a.length( 4 );
355     a[0] = 0.0; a[1] = 1.0;
356     a[2] = 1.0; a[3] = 1.0; 
357   }
358
359   h_data.myExpr = distr==3 ? h->GetExpressionFunction() : "1";
360   h_data.myConv = distr==2 || distr==3 ? h->ConversionMode() : 1; /*cut negative by default*/
361
362   return true;
363 }
364
365 bool StdMeshersGUI_NbSegmentsCreator::storeParamsToHypo( const NbSegmentsHypothesisData& h_data ) const
366 {
367   StdMeshers::StdMeshers_NumberOfSegments_var h =
368     StdMeshers::StdMeshers_NumberOfSegments::_narrow( hypothesis() );
369
370   bool ok = true;
371   try
372   {
373     if( isCreation() )
374       SMESH::SetName( SMESH::FindSObject( h ), h_data.myName.toLatin1().data() );
375
376     h->SetVarParameter( h_data.myNbSegVarName.toLatin1().constData(), "SetNumberOfSegments" );
377     h->SetNumberOfSegments( h_data.myNbSeg );
378     int distr = h_data.myDistrType;
379     h->SetDistrType( distr );
380     
381     if( distr==1 ) {
382       h->SetVarParameter( h_data.myScaleVarName.toLatin1().constData(), "SetScaleFactor" );
383       h->SetScaleFactor( h_data.myScale );
384     }
385     if( distr==2 || distr==3 )
386       h->SetConversionMode( h_data.myConv );
387
388     if( distr==1 || distr==2 || distr==3 ) {
389       h->SetReversedEdges( myDirectionWidget->GetListOfIDs() );
390       h->SetObjectEntry( myDirectionWidget->GetMainShapeEntry() );
391     }
392
393     if( distr==2 )
394       h->SetTableFunction( h_data.myTable );
395
396     if( distr==3 )
397       h->SetExpressionFunction( h_data.myExpr.toLatin1().data() );
398     //setting of function must follow after setConversionMode, because otherwise
399     //the function will be checked with old conversion mode, so that it may occurs
400     //unexpected errors for user
401   }
402   catch(const SALOME::SALOME_Exception& ex)
403   {
404     SalomeApp_Tools::QtCatchCorbaException(ex);
405     ok = false;
406   }
407   return ok;
408 }
409
410 bool StdMeshersGUI_NbSegmentsCreator::readParamsFromWidgets( NbSegmentsHypothesisData& h_data ) const
411 {
412   h_data.myName      = myName ? myName->text() : "";
413   h_data.myNbSeg     = myNbSeg->value();
414   h_data.myNbSegVarName =  myNbSeg->text();
415   h_data.myScaleVarName =  myScale->text();
416   h_data.myDistrType = myDistr->currentIndex();
417   h_data.myConv      = myConv->checkedId();
418   h_data.myScale     = myScale->value();
419   myTable->data( h_data.myTable );
420   h_data.myExpr      = myExpr->text();
421   return true;
422 }
423
424 void StdMeshersGUI_NbSegmentsCreator::onValueChanged()
425 {
426   int distr = myDistr->currentIndex();
427
428 /*  if( distr==2 ) //table func
429     myCutNeg->setText( tr( "SMESH_NO_CONV" ) );
430   else if( distr==3 )
431     myCutNeg->setText( tr( "SMESH_CUT_NEG_MODE" ) );*/
432
433   if( distr==2 && sender()==myConv ) //table func
434   {
435     myTable->setFuncMinValue( myConv->checkedId()==0 ? -1E20 : 0 );
436     SMESH::double_array arr;
437     myTable->data( arr );
438     myTable->setData( arr ); //update data in table
439   }
440
441   myScale->setShown( distr==1 );
442   myLScale->setShown( distr==1 );
443   myReversedEdgesBox->setShown( distr!=0 );
444   if ( myReversedEdgesHelper ) {
445     myReversedEdgesHelper->Clear();
446     myReversedEdgesHelper->setShown( distr!=0 );
447   }
448   myDirectionWidget->ShowPreview( distr!=0 );
449
450   bool isFunc = distr==2 || distr==3;
451   myPreview->setShown( isFunc );
452   myConvBox->setShown( isFunc );
453   
454   myTable->setShown( distr==2 );
455   myExpr->setShown( distr==3 );
456   myLExpr->setShown( distr==3 );
457   myInfo->setShown( distr==3);
458
459   //change of preview
460   int nbSeg = myNbSeg->value();
461   if( distr==2 ) //preview for table-described function
462   {
463     SMESH::double_array a;
464     myTable->data( a );
465     myPreview->setParams( a, nbSeg, false );
466   }
467   else if( distr==3 ) //preview for analytic-described function
468     myPreview->setParams( myExpr->text(), nbSeg, 100, false );
469
470   if( isFunc )
471     myPreview->setConversion( StdMeshersGUI_DistrPreview::Conversion( myConv->checkedId() ) );
472
473   if ( (QtxComboBox*)sender() == myDistr && dlg() ) {
474     QApplication::instance()->processEvents();
475     myGroupLayout->invalidate();
476     dlg()->layout()->invalidate();
477     dlg()->updateGeometry();
478     dlg()->setMinimumSize( dlg()->minimumSizeHint() );
479     dlg()->resize( dlg()->minimumSize() );
480     QApplication::instance()->processEvents();
481   }
482 }