1 // Copyright (C) 2007-2015 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, or (at your option) any later version.
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 #ifndef DISABLE_PLOT2DVIEWER
29 #include "StdMeshersGUI_DistrPreview.h"
31 #include "StdMeshersGUI_DistrTable.h"
32 #include "StdMeshersGUI_PropagationHelperWdg.h"
33 #include "StdMeshersGUI_SubShapeSelectorWdg.h"
36 #include <SMESHGUI_Utils.h>
37 #include <SMESHGUI_HypothesesUtils.h>
38 #include <SMESHGUI_SpinBox.h>
41 #include CORBA_SERVER_HEADER(SMESH_BasicHypothesis)
43 // SALOME GUI includes
44 #include <SalomeApp_Tools.h>
45 #include <SalomeApp_IntSpinBox.h>
46 #include <QtxComboBox.h>
53 #include <QButtonGroup>
54 #include <QRadioButton>
55 #include <QGridLayout>
56 #include <QVBoxLayout>
57 #include <QHBoxLayout>
58 #include <QApplication>
63 StdMeshersGUI_NbSegmentsCreator::StdMeshersGUI_NbSegmentsCreator()
64 : StdMeshersGUI_StdHypothesisCreator( "NumberOfSegments" ),
69 #ifndef DISABLE_PLOT2DVIEWER
85 StdMeshersGUI_NbSegmentsCreator::~StdMeshersGUI_NbSegmentsCreator()
89 bool StdMeshersGUI_NbSegmentsCreator::checkParams( QString& msg ) const
91 if( !SMESHGUI_GenericHypothesisCreator::checkParams( msg ) )
93 NbSegmentsHypothesisData data_old, data_new;
94 readParamsFromHypo( data_old );
95 readParamsFromWidgets( data_new );
96 bool res = storeParamsToHypo( data_new );
97 res = myNbSeg->isValid( msg, true ) && res;
98 res = myScale->isValid( msg, true ) && res;
100 storeParamsToHypo( data_old );
104 QFrame* StdMeshersGUI_NbSegmentsCreator::buildFrame()
106 QFrame* fr = new QFrame();
107 fr->setMinimumWidth(460);
109 QVBoxLayout* lay = new QVBoxLayout( fr );
111 lay->setSpacing( SPACING );
113 QGroupBox* GroupC1 = new QGroupBox( tr( "SMESH_ARGUMENTS" ), fr );
114 lay->addWidget( GroupC1 );
116 StdMeshers::StdMeshers_NumberOfSegments_var h =
117 StdMeshers::StdMeshers_NumberOfSegments::_narrow( hypothesis() );
119 myGroupLayout = new QGridLayout( GroupC1 );
120 myGroupLayout->setSpacing( SPACING );
121 myGroupLayout->setMargin( MARGIN );
122 myGroupLayout->setColumnStretch( 0, 0 );
123 myGroupLayout->setColumnStretch( 1, 1 );
130 myName = new QLineEdit( GroupC1 );
131 myGroupLayout->addWidget( new QLabel( tr( "SMESH_NAME" ), GroupC1 ), row, 0 );
132 myGroupLayout->addWidget( myName, row, 1 );
137 // 1) number of segments
138 myGroupLayout->addWidget( new QLabel( tr( "SMESH_NB_SEGMENTS_PARAM" ), GroupC1 ), row, 0 );
139 myNbSeg = new SalomeApp_IntSpinBox( GroupC1 );
140 myNbSeg->setMinimum( 1 );
141 myNbSeg->setMaximum( 9999 );
142 myGroupLayout->addWidget( myNbSeg, row, 1 );
146 // 2) type of distribution
147 myGroupLayout->addWidget( new QLabel( tr( "SMESH_DISTR_TYPE" ), GroupC1 ), row, 0 );
148 myDistr = new QtxComboBox( GroupC1 );
150 types.append( tr( "SMESH_DISTR_REGULAR" ) );
151 types.append( tr( "SMESH_DISTR_SCALE" ) );
152 types.append( tr( "SMESH_DISTR_TAB" ) );
153 types.append( tr( "SMESH_DISTR_EXPR" ) );
154 myDistr->addItems( types );
155 myGroupLayout->addWidget( myDistr, row, 1 );
160 myGroupLayout->addWidget( myLScale = new QLabel( tr( "SMESH_NB_SEGMENTS_SCALE_PARAM" ), GroupC1 ), row, 0 );
161 myScale = new SMESHGUI_SpinBox( GroupC1 );
162 myScale->RangeStepAndValidator( 1E-5, 1E+5, 0.1, "parametric_precision" );
163 myGroupLayout->addWidget( myScale, row, 1 );
167 // 4) Distribution definition
168 QGridLayout* myDistLayout = new QGridLayout(GroupC1);
169 myGroupLayout->addLayout( myDistLayout, row, 0, 1, 2 );
170 myGroupLayout->setRowStretch( row, 1 );
174 QHBoxLayout* myExprLayout = new QHBoxLayout(GroupC1);
175 myExprLayout->addWidget( myLExpr = new QLabel( "f(t)=", GroupC1 ), 0);
176 myExpr = new QLineEdit( GroupC1 );
177 myExprLayout->addWidget( myExpr,1);
178 myDistLayout->addLayout(myExprLayout,1 ,0);
179 myDistLayout->setRowStretch(2, 1);
182 myInfo = new QLabel( tr( "SMESH_FUNC_DOMAIN" ), GroupC1 );
183 myDistLayout->addWidget( myInfo, 0, 0, 1, 2);
186 myTable = new StdMeshersGUI_DistrTableFrame( GroupC1 );
187 myTable->setMinimumHeight(220);
188 myDistLayout->addWidget( myTable, 1, 0, 2, 1 );
190 #ifndef DISABLE_PLOT2DVIEWER
192 myPreview = new StdMeshersGUI_DistrPreview( GroupC1, h.in() );
193 myDistLayout->addWidget( myPreview, 1, 1, 2, 1 );
196 // 5) conversion (radiogroup)
197 myConvBox = new QGroupBox( tr( "SMESH_CONV_MODE" ), GroupC1 );
198 myConv = new QButtonGroup( GroupC1 );
200 QHBoxLayout* convLay = new QHBoxLayout( myConvBox );
201 convLay->setMargin( MARGIN );
202 convLay->setSpacing( SPACING );
204 QRadioButton* rbExp = new QRadioButton( tr( "SMESH_EXP_MODE" ), myConvBox );
205 QRadioButton* myCutNeg = new QRadioButton( tr( "SMESH_CUT_NEG_MODE" ), myConvBox );
207 convLay->addWidget( rbExp );
208 convLay->addWidget( myCutNeg );
209 myConv->addButton( rbExp, 0 );
210 myConv->addButton( myCutNeg, 1 );
212 myGroupLayout->addWidget( myConvBox, row, 0, 1, 2 );
216 // 6) reverse edge parameters
217 myReversedEdgesBox = new QGroupBox(tr( "SMESH_REVERSED_EDGES" ), fr);
218 QHBoxLayout* edgeLay = new QHBoxLayout( myReversedEdgesBox );
220 myDirectionWidget = new StdMeshersGUI_SubShapeSelectorWdg( myReversedEdgesBox );
221 QString aGeomEntry = getShapeEntry();
222 QString aMainEntry = getMainShapeEntry();
223 if ( aGeomEntry == "" )
224 aGeomEntry = h->GetObjectEntry();
225 myDirectionWidget->SetGeomShapeEntry( aGeomEntry, aMainEntry );
226 myDirectionWidget->SetListOfIDs( h->GetReversedEdges() );
227 edgeLay->addWidget( myDirectionWidget );
229 lay->addWidget( myReversedEdgesBox );
230 lay->setStretchFactor( GroupC1, 1);
231 lay->setStretchFactor( myReversedEdgesBox, 1);
233 myReversedEdgesHelper = 0;
234 if ( !aGeomEntry.isEmpty() || !aMainEntry.isEmpty() )
236 myReversedEdgesHelper = new StdMeshersGUI_PropagationHelperWdg( myDirectionWidget, fr, false );
237 lay->addWidget( myReversedEdgesHelper );
238 lay->setStretchFactor( myReversedEdgesHelper, 1 );
241 connect( myNbSeg, SIGNAL( valueChanged( const QString& ) ), this, SLOT( onValueChanged() ) );
242 connect( myDistr, SIGNAL( activated( int ) ), this, SLOT( onValueChanged() ) );
243 connect( myTable, SIGNAL( valueChanged( int, int ) ), this, SLOT( onValueChanged() ) );
244 connect( myExpr, SIGNAL( textChanged( const QString& ) ), this, SLOT( onValueChanged() ) );
245 connect( myConv, SIGNAL( buttonClicked( int ) ), this, SLOT( onValueChanged() ) );
252 void StdMeshersGUI_NbSegmentsCreator::retrieveParams() const
254 NbSegmentsHypothesisData data;
255 readParamsFromHypo( data );
258 myName->setText( data.myName );
259 if(data.myNbSegVarName.isEmpty())
260 myNbSeg->setValue( data.myNbSeg );
262 myNbSeg->setText( data.myNbSegVarName );
264 myDistr->setCurrentIndex( data.myDistrType );
265 if(data.myScaleVarName.isEmpty())
266 myScale->setValue( data.myScale );
268 myScale->setText( data.myScaleVarName );
269 myConv->button( data.myConv )->setChecked( true );
270 myTable->setFuncMinValue(myConv->checkedId()==0 ? -1E20 : 0);
271 myTable->setData( data.myTable );
272 myExpr->setText( data.myExpr );
275 dlg()->setMinimumSize( dlg()->minimumSizeHint().width(), dlg()->minimumSizeHint().height() );
278 QString StdMeshersGUI_NbSegmentsCreator::storeParams() const
280 NbSegmentsHypothesisData data;
281 readParamsFromWidgets( data );
282 storeParamsToHypo( data );
284 QString valStr = QString::number( data.myNbSeg ) += "; ";
288 Regular, //!< equidistant distribution
289 Scale, //!< scale distribution
290 TabFunc, //!< distribution with density function presented by table
291 ExprFunc //!< distribution with density function presented by expression
293 bool hasConv = false;
294 switch ( data.myDistrType ) {
296 valStr += tr("SMESH_DISTR_REGULAR");
299 valStr += tr("SMESH_NB_SEGMENTS_SCALE_PARAM") + " = " + QString::number( data.myScale );\
302 //valStr += tr("SMESH_TAB_FUNC");
304 for( int i=0; i < data.myTable.length(); i++, param = !param ) {
307 valStr += QString::number( data.myTable[ i ]);
308 valStr += ( param ? "," : "]" );
314 valStr += data.myExpr;
321 valStr += "; " + tr("SMESH_CUT_NEG_MODE");
323 valStr += "; " + tr("SMESH_EXP_MODE");
329 bool StdMeshersGUI_NbSegmentsCreator::readParamsFromHypo( NbSegmentsHypothesisData& h_data ) const
331 StdMeshers::StdMeshers_NumberOfSegments_var h =
332 StdMeshers::StdMeshers_NumberOfSegments::_narrow( initParamsHypothesis() );
334 h_data.myName = hypName();
336 h_data.myNbSeg = (int) h->GetNumberOfSegments();
338 CORBA::String_var aVaribaleName = h->GetVarParameter( "SetNumberOfSegments" );
339 h_data.myNbSegVarName = aVaribaleName.in();
341 int distr = (int) h->GetDistrType();
342 h_data.myDistrType = distr;
343 h_data.myScale = distr==1 ? h->GetScaleFactor() : 1.0;
346 aVaribaleName = h->GetVarParameter( "SetScaleFactor" );
347 h_data.myScaleVarName = aVaribaleName.in();
350 h_data.myScaleVarName = QString("");
354 SMESH::double_array* a = h->GetTableFunction();
360 SMESH::double_array& a = h_data.myTable;
361 // by default, constant table function f(t)=1
363 a[0] = 0.0; a[1] = 1.0;
364 a[2] = 1.0; a[3] = 1.0;
367 h_data.myExpr = distr==3 ? h->GetExpressionFunction() : "1";
368 h_data.myConv = distr==2 || distr==3 ? h->ConversionMode() : 1; /*cut negative by default*/
373 bool StdMeshersGUI_NbSegmentsCreator::storeParamsToHypo( const NbSegmentsHypothesisData& h_data ) const
375 StdMeshers::StdMeshers_NumberOfSegments_var h =
376 StdMeshers::StdMeshers_NumberOfSegments::_narrow( hypothesis() );
382 SMESH::SetName( SMESH::FindSObject( h ), h_data.myName.toLatin1().data() );
384 h->SetVarParameter( h_data.myNbSegVarName.toLatin1().constData(), "SetNumberOfSegments" );
385 h->SetNumberOfSegments( h_data.myNbSeg );
387 int distr = h_data.myDistrType;
389 h->SetDistrType( distr ); // this is actually needed at non-uniform -> uniform switch
391 h->SetVarParameter( h_data.myScaleVarName.toLatin1().constData(), "SetScaleFactor" );
392 h->SetScaleFactor( h_data.myScale );
394 if( distr==2 || distr==3 )
395 h->SetConversionMode( h_data.myConv );
397 if( distr==1 || distr==2 || distr==3 ) {
398 h->SetReversedEdges( myDirectionWidget->GetListOfIDs() );
399 h->SetObjectEntry( myDirectionWidget->GetMainShapeEntry() );
403 h->SetTableFunction( h_data.myTable );
406 h->SetExpressionFunction( h_data.myExpr.toLatin1().data() );
407 //setting of function must follow after setConversionMode, because otherwise
408 //the function will be checked with old conversion mode, so that it may occurs
409 //unexpected errors for user
411 catch(const SALOME::SALOME_Exception& ex)
413 SalomeApp_Tools::QtCatchCorbaException(ex);
419 bool StdMeshersGUI_NbSegmentsCreator::readParamsFromWidgets( NbSegmentsHypothesisData& h_data ) const
421 h_data.myName = myName ? myName->text() : "";
422 h_data.myNbSeg = myNbSeg->value();
423 h_data.myNbSegVarName = myNbSeg->text();
424 h_data.myScaleVarName = myScale->text();
425 h_data.myDistrType = myDistr->currentIndex();
426 h_data.myConv = myConv->checkedId();
427 h_data.myScale = myScale->value();
428 myTable->data( h_data.myTable );
429 h_data.myExpr = myExpr->text();
433 void StdMeshersGUI_NbSegmentsCreator::onValueChanged()
435 int distr = myDistr->currentIndex();
437 /* if( distr==2 ) //table func
438 myCutNeg->setText( tr( "SMESH_NO_CONV" ) );
440 myCutNeg->setText( tr( "SMESH_CUT_NEG_MODE" ) );*/
442 if( distr==2 && sender()==myConv ) //table func
444 myTable->setFuncMinValue( myConv->checkedId()==0 ? -1E20 : 0 );
445 SMESH::double_array arr;
446 myTable->data( arr );
447 myTable->setData( arr ); //update data in table
450 myScale->setShown( distr==1 );
451 myLScale->setShown( distr==1 );
452 myReversedEdgesBox->setShown( distr!=0 );
453 if ( myReversedEdgesHelper ) {
454 myReversedEdgesHelper->Clear();
455 myReversedEdgesHelper->setShown( distr!=0 );
457 myDirectionWidget->ShowPreview( distr!=0 );
459 bool isFunc = distr==2 || distr==3;
460 #ifndef DISABLE_PLOT2DVIEWER
461 myPreview->setShown( isFunc );
463 myConvBox->setShown( isFunc );
465 myTable->setShown( distr==2 );
466 myExpr->setShown( distr==3 );
467 myLExpr->setShown( distr==3 );
468 myInfo->setShown( distr==3);
470 #ifndef DISABLE_PLOT2DVIEWER
472 int nbSeg = myNbSeg->value();
473 if( distr==2 ) //preview for table-described function
475 SMESH::double_array a;
477 myPreview->setParams( a, nbSeg, false );
479 else if( distr==3 ) //preview for analytic-described function
480 myPreview->setParams( myExpr->text(), nbSeg, 100, false );
483 myPreview->setConversion( StdMeshersGUI_DistrPreview::Conversion( myConv->checkedId() ) );
486 if ( (QtxComboBox*)sender() == myDistr && dlg() ) {
487 QApplication::instance()->processEvents();
488 myGroupLayout->invalidate();
489 dlg()->layout()->invalidate();
490 dlg()->updateGeometry();
491 dlg()->setMinimumSize( dlg()->minimumSizeHint() );
492 dlg()->resize( dlg()->minimumSize() );
493 QApplication::instance()->processEvents();