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