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"
33 #include "GEOMImpl_Types.hxx"
35 #include "QAD_Desktop.h"
39 #include <qlineedit.h>
42 #include <TColStd_IndexedMapOfInteger.hxx>
43 #include <TColStd_MapOfInteger.hxx>
45 GroupGUI_GroupDlg::GroupGUI_GroupDlg(Mode mode,
48 SALOME_Selection* Sel,
51 :GEOMBase_Skeleton( parent, "GroupGUI_GroupDlg", Sel, false,
52 WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu),
56 QPixmap image0( QAD_Desktop::getResourceManager()->loadPixmap( "GEOM", tr( "ICON_OBJBROWSER_VERTEX" ) ) );
57 QPixmap image1( QAD_Desktop::getResourceManager()->loadPixmap( "GEOM", tr( "ICON_OBJBROWSER_EDGE" ) ) );
58 QPixmap image2( QAD_Desktop::getResourceManager()->loadPixmap( "GEOM", tr( "ICON_OBJBROWSER_FACE" ) ) );
59 QPixmap image3( QAD_Desktop::getResourceManager()->loadPixmap( "GEOM", tr( "ICON_OBJBROWSER_SOLID" ) ) );
61 QPixmap iconSelect( QAD_Desktop::getResourceManager()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
63 setCaption( myMode == CreateGroup ? tr( "CREATE_GROUP_TITLE" ) : tr( "EDIT_GROUP_TITLE" ) );
65 // Shape type button group
66 GroupConstructors->setEnabled( myMode == CreateGroup );
67 GroupConstructors->setTitle( tr( "SHAPE_TYPE" ) );
68 RadioButton1->setPixmap( image0 );
69 RadioButton2->setPixmap( image1 );
70 RadioButton3->setPixmap( image2 );
71 RadioButton4->setPixmap( image3 );
75 GroupBoxName->setTitle( tr( "GROUP_NAME" ) );
77 // Main shape and sub-shapes
78 GroupMedium = new QGroupBox( 1, Qt::Vertical, tr( "MAIN_SUB_SHAPES" ), this );
79 GroupMedium->setInsideMargin( 10 );
80 Layout1->addWidget( GroupMedium, 2, 0 );
82 QWidget* aFrame = new QWidget( GroupMedium );
83 QGridLayout* aMedLayout = new QGridLayout( aFrame, 4, 4, 0, 6 );
85 QLabel* aMainLabel = new QLabel( tr( "MAIN_SHAPE" ), aFrame );
87 mySelBtn = new QPushButton( aFrame );
88 mySelBtn->setPixmap( iconSelect );
89 mySelBtn->setEnabled( myMode == CreateGroup );
91 myMainName = new QLineEdit( aFrame );
92 myMainName->setReadOnly( true );
93 myMainName->setEnabled( myMode == CreateGroup );
95 mySelSubBtn = new QPushButton( tr( "SELECT_SUB_SHAPES" ), aFrame );
96 mySelAllBtn = new QPushButton( tr( "SELECT_ALL" ), aFrame );
97 myAddBtn = new QPushButton( tr( "ADD" ), aFrame );
98 myRemBtn = new QPushButton( tr( "REMOVE" ), aFrame );
99 myIdList = new QListBox( aFrame );
101 myIdList->setSelectionMode( QListBox::Extended );
102 myIdList->setRowMode( QListBox::FitToWidth );
104 aMedLayout->addWidget( aMainLabel, 0, 0 );
105 aMedLayout->addWidget( mySelBtn, 0, 1 );
106 aMedLayout->addMultiCellWidget( myMainName, 0, 0, 2, 3 );
107 aMedLayout->addMultiCellWidget( mySelSubBtn, 1, 1, 0, 2 );
108 aMedLayout->addWidget( mySelAllBtn, 1, 3 );
109 aMedLayout->addMultiCellWidget( myIdList, 2, 3, 0, 2 );
110 aMedLayout->addWidget( myAddBtn, 2, 3 );
111 aMedLayout->addWidget( myRemBtn, 3, 3 );
116 GroupGUI_GroupDlg::~GroupGUI_GroupDlg()
121 //=================================================================================
124 //=================================================================================
125 void GroupGUI_GroupDlg::Init( SALOME_Selection* Sel )
129 // san -- TODO: clear selected sub-shapes...
131 if ( myMode == CreateGroup ) {
132 initName( tr( "GROUP_PREFIX" ) );
134 // Get ready for main shape selection
135 myEditCurrentArgument = myMainName;
137 connect( GroupConstructors, SIGNAL( clicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
138 connect( mySelBtn, SIGNAL( clicked() ), this, SLOT(SetEditCurrentArgument()));
140 else if ( myMode == EditGroup && Sel->IObjectCount() ) {
141 Standard_Boolean aResult = Standard_False;
142 GEOM::GEOM_Object_var anObj =
143 GEOMBase::ConvertIOinGEOMObject( Sel->firstIObject(), aResult );
145 if ( aResult && !CORBA::is_nil( anObj ) && anObj->GetType() == GEOM_GROUP ) {
148 ResultName->setText( GEOMBase::GetName( myGroup ) );
150 GEOM::GEOM_IGroupOperations_var anOp = GEOM::GEOM_IGroupOperations::_narrow( getOperation() );
151 myMainObj = anOp->GetMainShape( myGroup );
152 if ( !CORBA::is_nil( myMainObj ) )
153 myMainName->setText( GEOMBase::GetName( myMainObj ) );
155 setShapeType( (TopAbs_ShapeEnum)anOp->GetType( myGroup ) );
157 GEOM::ListOfLong_var aCurrList = anOp->GetObjects( myGroup );
158 QListBoxItem* anItem;
159 for ( int i = 0, n = aCurrList->length(); i < n; i++ ) {
160 anItem = new QListBoxText( QString( "%1" ).arg(aCurrList[i] ) );
161 myIdList->insertItem( anItem );
164 myEditCurrentArgument = 0;
168 connect( buttonOk , SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
169 connect( buttonApply, SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
171 connect( mySelSubBtn, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
172 connect( mySelAllBtn, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
173 connect( mySelection,SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
174 connect( mySelAllBtn, SIGNAL( clicked() ), this, SLOT( selectAllSubShapes() ) );
175 connect( myAddBtn, SIGNAL( clicked() ), this, SLOT( add() ) );
176 connect( myRemBtn, SIGNAL( clicked() ), this, SLOT( remove() ) );
177 connect( myIdList, SIGNAL( selectionChanged() ), this, SLOT( selectionChanged() ) );
182 //=================================================================================
183 // function : enterEvent()
185 //=================================================================================
186 void GroupGUI_GroupDlg::enterEvent( QEvent* e )
188 if ( !buttonCancel->isEnabled() )
189 this->ActivateThisDialog();
192 //=================================================================================
193 // function : ClickOnOk()
195 //=================================================================================
196 void GroupGUI_GroupDlg::ClickOnOk()
198 if ( ClickOnApply() )
203 //=================================================================================
204 // function : ClickOnApply()
206 //=================================================================================
207 bool GroupGUI_GroupDlg::ClickOnApply()
209 if ( !onAccept( myMode == CreateGroup, true ) )
212 if ( myMode == CreateGroup )
218 //=================================================================================
219 // function : ActivateThisDialog()
221 //=================================================================================
222 void GroupGUI_GroupDlg::ActivateThisDialog()
224 GEOMBase_Skeleton::ActivateThisDialog();
226 connect( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
231 //=================================================================================
232 // function : LineEditReturnPressed()
234 //=================================================================================
235 void GroupGUI_GroupDlg::LineEditReturnPressed()
237 QLineEdit* send = ( QLineEdit* )sender();
239 if ( send == myMainName && !myEditCurrentArgument ) {
240 myEditCurrentArgument = myMainName;
244 GEOMBase_Skeleton::LineEditReturnPressed();
250 //=================================================================================
251 // function : SetEditCurrentArgument()
253 //=================================================================================
254 void GroupGUI_GroupDlg::SetEditCurrentArgument()
256 QPushButton* send = (QPushButton*)sender();
258 if ( send == mySelBtn )
259 myEditCurrentArgument = myMainName;
260 else if ( send == mySelSubBtn || send == mySelAllBtn )
261 myEditCurrentArgument = 0;
268 //=================================================================================
269 // function : SelectionIntoArgument()
270 // purpose : Called when selection has changed
271 //=================================================================================
272 void GroupGUI_GroupDlg::SelectionIntoArgument()
274 if ( myEditCurrentArgument ) { // Selection of a main shape is active
275 myEditCurrentArgument->setText( "" );
277 if ( mySelection->IObjectCount() == 1 ) {
278 Standard_Boolean aResult = Standard_False;
279 GEOM::GEOM_Object_var anObj =
280 GEOMBase::ConvertIOinGEOMObject( mySelection->firstIObject(), aResult );
282 if ( aResult && !anObj->_is_nil() ) {
284 myEditCurrentArgument->setText( GEOMBase::GetName( anObj ) );
290 myMainObj = GEOM::GEOM_Object::_nil();
292 else { // an attempt to synchronize list box selection with 3d viewer
296 bool isBlocked = myIdList->signalsBlocked();
297 myIdList->blockSignals( true );
298 myIdList->clearSelection();
300 if ( mySelection->IObjectCount() == 1 ) {
301 Standard_Boolean aResult = Standard_False;
302 GEOM::GEOM_Object_var anObj =
303 GEOMBase::ConvertIOinGEOMObject( mySelection->firstIObject(), aResult );
305 if ( aResult && !anObj->_is_nil() ) {
307 for ( int i = 0, n = myIdList->count(); i < n; i++ )
308 aMap.insert( myIdList->item( i )->text().toInt(), i );
310 TColStd_IndexedMapOfInteger aMapIndex;
311 mySelection->GetIndex( mySelection->firstIObject(), aMapIndex );
312 for ( int ii = 1, nn = aMapIndex.Extent(); ii <= nn; ii++ ) {
313 if ( aMap.contains( aMapIndex( ii ) ) )
314 myIdList->setSelected( aMap[aMapIndex( ii )], true );
319 myIdList->blockSignals( isBlocked );
325 //=================================================================================
326 // function : ConstructorsClicked()
327 // purpose : Radio button management
328 //=================================================================================
329 void GroupGUI_GroupDlg::ConstructorsClicked( int constructorId )
336 //=================================================================================
337 // function : selectAllSubShapes
339 //=================================================================================
340 void GroupGUI_GroupDlg::selectAllSubShapes()
342 if ( CORBA::is_nil( myMainObj ) )
345 GEOM::GEOM_IShapesOperations_var aShOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
346 GEOM::GEOM_ILocalOperations_var aLocOp = getGeomEngine()->GetILocalOperations( getStudyId() );
348 GEOM::ListOfGO_var aSubShapes = aShOp->MakeExplode( myMainObj, getShapeType(), false );
349 if ( !aShOp->IsDone() )
352 bool isBlocked = myIdList->signalsBlocked();
353 myIdList->blockSignals( true );
356 QListBoxItem* anItem;
357 for ( int i = 0, n = aSubShapes->length(); i < n; i++ ) {
358 CORBA::Long anIndex = aLocOp->GetSubShapeIndex( myMainObj, aSubShapes[i] );
362 anItem = new QListBoxText( QString( "%1" ).arg( anIndex ) );
363 myIdList->insertItem( anItem );
364 myIdList->setSelected( anItem, true );
367 myIdList->blockSignals( isBlocked );
368 highlightSubShapes();
372 //=================================================================================
375 //=================================================================================
376 void GroupGUI_GroupDlg::add()
378 TColStd_MapOfInteger aMap;
379 for ( int i = 0, n = myIdList->count(); i < n; i++ )
380 aMap.Add( myIdList->item( i )->text().toInt() );
382 if ( mySelection->IObjectCount() == 1 ) {
383 Standard_Boolean aResult = Standard_False;
384 GEOM::GEOM_Object_var anObj =
385 GEOMBase::ConvertIOinGEOMObject( mySelection->firstIObject(), aResult );
387 if ( aResult && !anObj->_is_nil() ) {
388 TColStd_IndexedMapOfInteger aMapIndex;
389 mySelection->GetIndex( mySelection->firstIObject(), aMapIndex );
390 QListBoxItem* anItem;
391 bool isBlocked = myIdList->signalsBlocked();
392 myIdList->blockSignals( true );
394 for ( int i = 1, n = aMapIndex.Extent(); i <= n; i++ ) {
395 if ( aMap.Contains( aMapIndex( i ) ) )
398 anItem = new QListBoxText( QString( "%1" ).arg( aMapIndex( i ) ) );
399 myIdList->insertItem( anItem );
400 myIdList->setSelected( anItem, true );
403 myIdList->blockSignals( isBlocked );
409 //=================================================================================
412 //=================================================================================
413 void GroupGUI_GroupDlg::remove()
415 for ( int i = myIdList->count() - 1; i >= 0; i-- ) {
416 if ( myIdList->isSelected( i ) )
417 myIdList->removeItem( i );
423 //=================================================================================
424 // function : getConstructorId()
426 //=================================================================================
427 int GroupGUI_GroupDlg::getConstructorId() const
429 return GroupConstructors->id( GroupConstructors->selected() );
432 //=================================================================================
433 // function : getShapeType()
435 //=================================================================================
436 TopAbs_ShapeEnum GroupGUI_GroupDlg::getShapeType() const
438 switch ( getConstructorId() ) {
439 case 0: return TopAbs_VERTEX;
440 case 1: return TopAbs_EDGE;
441 case 2: return TopAbs_FACE;
442 case 3: return TopAbs_SOLID;
443 default: return TopAbs_SHAPE;
447 //=================================================================================
448 // function : setShapeType()
450 //=================================================================================
451 void GroupGUI_GroupDlg::setShapeType( const TopAbs_ShapeEnum theType )
455 case TopAbs_VERTEX: anId = 0; break;
456 case TopAbs_EDGE: anId = 1; break;
457 case TopAbs_FACE: anId = 2; break;
458 case TopAbs_SOLID: anId = 3; break;
460 GroupConstructors->setButton( anId );
464 //=================================================================================
465 // function : activateSelection
466 // purpose : Activate selection in accordance with myEditCurrentArgument
467 //=================================================================================
468 void GroupGUI_GroupDlg::activateSelection()
472 if ( !myMainObj->_is_nil() && !myEditCurrentArgument ) {
473 localSelection( myMainObj, getShapeType() );
476 SelectionIntoArgument();
479 //=================================================================================
480 // function : updateState
482 //=================================================================================
483 void GroupGUI_GroupDlg::updateState()
486 if ( mySelection->IObjectCount() == 1 ) {
487 Standard_Boolean aResult = Standard_False;
488 GEOM::GEOM_Object_var anObj =
489 GEOMBase::ConvertIOinGEOMObject( mySelection->firstIObject(), aResult );
491 if ( aResult && !anObj->_is_nil() ) {
492 TColStd_IndexedMapOfInteger aMapIndex;
493 mySelection->GetIndex( mySelection->firstIObject(), aMapIndex );
494 isAdd = aMapIndex.Extent() > 0;
497 myAddBtn->setEnabled( !myEditCurrentArgument && !CORBA::is_nil( myMainObj ) && isAdd );
499 for ( int ii = 0, nn = myIdList->count(); !hasSel && ii < nn; ii++ )
500 hasSel = myIdList->isSelected( ii );
501 myRemBtn->setEnabled( hasSel );
502 mySelSubBtn->setEnabled( !CORBA::is_nil( myMainObj ) );
503 mySelAllBtn->setEnabled( !CORBA::is_nil( myMainObj ) );
506 //=================================================================================
507 // function : selectionChanged
509 //=================================================================================
510 void GroupGUI_GroupDlg::selectionChanged()
512 highlightSubShapes();
515 //=================================================================================
516 // function : highlightSubShapes
518 //=================================================================================
519 void GroupGUI_GroupDlg::highlightSubShapes()
521 if ( CORBA::is_nil( myMainObj ) )
524 Standard_Boolean isOk;
525 Handle(GEOM_AISShape) aSh =
526 GEOMBase::ConvertIORinGEOMAISShape( GEOMBase::GetIORFromObject( myMainObj ), isOk, true );
527 if ( !isOk || aSh.IsNull() )
530 TColStd_MapOfInteger anIds;
534 for ( int ii = 0, nn = myIdList->count(); ii < nn; ii++ )
535 if ( myIdList->isSelected( ii ) )
536 anIds.Add( myIdList->item( ii )->text().toInt() );
538 mySelection->ClearIObjects();
539 mySelection->AddOrRemoveIndex( aSh->getIO(), anIds, false, true );
546 //=================================================================================
547 // function : createOperation
549 //=================================================================================
550 GEOM::GEOM_IOperations_ptr GroupGUI_GroupDlg::createOperation()
552 return getGeomEngine()->GetIGroupOperations( getStudyId() );
555 #define RETURN_WITH_MSG(a, b) \
561 //=================================================================================
562 // function : isValid()
563 // purpose : Verify validity of input data
564 //=================================================================================
565 bool GroupGUI_GroupDlg::isValid( QString& theMessage )
567 QAD_Study* study = getStudy();
569 RETURN_WITH_MSG ( !study->getStudyDocument()->GetProperties()->IsLocked(), tr( "GEOM_STUDY_LOCKED" ) )
571 if ( myMode == CreateGroup ) {
572 RETURN_WITH_MSG( !CORBA::is_nil( myMainObj ), tr( "NO_MAIN_OBJ" ) )
575 RETURN_WITH_MSG( !CORBA::is_nil( myMainObj ), tr( "NO_GROUP" ) )
578 const char* aName = getNewObjectName();
579 RETURN_WITH_MSG ( aName && strlen( aName ), tr( "EMPTY_NAME" ) )
581 RETURN_WITH_MSG ( myIdList->count(), tr( "EMPTY_LIST" ) )
585 //=================================================================================
586 // function : execute
588 //=================================================================================
589 bool GroupGUI_GroupDlg::execute( ObjectList& objects )
591 GEOM::GEOM_IGroupOperations_var anOp = GEOM::GEOM_IGroupOperations::_narrow( getOperation() );
593 GEOM::GEOM_Object_var aGroup;
594 if ( myMode == CreateGroup )
595 aGroup = anOp->CreateGroup( myMainObj, getShapeType() );
596 else if ( myMode == EditGroup )
599 if ( CORBA::is_nil( aGroup ) || ( myMode == CreateGroup && !anOp->IsDone() ) )
602 GEOM::ListOfLong_var aCurrList = anOp->GetObjects( aGroup );
603 if ( !anOp->IsDone() )
606 for ( int i = 0, n = aCurrList->length(); i < n; i++ ) {
607 anOp->RemoveObject( aGroup, aCurrList[i] );
608 if ( !anOp->IsDone() )
612 for ( int ii = 0, nn = myIdList->count(); ii < nn; ii++ ) {
613 anOp->AddObject( aGroup, myIdList->item( ii )->text().toInt() );
614 if ( !anOp->IsDone() )
618 QAD_Study* study = getStudy();
620 string IOR = GEOMBase::GetIORFromObject( aGroup );
622 SALOMEDS::SObject_var SO = study->getStudyDocument()->FindObjectIOR( IOR.c_str() );
623 if ( !SO->_is_nil() ) {
624 SALOMEDS::StudyBuilder_var aBuilder = study->getStudyDocument()->NewBuilder();
625 aBuilder->SetName( SO, getNewObjectName() );
630 objects.push_back( aGroup._retn() );
635 //================================================================
636 // Function : getFather
637 // Purpose : Get father object for object to be added in study
638 // ( called with addInStudy method )
639 //================================================================
640 GEOM::GEOM_Object_ptr GroupGUI_GroupDlg::getFather( GEOM::GEOM_Object_ptr theObj )
642 GEOM::GEOM_Object_var aFatherObj;
643 if ( theObj->GetType() == GEOM_GROUP ) {
644 GEOM::GEOM_IGroupOperations_var anOp = GEOM::GEOM_IGroupOperations::_narrow( getOperation() );
645 aFatherObj = anOp->GetMainShape( theObj );
647 return aFatherObj._retn();