Back to index

salome-smesh  6.5.0
StdMeshersGUI_NbSegmentsCreator.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // File   : StdMeshersGUI_NbSegmentsCreator.cxx
00024 // Author : Open CASCADE S.A.S.
00025 // SMESH includes
00026 //
00027 #include "StdMeshersGUI_NbSegmentsCreator.h"
00028 #include "StdMeshersGUI_DistrTable.h"
00029 #include "StdMeshersGUI_DistrPreview.h"
00030 #include "StdMeshersGUI_SubShapeSelectorWdg.h"
00031 
00032 #include <SMESHGUI.h>
00033 #include <SMESHGUI_Utils.h>
00034 #include <SMESHGUI_HypothesesUtils.h>
00035 #include <SMESHGUI_SpinBox.h>
00036 
00037 // IDL includes
00038 #include CORBA_SERVER_HEADER(SMESH_BasicHypothesis)
00039 
00040 // SALOME GUI includes
00041 #include <SalomeApp_Tools.h>
00042 #include <SalomeApp_IntSpinBox.h>
00043 #include <QtxComboBox.h>
00044 
00045 // Qt includes
00046 #include <QLabel>
00047 #include <QGroupBox>
00048 #include <QFrame>
00049 #include <QLineEdit>
00050 #include <QButtonGroup>
00051 #include <QRadioButton>
00052 #include <QGridLayout>
00053 #include <QVBoxLayout>
00054 #include <QHBoxLayout>
00055 #include <QApplication>
00056 
00057 #define SPACING 6
00058 #define MARGIN  11
00059 
00060 StdMeshersGUI_NbSegmentsCreator::StdMeshersGUI_NbSegmentsCreator()
00061 : StdMeshersGUI_StdHypothesisCreator( "NumberOfSegments" ),
00062   myNbSeg( 0 ),
00063   myDistr( 0 ),
00064   myScale( 0 ),
00065   myTable( 0 ),
00066   myPreview( 0 ),
00067   myExpr( 0 ),
00068   myConvBox( 0 ),
00069   myConv( 0 ),
00070   myLScale( 0 ),
00071   myLTable( 0 ),
00072   myLExpr( 0 ),
00073   myInfo( 0 ),
00074   myGroupLayout( 0 ),
00075   myTableRow( 0 ),
00076   myPreviewRow( 0 )
00077 {
00078 }
00079 
00080 StdMeshersGUI_NbSegmentsCreator::~StdMeshersGUI_NbSegmentsCreator()
00081 {
00082 }
00083 
00084 bool StdMeshersGUI_NbSegmentsCreator::checkParams( QString& msg ) const
00085 {
00086   if( !SMESHGUI_GenericHypothesisCreator::checkParams( msg ) )
00087     return false;
00088   NbSegmentsHypothesisData data_old, data_new;
00089   readParamsFromHypo( data_old );
00090   readParamsFromWidgets( data_new );
00091   bool res = storeParamsToHypo( data_new );
00092   storeParamsToHypo( data_old );
00093   res = myNbSeg->isValid( msg, true ) && res;
00094   res = myScale->isValid( msg, true ) && res;
00095   return res;
00096 }
00097 
00098 QFrame* StdMeshersGUI_NbSegmentsCreator::buildFrame()
00099 {
00100   QFrame* fr = new QFrame();
00101   fr->setMinimumWidth(460);
00102 
00103   QVBoxLayout* lay = new QVBoxLayout( fr );
00104   lay->setMargin( 0 );
00105   lay->setSpacing( SPACING );
00106 
00107   QGroupBox* GroupC1 = new QGroupBox( tr( "SMESH_ARGUMENTS" ), fr );
00108   lay->addWidget( GroupC1 );
00109 
00110   StdMeshers::StdMeshers_NumberOfSegments_var h =
00111     StdMeshers::StdMeshers_NumberOfSegments::_narrow( hypothesis() );
00112 
00113   myGroupLayout = new QGridLayout( GroupC1 );
00114   myGroupLayout->setSpacing( SPACING );
00115   myGroupLayout->setMargin( MARGIN );
00116   myGroupLayout->setColumnStretch( 0, 0 );
00117   myGroupLayout->setColumnStretch( 1, 1 );
00118 
00119   int row = 0;
00120   // 0)  name
00121   myName = 0;
00122   if( isCreation() )
00123   {
00124     myName = new QLineEdit( GroupC1 );
00125     myGroupLayout->addWidget( new QLabel( tr( "SMESH_NAME" ), GroupC1 ), row, 0 );
00126     myGroupLayout->addWidget( myName, row, 1 );
00127     row++;
00128   }
00129 
00130 
00131   // 1)  number of segments
00132   myGroupLayout->addWidget( new QLabel( tr( "SMESH_NB_SEGMENTS_PARAM" ), GroupC1 ), row, 0 );
00133   myNbSeg = new SalomeApp_IntSpinBox( GroupC1 );
00134   myNbSeg->setMinimum( 1 );
00135   myNbSeg->setMaximum( 9999 );
00136   myGroupLayout->addWidget( myNbSeg, row, 1 );
00137   row++;
00138 
00139   
00140   // 2)  type of distribution
00141   myGroupLayout->addWidget( new QLabel( tr( "SMESH_DISTR_TYPE" ), GroupC1 ), row, 0 );
00142   myDistr = new QtxComboBox( GroupC1 );
00143   QStringList types;
00144   types.append( tr( "SMESH_DISTR_REGULAR" ) );
00145   types.append( tr( "SMESH_DISTR_SCALE"   ) );
00146   types.append( tr( "SMESH_DISTR_TAB"     ) );
00147   types.append( tr( "SMESH_DISTR_EXPR"    ) );
00148   myDistr->addItems( types );
00149   myGroupLayout->addWidget( myDistr, row, 1 );
00150   row++;
00151 
00152   
00153   // 3)  scale
00154   myGroupLayout->addWidget( myLScale = new QLabel( tr( "SMESH_NB_SEGMENTS_SCALE_PARAM" ), GroupC1 ), row, 0 );
00155   myScale = new SMESHGUI_SpinBox( GroupC1 );
00156   myScale->RangeStepAndValidator( 1E-5, 1E+5, 0.1, "parametric_precision" );
00157   myGroupLayout->addWidget( myScale, row, 1 );
00158   row++;
00159 
00160   
00161   // 4) Distribution definition
00162   QGridLayout* myDistLayout = new QGridLayout(GroupC1);
00163   myGroupLayout->addLayout( myDistLayout, row, 0, 1, 2 );
00164   myGroupLayout->setRowStretch( row, 1 );
00165   row ++;
00166 
00167        // a)  expression
00168   QHBoxLayout* myExprLayout = new QHBoxLayout(GroupC1);
00169   myExprLayout->addWidget( myLExpr = new QLabel( "f(t)=", GroupC1 ), 0);
00170   myExpr = new QLineEdit( GroupC1 );
00171   myExprLayout->addWidget( myExpr,1);
00172   myDistLayout->addLayout(myExprLayout,1 ,0);
00173   myDistLayout->setRowStretch(2, 1);
00174 
00175        // b)  warning
00176   myInfo = new QLabel( tr( "SMESH_FUNC_DOMAIN" ), GroupC1 );
00177   myDistLayout->addWidget( myInfo, 0, 0, 1, 2);
00178   
00179        // c)  table
00180   myTable = new StdMeshersGUI_DistrTableFrame( GroupC1 );
00181   myDistLayout->addWidget( myTable, 1, 0, 2, 1 );
00182 
00183        // d) preview
00184   myPreview = new StdMeshersGUI_DistrPreview( GroupC1, h.in() );  
00185   myPreview->setMinimumHeight(220);
00186   myDistLayout->addWidget( myPreview, 1, 1, 2, 1 );
00187   
00188   // 5)  conversion (radiogroup)
00189   myConvBox = new QGroupBox( tr( "SMESH_CONV_MODE" ), GroupC1 );
00190   myConv = new QButtonGroup( GroupC1 );
00191 
00192   QHBoxLayout* convLay = new QHBoxLayout( myConvBox );
00193   convLay->setMargin( MARGIN );
00194   convLay->setSpacing( SPACING );
00195 
00196   QRadioButton* rbExp = new QRadioButton( tr( "SMESH_EXP_MODE" ), myConvBox );
00197   QRadioButton* myCutNeg = new QRadioButton( tr( "SMESH_CUT_NEG_MODE" ), myConvBox );
00198 
00199   convLay->addWidget( rbExp );
00200   convLay->addWidget( myCutNeg );
00201   myConv->addButton( rbExp, 0 );
00202   myConv->addButton( myCutNeg, 1 );
00203 
00204   myGroupLayout->addWidget( myConvBox, row, 0, 1, 2 );
00205   row++;
00206 
00207 
00208   // 6) reverse edge parameters
00209   myReversedEdgesBox = new QGroupBox(tr( "SMESH_REVERSED_EDGES" ), fr);
00210   QHBoxLayout* edgeLay = new QHBoxLayout( myReversedEdgesBox );
00211 
00212   myDirectionWidget = new StdMeshersGUI_SubShapeSelectorWdg( myReversedEdgesBox );
00213   QString aGeomEntry = getShapeEntry();
00214   QString aMainEntry = getMainShapeEntry();
00215   if ( aGeomEntry == "" )
00216     aGeomEntry = h->GetObjectEntry();
00217   myDirectionWidget->SetGeomShapeEntry( aGeomEntry );
00218   myDirectionWidget->SetMainShapeEntry( aMainEntry );
00219   myDirectionWidget->SetListOfIDs( h->GetReversedEdges() );
00220   edgeLay->addWidget( myDirectionWidget );
00221 
00222   lay->addWidget( myReversedEdgesBox );
00223   lay->setStretchFactor( GroupC1, 2);
00224   lay->setStretchFactor( myReversedEdgesBox, 1);
00225   
00226   connect( myNbSeg, SIGNAL( valueChanged( const QString& ) ), this, SLOT( onValueChanged() ) );
00227   connect( myDistr, SIGNAL( activated( int ) ), this, SLOT( onValueChanged() ) );
00228   connect( myTable, SIGNAL( valueChanged( int, int ) ), this, SLOT( onValueChanged() ) );
00229   connect( myExpr,  SIGNAL( textChanged( const QString& ) ), this, SLOT( onValueChanged() ) );
00230   connect( myConv,  SIGNAL( buttonClicked( int ) ), this, SLOT( onValueChanged() ) );
00231 
00232   return fr;
00233 }
00234 
00235 void StdMeshersGUI_NbSegmentsCreator::retrieveParams() const
00236 {
00237   NbSegmentsHypothesisData data;
00238   readParamsFromHypo( data );
00239 
00240   if( myName )
00241     myName->setText( data.myName );
00242   if(data.myNbSegVarName.isEmpty())
00243     myNbSeg->setValue( data.myNbSeg );
00244   else
00245     myNbSeg->setText( data.myNbSegVarName );
00246   
00247   myDistr->setCurrentIndex( data.myDistrType );
00248   if(data.myScaleVarName.isEmpty())
00249     myScale->setValue( data.myScale );
00250   else
00251     myScale->setText( data.myScaleVarName );
00252   myConv->button( data.myConv )->setChecked( true );
00253   myTable->setFuncMinValue(myConv->checkedId()==0 ? -1E20 : 0);
00254   myTable->setData( data.myTable );
00255   myExpr->setText( data.myExpr );
00256 
00257   if ( dlg() )
00258     dlg()->setMinimumSize( dlg()->minimumSizeHint().width(), dlg()->minimumSizeHint().height() );
00259 }
00260 
00261 QString StdMeshersGUI_NbSegmentsCreator::storeParams() const
00262 {
00263   NbSegmentsHypothesisData data;
00264   readParamsFromWidgets( data );
00265   storeParamsToHypo( data );
00266     
00267   QString valStr = QString::number( data.myNbSeg ) += "; ";
00268 
00269   enum DistrType
00270   {
00271     Regular, 
00272     Scale,   
00273     TabFunc, 
00274     ExprFunc 
00275   };
00276   bool hasConv = false;
00277   switch ( data.myDistrType ) {
00278   case Regular :
00279     valStr += tr("SMESH_DISTR_REGULAR");
00280     break;
00281   case Scale   : 
00282     valStr += tr("SMESH_NB_SEGMENTS_SCALE_PARAM") + " = " + QString::number( data.myScale );\
00283     break;
00284   case TabFunc : {
00285     //valStr += tr("SMESH_TAB_FUNC");
00286     bool param = true;
00287     for( int i=0; i < data.myTable.length(); i++, param = !param ) {
00288       if ( param )
00289         valStr += "[";
00290       valStr += QString::number( data.myTable[ i ]);
00291       valStr += ( param ? "," : "]" );
00292     }
00293     hasConv = true;
00294     break;
00295   }
00296   case ExprFunc:
00297     valStr += data.myExpr;
00298     hasConv = true;
00299     break;
00300   }
00301   if ( hasConv )
00302   {
00303     if ( data.myConv )
00304       valStr += "; " + tr("SMESH_CUT_NEG_MODE");
00305     else
00306       valStr += "; " + tr("SMESH_EXP_MODE");
00307   }
00308 
00309   return valStr;
00310 }
00311 
00312 bool StdMeshersGUI_NbSegmentsCreator::readParamsFromHypo( NbSegmentsHypothesisData& h_data ) const
00313 {
00314   StdMeshers::StdMeshers_NumberOfSegments_var h =
00315     StdMeshers::StdMeshers_NumberOfSegments::_narrow( initParamsHypothesis() );
00316 
00317   h_data.myName = hypName();
00318 
00319   h_data.myNbSeg = (int) h->GetNumberOfSegments();
00320 
00321   CORBA::String_var aVaribaleName = h->GetVarParameter( "SetNumberOfSegments" );
00322   h_data.myNbSegVarName = aVaribaleName.in();
00323 
00324   int distr = (int) h->GetDistrType();
00325   h_data.myDistrType = distr;
00326   h_data.myScale = distr==1 ? h->GetScaleFactor() : 1.0;
00327   
00328   if(distr==1) {
00329     aVaribaleName = h->GetVarParameter( "SetScaleFactor" );
00330     h_data.myScaleVarName = aVaribaleName.in();
00331   }
00332   else 
00333     h_data.myScaleVarName = QString("");
00334 
00335   if( distr==2 )
00336   {
00337     SMESH::double_array* a = h->GetTableFunction();
00338     h_data.myTable = *a;
00339     delete a;
00340   }
00341   else
00342   {
00343     SMESH::double_array& a = h_data.myTable;
00344     // by default, constant table function f(t)=1
00345     a.length( 4 );
00346     a[0] = 0.0; a[1] = 1.0;
00347     a[2] = 1.0; a[3] = 1.0; 
00348   }
00349 
00350   h_data.myExpr = distr==3 ? h->GetExpressionFunction() : "1";
00351   h_data.myConv = distr==2 || distr==3 ? h->ConversionMode() : 1; /*cut negative by default*/
00352 
00353   return true;
00354 }
00355 
00356 bool StdMeshersGUI_NbSegmentsCreator::storeParamsToHypo( const NbSegmentsHypothesisData& h_data ) const
00357 {
00358   StdMeshers::StdMeshers_NumberOfSegments_var h =
00359     StdMeshers::StdMeshers_NumberOfSegments::_narrow( hypothesis() );
00360 
00361   bool ok = true;
00362   try
00363   {
00364     if( isCreation() )
00365       SMESH::SetName( SMESH::FindSObject( h ), h_data.myName.toLatin1().data() );
00366 
00367     h->SetVarParameter( h_data.myNbSegVarName.toLatin1().constData(), "SetNumberOfSegments" );
00368     h->SetNumberOfSegments( h_data.myNbSeg );
00369     int distr = h_data.myDistrType;
00370     h->SetDistrType( distr );
00371     
00372     if( distr==1 ) {
00373       h->SetVarParameter( h_data.myScaleVarName.toLatin1().constData(), "SetScaleFactor" );
00374       h->SetScaleFactor( h_data.myScale );
00375     }
00376     if( distr==2 || distr==3 )
00377       h->SetConversionMode( h_data.myConv );
00378 
00379     if( distr==1 || distr==2 || distr==3 ) {
00380       h->SetReversedEdges( myDirectionWidget->GetListOfIDs() );
00381       h->SetObjectEntry( myDirectionWidget->GetMainShapeEntry() );
00382     }
00383 
00384     if( distr==2 )
00385       h->SetTableFunction( h_data.myTable );
00386 
00387     if( distr==3 )
00388       h->SetExpressionFunction( h_data.myExpr.toLatin1().data() );
00389     //setting of function must follow after setConversionMode, because otherwise
00390     //the function will be checked with old conversion mode, so that it may occurs
00391     //unexpected errors for user
00392   }
00393   catch(const SALOME::SALOME_Exception& ex)
00394   {
00395     SalomeApp_Tools::QtCatchCorbaException(ex);
00396     ok = false;
00397   }
00398   return ok;
00399 }
00400 
00401 bool StdMeshersGUI_NbSegmentsCreator::readParamsFromWidgets( NbSegmentsHypothesisData& h_data ) const
00402 {
00403   h_data.myName      = myName ? myName->text() : "";
00404   h_data.myNbSeg     = myNbSeg->value();
00405   h_data.myNbSegVarName =  myNbSeg->text();
00406   h_data.myScaleVarName =  myScale->text();
00407   h_data.myDistrType = myDistr->currentIndex();
00408   h_data.myConv      = myConv->checkedId();
00409   h_data.myScale     = myScale->value();
00410   myTable->data( h_data.myTable );
00411   h_data.myExpr      = myExpr->text();
00412   return true;
00413 }
00414 
00415 void StdMeshersGUI_NbSegmentsCreator::onValueChanged()
00416 {
00417   int distr = myDistr->currentIndex();
00418 
00419 /*  if( distr==2 ) //table func
00420     myCutNeg->setText( tr( "SMESH_NO_CONV" ) );
00421   else if( distr==3 )
00422     myCutNeg->setText( tr( "SMESH_CUT_NEG_MODE" ) );*/
00423 
00424   if( distr==2 && sender()==myConv ) //table func
00425   {
00426     myTable->setFuncMinValue( myConv->checkedId()==0 ? -1E20 : 0 );
00427     SMESH::double_array arr;
00428     myTable->data( arr );
00429     myTable->setData( arr ); //update data in table
00430   }
00431 
00432   myScale->setShown( distr==1 );
00433   myLScale->setShown( distr==1 );
00434   myReversedEdgesBox->setShown( !distr==0 );
00435   myDirectionWidget->showPreview( !distr==0 );
00436 
00437   bool isFunc = distr==2 || distr==3;
00438   myPreview->setShown( isFunc );
00439   myConvBox->setShown( isFunc );
00440   
00441   myTable->setShown( distr==2 );
00442   myExpr->setShown( distr==3 );
00443   myLExpr->setShown( distr==3 );
00444   myInfo->setShown( distr==3);
00445 
00446   //change of preview
00447   int nbSeg = myNbSeg->value();
00448   if( distr==2 ) //preview for table-described function
00449   {
00450     SMESH::double_array a;
00451     myTable->data( a );
00452     myPreview->setParams( a, nbSeg, false );
00453   }
00454   else if( distr==3 ) //preview for analytic-described function
00455     myPreview->setParams( myExpr->text(), nbSeg, 100, false );
00456 
00457   if( isFunc )
00458     myPreview->setConversion( StdMeshersGUI_DistrPreview::Conversion( myConv->checkedId() ) );
00459 
00460   if ( (QtxComboBox*)sender() == myDistr && dlg() ) {
00461     QApplication::instance()->processEvents();
00462     myGroupLayout->invalidate();
00463     dlg()->layout()->invalidate();
00464     dlg()->updateGeometry();
00465     dlg()->setMinimumSize( dlg()->minimumSizeHint() );
00466     dlg()->resize( dlg()->minimumSize() );
00467     QApplication::instance()->processEvents();
00468   }
00469 }