Back to index

salome-geom  6.5.0
BlocksGUI_ExplodeDlg.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 // GEOM GEOMGUI : GUI for Geometry component
00024 // File   : BlocksGUI_ExplodeDlg.cxx
00025 // Author : Julia DOROVSKIKH, Open CASCADE S.A.S. (julia.dorovskikh@opencascade.com)
00026 //
00027 #include "BlocksGUI_ExplodeDlg.h"
00028 
00029 #include <DlgRef.h>
00030 #include <GeometryGUI.h>
00031 #include <GEOMBase.h>
00032 
00033 #include <SUIT_Session.h>
00034 #include <SUIT_Desktop.h>
00035 #include <SUIT_ResourceMgr.h>
00036 #include <SUIT_ViewWindow.h>
00037 #include <SUIT_ViewManager.h>
00038 #include <SUIT_MessageBox.h>
00039 #include <SalomeApp_Application.h>
00040 #include <LightApp_SelectionMgr.h>
00041 #include <OCCViewer_ViewModel.h>
00042 #include <SALOME_ListIteratorOfListIO.hxx>
00043 
00044 //=================================================================================
00045 // class    : BlocksGUI_ExplodeDlg()
00046 // purpose  : Constructs a BlocksGUI_ExplodeDlg which is a child of 'parent'.
00047 //=================================================================================
00048 BlocksGUI_ExplodeDlg::BlocksGUI_ExplodeDlg( GeometryGUI* theGeometryGUI, QWidget* parent )
00049   : GEOMBase_Skeleton( theGeometryGUI, parent )
00050 {
00051   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_BLOCK_EXPLODE" ) ) );
00052   QPixmap imageS( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
00053 
00054   setWindowTitle( tr( "GEOM_BLOCK_EXPLODE_TITLE" ) );
00055 
00056   /***************************************************************/
00057   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_BLOCK_EXPLODE" ) );
00058 
00059   mainFrame()->RadioButton1->setIcon( image1 );
00060   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
00061   mainFrame()->RadioButton2->close();
00062   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
00063   mainFrame()->RadioButton3->close();
00064 
00065   // Create first group
00066   myGrp1 = new DlgRef_1Sel2Spin1View1Check( centralWidget() );
00067   myGrp1->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
00068   myGrp1->TextLabel1->setText( tr( "GEOM_MAIN_OBJECT" ) );
00069   myGrp1->PushButton1->setIcon( imageS );
00070   myGrp1->LineEdit1->setReadOnly( true );
00071   myGrp1->TextLabel2->setText( tr( "NB_FACES_MIN" ) );
00072   myGrp1->TextLabel3->setText( tr( "NB_FACES_MAX" ) );
00073   myGrp1->CheckBox1->setText( tr( "GEOM_SUBSHAPE_SELECT" ) );
00074 
00075   // Add groups to layout
00076   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
00077   layout->setMargin( 0 ); layout->setSpacing( 6 );
00078   layout->addWidget( myGrp1 );
00079   /***************************************************************/
00080 
00081   setHelpFileName( "explode_on_blocks_operation_page.html" );
00082 
00083   Init();
00084 }
00085 
00086 //=================================================================================
00087 // function : ~BlocksGUI_ExplodeDlg()
00088 // purpose  : Destroys the object and frees any allocated resources
00089 //=================================================================================
00090 BlocksGUI_ExplodeDlg::~BlocksGUI_ExplodeDlg()
00091 {
00092   // no need to delete child widgets, Qt does it all for us
00093   clearTemporary();
00094 }
00095 
00096 //=================================================================================
00097 // function : Init()
00098 // purpose  :
00099 //=================================================================================
00100 void BlocksGUI_ExplodeDlg::Init()
00101 {
00102   mainFrame()->GroupBoxName->hide();
00103 
00104   // Set range of spinboxes
00105   int SpecificStep = 1;
00106   initSpinBox( myGrp1->SpinBox1, 0, 999, SpecificStep );
00107   initSpinBox( myGrp1->SpinBox2, 0, 999, SpecificStep );
00108 
00109   if ( SUIT_Session::session()->activeApplication()->desktop()->activeWindow()->getViewManager()->getType() 
00110        != OCCViewer_Viewer::Type() )
00111     myGrp1->CheckBox1->setEnabled( false );
00112 
00113   // signals and slots connections
00114   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
00115   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
00116 
00117   connect( myGrp1->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
00118 
00119   connect( myGrp1->SpinBox1, SIGNAL( valueChanged( int ) ), this, SLOT( ValueChangedInSpinBox( int ) ) );
00120   connect( myGrp1->SpinBox2, SIGNAL( valueChanged( int ) ), this, SLOT( ValueChangedInSpinBox( int ) ) );
00121 
00122   connect( myGrp1->CheckBox1, SIGNAL( stateChanged( int ) ), this, SLOT( SubShapeToggled() ) );
00123 
00124   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
00125            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
00126 
00127   myConstructorId = -1;
00128   ConstructorsClicked( 0 );
00129 }
00130 
00131 //=================================================================================
00132 // function : ConstructorsClicked()
00133 // purpose  : Radio button management
00134 //=================================================================================
00135 void BlocksGUI_ExplodeDlg::ConstructorsClicked( int constructorId )
00136 {
00137   if ( myConstructorId == constructorId )
00138     return;
00139 
00140   myConstructorId = constructorId;
00141 
00142   switch ( constructorId ) {
00143   case 0:
00144     myGrp1->show();
00145     myGrp1->SpinBox1->setValue( 6 );
00146     myGrp1->SpinBox2->setValue( 6 );
00147     myGrp1->CheckBox1->setChecked( false );
00148     break;
00149   default:
00150     break;
00151   }
00152 
00153   // init fields
00154   myEditCurrentArgument = myGrp1->LineEdit1;
00155   myObject = GEOM::GEOM_Object::_nil();
00156 
00157   activateSelection();
00158 }
00159 
00160 //=================================================================================
00161 // function : ClickOnOk()
00162 // purpose  :
00163 //=================================================================================
00164 void BlocksGUI_ExplodeDlg::ClickOnOk()
00165 {
00166   setIsApplyAndClose( true );
00167   if ( ClickOnApply() )
00168     ClickOnCancel();
00169 }
00170 
00171 //=================================================================================
00172 // function : ClickOnApply()
00173 // purpose  :
00174 //=================================================================================
00175 bool BlocksGUI_ExplodeDlg::ClickOnApply()
00176 {
00177   SUIT_Session::session()->activeApplication()->putInfo( tr( "" ) );
00178 
00179   // Explode all sub-shapes
00180   if ( isAllSubShapes() ) {
00181     // More than 30 sub-shapes : ask confirmation
00182     if ( myNbBlocks > 30 ) {
00183       if ( SUIT_MessageBox::warning( this, 
00184                                      tr( "GEOM_CONFIRM" ),
00185                                      tr( "GEOM_CONFIRM_INFO" ).arg( myNbBlocks ),
00186                                      tr( "GEOM_BUT_EXPLODE" ),
00187                                      tr( "GEOM_BUT_CANCEL" ) ) != 0 )
00188         return false;  /* aborted */
00189     }
00190   }
00191 
00192   if ( !onAccept() )
00193     return false;
00194 
00195   activateSelection();
00196 
00197   return true;
00198 }
00199 
00200 //=================================================================================
00201 // function : SelectionIntoArgument()
00202 // purpose  : Called when selection has changed
00203 //=================================================================================
00204 void BlocksGUI_ExplodeDlg::SelectionIntoArgument()
00205 {
00206   if (!isAllSubShapes())
00207     return;
00208 
00209   myObject = GEOM::GEOM_Object::_nil();
00210   myGrp1->LineEdit1->setText("");
00211 
00212   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
00213   SALOME_ListIO aSelList;
00214   aSelMgr->selectedObjects(aSelList);
00215 
00216   if (aSelList.Extent() == 1) {
00217     GEOM::GEOM_Object_var anObj = GEOMBase::ConvertIOinGEOMObject( aSelList.First() );
00218 
00219     if ( GEOMBase::IsShape(anObj) ) {
00220       myObject = anObj;
00221       myGrp1->LineEdit1->setText(GEOMBase::GetName(anObj));
00222     }
00223   }
00224 
00225   updateButtonState();
00226 }
00227 
00228 //=================================================================================
00229 // function : SetEditCurrentArgument()
00230 // purpose  :
00231 //=================================================================================
00232 void BlocksGUI_ExplodeDlg::SetEditCurrentArgument()
00233 {
00234   QPushButton* aSender = (QPushButton*)sender();
00235 
00236   if ( myGrp1->PushButton1 == aSender ) {
00237     myGrp1->LineEdit1->setFocus();
00238     myEditCurrentArgument = myGrp1->LineEdit1;
00239     myGrp1->CheckBox1->setChecked( false );
00240   }
00241 
00242   activateSelection();
00243 }
00244 
00245 //=================================================================================
00246 // function : ActivateThisDialog()
00247 // purpose  :
00248 //=================================================================================
00249 void BlocksGUI_ExplodeDlg::ActivateThisDialog()
00250 {
00251   GEOMBase_Skeleton::ActivateThisDialog();
00252   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 
00253            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
00254 
00255   activateSelection();
00256 }
00257 
00258 //=================================================================================
00259 // function : enterEvent()
00260 // purpose  :
00261 //=================================================================================
00262 void BlocksGUI_ExplodeDlg::enterEvent( QEvent* )
00263 {
00264   if ( !mainFrame()->GroupConstructors->isEnabled() )
00265     this->ActivateThisDialog();
00266 }
00267 
00268 //=================================================================================
00269 // function : ValueChangedInSpinBox()
00270 // purpose  :
00271 //=================================================================================
00272 void BlocksGUI_ExplodeDlg::ValueChangedInSpinBox()
00273 {
00274   if ( !isAllSubShapes() )
00275     activateSelection();
00276   else
00277     updateButtonState();
00278 }
00279 
00280 //=================================================================================
00281 // function : SubShapeToggled()
00282 // purpose  : Allow user selection of all or only selected sub-shapes
00283 //          : Called when 'myGrp1->CheckBox1' state change
00284 //=================================================================================
00285 void BlocksGUI_ExplodeDlg::SubShapeToggled()
00286 {
00287   activateSelection();
00288 }
00289 
00290 //=================================================================================
00291 // function : activateSelection
00292 // purpose  : Redisplay preview and Activate selection
00293 //=================================================================================
00294 void BlocksGUI_ExplodeDlg::activateSelection()
00295 {
00296   clearTemporary();
00297   erasePreview( true );
00298 
00299   if ( isAllSubShapes() ) { // Sub-shapes selection disabled
00300     disconnect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
00301                 SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
00302     globalSelection( GEOM_ALLSHAPES );
00303     if ( myObject->_is_nil() ) {
00304       SelectionIntoArgument();
00305     }
00306     connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
00307              SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
00308   } 
00309   else {
00310     displayPreview( true, true, true, false );
00311     globalSelection( GEOM_PREVIEW );
00312   }
00313 }
00314 
00315 //=================================================================================
00316 // function : updateButtonState
00317 // purpose  :
00318 //=================================================================================
00319 void BlocksGUI_ExplodeDlg::updateButtonState()
00320 {
00321   if ( SUIT_Session::session()->activeApplication()->desktop()->activeWindow()->getViewManager()->getType() 
00322        != OCCViewer_Viewer::Type() || myObject->_is_nil() ) {
00323     myGrp1->CheckBox1->setChecked( false );
00324     myGrp1->CheckBox1->setEnabled( false );
00325   } 
00326   else {
00327     myGrp1->CheckBox1->setEnabled( true );
00328   }
00329 
00330   myNbBlocks = 0;
00331 
00332   if ( myObject->_is_nil() ) {
00333     myGrp1->TextBrowser1->setText( "" );
00334   } 
00335   else {
00336     GEOM::GEOM_IBlocksOperations_var anOper = GEOM::GEOM_IBlocksOperations::_narrow(getOperation());
00337     bool isOnlyBlocks = anOper->IsCompoundOfBlocks( myObject,
00338                                                     myGrp1->SpinBox1->value(),
00339                                                     myGrp1->SpinBox2->value(),
00340                                                     myNbBlocks );
00341     if ( isOnlyBlocks )
00342       myGrp1->TextBrowser1->setText( tr( "GEOM_NB_BLOCKS_NO_OTHERS" ).arg( myNbBlocks ) );
00343     else
00344       myGrp1->TextBrowser1->setText( tr( "GEOM_NB_BLOCKS_SOME_OTHERS" ).arg( myNbBlocks ) );
00345   }
00346 }
00347 
00348 //=================================================================================
00349 // function : isAllSubShapes
00350 // purpose  :
00351 //=================================================================================
00352 bool BlocksGUI_ExplodeDlg::isAllSubShapes() const
00353 {
00354   return !myGrp1->CheckBox1->isChecked() || !myGrp1->CheckBox1->isEnabled();
00355 }
00356 
00357 //=================================================================================
00358 // function : createOperation
00359 // purpose  :
00360 //=================================================================================
00361 GEOM::GEOM_IOperations_ptr BlocksGUI_ExplodeDlg::createOperation()
00362 {
00363   return getGeomEngine()->GetIBlocksOperations( getStudyId() );
00364 }
00365 
00366 //=================================================================================
00367 // function : isValid()
00368 // purpose  : Verify validity of input data
00369 //=================================================================================
00370 bool BlocksGUI_ExplodeDlg::isValid (QString& msg)
00371 {
00372   bool okSP = true;
00373   okSP = myGrp1->SpinBox1->isValid( msg, !IsPreview() ) && okSP;
00374   okSP = myGrp1->SpinBox2->isValid( msg, !IsPreview() ) && okSP;
00375 
00376   bool ok = false;
00377   switch ( getConstructorId() ) {
00378   case 0:
00379     if ( IsPreview() ) {
00380       ok = !myObject->_is_nil();
00381     }
00382     else {
00383       LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
00384       SALOME_ListIO aSelList;
00385       aSelMgr->selectedObjects(aSelList);
00386 
00387       ok = !myObject->_is_nil() && (isAllSubShapes() || aSelList.Extent());
00388     }
00389     break;
00390   default:
00391     break;
00392   }
00393 
00394   return ok && okSP;
00395 }
00396 
00397 //=================================================================================
00398 // function : execute
00399 // purpose  :
00400 //=================================================================================
00401 bool BlocksGUI_ExplodeDlg::execute( ObjectList& objects )
00402 {
00403   GEOM::ListOfGO_var aList;
00404 
00405   GEOM::GEOM_IBlocksOperations_var anOper = GEOM::GEOM_IBlocksOperations::_narrow(getOperation());
00406 
00407   switch ( getConstructorId() ) {
00408   case 0:
00409     aList = anOper->ExplodeCompoundOfBlocks( myObject,
00410                                              myGrp1->SpinBox1->value(),
00411                                              myGrp1->SpinBox2->value() );
00412     break;
00413   }
00414   
00415   if ( !aList->length() )
00416     return false;
00417 
00418   if ( IsPreview() ) {
00419     clearTemporary();
00420     
00421     // Store objects. They will be put in study when "Apply" is pressed
00422     for ( int i = 0, n = aList->length(); i < n; i++ ) {
00423       objects.push_back( GEOM::GEOM_Object::_duplicate( aList[i] ) );
00424       myTmpObjs.push_back( GEOM::GEOM_Object::_duplicate( aList[i] ) );
00425     }
00426 
00427     return objects.size() > 0;
00428   }
00429 
00430   QStringList aParameters;
00431   aParameters << myGrp1->SpinBox1->text();
00432   aParameters << myGrp1->SpinBox2->text();
00433 
00434   // Throw away sub-shapes not selected by user if not in preview mode
00435   // and manual selection is active
00436   if ( !isAllSubShapes() ) {
00437     QMap<QString, char> selected;
00438 
00439     LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
00440     SALOME_ListIO aSelList;
00441     aSelMgr->selectedObjects(aSelList);
00442 
00443     // Get names of selected objects
00444     SALOME_ListIteratorOfListIO it (aSelList);
00445     for ( ; it.More(); it.Next() ) {
00446       selected.insert( it.Value()->getName(), 0 );
00447     }
00448 
00449     // Iterate through result and select objects with names from selection
00450     ObjectList toRemoveFromEngine;
00451     ObjectList::iterator anIter;
00452     for ( anIter = myTmpObjs.begin(); anIter != myTmpObjs.end(); ++anIter ) {
00453       CORBA::String_var objStr = myGeomGUI->getApp()->orb()->object_to_string( *anIter );
00454       if ( selected.contains( QString( objStr.in() ) ) )
00455       {
00456         if ( !IsPreview() )
00457           (*anIter)->SetParameters(aParameters.join(":").toLatin1().constData());
00458         objects.push_back( *anIter );
00459       }
00460       else
00461         toRemoveFromEngine.push_back( *anIter );
00462     }
00463 
00464     // Remove from engine useless objects
00465     ObjectList::iterator anIter2 = toRemoveFromEngine.begin();
00466     for ( ; anIter2 != toRemoveFromEngine.end(); ++anIter2 )
00467       getGeomEngine()->RemoveObject( *anIter2 );
00468 
00469     myTmpObjs.clear();
00470 
00471   }
00472   else {
00473     for ( int i = 0, n = aList->length(); i < n; i++ )
00474     {
00475       GEOM::GEOM_Object_var anObj = GEOM::GEOM_Object::_duplicate( aList[i] );
00476       if ( !IsPreview() )
00477         anObj->SetParameters(aParameters.join(":").toLatin1().constData());
00478       objects.push_back( anObj._retn() );
00479     }
00480   }
00481 
00482   return objects.size();
00483 }
00484 
00485 //=================================================================================
00486 // function : clearTemporary
00487 // purpose  : Remove temporary objects from engine
00488 //=================================================================================
00489 void BlocksGUI_ExplodeDlg::clearTemporary()
00490 {
00491   ObjectList::iterator anIter;
00492   for ( anIter = myTmpObjs.begin(); anIter != myTmpObjs.end(); ++anIter )
00493     getGeomEngine()->RemoveObject( *anIter );
00494 
00495   myTmpObjs.clear();
00496 }
00497 
00498 //================================================================
00499 // Function : getFather
00500 // Purpose  : Get father object for object to be added in study
00501 //            ( called with addInStudy method )
00502 //================================================================
00503 GEOM::GEOM_Object_ptr BlocksGUI_ExplodeDlg::getFather( GEOM::GEOM_Object_ptr )
00504 {
00505   return myObject;
00506 }
00507 
00508 //================================================================
00509 // Function : getNewObjectName
00510 // Purpose  : Redefine this method to return proper name for a new object
00511 //================================================================
00512 QString BlocksGUI_ExplodeDlg::getNewObjectName() const
00513 {
00514   return QString::null;
00515 }