]> SALOME platform Git repositories - modules/geom.git/blob - src/GroupGUI/GroupGUI_GroupDlg.cxx
Salome HOME
IPAL20715 impossible to select sub-shape after apply group
[modules/geom.git] / src / GroupGUI / GroupGUI_GroupDlg.cxx
1 // GEOM GEOMGUI : GUI for Geometry component
2 //
3 // Copyright (C) 2004  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 //
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. 
10 //
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. 
15 //
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 
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File   : GroupGUI_GroupDlg.cxx
23 // Author : Sergey ANIKIN, Open CASCADE S.A.S. (sergey.anikin@opencascade.com)
24 //
25
26 #include "GroupGUI_GroupDlg.h"
27
28 #include <LightApp_SelectionMgr.h>
29 #include <SUIT_ResourceMgr.h>
30 #include <SUIT_Desktop.h>
31 #include <SUIT_OverrideCursor.h>
32 #include <SUIT_Session.h>
33 #include <SalomeApp_Application.h>
34 #include <SalomeApp_Study.h>
35
36 #include <DlgRef.h>
37 #include <GEOMBase.h>
38 #include <GeometryGUI.h>
39 #include <GEOMImpl_Types.hxx>
40 #include <GEOM_Displayer.h>
41
42 #include <QLabel>
43 #include <QListWidget>
44 #include <QLineEdit>
45 #include <QMap>
46
47 #include <TColStd_IndexedMapOfInteger.hxx>
48 #include <TColStd_MapOfInteger.hxx>
49 #include <TColStd_DataMapIteratorOfDataMapOfIntegerInteger.hxx>
50
51 enum { ALL_SUBSHAPES = 0, GET_IN_PLACE, SUBSHAPES_OF_SHAPE2 };
52
53 GroupGUI_GroupDlg::GroupGUI_GroupDlg( Mode mode, GeometryGUI* theGeometryGUI, QWidget* parent )
54   : GEOMBase_Skeleton( theGeometryGUI, parent, false ),
55     myMode( mode ),
56     myBusy( false )
57 {
58   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
59
60   QPixmap image0     ( resMgr->loadPixmap( "GEOM", tr( "ICON_OBJBROWSER_VERTEX" ) ) );
61   QPixmap image1     ( resMgr->loadPixmap( "GEOM", tr( "ICON_OBJBROWSER_EDGE" ) ) );
62   QPixmap image2     ( resMgr->loadPixmap( "GEOM", tr( "ICON_OBJBROWSER_FACE" ) ) );
63   QPixmap image3     ( resMgr->loadPixmap( "GEOM", tr( "ICON_OBJBROWSER_SOLID" ) ) );
64   QPixmap iconSelect ( resMgr->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
65
66   setWindowTitle( myMode == CreateGroup ? tr( "CREATE_GROUP_TITLE" ) : tr( "EDIT_GROUP_TITLE" ) );
67
68   // Shape type button group
69   mainFrame()->GroupConstructors->setEnabled( myMode == CreateGroup );
70   mainFrame()->GroupConstructors->setTitle( tr( "SHAPE_TYPE" ) );
71   mainFrame()->RadioButton1->setIcon( image0 );
72   mainFrame()->RadioButton2->setIcon( image1 );
73   mainFrame()->RadioButton3->setIcon( image2 );
74   mainFrame()->RadioButton4->setIcon( image3 );
75   mainFrame()->RadioButton4->show();
76
77   // Group name
78   mainFrame()->GroupBoxName->setTitle( tr( "GROUP_NAME" ) );
79
80   // Main shape and sub-shapes
81   QGroupBox* GroupMedium = new QGroupBox( tr( "MAIN_SUB_SHAPES" ), centralWidget() );
82   QGridLayout* aMedLayout = new QGridLayout( GroupMedium );
83   aMedLayout->setMargin( 9 );
84   aMedLayout->setSpacing( 6 );
85   
86   QLabel* aMainLabel = new QLabel( tr( "MAIN_SHAPE" ), GroupMedium );
87
88   mySelBtn = new QPushButton( GroupMedium );
89   mySelBtn->setIcon( iconSelect );
90   mySelBtn->setEnabled( myMode == CreateGroup );
91
92   myMainName = new QLineEdit( GroupMedium );
93   myMainName->setReadOnly( true );
94   myMainName->setEnabled( myMode == CreateGroup );
95
96   QLabel* aSecondLabel = new QLabel( tr( "SECOND_SHAPE" ), GroupMedium );
97
98   mySelBtn2 = new QPushButton( GroupMedium );
99   mySelBtn2->setIcon( iconSelect );
100   mySelBtn2->setEnabled( false );
101
102   myShape2Name = new QLineEdit( GroupMedium );
103   myShape2Name->setReadOnly( true );
104   myShape2Name->setEnabled( false );
105
106   mySelectionWayGroupBox = new QGroupBox( tr("SHAPE_SEL_RESTR"), GroupMedium );
107   mySelectionWayGroup = new QButtonGroup( mySelectionWayGroupBox );
108   QRadioButton* allSubs     = new QRadioButton( tr( "NO_RESTR" )            , mySelectionWayGroupBox );
109   QRadioButton* inPlaceSubs = new QRadioButton( tr( "GEOM_PARTS_OF_SHAPE2" ), mySelectionWayGroupBox );
110   QRadioButton* shape2Subs  = new QRadioButton( tr( "SUBSHAPES_OF_SHAPE2" ) , mySelectionWayGroupBox );
111   QVBoxLayout* mySelWayLayout = new QVBoxLayout( mySelectionWayGroupBox );
112   mySelWayLayout->setMargin( 9 );
113   mySelWayLayout->setSpacing( 6 );
114   mySelWayLayout->addWidget( allSubs );
115   mySelWayLayout->addWidget( inPlaceSubs );
116   mySelWayLayout->addWidget( shape2Subs );
117   mySelectionWayGroup->addButton( allSubs,     ALL_SUBSHAPES );
118   mySelectionWayGroup->addButton( inPlaceSubs, GET_IN_PLACE );
119   mySelectionWayGroup->addButton( shape2Subs,  SUBSHAPES_OF_SHAPE2 );
120   allSubs->setChecked( true );
121
122   mySelAllBtn = new QPushButton( tr( "SELECT_ALL" ), GroupMedium );
123   myAddBtn    = new QPushButton( tr( "ADD" ), GroupMedium );
124   myRemBtn    = new QPushButton( tr( "REMOVE" ), GroupMedium );
125   myIdList    = new QListWidget( GroupMedium );
126
127   myIdList->setSelectionMode( QAbstractItemView::ExtendedSelection );
128   myIdList->setFlow( QListView::TopToBottom );
129   myIdList->setWrapping( true );
130
131   aMedLayout->addWidget( aMainLabel,             0, 0 );
132   aMedLayout->addWidget( mySelBtn,               0, 1 );
133   aMedLayout->addWidget( myMainName,             0, 2, 1, 2 );
134   aMedLayout->addWidget( aSecondLabel,           1, 0 );
135   aMedLayout->addWidget( mySelBtn2,              1, 1 );
136   aMedLayout->addWidget( myShape2Name,           1, 2, 1, 2 );
137   aMedLayout->addWidget( mySelectionWayGroupBox, 2, 0, 3, 3 );
138   aMedLayout->addWidget( mySelAllBtn,            2, 3 );
139   aMedLayout->addWidget( myAddBtn,               3, 3 );
140   aMedLayout->addWidget( myRemBtn,               4, 3 );
141   aMedLayout->addWidget( myIdList,               5, 0, 1, 4 );
142   aMedLayout->setRowStretch( 5, 1 );
143
144   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
145   layout->setMargin( 0 ); layout->setSpacing( 6 );
146   layout->addWidget( GroupMedium );
147
148   setHelpFileName( "work_with_groups_page.html" );
149
150   Init();
151 }
152
153 GroupGUI_GroupDlg::~GroupGUI_GroupDlg()
154 {
155 }
156
157 //=================================================================================
158 // function : Init()
159 // purpose  :
160 //=================================================================================
161 void GroupGUI_GroupDlg::Init()
162 {
163   // san -- TODO: clear selected sub-shapes...
164   //mySelSubBtn->setChecked( true );
165   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
166
167   if ( myMode == CreateGroup ) {
168     initName( tr( "GROUP_PREFIX" ) );
169
170     // Get ready for main shape selection
171     myEditCurrentArgument = myMainName;
172
173     connect( this, SIGNAL( constructorsClicked( int ) ),  this, SLOT( ConstructorsClicked( int ) ) );
174     connect( mySelBtn,          SIGNAL( clicked() ),      this, SLOT( SetEditCurrentArgument() ) );
175     connect( mySelBtn2,         SIGNAL( clicked() ),      this, SLOT( SetEditCurrentArgument() ) );
176   }
177   else if (myMode == EditGroup) {
178     SALOME_ListIO aSelList;
179     aSelMgr->selectedObjects(aSelList);
180
181     if (aSelList.Extent()) {
182       Standard_Boolean aResult = Standard_False;
183       GEOM::GEOM_Object_var anObj =
184         GEOMBase::ConvertIOinGEOMObject(aSelList.First(), aResult);
185
186       if ( aResult && !CORBA::is_nil( anObj ) && anObj->GetType() == GEOM_GROUP ) {
187         myGroup = anObj;
188
189         mainFrame()->ResultName->setText( GEOMBase::GetName( myGroup ) );
190
191         GEOM::GEOM_IGroupOperations_var anOp = GEOM::GEOM_IGroupOperations::_narrow( getOperation() );
192         myMainObj = anOp->GetMainShape( myGroup );
193         if ( !CORBA::is_nil( myMainObj ) )
194           myMainName->setText( GEOMBase::GetName( myMainObj ) );
195
196         setShapeType( (TopAbs_ShapeEnum)anOp->GetType( myGroup ) );
197
198         GEOM::ListOfLong_var aCurrList = anOp->GetObjects( myGroup );
199         for ( int i = 0, n = aCurrList->length(); i < n; i++ )
200           myIdList->addItem( new QListWidgetItem( QString( "%1" ).arg( aCurrList[i] ) ) );
201
202         myEditCurrentArgument = 0;
203       }
204       connect( mySelBtn2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
205     }
206   }
207
208   connect( aSelMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()) );
209
210   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk()    ) );
211   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
212
213   connect( mySelectionWayGroup, SIGNAL( buttonClicked( int ) ), this, SLOT( SetEditCurrentArgument() ) );
214   connect( mySelAllBtn,         SIGNAL( clicked() ),            this, SLOT( SetEditCurrentArgument() ) );
215   connect( myAddBtn,            SIGNAL( clicked() ),            this, SLOT( add() ) );
216   connect( myRemBtn,            SIGNAL( clicked() ),            this, SLOT( remove() ) );
217   connect( myIdList,            SIGNAL( selectionChanged() ),   this, SLOT( selectionChanged() ) );
218
219   setInPlaceObj(GEOM::GEOM_Object::_nil());
220
221   myBusy = true; // just activate but do not select in the list
222   activateSelection();
223   myBusy = false;
224 }
225
226 //=================================================================================
227 // function : enterEvent()
228 // purpose  :
229 //=================================================================================
230 void GroupGUI_GroupDlg::enterEvent( QEvent* e )
231 {
232   if ( !buttonCancel()->isEnabled() )
233     ActivateThisDialog();
234 }
235
236 //=======================================================================
237 //function : closeEvent
238 //purpose  : remove temporary geom object
239 //=======================================================================
240
241 void GroupGUI_GroupDlg::closeEvent( QCloseEvent* e )
242 {
243   setInPlaceObj( GEOM::GEOM_Object::_nil() );
244
245   GEOMBase_Skeleton::closeEvent( e );
246 }
247
248 //=================================================================================
249 // function : ClickOnOk()
250 // purpose  :
251 //=================================================================================
252 void GroupGUI_GroupDlg::ClickOnOk()
253 {
254   if ( ClickOnApply() )
255     ClickOnCancel();
256 }
257
258 //=================================================================================
259 // function : ClickOnApply()
260 // purpose  :
261 //=================================================================================
262 bool GroupGUI_GroupDlg::ClickOnApply()
263 {
264   if ( !onAccept( myMode == CreateGroup, true ) )
265     return false;
266
267   if ( myMode == CreateGroup )
268     {
269       initName();
270       myIdList->clear();
271       ConstructorsClicked( getConstructorId() );
272     }
273   else
274     activateSelection();
275   return true;
276 }
277
278 //=================================================================================
279 // function : ActivateThisDialog()
280 // purpose  :
281 //=================================================================================
282 void GroupGUI_GroupDlg::ActivateThisDialog()
283 {
284   GEOMBase_Skeleton::ActivateThisDialog();
285
286   connect( ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(),
287            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
288
289   activateSelection();
290 }
291
292 //=================================================================================
293 // function : LineEditReturnPressed()
294 // purpose  :
295 //=================================================================================
296 void GroupGUI_GroupDlg::LineEditReturnPressed()
297 {
298   QLineEdit* send = ( QLineEdit* )sender();
299
300   if ( send == myMainName && !myEditCurrentArgument ) {
301     myEditCurrentArgument = myMainName;
302     activateSelection();
303   }
304   else
305     GEOMBase_Skeleton::LineEditReturnPressed();
306
307   updateState();
308 }
309
310 //=================================================================================
311 // function : SetEditCurrentArgument()
312 // purpose  :
313 //=================================================================================
314 void GroupGUI_GroupDlg::SetEditCurrentArgument()
315 {
316   QPushButton* send = (QPushButton*)sender();
317
318   if ( send == mySelBtn ) {
319     myEditCurrentArgument = myMainName;
320     myShape2Name->setText( "" );
321   }
322   else if ( /*(QRadioButton*)sender() == mySelSubBtn ||*/ send == mySelAllBtn )
323     myEditCurrentArgument = 0;
324   else if ( send == mySelBtn2 || sender() == mySelectionWayGroup ) {
325     setInPlaceObj( GEOM::GEOM_Object::_nil() );
326     //if ( myPlaceCheckBox->isChecked() )
327     myShape2Name->setText( "" );
328     if ( subSelectionWay() != ALL_SUBSHAPES )
329     {
330       myEditCurrentArgument = myShape2Name;
331     }
332     else {
333       //myEditCurrentArgument = myMainName;
334       myEditCurrentArgument = 0;
335     }
336   }
337
338   activateSelection();
339
340   if ( send == mySelAllBtn ) {
341 //     myShape2Name->setText( "" );
342 //     myPlaceCheckBox->setChecked( false );
343 //     mySelBtn2->setEnabled( false );
344 //     myShape2Name->setEnabled( false );
345     selectAllSubShapes();
346   }
347   else
348     updateState();
349 }
350
351 //=================================================================================
352 // function : onGetInPlace()
353 // purpose  :
354 //=================================================================================
355 void GroupGUI_GroupDlg::onGetInPlace()
356 {
357   setInPlaceObj( GEOM::GEOM_Object::_nil() );
358   myEditCurrentArgument->setText( "" );
359
360   bool isBlocked = myIdList->signalsBlocked();
361   myIdList->blockSignals( true );
362   myIdList->clearSelection();
363   myIdList->blockSignals( isBlocked );
364
365   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
366   SALOME_ListIO aSelList;
367   aSelMgr->selectedObjects(aSelList);
368
369   if (aSelList.Extent() != 1)
370     return;
371
372   Standard_Boolean aResult = Standard_False;
373   GEOM::GEOM_Object_var anObj =
374     GEOMBase::ConvertIOinGEOMObject(aSelList.First(), aResult);
375   if (aResult && !anObj->_is_nil() && GEOMBase::IsShape(anObj)) {
376     if (!anObj->_is_equivalent(myMainObj) && !anObj->_is_equivalent(myGroup)) {
377       SUIT_OverrideCursor wc;
378       myEditCurrentArgument->setText(GEOMBase::GetName(anObj));
379       GEOM::GEOM_IShapesOperations_var aShapesOp =
380         getGeomEngine()->GetIShapesOperations(getStudyId());
381       if ( subSelectionWay() == GET_IN_PLACE ) {
382         GEOM::GEOM_Object_var aGetInPlaceObj = aShapesOp->GetInPlace(myMainObj, anObj);
383         setInPlaceObj( aGetInPlaceObj );
384       }
385       else {
386         setInPlaceObj( anObj );
387       }
388       myEditCurrentArgument = 0;
389       //myBusy = true; // just activate but do not select in the list
390       activateSelection();
391       //myBusy = false;
392     }
393   }
394 }
395
396 //=======================================================================
397 //function : setInPlaceObj
398 //purpose  : temporarily add an object to study and remove old InPlaceObj
399 //=======================================================================
400
401 void GroupGUI_GroupDlg::setInPlaceObj( GEOM::GEOM_Object_var theObj )
402 {
403   if ( ! myInPlaceObj->_is_equivalent( theObj ) )
404   {
405     const char* tmpName = "__InPlaceObj__";
406     // remove old InPlaceObj
407     if ( !myInPlaceObj->_is_nil() ) {
408       if ( myInPlaceObjSelectWay == GET_IN_PLACE ) { // hide temporary object
409         GEOM_Displayer aDisplayer(getStudy());
410         aDisplayer.Erase( myInPlaceObj, true );
411       }
412       if (_PTR(SObject) SO = getStudy()->studyDS()->FindObject( tmpName )) {
413         getStudy()->studyDS()->NewBuilder()->RemoveObjectWithChildren( SO );
414         getGeomEngine()->RemoveObject(myInPlaceObj);
415       }
416     }
417     // publish InPlaceObj to enable localSelection(InPlaceObj)
418     if ( !theObj->_is_nil() && subSelectionWay() == GET_IN_PLACE ) {
419       SALOMEDS::Study_var aStudyDS = GeometryGUI::ClientStudyToStudy(getStudy()->studyDS());
420       SALOMEDS::SObject_var aSO =
421         getGeomEngine()->AddInStudy(aStudyDS, theObj, tmpName, myMainObj);
422     }
423     myInPlaceObj = theObj;
424   }
425   // build map of indices
426   myMain2InPlaceIndices.Clear();
427   if ( !myInPlaceObj->_is_nil() ) {
428     GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
429     GEOM::GEOM_ILocalOperations_var aLocOp = getGeomEngine()->GetILocalOperations( getStudyId() );
430
431     GEOM::ListOfGO_var aSubObjects = aShapesOp->MakeExplode( myInPlaceObj, getShapeType(), false);
432     for (int i = 0; i < aSubObjects->length(); i++)
433     {
434       CORBA::Long aMainIndex = aLocOp->GetSubShapeIndex( myMainObj, aSubObjects[i] );
435       CORBA::Long aPlaceIndex = aLocOp->GetSubShapeIndex( myInPlaceObj, aSubObjects[i] );
436       if ( aMainIndex >= 0 && aPlaceIndex > 0)
437         myMain2InPlaceIndices.Bind( aMainIndex, aPlaceIndex );
438     }
439   }
440   myInPlaceObjSelectWay = subSelectionWay();
441 }
442
443 //=================================================================================
444 // function : SelectionIntoArgument()
445 // purpose  : Called when selection has changed
446 //=================================================================================
447 void GroupGUI_GroupDlg::SelectionIntoArgument()
448 {
449 //   if (myPlaceCheckBox->isChecked() && myEditCurrentArgument == myShape2Name )
450   if (subSelectionWay() != ALL_SUBSHAPES && myEditCurrentArgument == myShape2Name) {
451     onGetInPlace();
452     return;
453   }
454
455   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
456   SALOME_ListIO aSelList;
457   aSelMgr->selectedObjects(aSelList);
458
459   if (myEditCurrentArgument == myMainName) {  // Selection of a main shape is active
460     myEditCurrentArgument->setText("");
461     myIdList->clear();
462
463     if (aSelList.Extent() == 1) {
464       Standard_Boolean aResult = Standard_False;
465       GEOM::GEOM_Object_var anObj =
466         GEOMBase::ConvertIOinGEOMObject(aSelList.First(), aResult);
467
468       if ( aResult && !anObj->_is_nil() && GEOMBase::IsShape( anObj ) ) {
469         myMainObj = anObj;
470         myEditCurrentArgument->setText( GEOMBase::GetName( anObj ) );
471         // activate subshapes selection by default
472         myEditCurrentArgument = 0;
473         activateSelection();
474         updateState();
475         return;
476       }
477     }
478
479     myMainObj = GEOM::GEOM_Object::_nil();
480   }
481   else { // an attempt to synchronize list box selection with 3d viewer
482     if ( myBusy ) {
483       return;
484     }
485
486     bool isBlocked = myIdList->signalsBlocked();
487     myIdList->blockSignals( true );
488     myIdList->clearSelection();
489
490     TColStd_IndexedMapOfInteger aMapIndex;
491
492     LightApp_SelectionMgr::MapEntryOfMapOfInteger aMap;
493     aSelMgr->selectedSubOwners( aMap );
494     if ( aMap.Size() == 1 )
495       aMapIndex = LightApp_SelectionMgr::MapEntryOfMapOfInteger::Iterator( aMap ).Value();
496     bool subselected = aMapIndex.Extent();
497
498     // convert inPlace indices to main indices
499     //if ( subselected && myPlaceCheckBox->isChecked() )
500     if ( subselected && subSelectionWay() != ALL_SUBSHAPES )
501     {
502       TColStd_IndexedMapOfInteger aMapIndex2;
503       
504       TColStd_DataMapIteratorOfDataMapOfIntegerInteger m2ip( myMain2InPlaceIndices );
505       for ( ; m2ip.More(); m2ip.Next() ) {
506         int inPlaceId = m2ip.Value();
507         if ( aMapIndex.Contains( inPlaceId )) {
508           aMapIndex2.Add( m2ip.Key() );
509         }
510       }
511       aMapIndex = aMapIndex2;
512     }
513
514     // try to find out and process the object browser selection
515     if ( !subselected ) {
516       globalSelection( GEOM_ALLSHAPES );
517
518       GEOM::ListOfGO anObjects;
519       GEOMBase::ConvertListOfIOInListOfGO(aSelList, anObjects);
520
521       GEOM::GEOM_ILocalOperations_var aLocOp = getGeomEngine()->GetILocalOperations( getStudyId() );
522       GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
523
524       for (int i = 0; i < anObjects.length(); i++)
525       {
526         GEOM::GEOM_Object_var aGeomObj = anObjects[i];
527         GEOM::ListOfGO_var aSubObjects = new GEOM::ListOfGO();
528         TopoDS_Shape aShape;
529         if ( GEOMBase::GetShape(aGeomObj, aShape, getShapeType()) )
530         {
531           aSubObjects->length(1);
532           aSubObjects[0] = aGeomObj;
533         }
534         else if (aGeomObj->GetType() == GEOM_GROUP)
535           aSubObjects = aShapesOp->MakeExplode( aGeomObj, getShapeType(), false);
536         else
537           continue;
538
539         for (int i = 0; i < aSubObjects->length(); i++)
540         {
541           TopoDS_Shape aShape;
542           if ( GEOMBase::GetShape(aSubObjects[i], aShape, getShapeType()) )
543           {
544             CORBA::Long anIndex;
545             anIndex = aLocOp->GetSubShapeIndex( myMainObj, aSubObjects[i] );
546             if ( anIndex >= 0 ) {
547               //if ( myPlaceCheckBox->isChecked() && ! myMain2InPlaceIndices.IsBound( anIndex ))
548               if ( subSelectionWay() != ALL_SUBSHAPES &&
549                    ! myMain2InPlaceIndices.IsBound( anIndex ))
550                 continue;
551               aMapIndex.Add( anIndex );
552             }
553           }
554         }
555       }
556       if ( !myMainObj->_is_nil() /*&& mySelSubBtn->isChecked()*/)
557         if ( subSelectionWay() == ALL_SUBSHAPES )
558           localSelection( myMainObj, getShapeType() );
559         else if ( !myInPlaceObj->_is_nil() )
560           localSelection( myInPlaceObj, getShapeType() );
561     }
562
563     if (aMapIndex.Extent() >= 1) {
564       QMap<int, int> aMap;
565       for ( int i = 0, n = myIdList->count(); i < n; i++ )
566         aMap.insert( myIdList->item( i )->text().toInt(), i );
567
568       for ( int ii = 1, nn = aMapIndex.Extent(); ii <= nn; ii++ ) {
569         if ( aMap.contains( aMapIndex( ii ) ) )
570           myIdList->item( aMap[aMapIndex( ii )])->setSelected( true );
571       }
572     }
573     myIdList->blockSignals( isBlocked );
574   }
575
576   updateState();
577 }
578
579 //=================================================================================
580 // function : ConstructorsClicked()
581 // purpose  : Radio button management
582 //=================================================================================
583 void GroupGUI_GroupDlg::ConstructorsClicked( int constructorId )
584 {
585   myIdList->clear();
586   activateSelection();
587   updateState();
588   setInPlaceObj( myInPlaceObj ); // to rebuild myMain2InPlaceIndices
589 }
590
591 //=================================================================================
592 // function : selectAllSubShapes
593 // purpose  : 
594 //=================================================================================
595 void GroupGUI_GroupDlg::selectAllSubShapes()
596 {
597   if ( CORBA::is_nil( myMainObj ) )
598     return;
599
600   GEOM::ListOfLong_var aSubShapes;
601 //   if ( !myPlaceCheckBox->isChecked() )
602   if ( subSelectionWay() == ALL_SUBSHAPES )
603   {
604     myIdList->clear();
605     GEOM::GEOM_IShapesOperations_var aShOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
606     aSubShapes = aShOp->SubShapeAllIDs(myMainObj, getShapeType(), false);
607
608     if ( !aShOp->IsDone() )
609       return;
610   }
611   else
612   {
613     aSubShapes = new GEOM::ListOfLong();
614     aSubShapes->length( myMain2InPlaceIndices.Extent() );
615     TColStd_DataMapIteratorOfDataMapOfIntegerInteger m2ip( myMain2InPlaceIndices );
616     for ( int i = 0; m2ip.More(); i++, m2ip.Next() )
617       aSubShapes[ i ] = m2ip.Key();
618   }
619   bool isBlocked = myIdList->signalsBlocked();
620   myIdList->blockSignals( true );
621
622   for ( int i = 0, n = aSubShapes->length(); i < n; i++ ) {
623     CORBA::Long anIndex = aSubShapes[i];
624     if ( anIndex < 0 )
625       continue;
626
627     QListWidgetItem* anItem = 0;
628     QString text = QString( "%1" ).arg( anIndex );
629     if ( !myInPlaceObj->_is_nil() ) {
630       QList<QListWidgetItem*> found = myIdList->findItems( text, Qt::MatchExactly );
631       if ( found.count() ) anItem = found[0];
632     }
633     if ( !anItem ) {
634       anItem = new QListWidgetItem( text );
635       myIdList->addItem( anItem );
636     }
637     anItem->setSelected( true );
638   }
639
640   myIdList->blockSignals( isBlocked );
641   highlightSubShapes();
642 }
643
644 //=================================================================================
645 // function : add
646 // purpose  : 
647 //=================================================================================
648 void GroupGUI_GroupDlg::add()
649 {
650   TColStd_MapOfInteger aMap;
651   for ( int i = 0, n = myIdList->count(); i < n; i++ )
652     aMap.Add( myIdList->item( i )->text().toInt() );
653
654   TColStd_IndexedMapOfInteger aMapIndex;
655
656   SALOME_ListIO aSelIOs;
657   SalomeApp_Application* app = myGeomGUI->getApp();
658   if ( app ) {
659     LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
660     if ( aSelMgr ) {
661       LightApp_SelectionMgr::MapEntryOfMapOfInteger aMap;
662       aSelMgr->selectedSubOwners( aMap );
663       if ( aMap.Size() == 1 )
664         aMapIndex = LightApp_SelectionMgr::MapEntryOfMapOfInteger::Iterator( aMap ).Value();
665     }
666   }
667   GEOM::ListOfGO anObjects;
668   // get selected sub-shapes of myInPlaceObj
669   if ( aMapIndex.Extent() > 0 && !myInPlaceObj->_is_nil() )
670   {
671     GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
672
673     anObjects.length( aMapIndex.Extent() );
674     for ( int i = 1; i <= aMapIndex.Extent(); i++ )
675       anObjects[ i-1 ] = aShapesOp->GetSubShape( myInPlaceObj, aMapIndex( i ));
676
677     aMapIndex.Clear();
678   }
679
680   // try to find out and process the object browser selection or InPlace sub-shapes
681   if ( !aMapIndex.Extent() )
682   {
683     if ( anObjects.length() == 0 )
684       GEOMBase::ConvertListOfIOInListOfGO(aSelIOs, anObjects);
685
686     GEOM::GEOM_ILocalOperations_var aLocOp = getGeomEngine()->GetILocalOperations( getStudyId() );
687     GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
688
689     for (int i = 0; i < anObjects.length(); i++)
690     {
691       GEOM::GEOM_Object_var aGeomObj = anObjects[i];
692       GEOM::ListOfGO_var aSubObjects  = new GEOM::ListOfGO();
693       TopoDS_Shape aShape;
694       if ( GEOMBase::GetShape(aGeomObj, aShape, getShapeType()) )
695       {
696         aSubObjects->length(1);
697         aSubObjects[0] = aGeomObj;
698       }
699       else if (aGeomObj->GetType() == GEOM_GROUP)
700         aSubObjects = aShapesOp->MakeExplode( aGeomObj, getShapeType(), false);
701       else
702         break;
703
704       for (int i = 0; i < aSubObjects->length(); i++)
705       {
706         TopoDS_Shape aShape;
707         if ( GEOMBase::GetShape(aSubObjects[i], aShape, getShapeType()) )
708         {
709           CORBA::Long anIndex;
710             anIndex = aLocOp->GetSubShapeIndex( myMainObj, aSubObjects[i] );
711           if ( anIndex >= 0 )
712             aMapIndex.Add( anIndex );
713         }
714       }
715     }
716   }
717
718   if ( aMapIndex.Extent() >= 1 ) {
719     bool isBlocked = myIdList->signalsBlocked();
720     myIdList->blockSignals( true );
721
722     for ( int i = 1, n = aMapIndex.Extent(); i <= n; i++ ) {
723       if ( aMap.Contains( aMapIndex( i ) ) )
724         continue;
725
726       QListWidgetItem* anItem = new QListWidgetItem( QString( "%1" ).arg( aMapIndex( i ) ) );
727       myIdList->addItem( anItem );
728       anItem->setSelected( true );
729     }
730
731     myIdList->blockSignals( isBlocked );
732   }
733
734   updateState();
735 }
736
737 //=================================================================================
738 // function : remove
739 // purpose  : 
740 //=================================================================================
741 void GroupGUI_GroupDlg::remove()
742 {
743   bool isBlocked = myIdList->signalsBlocked();
744   myIdList->blockSignals( true );
745
746   QListIterator<QListWidgetItem*> it( myIdList->selectedItems() );
747   while ( it.hasNext() )
748     delete it.next();
749
750   myIdList->blockSignals( isBlocked );
751
752   highlightSubShapes();
753 }
754
755 //=======================================================================
756 //function : subSelectionWay
757 //purpose  : 
758 //=======================================================================
759
760 int GroupGUI_GroupDlg::subSelectionWay() const
761 {
762   return mySelectionWayGroup->checkedId();
763 }
764
765 //=================================================================================
766 // function : getShapeType()
767 // purpose  :
768 //=================================================================================
769 TopAbs_ShapeEnum GroupGUI_GroupDlg::getShapeType() const
770 {
771   switch ( getConstructorId() ) {
772   case 0:  return TopAbs_VERTEX;
773   case 1:  return TopAbs_EDGE;
774   case 2:  return TopAbs_FACE;
775   case 3:  return TopAbs_SOLID;
776   default: return TopAbs_SHAPE;
777   }
778 }
779
780 //=================================================================================
781 // function : setShapeType()
782 // purpose  :
783 //=================================================================================
784 void GroupGUI_GroupDlg::setShapeType( const TopAbs_ShapeEnum theType )
785 {
786   int anId = 0;
787   switch ( theType ) {
788   case TopAbs_VERTEX: anId = 0; break;
789   case TopAbs_EDGE:   anId = 1; break;
790   case TopAbs_FACE:   anId = 2; break;
791   case TopAbs_SOLID:  anId = 3; break;
792   }
793   setConstructorId( anId );
794 }
795
796
797 //=================================================================================
798 // function : activateSelection
799 // purpose  : Activate selection in accordance with myEditCurrentArgument
800 //=================================================================================
801 void GroupGUI_GroupDlg::activateSelection()
802 {
803   globalSelection( GEOM_ALLSHAPES );
804
805   // local selection
806   if ( !myMainObj->_is_nil() && !myEditCurrentArgument/* && mySelSubBtn->isChecked()*/) {
807 //     if ( !myPlaceCheckBox->isChecked() )
808     if ( subSelectionWay() == ALL_SUBSHAPES )
809       localSelection( myMainObj, getShapeType() );
810     else if ( !myInPlaceObj->_is_nil() )
811       localSelection( myInPlaceObj, getShapeType() );
812   }
813
814   SelectionIntoArgument();
815 }
816
817 //=================================================================================
818 // function : updateState
819 // purpose  : 
820 //=================================================================================
821 void GroupGUI_GroupDlg::updateState()
822 {
823   bool isAdd = false;
824
825   TColStd_IndexedMapOfInteger aMapIndex;
826
827   SALOME_ListIO aSelIOs;
828   SalomeApp_Application* app = myGeomGUI->getApp();
829   if ( app ) {
830     LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
831     if ( aSelMgr ) {
832       LightApp_SelectionMgr::MapEntryOfMapOfInteger aMap;
833       aSelMgr->selectedSubOwners( aMap );
834       if ( aMap.Size() == 1 )
835         aMapIndex = LightApp_SelectionMgr::MapEntryOfMapOfInteger::Iterator( aMap ).Value();
836     }
837   }
838
839   // try to find out and process the object browser selection
840   if ( !aMapIndex.Extent() && !CORBA::is_nil( myMainObj ) ) {
841     GEOM::ListOfGO anObjects;
842     //GEOMBase::ConvertListOfIOInListOfGO(selectedIO(), anObjects);
843     GEOMBase::ConvertListOfIOInListOfGO(aSelIOs, anObjects);
844
845     GEOM::GEOM_ILocalOperations_var aLocOp = getGeomEngine()->GetILocalOperations( getStudyId() );
846     GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
847
848     isAdd = true;
849
850     for (int i = 0; i < anObjects.length(); i++)
851     {
852       GEOM::GEOM_Object_var aGeomObj = anObjects[i];
853       GEOM::ListOfGO_var aSubObjects = new GEOM::ListOfGO();
854       TopoDS_Shape aShape;
855       if ( GEOMBase::GetShape(aGeomObj, aShape, getShapeType()) ) {
856         aSubObjects->length(1);
857         aSubObjects[0] = aGeomObj;
858       }
859       else if (aGeomObj->GetType() == GEOM_GROUP) {
860         aSubObjects = aShapesOp->MakeExplode( aGeomObj, getShapeType(), false);
861       }
862       else {
863         aMapIndex.Clear();
864         break;
865       }
866
867       for (int i = 0; i < aSubObjects->length(); i++)
868       {
869         TopoDS_Shape aShape;
870         aSubObjects[i];
871         if ( GEOMBase::GetShape(aSubObjects[i], aShape, getShapeType()) )
872         {
873           CORBA::Long anIndex;
874           anIndex = aLocOp->GetSubShapeIndex( myMainObj, aSubObjects[i] );
875           if ( anIndex >= 0 )
876             aMapIndex.Add( anIndex );
877           else
878             isAdd = false;
879         }
880         else
881           isAdd = false;
882
883         if ( !isAdd ) {
884           aMapIndex.Clear();
885           break;
886         }
887       }
888       if ( !isAdd ) {
889         aMapIndex.Clear();
890         break;
891       }
892     }
893   }
894
895   isAdd = aMapIndex.Extent() > 0;
896
897   myAddBtn->setEnabled( !myEditCurrentArgument && !CORBA::is_nil( myMainObj ) && isAdd );
898   bool hasSel = myIdList->selectedItems().count() > 0;
899   myRemBtn->setEnabled( hasSel );
900   //mySelSubBtn->setEnabled( !CORBA::is_nil( myMainObj ) );
901   //myPlaceCheckBox->setEnabled( !CORBA::is_nil( myMainObj ) );
902   mySelectionWayGroupBox->setEnabled( !CORBA::is_nil( myMainObj ) );
903   mySelAllBtn->setEnabled( !CORBA::is_nil( myMainObj ) );
904 //   mySelBtn2->setEnabled( myPlaceCheckBox->isChecked() );
905 //   myShape2Name->setEnabled( myPlaceCheckBox->isChecked() );
906   mySelBtn2->setEnabled(    subSelectionWay() != ALL_SUBSHAPES );
907   myShape2Name->setEnabled( subSelectionWay() != ALL_SUBSHAPES );
908 //   if ( !myPlaceCheckBox->isChecked() )
909   if ( subSelectionWay() == ALL_SUBSHAPES )
910     setInPlaceObj( GEOM::GEOM_Object::_nil() );
911 }
912
913 //=================================================================================
914 // function : selectionChanged
915 // purpose  :
916 //=================================================================================
917 void GroupGUI_GroupDlg::selectionChanged()
918 {
919   highlightSubShapes();
920 }
921
922 //=================================================================================
923 // function : highlightSubShapes
924 // purpose  :
925 //=================================================================================
926 void GroupGUI_GroupDlg::highlightSubShapes()
927 {
928   if ( CORBA::is_nil( myMainObj ) )
929     return;
930
931   Standard_Boolean isOk;
932   char* objIOR;
933
934   if ( myInPlaceObj->_is_nil() )
935     objIOR = GEOMBase::GetIORFromObject( myMainObj );
936   else
937     objIOR = GEOMBase::GetIORFromObject( myInPlaceObj );
938
939   Handle(GEOM_AISShape) aSh = GEOMBase::ConvertIORinGEOMAISShape( objIOR, isOk, true );
940   free( objIOR );
941   if ( !isOk || aSh.IsNull() )
942     return;
943
944   TColStd_MapOfInteger anIds;
945
946   myBusy = true;
947
948   int ii = 0, nn = myIdList->count();
949   for ( ; ii < nn; ii++ )
950   {
951     if ( myIdList->item( ii )->isSelected() ) {
952       int id = myIdList->item( ii )->text().toInt();
953       //       if ( myPlaceCheckBox->isChecked() )
954       if ( subSelectionWay() != ALL_SUBSHAPES )
955       {
956         if ( myMain2InPlaceIndices.IsBound( id ) )
957           id = myMain2InPlaceIndices( id );
958         else {
959           myIdList->item( ii )->setSelected( false );
960           continue;
961         }
962       }
963       anIds.Add( id );
964     }
965   }
966   SalomeApp_Application* app = myGeomGUI->getApp();
967   LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
968   aSelMgr->clearSelected();
969
970   aSelMgr->AddOrRemoveIndex(aSh->getIO(), anIds, false);
971
972   myBusy = false;
973
974   if (nn < 3000)
975     updateState();
976   else {
977     myAddBtn->setEnabled( true );
978     myRemBtn->setEnabled( true );
979   }
980 }
981
982 //=================================================================================
983 // function : createOperation
984 // purpose  :
985 //=================================================================================
986 GEOM::GEOM_IOperations_ptr GroupGUI_GroupDlg::createOperation()
987 {
988   return getGeomEngine()->GetIGroupOperations( getStudyId() );
989 }
990
991 #define RETURN_WITH_MSG(a, b) \
992   if ( !(a) ) { \
993     theMessage += (b); \
994     return false; \
995   }
996
997 //=================================================================================
998 // function : isValid()
999 // purpose  : Verify validity of input data
1000 //=================================================================================
1001 bool GroupGUI_GroupDlg::isValid( QString& theMessage )
1002 {
1003   SalomeApp_Study* study = getStudy();
1004   ASSERT(study);
1005   RETURN_WITH_MSG  ( !study->studyDS()->GetProperties()->IsLocked(), tr( "GEOM_STUDY_LOCKED" ) )
1006
1007   if ( myMode == CreateGroup ) {
1008     RETURN_WITH_MSG( !CORBA::is_nil( myMainObj ), tr( "NO_MAIN_OBJ" ) )
1009   }
1010   else {
1011     RETURN_WITH_MSG( !CORBA::is_nil( myMainObj ), tr( "NO_GROUP" ) )
1012   }
1013
1014   QString aName (getNewObjectName());
1015   RETURN_WITH_MSG  ( !aName.trimmed().isEmpty(), tr( "EMPTY_NAME" ) )
1016
1017   RETURN_WITH_MSG  ( myIdList->count(), tr( "EMPTY_LIST" ) )
1018   return true;
1019 }
1020
1021 //=================================================================================
1022 // function : execute
1023 // purpose  :
1024 //=================================================================================
1025 bool GroupGUI_GroupDlg::execute( ObjectList& objects )
1026 {
1027   setInPlaceObj( GEOM::GEOM_Object::_nil() );
1028
1029   GEOM::GEOM_IGroupOperations_var anOp = GEOM::GEOM_IGroupOperations::_narrow(getOperation());
1030
1031   GEOM::GEOM_Object_var aGroup;
1032   if (myMode == CreateGroup)
1033     aGroup = anOp->CreateGroup(myMainObj, getShapeType());
1034   else if (myMode == EditGroup)
1035     aGroup = myGroup;
1036
1037   if (CORBA::is_nil(aGroup) || (myMode == CreateGroup && !anOp->IsDone()))
1038     return false;
1039
1040   GEOM::ListOfLong_var aCurrList = anOp->GetObjects(aGroup);
1041   if (!anOp->IsDone())
1042     return false;
1043
1044   if (aCurrList->length() > 0)
1045   {
1046     anOp->DifferenceIDs(aGroup, aCurrList);
1047     if (!anOp->IsDone())
1048       return false;
1049   }
1050
1051   int ii, nn = myIdList->count();
1052   if (nn > 0)
1053   {
1054     GEOM::ListOfLong_var aNewList = new GEOM::ListOfLong;
1055     aNewList->length(nn);
1056     for (ii = 0; ii < nn; ii++) {
1057       aNewList[ii] = myIdList->item(ii)->text().toInt();
1058     }
1059     anOp->UnionIDs(aGroup, aNewList);
1060     if (!anOp->IsDone())
1061       return false;
1062   }
1063
1064   SalomeApp_Study* study = getStudy();
1065   if ( study ) {
1066     char* objIOR = GEOMBase::GetIORFromObject( aGroup );
1067     std::string IOR( objIOR );
1068     free( objIOR );
1069     if ( IOR != "" ) {
1070       _PTR(SObject) SO ( study->studyDS()->FindObjectIOR( IOR ) );
1071       if ( SO ) {
1072         _PTR(StudyBuilder) aBuilder (study->studyDS()->NewBuilder());
1073         aBuilder->SetName( SO, getNewObjectName().toLatin1().constData() );
1074       }
1075     }
1076   }
1077
1078   objects.push_back( aGroup._retn() );
1079
1080   return true;
1081 }
1082
1083 //================================================================
1084 // Function : getFather
1085 // Purpose  : Get father object for object to be added in study
1086 //            ( called with addInStudy method )
1087 //================================================================
1088 GEOM::GEOM_Object_ptr GroupGUI_GroupDlg::getFather( GEOM::GEOM_Object_ptr theObj )
1089 {
1090   GEOM::GEOM_Object_var aFatherObj;
1091   if ( theObj->GetType() == GEOM_GROUP ) {
1092     GEOM::GEOM_IGroupOperations_var anOp = GEOM::GEOM_IGroupOperations::_narrow( getOperation() );
1093     aFatherObj = anOp->GetMainShape( theObj );
1094   }
1095   return aFatherObj._retn();
1096 }