1 // Copyright (C) 2007-2010 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
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File : EntityGUI_SubShapeDlg.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
27 #include "EntityGUI_SubShapeDlg.h"
30 #include <GeometryGUI.h>
33 #include <SUIT_Desktop.h>
34 #include <SUIT_ResourceMgr.h>
35 #include <SUIT_Session.h>
36 #include <SUIT_ViewManager.h>
37 #include <SUIT_ViewWindow.h>
38 #include <OCCViewer_ViewModel.h>
39 #include <SalomeApp_Application.h>
40 #include <LightApp_SelectionMgr.h>
42 #include <TColStd_IndexedMapOfInteger.hxx>
43 #include <TopoDS_Iterator.hxx>
44 #include <TopExp_Explorer.hxx>
45 #include <TopTools_MapOfShape.hxx>
47 #include <QMessageBox>
49 //=================================================================================
50 // class : EntityGUI_SubShapeDlg
51 // purpose : Constructs a EntityGUI_SubShapeDlg which is a child of 'parent', with the
52 // name 'name' and widget flags set to 'f'.
53 // The dialog will by default be modeless, unless you set 'modal' to
54 // TRUE to construct a modal dialog.
55 //=================================================================================
56 EntityGUI_SubShapeDlg::EntityGUI_SubShapeDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
57 bool modal, Qt::WindowFlags fl )
58 : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
60 QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_SUBSHAPE" ) ) );
61 QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
63 setWindowTitle( tr( "GEOM_SUBSHAPE_TITLE" ) );
65 /***************************************************************/
66 mainFrame()->GroupConstructors->setTitle( tr( "GEOM_SUB_SHAPE" ) );
67 mainFrame()->RadioButton1->setIcon( image0 );
68 mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
69 mainFrame()->RadioButton2->close();
70 mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
71 mainFrame()->RadioButton3->close();
73 GroupPoints = new DlgRef_1Sel1Check1List( centralWidget() );
75 GroupPoints->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
76 GroupPoints->TextLabel1->setText( tr( "GEOM_MAIN_OBJECT" ) );
77 GroupPoints->TextLabel2->setText( tr( "GEOM_SUBSHAPE_TYPE" ) );
78 GroupPoints->CheckButton1->setText( tr( "GEOM_SUBSHAPE_SELECT" ) );
79 GroupPoints->PushButton1->setIcon( image1 );
80 GroupPoints->LineEdit1->setReadOnly( true );
82 QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
83 layout->setMargin( 0 ); layout->setSpacing( 6 );
84 layout->addWidget( GroupPoints );
85 /***************************************************************/
87 setHelpFileName( "create_explode_page.html" );
89 mainFrame()->GroupBoxName->hide();
95 //=================================================================================
96 // function : ~EntityGUI_SubShapeDlg()
97 // purpose : Destroys the object and frees any allocated resources
98 //=================================================================================
99 EntityGUI_SubShapeDlg::~EntityGUI_SubShapeDlg()
104 //=================================================================================
107 //=================================================================================
108 void EntityGUI_SubShapeDlg::Init()
111 myEditCurrentArgument = GroupPoints->LineEdit1;
112 myObject = GEOM::GEOM_Object::_nil();
116 /* type for sub shape selection */
117 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Compound" );
118 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Compsolid" );
119 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Solid" );
120 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Shell" );
121 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Face" );
122 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Wire" );
123 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Edge" );
124 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Vertex" );
125 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Shape" );
127 if ( SUIT_Session::session()->activeApplication()->desktop()->activeWindow()->getViewManager()->getType()
128 != OCCViewer_Viewer::Type() )
129 GroupPoints->CheckButton1->setEnabled( false );
131 /* signals and slots connections */
132 connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
134 connect( buttonOk(), SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
135 connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
137 connect( GroupPoints->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
138 connect( GroupPoints->LineEdit1, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
140 connect( GroupPoints->ComboBox1, SIGNAL( activated( int ) ), this, SLOT( ComboTextChanged() ) );
141 connect( GroupPoints->CheckButton1, SIGNAL( stateChanged( int ) ), this, SLOT( SubShapeToggled() ) );
143 connect( myGeomGUI->getApp()->selectionMgr(),
144 SIGNAL( currentSelectionChanged( )), this, SLOT( SelectionIntoArgument() ) );
148 SelectionIntoArgument();
152 //=================================================================================
153 // function : ClickOnOk()
155 //=================================================================================
156 void EntityGUI_SubShapeDlg::ClickOnOk()
158 if ( ClickOnApply() )
163 //=================================================================================
164 // function : ClickOnApply()
166 //=================================================================================
167 bool EntityGUI_SubShapeDlg::ClickOnApply()
169 SUIT_Session::session()->activeApplication()->putInfo( "" );
171 /* Explode all sub shapes */
172 if ( isAllSubShapes() ) {
173 /* More than 30 subshapes : ask confirmation */
174 unsigned int nb = NumberOfSubShapes( myShape, shapeType() );
176 const QString caption = tr( "GEOM_CONFIRM" );
177 const QString text = tr( "GEOM_CONFIRM_INFO" ).arg( nb );
178 const QString button0 = tr( "GEOM_BUT_EXPLODE" );
179 const QString button1 = tr( "GEOM_BUT_CANCEL" );
181 if ( QMessageBox::warning( this, caption, text, button0, button1 ) != 0 )
182 return false; /* aborted */
186 bool isOk = onAccept();
188 // restore selection, corresponding to current selection mode
195 //=================================================================================
196 // function : SelectionIntoArgument()
197 // purpose : Called when selection as changed or other case
198 // : used only by SelectButtonC1A1 (LineEditC1A1)
199 //=================================================================================
200 void EntityGUI_SubShapeDlg::SelectionIntoArgument()
202 if (!isAllSubShapes())
205 ResetStateOfDialog();
207 QString aString = ""; /* name of selection */
209 LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
210 SALOME_ListIO aSelList;
211 aSelMgr->selectedObjects(aSelList);
213 int nbSel = GEOMBase::GetNameOfSelectedIObjects(aSelList, aString, true);
218 Handle(SALOME_InteractiveObject) IO = aSelList.First();
219 if ( !IO->hasEntry() ) {
220 SUIT_Session::session()->activeApplication()->putInfo( tr( "GEOM_PRP_SHAPE_IN_STUDY" ) );
225 if (!GEOMBase::GetTopoFromSelection(aSelList, S) ||
227 S.ShapeType() == TopAbs_VERTEX) {
228 myObject = GEOM::GEOM_Object::_nil();
233 Standard_Boolean testResult;
234 myObject = GEOMBase::ConvertIOinGEOMObject(IO, testResult);
235 if (!testResult || myObject->_is_nil()) {
241 GroupPoints->LineEdit1->setText(aString);
243 int SelectedShapeType = GroupPoints->ComboBox1->currentIndex();
244 int count = GroupPoints->ComboBox1->count();
251 if (myShape.ShapeType() == TopAbs_COMPOUND) {
252 unsigned int nb = NumberOfSubShapes(myShape, TopAbs_COMPOUND);
256 while (i <= myShape.ShapeType()) {
257 GroupPoints->ComboBox1->removeItem(0);
261 if (myShape.ShapeType() == TopAbs_COMPOUND) {
262 if (myWithShape == false) {
263 GroupPoints->ComboBox1->insertItem(GroupPoints->ComboBox1->count(), "Shape");
268 if ( myWithShape == true ) {
269 GroupPoints->ComboBox1->removeItem( GroupPoints->ComboBox1->count() - 1 );
274 int count1 = GroupPoints->ComboBox1->count();
278 if ( SelectedShapeType > myShape.ShapeType() ) {
279 if ( SelectedShapeType == 8 ) {
280 if ( myShape.ShapeType() != TopAbs_COMPOUND ) {
281 GroupPoints->ComboBox1->setCurrentIndex( 0 );
286 GroupPoints->ComboBox1->setCurrentIndex( count1 - count + SelectedShapeType );
289 GroupPoints->ComboBox1->setCurrentIndex( 0 );
297 //=================================================================================
298 // function : SetEditCurrentArgument()
300 //=================================================================================
301 void EntityGUI_SubShapeDlg::SetEditCurrentArgument()
303 GroupPoints->LineEdit1->setFocus();
304 myEditCurrentArgument = GroupPoints->LineEdit1;
306 GroupPoints->CheckButton1->setChecked( false );
308 SelectionIntoArgument();
312 //=================================================================================
313 // function : LineEditReturnPressed()
315 //=================================================================================
316 void EntityGUI_SubShapeDlg::LineEditReturnPressed()
318 QLineEdit* send = (QLineEdit*)sender();
319 if ( send == GroupPoints->LineEdit1 )
320 SetEditCurrentArgument();
324 GEOMBase_Skeleton::LineEditReturnPressed();
328 //=================================================================================
329 // function : DeactivateActiveDialog()
331 //=================================================================================
332 void EntityGUI_SubShapeDlg::DeactivateActiveDialog()
334 if ( mainFrame()->GroupConstructors->isEnabled() ) {
335 GEOMBase_Skeleton::DeactivateActiveDialog();
340 //=================================================================================
341 // function : ActivateThisDialog()
343 //=================================================================================
344 void EntityGUI_SubShapeDlg::ActivateThisDialog()
346 GEOMBase_Skeleton::ActivateThisDialog();
347 connect( myGeomGUI->getApp()->selectionMgr(),
348 SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
354 //=================================================================================
355 // function : enterEvent()
357 //=================================================================================
358 void EntityGUI_SubShapeDlg::enterEvent( QEvent* )
360 if ( !mainFrame()->GroupConstructors->isEnabled() )
361 ActivateThisDialog();
364 //=================================================================================
365 // function : ResetStateOfDialog()
366 // purpose : Completely reset the state of method including local context
367 //=================================================================================
368 void EntityGUI_SubShapeDlg::ResetStateOfDialog()
370 myObject = GEOM::GEOM_Object::_nil();
372 myEditCurrentArgument->setText( "" );
374 int SelectedShapeType = GroupPoints->ComboBox1->currentIndex();
375 int count = GroupPoints->ComboBox1->count();
379 /* type for sub shape selection */
380 GroupPoints->ComboBox1->clear();
381 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Compound" );
382 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Compsolid" );
383 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Solid" );
384 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Shell" );
385 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Face" );
386 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Wire" );
387 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Edge" );
388 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Vertex" );
389 GroupPoints->ComboBox1->insertItem( GroupPoints->ComboBox1->count(), "Shape" );
393 GroupPoints->ComboBox1->setCurrentIndex( 8 - count + SelectedShapeType );
400 //=================================================================================
401 // function : SubShapeToggled()
402 // purpose : Allow user selection of all or only selected sub shapes
403 // : Called when 'CheckButton1' state change
404 //=================================================================================
405 void EntityGUI_SubShapeDlg::SubShapeToggled()
407 globalSelection( GEOM_ALLSHAPES );
409 if ( !isAllSubShapes() )
410 localSelection( myObject, shapeType() );
414 //=================================================================================
415 // function : ComboTextChanged()
417 //=================================================================================
418 void EntityGUI_SubShapeDlg::ComboTextChanged()
420 /* Select sub shapes mode not checked */
426 //=================================================================================
427 // function : NumberOfSubShapes()
429 //=================================================================================
430 unsigned int EntityGUI_SubShapeDlg::NumberOfSubShapes( const TopoDS_Shape& S,
431 const int shapeType ) const
436 unsigned int index = 0;
437 TopTools_MapOfShape M;
439 if ( S.ShapeType() == TopAbs_COMPOUND &&
440 ( TopAbs_ShapeEnum(shapeType) == TopAbs_SHAPE ||
441 TopAbs_ShapeEnum(shapeType) == TopAbs_COMPSOLID ||
442 TopAbs_ShapeEnum(shapeType) == TopAbs_COMPOUND ) ) {
443 TopoDS_Iterator It( S, Standard_True, Standard_True );
444 for ( ; It.More(); It.Next() ) {
445 if ( M.Add( It.Value() ) ) {
446 if ( TopAbs_ShapeEnum( shapeType ) == TopAbs_SHAPE ||
447 TopAbs_ShapeEnum( shapeType ) == It.Value().ShapeType() ) {
454 TopExp_Explorer Exp ( S, TopAbs_ShapeEnum( shapeType ) );
455 for ( ; Exp.More(); Exp.Next() ) {
456 if ( M.Add( Exp.Current() ) ) {
466 //=================================================================================
467 // function : updateButtonState
469 //=================================================================================
470 void EntityGUI_SubShapeDlg::updateButtonState()
472 if ( SUIT_Session::session()->activeApplication()->desktop()->activeWindow()->getViewManager()->getType() != OCCViewer_Viewer::Type() ||
473 myObject->_is_nil() || shapeType() == TopAbs_SHAPE || shapeType() == TopAbs_COMPOUND ) {
474 GroupPoints->CheckButton1->setChecked( false );
475 GroupPoints->CheckButton1->setEnabled( false );
478 GroupPoints->CheckButton1->setEnabled( true );
481 //=================================================================================
482 // function : isAllSubShapes
484 //=================================================================================
485 bool EntityGUI_SubShapeDlg::isAllSubShapes() const
487 return !GroupPoints->CheckButton1->isChecked() || !GroupPoints->CheckButton1->isEnabled();
490 //=================================================================================
491 // function : shapeType
493 //=================================================================================
494 int EntityGUI_SubShapeDlg::shapeType() const
496 int type = GroupPoints->ComboBox1->currentIndex();
498 if ( myObject->_is_nil() )
502 type += myShape.ShapeType() + 1;
503 if ( myShape.ShapeType() == TopAbs_COMPOUND &&
504 NumberOfSubShapes( myShape, TopAbs_COMPOUND ) > 0 ) {
511 //=================================================================================
512 // function : createOperation
514 //=================================================================================
515 GEOM::GEOM_IOperations_ptr EntityGUI_SubShapeDlg::createOperation()
517 return getGeomEngine()->GetIShapesOperations( getStudyId() );
520 //=================================================================================
521 // function : isValid
523 //=================================================================================
524 bool EntityGUI_SubShapeDlg::isValid (QString& msg)
528 if (myObject->_is_nil()) {
533 if (isAllSubShapes())
536 LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
537 SALOME_ListIO aSelList;
538 aSelMgr->selectedObjects(aSelList);
540 if (aSelList.Extent() == 1) {
541 Standard_Boolean testResult;
542 GEOM::GEOM_Object_var anObj = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), testResult);
543 if (testResult && !anObj->_is_nil()) {
544 TColStd_IndexedMapOfInteger aMapIndex;
545 aSelMgr->GetIndexes(aSelList.First(), aMapIndex);
546 isOk = aMapIndex.Extent() > 0;
548 msg += tr("NO_SUBSHAPES_SELECTED");
556 //=================================================================================
557 // function : execute
559 //=================================================================================
560 bool EntityGUI_SubShapeDlg::execute (ObjectList& objects)
562 GEOM::GEOM_IShapesOperations_var anOper = GEOM::GEOM_IShapesOperations::_narrow(getOperation());
563 GEOM::ListOfGO_var aList = anOper->MakeExplode(myObject, shapeType(), true);
565 if (!aList->length())
568 // Throw away sub-shapes not selected by user if not in preview mode
569 // and manual selection is active
570 if (!isAllSubShapes()) {
571 LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
572 SALOME_ListIO aSelList;
573 aSelMgr->selectedObjects(aSelList);
575 if (aSelList.Extent() == 1) {
576 Standard_Boolean aResult = Standard_False;
577 GEOM::GEOM_Object_var anObj =
578 GEOMBase::ConvertIOinGEOMObject(aSelList.First(), aResult);
580 if (aResult && !anObj->_is_nil()) {
581 TColStd_IndexedMapOfInteger aMapIndex;
582 aSelMgr->GetIndexes(aSelList.First(), aMapIndex);
584 GEOM::GEOM_ILocalOperations_var aLocOp =
585 getGeomEngine()->GetILocalOperations(getStudyId());
587 for (int i = 0, n = aList->length(); i < n; i++)
588 if (aMapIndex.Contains(aLocOp->GetSubShapeIndex(myObject, aList[i])))
589 objects.push_back(GEOM::GEOM_Object::_duplicate(aList[i]));
594 for (int i = 0, n = aList->length(); i < n; i++)
595 objects.push_back(GEOM::GEOM_Object::_duplicate(aList[i]));
597 return objects.size();
600 //================================================================
601 // Function : getFather
602 // Purpose : Get father object for object to be added in study
603 // ( called with addInStudy method )
604 //================================================================
605 GEOM::GEOM_Object_ptr EntityGUI_SubShapeDlg::getFather( GEOM::GEOM_Object_ptr )
610 QString EntityGUI_SubShapeDlg::getNewObjectName() const
612 return QString::null;