Salome HOME
IPAL21558 In Group creation dialog, selection of sub-shapes in OB, viewer and dialog...
[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       bool highlight = false;
492       for (int ii = 1, nn = aMapIndex.Extent(); ii <= nn; ii++) {
493         if (aMap.contains(aMapIndex(ii))) {
494           myIdList->item(aMap[aMapIndex(ii)])->setSelected(true);
495           highlight = true;
496         }
497       }
498       if ( highlight )
499         highlightSubShapes();
500     }
501     myIdList->blockSignals(isBlocked);
502
503     updateState(nbSel);
504   }
505 }
506
507 //=================================================================================
508 // function : ConstructorsClicked()
509 // purpose  : Radio button management
510 //=================================================================================
511 void GroupGUI_GroupDlg::ConstructorsClicked(int constructorId)
512 {
513   if (getConstructorId() != constructorId)
514     setConstructorId(constructorId);
515
516   myIsShapeType = true;
517   myIdList->clear();
518   myEditCurrentArgument = 0;
519
520   setInPlaceObj(myInPlaceObj); // to rebuild myMain2InPlaceIndices
521   activateSelection();
522   updateState();
523 }
524
525 //=================================================================================
526 // function : selectAllSubShapes
527 // purpose  :
528 //=================================================================================
529 void GroupGUI_GroupDlg::selectAllSubShapes()
530 {
531   if (CORBA::is_nil(myMainObj) || !myIsShapeType)
532     return;
533
534   GEOM::ListOfLong_var aSubShapes;
535   GEOM::GEOM_IShapesOperations_var aShOp = getGeomEngine()->GetIShapesOperations(getStudyId());
536   aSubShapes = aShOp->SubShapeAllIDs(myMainObj, getShapeType(), false);
537   if (aSubShapes->length() > 0) {
538     if (subSelectionWay() == ALL_SUBSHAPES)
539     {
540       myIdList->clear(); // for sorted final list?
541
542       if (!aShOp->IsDone())
543         return;
544     }
545     else
546     {
547       aSubShapes = new GEOM::ListOfLong();
548       aSubShapes->length(myMain2InPlaceIndices.Extent());
549       TColStd_DataMapIteratorOfDataMapOfIntegerInteger m2ip (myMain2InPlaceIndices);
550       for (int i = 0; m2ip.More(); i++, m2ip.Next())
551         aSubShapes[i] = m2ip.Key();
552     }
553
554     bool isBlocked = myIdList->signalsBlocked();
555     myIdList->blockSignals(true);
556
557     for (int i = 0, n = aSubShapes->length(); i < n; i++) {
558       CORBA::Long anIndex = aSubShapes[i];
559       if (anIndex < 0)
560         continue;
561
562       QListWidgetItem* anItem = 0;
563       QString text = QString("%1").arg(anIndex);
564       if (!myInPlaceObj->_is_nil()) {
565         QList<QListWidgetItem*> found = myIdList->findItems(text, Qt::MatchExactly);
566         if (found.count()) anItem = found[0];
567       }
568       if (!anItem) {
569         anItem = new QListWidgetItem(text);
570         myIdList->addItem(anItem);
571       }
572       anItem->setSelected(true);
573     }
574
575     myIdList->blockSignals(isBlocked);
576     highlightSubShapes();
577   }
578 }
579
580 //=================================================================================
581 // function : showOnlySelected
582 // purpose  :
583 //=================================================================================
584 void GroupGUI_GroupDlg::showOnlySelected()
585 {
586   if (CORBA::is_nil(myMainObj) || !myIsShapeType)
587     return;
588
589   QPushButton* send = (QPushButton*)sender();
590   if (send == myShowAllBtn) {
591     activateSelection();
592     return;
593   }
594
595   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
596   SALOME_ListIO aSelList;
597   aSelMgr->selectedObjects(aSelList);
598
599   GEOM_Displayer* aDisplayer = getDisplayer();
600
601   if (send == myHideSelBtn) {
602     aDisplayer->Erase(aSelList, false, true);
603   }
604   else {
605     aDisplayer->EraseAll();
606     aDisplayer->Display(aSelList, true);
607   }
608 }
609
610 //=================================================================================
611 // function : getSelectedSubshapes
612 // purpose  :
613 //=================================================================================
614 int GroupGUI_GroupDlg::getSelectedSubshapes (TColStd_IndexedMapOfInteger& theMapIndex)
615 {
616   theMapIndex.Clear();
617
618   SalomeApp_Application* app = myGeomGUI->getApp();
619   if (!app) return 0;
620
621   LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
622   SALOME_ListIO aSelList;
623   aSelMgr->selectedObjects(aSelList);
624
625   // try to find out and process the global selection
626   // (of not published objects and of published sub-shapes)
627   {
628     SALOME_ListIteratorOfListIO anIter (aSelList);
629     for (int i = 0; anIter.More(); anIter.Next(), i++)
630     {
631       Handle(SALOME_InteractiveObject) anIObj = anIter.Value();
632       QString anEntry = anIObj->getEntry();
633       QString str = "_";
634       int index = anEntry.lastIndexOf(str);
635       if (index > 0) // selection among special preview
636       {
637         anEntry.remove(0, index+1);
638         int anIndex = anEntry.toInt();
639         if (anIndex)
640           theMapIndex.Add(anIndex);
641       }
642       else // selection among published shapes
643       {
644         SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
645         if (!appStudy) return 0;
646         _PTR(Study) aStudy = appStudy->studyDS();
647
648         _PTR(SObject) aSObj (aStudy->FindObjectID(anEntry.toLatin1().constData()));
649         GEOM::GEOM_Object_var aGeomObj =
650           GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(aSObj));
651         TopoDS_Shape aShape;
652         if (GEOMBase::GetShape(aGeomObj, aShape)) {
653           if (aGeomObj->GetType() == GEOM_GROUP || aShape.ShapeType() == getShapeType()) {
654             TopTools_IndexedMapOfShape aMainMap;
655             TopoDS_Shape aMainShape = GEOM_Client().GetShape(GeometryGUI::GetGeomGen(), myMainObj);
656             TopExp::MapShapes(aMainShape, aMainMap);
657
658             TopExp_Explorer anExp (aShape, getShapeType());
659             for (; anExp.More(); anExp.Next()) {
660               TopoDS_Shape aSubShape = anExp.Current();
661               int anIndex = aMainMap.FindIndex(aSubShape);
662               if (anIndex >= 0) {
663                 if (subSelectionWay() != ALL_SUBSHAPES &&
664                     !myMain2InPlaceIndices.IsBound(anIndex))
665                   continue;
666                 theMapIndex.Add(anIndex);
667               }
668             }
669           }
670         }
671       }
672     } // for aSelList
673   }
674
675   return theMapIndex.Extent();
676 }
677
678 //=================================================================================
679 // function : add
680 // purpose  :
681 //=================================================================================
682 void GroupGUI_GroupDlg::add()
683 {
684   TColStd_IndexedMapOfInteger aMapIndex;
685   int nbSel = getSelectedSubshapes(aMapIndex);
686
687   TColStd_MapOfInteger aMap;
688   for (int i = 0, n = myIdList->count(); i < n; i++)
689     aMap.Add(myIdList->item(i)->text().toInt());
690
691   if (nbSel > 0) {
692     bool isBlocked = myIdList->signalsBlocked();
693     myIdList->blockSignals(true);
694
695     for (int i = 1, n = aMapIndex.Extent(); i <= n; i++) {
696       if (aMap.Contains(aMapIndex(i)))
697         continue;
698
699       QListWidgetItem* anItem = new QListWidgetItem(QString("%1").arg(aMapIndex(i)));
700       myIdList->addItem(anItem);
701       anItem->setSelected(true);
702     }
703
704     myIdList->blockSignals(isBlocked);
705   }
706
707   updateState();
708 }
709
710 //=================================================================================
711 // function : remove
712 // purpose  :
713 //=================================================================================
714 void GroupGUI_GroupDlg::remove()
715 {
716   bool isBlocked = myIdList->signalsBlocked();
717   myIdList->blockSignals(true);
718
719   QListIterator<QListWidgetItem*> it (myIdList->selectedItems());
720   while (it.hasNext())
721     delete it.next();
722
723   myIdList->blockSignals(isBlocked);
724
725   highlightSubShapes();
726 }
727
728 //=================================================================================
729 //function : subSelectionWay
730 //purpose  :
731 //=================================================================================
732 int GroupGUI_GroupDlg::subSelectionWay() const
733 {
734   return myRestrictGroup->checkedId();
735 }
736
737 //=================================================================================
738 // function : getShapeType()
739 // purpose  :
740 //=================================================================================
741 TopAbs_ShapeEnum GroupGUI_GroupDlg::getShapeType() const
742 {
743   switch (getConstructorId()) {
744   case 0:  return TopAbs_VERTEX;
745   case 1:  return TopAbs_EDGE;
746   case 2:  return TopAbs_FACE;
747   case 3:  return TopAbs_SOLID;
748   default: return TopAbs_SHAPE;
749   }
750 }
751
752 //=================================================================================
753 // function : setShapeType()
754 // purpose  :
755 //=================================================================================
756 void GroupGUI_GroupDlg::setShapeType(const TopAbs_ShapeEnum theType)
757 {
758   int anId = 0;
759   switch (theType) {
760   case TopAbs_VERTEX: anId = 0; break;
761   case TopAbs_EDGE:   anId = 1; break;
762   case TopAbs_FACE:   anId = 2; break;
763   case TopAbs_SOLID:  anId = 3; break;
764   }
765   setConstructorId(anId);
766   if (!myIsShapeType)
767   {
768     myIsShapeType = true;
769     // workaround to avoid set checked button 0
770     setConstructorId(anId);
771   }
772 }
773
774 //=================================================================================
775 // function : activateSelection
776 // purpose  : Activate selection in accordance with myEditCurrentArgument
777 //=================================================================================
778 void GroupGUI_GroupDlg::activateSelection()
779 {
780   erasePreview(false);
781
782   // local selection
783   if (!myMainObj->_is_nil() &&
784       !myEditCurrentArgument &&
785       myIsShapeType) // check if shape type is already choosen by user
786   {
787     GEOM_Displayer* aDisplayer = getDisplayer();
788
789     SUIT_ViewWindow* aViewWindow = 0;
790     SUIT_Study* activeStudy = SUIT_Session::session()->activeApplication()->activeStudy();
791     if (activeStudy)
792       aViewWindow = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
793     if (aViewWindow == 0) return;
794
795     SUIT_ViewManager* aViewManager = aViewWindow->getViewManager();
796     if (aViewManager->getType() != OCCViewer_Viewer::Type() &&
797         aViewManager->getType() != SVTK_Viewer::Type())
798       return;
799
800     SUIT_ViewModel* aViewModel = aViewManager->getViewModel();
801     SALOME_View* aView = dynamic_cast<SALOME_View*>(aViewModel);
802     if (aView == 0) return;
803
804     TopoDS_Shape aMainShape = GEOM_Client().GetShape(GeometryGUI::GetGeomGen(), myMainObj);
805     TopoDS_Shape aRestrictionShape;
806     if (subSelectionWay() == ALL_SUBSHAPES)
807       aRestrictionShape = aMainShape;
808     else if (!myInPlaceObj->_is_nil())
809       aRestrictionShape = GEOM_Client().GetShape(GeometryGUI::GetGeomGen(), myInPlaceObj);
810     else ;
811
812     TopTools_IndexedMapOfShape aSubShapesMap;
813     TopExp::MapShapes(aMainShape, aSubShapesMap);
814     CORBA::String_var aMainEntry = myMainObj->GetStudyEntry();
815     QString anEntryBase = aMainEntry.in();
816
817     TopExp_Explorer anExp (aRestrictionShape, getShapeType());
818     for (; anExp.More(); anExp.Next())
819     {
820       TopoDS_Shape aSubShape = anExp.Current();
821       int index = aSubShapesMap.FindIndex(aSubShape);
822       QString anEntry = anEntryBase + QString("_%1").arg(index);
823
824       SALOME_Prs* aPrs = aDisplayer->buildSubshapePresentation(aSubShape, anEntry, aView);
825       if (aPrs) {
826         displayPreview(aPrs, true, false); // append, do not update
827       }
828     }
829     aDisplayer->UpdateViewer();
830   }
831
832   globalSelection(GEOM_ALLSHAPES);
833
834   SelectionIntoArgument();
835 }
836
837 //=================================================================================
838 // function : updateState
839 // purpose  :
840 //=================================================================================
841 void GroupGUI_GroupDlg::updateState (bool isAdd)
842 {
843   myAddBtn->setEnabled(!myEditCurrentArgument && !CORBA::is_nil(myMainObj) && isAdd);
844   //myShowOnlyBtn->setEnabled(!myEditCurrentArgument && !CORBA::is_nil(myMainObj) && isAdd);
845
846   bool hasSel = myIdList->selectedItems().count() > 0;
847
848   myRemBtn->setEnabled(hasSel);
849   myRestrictGroupBox->setEnabled(!CORBA::is_nil(myMainObj));
850   mySelAllBtn->setEnabled(!CORBA::is_nil(myMainObj));
851
852   mySelBtn2->setEnabled(   subSelectionWay() != ALL_SUBSHAPES);
853   myShape2Name->setEnabled(subSelectionWay() != ALL_SUBSHAPES);
854   if (subSelectionWay() == ALL_SUBSHAPES)
855     setInPlaceObj(GEOM::GEOM_Object::_nil());
856 }
857
858 //=================================================================================
859 // function : selectionChanged
860 // purpose  :
861 //=================================================================================
862 void GroupGUI_GroupDlg::selectionChanged()
863 {
864   highlightSubShapes();
865 }
866
867 //=================================================================================
868 // function : highlightSubShapes
869 // purpose  :
870 //=================================================================================
871 void GroupGUI_GroupDlg::highlightSubShapes()
872 {
873   if (CORBA::is_nil(myMainObj))
874     return;
875
876   TColStd_MapOfInteger anIds;
877
878   myBusy = true;
879
880   int ii = 0, nn = myIdList->count();
881   for (; ii < nn; ii++)
882   {
883     if (myIdList->item(ii)->isSelected()) {
884       int id = myIdList->item(ii)->text().toInt();
885       if (subSelectionWay() != ALL_SUBSHAPES &&
886           !myMain2InPlaceIndices.IsBound(id)) {
887         myIdList->item(ii)->setSelected(false);
888       }
889       else {
890         anIds.Add(id);
891       }
892     }
893   }
894   SalomeApp_Application* app = myGeomGUI->getApp();
895   LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
896   aSelMgr->clearSelected();
897
898   SUIT_ViewWindow* aViewWindow = 0;
899   SUIT_Study* activeStudy = app->activeStudy();
900   if (activeStudy)
901     aViewWindow = app->desktop()->activeWindow();
902   if (aViewWindow == 0) return;
903
904   SUIT_ViewManager* aViewManager = aViewWindow->getViewManager();
905   if (aViewManager->getType() != OCCViewer_Viewer::Type() &&
906       aViewManager->getType() != SVTK_Viewer::Type())
907     return;
908
909   SUIT_ViewModel* aViewModel = aViewManager->getViewModel();
910   SALOME_View* aView = dynamic_cast<SALOME_View*>(aViewModel);
911   if (aView == 0) return;
912
913   // TODO: use here GEOMBase_Helper::myPreview instead of ic->DisplayedObjects()
914
915   OCCViewer_Viewer* v3d = ((OCCViewer_ViewManager*)aViewManager)->getOCCViewer();
916   Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
917   AIS_ListOfInteractive List;
918   ic->DisplayedObjects(List);
919
920   SALOME_ListIO aSelList;
921
922   // To highlight the selected subshape in Object Browser, if it's already pudlished under the main shape
923   GEOM::GEOM_ILocalOperations_var aLocOp = getGeomEngine()->GetILocalOperations(getStudyId());
924   QMap<int, QString> childsMap;
925   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
926   if (appStudy) {
927     _PTR(Study) aStudy = appStudy->studyDS();
928     CORBA::String_var aMainEntry = myMainObj->GetStudyEntry();
929     QString anEntry = aMainEntry.in();
930     _PTR(SObject) aSObj (aStudy->FindObjectID(anEntry.toLatin1().constData()));
931     _PTR(ChildIterator) anIt ( aStudy->NewChildIterator( aSObj ) );
932     for ( anIt->InitEx( true ); anIt->More(); anIt->Next() ) {
933       GEOM::GEOM_Object_var aChild = GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(anIt->Value()));
934       if ( !CORBA::is_nil( aChild ) ) {
935         int index = aLocOp->GetSubShapeIndex(myMainObj, aChild);
936         CORBA::String_var aChildEntry = aChild->GetStudyEntry();
937         QString anEntry = aChildEntry.in();
938         childsMap.insert(index, anEntry);
939       }
940     }
941   }
942
943   AIS_ListIteratorOfListOfInteractive ite (List);
944   for (; ite.More(); ite.Next()) {
945     if (ite.Value()->IsInstance(STANDARD_TYPE(GEOM_AISShape))) {
946       Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(ite.Value());
947       if (aSh->hasIO()) {
948         Handle(SALOME_InteractiveObject) anIO = aSh->getIO();
949         QString anEntry = anIO->getEntry();
950         int index = anEntry.lastIndexOf("_");
951         anEntry.remove(0, index+1);
952         int anIndex = anEntry.toInt();
953         if ( anIds.Contains(anIndex) ) {
954           aSelList.Append(anIO);
955           if ( childsMap.contains ( anIndex )) {
956             Handle(SALOME_InteractiveObject) tmpIO = new SALOME_InteractiveObject( childsMap.value(anIndex).toLatin1().constData(), "GEOM", "TEMP_IO" );
957             aSelList.Append(tmpIO);
958           }
959         } 
960       }
961     }
962   }
963   aSelMgr->setSelectedObjects(aSelList);
964
965   myBusy = false;
966
967   if (nn < 3000)
968     updateState(aSelList.Extent() > 0);
969   else {
970     myAddBtn->setEnabled(true);
971     myAddBtn->setEnabled(true);
972     myRemBtn->setEnabled(true);
973   }
974 }
975
976 //=================================================================================
977 // function : createOperation
978 // purpose  :
979 //=================================================================================
980 GEOM::GEOM_IOperations_ptr GroupGUI_GroupDlg::createOperation()
981 {
982   return getGeomEngine()->GetIGroupOperations(getStudyId());
983 }
984
985 #define RETURN_WITH_MSG(a, b) \
986   if (!(a)) { \
987     theMessage += (b); \
988     return false; \
989   }
990
991 //=================================================================================
992 // function : isValid()
993 // purpose  : Verify validity of input data
994 //=================================================================================
995 bool GroupGUI_GroupDlg::isValid(QString& theMessage)
996 {
997   SalomeApp_Study* study = getStudy();
998   ASSERT(study);
999   RETURN_WITH_MSG  (!study->studyDS()->GetProperties()->IsLocked(), tr("GEOM_STUDY_LOCKED"))
1000
1001   if (myMode == CreateGroup) {
1002     RETURN_WITH_MSG(!CORBA::is_nil(myMainObj), tr("NO_MAIN_OBJ"))
1003   }
1004   else {
1005     RETURN_WITH_MSG(!CORBA::is_nil(myMainObj), tr("NO_GROUP"))
1006   }
1007
1008   QString aName (getNewObjectName());
1009   RETURN_WITH_MSG  (!aName.trimmed().isEmpty(), tr("EMPTY_NAME"))
1010
1011   RETURN_WITH_MSG  (myIdList->count(), tr("EMPTY_LIST"))
1012   return true;
1013 }
1014
1015 //=================================================================================
1016 // function : execute
1017 // purpose  :
1018 //=================================================================================
1019 bool GroupGUI_GroupDlg::execute(ObjectList& objects)
1020 {
1021   GEOM::GEOM_IGroupOperations_var anOper = GEOM::GEOM_IGroupOperations::_narrow(getOperation());
1022
1023   GEOM::GEOM_Object_var aGroup;
1024   if (myMode == CreateGroup)
1025     aGroup = anOper->CreateGroup(myMainObj, getShapeType());
1026   else if (myMode == EditGroup)
1027     aGroup = myGroup;
1028
1029   if (CORBA::is_nil(aGroup) || (myMode == CreateGroup && !anOper->IsDone()))
1030     return false;
1031
1032   GEOM::ListOfLong_var aCurrList = anOper->GetObjects(aGroup);
1033   if (!anOper->IsDone())
1034     return false;
1035
1036   if (aCurrList->length() > 0)
1037   {
1038     anOper->DifferenceIDs(aGroup, aCurrList);
1039     if (!anOper->IsDone())
1040       return false;
1041   }
1042
1043   int ii, nn = myIdList->count();
1044   if (nn > 0)
1045   {
1046     GEOM::ListOfLong_var aNewList = new GEOM::ListOfLong;
1047     aNewList->length(nn);
1048     for (ii = 0; ii < nn; ii++) {
1049       aNewList[ii] = myIdList->item(ii)->text().toInt();
1050     }
1051     anOper->UnionIDs(aGroup, aNewList);
1052     if (!anOper->IsDone())
1053       return false;
1054   }
1055
1056   SalomeApp_Study* study = getStudy();
1057   if (study) {
1058     char* objIOR = GEOMBase::GetIORFromObject(aGroup);
1059     std::string IOR(objIOR);
1060     free(objIOR);
1061     if (IOR != "") {
1062       _PTR(SObject) SO (study->studyDS()->FindObjectIOR(IOR));
1063       if (SO) {
1064         _PTR(StudyBuilder) aBuilder (study->studyDS()->NewBuilder());
1065         aBuilder->SetName(SO, getNewObjectName().toLatin1().constData());
1066       }
1067     }
1068   }
1069
1070   objects.push_back(aGroup._retn());
1071
1072   return true;
1073 }
1074
1075 //================================================================
1076 // Function : getFather
1077 // Purpose  : Get father object for object to be added in study
1078 //            ( called with addInStudy method )
1079 //================================================================
1080 GEOM::GEOM_Object_ptr GroupGUI_GroupDlg::getFather(GEOM::GEOM_Object_ptr theObj)
1081 {
1082   GEOM::GEOM_Object_var aFatherObj;
1083   if (theObj->GetType() == GEOM_GROUP) {
1084     GEOM::GEOM_IGroupOperations_var anOper = GEOM::GEOM_IGroupOperations::_narrow(getOperation());
1085     aFatherObj = anOper->GetMainShape(theObj);
1086   }
1087   return aFatherObj._retn();
1088 }