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