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