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