1 // GEOM GEOMGUI : GUI for Geometry component
3 // Copyright (C) 2004 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.org
24 // File : GroupGUI_GroupDlg.cxx
25 // Author : Sergey ANIKIN
29 #include "GroupGUI_GroupDlg.h"
31 #include "SUIT_Desktop.h"
32 #include "SUIT_Session.h"
33 #include "SalomeApp_Application.h"
34 #include "SalomeApp_Study.h"
35 #include "LightApp_SelectionMgr.h"
39 #include "GEOMImpl_Types.hxx"
43 #include <qlineedit.h>
46 #include <TColStd_IndexedMapOfInteger.hxx>
47 #include <TColStd_MapOfInteger.hxx>
50 GroupGUI_GroupDlg::GroupGUI_GroupDlg(Mode mode,
55 :GEOMBase_Skeleton( parent, "GroupGUI_GroupDlg", false,
56 WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu),
60 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
62 QPixmap image0 (resMgr->loadPixmap("GEOM", tr("ICON_OBJBROWSER_VERTEX")));
63 QPixmap image1 (resMgr->loadPixmap("GEOM", tr("ICON_OBJBROWSER_EDGE")));
64 QPixmap image2 (resMgr->loadPixmap("GEOM", tr("ICON_OBJBROWSER_FACE")));
65 QPixmap image3 (resMgr->loadPixmap("GEOM", tr("ICON_OBJBROWSER_SOLID")));
66 QPixmap iconSelect (resMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
68 setCaption( myMode == CreateGroup ? tr( "CREATE_GROUP_TITLE" ) : tr( "EDIT_GROUP_TITLE" ) );
70 // Shape type button group
71 GroupConstructors->setEnabled( myMode == CreateGroup );
72 GroupConstructors->setTitle( tr( "SHAPE_TYPE" ) );
73 RadioButton1->setPixmap( image0 );
74 RadioButton2->setPixmap( image1 );
75 RadioButton3->setPixmap( image2 );
76 RadioButton4->setPixmap( image3 );
80 GroupBoxName->setTitle( tr( "GROUP_NAME" ) );
82 // Main shape and sub-shapes
83 GroupMedium = new QGroupBox( 1, Qt::Vertical, tr( "MAIN_SUB_SHAPES" ), this );
84 GroupMedium->setInsideMargin( 10 );
85 Layout1->addWidget( GroupMedium, 2, 0 );
87 QWidget* aFrame = new QWidget( GroupMedium );
88 QGridLayout* aMedLayout = new QGridLayout( aFrame, 4, 4, 0, 6 );
90 QLabel* aMainLabel = new QLabel( tr( "MAIN_SHAPE" ), aFrame );
92 mySelBtn = new QPushButton( aFrame );
93 mySelBtn->setPixmap( iconSelect );
94 mySelBtn->setEnabled( myMode == CreateGroup );
96 myMainName = new QLineEdit( aFrame );
97 myMainName->setReadOnly( true );
98 myMainName->setEnabled( myMode == CreateGroup );
100 mySelSubBtn = new QPushButton( tr( "SELECT_SUB_SHAPES" ), aFrame );
101 mySelAllBtn = new QPushButton( tr( "SELECT_ALL" ), aFrame );
102 myAddBtn = new QPushButton( tr( "ADD" ), aFrame );
103 myRemBtn = new QPushButton( tr( "REMOVE" ), aFrame );
104 myIdList = new QListBox( aFrame );
106 myIdList->setSelectionMode( QListBox::Extended );
107 myIdList->setRowMode( QListBox::FitToWidth );
109 aMedLayout->addWidget( aMainLabel, 0, 0 );
110 aMedLayout->addWidget( mySelBtn, 0, 1 );
111 aMedLayout->addMultiCellWidget( myMainName, 0, 0, 2, 3 );
112 aMedLayout->addMultiCellWidget( mySelSubBtn, 1, 1, 0, 2 );
113 aMedLayout->addWidget( mySelAllBtn, 1, 3 );
114 aMedLayout->addMultiCellWidget( myIdList, 2, 3, 0, 2 );
115 aMedLayout->addWidget( myAddBtn, 2, 3 );
116 aMedLayout->addWidget( myRemBtn, 3, 3 );
121 GroupGUI_GroupDlg::~GroupGUI_GroupDlg()
126 //=================================================================================
129 //=================================================================================
130 void GroupGUI_GroupDlg::Init()
132 // san -- TODO: clear selected sub-shapes...
134 if ( myMode == CreateGroup ) {
135 initName( tr( "GROUP_PREFIX" ) );
137 // Get ready for main shape selection
138 myEditCurrentArgument = myMainName;
140 connect( GroupConstructors, SIGNAL( clicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
141 connect( mySelBtn, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
143 else if ( myMode == EditGroup && IObjectCount() ) {
144 Standard_Boolean aResult = Standard_False;
145 GEOM::GEOM_Object_var anObj =
146 GEOMBase::ConvertIOinGEOMObject( firstIObject(), aResult );
148 if ( aResult && !CORBA::is_nil( anObj ) && anObj->GetType() == GEOM_GROUP ) {
151 ResultName->setText( GEOMBase::GetName( myGroup ) );
153 GEOM::GEOM_IGroupOperations_var anOp = GEOM::GEOM_IGroupOperations::_narrow( getOperation() );
154 myMainObj = anOp->GetMainShape( myGroup );
155 if ( !CORBA::is_nil( myMainObj ) )
156 myMainName->setText( GEOMBase::GetName( myMainObj ) );
158 setShapeType( (TopAbs_ShapeEnum)anOp->GetType( myGroup ) );
160 GEOM::ListOfLong_var aCurrList = anOp->GetObjects( myGroup );
161 QListBoxItem* anItem;
162 for ( int i = 0, n = aCurrList->length(); i < n; i++ ) {
163 anItem = new QListBoxText( QString( "%1" ).arg(aCurrList[i] ) );
164 myIdList->insertItem( anItem );
167 myEditCurrentArgument = 0;
171 LightApp_SelectionMgr* aSelMgr =
172 ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr();
174 connect( aSelMgr, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
176 connect( buttonOk , SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
177 connect( buttonApply, SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
179 connect( mySelSubBtn, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
180 connect( mySelAllBtn, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
181 connect( myAddBtn, SIGNAL( clicked() ), this, SLOT( add() ) );
182 connect( myRemBtn, SIGNAL( clicked() ), this, SLOT( remove() ) );
183 connect( myIdList, SIGNAL( selectionChanged() ), this, SLOT( selectionChanged() ) );
188 //=================================================================================
189 // function : enterEvent()
191 //=================================================================================
192 void GroupGUI_GroupDlg::enterEvent( QEvent* e )
194 if ( !buttonCancel->isEnabled() )
195 this->ActivateThisDialog();
198 //=================================================================================
199 // function : ClickOnOk()
201 //=================================================================================
202 void GroupGUI_GroupDlg::ClickOnOk()
204 if ( ClickOnApply() )
209 //=================================================================================
210 // function : ClickOnApply()
212 //=================================================================================
213 bool GroupGUI_GroupDlg::ClickOnApply()
215 if ( !onAccept( myMode == CreateGroup, true ) )
218 if ( myMode == CreateGroup )
224 //=================================================================================
225 // function : ActivateThisDialog()
227 //=================================================================================
228 void GroupGUI_GroupDlg::ActivateThisDialog()
230 GEOMBase_Skeleton::ActivateThisDialog();
232 connect( ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(),
233 SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
238 //=================================================================================
239 // function : LineEditReturnPressed()
241 //=================================================================================
242 void GroupGUI_GroupDlg::LineEditReturnPressed()
244 QLineEdit* send = ( QLineEdit* )sender();
246 if ( send == myMainName && !myEditCurrentArgument ) {
247 myEditCurrentArgument = myMainName;
251 GEOMBase_Skeleton::LineEditReturnPressed();
257 //=================================================================================
258 // function : SetEditCurrentArgument()
260 //=================================================================================
261 void GroupGUI_GroupDlg::SetEditCurrentArgument()
263 QPushButton* send = (QPushButton*)sender();
265 if ( send == mySelBtn )
266 myEditCurrentArgument = myMainName;
267 else if ( send == mySelSubBtn || send == mySelAllBtn )
268 myEditCurrentArgument = 0;
272 if ( send == mySelAllBtn )
273 selectAllSubShapes();
279 //=================================================================================
280 // function : SelectionIntoArgument()
281 // purpose : Called when selection has changed
282 //=================================================================================
283 void GroupGUI_GroupDlg::SelectionIntoArgument()
285 if ( myEditCurrentArgument ) { // Selection of a main shape is active
286 myEditCurrentArgument->setText( "" );
289 if ( IObjectCount() == 1 ) {
290 Standard_Boolean aResult = Standard_False;
291 GEOM::GEOM_Object_var anObj =
292 GEOMBase::ConvertIOinGEOMObject( firstIObject(), aResult );
294 if ( aResult && !anObj->_is_nil() && GEOMBase::IsShape( anObj ) ) {
296 myEditCurrentArgument->setText( GEOMBase::GetName( anObj ) );
302 myMainObj = GEOM::GEOM_Object::_nil();
304 else { // an attempt to synchronize list box selection with 3d viewer
308 bool isBlocked = myIdList->signalsBlocked();
309 myIdList->blockSignals( true );
310 myIdList->clearSelection();
312 TColStd_IndexedMapOfInteger aMapIndex;
314 if ( IObjectCount() == 1 ) {
315 Standard_Boolean aResult = Standard_False;
316 GEOM::GEOM_Object_var anObj =
317 GEOMBase::ConvertIOinGEOMObject( firstIObject(), aResult );
319 if ( aResult && !anObj->_is_nil() )
320 ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->
321 selectionMgr()->GetIndexes( firstIObject(), aMapIndex );
324 // try to find out and process the object browser selection
325 if ( !aMapIndex.Extent() ) {
326 globalSelection( GEOM_ALLSHAPES );
328 GEOM::ListOfGO anObjects;
329 GEOMBase::ConvertListOfIOInListOfGO(selectedIO(), anObjects);
330 GEOM::GEOM_ILocalOperations_var aLocOp = getGeomEngine()->GetILocalOperations( getStudyId() );
331 for (int i = 0; i < anObjects.length(); i++) {
333 if ( GEOMBase::GetShape(anObjects[i], aShape, getShapeType()) ) {
334 CORBA::Long anIndex = aLocOp->GetSubShapeIndex( myMainObj, anObjects[i] );
336 aMapIndex.Add( anIndex );
340 if ( !myMainObj->_is_nil() )
341 localSelection( myMainObj, getShapeType() );
344 if (aMapIndex.Extent() >= 1) {
346 for ( int i = 0, n = myIdList->count(); i < n; i++ )
347 aMap.insert( myIdList->item( i )->text().toInt(), i );
349 for ( int ii = 1, nn = aMapIndex.Extent(); ii <= nn; ii++ ) {
350 if ( aMap.contains( aMapIndex( ii ) ) )
351 myIdList->setSelected( aMap[aMapIndex( ii )], true );
354 myIdList->blockSignals( isBlocked );
360 //=================================================================================
361 // function : ConstructorsClicked()
362 // purpose : Radio button management
363 //=================================================================================
364 void GroupGUI_GroupDlg::ConstructorsClicked( int constructorId )
371 //=================================================================================
372 // function : selectAllSubShapes
374 //=================================================================================
375 void GroupGUI_GroupDlg::selectAllSubShapes()
377 if ( CORBA::is_nil( myMainObj ) )
380 GEOM::GEOM_IShapesOperations_var aShOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
381 GEOM::GEOM_ILocalOperations_var aLocOp = getGeomEngine()->GetILocalOperations( getStudyId() );
383 GEOM::ListOfGO_var aSubShapes = aShOp->MakeExplode( myMainObj, getShapeType(), false );
384 if ( !aShOp->IsDone() )
387 bool isBlocked = myIdList->signalsBlocked();
388 myIdList->blockSignals( true );
391 QListBoxItem* anItem;
392 for ( int i = 0, n = aSubShapes->length(); i < n; i++ ) {
393 CORBA::Long anIndex = aLocOp->GetSubShapeIndex( myMainObj, aSubShapes[i] );
397 anItem = new QListBoxText( QString( "%1" ).arg( anIndex ) );
398 myIdList->insertItem( anItem );
399 myIdList->setSelected( anItem, true );
402 myIdList->blockSignals( isBlocked );
403 highlightSubShapes();
407 //=================================================================================
410 //=================================================================================
411 void GroupGUI_GroupDlg::add()
413 TColStd_MapOfInteger aMap;
414 for ( int i = 0, n = myIdList->count(); i < n; i++ )
415 aMap.Add( myIdList->item( i )->text().toInt() );
417 TColStd_IndexedMapOfInteger aMapIndex;
419 if ( IObjectCount() == 1 ) {
420 Standard_Boolean aResult = Standard_False;
421 GEOM::GEOM_Object_var anObj =
422 GEOMBase::ConvertIOinGEOMObject( firstIObject(), aResult );
424 if ( aResult && !anObj->_is_nil() )
425 ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->
426 selectionMgr()->GetIndexes( firstIObject(), aMapIndex );
429 // try to find out and process the object browser selection
430 if ( !aMapIndex.Extent() ) {
431 GEOM::ListOfGO anObjects;
432 GEOMBase::ConvertListOfIOInListOfGO(selectedIO(), anObjects);
433 GEOM::GEOM_ILocalOperations_var aLocOp = getGeomEngine()->GetILocalOperations( getStudyId() );
434 for (int i = 0; i < anObjects.length(); i++) {
436 if ( GEOMBase::GetShape(anObjects[i], aShape, getShapeType()) ) {
437 CORBA::Long anIndex = aLocOp->GetSubShapeIndex( myMainObj, anObjects[i] );
439 aMapIndex.Add( anIndex );
444 if ( aMapIndex.Extent() >= 1 ) {
445 QListBoxItem* anItem;
446 bool isBlocked = myIdList->signalsBlocked();
447 myIdList->blockSignals( true );
449 for ( int i = 1, n = aMapIndex.Extent(); i <= n; i++ ) {
450 if ( aMap.Contains( aMapIndex( i ) ) )
453 anItem = new QListBoxText( QString( "%1" ).arg( aMapIndex( i ) ) );
454 myIdList->insertItem( anItem );
455 myIdList->setSelected( anItem, true );
458 myIdList->blockSignals( isBlocked );
464 //=================================================================================
467 //=================================================================================
468 void GroupGUI_GroupDlg::remove()
470 for ( int i = myIdList->count() - 1; i >= 0; i-- ) {
471 if ( myIdList->isSelected( i ) )
472 myIdList->removeItem( i );
478 //=================================================================================
479 // function : getConstructorId()
481 //=================================================================================
482 int GroupGUI_GroupDlg::getConstructorId() const
484 return GroupConstructors->id( GroupConstructors->selected() );
487 //=================================================================================
488 // function : getShapeType()
490 //=================================================================================
491 TopAbs_ShapeEnum GroupGUI_GroupDlg::getShapeType() const
493 switch ( getConstructorId() ) {
494 case 0: return TopAbs_VERTEX;
495 case 1: return TopAbs_EDGE;
496 case 2: return TopAbs_FACE;
497 case 3: return TopAbs_SOLID;
498 default: return TopAbs_SHAPE;
502 //=================================================================================
503 // function : setShapeType()
505 //=================================================================================
506 void GroupGUI_GroupDlg::setShapeType( const TopAbs_ShapeEnum theType )
510 case TopAbs_VERTEX: anId = 0; break;
511 case TopAbs_EDGE: anId = 1; break;
512 case TopAbs_FACE: anId = 2; break;
513 case TopAbs_SOLID: anId = 3; break;
515 GroupConstructors->setButton( anId );
519 //=================================================================================
520 // function : activateSelection
521 // purpose : Activate selection in accordance with myEditCurrentArgument
522 //=================================================================================
523 void GroupGUI_GroupDlg::activateSelection()
525 globalSelection( GEOM_ALLSHAPES );
527 if ( !myMainObj->_is_nil() && !myEditCurrentArgument ) {
528 localSelection( myMainObj, getShapeType() );
531 SelectionIntoArgument();
534 //=================================================================================
535 // function : updateState
537 //=================================================================================
538 void GroupGUI_GroupDlg::updateState()
542 TColStd_IndexedMapOfInteger aMapIndex;
544 if ( IObjectCount() == 1 ) {
545 Standard_Boolean aResult = Standard_False;
546 GEOM::GEOM_Object_var anObj =
547 GEOMBase::ConvertIOinGEOMObject( firstIObject(), aResult );
549 if ( aResult && !anObj->_is_nil() )
550 ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->
551 selectionMgr()->GetIndexes( firstIObject(), aMapIndex );
554 // try to find out and process the object browser selection
555 if ( !aMapIndex.Extent() && !CORBA::is_nil( myMainObj ) ) {
557 GEOM::ListOfGO anObjects;
558 GEOMBase::ConvertListOfIOInListOfGO(selectedIO(), anObjects);
559 GEOM::GEOM_ILocalOperations_var aLocOp = getGeomEngine()->GetILocalOperations( getStudyId() );
560 for (int i = 0; i < anObjects.length(); i++) {
562 if ( GEOMBase::GetShape(anObjects[i], aShape, getShapeType()) ) {
563 CORBA::Long anIndex = aLocOp->GetSubShapeIndex( myMainObj, anObjects[i] );
565 aMapIndex.Add( anIndex );
579 isAdd = aMapIndex.Extent() > 0;
581 myAddBtn->setEnabled( !myEditCurrentArgument && !CORBA::is_nil( myMainObj ) && isAdd );
583 for ( int ii = 0, nn = myIdList->count(); !hasSel && ii < nn; ii++ )
584 hasSel = myIdList->isSelected( ii );
585 myRemBtn->setEnabled( hasSel );
586 mySelSubBtn->setEnabled( !CORBA::is_nil( myMainObj ) );
587 mySelAllBtn->setEnabled( !CORBA::is_nil( myMainObj ) );
590 //=================================================================================
591 // function : selectionChanged
593 //=================================================================================
594 void GroupGUI_GroupDlg::selectionChanged()
596 highlightSubShapes();
599 //=================================================================================
600 // function : highlightSubShapes
602 //=================================================================================
603 void GroupGUI_GroupDlg::highlightSubShapes()
605 if ( CORBA::is_nil( myMainObj ) )
608 Standard_Boolean isOk;
609 Handle(GEOM_AISShape) aSh =
610 GEOMBase::ConvertIORinGEOMAISShape( GEOMBase::GetIORFromObject( myMainObj ), isOk, true );
611 if ( !isOk || aSh.IsNull() )
614 TColStd_MapOfInteger anIds;
618 for ( int ii = 0, nn = myIdList->count(); ii < nn; ii++ )
619 if ( myIdList->isSelected( ii ) )
620 anIds.Add( myIdList->item( ii )->text().toInt() );
622 LightApp_SelectionMgr* aSelMgr =
623 ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr();
624 aSelMgr->clearSelected();
625 aSelMgr->AddOrRemoveIndex( aSh->getIO(), anIds, false );
632 //=================================================================================
633 // function : createOperation
635 //=================================================================================
636 GEOM::GEOM_IOperations_ptr GroupGUI_GroupDlg::createOperation()
638 return getGeomEngine()->GetIGroupOperations( getStudyId() );
641 #define RETURN_WITH_MSG(a, b) \
647 //=================================================================================
648 // function : isValid()
649 // purpose : Verify validity of input data
650 //=================================================================================
651 bool GroupGUI_GroupDlg::isValid( QString& theMessage )
653 SalomeApp_Study* study = getStudy();
655 RETURN_WITH_MSG ( !study->studyDS()->GetProperties()->IsLocked(), tr( "GEOM_STUDY_LOCKED" ) )
657 if ( myMode == CreateGroup ) {
658 RETURN_WITH_MSG( !CORBA::is_nil( myMainObj ), tr( "NO_MAIN_OBJ" ) )
661 RETURN_WITH_MSG( !CORBA::is_nil( myMainObj ), tr( "NO_GROUP" ) )
664 QString aName (getNewObjectName());
665 RETURN_WITH_MSG ( !aName.stripWhiteSpace().isEmpty(), tr( "EMPTY_NAME" ) )
667 RETURN_WITH_MSG ( myIdList->count(), tr( "EMPTY_LIST" ) )
671 //=================================================================================
672 // function : execute
674 //=================================================================================
675 bool GroupGUI_GroupDlg::execute( ObjectList& objects )
677 GEOM::GEOM_IGroupOperations_var anOp = GEOM::GEOM_IGroupOperations::_narrow( getOperation() );
679 GEOM::GEOM_Object_var aGroup;
680 if ( myMode == CreateGroup )
681 aGroup = anOp->CreateGroup( myMainObj, getShapeType() );
682 else if ( myMode == EditGroup )
685 if ( CORBA::is_nil( aGroup ) || ( myMode == CreateGroup && !anOp->IsDone() ) )
688 GEOM::ListOfLong_var aCurrList = anOp->GetObjects( aGroup );
689 if ( !anOp->IsDone() )
692 for ( int i = 0, n = aCurrList->length(); i < n; i++ ) {
693 anOp->RemoveObject( aGroup, aCurrList[i] );
694 if ( !anOp->IsDone() )
698 for ( int ii = 0, nn = myIdList->count(); ii < nn; ii++ ) {
699 anOp->AddObject( aGroup, myIdList->item( ii )->text().toInt() );
700 if ( !anOp->IsDone() )
704 SalomeApp_Study* study = getStudy();
706 string IOR = GEOMBase::GetIORFromObject( aGroup );
708 _PTR(SObject) SO ( study->studyDS()->FindObjectIOR( IOR ) );
710 _PTR(StudyBuilder) aBuilder (study->studyDS()->NewBuilder());
711 aBuilder->SetName( SO, getNewObjectName() );
716 objects.push_back( aGroup._retn() );
721 //================================================================
722 // Function : getFather
723 // Purpose : Get father object for object to be added in study
724 // ( called with addInStudy method )
725 //================================================================
726 GEOM::GEOM_Object_ptr GroupGUI_GroupDlg::getFather( GEOM::GEOM_Object_ptr theObj )
728 GEOM::GEOM_Object_var aFatherObj;
729 if ( theObj->GetType() == GEOM_GROUP ) {
730 GEOM::GEOM_IGroupOperations_var anOp = GEOM::GEOM_IGroupOperations::_narrow( getOperation() );
731 aFatherObj = anOp->GetMainShape( theObj );
733 return aFatherObj._retn();