Back to index

salome-smesh  6.5.0
SMESHGUI_MeshDlg.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 // SMESH SMESHGUI : GUI for SMESH component
00024 // File   : SMESHGUI_MeshDlg.cxx
00025 // Author : Sergey LITONIN, Open CASCADE S.A.S.
00026 // SMESH includes
00027 //
00028 #include "SMESHGUI_MeshDlg.h"
00029 
00030 // SALOME GUI includes
00031 #include <SUIT_Session.h>
00032 #include <SUIT_ResourceMgr.h>
00033 
00034 // Qt includes
00035 #include <QVBoxLayout>
00036 #include <QGridLayout>
00037 #include <QLabel>
00038 #include <QTabWidget>
00039 #include <QGroupBox>
00040 #include <QToolButton>
00041 #include <QComboBox>
00042 #include <QMenu>
00043 #include <QCursor>
00044 #include <QPushButton>
00045 
00046 #define SPACING 6
00047 #define MARGIN  11
00048 
00054 //================================================================================
00061 //================================================================================ 
00062 SMESHGUI_MeshTab::SMESHGUI_MeshTab( QWidget* theParent )
00063   : QFrame( theParent )
00064 {
00065   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
00066   QIcon aCreateIcon( aResMgr->loadPixmap( "SMESH", tr( "ICON_HYPO" ) ) );
00067   QIcon aEditIcon( aResMgr->loadPixmap( "SMESH", tr( "ICON_HYPO_EDIT" ) ) );
00068   
00069   // Algorifm
00070   QLabel* anAlgoLbl = new QLabel( tr( "ALGORITHM" ), this );
00071   myHyp[ Algo ] = new QComboBox( this );
00072   
00073   // Hypothesis
00074   QLabel* aHypLbl = new QLabel( tr( "HYPOTHESIS" ), this );
00075   myHyp[ MainHyp ] = new QComboBox( this );
00076   myHyp[ MainHyp ]->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
00077   myCreateHyp[ MainHyp ] = new QToolButton( this );
00078   myCreateHyp[ MainHyp ]->setIcon( aCreateIcon );
00079   myEditHyp[ MainHyp ] = new QToolButton( this );
00080   myEditHyp[ MainHyp ]->setIcon( aEditIcon );
00081   
00082   // Line
00083   QFrame* aLine = new QFrame( this );
00084   aLine->setFrameStyle( QFrame::HLine | QFrame::Sunken );
00085   
00086   // Add. hypothesis
00087   QLabel* anAddHypLbl = new QLabel( tr( "ADD_HYPOTHESIS" ), this );
00088   myHyp[ AddHyp ] = new QComboBox( this );
00089   myHyp[ AddHyp ]->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
00090   myCreateHyp[ AddHyp ] = new QToolButton( this );
00091   myCreateHyp[ AddHyp ]->setIcon( aCreateIcon );
00092   myEditHyp[ AddHyp ] = new QToolButton( this );
00093   myEditHyp[ AddHyp ]->setIcon( aEditIcon );
00094   
00095   // Fill layout
00096   QGridLayout* aLay = new QGridLayout( this );
00097   aLay->setMargin( MARGIN );
00098   aLay->setSpacing( SPACING );
00099 
00100   aLay->addWidget( anAlgoLbl, 0, 0 );
00101   aLay->addWidget( myHyp[ Algo ], 0, 1 );
00102   aLay->addWidget( aHypLbl, 1, 0 );
00103   aLay->addWidget( myHyp[ MainHyp ], 1, 1 );
00104   aLay->addWidget( myCreateHyp[ MainHyp ], 1, 2 );
00105   aLay->addWidget( myEditHyp[ MainHyp ], 1, 3 );
00106   aLay->addWidget( aLine, 2, 0, 1, 4 );
00107   aLay->addWidget( anAddHypLbl, 3, 0 );
00108   aLay->addWidget( myHyp[ AddHyp ], 3, 1 );
00109   aLay->addWidget( myCreateHyp[ AddHyp ], 3, 2 );
00110   aLay->addWidget( myEditHyp[ AddHyp ], 3, 3 );
00111   aLay->addItem( new QSpacerItem( 0, 0, QSizePolicy::Fixed, QSizePolicy::Expanding ), 4, 0 );
00112     
00113   // Connect signals and slots
00114   for ( int i = MainHyp; i <= AddHyp; i++ )
00115   {
00116     connect( myCreateHyp[ i ], SIGNAL( clicked() )       ,  SLOT( onCreateHyp() ) );
00117     connect( myEditHyp[ i ]  , SIGNAL( clicked() )       ,  SLOT( onEditHyp() ) );
00118     connect( myHyp[ i ]      , SIGNAL( activated( int ) ),  SLOT( onHyp( int ) ) );
00119   }
00120   connect( myHyp[ Algo ], SIGNAL( activated( int ) ), SLOT( onHyp( int ) ) );
00121   
00122   // Initialize controls
00123   
00124   setAvailableHyps( Algo, QStringList() );
00125   setAvailableHyps( MainHyp, QStringList() );
00126   setAvailableHyps( AddHyp, QStringList() );
00127 }
00128 
00129 //================================================================================
00133 //================================================================================ 
00134 SMESHGUI_MeshTab::~SMESHGUI_MeshTab()
00135 {
00136 }
00137 
00138 //================================================================================
00146 //================================================================================
00147 void SMESHGUI_MeshTab::setAvailableHyps( const int theId, const QStringList& theHyps )
00148 {
00149   myAvailableHyps[ theId ] = theHyps;
00150 
00151   bool enable = ! theHyps.isEmpty();
00152   if ( theId == Algo )
00153   {
00154     myHyp[ Algo ]->clear();
00155     myHyp[ Algo ]->addItem( tr( "NONE" ) );
00156     myHyp[ Algo ]->addItems( theHyps );
00157     myHyp[ Algo ]->setCurrentIndex( 0 );
00158   }
00159   else {
00160     myCreateHyp[ theId ]->setEnabled( enable );
00161     myEditHyp[ theId ]->setEnabled( false );
00162   }
00163   myHyp[ theId ]->setEnabled( enable );
00164 }
00165 
00166 //================================================================================
00174 //================================================================================
00175 void SMESHGUI_MeshTab::setExistingHyps( const int theId, const QStringList& theHyps )
00176 {
00177   if ( theId != Algo )
00178   {
00179     myHyp[ theId ]->clear();
00180     myHyp[ theId ]->addItem( tr( "NONE" ) );
00181     myHyp[ theId ]->addItems( theHyps );
00182     myHyp[ theId ]->setCurrentIndex( 0 );
00183     myHyp[ theId ]->setEnabled( !theHyps.isEmpty() );
00184     myEditHyp[ theId ]->setEnabled( false );
00185   }
00186 }
00187 
00188 //================================================================================
00197 //================================================================================
00198 void SMESHGUI_MeshTab::addHyp( const int theId, const QString& theHyp )
00199 {
00200   myHyp[ theId ]->addItem( theHyp );
00201   myHyp[ theId ]->setCurrentIndex( myHyp[ theId ]->count() - 1 );
00202   myEditHyp[ theId ]->setEnabled( true );
00203   myHyp[ theId ]->setEnabled( true );
00204 }
00205 
00206 //================================================================================
00215 //================================================================================
00216 void SMESHGUI_MeshTab::renameHyp( const int theId, 
00217                                   const int theIndex, 
00218                                   const QString& theNewName )
00219 {
00220   if ( theIndex > 0 && theIndex < myHyp[ theId ]->count() )
00221     myHyp[ theId ]->setItemText( theIndex, theNewName );
00222 }                                  
00223 
00224 //================================================================================
00232 //================================================================================
00233 void SMESHGUI_MeshTab::setCurrentHyp( const int theId, const int theIndex )
00234 {
00235   if ( theIndex >= 0 && theIndex < myHyp[ theId ]->count() )
00236   {
00237     myHyp[ theId ]->setCurrentIndex( theIndex );
00238     if ( myEditHyp[ theId ] )
00239       myEditHyp[ theId ]->setEnabled( theIndex > 0 );
00240   }
00241 }
00242 
00243 //================================================================================
00251 //================================================================================
00252 int SMESHGUI_MeshTab::currentHyp( const int theId ) const
00253 {
00254   return myHyp[ theId ]->currentIndex();
00255 }
00256 
00257 //================================================================================
00264 //================================================================================
00265 void SMESHGUI_MeshTab::onCreateHyp()
00266 {
00267   bool isMainHyp = sender() == myCreateHyp[ MainHyp ];
00268 
00269   QMenu aPopup( this );
00270   
00271   QStringList aHypNames = isMainHyp ? 
00272     myAvailableHyps[ MainHyp ] : myAvailableHyps[ AddHyp ];
00273 
00274   QList<QAction*> actions;
00275   for ( int i = 0, n = aHypNames.count(); i < n; i++ )
00276     actions.append( aPopup.addAction( aHypNames[ i ] ) );
00277 
00278   QAction* a = aPopup.exec( QCursor::pos() );
00279   if ( a )
00280     emit createHyp( isMainHyp ? MainHyp : AddHyp, actions.indexOf( a ) );
00281 }
00282 
00283 //================================================================================
00290 //================================================================================
00291 void SMESHGUI_MeshTab::onEditHyp()
00292 {
00293   const QObject* aSender = sender();
00294   int aHypType = aSender == myEditHyp[ MainHyp ] ? MainHyp : AddHyp;
00295   emit editHyp( aHypType, myHyp[ aHypType ]->currentIndex() - 1 );  // - 1 because there is NONE on the top
00296 }
00297 
00298 //================================================================================
00306 //================================================================================
00307 void SMESHGUI_MeshTab::onHyp( int theIndex )
00308 {
00309   const QObject* aSender = sender();
00310   if ( aSender == myHyp[ Algo ] )
00311     emit selectAlgo( theIndex - 1 ); // - 1 because there is NONE on the top
00312   else {
00313     int anIndex = aSender == myHyp[ MainHyp ] ? MainHyp : AddHyp;
00314     myEditHyp[ anIndex ]->setEnabled( theIndex > 0 );
00315   }
00316 }
00317 
00318 //================================================================================
00324 //================================================================================  
00325 void SMESHGUI_MeshTab::reset()
00326 {
00327   for ( int i = Algo; i <= AddHyp; i++ )
00328   {
00329     myHyp[ i ]->setCurrentIndex( 0 );
00330     if ( myEditHyp[ i ] )
00331       myEditHyp[ i ]->setEnabled( false );
00332   }
00333 }
00334 
00341 //================================================================================
00351 //================================================================================
00352 SMESHGUI_MeshDlg::SMESHGUI_MeshDlg( const bool theToCreate, const bool theIsMesh )
00353 : SMESHGUI_Dialog( 0, false, true )
00354 {
00355   // Create top controls
00356 
00357   setObjectPixmap( "SMESH", tr( "ICON_SELECT" ) );
00358   // name 
00359   createObject( tr( "NAME" ), mainFrame(), Obj );
00360   setNameIndication( Obj, OneName );
00361   setReadOnly( Obj, false );
00362   // mesh
00363   createObject( tr( "MESH" ), mainFrame(), Mesh );
00364   // geometry
00365   createObject( tr( "GEOMETRY" ), mainFrame(), Geom );
00366   myGeomPopup = 0;
00367   
00368   // Create tab widget
00369   
00370   myTabWg = new QTabWidget( mainFrame() );
00371   myTabs[ Dim0D ] = new SMESHGUI_MeshTab( myTabWg );
00372   myTabs[ Dim1D ] = new SMESHGUI_MeshTab( myTabWg );
00373   myTabs[ Dim2D ] = new SMESHGUI_MeshTab( myTabWg );
00374   myTabs[ Dim3D ] = new SMESHGUI_MeshTab( myTabWg );
00375   myTabWg->addTab( myTabs[ Dim3D ], tr( "DIM_3D" ) );
00376   myTabWg->addTab( myTabs[ Dim2D ], tr( "DIM_2D" ) );
00377   myTabWg->addTab( myTabs[ Dim1D ], tr( "DIM_1D" ) );
00378   myTabWg->addTab( myTabs[ Dim0D ], tr( "DIM_0D" ) );
00379 
00380   // Hypotheses Sets
00381   myHypoSetButton = new QToolButton( mainFrame() );
00382   myHypoSetButton->setText( tr( "HYPOTHESES_SETS" ) );
00383   myHypoSetButton->setEnabled( false );
00384   myHypoSetButton->setSizePolicy( QSizePolicy::MinimumExpanding, 
00385                                   myHypoSetButton->sizePolicy().verticalPolicy() );
00386   
00387   // Fill layout
00388   QGridLayout* aLay = new QGridLayout( mainFrame() );
00389   aLay->setMargin( 0 );
00390   aLay->setSpacing( SPACING );
00391 
00392   aLay->addWidget( objectWg( Obj,  Label ),   0, 0 );
00393   aLay->addWidget( objectWg( Obj,  Btn ),     0, 1 );
00394   aLay->addWidget( objectWg( Obj,  Control ), 0, 2 );
00395   aLay->addWidget( objectWg( Mesh, Label ),   1, 0 );
00396   aLay->addWidget( objectWg( Mesh, Btn ),     1, 1 );
00397   aLay->addWidget( objectWg( Mesh, Control ), 1, 2 );
00398   aLay->addWidget( objectWg( Geom, Label ),   2, 0 );
00399   aLay->addWidget( objectWg( Geom, Btn ),     2, 1 );
00400   aLay->addWidget( objectWg( Geom, Control ), 2, 2 );
00401   aLay->addWidget( myTabWg,                   4, 0, 1, 3 );
00402   aLay->addWidget( myHypoSetButton,           5, 0, 1, 3 );
00403   aLay->setRowMinimumHeight( 3, 20 );
00404 
00405   // Disable controls if necessary
00406   setObjectShown( Mesh, false );
00407   if ( theToCreate )
00408   {
00409     setWindowTitle( tr( "CREATE_MESH" ) );
00410     objectWg( Obj, Btn )->hide();
00411     if ( theIsMesh )
00412       setWindowTitle( tr( "CREATE_MESH" ) );
00413     else
00414     {
00415       setWindowTitle( tr( "CREATE_SUBMESH" ) );
00416       setObjectShown( Mesh, true );
00417     }
00418   }
00419   else
00420   {
00421     setWindowTitle( tr( "EDIT_MESH_SUBMESH" ) );
00422     objectWg( Mesh, Btn )->hide();
00423     objectWg( Geom, Btn )->hide();
00424   }
00425 }
00426 
00427 SMESHGUI_MeshDlg::~SMESHGUI_MeshDlg()
00428 {
00429 }
00430 
00431 //================================================================================
00440 //================================================================================
00441 SMESHGUI_MeshTab* SMESHGUI_MeshDlg::tab( const int theId ) const
00442 {
00443   return ( theId >= Dim0D && theId <= Dim3D ? myTabs[ theId ] : 0 );
00444 }
00445 
00446 //================================================================================
00450 //================================================================================  
00451 void SMESHGUI_MeshDlg::reset()
00452 {
00453   clearSelection();
00454   myTabs[ Dim0D ]->reset();
00455   myTabs[ Dim1D ]->reset();
00456   myTabs[ Dim2D ]->reset();
00457   myTabs[ Dim3D ]->reset();
00458 }
00459 
00460 //================================================================================
00464 //================================================================================    
00465 void SMESHGUI_MeshDlg::setCurrentTab( const int theId  )
00466 {
00467   myTabWg->setCurrentIndex( Dim3D - theId );
00468 }
00469 
00470 //================================================================================
00475 //================================================================================
00476 
00477 void SMESHGUI_MeshDlg::setMaxHypoDim( const int maxDim )
00478 {
00479   const int DIM = maxDim;
00480   for ( int dim = Dim0D; dim <= Dim3D; ++dim ) {
00481     bool enable = ( dim <= DIM );
00482     if ( !enable ) {
00483       myTabs[ dim ]->reset();
00484       disableTab( dim );
00485     }
00486     else {
00487       enableTab( dim );
00488     }
00489   }
00490   // deselect desabled tab
00491   if ( !myTabWg->isTabEnabled( myTabWg->currentIndex() ) )
00492     setCurrentTab( DIM );
00493 }
00494 
00495 //================================================================================
00499 //================================================================================
00500 
00501 void SMESHGUI_MeshDlg::setHypoSets( const QStringList& theSets )
00502 {
00503   QMenu* aHypoSetPopup = myHypoSetButton->menu();
00504   if ( !aHypoSetPopup ) {
00505     aHypoSetPopup = new QMenu( myHypoSetButton );
00506     connect( aHypoSetPopup, SIGNAL( triggered( QAction* ) ), SLOT( onHypoSetPopup( QAction* ) ) );
00507     myHypoSetButton->setMenu( aHypoSetPopup );
00508     myHypoSetButton->setPopupMode( QToolButton::InstantPopup );
00509   }
00510   aHypoSetPopup->clear();
00511   for ( int i = 0, n = theSets.count(); i < n; i++ ) {
00512     aHypoSetPopup->addAction( theSets[ i ] );
00513   }
00514   myHypoSetButton->setEnabled( !aHypoSetPopup->isEmpty() );
00515 }
00516 
00517 //================================================================================
00524 //================================================================================
00525 
00526 void SMESHGUI_MeshDlg::onHypoSetPopup( QAction* a )
00527 {
00528   emit hypoSet( a->text() );
00529 }
00530   
00531 //================================================================================
00536 //================================================================================
00537 
00538 enum { DIRECT_GEOM_INDEX = 0, GEOM_BY_MESH_INDEX };
00539 
00540 void SMESHGUI_MeshDlg::setGeomPopupEnabled( const bool enable )
00541 {
00542   if ( QToolButton* selBtn = qobject_cast<QToolButton*>( objectWg( Geom, Btn )))
00543   {
00544     if ( enable ) {
00545       if ( ! myGeomPopup ) {
00546         myGeomPopup = new QMenu();
00547         myGeomPopup->addAction( tr("DIRECT_GEOM_SELECTION") )->setData( DIRECT_GEOM_INDEX );
00548         myGeomPopup->addAction( tr("GEOM_BY_MESH_ELEM_SELECTION") )->setData( GEOM_BY_MESH_INDEX );
00549         connect( myGeomPopup, SIGNAL( triggered( QAction* ) ), SLOT( onGeomPopup( QAction* ) ) );
00550         connect( selBtn, SIGNAL( toggled(bool) ), this, SLOT( onGeomSelectionButton(bool) ));
00551       }
00552     }
00553     else {
00554       disconnect( selBtn, SIGNAL( toggled(bool) ), this, SLOT( onGeomSelectionButton(bool) ));
00555       if ( myGeomPopup ) {
00556         delete myGeomPopup;
00557         myGeomPopup = 0;
00558       }
00559     }
00560   }
00561 }
00562 
00563 
00564 //================================================================================
00569 //================================================================================
00570 void SMESHGUI_MeshDlg::disableTab(const int theTabId) {
00571   myTabWg->setTabEnabled( myTabWg->indexOf( myTabs[ theTabId ] ), false );
00572   if ( theTabId == Dim3D ) myHypoSetButton->setEnabled( false );
00573 }
00574 
00575 //================================================================================
00580 //================================================================================
00581 void SMESHGUI_MeshDlg::enableTab(const int theTabId) {
00582   myTabWg->setTabEnabled( myTabWg->indexOf( myTabs[ theTabId ] ), true );
00583   if ( theTabId == Dim3D ) {
00584     QMenu* aHypoSetPopup = myHypoSetButton->menu();
00585     myHypoSetButton->setEnabled( aHypoSetPopup && !aHypoSetPopup->actions().isEmpty() );
00586   }
00587 }
00588 
00589 //================================================================================
00594 //================================================================================
00595 bool SMESHGUI_MeshDlg::isTabEnabled(const int theTabId) const {
00596   return myTabWg->isTabEnabled( myTabWg->indexOf( myTabs[ theTabId ] ) );
00597 }
00598 
00599 void SMESHGUI_MeshDlg::onGeomSelectionButton(bool isBtnOn)
00600 {
00601   if ( myGeomPopup && isBtnOn )
00602     myGeomPopup->exec( QCursor::pos() );
00603 }
00604 
00605 void SMESHGUI_MeshDlg::onGeomPopup( QAction* a )
00606 {
00607   emit geomSelectionByMesh( a->data().toInt() == GEOM_BY_MESH_INDEX );
00608 }
00609 
00610 int SMESHGUI_MeshDlg::getActiveObject()
00611 {
00612   for (int i = 0; i < 3; ++i )
00613     if ( isObjectShown( i ) &&
00614          (( QToolButton* )objectWg( i, Btn ))->isChecked())
00615       return i;
00616   return -1;
00617 }