Salome HOME
Initialize myDeflection (and other fields) to avoid FPE at setting deflection
[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, false, true);
621   }
622   else {
623     aDisplayer->EraseAll();
624     aDisplayer->Display(aSelList, true);
625   }
626 }
627
628 //=================================================================================
629 // function : getSelectedSubshapes
630 // purpose  :
631 //=================================================================================
632 int GroupGUI_GroupDlg::getSelectedSubshapes (TColStd_IndexedMapOfInteger& theMapIndex)
633 {
634   theMapIndex.Clear();
635
636   SalomeApp_Application* app = myGeomGUI->getApp();
637   if (!app || myMainObj->_is_nil())
638     return 0;
639
640   LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
641   SALOME_ListIO aSelList;
642   aSelMgr->selectedObjects(aSelList);
643
644   // try to find out and process the global selection
645   // (of not published objects and of published sub-shapes)
646   {
647     SALOME_ListIteratorOfListIO anIter (aSelList);
648     for (int i = 0; anIter.More(); anIter.Next(), i++)
649     {
650       Handle(SALOME_InteractiveObject) anIObj = anIter.Value();
651       QString anEntry = anIObj->getEntry();
652       QString str = "_";
653       int index = anEntry.lastIndexOf(str);
654       if (index > 0) // selection among special preview
655       {
656         anEntry.remove(0, index+1);
657         int anIndex = anEntry.toInt();
658         if (anIndex)
659           theMapIndex.Add(anIndex);
660       }
661       else // selection among published shapes
662       {
663         SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
664         if (!appStudy) return 0;
665         _PTR(Study) aStudy = appStudy->studyDS();
666
667         _PTR(SObject) aSObj (aStudy->FindObjectID(anEntry.toLatin1().constData()));
668         GEOM::GEOM_Object_var aGeomObj =
669           GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(aSObj));
670         TopoDS_Shape aShape;
671         if (GEOMBase::GetShape(aGeomObj, aShape)) {
672           if (aGeomObj->GetType() == GEOM_GROUP || aShape.ShapeType() == getShapeType()) {
673             TopTools_IndexedMapOfShape aMainMap;
674             TopoDS_Shape aMainShape = GEOM_Client::get_client().GetShape(GeometryGUI::GetGeomGen(), myMainObj);
675             TopExp::MapShapes(aMainShape, aMainMap);
676
677             TopExp_Explorer anExp (aShape, getShapeType());
678             for (; anExp.More(); anExp.Next()) {
679               TopoDS_Shape aSubShape = anExp.Current();
680               int anIndex = aMainMap.FindIndex(aSubShape);
681               if (anIndex >= 0) {
682                 if (subSelectionWay() != ALL_SUBSHAPES &&
683                     !myMain2InPlaceIndices.IsBound(anIndex))
684                   continue;
685                 theMapIndex.Add(anIndex);
686               }
687             }
688           }
689         }
690       }
691     } // for aSelList
692   }
693
694   return theMapIndex.Extent();
695 }
696
697 //=================================================================================
698 // function : add
699 // purpose  :
700 //=================================================================================
701 void GroupGUI_GroupDlg::add()
702 {
703   TColStd_IndexedMapOfInteger aMapIndex;
704   int nbSel = getSelectedSubshapes(aMapIndex);
705
706   TColStd_MapOfInteger aMap;
707   for (int i = 0, n = myIdList->count(); i < n; i++)
708     aMap.Add(myIdList->item(i)->text().toInt());
709
710   if (nbSel > 0) {
711     bool isBlocked = myIdList->signalsBlocked();
712     myIdList->blockSignals(true);
713
714     for (int i = 1, n = aMapIndex.Extent(); i <= n; i++) {
715       if (aMap.Contains(aMapIndex(i)))
716         continue;
717
718       QListWidgetItem* anItem = new QListWidgetItem(QString("%1").arg(aMapIndex(i)));
719       myIdList->addItem(anItem);
720       anItem->setSelected(true);
721     }
722
723     myIdList->blockSignals(isBlocked);
724   }
725
726   updateState();
727 }
728
729 //=================================================================================
730 // function : remove
731 // purpose  :
732 //=================================================================================
733 void GroupGUI_GroupDlg::remove()
734 {
735   bool isBlocked = myIdList->signalsBlocked();
736   myIdList->blockSignals(true);
737
738   QListIterator<QListWidgetItem*> it (myIdList->selectedItems());
739   while (it.hasNext())
740     delete it.next();
741
742   myIdList->blockSignals(isBlocked);
743
744   highlightSubShapes();
745 }
746
747 //=================================================================================
748 //function : subSelectionWay
749 //purpose  :
750 //=================================================================================
751 int GroupGUI_GroupDlg::subSelectionWay() const
752 {
753   return myRestrictGroup->checkedId();
754 }
755
756 //=================================================================================
757 // function : getShapeType()
758 // purpose  :
759 //=================================================================================
760 TopAbs_ShapeEnum GroupGUI_GroupDlg::getShapeType() const
761 {
762   switch (getConstructorId()) {
763   case 0:  return TopAbs_VERTEX;
764   case 1:  return TopAbs_EDGE;
765   case 2:  return TopAbs_FACE;
766   case 3:  return TopAbs_SOLID;
767   default: return TopAbs_SHAPE;
768   }
769 }
770
771 //=================================================================================
772 // function : setShapeType()
773 // purpose  :
774 //=================================================================================
775 void GroupGUI_GroupDlg::setShapeType(const TopAbs_ShapeEnum theType)
776 {
777   int anId = 0;
778   switch (theType) {
779   case TopAbs_VERTEX: anId = 0; break;
780   case TopAbs_EDGE:   anId = 1; break;
781   case TopAbs_FACE:   anId = 2; break;
782   case TopAbs_SOLID:  anId = 3; break;
783   }
784   setConstructorId(anId);
785   if (!myIsShapeType)
786   {
787     myIsShapeType = true;
788     // workaround to avoid set checked button 0
789     setConstructorId(anId);
790   }
791 }
792
793 //=================================================================================
794 // function : activateSelection
795 // purpose  : Activate selection in accordance with myEditCurrentArgument
796 //=================================================================================
797 void GroupGUI_GroupDlg::activateSelection()
798 {
799   erasePreview(false);
800
801   // local selection
802   if (!myMainObj->_is_nil() &&
803       !myEditCurrentArgument &&
804       myIsShapeType) // check if shape type is already choosen by user
805   {
806     GEOM_Displayer* aDisplayer = getDisplayer();
807     SALOME_View* view = GEOM_Displayer::GetActiveView();
808     if (view) {
809       CORBA::String_var aMainEntry = myMainObj->GetStudyEntry();
810       Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject (aMainEntry.in(), "GEOM", "TEMP_IO");
811       if (view->isVisible(io)) {
812         aDisplayer->Erase(myMainObj, false, false);
813         myIsHiddenMain = true;
814       }
815     }
816
817     int prevDisplayMode = aDisplayer->SetDisplayMode(0);
818
819     SUIT_ViewWindow* aViewWindow = 0;
820     SUIT_Study* activeStudy = SUIT_Session::session()->activeApplication()->activeStudy();
821     if (activeStudy)
822       aViewWindow = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
823     if (aViewWindow == 0) return;
824
825     SUIT_ViewManager* aViewManager = aViewWindow->getViewManager();
826     if (aViewManager->getType() != OCCViewer_Viewer::Type() &&
827         aViewManager->getType() != SVTK_Viewer::Type())
828       return;
829
830     SUIT_ViewModel* aViewModel = aViewManager->getViewModel();
831     SALOME_View* aView = dynamic_cast<SALOME_View*>(aViewModel);
832     if (aView == 0) return;
833
834     TopoDS_Shape aMainShape = GEOM_Client::get_client().GetShape(GeometryGUI::GetGeomGen(), myMainObj);
835     TopoDS_Shape aRestrictionShape;
836
837     if (subSelectionWay() == ALL_SUBSHAPES) {
838       aRestrictionShape = aMainShape;
839
840       TopTools_IndexedMapOfShape aSubShapesMap;
841       TopExp::MapShapes(aMainShape, aSubShapesMap);
842       CORBA::String_var aMainEntry = myMainObj->GetStudyEntry();
843       QString anEntryBase = aMainEntry.in();
844
845       TopExp_Explorer anExp (aRestrictionShape, getShapeType());
846       for (; anExp.More(); anExp.Next()) {
847         TopoDS_Shape aSubShape = anExp.Current();
848         int index = aSubShapesMap.FindIndex(aSubShape);
849         QString anEntry = anEntryBase + QString("_%1").arg(index);
850
851         SALOME_Prs* aPrs = aDisplayer->buildSubshapePresentation(aSubShape, anEntry, aView);
852         if (aPrs) {
853           displayPreview(aPrs, true, false); // append, do not update
854         }
855       }
856     }
857     else if (!myInPlaceObj->_is_nil()) {
858       TopTools_IndexedMapOfShape aSubShapesMap;
859       TopExp::MapShapes(aMainShape, aSubShapesMap);
860       CORBA::String_var aMainEntry = myMainObj->GetStudyEntry();
861       QString anEntryBase = aMainEntry.in();
862
863       TColStd_DataMapIteratorOfDataMapOfIntegerInteger aM2IPit (myMain2InPlaceIndices);
864       for (; aM2IPit.More(); aM2IPit.Next()) {
865         int index = aM2IPit.Key();
866         TopoDS_Shape aSubShape = aSubShapesMap.FindKey(index);
867         QString anEntry = anEntryBase + QString("_%1").arg(index);
868
869         SALOME_Prs* aPrs = aDisplayer->buildSubshapePresentation(aSubShape, anEntry, aView);
870         if (aPrs) {
871           displayPreview(aPrs, true, false); // append, do not update
872         }
873       }
874     }
875     else ;
876
877     aDisplayer->UpdateViewer();
878     aDisplayer->SetDisplayMode(prevDisplayMode);
879   }
880
881   globalSelection(GEOM_ALLSHAPES);
882
883   SelectionIntoArgument();
884 }
885
886 //=================================================================================
887 // function : updateState
888 // purpose  :
889 //=================================================================================
890 void GroupGUI_GroupDlg::updateState (bool isAdd)
891 {
892   myAddBtn->setEnabled(!myEditCurrentArgument && !CORBA::is_nil(myMainObj) && isAdd);
893   //myShowOnlyBtn->setEnabled(!myEditCurrentArgument && !CORBA::is_nil(myMainObj) && isAdd);
894
895   bool hasSel = myIdList->selectedItems().count() > 0;
896
897   myRemBtn->setEnabled(hasSel);
898   myRestrictGroupBox->setEnabled(!CORBA::is_nil(myMainObj));
899   mySelAllBtn->setEnabled(!CORBA::is_nil(myMainObj));
900
901   mySelBtn2->setEnabled(subSelectionWay() != ALL_SUBSHAPES);
902   myShape2Name->setEnabled(subSelectionWay() != ALL_SUBSHAPES);
903   if (subSelectionWay() == ALL_SUBSHAPES)
904     setInPlaceObj(GEOM::GEOM_Object::_nil());
905 }
906
907 //=================================================================================
908 // function : selectionChanged
909 // purpose  :
910 //=================================================================================
911 void GroupGUI_GroupDlg::selectionChanged()
912 {
913   highlightSubShapes();
914 }
915
916 //=================================================================================
917 // function : highlightSubShapes
918 // purpose  :
919 //=================================================================================
920 void GroupGUI_GroupDlg::highlightSubShapes()
921 {
922   if (CORBA::is_nil(myMainObj))
923     return;
924
925   TColStd_MapOfInteger anIds;
926
927   myBusy = true;
928
929   int ii = 0, nn = myIdList->count();
930   for (; ii < nn; ii++)
931   {
932     if (myIdList->item(ii)->isSelected()) {
933       int id = myIdList->item(ii)->text().toInt();
934       if (subSelectionWay() != ALL_SUBSHAPES &&
935           !myMain2InPlaceIndices.IsBound(id)) {
936         //myIdList->item(ii)->setSelected(false);
937       }
938       else {
939         anIds.Add(id);
940       }
941     }
942   }
943   SalomeApp_Application* app = myGeomGUI->getApp();
944   LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
945   aSelMgr->clearSelected();
946
947   SUIT_ViewWindow* aViewWindow = 0;
948   SUIT_Study* activeStudy = app->activeStudy();
949   if (activeStudy)
950     aViewWindow = app->desktop()->activeWindow();
951   if (aViewWindow == 0) return;
952
953   SUIT_ViewManager* aViewManager = aViewWindow->getViewManager();
954   if (aViewManager->getType() != OCCViewer_Viewer::Type() &&
955       aViewManager->getType() != SVTK_Viewer::Type())
956     return;
957
958   SUIT_ViewModel* aViewModel = aViewManager->getViewModel();
959   SALOME_View* aView = dynamic_cast<SALOME_View*>(aViewModel);
960   if (aView == 0) return;
961
962   // TODO: use here GEOMBase_Helper::myPreview instead of ic->DisplayedObjects()
963
964   OCCViewer_Viewer* v3d = ((OCCViewer_ViewManager*)aViewManager)->getOCCViewer();
965   Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
966   AIS_ListOfInteractive List;
967   ic->DisplayedObjects(List);
968
969   SALOME_ListIO aSelList;
970
971   // To highlight the selected subshape in Object Browser, if it's already pudlished under the main shape
972   GEOM::GEOM_ILocalOperations_var aLocOp = getGeomEngine()->GetILocalOperations(getStudyId());
973   QMap<int, QString> childsMap;
974   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
975   if (appStudy) {
976     _PTR(Study) aStudy = appStudy->studyDS();
977     CORBA::String_var aMainEntry = myMainObj->GetStudyEntry();
978     QString anEntry = aMainEntry.in();
979     _PTR(SObject) aSObj (aStudy->FindObjectID(anEntry.toLatin1().constData()));
980     _PTR(ChildIterator) anIt (aStudy->NewChildIterator(aSObj));
981     for (anIt->InitEx(true); anIt->More(); anIt->Next()) {
982       GEOM::GEOM_Object_var aChild = GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(anIt->Value()));
983       if (!CORBA::is_nil(aChild)) {
984         int index = aLocOp->GetSubShapeIndex(myMainObj, aChild);
985         CORBA::String_var aChildEntry = aChild->GetStudyEntry();
986         QString anEntry = aChildEntry.in();
987         childsMap.insert(index, anEntry);
988       }
989     }
990   }
991
992   AIS_ListIteratorOfListOfInteractive ite (List);
993   for (; ite.More(); ite.Next()) {
994     if (ite.Value()->IsInstance(STANDARD_TYPE(GEOM_AISShape))) {
995       Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(ite.Value());
996       if (aSh->hasIO()) {
997         Handle(SALOME_InteractiveObject) anIO = aSh->getIO();
998         QString anEntry = anIO->getEntry();
999         int index = anEntry.lastIndexOf("_");
1000         anEntry.remove(0, index+1);
1001         int anIndex = anEntry.toInt();
1002         if (anIds.Contains(anIndex)) {
1003           aSelList.Append(anIO);
1004           if (childsMap.contains (anIndex)) {
1005             Handle(SALOME_InteractiveObject) tmpIO = new SALOME_InteractiveObject(childsMap.value(anIndex).toLatin1().constData(), "GEOM", "TEMP_IO");
1006             aSelList.Append(tmpIO);
1007           }
1008         }
1009       }
1010     }
1011   }
1012   aSelMgr->setSelectedObjects(aSelList);
1013
1014   myBusy = false;
1015
1016   if (nn < 3000)
1017     updateState(aSelList.Extent() > 0);
1018   else {
1019     myAddBtn->setEnabled(true);
1020     myAddBtn->setEnabled(true);
1021     myRemBtn->setEnabled(true);
1022   }
1023 }
1024
1025 //=================================================================================
1026 // function : createOperation
1027 // purpose  :
1028 //=================================================================================
1029 GEOM::GEOM_IOperations_ptr GroupGUI_GroupDlg::createOperation()
1030 {
1031   return getGeomEngine()->GetIGroupOperations(getStudyId());
1032 }
1033
1034 #define RETURN_WITH_MSG(a, b) \
1035   if (!(a)) { \
1036     theMessage += (b); \
1037     return false; \
1038   }
1039
1040 //=================================================================================
1041 // function : isValid()
1042 // purpose  : Verify validity of input data
1043 //=================================================================================
1044 bool GroupGUI_GroupDlg::isValid(QString& theMessage)
1045 {
1046   SalomeApp_Study* study = getStudy();
1047   ASSERT(study);
1048   RETURN_WITH_MSG  (!study->studyDS()->GetProperties()->IsLocked(), tr("GEOM_STUDY_LOCKED"))
1049
1050   if (myMode == CreateGroup) {
1051     RETURN_WITH_MSG(!CORBA::is_nil(myMainObj), tr("NO_MAIN_OBJ"))
1052   }
1053   else {
1054     RETURN_WITH_MSG(!CORBA::is_nil(myMainObj), tr("NO_GROUP"))
1055   }
1056
1057   QString aName (getNewObjectName());
1058   RETURN_WITH_MSG  (!aName.trimmed().isEmpty(), tr("EMPTY_NAME"))
1059
1060   RETURN_WITH_MSG  (myIdList->count(), tr("EMPTY_LIST"))
1061   return true;
1062 }
1063
1064 //=================================================================================
1065 // function : execute
1066 // purpose  :
1067 //=================================================================================
1068 bool GroupGUI_GroupDlg::execute(ObjectList& objects)
1069 {
1070   GEOM::GEOM_IGroupOperations_var anOper = GEOM::GEOM_IGroupOperations::_narrow(getOperation());
1071
1072   GEOM::GEOM_Object_var aGroup;
1073   if (myMode == CreateGroup)
1074     aGroup = anOper->CreateGroup(myMainObj, getShapeType());
1075   else if (myMode == EditGroup)
1076     aGroup = myGroup;
1077
1078   if (CORBA::is_nil(aGroup) || (myMode == CreateGroup && !anOper->IsDone()))
1079     return false;
1080
1081   GEOM::ListOfLong_var aCurrList = anOper->GetObjects(aGroup);
1082   if (!anOper->IsDone())
1083     return false;
1084
1085   if (aCurrList->length() > 0)
1086   {
1087     anOper->DifferenceIDs(aGroup, aCurrList);
1088     if (!anOper->IsDone())
1089       return false;
1090   }
1091
1092   int ii, nn = myIdList->count();
1093   if (nn > 0)
1094   {
1095     GEOM::ListOfLong_var aNewList = new GEOM::ListOfLong;
1096     aNewList->length(nn);
1097     for (ii = 0; ii < nn; ii++) {
1098       aNewList[ii] = myIdList->item(ii)->text().toInt();
1099     }
1100     anOper->UnionIDs(aGroup, aNewList);
1101     if (!anOper->IsDone())
1102       return false;
1103   }
1104
1105   SalomeApp_Study* study = getStudy();
1106   if (study) {
1107     QString objIOR = GEOMBase::GetIORFromObject(aGroup);
1108     if (objIOR != "") {
1109       _PTR(SObject) SO (study->studyDS()->FindObjectIOR(objIOR.toLatin1().constData()));
1110       if (SO) {
1111         _PTR(StudyBuilder) aBuilder (study->studyDS()->NewBuilder());
1112         aBuilder->SetName(SO, getNewObjectName().toLatin1().constData());
1113       }
1114     }
1115   }
1116
1117   objects.push_back(aGroup._retn());
1118
1119   return true;
1120 }
1121
1122 //================================================================
1123 // Function : getFather
1124 // Purpose  : Get father object for object to be added in study
1125 //            (called with addInStudy method)
1126 //================================================================
1127 GEOM::GEOM_Object_ptr GroupGUI_GroupDlg::getFather(GEOM::GEOM_Object_ptr theObj)
1128 {
1129   GEOM::GEOM_Object_var aFatherObj;
1130   if (theObj->GetType() == GEOM_GROUP) {
1131     GEOM::GEOM_IGroupOperations_var anOper = GEOM::GEOM_IGroupOperations::_narrow(getOperation());
1132     aFatherObj = anOper->GetMainShape(theObj);
1133   }
1134   return aFatherObj._retn();
1135 }