1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : StdMeshersGUI_NbSegmentsCreator.cxx
24 // Author : Open CASCADE S.A.S.
27 #include "StdMeshersGUI_NbSegmentsCreator.h"
28 #include "StdMeshersGUI_DistrTable.h"
29 #include "StdMeshersGUI_DistrPreview.h"
30 #include "StdMeshersGUI_SubShapeSelectorWdg.h"
33 #include <SMESHGUI_Utils.h>
34 #include <SMESHGUI_HypothesesUtils.h>
35 #include <SMESHGUI_SpinBox.h>
38 #include CORBA_SERVER_HEADER(SMESH_BasicHypothesis)
40 // SALOME GUI includes
41 #include <SalomeApp_Tools.h>
42 #include <SalomeApp_IntSpinBox.h>
43 #include <QtxComboBox.h>
50 #include <QButtonGroup>
51 #include <QRadioButton>
52 #include <QGridLayout>
53 #include <QVBoxLayout>
54 #include <QHBoxLayout>
55 #include <QApplication>
60 StdMeshersGUI_NbSegmentsCreator::StdMeshersGUI_NbSegmentsCreator()
61 : StdMeshersGUI_StdHypothesisCreator( "NumberOfSegments" ),
80 StdMeshersGUI_NbSegmentsCreator::~StdMeshersGUI_NbSegmentsCreator()
84 bool StdMeshersGUI_NbSegmentsCreator::checkParams( QString& msg ) const
86 if( !SMESHGUI_GenericHypothesisCreator::checkParams( msg ) )
88 NbSegmentsHypothesisData data_old, data_new;
89 readParamsFromHypo( data_old );
90 readParamsFromWidgets( data_new );
91 bool res = storeParamsToHypo( data_new );
92 storeParamsToHypo( data_old );
93 res = myNbSeg->isValid( msg, true ) && res;
94 res = myScale->isValid( msg, true ) && res;
98 QFrame* StdMeshersGUI_NbSegmentsCreator::buildFrame()
100 QFrame* fr = new QFrame();
101 fr->setMinimumWidth(460);
103 QVBoxLayout* lay = new QVBoxLayout( fr );
105 lay->setSpacing( SPACING );
107 QGroupBox* GroupC1 = new QGroupBox( tr( "SMESH_ARGUMENTS" ), fr );
108 lay->addWidget( GroupC1 );
110 StdMeshers::StdMeshers_NumberOfSegments_var h =
111 StdMeshers::StdMeshers_NumberOfSegments::_narrow( hypothesis() );
113 myGroupLayout = new QGridLayout( GroupC1 );
114 myGroupLayout->setSpacing( SPACING );
115 myGroupLayout->setMargin( MARGIN );
116 myGroupLayout->setColumnStretch( 0, 0 );
117 myGroupLayout->setColumnStretch( 1, 1 );
124 myName = new QLineEdit( GroupC1 );
125 myGroupLayout->addWidget( new QLabel( tr( "SMESH_NAME" ), GroupC1 ), row, 0 );
126 myGroupLayout->addWidget( myName, row, 1 );
131 // 1) number of segments
132 myGroupLayout->addWidget( new QLabel( tr( "SMESH_NB_SEGMENTS_PARAM" ), GroupC1 ), row, 0 );
133 myNbSeg = new SalomeApp_IntSpinBox( GroupC1 );
134 myNbSeg->setMinimum( 1 );
135 myNbSeg->setMaximum( 9999 );
136 myGroupLayout->addWidget( myNbSeg, row, 1 );
140 // 2) type of distribution
141 myGroupLayout->addWidget( new QLabel( tr( "SMESH_DISTR_TYPE" ), GroupC1 ), row, 0 );
142 myDistr = new QtxComboBox( GroupC1 );
144 types.append( tr( "SMESH_DISTR_REGULAR" ) );
145 types.append( tr( "SMESH_DISTR_SCALE" ) );
146 types.append( tr( "SMESH_DISTR_TAB" ) );
147 types.append( tr( "SMESH_DISTR_EXPR" ) );
148 myDistr->addItems( types );
149 myGroupLayout->addWidget( myDistr, row, 1 );
154 myGroupLayout->addWidget( myLScale = new QLabel( tr( "SMESH_NB_SEGMENTS_SCALE_PARAM" ), GroupC1 ), row, 0 );
155 myScale = new SMESHGUI_SpinBox( GroupC1 );
156 myScale->RangeStepAndValidator( 1E-5, 1E+5, 0.1, "parametric_precision" );
157 myGroupLayout->addWidget( myScale, row, 1 );
161 // 4) Distribution definition
162 QGridLayout* myDistLayout = new QGridLayout(GroupC1);
163 myGroupLayout->addLayout( myDistLayout, row, 0, 1, 2 );
164 myGroupLayout->setRowStretch( row, 1 );
168 QHBoxLayout* myExprLayout = new QHBoxLayout(GroupC1);
169 myExprLayout->addWidget( myLExpr = new QLabel( "f(t)=", GroupC1 ), 0);
170 myExpr = new QLineEdit( GroupC1 );
171 myExprLayout->addWidget( myExpr,1);
172 myDistLayout->addLayout(myExprLayout,1 ,0);
173 myDistLayout->setRowStretch(2, 1);
176 myInfo = new QLabel( tr( "SMESH_FUNC_DOMAIN" ), GroupC1 );
177 myDistLayout->addWidget( myInfo, 0, 0, 1, 2);
180 myTable = new StdMeshersGUI_DistrTableFrame( GroupC1 );
181 myDistLayout->addWidget( myTable, 1, 0, 2, 1 );
184 myPreview = new StdMeshersGUI_DistrPreview( GroupC1, h.in() );
185 myPreview->setMinimumHeight(220);
186 myDistLayout->addWidget( myPreview, 1, 1, 2, 1 );
188 // 5) conversion (radiogroup)
189 myConvBox = new QGroupBox( tr( "SMESH_CONV_MODE" ), GroupC1 );
190 myConv = new QButtonGroup( GroupC1 );
192 QHBoxLayout* convLay = new QHBoxLayout( myConvBox );
193 convLay->setMargin( MARGIN );
194 convLay->setSpacing( SPACING );
196 QRadioButton* rbExp = new QRadioButton( tr( "SMESH_EXP_MODE" ), myConvBox );
197 QRadioButton* myCutNeg = new QRadioButton( tr( "SMESH_CUT_NEG_MODE" ), myConvBox );
199 convLay->addWidget( rbExp );
200 convLay->addWidget( myCutNeg );
201 myConv->addButton( rbExp, 0 );
202 myConv->addButton( myCutNeg, 1 );
204 myGroupLayout->addWidget( myConvBox, row, 0, 1, 2 );
208 // 6) reverse edge parameters
209 myReversedEdgesBox = new QGroupBox(tr( "SMESH_REVERSED_EDGES" ), fr);
210 QHBoxLayout* edgeLay = new QHBoxLayout( myReversedEdgesBox );
212 myDirectionWidget = new StdMeshersGUI_SubShapeSelectorWdg( myReversedEdgesBox );
213 QString aGeomEntry = getShapeEntry();
214 QString aMainEntry = getMainShapeEntry();
215 if ( aGeomEntry == "" )
216 aGeomEntry = h->GetObjectEntry();
217 myDirectionWidget->SetGeomShapeEntry( aGeomEntry );
218 myDirectionWidget->SetMainShapeEntry( aMainEntry );
219 myDirectionWidget->SetListOfIDs( h->GetReversedEdges() );
220 edgeLay->addWidget( myDirectionWidget );
222 lay->addWidget( myReversedEdgesBox );
223 lay->setStretchFactor( GroupC1, 2);
224 lay->setStretchFactor( myReversedEdgesBox, 1);
226 connect( myNbSeg, SIGNAL( valueChanged( const QString& ) ), this, SLOT( onValueChanged() ) );
227 connect( myDistr, SIGNAL( activated( int ) ), this, SLOT( onValueChanged() ) );
228 connect( myTable, SIGNAL( valueChanged( int, int ) ), this, SLOT( onValueChanged() ) );
229 connect( myExpr, SIGNAL( textChanged( const QString& ) ), this, SLOT( onValueChanged() ) );
230 connect( myConv, SIGNAL( buttonClicked( int ) ), this, SLOT( onValueChanged() ) );
235 void StdMeshersGUI_NbSegmentsCreator::retrieveParams() const
237 NbSegmentsHypothesisData data;
238 readParamsFromHypo( data );
241 myName->setText( data.myName );
242 if(data.myNbSegVarName.isEmpty())
243 myNbSeg->setValue( data.myNbSeg );
245 myNbSeg->setText( data.myNbSegVarName );
247 myDistr->setCurrentIndex( data.myDistrType );
248 if(data.myScaleVarName.isEmpty())
249 myScale->setValue( data.myScale );
251 myScale->setText( data.myScaleVarName );
252 myConv->button( data.myConv )->setChecked( true );
253 myTable->setFuncMinValue(myConv->checkedId()==0 ? -1E20 : 0);
254 myTable->setData( data.myTable );
255 myExpr->setText( data.myExpr );
258 dlg()->setMinimumSize( dlg()->minimumSizeHint().width(), dlg()->minimumSizeHint().height() );
261 QString StdMeshersGUI_NbSegmentsCreator::storeParams() const
263 NbSegmentsHypothesisData data;
264 readParamsFromWidgets( data );
265 storeParamsToHypo( data );
267 QString valStr = QString::number( data.myNbSeg ) += "; ";
271 Regular, //!< equidistant distribution
272 Scale, //!< scale distribution
273 TabFunc, //!< distribution with density function presented by table
274 ExprFunc //!< distribution with density function presented by expression
276 bool hasConv = false;
277 switch ( data.myDistrType ) {
279 valStr += tr("SMESH_DISTR_REGULAR");
282 valStr += tr("SMESH_NB_SEGMENTS_SCALE_PARAM") + " = " + QString::number( data.myScale );\
285 //valStr += tr("SMESH_TAB_FUNC");
287 for( int i=0; i < data.myTable.length(); i++, param = !param ) {
290 valStr += QString::number( data.myTable[ i ]);
291 valStr += ( param ? "," : "]" );
297 valStr += data.myExpr;
304 valStr += "; " + tr("SMESH_CUT_NEG_MODE");
306 valStr += "; " + tr("SMESH_EXP_MODE");
312 bool StdMeshersGUI_NbSegmentsCreator::readParamsFromHypo( NbSegmentsHypothesisData& h_data ) const
314 StdMeshers::StdMeshers_NumberOfSegments_var h =
315 StdMeshers::StdMeshers_NumberOfSegments::_narrow( initParamsHypothesis() );
317 h_data.myName = hypName();
319 h_data.myNbSeg = (int) h->GetNumberOfSegments();
321 CORBA::String_var aVaribaleName = h->GetVarParameter( "SetNumberOfSegments" );
322 h_data.myNbSegVarName = aVaribaleName.in();
324 int distr = (int) h->GetDistrType();
325 h_data.myDistrType = distr;
326 h_data.myScale = distr==1 ? h->GetScaleFactor() : 1.0;
329 aVaribaleName = h->GetVarParameter( "SetScaleFactor" );
330 h_data.myScaleVarName = aVaribaleName.in();
333 h_data.myScaleVarName = QString("");
337 SMESH::double_array* a = h->GetTableFunction();
343 SMESH::double_array& a = h_data.myTable;
344 // by default, constant table function f(t)=1
346 a[0] = 0.0; a[1] = 1.0;
347 a[2] = 1.0; a[3] = 1.0;
350 h_data.myExpr = distr==3 ? h->GetExpressionFunction() : "1";
351 h_data.myConv = distr==2 || distr==3 ? h->ConversionMode() : 1; /*cut negative by default*/
356 bool StdMeshersGUI_NbSegmentsCreator::storeParamsToHypo( const NbSegmentsHypothesisData& h_data ) const
358 StdMeshers::StdMeshers_NumberOfSegments_var h =
359 StdMeshers::StdMeshers_NumberOfSegments::_narrow( hypothesis() );
365 SMESH::SetName( SMESH::FindSObject( h ), h_data.myName.toLatin1().data() );
367 h->SetVarParameter( h_data.myNbSegVarName.toLatin1().constData(), "SetNumberOfSegments" );
368 h->SetNumberOfSegments( h_data.myNbSeg );
369 int distr = h_data.myDistrType;
370 h->SetDistrType( distr );
373 h->SetVarParameter( h_data.myScaleVarName.toLatin1().constData(), "SetScaleFactor" );
374 h->SetScaleFactor( h_data.myScale );
376 if( distr==2 || distr==3 )
377 h->SetConversionMode( h_data.myConv );
379 if( distr==1 || distr==2 || distr==3 ) {
380 h->SetReversedEdges( myDirectionWidget->GetListOfIDs() );
381 h->SetObjectEntry( myDirectionWidget->GetMainShapeEntry() );
385 h->SetTableFunction( h_data.myTable );
388 h->SetExpressionFunction( h_data.myExpr.toLatin1().data() );
389 //setting of function must follow after setConversionMode, because otherwise
390 //the function will be checked with old conversion mode, so that it may occurs
391 //unexpected errors for user
393 catch(const SALOME::SALOME_Exception& ex)
395 SalomeApp_Tools::QtCatchCorbaException(ex);
401 bool StdMeshersGUI_NbSegmentsCreator::readParamsFromWidgets( NbSegmentsHypothesisData& h_data ) const
403 h_data.myName = myName ? myName->text() : "";
404 h_data.myNbSeg = myNbSeg->value();
405 h_data.myNbSegVarName = myNbSeg->text();
406 h_data.myScaleVarName = myScale->text();
407 h_data.myDistrType = myDistr->currentIndex();
408 h_data.myConv = myConv->checkedId();
409 h_data.myScale = myScale->value();
410 myTable->data( h_data.myTable );
411 h_data.myExpr = myExpr->text();
415 void StdMeshersGUI_NbSegmentsCreator::onValueChanged()
417 int distr = myDistr->currentIndex();
419 /* if( distr==2 ) //table func
420 myCutNeg->setText( tr( "SMESH_NO_CONV" ) );
422 myCutNeg->setText( tr( "SMESH_CUT_NEG_MODE" ) );*/
424 if( distr==2 && sender()==myConv ) //table func
426 myTable->setFuncMinValue( myConv->checkedId()==0 ? -1E20 : 0 );
427 SMESH::double_array arr;
428 myTable->data( arr );
429 myTable->setData( arr ); //update data in table
432 myScale->setShown( distr==1 );
433 myLScale->setShown( distr==1 );
434 myReversedEdgesBox->setShown( !distr==0 );
435 myDirectionWidget->showPreview( !distr==0 );
437 bool isFunc = distr==2 || distr==3;
438 myPreview->setShown( isFunc );
439 myConvBox->setShown( isFunc );
441 myTable->setShown( distr==2 );
442 myExpr->setShown( distr==3 );
443 myLExpr->setShown( distr==3 );
444 myInfo->setShown( distr==3);
447 int nbSeg = myNbSeg->value();
448 if( distr==2 ) //preview for table-described function
450 SMESH::double_array a;
452 myPreview->setParams( a, nbSeg, false );
454 else if( distr==3 ) //preview for analytic-described function
455 myPreview->setParams( myExpr->text(), nbSeg, 100, false );
458 myPreview->setConversion( StdMeshersGUI_DistrPreview::Conversion( myConv->checkedId() ) );
460 if ( (QtxComboBox*)sender() == myDistr && dlg() ) {
461 QApplication::instance()->processEvents();
462 myGroupLayout->invalidate();
463 dlg()->layout()->invalidate();
464 dlg()->updateGeometry();
465 dlg()->setMinimumSize( dlg()->minimumSizeHint() );
466 dlg()->resize( dlg()->minimumSize() );
467 QApplication::instance()->processEvents();