1 // Copyright (C) 2007-2008 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
22 // GEOM GEOMGUI : GUI for Geometry component
23 // File : EntityGUI_SubShapeDlg.cxx
24 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
26 #include "EntityGUI_SubShapeDlg.h"
29 #include <GeometryGUI.h>
32 #include <SUIT_Desktop.h>
33 #include <SUIT_ResourceMgr.h>
34 #include <SUIT_Session.h>
35 #include <SUIT_ViewManager.h>
36 #include <SUIT_ViewWindow.h>
37 #include <OCCViewer_ViewModel.h>
38 #include <SalomeApp_Application.h>
39 #include <LightApp_SelectionMgr.h>
41 #include <TColStd_IndexedMapOfInteger.hxx>
42 #include <TopoDS_Iterator.hxx>
43 #include <TopExp_Explorer.hxx>
44 #include <TopTools_MapOfShape.hxx>
46 #include <QMessageBox>
48 //=================================================================================
49 // class : EntityGUI_SubShapeDlg
50 // purpose : Constructs a EntityGUI_SubShapeDlg which is a child of 'parent', with the
51 // name 'name' and widget flags set to 'f'.
52 // The dialog will by default be modeless, unless you set 'modal' to
53 // TRUE to construct a modal dialog.
54 //=================================================================================
55 EntityGUI_SubShapeDlg::EntityGUI_SubShapeDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
56 bool modal, Qt::WindowFlags fl )
57 : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
59 QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_SUBSHAPE" ) ) );
60 QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
62 setWindowTitle( tr( "GEOM_SUBSHAPE_TITLE" ) );
64 /***************************************************************/
65 mainFrame()->GroupConstructors->setTitle( tr( "GEOM_SUB_SHAPE" ) );
66 mainFrame()->RadioButton1->setIcon( image0 );
67 mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
68 mainFrame()->RadioButton2->close();
69 mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
70 mainFrame()->RadioButton3->close();
72 GroupPoints = new DlgRef_1Sel1Check1List( centralWidget() );
74 GroupPoints->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
75 GroupPoints->TextLabel1->setText( tr( "GEOM_MAIN_OBJECT" ) );
76 GroupPoints->TextLabel2->setText( tr( "GEOM_SUBSHAPE_TYPE" ) );
77 GroupPoints->CheckButton1->setText( tr( "GEOM_SUBSHAPE_SELECT" ) );
78 GroupPoints->PushButton1->setIcon( image1 );
79 GroupPoints->LineEdit1->setReadOnly( true );
81 QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
82 layout->setMargin( 0 ); layout->setSpacing( 6 );
83 layout->addWidget( GroupPoints );
84 /***************************************************************/
86 setHelpFileName( "create_explode_page.html" );
88 mainFrame()->GroupBoxName->hide();
94 //=================================================================================
95 // function : ~EntityGUI_SubShapeDlg()
96 // purpose : Destroys the object and frees any allocated resources
97 //=================================================================================
98 EntityGUI_SubShapeDlg::~EntityGUI_SubShapeDlg()
103 //=================================================================================
106 //=================================================================================
107 void EntityGUI_SubShapeDlg::Init()
110 myEditCurrentArgument = GroupPoints->LineEdit1;
111 myObject = GEOM::GEOM_Object::_nil();
115 /* type for sub shape selection */
116 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Compound" );
117 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Compsolid" );
118 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Solid" );
119 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Shell" );
120 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Face" );
121 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Wire" );
122 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Edge" );
123 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Vertex" );
124 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Shape" );
126 if ( SUIT_Session::session()->activeApplication()->desktop()->activeWindow()->getViewManager()->getType()
127 != OCCViewer_Viewer::Type() )
128 GroupPoints->CheckButton1->setEnabled( false );
130 /* signals and slots connections */
131 connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
133 connect( buttonOk(), SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
134 connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
136 connect( GroupPoints->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
137 connect( GroupPoints->LineEdit1, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
139 connect( GroupPoints->ComboBox1, SIGNAL( activated( int ) ), this, SLOT( ComboTextChanged() ) );
140 connect( GroupPoints->CheckButton1, SIGNAL( stateChanged( int ) ), this, SLOT( SubShapeToggled() ) );
142 connect( myGeomGUI->getApp()->selectionMgr(),
143 SIGNAL( currentSelectionChanged( )), this, SLOT( SelectionIntoArgument() ) );
147 SelectionIntoArgument();
151 //=================================================================================
152 // function : ClickOnOk()
154 //=================================================================================
155 void EntityGUI_SubShapeDlg::ClickOnOk()
157 if ( ClickOnApply() )
162 //=================================================================================
163 // function : ClickOnApply()
165 //=================================================================================
166 bool EntityGUI_SubShapeDlg::ClickOnApply()
168 SUIT_Session::session()->activeApplication()->putInfo( "" );
170 /* Explode all sub shapes */
171 if ( isAllSubShapes() ) {
172 /* More than 30 subshapes : ask confirmation */
173 unsigned int nb = NumberOfSubShapes( myShape, shapeType() );
175 const QString caption = tr( "GEOM_CONFIRM" );
176 const QString text = tr( "GEOM_CONFIRM_INFO" ).arg( nb );
177 const QString button0 = tr( "GEOM_BUT_EXPLODE" );
178 const QString button1 = tr( "GEOM_BUT_CANCEL" );
180 if ( QMessageBox::warning( this, caption, text, button0, button1 ) != 0 )
181 return false; /* aborted */
185 bool isOk = onAccept();
187 // restore selection, corresponding to current selection mode
194 //=================================================================================
195 // function : SelectionIntoArgument()
196 // purpose : Called when selection as changed or other case
197 // : used only by SelectButtonC1A1 (LineEditC1A1)
198 //=================================================================================
199 void EntityGUI_SubShapeDlg::SelectionIntoArgument()
201 if (!isAllSubShapes())
204 ResetStateOfDialog();
206 QString aString = ""; /* name of selection */
208 LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
209 SALOME_ListIO aSelList;
210 aSelMgr->selectedObjects(aSelList);
212 int nbSel = GEOMBase::GetNameOfSelectedIObjects(aSelList, aString, true);
217 Handle(SALOME_InteractiveObject) IO = aSelList.First();
218 if ( !IO->hasEntry() ) {
219 SUIT_Session::session()->activeApplication()->putInfo( tr( "GEOM_PRP_SHAPE_IN_STUDY" ) );
224 if (!GEOMBase::GetTopoFromSelection(aSelList, S) ||
226 S.ShapeType() == TopAbs_VERTEX) {
227 myObject = GEOM::GEOM_Object::_nil();
232 Standard_Boolean testResult;
233 myObject = GEOMBase::ConvertIOinGEOMObject(IO, testResult);
234 if (!testResult || myObject->_is_nil()) {
240 GroupPoints->LineEdit1->setText(aString);
242 int SelectedShapeType = GroupPoints->ComboBox1->currentIndex();
243 int count = GroupPoints->ComboBox1->count();
250 if (myShape.ShapeType() == TopAbs_COMPOUND) {
251 unsigned int nb = NumberOfSubShapes(myShape, TopAbs_COMPOUND);
255 while (i <= myShape.ShapeType()) {
256 GroupPoints->ComboBox1->removeItem(0);
260 if (myShape.ShapeType() == TopAbs_COMPOUND) {
261 if (myWithShape == false) {
262 GroupPoints->ComboBox1->insertItem(GroupPoints->ComboBox1->count(), "Shape");
267 if ( myWithShape == true ) {
268 GroupPoints->ComboBox1->removeItem( GroupPoints->ComboBox1->count() - 1 );
273 int count1 = GroupPoints->ComboBox1->count();
277 if ( SelectedShapeType > myShape.ShapeType() ) {
278 if ( SelectedShapeType == 8 ) {
279 if ( myShape.ShapeType() != TopAbs_COMPOUND ) {
280 GroupPoints->ComboBox1->setCurrentIndex( 0 );
285 GroupPoints->ComboBox1->setCurrentIndex( count1 - count + SelectedShapeType );
288 GroupPoints->ComboBox1->setCurrentIndex( 0 );
296 //=================================================================================
297 // function : SetEditCurrentArgument()
299 //=================================================================================
300 void EntityGUI_SubShapeDlg::SetEditCurrentArgument()
302 GroupPoints->LineEdit1->setFocus();
303 myEditCurrentArgument = GroupPoints->LineEdit1;
305 GroupPoints->CheckButton1->setChecked( false );
307 SelectionIntoArgument();
311 //=================================================================================
312 // function : LineEditReturnPressed()
314 //=================================================================================
315 void EntityGUI_SubShapeDlg::LineEditReturnPressed()
317 QLineEdit* send = (QLineEdit*)sender();
318 if ( send == GroupPoints->LineEdit1 )
319 SetEditCurrentArgument();
323 GEOMBase_Skeleton::LineEditReturnPressed();
327 //=================================================================================
328 // function : DeactivateActiveDialog()
330 //=================================================================================
331 void EntityGUI_SubShapeDlg::DeactivateActiveDialog()
333 if ( mainFrame()->GroupConstructors->isEnabled() ) {
334 GEOMBase_Skeleton::DeactivateActiveDialog();
339 //=================================================================================
340 // function : ActivateThisDialog()
342 //=================================================================================
343 void EntityGUI_SubShapeDlg::ActivateThisDialog()
345 GEOMBase_Skeleton::ActivateThisDialog();
346 connect( myGeomGUI->getApp()->selectionMgr(),
347 SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
353 //=================================================================================
354 // function : enterEvent()
356 //=================================================================================
357 void EntityGUI_SubShapeDlg::enterEvent( QEvent* )
359 if ( !mainFrame()->GroupConstructors->isEnabled() )
360 ActivateThisDialog();
363 //=================================================================================
364 // function : ResetStateOfDialog()
365 // purpose : Completely reset the state of method including local context
366 //=================================================================================
367 void EntityGUI_SubShapeDlg::ResetStateOfDialog()
369 myObject = GEOM::GEOM_Object::_nil();
371 myEditCurrentArgument->setText( "" );
373 int SelectedShapeType = GroupPoints->ComboBox1->currentIndex();
374 int count = GroupPoints->ComboBox1->count();
378 /* type for sub shape selection */
379 GroupPoints->ComboBox1->clear();
380 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Compound" );
381 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Compsolid" );
382 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Solid" );
383 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Shell" );
384 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Face" );
385 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Wire" );
386 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Edge" );
387 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Vertex" );
388 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Shape" );
392 GroupPoints->ComboBox1->setCurrentIndex( 8 - count + SelectedShapeType );
399 //=================================================================================
400 // function : SubShapeToggled()
401 // purpose : Allow user selection of all or only selected sub shapes
402 // : Called when 'CheckButton1' state change
403 //=================================================================================
404 void EntityGUI_SubShapeDlg::SubShapeToggled()
406 globalSelection( GEOM_ALLSHAPES );
408 if ( !isAllSubShapes() )
409 localSelection( myObject, shapeType() );
413 //=================================================================================
414 // function : ComboTextChanged()
416 //=================================================================================
417 void EntityGUI_SubShapeDlg::ComboTextChanged()
419 /* Select sub shapes mode not checked */
425 //=================================================================================
426 // function : NumberOfSubShapes()
428 //=================================================================================
429 unsigned int EntityGUI_SubShapeDlg::NumberOfSubShapes( const TopoDS_Shape& S,
430 const int shapeType ) const
435 unsigned int index = 0;
436 TopTools_MapOfShape M;
438 if ( S.ShapeType() == TopAbs_COMPOUND &&
439 ( TopAbs_ShapeEnum(shapeType) == TopAbs_SHAPE ||
440 TopAbs_ShapeEnum(shapeType) == TopAbs_COMPSOLID ||
441 TopAbs_ShapeEnum(shapeType) == TopAbs_COMPOUND ) ) {
442 TopoDS_Iterator It( S, Standard_True, Standard_True );
443 for ( ; It.More(); It.Next() ) {
444 if ( M.Add( It.Value() ) ) {
445 if ( TopAbs_ShapeEnum( shapeType ) == TopAbs_SHAPE ||
446 TopAbs_ShapeEnum( shapeType ) == It.Value().ShapeType() ) {
453 TopExp_Explorer Exp ( S, TopAbs_ShapeEnum( shapeType ) );
454 for ( ; Exp.More(); Exp.Next() ) {
455 if ( M.Add( Exp.Current() ) ) {
465 //=================================================================================
466 // function : updateButtonState
468 //=================================================================================
469 void EntityGUI_SubShapeDlg::updateButtonState()
471 if ( SUIT_Session::session()->activeApplication()->desktop()->activeWindow()->getViewManager()->getType() != OCCViewer_Viewer::Type() ||
472 myObject->_is_nil() || shapeType() == TopAbs_SHAPE || shapeType() == TopAbs_COMPOUND ) {
473 GroupPoints->CheckButton1->setChecked( false );
474 GroupPoints->CheckButton1->setEnabled( false );
477 GroupPoints->CheckButton1->setEnabled( true );
480 //=================================================================================
481 // function : isAllSubShapes
483 //=================================================================================
484 bool EntityGUI_SubShapeDlg::isAllSubShapes() const
486 return !GroupPoints->CheckButton1->isChecked() || !GroupPoints->CheckButton1->isEnabled();
489 //=================================================================================
490 // function : shapeType
492 //=================================================================================
493 int EntityGUI_SubShapeDlg::shapeType() const
495 int type = GroupPoints->ComboBox1->currentIndex();
497 if ( myObject->_is_nil() )
501 type += myShape.ShapeType() + 1;
502 if ( myShape.ShapeType() == TopAbs_COMPOUND &&
503 NumberOfSubShapes( myShape, TopAbs_COMPOUND ) > 0 ) {
510 //=================================================================================
511 // function : createOperation
513 //=================================================================================
514 GEOM::GEOM_IOperations_ptr EntityGUI_SubShapeDlg::createOperation()
516 return getGeomEngine()->GetIShapesOperations( getStudyId() );
519 //=================================================================================
520 // function : isValid
522 //=================================================================================
523 bool EntityGUI_SubShapeDlg::isValid (QString& msg)
527 if (myObject->_is_nil()) {
532 if (isAllSubShapes())
535 LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
536 SALOME_ListIO aSelList;
537 aSelMgr->selectedObjects(aSelList);
539 if (aSelList.Extent() == 1) {
540 Standard_Boolean testResult;
541 GEOM::GEOM_Object_var anObj = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), testResult);
542 if (testResult && !anObj->_is_nil()) {
543 TColStd_IndexedMapOfInteger aMapIndex;
544 aSelMgr->GetIndexes(aSelList.First(), aMapIndex);
545 isOk = aMapIndex.Extent() > 0;
547 msg += tr("NO_SUBSHAPES_SELECTED");
555 //=================================================================================
556 // function : execute
558 //=================================================================================
559 bool EntityGUI_SubShapeDlg::execute (ObjectList& objects)
561 GEOM::ListOfGO_var aList = GEOM::GEOM_IShapesOperations::_narrow(getOperation())->
562 MakeExplode(myObject, shapeType(), true);
564 if (!aList->length())
567 // Throw away sub-shapes not selected by user if not in preview mode
568 // and manual selection is active
569 if (!isAllSubShapes()) {
570 LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
571 SALOME_ListIO aSelList;
572 aSelMgr->selectedObjects(aSelList);
574 if (aSelList.Extent() == 1) {
575 Standard_Boolean aResult = Standard_False;
576 GEOM::GEOM_Object_var anObj =
577 GEOMBase::ConvertIOinGEOMObject(aSelList.First(), aResult);
579 if (aResult && !anObj->_is_nil()) {
580 TColStd_IndexedMapOfInteger aMapIndex;
581 aSelMgr->GetIndexes(aSelList.First(), aMapIndex);
583 GEOM::GEOM_ILocalOperations_var aLocOp =
584 getGeomEngine()->GetILocalOperations(getStudyId());
586 for (int i = 0, n = aList->length(); i < n; i++)
587 if (aMapIndex.Contains(aLocOp->GetSubShapeIndex(myObject, aList[i])))
588 objects.push_back(GEOM::GEOM_Object::_duplicate(aList[i]));
593 for (int i = 0, n = aList->length(); i < n; i++)
594 objects.push_back(GEOM::GEOM_Object::_duplicate(aList[i]));
596 return objects.size();
599 //================================================================
600 // Function : getFather
601 // Purpose : Get father object for object to be added in study
602 // ( called with addInStudy method )
603 //================================================================
604 GEOM::GEOM_Object_ptr EntityGUI_SubShapeDlg::getFather( GEOM::GEOM_Object_ptr )
609 QString EntityGUI_SubShapeDlg::getNewObjectName() const
611 return QString::null;