Salome HOME
Merge branch 'V8_0_BR'
[modules/geom.git] / src / GroupGUI / GroupGUI_GroupDlg.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  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, or (at your option) any later version.
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
23 //  GEOM GEOMGUI : GUI for Geometry component
24 //  File   : GroupGUI_GroupDlg.cxx
25 //  Author : Sergey ANIKIN, Open CASCADE S.A.S. (sergey.anikin@opencascade.com)
26
27 #include "GroupGUI_GroupDlg.h"
28
29 #include <DlgRef.h>
30 #include <GEOMBase.h>
31 #include <GeometryGUI.h>
32 #include <GEOM_Displayer.h>
33 #include <GEOMUtils.hxx>
34 #ifndef DISABLE_PLOT2DVIEWER
35 #include <MeasureGUI_ShapeStatisticsDlg.h>
36 #endif
37
38 #include <SalomeApp_Application.h>
39 #include <SalomeApp_Study.h>
40 #include <SalomeApp_Tools.h>
41
42 #include <LightApp_SelectionMgr.h>
43 #include "utilities.h"
44
45 #include <OCCViewer_ViewModel.h>
46 #include <OCCViewer_ViewManager.h>
47 #include <SVTK_ViewModel.h>
48 #include <SALOME_Prs.h>
49 #include <SALOME_ListIO.hxx>
50 #include "utilities.h"
51
52 #include <SUIT_Desktop.h>
53 #include <SUIT_MessageBox.h>
54 #include <SUIT_OverrideCursor.h>
55 #include <SUIT_ResourceMgr.h>
56 #include <SUIT_Session.h>
57 #include <SUIT_ViewWindow.h>
58 #include <SUIT_ViewManager.h>
59
60 #include <QLabel>
61 #include <QListWidget>
62 #include <QLineEdit>
63 #include <QMap>
64
65 #include <AIS_ListOfInteractive.hxx>
66 #include <AIS_ListIteratorOfListOfInteractive.hxx>
67
68 #include <TopExp.hxx>
69 #include <TopExp_Explorer.hxx>
70 #include <TopTools_IndexedMapOfShape.hxx>
71 #include <TColStd_IndexedMapOfInteger.hxx>
72 #include <TColStd_MapOfInteger.hxx>
73 #include <TColStd_DataMapIteratorOfDataMapOfIntegerInteger.hxx>
74
75 #include <GEOMImpl_Types.hxx>
76
77 #define GROUP_IDLST_COLOR Qt::blue // Specific color for the IDs of subShapes in the dialog box
78 #define GROUP_NEWIDLST_COLOR Qt::red // Specific color for the new IDs of subShapes in the dialog box
79
80 namespace {
81   enum { ALL_SUBSHAPES = 0, GET_IN_PLACE, SUBSHAPES_OF_SHAPE2, SUBSHAPES_OF_INVISIBLE_SHAPE2 };
82   enum { Filter_LT, Filter_LE, Filter_GT, Filter_GE };
83 }
84
85 GroupGUI_GroupDlg::GroupGUI_GroupDlg (Mode mode, GeometryGUI* theGeometryGUI, QWidget* parent)
86   : GEOMBase_Skeleton(theGeometryGUI, parent, false),
87     myMode(mode),
88     myBusy(false),
89     myIsShapeType(false),
90     myIsHiddenMain(false),
91     myWasHiddenMain(true),
92     myIsAccept(false)
93 {
94   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
95
96   QPixmap image0     (resMgr->loadPixmap("GEOM", tr("ICON_OBJBROWSER_VERTEX")));
97   QPixmap image1     (resMgr->loadPixmap("GEOM", tr("ICON_OBJBROWSER_EDGE")));
98   QPixmap image2     (resMgr->loadPixmap("GEOM", tr("ICON_OBJBROWSER_FACE")));
99   QPixmap image3     (resMgr->loadPixmap("GEOM", tr("ICON_OBJBROWSER_SOLID")));
100   QPixmap iconSelect (resMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
101
102   setWindowTitle(myMode == CreateGroup ? tr("CREATE_GROUP_TITLE") : tr("EDIT_GROUP_TITLE"));
103
104   // Shape type button group
105   mainFrame()->GroupConstructors->setEnabled(myMode == CreateGroup);
106   mainFrame()->GroupConstructors->setTitle(tr("SHAPE_TYPE"));
107   mainFrame()->RadioButton1->setIcon(image0);
108   mainFrame()->RadioButton2->setIcon(image1);
109   mainFrame()->RadioButton3->setIcon(image2);
110   mainFrame()->RadioButton4->setIcon(image3);
111   mainFrame()->RadioButton4->show();
112
113   // Group name
114   mainFrame()->GroupBoxName->setTitle(tr("GROUP_NAME"));
115
116   // Main shape and sub-shapes
117   QGroupBox* GroupMedium = new QGroupBox(tr("MAIN_SUB_SHAPES"), centralWidget());
118   QGridLayout* aMedLayout = new QGridLayout(GroupMedium);
119   aMedLayout->setMargin(9);
120   aMedLayout->setSpacing(6);
121
122   QLabel* aMainLabel = new QLabel(tr("MAIN_SHAPE"), GroupMedium);
123
124   mySelBtn = new QPushButton(GroupMedium);
125   mySelBtn->setIcon(iconSelect);
126   mySelBtn->setEnabled(myMode == CreateGroup);
127
128   myMainName = new QLineEdit(GroupMedium);
129   myMainName->setReadOnly(true);
130   myMainName->setEnabled(myMode == CreateGroup);
131
132   myRestrictGroupBox = new QGroupBox(tr("SHAPE_SEL_RESTR"), GroupMedium);
133   myRestrictGroup = new QButtonGroup(myRestrictGroupBox);
134   QRadioButton* allSubs     = new QRadioButton(tr("NO_RESTR")            , myRestrictGroupBox);
135   QRadioButton* inPlaceSubs = new QRadioButton(tr("GEOM_PARTS_OF_SHAPE2"), myRestrictGroupBox);
136   QRadioButton* shape2Subs  = new QRadioButton(tr("SUBSHAPES_OF_SHAPE2") , myRestrictGroupBox);
137   QGridLayout* aRestrictLayout = new QGridLayout(myRestrictGroupBox);
138
139   QLabel* aSecondLabel = new QLabel(tr("SECOND_SHAPE"), myRestrictGroupBox);
140   mySelBtn2 = new QPushButton(myRestrictGroupBox);
141   mySelBtn2->setIcon(iconSelect);
142   mySelBtn2->setEnabled(false);
143   myShape2Name = new QLineEdit(myRestrictGroupBox);
144   myShape2Name->setReadOnly(true);
145   myShape2Name->setEnabled(false);
146
147   aRestrictLayout->setMargin(9);
148   aRestrictLayout->setSpacing(6);
149   aRestrictLayout->addWidget(allSubs,      0, 0, 1, 3);
150   aRestrictLayout->addWidget(inPlaceSubs,  1, 0, 1, 3);
151   aRestrictLayout->addWidget(shape2Subs,   2, 0, 1, 3);
152   aRestrictLayout->addWidget(aSecondLabel, 3, 0);
153   aRestrictLayout->addWidget(mySelBtn2,    3, 1);
154   aRestrictLayout->addWidget(myShape2Name, 3, 2);
155   myRestrictGroup->addButton(allSubs,      ALL_SUBSHAPES);
156   myRestrictGroup->addButton(inPlaceSubs,  GET_IN_PLACE);
157   myRestrictGroup->addButton(shape2Subs,   SUBSHAPES_OF_SHAPE2);
158   myRestrictGroupBox->setEnabled(!CORBA::is_nil(myMainObj));
159   allSubs->setChecked(true);
160
161   myShowOnlyBtn = new QPushButton(tr("SHOW_ONLY_SELECTED"), GroupMedium);
162   myHideSelBtn  = new QPushButton(tr("HIDE_SELECTED"), GroupMedium);
163   myShowAllBtn  = new QPushButton(tr("SHOW_ALL_SUB_SHAPES"), GroupMedium);
164
165   mySelAllBtn   = new QPushButton(tr("SELECT_ALL"), GroupMedium);
166   myAddBtn      = new QPushButton(tr("ADD"), GroupMedium);
167   myRemBtn      = new QPushButton(tr("REMOVE"), GroupMedium);
168
169   myIdList    = new QListWidget(GroupMedium);
170
171   myIdList->setSelectionMode(QAbstractItemView::ExtendedSelection);
172   myIdList->setFlow(QListView::TopToBottom);
173   myIdList->setWrapping(true);
174
175   aMedLayout->addWidget(aMainLabel,         0, 0);
176   aMedLayout->addWidget(mySelBtn,           0, 1);
177   aMedLayout->addWidget(myMainName,         0, 2, 1, 2);
178   aMedLayout->addWidget(myRestrictGroupBox, 1, 0, 4, 3);
179
180   aMedLayout->addWidget(myShowOnlyBtn,      1, 3);
181   aMedLayout->addWidget(myHideSelBtn,       2, 3);
182   aMedLayout->addWidget(myShowAllBtn,       3, 3);
183
184   aMedLayout->addWidget(myIdList,           5, 0, 4, 3);
185   aMedLayout->addWidget(mySelAllBtn,        5, 3);
186   aMedLayout->addWidget(myAddBtn,           6, 3);
187   aMedLayout->addWidget(myRemBtn,           7, 3);
188
189   aMedLayout->setColumnStretch(2, 5);
190   aMedLayout->setRowStretch(5, 5);
191   aMedLayout->setRowStretch(8, 5);
192
193   //filter group
194
195   myFilterGrp = new QGroupBox(tr("GEOM_FILTER"), centralWidget());
196   myLessFilterCheck = new QCheckBox(myFilterGrp);
197   myLessFilterCombo = new QComboBox(myFilterGrp);
198   myLessFilterCombo->addItem( tr("GEOM_LESS_THAN"), Filter_LT );
199   myLessFilterCombo->addItem( tr("GEOM_LESSOREQUAL_THAN"), Filter_LE );
200   myGreaterFilterCheck = new QCheckBox(myFilterGrp);
201   myGreaterFilterCombo = new QComboBox(myFilterGrp);
202   myGreaterFilterCombo->addItem( tr("GEOM_GREAT_THAN"), Filter_GT );
203   myGreaterFilterCombo->addItem( tr("GEOM_GREATOREQUAL_THAN"), Filter_GE );
204   myLessFilterSpin = new SalomeApp_DoubleSpinBox(myFilterGrp);
205   myGreaterFilterSpin = new SalomeApp_DoubleSpinBox(myFilterGrp);
206   myApplyFilterButton = new QPushButton(tr("GEOM_BUT_APPLY"), myFilterGrp);
207 #ifndef DISABLE_PLOT2DVIEWER
208   myPlotDistributionButton = new QPushButton(tr("GEOM_PLOT_DISTRIBUTION"), myFilterGrp);
209 #endif
210
211   QGridLayout* filterLayout = new QGridLayout(myFilterGrp);
212   filterLayout->addWidget(myLessFilterCheck,    0, 0);
213   filterLayout->addWidget(myLessFilterCombo,    0, 1);
214   filterLayout->addWidget(myLessFilterSpin,     0, 2);
215   filterLayout->addWidget(myGreaterFilterCheck, 1, 0);
216   filterLayout->addWidget(myGreaterFilterCombo, 1, 1);
217   filterLayout->addWidget(myGreaterFilterSpin,  1, 2);
218   filterLayout->addWidget(myApplyFilterButton,  0, 3);
219 #ifndef DISABLE_PLOT2DVIEWER
220   filterLayout->addWidget(myPlotDistributionButton,  1, 3);
221 #endif
222
223   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
224   layout->setMargin(0); layout->setSpacing(6);
225   layout->addWidget(GroupMedium);
226   layout->addWidget(myFilterGrp);
227
228   setHelpFileName("work_with_groups_page.html");
229
230   Init();
231   updateState();
232 }
233
234 GroupGUI_GroupDlg::~GroupGUI_GroupDlg()
235 {
236   GEOM_Displayer* aDisplayer = getDisplayer();
237   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
238   bool isHideObjects = resMgr->booleanValue( "Geometry", "hide_input_object", true);
239   if (myWasHiddenMain || ( isHideObjects && myIsAccept ) ) {
240     myIsHiddenMain = true;
241   }
242   else {
243     aDisplayer->Display(myMainObj);
244     myIsHiddenMain = false;
245   }
246   aDisplayer->Display(myGroup);
247   myDmMode = -1;
248 }
249
250 //=================================================================================
251 // function : Init()
252 // purpose  :
253 //=================================================================================
254 void GroupGUI_GroupDlg::Init()
255 {
256   // Get setting of step value from file configuration
257   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
258   double step = resMgr->doubleValue("Geometry", "SettingsGeomStep", 100);
259
260   // min, max, step and decimals for spin boxes
261   initSpinBox(myLessFilterSpin, 0., COORD_MAX, step, "length_precision" );
262   initSpinBox(myGreaterFilterSpin, 0., COORD_MAX, step, "length_precision" );
263   myLessFilterSpin->setValue( 0. );
264   myGreaterFilterSpin->setValue( 0. );
265
266   myDmMode = -1;
267   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
268
269   //unset shape type to avoid preparation of selection before exact user shape type selection
270   unsetConstructorId();
271   myIsShapeType = false;
272
273   if (myMode == CreateGroup) {
274     initName(tr("GROUP_PREFIX"));
275
276     // Get ready for main shape selection
277     myEditCurrentArgument = myMainName;
278
279     connect(this, SIGNAL(constructorsClicked(int)), this, SLOT(ConstructorsClicked(int)));
280     connect(mySelBtn,         SIGNAL(clicked()),    this, SLOT(SetEditCurrentArgument()));
281     connect(mySelBtn2,        SIGNAL(clicked()),    this, SLOT(SetEditCurrentArgument()));
282   }
283   else if (myMode == EditGroup) {
284     SALOME_ListIO aSelList;
285     aSelMgr->selectedObjects(aSelList);
286
287     if (aSelList.Extent()) {
288       GEOM::GEOM_Object_var anObj =
289         GEOMBase::ConvertIOinGEOMObject(aSelList.First());
290
291       if (!CORBA::is_nil(anObj) && anObj->GetType() == GEOM_GROUP) {
292         myGroup = anObj;
293
294         mainFrame()->ResultName->setText(GEOMBase::GetName(myGroup));
295
296         GEOM::GEOM_IGroupOperations_var anOper = GEOM::GEOM_IGroupOperations::_narrow(getOperation());
297         myMainObj = anOper->GetMainShape(myGroup);
298         if (!CORBA::is_nil(myMainObj)) {
299           myMainName->setText(GEOMBase::GetName(myMainObj));
300           SALOME_View* view = GEOM_Displayer::GetActiveView();
301           if (view) {
302             CORBA::String_var aMainEntry = myMainObj->GetStudyEntry();
303             Handle(SALOME_InteractiveObject) io =
304               new SALOME_InteractiveObject (aMainEntry.in(), "GEOM", "TEMP_IO");
305             if (view->isVisible(io)) myWasHiddenMain = false;
306           }
307         }
308
309         setShapeType((TopAbs_ShapeEnum)anOper->GetType(myGroup));
310
311         GEOM::ListOfLong_var aCurrList = anOper->GetObjects(myGroup);
312         for (int i = 0, n = aCurrList->length(); i < n; i++) {
313           QListWidgetItem* itm = new QListWidgetItem( QString( "%1" ).arg( aCurrList[ i ] ) );
314           myGroupIdList.append( aCurrList[ i ] );
315           itm->setTextColor( QColor( GROUP_IDLST_COLOR ) );
316           myIdList->addItem( itm );
317         }
318         myEditCurrentArgument = 0;
319       }
320       connect(mySelBtn2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
321     }
322   }
323
324   connect(aSelMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
325
326   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
327   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
328
329   connect(myRestrictGroup, SIGNAL(buttonClicked(int)),     this, SLOT(SetEditCurrentArgument()));
330   connect(mySelAllBtn,     SIGNAL(clicked()),              this, SLOT(selectAllSubShapes()));
331   connect(myAddBtn,        SIGNAL(clicked()),              this, SLOT(add()));
332   connect(myRemBtn,        SIGNAL(clicked()),              this, SLOT(remove()));
333   connect(myShowOnlyBtn,   SIGNAL(clicked()),              this, SLOT(showOnlySelected()));
334   connect(myHideSelBtn,    SIGNAL(clicked()),              this, SLOT(showOnlySelected()));
335   connect(myShowAllBtn,    SIGNAL(clicked()),              this, SLOT(showOnlySelected()));
336   connect(myIdList,        SIGNAL(itemSelectionChanged()), this, SLOT(selectionChanged()));
337
338   connect(myApplyFilterButton, SIGNAL(clicked()),         this, SLOT(ClickOnOkFilter()));
339 #ifndef DISABLE_PLOT2DVIEWER
340   connect(myPlotDistributionButton, SIGNAL(clicked()),    this, SLOT(ClickOnPlot()));
341 #endif
342   connect(myLessFilterCheck,   SIGNAL(stateChanged(int)), this, SLOT(MeasureToggled()));
343   connect(myGreaterFilterCheck,   SIGNAL(stateChanged(int)), this, SLOT(MeasureToggled()));
344
345   setInPlaceObj(GEOM::GEOM_Object::_nil());
346
347   myBusy = true; // just activate but do not select in the list
348   activateSelection();
349   myBusy = false;
350   MeasureToggled();
351 }
352
353 //=================================================================================
354 // function : enterEvent()
355 // purpose  :
356 //=================================================================================
357 void GroupGUI_GroupDlg::enterEvent(QEvent* e)
358 {
359   if (!buttonCancel()->isEnabled())
360     ActivateThisDialog();
361 }
362
363 //=================================================================================
364 //function : closeEvent
365 //purpose  : remove temporary geom object
366 //=================================================================================
367 void GroupGUI_GroupDlg::closeEvent(QCloseEvent* e)
368 {
369   setInPlaceObj(GEOM::GEOM_Object::_nil());
370   erasePreview(true);
371
372   GEOMBase_Skeleton::closeEvent(e);
373 }
374
375 //=================================================================================
376 // function : ClickOnOk()
377 // purpose  :
378 //=================================================================================
379 void GroupGUI_GroupDlg::ClickOnOk()
380 {
381   setIsApplyAndClose(true);
382   if (ClickOnApply())
383     ClickOnCancel();
384   setIsApplyAndClose(false);
385   getDisplayer()->UnsetDisplayMode();
386 }
387
388 //=================================================================================
389 // function : ClickOnApply()
390 // purpose  :
391 //=================================================================================
392 bool GroupGUI_GroupDlg::ClickOnApply()
393 {
394   if(!isApplyAndClose()) {
395     setIsDisableBrowsing( true );
396     setIsDisplayResult( false );
397   }
398     
399   myIsAccept = onAccept(myMode == CreateGroup, true, isApplyAndClose());
400   if (!myIsAccept)
401     return false;
402
403   if(!isApplyAndClose()) {
404     setIsDisableBrowsing( false );
405     setIsDisplayResult( true );
406   }
407
408   if (myMode == CreateGroup)
409   {
410     initName();
411     myIdList->clear();
412     ConstructorsClicked(getConstructorId());
413   }
414   else
415   {
416     int n = myIdList->count();
417     myGroupIdList.clear();
418     if (n > 0)
419     {
420       for (int i = 0; i < n; i++) {
421         QListWidgetItem* anItem  = myIdList->item( i );
422         myGroupIdList.append( anItem->text().toInt() );
423         anItem->setTextColor( GROUP_IDLST_COLOR );
424       }
425     }
426     activateSelection();
427   }
428   return true;
429 }
430
431 //=================================================================================
432 // function : ActivateThisDialog()
433 // purpose  :
434 //=================================================================================
435 void GroupGUI_GroupDlg::ActivateThisDialog()
436 {
437   GEOMBase_Skeleton::ActivateThisDialog();
438
439   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
440           this, SLOT(SelectionIntoArgument()));
441
442   activateSelection();
443 }
444
445 //=================================================================================
446 // function : SetEditCurrentArgument()
447 // purpose  :
448 //=================================================================================
449 void GroupGUI_GroupDlg::SetEditCurrentArgument()
450 {
451   QPushButton* send = qobject_cast<QPushButton*>( sender() );
452
453   if (send == mySelBtn) {
454     myEditCurrentArgument = myMainName;
455     myShape2Name->setText("");
456     mySelBtn->setDown(true);
457     mySelBtn2->setDown(false);
458   }
459   else if (send == mySelBtn2 || sender() == myRestrictGroup) {
460     setInPlaceObj(GEOM::GEOM_Object::_nil());
461     myShape2Name->setText("");
462     if ( send == mySelBtn2 ) {
463       mySelBtn2->setDown(true);
464       mySelBtn->setDown(false);
465     }
466   if (subSelectionWay() != ALL_SUBSHAPES) {
467       myEditCurrentArgument = myShape2Name;
468     }
469     else {
470       myEditCurrentArgument = 0;
471     }
472   }
473
474   //  activateSelection();
475   if(myEditCurrentArgument) {
476     myEditCurrentArgument->setFocus();
477     if ( send )
478       send->setDown(true);
479   }
480
481   updateState();
482 }
483
484 //=================================================================================
485 // function : onGetInPlace()
486 // purpose  :
487 //=================================================================================
488 void GroupGUI_GroupDlg::onGetInPlace()
489 {
490   setInPlaceObj(GEOM::GEOM_Object::_nil());
491   myEditCurrentArgument->setText("");
492
493   bool isBlocked = myIdList->signalsBlocked();
494   myIdList->blockSignals(true);
495   myIdList->clearSelection();
496   myIdList->blockSignals(isBlocked);
497
498   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
499   SALOME_ListIO aSelList;
500   aSelMgr->selectedObjects(aSelList);
501
502   if (aSelList.Extent() != 1)
503     return;
504
505   GEOM::GEOM_Object_var anObj =
506     GEOMBase::ConvertIOinGEOMObject(aSelList.First());
507   if (GEOMBase::IsShape(anObj)) {
508     if (!anObj->_is_equivalent(myMainObj) && !anObj->_is_equivalent(myGroup)) {
509       SUIT_OverrideCursor wc;
510       myEditCurrentArgument->setText(GEOMBase::GetName(anObj));
511       GEOM::GEOM_IShapesOperations_var aShapesOp =
512         getGeomEngine()->GetIShapesOperations(getStudyId());
513       if (subSelectionWay() == GET_IN_PLACE) {
514         GEOM::GEOM_Object_var aGetInPlaceObj = aShapesOp->GetInPlace(myMainObj, anObj);
515         setInPlaceObj(aGetInPlaceObj);
516       }
517       else {
518         bool isVisible = true;
519         if (SALOME_View* view = GEOM_Displayer::GetActiveView())
520           isVisible = view->isVisible(aSelList.First());
521         setInPlaceObj(anObj, isVisible);
522       }
523       myEditCurrentArgument = 0;
524       //myBusy = true; // just activate but do not select in the list
525       activateSelection();
526       //myBusy = false;
527     }
528   }
529 }
530
531 //=================================================================================
532 //function : setInPlaceObj
533 //purpose  : temporarily add an object to study and remove old InPlaceObj
534 //=================================================================================
535 void GroupGUI_GroupDlg::setInPlaceObj(GEOM::GEOM_Object_var theObj, const bool isVisible)
536 {
537   if (!myInPlaceObj->_is_equivalent(theObj))
538   {
539     myInPlaceObj = theObj;
540   }
541
542   // build map of indices
543   myMain2InPlaceIndices.Clear();
544   if (!myInPlaceObj->_is_nil()) {
545     GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations(getStudyId());
546     GEOM::GEOM_ILocalOperations_var aLocOp = getGeomEngine()->GetILocalOperations(getStudyId());
547
548     GEOM::ListOfGO_var aSubObjects = aShapesOp->MakeExplode(myInPlaceObj, getShapeType(), false);
549     for ( int i = 0; i < aSubObjects->length(); i++ )
550     {
551       GEOM::ListOfLong_var aCurrList = aShapesOp->GetSameIDs( myMainObj, aSubObjects[i] );
552       if( aCurrList->length() > 1 ) {
553         //rnv : To Fix the 21561: EDF 2184 GEOM: Group with second shape restriction.
554         //      In case if GetSameIDs(...) method return more then one ID use 
555         //      GetSharedShapes(...) method to get sub-shapes of the second shape.
556         GEOM::ListOfGO_var aSubObjects2 = aShapesOp->GetSharedShapes( myMainObj, aSubObjects[i], getShapeType() );
557         for( int j = 0; j < aSubObjects2->length(); j++ ) {
558           CORBA::Long aMainIndex =  aLocOp->GetSubShapeIndex( myMainObj, aSubObjects2[j] );
559           CORBA::Long aPlaceIndex = aLocOp->GetSubShapeIndex( myInPlaceObj, aSubObjects[i]);
560           if ( aMainIndex >= 0 && aPlaceIndex > 0 ) {
561             myMain2InPlaceIndices.Bind( aMainIndex, aPlaceIndex );
562           }
563         }
564       } else if(aCurrList->length() > 0 ) {
565         CORBA::Long aMainIndex = aCurrList[0];
566         CORBA::Long aPlaceIndex = aLocOp->GetSubShapeIndex( myInPlaceObj, aSubObjects[i] );
567         if ( aMainIndex >= 0 && aPlaceIndex > 0) {
568           myMain2InPlaceIndices.Bind( aMainIndex, aPlaceIndex );
569         }
570       }
571     }
572   }
573   myInPlaceObjSelectState = subSelectionWay();
574   if (myInPlaceObjSelectState == SUBSHAPES_OF_SHAPE2 && !isVisible)
575     myInPlaceObjSelectState = SUBSHAPES_OF_INVISIBLE_SHAPE2;
576 }
577
578 //=================================================================================
579 // function : SelectionIntoArgument()
580 // purpose  : Called when selection has changed
581 //=================================================================================
582 void GroupGUI_GroupDlg::SelectionIntoArgument()
583 {
584   if (subSelectionWay() != ALL_SUBSHAPES && myEditCurrentArgument == myShape2Name) {
585     onGetInPlace();
586     if( !myInPlaceObj->_is_nil() ) {
587       mySelBtn2->setDown(false);
588     }
589     return;
590   }
591
592   if (myEditCurrentArgument == myMainName) {  // Selection of a main shape is active
593     myEditCurrentArgument->setText("");
594     myIdList->clear();
595
596     LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
597     SALOME_ListIO aSelList;
598     aSelMgr->selectedObjects(aSelList);
599     int nbSel = aSelList.Extent();
600
601     if (nbSel == 1) {
602       GEOM::GEOM_Object_var anObj =
603         GEOMBase::ConvertIOinGEOMObject(aSelList.First());
604
605       if (GEOMBase::IsShape(anObj)) {
606         if (myIsHiddenMain) {
607           GEOM_Displayer* aDisplayer = getDisplayer();
608           aDisplayer->Display(myMainObj);
609           myIsHiddenMain = false;
610         }
611         myMainObj = anObj;
612         if (!CORBA::is_nil(myMainObj)) {
613           mySelBtn->setDown(false);
614           SALOME_View* view = GEOM_Displayer::GetActiveView();
615           if (view) {
616             CORBA::String_var aMainEntry = myMainObj->GetStudyEntry();
617             Handle(SALOME_InteractiveObject) io =
618               new SALOME_InteractiveObject (aMainEntry.in(), "GEOM", "TEMP_IO");
619             if (view->isVisible(io)) myWasHiddenMain = false;
620           }
621         }
622         myEditCurrentArgument->setText(GEOMBase::GetName(anObj));
623         // activate sub-shapes selection by default
624         myEditCurrentArgument = 0;
625         activateSelection();
626         updateState();
627       }
628     }
629     else {
630       if (myIsHiddenMain) {
631         GEOM_Displayer* aDisplayer = getDisplayer();
632         aDisplayer->Display(myMainObj);
633         myIsHiddenMain = false;
634       }
635       myMainObj = GEOM::GEOM_Object::_nil();
636     }
637   }
638   else { // an attempt to synchronize list box selection with 3d viewer
639     if (myBusy || myMainObj->_is_nil()) {
640       return;
641     }
642
643     bool isBlocked = myIdList->signalsBlocked();
644     myIdList->blockSignals(true);
645     myIdList->clearSelection();
646
647     TColStd_IndexedMapOfInteger aMapIndex;
648     int nbSel = getSelectedSubshapes(aMapIndex);
649
650     if (nbSel) {
651       QMap<int, int> aMap;
652       for (int i = 0, n = myIdList->count(); i < n; i++)
653         aMap.insert(myIdList->item(i)->text().toInt(), i);
654
655       bool highlight = false;
656       for (int ii = 1, nn = aMapIndex.Extent(); ii <= nn; ii++) {
657         if (aMap.contains(aMapIndex(ii))) {
658           myIdList->item(aMap[aMapIndex(ii)])->setSelected(true);
659           highlight = true;
660         }
661       }
662       if (highlight)
663         highlightSubShapes();
664     }
665     myIdList->blockSignals(isBlocked);
666
667     updateState(nbSel);
668   }
669 }
670
671 //=================================================================================
672 // function : ConstructorsClicked()
673 // purpose  : Radio button management
674 //=================================================================================
675 void GroupGUI_GroupDlg::ConstructorsClicked(int constructorId)
676 {
677   if (getConstructorId() != constructorId)
678     setConstructorId(constructorId);
679
680   myIsShapeType = true;
681   myIdList->clear();
682   myEditCurrentArgument = 0;
683
684   setInPlaceObj(myInPlaceObj); // to rebuild myMain2InPlaceIndices
685   activateSelection();
686   updateState();
687 }
688
689 //=================================================================================
690 // function : selectAllSubShapes
691 // purpose  :
692 //=================================================================================
693 void GroupGUI_GroupDlg::selectAllSubShapes()
694 {
695   if (CORBA::is_nil(myMainObj) || !myIsShapeType)
696     return;
697
698   GEOM::ListOfLong_var aSubShapes;
699   GEOM::GEOM_IShapesOperations_var aShOp = getGeomEngine()->GetIShapesOperations(getStudyId());
700   aSubShapes = aShOp->SubShapeAllIDs(myMainObj, getShapeType(), false);
701   if (aSubShapes->length() > 0) {
702     if (subSelectionWay() == ALL_SUBSHAPES)
703     {
704 //      myIdList->clear(); // for sorted final list?
705
706       if (!aShOp->IsDone())
707         return;
708     }
709     else
710     {
711       aSubShapes = new GEOM::ListOfLong();
712       aSubShapes->length(myMain2InPlaceIndices.Extent());
713       TColStd_DataMapIteratorOfDataMapOfIntegerInteger m2ip (myMain2InPlaceIndices);
714       for (int i = 0; m2ip.More(); i++, m2ip.Next())
715         aSubShapes[i] = m2ip.Key();
716     }
717
718     bool isBlocked = myIdList->signalsBlocked();
719     myIdList->blockSignals(true);
720
721     for (int i = 0, n = aSubShapes->length(); i < n; i++) {
722       CORBA::Long anIndex = aSubShapes[i];
723       if (anIndex < 0)
724         continue;
725
726       QListWidgetItem* anItem = 0;
727       QString text = QString("%1").arg(anIndex);
728 //      if (!myInPlaceObj->_is_nil()) {
729         QList<QListWidgetItem*> found = myIdList->findItems(text, Qt::MatchExactly);
730         if (found.count()) anItem = found[0];
731 //      }
732       if (!anItem) {
733         anItem = new QListWidgetItem(text);
734         anItem->setTextColor( myGroupIdList.contains(anIndex) ? QColor( GROUP_IDLST_COLOR ) : QColor( GROUP_NEWIDLST_COLOR ) );
735         myIdList->addItem(anItem);
736       }
737       anItem->setSelected(true);
738     }
739
740     myIdList->blockSignals(isBlocked);
741     highlightSubShapes();
742   }
743 }
744
745 //=================================================================================
746 // function : showOnlySelected
747 // purpose  :
748 //=================================================================================
749 void GroupGUI_GroupDlg::showOnlySelected()
750 {
751   if (CORBA::is_nil(myMainObj) || !myIsShapeType)
752     return;
753
754   QPushButton* send = (QPushButton*)sender();
755   if (send == myShowAllBtn) {
756     activateSelection();
757     return;
758   }
759
760   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
761   SALOME_ListIO aSelList;
762   aSelMgr->selectedObjects(aSelList);
763
764   GEOM_Displayer* aDisplayer = getDisplayer();
765
766   if (send == myHideSelBtn) {
767     aDisplayer->Erase(aSelList, /*forced=*/false, /*updateViewer=*/true);
768   }
769   else {
770     // Mantis issue 0021421: do not hide main shape, if explode on VERTEX
771     SALOME_View* view = GEOM_Displayer::GetActiveView();
772     if (view) {
773       CORBA::String_var aMainEntry = myMainObj->GetStudyEntry();
774       Handle(SALOME_InteractiveObject) io =
775         new SALOME_InteractiveObject (aMainEntry.in(), "GEOM", "TEMP_IO");
776       if (view->isVisible(io)) myIsHiddenMain = true;
777       
778       //keep the selected entry and IO in the map for checking
779       std::map<QString, Handle(SALOME_InteractiveObject)> aSelEntriesMap;
780       SALOME_ListIteratorOfListIO aSelIt(aSelList);
781       for ( ; aSelIt.More(); aSelIt.Next() ) {
782         Handle(SALOME_InteractiveObject) anSelIO = aSelIt.Value();
783         aSelEntriesMap[anSelIO->getEntry()] = anSelIO;
784       }
785       //get the displayed sub-shapes
786       SALOME_ListIO displayed;
787       view->GetVisible(displayed);
788       // Erase all, except the selected sub-shapes
789       std::map<QString, Handle(SALOME_InteractiveObject)>::iterator 
790         aSelDispIter = aSelEntriesMap.end();
791       SALOME_ListIteratorOfListIO aDispIt( displayed );
792       for ( ; aDispIt.More(); aDispIt.Next() ) {
793         Handle(SALOME_InteractiveObject) anIO = aDispIt.Value();
794         aSelDispIter = aSelEntriesMap.find( anIO->getEntry() );
795         if ( aSelDispIter != aSelEntriesMap.end() ) {
796           //sub-shape is selected, so erase it's record from map to keep in it not displayed, but selected sub-shapes only
797           aSelEntriesMap.erase(aSelDispIter);
798         } else {
799           //sub-shape is not in the map of selected, then erase it from view
800           aDisplayer->Erase( anIO, /*forced = */false, /*updateViewer = */false );
801         }
802       }
803
804       if ( !aSelEntriesMap.empty() ) {
805         // Build a presentation of the selected, but not displayed sub-shapes
806         TopoDS_Shape aMainShape = GEOM_Client::get_client().GetShape(GeometryGUI::GetGeomGen(), myMainObj);
807         TopTools_IndexedMapOfShape aSubShapesMap;
808         TopExp::MapShapes(aMainShape, aSubShapesMap);
809         QString anEntryBase = aMainEntry.in();
810
811         TopExp_Explorer anExp (aMainShape, getShapeType());
812         for (; anExp.More(); anExp.Next()) {
813           TopoDS_Shape aSubShape = anExp.Current();
814           int index = aSubShapesMap.FindIndex(aSubShape);
815           QString anEntry = QString( "TEMP_" ) + anEntryBase + QString("_%1").arg(index);
816           if ( aSelEntriesMap.find( anEntry ) == aSelEntriesMap.end() ) {
817             //skip not selected sub-shapes
818             continue;
819           }
820           SALOME_Prs* aPrs = aDisplayer->buildSubshapePresentation(aSubShape, anEntry, view);
821           if (aPrs) {
822             displayPreview(aPrs, true, false); // append, do not update
823           }
824         }
825       }
826       aDisplayer->UpdateViewer();
827     }
828
829     // Mantis issue 0021421: do not hide main shape, if explode on VERTEX
830     if (getShapeType() == TopAbs_VERTEX && myIsHiddenMain) {
831       aDisplayer->Display(myMainObj);
832     }
833
834     // for the case when selected ids were not displayed in the viewer: Mantis issue 0021367
835     highlightSubShapes();
836   }
837 }
838
839 //=================================================================================
840 // function : getSelectedSubshapes
841 // purpose  :
842 //=================================================================================
843 int GroupGUI_GroupDlg::getSelectedSubshapes (TColStd_IndexedMapOfInteger& theMapIndex)
844 {
845   theMapIndex.Clear();
846
847   SalomeApp_Application* app = myGeomGUI->getApp();
848   if (!app || myMainObj->_is_nil())
849     return 0;
850
851   LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
852   SALOME_ListIO aSelList;
853   aSelMgr->selectedObjects(aSelList);
854
855   // try to find out and process the global selection
856   // (of not published objects and of published sub-shapes)
857   {
858     SALOME_ListIteratorOfListIO anIter (aSelList);
859     for (int i = 0; anIter.More(); anIter.Next(), i++)
860     {
861       Handle(SALOME_InteractiveObject) anIObj = anIter.Value();
862       QString anEntry = anIObj->getEntry();
863       QString str = "_";
864       int index = anEntry.lastIndexOf(str);
865       if (index > 0) // selection among special preview
866       {
867         anEntry.remove(0, index+1);
868         int anIndex = anEntry.toInt();
869         if (anIndex)
870           theMapIndex.Add(anIndex);
871       }
872       else // selection among published shapes
873       {
874         SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
875         if (!appStudy) return 0;
876         _PTR(Study) aStudy = appStudy->studyDS();
877
878         _PTR(SObject) aSObj (aStudy->FindObjectID(anEntry.toLatin1().constData()));
879         GEOM::GEOM_Object_var aGeomObj =
880           GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(aSObj));
881         TopoDS_Shape aShape;
882         if (GEOMBase::GetShape(aGeomObj, aShape)) {
883           if (aGeomObj->GetType() == GEOM_GROUP || aShape.ShapeType() == getShapeType()) {
884             if (subSelectionWay() != ALL_SUBSHAPES &&
885                 GEOMBase::GetName(aGeomObj) == myShape2Name->text()) {
886               // Skip selected in place object.
887               continue;
888             }
889
890             TopTools_IndexedMapOfShape aMainMap;
891             TopoDS_Shape aMainShape = GEOM_Client::get_client().GetShape(GeometryGUI::GetGeomGen(), myMainObj);
892             TopExp::MapShapes(aMainShape, aMainMap);
893
894             TopExp_Explorer anExp (aShape, getShapeType());
895             bool isShowWarning = true;
896             for (; anExp.More(); anExp.Next()) {
897               TopoDS_Shape aSubShape = anExp.Current();
898               int anIndex = aMainMap.FindIndex(aSubShape);
899               if (anIndex == 0) {
900                 if (isShowWarning) {
901                   SUIT_MessageBox::warning(app->desktop(), QObject::tr("WRN_WARNING"),
902                                          tr("WRN_NOT_SUBSHAPE"));
903                   isShowWarning = false;
904                 }
905               }
906               else {
907                 if (subSelectionWay() != ALL_SUBSHAPES &&
908                     !myMain2InPlaceIndices.IsBound(anIndex))
909                   continue;
910                 theMapIndex.Add(anIndex);
911               }
912             }
913           }
914         }
915       }
916     } // for aSelList
917   }
918
919   return theMapIndex.Extent();
920 }
921
922 //=================================================================================
923 // function : add
924 // purpose  :
925 //=================================================================================
926 void GroupGUI_GroupDlg::add()
927 {
928   TColStd_IndexedMapOfInteger aMapIndex;
929   int nbSel = getSelectedSubshapes(aMapIndex);
930
931   TColStd_MapOfInteger aMap;
932   for (int i = 0, n = myIdList->count(); i < n; i++)
933     aMap.Add(myIdList->item(i)->text().toInt());
934
935   if (nbSel > 0) {
936     bool isBlocked = myIdList->signalsBlocked();
937     myIdList->blockSignals(true);
938
939     for (int i = 1, n = aMapIndex.Extent(); i <= n; i++) {
940       if (aMap.Contains(aMapIndex(i)))
941         continue;
942
943       QListWidgetItem* anItem = new QListWidgetItem(QString("%1").arg(aMapIndex(i)));
944       anItem->setTextColor( myGroupIdList.contains( aMapIndex( i ) ) ? QColor( GROUP_IDLST_COLOR ) : QColor( GROUP_NEWIDLST_COLOR ) );
945       myIdList->addItem(anItem);
946       anItem->setSelected(true);
947     }
948
949     myIdList->blockSignals(isBlocked);
950   }
951
952   updateState();
953 }
954
955 //=================================================================================
956 // function : remove
957 // purpose  :
958 //=================================================================================
959 void GroupGUI_GroupDlg::remove()
960 {
961   bool isBlocked = myIdList->signalsBlocked();
962   myIdList->blockSignals(true);
963
964   QListIterator<QListWidgetItem*> it (myIdList->selectedItems());
965   while (it.hasNext())
966     delete it.next();
967
968   myIdList->blockSignals(isBlocked);
969
970   highlightSubShapes();
971 }
972
973 //=================================================================================
974 //function : subSelectionWay
975 //purpose  :
976 //=================================================================================
977 int GroupGUI_GroupDlg::subSelectionWay() const
978 {
979   return myRestrictGroup->checkedId();
980 }
981
982 //=================================================================================
983 // function : getShapeType()
984 // purpose  :
985 //=================================================================================
986 TopAbs_ShapeEnum GroupGUI_GroupDlg::getShapeType() const
987 {
988   switch (getConstructorId()) {
989   case 0:  return TopAbs_VERTEX;
990   case 1:  return TopAbs_EDGE;
991   case 2:  return TopAbs_FACE;
992   case 3:  return TopAbs_SOLID;
993   default: return TopAbs_SHAPE;
994   }
995 }
996
997 //=================================================================================
998 // function : setShapeType()
999 // purpose  :
1000 //=================================================================================
1001 void GroupGUI_GroupDlg::setShapeType(const TopAbs_ShapeEnum theType)
1002 {
1003   int anId = 0;
1004   switch (theType) {
1005   case TopAbs_VERTEX: anId = 0; break;
1006   case TopAbs_EDGE:   anId = 1; break;
1007   case TopAbs_FACE:   anId = 2; break;
1008   case TopAbs_SOLID:  anId = 3; break;
1009   }
1010   setConstructorId(anId);
1011   if (!myIsShapeType)
1012   {
1013     myIsShapeType = true;
1014     // workaround to avoid set checked button 0
1015     setConstructorId(anId);
1016   }
1017 }
1018
1019 //=================================================================================
1020 // function : activateSelection
1021 // purpose  : Activate selection in accordance with myEditCurrentArgument
1022 //=================================================================================
1023 void GroupGUI_GroupDlg::activateSelection()
1024 {
1025   bool isApply = ((QPushButton*)sender() == buttonApply());
1026   if(!isApplyAndClose())
1027     erasePreview(false);
1028
1029   // local selection
1030   if (!myMainObj->_is_nil() &&
1031       !myEditCurrentArgument &&
1032       myIsShapeType) // check if shape type is already choosen by user
1033   {
1034     GEOM_Displayer* aDisplayer = getDisplayer();
1035
1036     //display mode for main shape
1037     if ( myDmMode == -1 ) {
1038     SALOME_View* view = GEOM_Displayer::GetActiveView();
1039       if (view) {
1040         CORBA::String_var aMainEntry = myMainObj->GetStudyEntry();
1041         Handle(SALOME_InteractiveObject) io =
1042           new SALOME_InteractiveObject (aMainEntry.in(), "GEOM", "TEMP_IO");
1043         if ( view->isVisible( io ) ) {
1044           Handle(GEOM_AISShape) aSh = GEOMBase::ConvertIOinGEOMAISShape( io, true );
1045           if(!aSh.IsNull()) {
1046             myDmMode = aSh->isTopLevel() ? aSh->prevDisplayMode() : aSh->DisplayMode();
1047           }
1048         }
1049         else
1050           myDmMode = SUIT_Session::session()->resourceMgr()->integerValue( "Geometry", "display_mode" );
1051       }
1052     }
1053     aDisplayer->SetDisplayMode(myDmMode);
1054
1055     // Mantis issue 0021421: do not hide main shape, if explode on VERTEX
1056     if(getShapeType() != TopAbs_VERTEX) {
1057       aDisplayer->Erase(myMainObj, false, false);
1058       myIsHiddenMain = true;
1059     }
1060     else
1061       aDisplayer->Display(myMainObj);
1062
1063     aDisplayer->Erase(myGroup, false, false);
1064
1065     QColor aColor = SUIT_Session::session()->resourceMgr()->colorValue( "Geometry", "editgroup_color" );
1066     Quantity_NameOfColor aCol = SalomeApp_Tools::color( aColor ).Name();
1067
1068     if(!isApplyAndClose()) {
1069       SUIT_ViewWindow* aViewWindow = 0;
1070       SUIT_Study* activeStudy = SUIT_Session::session()->activeApplication()->activeStudy();
1071       if (activeStudy)
1072         aViewWindow = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
1073       if (aViewWindow == 0) return;
1074
1075       SUIT_ViewManager* aViewManager = aViewWindow->getViewManager();
1076       if (aViewManager->getType() != OCCViewer_Viewer::Type() &&
1077           aViewManager->getType() != SVTK_Viewer::Type())
1078         return;
1079
1080       SUIT_ViewModel* aViewModel = aViewManager->getViewModel();
1081       SALOME_View* aView = dynamic_cast<SALOME_View*>(aViewModel);
1082       if (aView == 0) return;
1083
1084       TopoDS_Shape aMainShape = GEOM_Client::get_client().GetShape(GeometryGUI::GetGeomGen(), myMainObj);
1085       TopoDS_Shape aRestrictionShape;
1086
1087       if (subSelectionWay() == ALL_SUBSHAPES) {
1088         aRestrictionShape = aMainShape;
1089
1090         TopTools_IndexedMapOfShape aSubShapesMap;
1091         TopExp::MapShapes(aMainShape, aSubShapesMap);
1092         CORBA::String_var aMainEntry = myMainObj->GetStudyEntry();
1093         QString anEntryBase = aMainEntry.in();
1094
1095         TopExp_Explorer anExp (aRestrictionShape, getShapeType());
1096         for (; anExp.More(); anExp.Next()) {
1097           TopoDS_Shape aSubShape = anExp.Current();
1098           int index = aSubShapesMap.FindIndex(aSubShape);
1099           QString anEntry = QString( "TEMP_" ) + anEntryBase + QString("_%1").arg(index);
1100           Handle(SALOME_InteractiveObject) io =
1101             new SALOME_InteractiveObject(anEntry.toLatin1(), "GEOM", "TEMP_IO");
1102           if ( myGroupIdList.contains( index ) ) {
1103             aDisplayer->SetColor( aCol );
1104           }
1105           else {
1106             aDisplayer->UnsetColor();
1107           }
1108           SALOME_Prs* aPrs = aDisplayer->buildSubshapePresentation(aSubShape, anEntry, aView);
1109           if (aPrs) {
1110             displayPreview(aPrs, true, false); // append, do not update
1111             // TODO: map or delete Prs
1112           }
1113         }
1114       }
1115       else if (!myInPlaceObj->_is_nil()) {
1116         TopTools_IndexedMapOfShape aSubShapesMap;
1117         TopExp::MapShapes(aMainShape, aSubShapesMap);
1118         CORBA::String_var aMainEntry = myMainObj->GetStudyEntry();
1119         QString anEntryBase = aMainEntry.in();
1120
1121         TColStd_DataMapIteratorOfDataMapOfIntegerInteger aM2IPit (myMain2InPlaceIndices);
1122         for (; aM2IPit.More(); aM2IPit.Next()) {
1123           int index = aM2IPit.Key();
1124           TopoDS_Shape aSubShape = aSubShapesMap.FindKey(index);
1125           QString anEntry = QString( "TEMP_" ) + anEntryBase + QString("_%1").arg(index);
1126
1127           if ( myGroupIdList.contains( index ) ) {
1128             aDisplayer->SetColor( aCol );
1129           }
1130           else {
1131             aDisplayer->UnsetColor();
1132           }
1133
1134           SALOME_Prs* aPrs = aDisplayer->buildSubshapePresentation(aSubShape, anEntry, aView);
1135           if (aPrs) {
1136             displayPreview(aPrs, true, false); // append, do not update
1137           }
1138         }
1139       }
1140       else ;
1141       aDisplayer->UnsetDisplayMode();
1142       aDisplayer->UnsetColor();
1143       aDisplayer->UpdateViewer();
1144     }
1145   }
1146
1147   globalSelection(GEOM_ALLSHAPES);
1148
1149   SelectionIntoArgument();
1150 }
1151
1152 //=================================================================================
1153 // function : updateState
1154 // purpose  :
1155 //=================================================================================
1156 void GroupGUI_GroupDlg::updateState (bool isAdd)
1157 {
1158   myAddBtn->setEnabled(!myEditCurrentArgument && !CORBA::is_nil(myMainObj) && isAdd);
1159   //myShowOnlyBtn->setEnabled(!myEditCurrentArgument && !CORBA::is_nil(myMainObj) && isAdd);
1160
1161   bool hasSel = myIdList->selectedItems().count() > 0;
1162
1163   myRemBtn->setEnabled(hasSel);
1164   myRestrictGroupBox->setEnabled(!CORBA::is_nil(myMainObj));
1165   mySelAllBtn->setEnabled(!CORBA::is_nil(myMainObj));
1166   
1167   mySelBtn2->setEnabled(subSelectionWay() != ALL_SUBSHAPES);
1168   myShape2Name->setEnabled(subSelectionWay() != ALL_SUBSHAPES);
1169   myFilterGrp->setEnabled(!CORBA::is_nil(myMainObj) &&
1170                           subSelectionWay() == ALL_SUBSHAPES &&
1171                           myIsShapeType &&
1172                           getShapeType() != TopAbs_VERTEX);
1173   // manage of 'Plot' button access
1174   GEOM::GEOM_IShapesOperations_var aShOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
1175   GEOM::ListOfLong_var aSubShapes = aShOp->SubShapeAllIDs( myMainObj, getShapeType(), false );
1176   bool hasCurrentEntities = aSubShapes->length() > 0;
1177 #ifndef DISABLE_PLOT2DVIEWER
1178   myPlotDistributionButton->setEnabled( myFilterGrp->isEnabled() &&
1179                                         myIsShapeType &&
1180                                         ( getShapeType() == TopAbs_EDGE || 
1181                                           getShapeType() == TopAbs_FACE ||
1182                                           getShapeType() == TopAbs_SOLID ) &&
1183                                         hasCurrentEntities );
1184 #endif
1185   if (subSelectionWay() == ALL_SUBSHAPES)
1186     setInPlaceObj(GEOM::GEOM_Object::_nil());
1187 }
1188
1189 //=================================================================================
1190 // function : selectionChanged
1191 // purpose  :
1192 //=================================================================================
1193 void GroupGUI_GroupDlg::selectionChanged()
1194 {
1195   highlightSubShapes();
1196 }
1197
1198 //=================================================================================
1199 // function : highlightSubShapes
1200 // purpose  :
1201 //=================================================================================
1202 void GroupGUI_GroupDlg::highlightSubShapes()
1203 {
1204   if (CORBA::is_nil(myMainObj))
1205     return;
1206
1207   TColStd_MapOfInteger anIds;
1208
1209   myBusy = true;
1210
1211   int ii = 0, nn = myIdList->count();
1212   for (; ii < nn; ii++)
1213   {
1214     if (myIdList->item(ii)->isSelected()) {
1215       int id = myIdList->item(ii)->text().toInt();
1216       if (subSelectionWay() != ALL_SUBSHAPES &&
1217           !myMain2InPlaceIndices.IsBound(id)) {
1218         //myIdList->item(ii)->setSelected(false);
1219       }
1220       else {
1221         anIds.Add(id);
1222       }
1223     }
1224   }
1225   SalomeApp_Application* app = myGeomGUI->getApp();
1226   LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
1227   aSelMgr->clearSelected();
1228
1229   SUIT_ViewWindow* aViewWindow = 0;
1230   SUIT_Study* activeStudy = app->activeStudy();
1231   if (activeStudy)
1232     aViewWindow = app->desktop()->activeWindow();
1233   if (aViewWindow == 0) return;
1234
1235   SUIT_ViewManager* aViewManager = aViewWindow->getViewManager();
1236   if (aViewManager->getType() != OCCViewer_Viewer::Type() &&
1237       aViewManager->getType() != SVTK_Viewer::Type())
1238     return;
1239
1240   SUIT_ViewModel* aViewModel = aViewManager->getViewModel();
1241   SALOME_View* aView = dynamic_cast<SALOME_View*>(aViewModel);
1242   if (aView == 0) return;
1243
1244   // TODO??: use here GEOMBase_Helper::myPreview instead of ic->DisplayedObjects()
1245
1246   OCCViewer_Viewer* v3d = ((OCCViewer_ViewManager*)aViewManager)->getOCCViewer();
1247   Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
1248   AIS_ListOfInteractive List;
1249   //ic->DisplayedObjects(List);
1250   ic->ObjectsInside(List); // Mantis issue 0021367
1251
1252   SALOME_ListIO aSelList;
1253
1254   // To highlight the selected sub-shape in Object Browser, if it's already published under the main shape
1255   GEOM::GEOM_ILocalOperations_var aLocOp = getGeomEngine()->GetILocalOperations(getStudyId());
1256   QMap<int, QString> childsMap;
1257   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
1258   if (appStudy) {
1259     _PTR(Study) aStudy = appStudy->studyDS();
1260     CORBA::String_var aMainEntry = myMainObj->GetStudyEntry();
1261     QString anEntry = aMainEntry.in();
1262     _PTR(SObject) aSObj (aStudy->FindObjectID(anEntry.toLatin1().constData()));
1263     _PTR(ChildIterator) anIt (aStudy->NewChildIterator(aSObj));
1264     for (anIt->InitEx(true); anIt->More(); anIt->Next()) {
1265       GEOM::GEOM_Object_var aChild = GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(anIt->Value()));
1266       if (!CORBA::is_nil(aChild)) {
1267         int index = aLocOp->GetSubShapeIndex(myMainObj, aChild);
1268         CORBA::String_var aChildEntry = aChild->GetStudyEntry();
1269         QString anEntry = aChildEntry.in();
1270         childsMap.insert(index, anEntry);
1271       }
1272     }
1273   }
1274
1275   AIS_ListIteratorOfListOfInteractive ite (List);
1276   for (; ite.More(); ite.Next()) {
1277     if (ite.Value()->IsInstance(STANDARD_TYPE(GEOM_AISShape))) {
1278       Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(ite.Value());
1279       if (aSh->hasIO()) {
1280         Handle(SALOME_InteractiveObject) anIO = aSh->getIO();
1281         QString anEntry = anIO->getEntry();
1282         int index = anEntry.lastIndexOf("_");
1283         anEntry.remove(0, index+1);
1284         int anIndex = anEntry.toInt();
1285         if (anIds.Contains(anIndex)) {
1286           aSelList.Append(anIO);
1287           if (childsMap.contains (anIndex)) {
1288             Handle(SALOME_InteractiveObject) tmpIO = new SALOME_InteractiveObject(childsMap.value(anIndex).toLatin1().constData(), "GEOM", "TEMP_IO");
1289             aSelList.Append(tmpIO);
1290           }
1291         }
1292       }
1293     }
1294   }
1295   aSelMgr->setSelectedObjects(aSelList);
1296
1297   myBusy = false;
1298
1299   if (nn < 3000)
1300     updateState(aSelList.Extent() > 0);
1301   else {
1302     myAddBtn->setEnabled(true);
1303     myAddBtn->setEnabled(true);
1304     myRemBtn->setEnabled(true);
1305   }
1306 }
1307
1308 //=================================================================================
1309 // function : createOperation
1310 // purpose  :
1311 //=================================================================================
1312 GEOM::GEOM_IOperations_ptr GroupGUI_GroupDlg::createOperation()
1313 {
1314   return getGeomEngine()->GetIGroupOperations(getStudyId());
1315 }
1316
1317 #define RETURN_WITH_MSG(a, b) \
1318   if (!(a)) { \
1319     theMessage += (b); \
1320     return false; \
1321   }
1322
1323 //=================================================================================
1324 // function : isValid()
1325 // purpose  : Verify validity of input data
1326 //=================================================================================
1327 bool GroupGUI_GroupDlg::isValid(QString& theMessage)
1328 {
1329   SalomeApp_Study* study = getStudy();
1330   ASSERT(study);
1331   RETURN_WITH_MSG  (!study->studyDS()->GetProperties()->IsLocked(), tr("GEOM_STUDY_LOCKED"))
1332
1333   if (myMode == CreateGroup) {
1334     RETURN_WITH_MSG(!CORBA::is_nil(myMainObj), tr("NO_MAIN_OBJ"))
1335   }
1336   else {
1337     RETURN_WITH_MSG(!CORBA::is_nil(myMainObj), tr("NO_GROUP"))
1338   }
1339
1340   QString aName (getNewObjectName());
1341   RETURN_WITH_MSG  (!aName.trimmed().isEmpty(), tr("EMPTY_NAME"))
1342
1343   RETURN_WITH_MSG  (myIdList->count(), tr("EMPTY_LIST"))
1344   return true;
1345 }
1346
1347 //=================================================================================
1348 // function : execute
1349 // purpose  :
1350 //=================================================================================
1351 bool GroupGUI_GroupDlg::execute(ObjectList& objects)
1352 {
1353   GEOM::GEOM_IGroupOperations_var anOper = GEOM::GEOM_IGroupOperations::_narrow(getOperation());
1354
1355   GEOM::GEOM_Object_var aGroup;
1356   if (myMode == CreateGroup)
1357     aGroup = anOper->CreateGroup(myMainObj, getShapeType());
1358   else if (myMode == EditGroup)
1359     aGroup = myGroup;
1360
1361   if (CORBA::is_nil(aGroup) || (myMode == CreateGroup && !anOper->IsDone()))
1362     return false;
1363
1364   GEOM::ListOfLong_var aCurrList = anOper->GetObjects(aGroup);
1365   if (!anOper->IsDone())
1366     return false;
1367
1368   if (aCurrList->length() > 0)
1369   {
1370     anOper->DifferenceIDs(aGroup, aCurrList);
1371     if (!anOper->IsDone())
1372       return false;
1373   }
1374
1375   int ii, nn = myIdList->count();
1376   if (nn > 0)
1377   {
1378     GEOM::ListOfLong_var aNewList = new GEOM::ListOfLong;
1379     aNewList->length(nn);
1380     for (ii = 0; ii < nn; ii++) {
1381       aNewList[ii] = myIdList->item(ii)->text().toInt();
1382     }
1383     anOper->UnionIDs(aGroup, aNewList);
1384     if (!anOper->IsDone())
1385       return false;
1386   }
1387
1388   SalomeApp_Study* study = getStudy();
1389   if (study) {
1390     QString objIOR = GEOMBase::GetIORFromObject(aGroup);
1391     if (objIOR != "") {
1392       _PTR(SObject) SO (study->studyDS()->FindObjectIOR(objIOR.toLatin1().constData()));
1393       if (SO) {
1394         _PTR(StudyBuilder) aBuilder (study->studyDS()->NewBuilder());
1395         aBuilder->SetName(SO, getNewObjectName().toLatin1().constData());
1396       }
1397     }
1398   }
1399
1400   objects.push_back(aGroup._retn());
1401
1402   return true;
1403 }
1404
1405 //================================================================
1406 // Function : getFather
1407 // Purpose  : Get father object for object to be added in study
1408 //            (called with addInStudy method)
1409 //================================================================
1410 GEOM::GEOM_Object_ptr GroupGUI_GroupDlg::getFather(GEOM::GEOM_Object_ptr theObj)
1411 {
1412   GEOM::GEOM_Object_var aFatherObj;
1413   if (theObj->GetType() == GEOM_GROUP) {
1414     GEOM::GEOM_IGroupOperations_var anOper = GEOM::GEOM_IGroupOperations::_narrow(getOperation());
1415     aFatherObj = anOper->GetMainShape(theObj);
1416   }
1417   return aFatherObj._retn();
1418 }
1419
1420 void GroupGUI_GroupDlg::ClickOnOkFilter()
1421 {
1422   if (CORBA::is_nil(myMainObj) || subSelectionWay() != ALL_SUBSHAPES || !myIsShapeType || getShapeType() == TopAbs_VERTEX)
1423     return;
1424   
1425   TopoDS_Shape aMainShape = GEOM_Client::get_client().GetShape(GeometryGUI::GetGeomGen(), myMainObj);
1426   TopTools_IndexedMapOfShape aSubShapesMap;
1427   TopExp::MapShapes(aMainShape, aSubShapesMap);
1428   SALOME_View* view = GEOM_Displayer::GetActiveView();
1429   getDisplayer()->Erase(myMainObj, false, false);
1430   CORBA::String_var aMainEntry = myMainObj->GetStudyEntry();
1431   QString anEntryBase = aMainEntry.in();
1432
1433   SALOME_ListIO toSelect;
1434
1435   TopExp_Explorer anExp (aMainShape, (TopAbs_ShapeEnum)getShapeType());
1436   for (; anExp.More(); anExp.Next())
1437   {
1438     TopoDS_Shape aSubShape = anExp.Current();
1439     int index = aSubShapesMap.FindIndex(aSubShape);
1440     QString anEntry = QString( "TEMP_" ) + anEntryBase + QString("_%1").arg(index);
1441     if ( !getDisplayer()->IsDisplayed( anEntry ) )
1442       continue;
1443
1444     double factor = GEOMUtils::ShapeToDouble(aSubShape).second;
1445     double v1 = myLessFilterSpin->value();
1446     double v2 = myGreaterFilterSpin->value();
1447     bool isLess = myLessFilterCombo->itemData(myLessFilterCombo->currentIndex()).toInt() == Filter_LT ? factor < v1 : factor <= v1;
1448     bool isGreater = myGreaterFilterCombo->itemData(myGreaterFilterCombo->currentIndex()).toInt() == Filter_GT ? factor > v2 : factor >= v2;
1449     if ( ( myLessFilterCheck->isChecked() && myGreaterFilterCheck->isChecked() && isLess && isGreater ) ||
1450          ( myLessFilterCheck->isChecked() && !myGreaterFilterCheck->isChecked() && isLess ) ||
1451          ( myGreaterFilterCheck->isChecked() && !myLessFilterCheck->isChecked() && isGreater ) ) {
1452       Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject();
1453       io->setEntry( anEntry.toLatin1().constData() );
1454       toSelect.Append(io);
1455     }
1456   }
1457   if ( toSelect.Extent() > 0 ) {
1458     myGeomGUI->getApp()->selectionMgr()->setSelectedObjects(toSelect);
1459     SUIT_MessageBox::information( this,
1460                                   tr( "INF_INFO" ),
1461                                   tr( "GEOM_SOME_SHAPES_SELECTED").arg( toSelect.Extent() ),
1462                                   tr( "BUT_OK" ) );
1463   }
1464   else {
1465     SUIT_MessageBox::information( this,
1466                                   tr( "INF_INFO" ),
1467                                   tr( "GEOM_NO_SHAPES_SELECTED" ),
1468                                   tr( "BUT_OK" ) );
1469   }
1470   updateState(true);
1471 }
1472
1473 #ifndef DISABLE_PLOT2DVIEWER
1474 //=================================================================================
1475 // function : ClickOnPlot()
1476 // purpose  : opens "Shape Statistics" dialog box in order to plot sub-shapes distribution.
1477 //=================================================================================
1478 void GroupGUI_GroupDlg::ClickOnPlot()
1479 {
1480   TopoDS_Shape aMainShape = GEOM_Client::get_client().GetShape(GeometryGUI::GetGeomGen(), myMainObj);
1481   QDialog* dlg = new MeasureGUI_ShapeStatisticsDlg( this, aMainShape, getShapeType() );
1482   if ( dlg ) {
1483     dlg->show();
1484   }
1485 }
1486 #endif
1487
1488 void GroupGUI_GroupDlg::MeasureToggled()
1489 {
1490   myLessFilterSpin->setEnabled(myLessFilterCheck->isChecked());
1491   myLessFilterCombo->setEnabled(myLessFilterCheck->isChecked());
1492   myGreaterFilterSpin->setEnabled(myGreaterFilterCheck->isChecked());
1493   myGreaterFilterCombo->setEnabled(myGreaterFilterCheck->isChecked());
1494   myApplyFilterButton->setEnabled(myLessFilterCheck->isChecked() || myGreaterFilterCheck->isChecked());
1495 }
1496
1497 //=================================================================================
1498 // function : getSourceObjects
1499 // purpose  : virtual method to get source objects
1500 //=================================================================================
1501 QList<GEOM::GeomObjPtr> GroupGUI_GroupDlg::getSourceObjects()
1502 {
1503   QList<GEOM::GeomObjPtr> res;
1504   GEOM::GeomObjPtr aGeomObjPtr1(myMainObj), aGeomObjPtr2(myInPlaceObj);
1505   res << aGeomObjPtr1 << aGeomObjPtr2;
1506   return res;
1507 }