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