1 // Copyright (C) 2007-2010 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();
102 QVBoxLayout* lay = new QVBoxLayout( fr );
104 lay->setSpacing( SPACING );
106 QGroupBox* GroupC1 = new QGroupBox( tr( "SMESH_ARGUMENTS" ), fr );
107 lay->addWidget( GroupC1 );
109 StdMeshers::StdMeshers_NumberOfSegments_var h =
110 StdMeshers::StdMeshers_NumberOfSegments::_narrow( hypothesis() );
111 myPreview = new StdMeshersGUI_DistrPreview( GroupC1, h.in() );
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 );
130 // 1) number of segments
131 myGroupLayout->addWidget( new QLabel( tr( "SMESH_NB_SEGMENTS_PARAM" ), GroupC1 ), row, 0 );
132 myNbSeg = new SalomeApp_IntSpinBox( GroupC1 );
133 myNbSeg->setMinimum( 1 );
134 myNbSeg->setMaximum( 9999 );
135 myGroupLayout->addWidget( myNbSeg, row, 1 );
138 // 2) type of distribution
139 myGroupLayout->addWidget( new QLabel( tr( "SMESH_DISTR_TYPE" ), GroupC1 ), row, 0 );
140 myDistr = new QtxComboBox( GroupC1 );
142 types.append( tr( "SMESH_DISTR_REGULAR" ) );
143 types.append( tr( "SMESH_DISTR_SCALE" ) );
144 types.append( tr( "SMESH_DISTR_TAB" ) );
145 types.append( tr( "SMESH_DISTR_EXPR" ) );
146 myDistr->addItems( types );
147 myGroupLayout->addWidget( myDistr, row, 1 );
151 myGroupLayout->addWidget( myLScale = new QLabel( tr( "SMESH_NB_SEGMENTS_SCALE_PARAM" ), GroupC1 ), row, 0 );
152 myScale = new SMESHGUI_SpinBox( GroupC1 );
153 myScale->RangeStepAndValidator( 1E-5, 1E+5, 0.1, "parametric_precision" );
154 myGroupLayout->addWidget( myScale, row, 1 );
157 myInfo = new QLabel( tr( "SMESH_FUNC_DOMAIN" ), GroupC1 );
158 myGroupLayout->addWidget( myInfo, row, 0, 1, 2 );
162 myGroupLayout->addWidget( myLTable = new QLabel( tr( "SMESH_TAB_FUNC" ), GroupC1 ), row, 0 );
163 myTable = new StdMeshersGUI_DistrTableFrame( GroupC1 );
164 myGroupLayout->addWidget( myTable, row, 1 );
165 myGroupLayout->setRowStretch( row, 1 );
170 myGroupLayout->addWidget( myLExpr = new QLabel( tr( "SMESH_EXPR_FUNC" ), GroupC1 ), row, 0 );
171 myExpr = new QLineEdit( GroupC1 );
172 myGroupLayout->addWidget( myExpr, row, 1 );
175 // 6) conversion (radiogroup)
176 myConvBox = new QGroupBox( tr( "SMESH_CONV_MODE" ), GroupC1 );
177 myConv = new QButtonGroup( GroupC1 );
179 QHBoxLayout* convLay = new QHBoxLayout( myConvBox );
180 convLay->setMargin( MARGIN );
181 convLay->setSpacing( SPACING );
183 QRadioButton* rbExp = new QRadioButton( tr( "SMESH_EXP_MODE" ), myConvBox );
184 QRadioButton* myCutNeg = new QRadioButton( tr( "SMESH_CUT_NEG_MODE" ), myConvBox );
186 convLay->addWidget( rbExp );
187 convLay->addWidget( myCutNeg );
188 myConv->addButton( rbExp, 0 );
189 myConv->addButton( myCutNeg, 1 );
191 myGroupLayout->addWidget( myConvBox, row, 0, 1, 2 );
194 // 7) distribution preview
195 myGroupLayout->addWidget( myPreview, row, 0, 1, 2 );
196 myGroupLayout->setRowStretch( row, 1 );
200 // 8) reverce edge parameters
201 myReversedEdgesBox = new QGroupBox(tr( "SMESH_REVERSED_EDGES" ), fr);
202 QHBoxLayout* edgeLay = new QHBoxLayout( myReversedEdgesBox );
204 myDirectionWidget = new StdMeshersGUI_SubShapeSelectorWdg();
205 QString aGeomEntry = getShapeEntry();
206 QString aMainEntry = getMainShapeEntry();
207 if ( aGeomEntry == "" )
208 aGeomEntry = h->GetObjectEntry();
209 myDirectionWidget->SetGeomShapeEntry( aGeomEntry );
210 myDirectionWidget->SetMainShapeEntry( aMainEntry );
211 myDirectionWidget->SetListOfIDs( h->GetReversedEdges() );
212 edgeLay->addWidget( myDirectionWidget );
214 lay->addWidget( myReversedEdgesBox );
216 connect( myNbSeg, SIGNAL( valueChanged( const QString& ) ), this, SLOT( onValueChanged() ) );
217 connect( myDistr, SIGNAL( activated( int ) ), this, SLOT( onValueChanged() ) );
218 connect( myTable, SIGNAL( valueChanged( int, int ) ), this, SLOT( onValueChanged() ) );
219 connect( myExpr, SIGNAL( textChanged( const QString& ) ), this, SLOT( onValueChanged() ) );
220 connect( myConv, SIGNAL( buttonClicked( int ) ), this, SLOT( onValueChanged() ) );
225 void StdMeshersGUI_NbSegmentsCreator::retrieveParams() const
227 NbSegmentsHypothesisData data;
228 readParamsFromHypo( data );
231 myName->setText( data.myName );
232 if(data.myNbSegVarName.isEmpty())
233 myNbSeg->setValue( data.myNbSeg );
235 myNbSeg->setText( data.myNbSegVarName );
237 myDistr->setCurrentIndex( data.myDistrType );
238 if(data.myScaleVarName.isEmpty())
239 myScale->setValue( data.myScale );
241 myScale->setText( data.myScaleVarName );
242 myConv->button( data.myConv )->setChecked( true );
243 myTable->setFuncMinValue(myConv->checkedId()==0 ? -1E20 : 0);
244 myTable->setData( data.myTable );
245 myExpr->setText( data.myExpr );
248 dlg()->setMinimumSize( dlg()->minimumSizeHint().width(), dlg()->minimumSizeHint().height() );
251 QString StdMeshersGUI_NbSegmentsCreator::storeParams() const
253 NbSegmentsHypothesisData data;
254 readParamsFromWidgets( data );
255 storeParamsToHypo( data );
257 QString valStr = QString::number( data.myNbSeg ) += "; ";
261 Regular, //!< equidistant distribution
262 Scale, //!< scale distribution
263 TabFunc, //!< distribution with density function presented by table
264 ExprFunc //!< distribution with density function presented by expression
266 bool hasConv = false;
267 switch ( data.myDistrType ) {
269 valStr += tr("SMESH_DISTR_REGULAR");
272 valStr += tr("SMESH_NB_SEGMENTS_SCALE_PARAM") + " = " + QString::number( data.myScale );\
275 //valStr += tr("SMESH_TAB_FUNC");
277 for( int i=0; i < data.myTable.length(); i++, param = !param ) {
280 valStr += QString::number( data.myTable[ i ]);
281 valStr += ( param ? "," : "]" );
287 valStr += data.myExpr;
293 valStr += "; " + tr("SMESH_CUT_NEG_MODE");
295 valStr += "; " + tr("SMESH_EXP_MODE");
300 bool StdMeshersGUI_NbSegmentsCreator::readParamsFromHypo( NbSegmentsHypothesisData& h_data ) const
302 StdMeshers::StdMeshers_NumberOfSegments_var h =
303 StdMeshers::StdMeshers_NumberOfSegments::_narrow( initParamsHypothesis() );
305 h_data.myName = hypName();
307 h_data.myNbSeg = (int) h->GetNumberOfSegments();
309 SMESH::ListOfParameters_var aParameters = h->GetLastParameters();
311 h_data.myNbSegVarName = (aParameters->length() > 0) ? QString(aParameters[0].in()) : QString("");
313 int distr = (int) h->GetDistrType();
314 h_data.myDistrType = distr;
315 h_data.myScale = distr==1 ? h->GetScaleFactor() : 1.0;
318 h_data.myScaleVarName = (aParameters->length() > 1) ? QString(aParameters[1].in()) : QString("");
321 h_data.myScaleVarName = QString("");
325 SMESH::double_array* a = h->GetTableFunction();
331 SMESH::double_array& a = h_data.myTable;
332 // by default, constant table function f(t)=1
334 a[0] = 0.0; a[1] = 1.0;
335 a[2] = 1.0; a[3] = 1.0;
338 h_data.myExpr = distr==3 ? h->GetExpressionFunction() : "1";
339 h_data.myConv = distr==2 || distr==3 ? h->ConversionMode() : 1; /*cut negative by default*/
344 bool StdMeshersGUI_NbSegmentsCreator::storeParamsToHypo( const NbSegmentsHypothesisData& h_data ) const
346 StdMeshers::StdMeshers_NumberOfSegments_var h =
347 StdMeshers::StdMeshers_NumberOfSegments::_narrow( hypothesis() );
353 SMESH::SetName( SMESH::FindSObject( h ), h_data.myName.toLatin1().data() );
355 QStringList aVariablesList;
356 aVariablesList.append(h_data.myNbSegVarName);
358 h->SetNumberOfSegments( h_data.myNbSeg );
359 int distr = h_data.myDistrType;
360 h->SetDistrType( distr );
363 h->SetScaleFactor( h_data.myScale );
364 aVariablesList.append(h_data.myScaleVarName);
366 if( distr==2 || distr==3 )
367 h->SetConversionMode( h_data.myConv );
369 if( distr==1 || distr==2 || distr==3 ) {
370 h->SetReversedEdges( myDirectionWidget->GetListOfIDs() );
371 h->SetObjectEntry( myDirectionWidget->GetMainShapeEntry() );
375 h->SetTableFunction( h_data.myTable );
378 h->SetExpressionFunction( h_data.myExpr.toLatin1().data() );
379 //setting of function must follow after setConversionMode, because otherwise
380 //the function will be checked with old conversion mode, so that it may occurs
381 //unexpected errors for user
383 h->SetParameters(aVariablesList.join(":").toLatin1().constData());
385 catch(const SALOME::SALOME_Exception& ex)
387 SalomeApp_Tools::QtCatchCorbaException(ex);
393 bool StdMeshersGUI_NbSegmentsCreator::readParamsFromWidgets( NbSegmentsHypothesisData& h_data ) const
395 h_data.myName = myName ? myName->text() : "";
396 h_data.myNbSeg = myNbSeg->value();
397 h_data.myNbSegVarName = myNbSeg->text();
398 h_data.myScaleVarName = myScale->text();
399 h_data.myDistrType = myDistr->currentIndex();
400 h_data.myConv = myConv->checkedId();
401 h_data.myScale = myScale->value();
402 myTable->data( h_data.myTable );
403 h_data.myExpr = myExpr->text();
407 void StdMeshersGUI_NbSegmentsCreator::onValueChanged()
409 int distr = myDistr->currentIndex();
411 /* if( distr==2 ) //table func
412 myCutNeg->setText( tr( "SMESH_NO_CONV" ) );
414 myCutNeg->setText( tr( "SMESH_CUT_NEG_MODE" ) );*/
416 if( distr==2 && sender()==myConv ) //table func
418 myTable->setFuncMinValue( myConv->checkedId()==0 ? -1E20 : 0 );
419 SMESH::double_array arr;
420 myTable->data( arr );
421 myTable->setData( arr ); //update data in table
424 myScale->setShown( distr==1 );
425 myLScale->setShown( distr==1 );
426 myReversedEdgesBox->setShown( !distr==0 );
427 myDirectionWidget->showPreview( !distr==0 );
429 bool isFunc = distr==2 || distr==3;
430 myPreview->setShown( isFunc );
431 myGroupLayout->setRowStretch( myPreviewRow, isFunc ? 1 : 0 );
433 myConvBox->setShown( isFunc );
439 myLTable->setShown( distr==2 );
440 myGroupLayout->setRowStretch( myTableRow, distr==2 ? 1 : 0 );
442 myExpr->setShown( distr==3 );
443 myLExpr->setShown( distr==3 );
444 myInfo->setShown( isFunc );
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();