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