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