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