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 "SalomeApp_SelectionMgr.h"
39 #include "GEOMImpl_Types.hxx"
43 #include <qlineedit.h>
46 #include <TColStd_IndexedMapOfInteger.hxx>
47 #include <TColStd_MapOfInteger.hxx>
52 GroupGUI_GroupDlg::GroupGUI_GroupDlg(Mode mode,
57 :GEOMBase_Skeleton( parent, "GroupGUI_GroupDlg", false,
58 WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu),
62 QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_OBJBROWSER_VERTEX" ) ) );
63 QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_OBJBROWSER_EDGE" ) ) );
64 QPixmap image2( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_OBJBROWSER_FACE" ) ) );
65 QPixmap image3( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_OBJBROWSER_SOLID" ) ) );
67 QPixmap iconSelect( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
69 setCaption( myMode == CreateGroup ? tr( "CREATE_GROUP_TITLE" ) : tr( "EDIT_GROUP_TITLE" ) );
71 // Shape type button group
72 GroupConstructors->setEnabled( myMode == CreateGroup );
73 GroupConstructors->setTitle( tr( "SHAPE_TYPE" ) );
74 RadioButton1->setPixmap( image0 );
75 RadioButton2->setPixmap( image1 );
76 RadioButton3->setPixmap( image2 );
77 RadioButton4->setPixmap( image3 );
81 GroupBoxName->setTitle( tr( "GROUP_NAME" ) );
83 // Main shape and sub-shapes
84 GroupMedium = new QGroupBox( 1, Qt::Vertical, tr( "MAIN_SUB_SHAPES" ), this );
85 GroupMedium->setInsideMargin( 10 );
86 Layout1->addWidget( GroupMedium, 2, 0 );
88 QWidget* aFrame = new QWidget( GroupMedium );
89 QGridLayout* aMedLayout = new QGridLayout( aFrame, 4, 4, 0, 6 );
91 QLabel* aMainLabel = new QLabel( tr( "MAIN_SHAPE" ), aFrame );
93 mySelBtn = new QPushButton( aFrame );
94 mySelBtn->setPixmap( iconSelect );
95 mySelBtn->setEnabled( myMode == CreateGroup );
97 myMainName = new QLineEdit( aFrame );
98 myMainName->setReadOnly( true );
99 myMainName->setEnabled( myMode == CreateGroup );
101 mySelSubBtn = new QPushButton( tr( "SELECT_SUB_SHAPES" ), aFrame );
102 mySelAllBtn = new QPushButton( tr( "SELECT_ALL" ), aFrame );
103 myAddBtn = new QPushButton( tr( "ADD" ), aFrame );
104 myRemBtn = new QPushButton( tr( "REMOVE" ), aFrame );
105 myIdList = new QListBox( aFrame );
107 myIdList->setSelectionMode( QListBox::Extended );
108 myIdList->setRowMode( QListBox::FitToWidth );
110 aMedLayout->addWidget( aMainLabel, 0, 0 );
111 aMedLayout->addWidget( mySelBtn, 0, 1 );
112 aMedLayout->addMultiCellWidget( myMainName, 0, 0, 2, 3 );
113 aMedLayout->addMultiCellWidget( mySelSubBtn, 1, 1, 0, 2 );
114 aMedLayout->addWidget( mySelAllBtn, 1, 3 );
115 aMedLayout->addMultiCellWidget( myIdList, 2, 3, 0, 2 );
116 aMedLayout->addWidget( myAddBtn, 2, 3 );
117 aMedLayout->addWidget( myRemBtn, 3, 3 );
122 GroupGUI_GroupDlg::~GroupGUI_GroupDlg()
127 //=================================================================================
130 //=================================================================================
131 void GroupGUI_GroupDlg::Init()
133 // san -- TODO: clear selected sub-shapes...
135 if ( myMode == CreateGroup ) {
136 initName( tr( "GROUP_PREFIX" ) );
138 // Get ready for main shape selection
139 myEditCurrentArgument = myMainName;
141 connect( GroupConstructors, SIGNAL( clicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
142 connect( mySelBtn, SIGNAL( clicked() ), this, SLOT(SetEditCurrentArgument()));
144 else if ( myMode == EditGroup && IObjectCount() ) {
145 Standard_Boolean aResult = Standard_False;
146 GEOM::GEOM_Object_var anObj =
147 GEOMBase::ConvertIOinGEOMObject( firstIObject(), aResult );
149 if ( aResult && !CORBA::is_nil( anObj ) && anObj->GetType() == GEOM_GROUP ) {
152 ResultName->setText( GEOMBase::GetName( myGroup ) );
154 GEOM::GEOM_IGroupOperations_var anOp = GEOM::GEOM_IGroupOperations::_narrow( getOperation() );
155 myMainObj = anOp->GetMainShape( myGroup );
156 if ( !CORBA::is_nil( myMainObj ) )
157 myMainName->setText( GEOMBase::GetName( myMainObj ) );
159 setShapeType( (TopAbs_ShapeEnum)anOp->GetType( myGroup ) );
161 GEOM::ListOfLong_var aCurrList = anOp->GetObjects( myGroup );
162 QListBoxItem* anItem;
163 for ( int i = 0, n = aCurrList->length(); i < n; i++ ) {
164 anItem = new QListBoxText( QString( "%1" ).arg(aCurrList[i] ) );
165 myIdList->insertItem( anItem );
168 myEditCurrentArgument = 0;
172 connect( buttonOk , SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
173 connect( buttonApply, SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
175 connect( mySelSubBtn, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
176 connect( mySelAllBtn, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
177 connect( ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(),
178 SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
179 connect( mySelAllBtn, SIGNAL( clicked() ), this, SLOT( selectAllSubShapes() ) );
180 connect( myAddBtn, SIGNAL( clicked() ), this, SLOT( add() ) );
181 connect( myRemBtn, SIGNAL( clicked() ), this, SLOT( remove() ) );
182 connect( myIdList, SIGNAL( selectionChanged() ), this, SLOT( selectionChanged() ) );
187 //=================================================================================
188 // function : enterEvent()
190 //=================================================================================
191 void GroupGUI_GroupDlg::enterEvent( QEvent* e )
193 if ( !buttonCancel->isEnabled() )
194 this->ActivateThisDialog();
197 //=================================================================================
198 // function : ClickOnOk()
200 //=================================================================================
201 void GroupGUI_GroupDlg::ClickOnOk()
203 if ( ClickOnApply() )
208 //=================================================================================
209 // function : ClickOnApply()
211 //=================================================================================
212 bool GroupGUI_GroupDlg::ClickOnApply()
214 if ( !onAccept( myMode == CreateGroup, true ) )
217 if ( myMode == CreateGroup )
223 //=================================================================================
224 // function : ActivateThisDialog()
226 //=================================================================================
227 void GroupGUI_GroupDlg::ActivateThisDialog()
229 GEOMBase_Skeleton::ActivateThisDialog();
231 connect( ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(),
232 SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
237 //=================================================================================
238 // function : LineEditReturnPressed()
240 //=================================================================================
241 void GroupGUI_GroupDlg::LineEditReturnPressed()
243 QLineEdit* send = ( QLineEdit* )sender();
245 if ( send == myMainName && !myEditCurrentArgument ) {
246 myEditCurrentArgument = myMainName;
250 GEOMBase_Skeleton::LineEditReturnPressed();
256 //=================================================================================
257 // function : SetEditCurrentArgument()
259 //=================================================================================
260 void GroupGUI_GroupDlg::SetEditCurrentArgument()
262 QPushButton* send = (QPushButton*)sender();
264 if ( send == mySelBtn )
265 myEditCurrentArgument = myMainName;
266 else if ( send == mySelSubBtn || send == mySelAllBtn )
267 myEditCurrentArgument = 0;
274 //=================================================================================
275 // function : SelectionIntoArgument()
276 // purpose : Called when selection has changed
277 //=================================================================================
278 void GroupGUI_GroupDlg::SelectionIntoArgument()
280 if ( myEditCurrentArgument ) { // Selection of a main shape is active
281 myEditCurrentArgument->setText( "" );
283 if ( IObjectCount() == 1 ) {
284 Standard_Boolean aResult = Standard_False;
285 GEOM::GEOM_Object_var anObj =
286 GEOMBase::ConvertIOinGEOMObject( firstIObject(), aResult );
288 if ( aResult && !anObj->_is_nil() && GEOMBase::IsShape( anObj ) ) {
290 myEditCurrentArgument->setText( GEOMBase::GetName( anObj ) );
296 myMainObj = GEOM::GEOM_Object::_nil();
298 else { // an attempt to synchronize list box selection with 3d viewer
302 bool isBlocked = myIdList->signalsBlocked();
303 myIdList->blockSignals( true );
304 myIdList->clearSelection();
306 if ( IObjectCount() == 1 ) {
307 Standard_Boolean aResult = Standard_False;
308 GEOM::GEOM_Object_var anObj =
309 GEOMBase::ConvertIOinGEOMObject( firstIObject(), aResult );
311 if ( aResult && !anObj->_is_nil() ) {
313 for ( int i = 0, n = myIdList->count(); i < n; i++ )
314 aMap.insert( myIdList->item( i )->text().toInt(), i );
316 TColStd_IndexedMapOfInteger aMapIndex;
317 ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr()->GetIndexes( firstIObject(), aMapIndex );
318 for ( int ii = 1, nn = aMapIndex.Extent(); ii <= nn; ii++ ) {
319 if ( aMap.contains( aMapIndex( ii ) ) )
320 myIdList->setSelected( aMap[aMapIndex( ii )], true );
325 myIdList->blockSignals( isBlocked );
331 //=================================================================================
332 // function : ConstructorsClicked()
333 // purpose : Radio button management
334 //=================================================================================
335 void GroupGUI_GroupDlg::ConstructorsClicked( int constructorId )
342 //=================================================================================
343 // function : selectAllSubShapes
345 //=================================================================================
346 void GroupGUI_GroupDlg::selectAllSubShapes()
348 if ( CORBA::is_nil( myMainObj ) )
351 GEOM::GEOM_IShapesOperations_var aShOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
352 GEOM::GEOM_ILocalOperations_var aLocOp = getGeomEngine()->GetILocalOperations( getStudyId() );
354 GEOM::ListOfGO_var aSubShapes = aShOp->MakeExplode( myMainObj, getShapeType(), false );
355 if ( !aShOp->IsDone() )
358 bool isBlocked = myIdList->signalsBlocked();
359 myIdList->blockSignals( true );
362 QListBoxItem* anItem;
363 for ( int i = 0, n = aSubShapes->length(); i < n; i++ ) {
364 CORBA::Long anIndex = aLocOp->GetSubShapeIndex( myMainObj, aSubShapes[i] );
368 anItem = new QListBoxText( QString( "%1" ).arg( anIndex ) );
369 myIdList->insertItem( anItem );
370 myIdList->setSelected( anItem, true );
373 myIdList->blockSignals( isBlocked );
374 highlightSubShapes();
378 //=================================================================================
381 //=================================================================================
382 void GroupGUI_GroupDlg::add()
384 TColStd_MapOfInteger aMap;
385 for ( int i = 0, n = myIdList->count(); i < n; i++ )
386 aMap.Add( myIdList->item( i )->text().toInt() );
388 if ( IObjectCount() == 1 ) {
389 Standard_Boolean aResult = Standard_False;
390 GEOM::GEOM_Object_var anObj =
391 GEOMBase::ConvertIOinGEOMObject( firstIObject(), aResult );
393 if ( aResult && !anObj->_is_nil() ) {
394 TColStd_IndexedMapOfInteger aMapIndex;
395 ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr()->GetIndexes( firstIObject(), aMapIndex );
396 QListBoxItem* anItem;
397 bool isBlocked = myIdList->signalsBlocked();
398 myIdList->blockSignals( true );
400 for ( int i = 1, n = aMapIndex.Extent(); i <= n; i++ ) {
401 if ( aMap.Contains( aMapIndex( i ) ) )
404 anItem = new QListBoxText( QString( "%1" ).arg( aMapIndex( i ) ) );
405 myIdList->insertItem( anItem );
406 myIdList->setSelected( anItem, true );
409 myIdList->blockSignals( isBlocked );
415 //=================================================================================
418 //=================================================================================
419 void GroupGUI_GroupDlg::remove()
421 for ( int i = myIdList->count() - 1; i >= 0; i-- ) {
422 if ( myIdList->isSelected( i ) )
423 myIdList->removeItem( i );
429 //=================================================================================
430 // function : getConstructorId()
432 //=================================================================================
433 int GroupGUI_GroupDlg::getConstructorId() const
435 return GroupConstructors->id( GroupConstructors->selected() );
438 //=================================================================================
439 // function : getShapeType()
441 //=================================================================================
442 TopAbs_ShapeEnum GroupGUI_GroupDlg::getShapeType() const
444 switch ( getConstructorId() ) {
445 case 0: return TopAbs_VERTEX;
446 case 1: return TopAbs_EDGE;
447 case 2: return TopAbs_FACE;
448 case 3: return TopAbs_SOLID;
449 default: return TopAbs_SHAPE;
453 //=================================================================================
454 // function : setShapeType()
456 //=================================================================================
457 void GroupGUI_GroupDlg::setShapeType( const TopAbs_ShapeEnum theType )
461 case TopAbs_VERTEX: anId = 0; break;
462 case TopAbs_EDGE: anId = 1; break;
463 case TopAbs_FACE: anId = 2; break;
464 case TopAbs_SOLID: anId = 3; break;
466 GroupConstructors->setButton( anId );
470 //=================================================================================
471 // function : activateSelection
472 // purpose : Activate selection in accordance with myEditCurrentArgument
473 //=================================================================================
474 void GroupGUI_GroupDlg::activateSelection()
476 globalSelection( GEOM_ALLSHAPES );
478 if ( !myMainObj->_is_nil() && !myEditCurrentArgument ) {
479 localSelection( myMainObj, getShapeType() );
482 SelectionIntoArgument();
485 //=================================================================================
486 // function : updateState
488 //=================================================================================
489 void GroupGUI_GroupDlg::updateState()
492 if ( IObjectCount() == 1 ) {
493 Standard_Boolean aResult = Standard_False;
494 GEOM::GEOM_Object_var anObj =
495 GEOMBase::ConvertIOinGEOMObject( firstIObject(), aResult );
497 if ( aResult && !anObj->_is_nil() ) {
498 TColStd_IndexedMapOfInteger aMapIndex;
499 ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr()->GetIndexes( firstIObject(), aMapIndex );
500 isAdd = aMapIndex.Extent() > 0;
503 myAddBtn->setEnabled( !myEditCurrentArgument && !CORBA::is_nil( myMainObj ) && isAdd );
505 for ( int ii = 0, nn = myIdList->count(); !hasSel && ii < nn; ii++ )
506 hasSel = myIdList->isSelected( ii );
507 myRemBtn->setEnabled( hasSel );
508 mySelSubBtn->setEnabled( !CORBA::is_nil( myMainObj ) );
509 mySelAllBtn->setEnabled( !CORBA::is_nil( myMainObj ) );
512 //=================================================================================
513 // function : selectionChanged
515 //=================================================================================
516 void GroupGUI_GroupDlg::selectionChanged()
518 highlightSubShapes();
521 //=================================================================================
522 // function : highlightSubShapes
524 //=================================================================================
525 void GroupGUI_GroupDlg::highlightSubShapes()
527 if ( CORBA::is_nil( myMainObj ) )
530 Standard_Boolean isOk;
531 Handle(GEOM_AISShape) aSh =
532 GEOMBase::ConvertIORinGEOMAISShape( GEOMBase::GetIORFromObject( myMainObj ), isOk, true );
533 if ( !isOk || aSh.IsNull() )
536 TColStd_MapOfInteger anIds;
540 for ( int ii = 0, nn = myIdList->count(); ii < nn; ii++ )
541 if ( myIdList->isSelected( ii ) )
542 anIds.Add( myIdList->item( ii )->text().toInt() );
544 SalomeApp_SelectionMgr* aSelMgr = ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr();
545 aSelMgr->clearSelected();
546 aSelMgr->AddOrRemoveIndex( aSh->getIO(), anIds, false );
553 //=================================================================================
554 // function : createOperation
556 //=================================================================================
557 GEOM::GEOM_IOperations_ptr GroupGUI_GroupDlg::createOperation()
559 return getGeomEngine()->GetIGroupOperations( getStudyId() );
562 #define RETURN_WITH_MSG(a, b) \
568 //=================================================================================
569 // function : isValid()
570 // purpose : Verify validity of input data
571 //=================================================================================
572 bool GroupGUI_GroupDlg::isValid( QString& theMessage )
574 SalomeApp_Study* study = getStudy();
576 RETURN_WITH_MSG ( !study->studyDS()->GetProperties()->IsLocked(), tr( "GEOM_STUDY_LOCKED" ) )
578 if ( myMode == CreateGroup ) {
579 RETURN_WITH_MSG( !CORBA::is_nil( myMainObj ), tr( "NO_MAIN_OBJ" ) )
582 RETURN_WITH_MSG( !CORBA::is_nil( myMainObj ), tr( "NO_GROUP" ) )
585 const char* aName = getNewObjectName();
586 RETURN_WITH_MSG ( aName && strlen( aName ), tr( "EMPTY_NAME" ) )
588 RETURN_WITH_MSG ( myIdList->count(), tr( "EMPTY_LIST" ) )
592 //=================================================================================
593 // function : execute
595 //=================================================================================
596 bool GroupGUI_GroupDlg::execute( ObjectList& objects )
598 GEOM::GEOM_IGroupOperations_var anOp = GEOM::GEOM_IGroupOperations::_narrow( getOperation() );
600 GEOM::GEOM_Object_var aGroup;
601 if ( myMode == CreateGroup )
602 aGroup = anOp->CreateGroup( myMainObj, getShapeType() );
603 else if ( myMode == EditGroup )
606 if ( CORBA::is_nil( aGroup ) || ( myMode == CreateGroup && !anOp->IsDone() ) )
609 GEOM::ListOfLong_var aCurrList = anOp->GetObjects( aGroup );
610 if ( !anOp->IsDone() )
613 for ( int i = 0, n = aCurrList->length(); i < n; i++ ) {
614 anOp->RemoveObject( aGroup, aCurrList[i] );
615 if ( !anOp->IsDone() )
619 for ( int ii = 0, nn = myIdList->count(); ii < nn; ii++ ) {
620 anOp->AddObject( aGroup, myIdList->item( ii )->text().toInt() );
621 if ( !anOp->IsDone() )
625 SalomeApp_Study* study = getStudy();
627 string IOR = GEOMBase::GetIORFromObject( aGroup );
629 _PTR(SObject) SO ( study->studyDS()->FindObjectIOR( IOR ) );
631 _PTR(StudyBuilder) aBuilder (study->studyDS()->NewBuilder());
632 aBuilder->SetName( SO, getNewObjectName() );
637 objects.push_back( aGroup._retn() );
642 //================================================================
643 // Function : getFather
644 // Purpose : Get father object for object to be added in study
645 // ( called with addInStudy method )
646 //================================================================
647 GEOM::GEOM_Object_ptr GroupGUI_GroupDlg::getFather( GEOM::GEOM_Object_ptr theObj )
649 GEOM::GEOM_Object_var aFatherObj;
650 if ( theObj->GetType() == GEOM_GROUP ) {
651 GEOM::GEOM_IGroupOperations_var anOp = GEOM::GEOM_IGroupOperations::_narrow( getOperation() );
652 aFatherObj = anOp->GetMainShape( theObj );
654 return aFatherObj._retn();