Salome HOME
0020317: EDF SMESH 1004: "Edit group" changes the name of the group
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_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 //  SMESH SMESHGUI : GUI for SMESH component
23 //  File   : SMESHGUI_GroupDlg.cxx
24 //  Author : Natalia KOPNOVA
25 //  Module : SMESH
26 //  $Header$
27 //
28 #include "SMESHGUI_GroupDlg.h"
29 #include "SMESHGUI_FilterDlg.h"
30 #include "SMESHGUI_ShapeByMeshDlg.h"
31
32 #include "SMESHGUI.h"
33 #include "SMESHGUI_Utils.h"
34 #include "SMESHGUI_VTKUtils.h"
35 #include "SMESHGUI_MeshUtils.h"
36 #include "SMESHGUI_GroupUtils.h"
37 #include "SMESHGUI_FilterUtils.h"
38 #include "SMESHGUI_GEOMGenUtils.h"
39
40 #include "SMESH_TypeFilter.hxx"
41 #include "SMESH_Actor.h"
42 #include "SMESH_ActorUtils.h"
43
44 #include "GEOMBase.h"
45 #include "GEOM_SelectionFilter.h"
46
47 #include "SUIT_Desktop.h"
48 #include "SUIT_ResourceMgr.h"
49 #include "SUIT_Session.h"
50 #include "SUIT_MessageBox.h"
51
52 #include "SalomeApp_Tools.h"
53 #include "SalomeApp_Application.h"
54 #include "SalomeApp_Study.h"
55 #include "LightApp_Application.h"
56 #include "SALOMEDSClient_Study.hxx"
57 #include "SALOME_ListIO.hxx"
58 #include "SALOME_ListIteratorOfListIO.hxx"
59
60 #include "SVTK_ViewWindow.h"
61 #include "SVTK_Selector.h"
62
63 #include "utilities.h"
64
65 // VTK Includes
66 #include <vtkRenderer.h>
67 #include <vtkActorCollection.h>
68
69 // OCCT Includes
70 #include <TColStd_MapOfInteger.hxx>
71
72 // QT Includes
73 #include <qbuttongroup.h>
74 #include <qcursor.h>
75 #include <qgroupbox.h>
76 #include <qhbox.h>
77 #include <qlabel.h>
78 #include <qlineedit.h>
79 #include <qpushbutton.h>
80 #include <qtoolbutton.h>
81 #include <qradiobutton.h>
82 #include <qcheckbox.h>
83 #include <qlayout.h>
84 #include <qlistbox.h>
85 #include <qimage.h>
86 #include <qpixmap.h>
87 #include <qmemarray.h>
88 #include <qwidgetstack.h>
89 #include <qcolordialog.h>
90
91 #include <QtxIntSpinBox.h>
92
93 // STL includes
94 #include <vector>
95 #include <algorithm>
96 #include <set>
97
98 using namespace std;
99
100 //=================================================================================
101 // function : SMESHGUI_GroupDlg()
102 // purpose  :
103 //=================================================================================
104 SMESHGUI_GroupDlg::SMESHGUI_GroupDlg( SMESHGUI* theModule, const char* name,
105                                       SMESH::SMESH_Mesh_ptr theMesh, bool modal, WFlags fl)
106      : QDialog( SMESH::GetDesktop( theModule ), name, modal, WStyle_Customize | WStyle_NormalBorder |
107                 WStyle_Title | WStyle_SysMenu | WDestructiveClose),
108      mySMESHGUI( theModule ),
109      mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
110      mySelector(SMESH::GetViewWindow( theModule )->GetSelector()),
111      myIsBusy( false ),
112      myNameChanged(false),
113      myActor( 0 )
114 {
115   if (!name) setName("SMESHGUI_GroupDlg");
116   initDialog(true);
117   if (!theMesh->_is_nil())
118     init(theMesh);
119   else {
120     mySelectSubMesh->setEnabled(false);
121     mySelectGroup->setEnabled(false);
122     myGeomGroupBtn->setEnabled(false);
123     myGeomGroupLine->setEnabled(false);
124   }
125 }
126
127 //=================================================================================
128 // function : SMESHGUI_GroupDlg()
129 // purpose  :
130 //=================================================================================
131 SMESHGUI_GroupDlg::SMESHGUI_GroupDlg( SMESHGUI* theModule, const char* name,
132                                       SMESH::SMESH_GroupBase_ptr theGroup, bool modal, WFlags fl)
133      : QDialog( SMESH::GetDesktop( theModule ), name, modal, WStyle_Customize | WStyle_NormalBorder |
134                 WStyle_Title | WStyle_SysMenu | WDestructiveClose),
135      mySMESHGUI( theModule ),
136      mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
137      mySelector(SMESH::GetViewWindow( theModule )->GetSelector()),
138      myIsBusy( false ),
139      myNameChanged(false)
140 {
141   if (!name) setName("SMESHGUI_GroupDlg");
142
143   initDialog(false);
144   if (!theGroup->_is_nil())
145     init(theGroup);
146   else {
147     mySelectSubMesh->setEnabled(false);
148     mySelectGroup->setEnabled(false);
149
150     myCurrentLineEdit = myMeshGroupLine;
151     setSelectionMode(5);
152   }
153 }
154
155 //=================================================================================
156 // function : SMESHGUI_GroupDlg()
157 // purpose  :
158 //=================================================================================
159 void SMESHGUI_GroupDlg::initDialog(bool create)
160 {
161   myFilterDlg = 0;
162   myCreate = create;
163   myCurrentLineEdit = 0;
164
165   myShapeByMeshOp = 0;
166   myGeomPopup = 0;
167   myGeomObjects = new GEOM::ListOfGO();
168   myGeomObjects->length(0);
169
170   QPixmap image0 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
171
172   if (create) {
173     setCaption(tr("SMESH_CREATE_GROUP_TITLE"));
174     myHelpFileName = "creating_groups_page.html";
175   }
176   else {
177     setCaption(tr("SMESH_EDIT_GROUP_TITLE"));
178     myHelpFileName = "editing_groups_page.html";
179   }
180
181   setSizeGripEnabled(TRUE);
182
183   QGridLayout* aMainLayout = new QGridLayout(this, 7, 3, 11, 6);
184
185   /***************************************************************/
186   QLabel* meshGroupLab = new QLabel(this, "mesh/group label");
187   if (create)
188     meshGroupLab->setText(tr("SMESH_MESH"));
189   else
190     meshGroupLab->setText(tr("SMESH_GROUP"));
191   myMeshGroupBtn = new QPushButton(this, "mesh/group button");
192   myMeshGroupBtn->setPixmap(image0);
193   myMeshGroupLine = new QLineEdit(this, "mesh/group line");
194   myMeshGroupLine->setReadOnly(true);
195
196   /***************************************************************/
197   myTypeGroup = new QButtonGroup(1, Qt::Vertical, this, "Group types");
198   myTypeGroup->setTitle(tr("SMESH_ELEMENTS_TYPE"));
199   myTypeGroup->setExclusive(true);
200
201   QStringList types;
202   types.append(tr("MESH_NODE"));
203   types.append(tr("SMESH_EDGE"));
204   types.append(tr("SMESH_FACE"));
205   types.append(tr("SMESH_VOLUME"));
206   QRadioButton* rb;
207   for (int i = 0; i < types.count(); i++) {
208     rb = new QRadioButton(types[i], myTypeGroup);
209   }
210   myTypeGroup->setEnabled(create);
211   myTypeId = -1;
212
213   /***************************************************************/
214   QLabel* aName = new QLabel(this, "name label");
215   aName->setText(tr("SMESH_NAME"));
216   aName->setMinimumSize(50,0);
217   myName = new QLineEdit(this, "name");
218
219   /***************************************************************/
220   myGrpTypeGroup = new QButtonGroup(1, Qt::Vertical, this, "Type of group");
221   myGrpTypeGroup->setTitle(tr("SMESH_GROUP_TYPE"));
222   myGrpTypeGroup->setExclusive(true);
223   QRadioButton* rb1 = new QRadioButton( tr("SMESH_GROUP_STANDALONE"), myGrpTypeGroup);
224   QRadioButton* rb2 = new QRadioButton( tr("SMESH_GROUP_GEOMETRY"),   myGrpTypeGroup);
225   myGrpTypeGroup->setEnabled(create);
226   myGrpTypeId = -1;
227
228   /***************************************************************/
229   myWGStack = new QWidgetStack( this, "widget stack");
230   QWidget* wg1 = new QFrame( myWGStack, "first widget" );
231   QWidget* wg2 = new QFrame( myWGStack, "second widget" );
232
233   /***************************************************************/
234   QGroupBox* aContentBox = new QGroupBox(1, Qt::Horizontal, wg1, "content box");
235   aContentBox->setTitle(tr("SMESH_CONTENT"));
236   QFrame* aContent = new QFrame(aContentBox, "content");
237   QGridLayout* aLayout = new QGridLayout(aContent, 7, 4);
238   aLayout->setSpacing(6);
239   aLayout->setAutoAdd(false);
240
241   QLabel* aLabel = new QLabel(aContent, "elements label");
242   aLabel->setText(tr("SMESH_ID_ELEMENTS"));
243   myElements = new QListBox(aContent, "elements list");
244   myElements->setSelectionMode(QListBox::Extended);
245
246   myFilter = new QPushButton(aContent, "filter");
247   myFilter->setText(tr("SMESH_BUT_FILTER"));
248   QPushButton* aAddBtn = new QPushButton(aContent, "add");
249   aAddBtn->setText(tr("SMESH_BUT_ADD"));
250   QPushButton* aRemoveBtn = new QPushButton(aContent, "remove");
251   aRemoveBtn->setText(tr("SMESH_BUT_REMOVE"));
252   QPushButton* aSortBtn = new QPushButton(aContent, "sort");
253   aSortBtn->setText(tr("SMESH_BUT_SORT"));
254
255   aLayout->addWidget(aLabel, 0, 0);
256   aLayout->addMultiCellWidget(myElements, 1, 6, 0, 0);
257   aLayout->addWidget(myFilter, 1, 2);
258   aLayout->addWidget(aAddBtn, 3, 2);
259   aLayout->addWidget(aRemoveBtn, 4, 2);
260   aLayout->addWidget(aSortBtn, 6, 2);
261
262   aLayout->setColStretch(0, 1);
263   aLayout->addColSpacing(1, 20);
264   aLayout->addColSpacing(3, 20);
265   aLayout->setRowStretch(2, 1);
266   aLayout->setRowStretch(5, 1);
267
268   /***************************************************************/
269   QGroupBox* aSelectBox = new QGroupBox(3, Qt::Horizontal, wg1, "select box");
270   aSelectBox->setTitle(tr("SMESH_SELECT_FROM"));
271
272   mySelectSubMesh = new QCheckBox(aSelectBox, "submesh checkbox");
273   mySelectSubMesh->setText(tr("SMESH_SUBMESH"));
274   mySelectSubMesh->setMinimumSize(50, 0);
275   mySubMeshBtn = new QPushButton(aSelectBox, "submesh button");
276   mySubMeshBtn->setText("");
277   mySubMeshBtn->setPixmap(image0);
278   mySubMeshLine = new QLineEdit(aSelectBox, "submesh line");
279   mySubMeshLine->setReadOnly(true);
280   onSelectSubMesh(false);
281
282   mySelectGroup = new QCheckBox(aSelectBox, "group checkbox");
283   mySelectGroup->setText(tr("SMESH_GROUP"));
284   mySelectGroup->setMinimumSize(50, 0);
285   myGroupBtn = new QPushButton(aSelectBox, "group button");
286   myGroupBtn->setText("");
287   myGroupBtn->setPixmap(image0);
288   myGroupLine = new QLineEdit(aSelectBox, "group line");
289   myGroupLine->setReadOnly(true);
290   onSelectGroup(false);
291
292   /***************************************************************/
293   QGridLayout* wg1Layout = new QGridLayout( wg1, 3, 1, 0, 6 );
294   wg1Layout->addWidget(aContentBox, 0, 0);
295   wg1Layout->addWidget(aSelectBox, 1, 0);
296   wg1Layout->setRowStretch(2, 5);
297
298   /***************************************************************/
299   QLabel* geomObject = new QLabel(wg2, "geometry object label");
300   geomObject->setText(tr("SMESH_OBJECT_GEOM"));
301   myGeomGroupBtn = new QToolButton(wg2, "geometry group button");
302   myGeomGroupBtn->setIconSet( QIconSet(image0) );
303   myGeomGroupBtn->setToggleButton(true);
304   myGeomGroupLine = new QLineEdit(wg2, "geometry group line");
305   myGeomGroupLine->setReadOnly(true); //VSR ???
306   onSelectGeomGroup(false);
307
308   if (!create)
309     {
310       myGeomGroupBtn->setEnabled(false);
311       myGeomGroupLine->setEnabled(false);
312     }
313
314   /***************************************************************/
315   QGridLayout* wg2Layout = new QGridLayout( wg2, 2, 3, 0, 6 );
316   wg2Layout->addWidget(geomObject,     0, 0);
317   wg2Layout->addWidget(myGeomGroupBtn, 0, 1);
318   wg2Layout->addWidget(myGeomGroupLine,0, 2);
319   wg2Layout->setRowStretch(1, 5);
320
321   /***************************************************************/
322   QVBoxLayout* dumb = new QVBoxLayout(myWGStack);
323   dumb->addWidget(wg1);
324   dumb->addWidget(wg2);
325   myWGStack->addWidget( wg1, myGrpTypeGroup->id(rb1) );
326   myWGStack->addWidget( wg2, myGrpTypeGroup->id(rb2) );
327
328   /***************************************************************/
329   QGroupBox* aColorBox = new QGroupBox(2, Qt::Horizontal, this, "color box");
330   aColorBox->setTitle(tr("SMESH_SET_COLOR"));
331
332   new QLabel( tr("SMESH_CHECK_COLOR"), aColorBox, "color label" );
333   myColorBtn = new QPushButton(aColorBox, "color button");
334   myColorBtn->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
335
336   /***************************************************************/
337
338   QFrame* aButtons = new QFrame(this, "button box");
339   aButtons->setFrameStyle(QFrame::Box | QFrame::Sunken);
340   QHBoxLayout* aBtnLayout = new QHBoxLayout(aButtons, 11, 6);
341   aBtnLayout->setAutoAdd(false);
342
343   QPushButton* aApplyBtn = new QPushButton(aButtons, "apply");
344   aApplyBtn->setText(tr("SMESH_BUT_APPLY"));
345   aApplyBtn->setAutoDefault(true);
346   QPushButton* aCloseBtn = new QPushButton(aButtons, "close");
347   aCloseBtn->setText(tr("SMESH_BUT_CLOSE"));
348   aCloseBtn->setAutoDefault(true);
349   QPushButton* aOKBtn = new QPushButton(aButtons, "ok");
350   aOKBtn->setText(tr("SMESH_BUT_APPLY_AND_CLOSE"));
351   aOKBtn->setAutoDefault(true);
352   aOKBtn->setDefault(true);
353   QPushButton* aHelpBtn = new QPushButton(aButtons, "help");
354   aHelpBtn->setText(tr("SMESH_BUT_HELP"));
355   aHelpBtn->setAutoDefault(true);
356
357   aBtnLayout->addWidget(aOKBtn);
358   aBtnLayout->addWidget(aApplyBtn);
359   aBtnLayout->addStretch();
360   aBtnLayout->addWidget(aCloseBtn);
361   aBtnLayout->addWidget(aHelpBtn);
362
363   /***************************************************************/
364   aMainLayout->addWidget(meshGroupLab,    0, 0);
365   aMainLayout->addWidget(myMeshGroupBtn,  0, 1);
366   aMainLayout->addWidget(myMeshGroupLine, 0, 2);
367   aMainLayout->addMultiCellWidget(myTypeGroup,    1, 1, 0, 2);
368   aMainLayout->addWidget(aName,      2, 0);
369   aMainLayout->addWidget(myName,     2, 2);
370   aMainLayout->addMultiCellWidget(myGrpTypeGroup, 3, 3, 0, 2);
371   aMainLayout->addMultiCellWidget(myWGStack,      4, 4, 0, 2);
372   aMainLayout->setRowStretch( 5, 5 );
373   aMainLayout->addMultiCellWidget(aColorBox,   6, 6, 0, 2);
374   aMainLayout->addMultiCellWidget(aButtons,       7, 7, 0, 2);
375
376   /* signals and slots connections */
377   connect(myMeshGroupBtn, SIGNAL(clicked()), this, SLOT(setCurrentSelection()));
378
379   connect(myGrpTypeGroup, SIGNAL(clicked(int)), this, SLOT(onGrpTypeChanged(int)));
380
381   connect(myTypeGroup, SIGNAL(clicked(int)), this, SLOT(onTypeChanged(int)));
382
383   connect(myName, SIGNAL(textChanged(const QString&)), this, SLOT(onNameChanged(const QString&)));
384   connect(myElements, SIGNAL(selectionChanged()), this, SLOT(onListSelectionChanged()));
385
386   connect(myFilter, SIGNAL(clicked()), this, SLOT(setFilters()));
387   connect(aAddBtn, SIGNAL(clicked()), this, SLOT(onAdd()));
388   connect(aRemoveBtn, SIGNAL(clicked()), this, SLOT(onRemove()));
389   connect(aSortBtn, SIGNAL(clicked()), this, SLOT(onSort()));
390
391   connect(mySelectSubMesh, SIGNAL(toggled(bool)), this, SLOT(onSelectSubMesh(bool)));
392   connect(mySelectGroup, SIGNAL(toggled(bool)), this, SLOT(onSelectGroup(bool)));
393   connect(mySubMeshBtn, SIGNAL(clicked()), this, SLOT(setCurrentSelection()));
394   connect(myGroupBtn, SIGNAL(clicked()), this, SLOT(setCurrentSelection()));
395   connect(myGeomGroupBtn, SIGNAL(toggled(bool)), this, SLOT(onGeomSelectionButton(bool)));
396   connect(myColorBtn, SIGNAL(clicked()), this, SLOT(onSelectColor()));
397
398   connect(aOKBtn, SIGNAL(clicked()), this, SLOT(onOK()));
399   connect(aApplyBtn, SIGNAL(clicked()), this, SLOT(onApply()));
400   connect(aCloseBtn, SIGNAL(clicked()), this, SLOT(onClose()));
401   connect(aHelpBtn, SIGNAL(clicked()), this, SLOT(onHelp()));
402
403   /* Init selection */
404   mySMESHGUI->SetActiveDialogBox(this);
405   mySMESHGUI->SetState(800);
406
407   mySelectionMode = -1;
408   myMeshFilter = new SMESH_TypeFilter(MESH);
409   mySubMeshFilter = new SMESH_TypeFilter(SUBMESH);
410   myGroupFilter = new SMESH_TypeFilter(GROUP);
411   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( mySMESHGUI->application()->activeStudy() );
412   myGeomFilter = new GEOM_SelectionFilter( aStudy, true );
413
414   connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(onDeactivate()));
415   connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(onClose()));
416   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(onObjectSelectionChanged()));
417
418   myGrpTypeGroup->setButton(myGrpTypeGroup->id(rb1)); // VSR !!!
419   onGrpTypeChanged(myGrpTypeGroup->id(rb1)); // VSR!!!
420
421   if (myMesh->_is_nil() )
422     myTypeGroup->setButton(0);
423
424   updateButtons();
425   //myName->setText(GetDefaultName(tr("SMESH_GROUP")));
426 }
427
428 //=================================================================================
429 // function : ~SMESHGUI_GroupDlg()
430 // purpose  : Destroys the object and frees any allocated resources
431 //=================================================================================
432 SMESHGUI_GroupDlg::~SMESHGUI_GroupDlg()
433 {
434   // no need to delete child widgets, Qt does it all for us
435   if ( myFilterDlg != 0 )
436   {
437     myFilterDlg->reparent( 0, QPoint() );
438     delete myFilterDlg;
439   }
440 }
441
442 //=================================================================================
443 // function : GetDefaultName()
444 // purpose  : Get the Group Name if Create new Group
445 //=================================================================================
446 QString SMESHGUI_GroupDlg::GetDefaultName(const QString& theOperation)
447 {
448     QString aName = "";
449
450     // collect all object names of SMESH component
451     SalomeApp_Study* appStudy =
452       dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
453     if ( !appStudy ) return aName;
454     _PTR(Study) aStudy = appStudy->studyDS();
455
456     std::set<std::string> aSet;
457     _PTR(SComponent) aMeshCompo (aStudy->FindComponent("SMESH"));
458     if (aMeshCompo) {
459       _PTR(ChildIterator) it (aStudy->NewChildIterator(aMeshCompo));
460       _PTR(SObject) obj;
461       for (it->InitEx(true); it->More(); it->Next()) {
462         obj = it->Value();
463         aSet.insert(obj->GetName());
464       }
465     }
466
467     // build a unique name
468     int aNumber = 0;
469     bool isUnique = false;
470     while (!isUnique) {
471       aName = theOperation + "_" + QString::number(++aNumber);
472       isUnique = (aSet.count(aName.latin1()) == 0);
473     }
474
475     return aName;
476 }
477
478 //=================================================================================
479 // function : Init()
480 // purpose  :
481 //=================================================================================
482 void SMESHGUI_GroupDlg::init (SMESH::SMESH_Mesh_ptr theMesh)
483 {
484   mySelectionMgr->installFilter(myMeshFilter);
485
486   /* init data from current selection */
487   restoreShowEntityMode();
488   myMesh = SMESH::SMESH_Mesh::_duplicate(theMesh);
489   setShowEntityMode();
490   myGroup = SMESH::SMESH_Group::_nil();
491   myGroupOnGeom = SMESH::SMESH_GroupOnGeom::_nil();
492
493   // NPAL19389: create a group with a selection in another group
494   // set actor of myMesh, if it is visible, else try
495   // any visible actor of group or submesh of myMesh
496   SetAppropriateActor();
497
498   setDefaultGroupColor();
499
500   SALOME_ListIO aList;
501   mySelectionMgr->selectedObjects( aList );
502   if ( !aList.IsEmpty() )
503   {
504     QString aName = aList.First()->getName();
505     myMeshGroupLine->setText(aName);
506     myMeshGroupLine->home( false );
507   }
508
509   myCurrentLineEdit = 0;
510
511   myTypeGroup->setButton(0);
512   onTypeChanged(0);
513 }
514
515 //=================================================================================
516 // function : Init()
517 // purpose  :
518 //=================================================================================
519 void SMESHGUI_GroupDlg::init (SMESH::SMESH_GroupBase_ptr theGroup)
520 {
521   restoreShowEntityMode();
522   myMesh = theGroup->GetMesh();
523   setShowEntityMode();
524
525   myNameChanged = true;
526   myName->blockSignals(true);
527   myName->setText(theGroup->GetName());
528   myName->blockSignals(false);
529   myName->home(false);
530
531   SALOMEDS::Color aColor = theGroup->GetColor();
532   setGroupColor( aColor );
533
534   myMeshGroupLine->setText(theGroup->GetName());
535
536   int aType = 0;
537   switch(theGroup->GetType()) {
538   case SMESH::NODE: aType= 0; break;
539   case SMESH::EDGE: aType = 1; break;
540   case SMESH::FACE: aType = 2; break;
541   case SMESH::VOLUME: aType = 3; break;
542   }
543   myTypeGroup->setButton(aType);
544
545   myGroup = SMESH::SMESH_Group::_narrow( theGroup );
546
547   if (!myGroup->_is_nil())
548   {
549     // NPAL19389: create a group with a selection in another group
550     // set actor of myMesh, if it is visible, else set
551     // actor of myGroup, if it is visible, else try
552     // any visible actor of group or submesh of myMesh
553     // commented, because an attempt to set selection on not displayed cells leads to error
554     //SetAppropriateActor();
555     myActor = SMESH::FindActorByObject(myMesh);
556     if ( !myActor )
557       myActor = SMESH::FindActorByObject(myGroup);
558     SMESH::SetPickable(myActor);
559
560     myGrpTypeGroup->setButton(0);
561     onGrpTypeChanged(0);
562
563     myCurrentLineEdit = 0;
564     myElements->clear();
565     setSelectionMode(aType);
566     myTypeId = aType;
567
568     setShowEntityMode(); // depends on myTypeId
569
570     myIdList.clear();
571     if (!myGroup->IsEmpty()) {
572       SMESH::long_array_var anElements = myGroup->GetListOfID();
573       int k = anElements->length();
574       for (int i = 0; i < k; i++) {
575         myIdList.append(anElements[i]);
576         myElements->insertItem(QString::number(anElements[i]));
577       }
578       myElements->selectAll(true);
579     }
580   }
581   else
582   {
583     myGroupOnGeom = SMESH::SMESH_GroupOnGeom::_narrow( theGroup );
584
585     if ( !myGroupOnGeom->_is_nil() )
586     {
587       // NPAL19389: create a group with a selection in another group
588       // set actor of myMesh, if it is visible, else set
589       // actor of myGroupOnGeom, if it is visible, else try
590       // any visible actor of group or submesh of myMesh
591       // commented, because an attempt to set selection on not displayed cells leads to error
592       //SetAppropriateActor();
593       myActor = SMESH::FindActorByObject(myMesh);
594       if ( !myActor )
595         myActor = SMESH::FindActorByObject(myGroupOnGeom);
596       SMESH::SetPickable(myActor);
597
598       myGrpTypeGroup->setButton(1);
599       onGrpTypeChanged(1);
600
601       QString aShapeName("");
602       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
603       GEOM::GEOM_Object_var aGroupShape = myGroupOnGeom->GetShape();
604       if (!aGroupShape->_is_nil())
605       {
606         _PTR(SObject) aGroupShapeSO = aStudy->FindObjectID(aGroupShape->GetStudyEntry());
607         aShapeName = aGroupShapeSO->GetName().c_str();
608       }
609       myGeomGroupLine->setText( aShapeName );
610       myNameChanged = true;
611       myName->blockSignals(true);
612       myName->setText(theGroup->GetName());
613       myName->blockSignals(false);
614     }
615   }
616 }
617
618 //=================================================================================
619 // function : updateButtons()
620 // purpose  :
621 //=================================================================================
622 void SMESHGUI_GroupDlg::updateButtons()
623 {
624   bool enable = !myName->text().stripWhiteSpace().isEmpty();
625
626   if (myGrpTypeId == 0) {
627     enable = enable && myElements->count() > 0;
628     enable = enable && (!myGroup->_is_nil() || !myMesh->_is_nil());
629   }
630   else if (myGrpTypeId == 1) {
631     if (CORBA::is_nil(myGroupOnGeom)) { // creation mode
632       enable = enable && myGeomObjects->length() > 0 && !myMesh->_is_nil();
633     }
634   }
635
636   QPushButton* aBtn;
637   aBtn = (QPushButton*) child("ok", "QPushButton");
638   if (aBtn) aBtn->setEnabled(enable);
639   aBtn = (QPushButton*) child("apply", "QPushButton");
640   if (aBtn) aBtn->setEnabled(enable);
641 }
642
643 //=================================================================================
644 // function : onNameChanged()
645 // purpose  :
646 //=================================================================================
647 void SMESHGUI_GroupDlg::onNameChanged (const QString& text)
648 {
649   myOldName = myName->text();
650   updateButtons();
651   QString tmp = myName->text();
652   tmp.stripWhiteSpace();
653   if( !tmp.isEmpty() )
654     myNameChanged = true;
655   else
656     myNameChanged = false;
657 }
658
659 //=================================================================================
660 // function : onTypeChanged()
661 // purpose  : Group elements type radio button management
662 //=================================================================================
663 void SMESHGUI_GroupDlg::onTypeChanged (int id)
664 {
665   if (myTypeId != id) {
666     myElements->clear();
667     if (myCurrentLineEdit == 0)
668       setSelectionMode(id);
669     myTypeId = id;
670     setShowEntityMode();
671   }
672 }
673
674 //=================================================================================
675 // function : onGrpTypeChanged()
676 // purpose  : Group type radio button management
677 //=================================================================================
678 void SMESHGUI_GroupDlg::onGrpTypeChanged (int id)
679 {
680   if (myGrpTypeId != id) {
681     myWGStack->raiseWidget( id );
682     myName->blockSignals(true);
683     myName->setText(myOldName);
684     myName->blockSignals(false);
685     onSelectGeomGroup(id == 1);
686   }
687   myGrpTypeId = id;
688 }
689
690 //=================================================================================
691 // function : onSelectColor()
692 // purpose  :
693 //=================================================================================
694 void SMESHGUI_GroupDlg::onSelectColor()
695 {
696   QColor color = getGroupQColor();
697   color = QColorDialog::getColor( color );
698   setGroupQColor( color );
699
700   updateButtons();
701 }
702
703 //=================================================================================
704 // function : setSelectionMode()
705 // purpose  : Radio button management
706 //=================================================================================
707 void SMESHGUI_GroupDlg::setSelectionMode (int theMode)
708 {
709   // PAL7314
710   if (myMesh->_is_nil())
711     return;
712   if (mySelectionMode != theMode) {
713     // [PAL10408] mySelectionMgr->clearSelected();
714     mySelectionMgr->clearFilters();
715     if (myActor)
716       myActor->SetPointRepresentation(false);
717     else
718       SMESH::SetPointRepresentation(false);
719     if (theMode < 4) {
720       switch (theMode) {
721       case 0:
722         if (myActor)
723           myActor->SetPointRepresentation(true);
724         else
725           SMESH::SetPointRepresentation(true);
726         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
727           aViewWindow->SetSelectionMode(NodeSelection);
728         break;
729       case 1:
730         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
731           aViewWindow->SetSelectionMode(EdgeSelection);
732         break;
733       case 2:
734         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
735           aViewWindow->SetSelectionMode(FaceSelection);
736         break;
737       default:
738         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
739           aViewWindow->SetSelectionMode(VolumeSelection);
740       }
741     } else {
742       if (theMode == 4)
743         mySelectionMgr->installFilter(mySubMeshFilter);
744       else if (theMode == 5)
745         mySelectionMgr->installFilter(myGroupFilter);
746       else if (theMode == 6)
747         mySelectionMgr->installFilter(myMeshFilter);
748       else if (theMode == 7)
749         mySelectionMgr->installFilter(myGeomFilter);
750
751       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
752         aViewWindow->SetSelectionMode(ActorSelection);
753     }
754     mySelectionMode = theMode;
755   }
756 }
757
758 //=================================================================================
759 // function : onApply()
760 // purpose  :
761 //=================================================================================
762 bool SMESHGUI_GroupDlg::onApply()
763 {
764   if (mySMESHGUI->isActiveStudyLocked())
765     return false;
766
767   if (myName->text().stripWhiteSpace().isEmpty())
768     return false;
769
770   if (myGrpTypeId == 0) { // on mesh elements
771     if (!myElements->count())
772       return false;
773
774     mySelectionMgr->clearSelected();
775
776     if (myGroup->_is_nil()) { // creation
777       if (myMesh->_is_nil())
778         return false;
779
780       SMESH::ElementType aType = SMESH::ALL;
781       switch (myTypeId) {
782       case 0: aType = SMESH::NODE; break;
783       case 1: aType = SMESH::EDGE; break;
784       case 2: aType = SMESH::FACE; break;
785       case 3: aType = SMESH::VOLUME; break;
786       }
787
788       SMESH::long_array_var anIdList = new SMESH::long_array;
789       int i, k = myElements->count();
790       anIdList->length(k);
791       QListBoxItem* anItem;
792       for (i = 0, anItem = myElements->firstItem(); anItem != 0; i++, anItem = anItem->next()) {
793         anIdList[i] = anItem->text().toInt();
794       }
795
796       myGroup = SMESH::AddGroup(myMesh, aType, myName->text());
797       myGroup->Add(anIdList.inout());
798
799       SALOMEDS::Color aColor = getGroupColor();
800       myGroup->SetColor(aColor);
801
802       _PTR(SObject) aMeshGroupSO = SMESH::FindSObject(myGroup);
803
804       //SMESH::setFileName ( aMeshGroupSO, QString::number(myColorSpinBox->value()) );
805       SMESH::setFileType ( aMeshGroupSO, "COULEURGROUP" );
806
807       /* init for next operation */
808       myName->setText("");
809       myElements->clear();
810       myGroup = SMESH::SMESH_Group::_nil();
811
812     } else { // edition
813       myGroup->SetName(myName->text());
814
815       SALOMEDS::Color aColor = getGroupColor();
816       myGroup->SetColor(aColor);
817
818       _PTR(SObject) aMeshGroupSO = SMESH::FindSObject(myGroup);
819
820       if (SMESH_Actor *anActor = SMESH::FindActorByEntry(aMeshGroupSO->GetID().c_str())) {
821         switch ( myTypeId ) {
822         case 0: anActor->SetNodeColor( aColor.R, aColor.G, aColor.B ); break;
823         case 1: anActor->SetEdgeColor( aColor.R, aColor.G, aColor.B ); break;
824         case 2:
825         case 3: anActor->SetSufaceColor( aColor.R, aColor.G, aColor.B ); break;
826         }
827       }
828
829       QValueList<int> aAddList;
830       QValueList<int>::iterator anIt;
831       QListBoxItem* anItem;
832
833       for (anItem = myElements->firstItem(); anItem != 0; anItem = anItem->next()) {
834         int anId = anItem->text().toInt();
835         if ((anIt = myIdList.find(anId)) == myIdList.end())
836           aAddList.append(anId);
837         else
838           myIdList.remove(anIt);
839       }
840       if (!aAddList.empty()) {
841         SMESH::long_array_var anIdList = new SMESH::long_array;
842         anIdList->length(aAddList.count());
843         int i;
844         for (i = 0, anIt = aAddList.begin(); anIt != aAddList.end(); anIt++, i++)
845           anIdList[i] = *anIt;
846         myGroup->Add(anIdList.inout());
847       }
848       if (!myIdList.empty()) {
849         SMESH::long_array_var anIdList = new SMESH::long_array;
850         anIdList->length(myIdList.count());
851         int i;
852         for (i = 0, anIt = myIdList.begin(); anIt != myIdList.end(); anIt++, i++)
853           anIdList[i] = *anIt;
854         myGroup->Remove(anIdList.inout());
855       }
856       /* init for next operation */
857       myIdList.clear();
858       for (anItem = myElements->firstItem(); anItem != 0; anItem = anItem->next())
859         myIdList.append(anItem->text().toInt());
860     }
861
862     mySMESHGUI->updateObjBrowser(true);
863     SMESH::UpdateView(); // asv: fix of BUG PAL5515
864     mySelectionMgr->clearSelected();
865     return true;
866   }
867   else if (myGrpTypeId == 1) { // on geom object
868     if (CORBA::is_nil(myGroupOnGeom)) { // creation
869       if (myMesh->_is_nil() || !myGeomObjects->length())
870         return false;
871
872       SMESH::ElementType aType = SMESH::ALL;
873       switch (myTypeId) {
874       case 0: aType = SMESH::NODE; break;
875       case 1: aType = SMESH::EDGE; break;
876       case 2: aType = SMESH::FACE; break;
877       case 3: aType = SMESH::VOLUME; break;
878       }
879
880       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
881       GEOM::GEOM_IGroupOperations_var aGroupOp =
882         SMESH::GetGEOMGen()->GetIGroupOperations(aStudy->StudyId());
883
884       if (myGeomObjects->length() == 1) {
885         myGroupOnGeom = myMesh->CreateGroupFromGEOM(aType, myName->text(),myGeomObjects[0]);
886       }
887       else {
888         SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
889         if ( aSMESHGen->_is_nil() )
890           return false;
891
892         // create a geometry group
893         GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
894         _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
895
896         if (geomGen->_is_nil() || !aStudy)
897           return false;
898
899         GEOM::GEOM_IGroupOperations_var op =
900           geomGen->GetIGroupOperations(aStudy->StudyId());
901         if (op->_is_nil())
902           return false;
903
904         // check and add all selected GEOM objects: they must be
905         // a sub-shapes of the main GEOM and must be of one type
906         TopAbs_ShapeEnum aGroupType = TopAbs_SHAPE;
907         for ( int i =0; i < myGeomObjects->length(); i++) {
908           TopAbs_ShapeEnum aSubShapeType = (TopAbs_ShapeEnum)myGeomObjects[i]->GetShapeType();
909           if (i == 0)
910             aGroupType = aSubShapeType;
911           else if (aSubShapeType != aGroupType) {
912             aGroupType = TopAbs_SHAPE;
913             break;
914           }
915         }
916
917         GEOM::GEOM_Object_var aMeshShape = myMesh->GetShapeToMesh();
918         GEOM::GEOM_Object_var aGroupVar = op->CreateGroup(aMeshShape, aGroupType);
919         op->UnionList(aGroupVar, myGeomObjects);
920
921         if (op->IsDone()) {
922           // publish the GEOM group in study
923           QString aNewGeomGroupName ("Auto_group_for_");
924           aNewGeomGroupName += myName->text();
925           SALOMEDS::SObject_var aNewGroupSO =
926             geomGen->AddInStudy(aSMESHGen->GetCurrentStudy(), aGroupVar, aNewGeomGroupName, aMeshShape);
927         }
928
929         myGroupOnGeom = myMesh->CreateGroupFromGEOM(aType, myName->text(), aGroupVar);
930       }
931
932       SALOMEDS::Color aColor = getGroupColor();
933       myGroupOnGeom->SetColor(aColor);
934
935       _PTR(SObject) aMeshGroupSO = SMESH::FindSObject(myGroupOnGeom);
936
937       //SMESH::setFileName ( aMeshGroupSO, QString::number(myColorSpinBox->value()) );
938       SMESH::setFileType ( aMeshGroupSO,"COULEURGROUP" );
939
940       /* init for next operation */
941       myName->setText("");
942       myGroupOnGeom = SMESH::SMESH_GroupOnGeom::_nil();
943     }
944     else { // edition
945       myGroupOnGeom->SetName(myName->text());
946
947       SALOMEDS::Color aColor = getGroupColor();
948       myGroupOnGeom->SetColor(aColor);
949
950       _PTR(SObject) aMeshGroupSO = SMESH::FindSObject(myGroupOnGeom);
951       if (SMESH_Actor *anActor = SMESH::FindActorByEntry(aMeshGroupSO->GetID().c_str())) {
952         switch ( myTypeId ) {
953         case 0: anActor->SetNodeColor( aColor.R, aColor.G, aColor.B ); break;
954         case 1: anActor->SetEdgeColor( aColor.R, aColor.G, aColor.B ); break;
955         case 2:
956         case 3: anActor->SetSufaceColor( aColor.R, aColor.G, aColor.B ); break;
957         }
958       }
959     }
960
961     mySMESHGUI->updateObjBrowser(true);
962     mySelectionMgr->clearSelected();
963     return true;
964   }
965
966   return false;
967 }
968
969 //=================================================================================
970 // function : onOK()
971 // purpose  :
972 //=================================================================================
973 void SMESHGUI_GroupDlg::onOK()
974 {
975   if ( onApply() )
976     onClose();
977 }
978
979 //=================================================================================
980 // function : onListSelectionChanged()
981 // purpose  : Called when selection in element list is changed
982 //=================================================================================
983 void SMESHGUI_GroupDlg::onListSelectionChanged()
984 {
985   //  MESSAGE("SMESHGUI_GroupDlg::onListSelectionChanged(); myActor = " << myActor);
986   if ( myIsBusy || !myActor) return;
987     myIsBusy = true;
988
989   if (myCurrentLineEdit == 0) {
990     mySelectionMgr->clearSelected();
991     TColStd_MapOfInteger aIndexes;
992     QListBoxItem* anItem;
993     for (anItem = myElements->firstItem(); anItem != 0; anItem = anItem->next()) {
994       if (anItem->isSelected()) {
995         int anId = anItem->text().toInt();
996         aIndexes.Add(anId);
997       }
998     }
999     mySelector->AddOrRemoveIndex(myActor->getIO(), aIndexes, false);
1000     SALOME_ListIO aList;
1001     aList.Append(myActor->getIO());
1002     mySelectionMgr->setSelectedObjects(aList,false);
1003   }
1004   myIsBusy = false;
1005 }
1006
1007 //=================================================================================
1008 // function : onObjectSelectionChanged()
1009 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
1010 //=================================================================================
1011 void SMESHGUI_GroupDlg::onObjectSelectionChanged()
1012 {
1013   if (myIsBusy || !isEnabled()) return;
1014   if (myCurrentLineEdit == myGeomGroupLine && !myGeomGroupBtn->isOn()) return;
1015
1016   myIsBusy = true;
1017
1018   SALOME_ListIO aList;
1019   mySelectionMgr->selectedObjects( aList );
1020
1021   int aNbSel = aList.Extent();
1022   myElements->clearSelection();
1023
1024   if (myCurrentLineEdit)
1025   {
1026     myCurrentLineEdit->setText("");
1027     QString aString = "";
1028
1029     if (myCurrentLineEdit == myMeshGroupLine)
1030     {
1031       mySelectSubMesh->setEnabled(false);
1032       mySelectGroup->setEnabled(false);
1033       myGroupLine->setText("");
1034       mySubMeshLine->setText("");
1035
1036       myGeomGroupBtn->setEnabled(false);
1037       myGeomGroupLine->setEnabled(false);
1038       myGeomGroupLine->setText("");
1039       myGeomObjects = new GEOM::ListOfGO();
1040       myGeomObjects->length(0);
1041
1042       if (myGeomGroupBtn->isOn())
1043         myGeomGroupBtn->setOn(false);
1044       if (!myCreate)
1045         myName->setText("");
1046
1047       myElements->clear();
1048
1049       if (aNbSel != 1 ) {
1050         myGroup = SMESH::SMESH_Group::_nil();
1051         myGroupOnGeom = SMESH::SMESH_GroupOnGeom::_nil();
1052         restoreShowEntityMode();
1053         myMesh = SMESH::SMESH_Mesh::_nil();
1054         updateGeomPopup();
1055         updateButtons();
1056         myIsBusy = false;
1057         return;
1058       }
1059       Handle(SALOME_InteractiveObject) IO = aList.First();
1060
1061       if (myCreate) {
1062         restoreShowEntityMode();
1063         myMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(IO);
1064         setShowEntityMode();
1065         updateGeomPopup();
1066         if (myMesh->_is_nil())
1067         {
1068           updateButtons();
1069           myIsBusy = false;
1070           return;
1071         }
1072         myGroup = SMESH::SMESH_Group::_nil();
1073
1074         // NPAL19389: create a group with a selection in another group
1075         // set actor of myMesh, if it is visible, else try
1076         // any visible actor of group or submesh of myMesh
1077         SetAppropriateActor();
1078
1079         aString = aList.First()->getName();
1080         myMeshGroupLine->setText(aString);
1081         myMeshGroupLine->home( false );
1082
1083         mySelectSubMesh->setEnabled(true);
1084         mySelectGroup->setEnabled(true);
1085         myGeomGroupBtn->setEnabled(true);
1086         myGeomGroupLine->setEnabled(true);
1087         updateButtons();
1088       } else {
1089         SMESH::SMESH_GroupBase_var aGroup = SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(IO);
1090         if (aGroup->_is_nil())
1091         {
1092           myIsBusy = false;
1093           return;
1094         }
1095         myIsBusy = false;
1096         myCurrentLineEdit = 0;
1097
1098         myGroup = SMESH::SMESH_Group::_nil();
1099         myGroupOnGeom = SMESH::SMESH_GroupOnGeom::_nil();
1100
1101         init(aGroup);
1102         myIsBusy = true;
1103         mySelectSubMesh->setEnabled(true);
1104         mySelectGroup->setEnabled(true);
1105       }
1106       myCurrentLineEdit = 0;
1107       myIsBusy = false;
1108       if (!myCreate)
1109         return;
1110
1111       if (myGrpTypeId == 0)
1112       {
1113         if (myTypeId == -1)
1114           onTypeChanged(0);
1115         else
1116         {
1117           myElements->clear();
1118           setSelectionMode(myTypeId);
1119         }
1120       }
1121
1122       myIsBusy = false;
1123       return;
1124     }
1125     else if (myCurrentLineEdit == myGeomGroupLine)
1126     {
1127       myGeomObjects = new GEOM::ListOfGO();
1128
1129       // The mesh SObject
1130       _PTR(SObject) aMeshSO = SMESH::FindSObject(myMesh);
1131
1132       if (aNbSel == 0 || !aMeshSO)
1133       {
1134         myGeomObjects->length(0);
1135         updateButtons();
1136         myIsBusy = false;
1137         return;
1138       }
1139
1140       myGeomObjects->length(aNbSel);
1141
1142       GEOM::GEOM_Object_var aGeomGroup;
1143       Standard_Boolean testResult;
1144       int i = 0;
1145
1146       SALOME_ListIteratorOfListIO anIt (aList);
1147       for (; anIt.More(); anIt.Next())
1148       {
1149         testResult = Standard_False;
1150         aGeomGroup = GEOMBase::ConvertIOinGEOMObject(anIt.Value(), testResult);
1151
1152         // Check if the object is a geometry group
1153         if (!testResult || CORBA::is_nil(aGeomGroup))
1154           continue;
1155
1156         // Check if group constructed on the same shape as a mesh or on its child
1157         _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1158         GEOM::GEOM_IGroupOperations_var anOp =
1159           SMESH::GetGEOMGen()->GetIGroupOperations(aStudy->StudyId());
1160
1161         // The main shape of the group
1162         GEOM::GEOM_Object_var aGroupMainShape;
1163         if (aGeomGroup->GetType() == 37)
1164           aGroupMainShape = anOp->GetMainShape(aGeomGroup);
1165         else
1166           aGroupMainShape = GEOM::GEOM_Object::_duplicate(aGeomGroup);
1167         _PTR(SObject) aGroupMainShapeSO =
1168           //aStudy->FindObjectIOR(aStudy->ConvertObjectToIOR(aGroupMainShape));
1169           aStudy->FindObjectID(aGroupMainShape->GetStudyEntry());
1170
1171         _PTR(SObject) anObj, aRef;
1172         bool isRefOrSubShape = false;
1173         if (aMeshSO->FindSubObject(1, anObj) &&  anObj->ReferencedObject(aRef)) {
1174           //if (strcmp(aRef->GetID(), aGroupMainShapeSO->GetID()) == 0) {
1175           if (aRef->GetID() == aGroupMainShapeSO->GetID()) {
1176             isRefOrSubShape = true;
1177           } else {
1178             _PTR(SObject) aFather = aGroupMainShapeSO->GetFather();
1179             _PTR(SComponent) aComponent = aGroupMainShapeSO->GetFatherComponent();
1180             //while (!isRefOrSubShape && strcmp(aFather->GetID(), aComponent->GetID()) != 0) {
1181             while (!isRefOrSubShape && aFather->GetID() != aComponent->GetID()) {
1182               //if (strcmp(aRef->GetID(), aFather->GetID()) == 0)
1183               if (aRef->GetID() == aFather->GetID())
1184                 isRefOrSubShape = true;
1185               else
1186                 aFather = aFather->GetFather();
1187             }
1188           }
1189         }
1190         if (isRefOrSubShape)
1191           myGeomObjects[i++] = aGeomGroup;
1192       }
1193
1194       myGeomObjects->length(i);
1195       if ( i == 0 )
1196         {
1197           myIsBusy = false;
1198           return;
1199         }
1200
1201       aNbSel = i;
1202     }
1203
1204     if (aNbSel >= 1) {
1205       if (aNbSel > 1) {
1206         if (myCurrentLineEdit == mySubMeshLine)
1207           aString = tr("SMESH_SUBMESH_SELECTED").arg(aNbSel);
1208         else if (myCurrentLineEdit == myGroupLine)
1209           aString = tr("SMESH_GROUP_SELECTED").arg(aNbSel);
1210         else if (myCurrentLineEdit == myGeomGroupLine)
1211           aString = tr("%1 Objects").arg(aNbSel);
1212       }
1213       else {
1214         aString = aList.First()->getName();
1215       }
1216     }
1217
1218     myCurrentLineEdit->setText(aString);
1219     myCurrentLineEdit->home(false);
1220     // 07.06.2008 skl for IPAL19574:
1221     // change name of group only if it is empty
1222     QString tmp = myName->text();
1223     tmp.stripWhiteSpace();
1224     if( tmp.isEmpty() || !myNameChanged ) {
1225       myOldName = myName->text();
1226       myName->blockSignals(true);
1227       myName->setText(aString);
1228       myName->blockSignals(false);
1229     }
1230
1231     updateButtons();
1232   }
1233   else // !myCurrentLineEdit: local selection of nodes or elements
1234   {
1235     if (aNbSel == 1 && myActor && myActor->hasIO())
1236     {
1237 #ifdef ENABLE_SWITCH_ACTOR_DURING_ELEMENTS_SELECTION
1238       // NPAL19389: create a group with a selection in another group
1239       // Switch myActor to the newly selected one, if the last
1240       // is visible and belongs to group or submesh of myMesh
1241       Handle(SALOME_InteractiveObject) curIO = myActor->getIO();
1242       Handle(SALOME_InteractiveObject) selIO = aList.First();
1243       if (curIO->hasEntry() && selIO->hasEntry()) {
1244         const char* selEntry = selIO->getEntry();
1245         if (strcmp(curIO->getEntry(), selEntry) != 0) {
1246           // different objects: selected and myActor
1247           SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView();
1248           if (aViewWindow && aViewWindow->isVisible(selIO)) {
1249             // newly selected actor is visible
1250
1251             // mesh entry
1252             _PTR(SObject) aSObject = SMESH::FindSObject(myMesh);
1253             if (aSObject) {
1254               CORBA::String_var meshEntry = aSObject->GetID().c_str();
1255               int len = strlen(meshEntry);
1256
1257               if (strncmp(selEntry, meshEntry, len) == 0) {
1258                 // selected object is myMesh or a part of it
1259                 SMESH_Actor* anActor = SMESH::FindActorByEntry(selEntry);
1260                 if (anActor) {
1261                   myActor = anActor;
1262                   SMESH::SetPickable(myActor);
1263                 }
1264               }
1265             }
1266           }
1267         }
1268       }
1269       // NPAL19389 END
1270 #endif // ENABLE_SWITCH_ACTOR_DURING_ELEMENTS_SELECTION
1271
1272       QString aListStr = "";
1273       int aNbItems = 0;
1274       if (myTypeId == 0) {
1275         aNbItems = SMESH::GetNameOfSelectedNodes(mySelector, myActor->getIO(), aListStr);
1276       } else {
1277         aNbItems = SMESH::GetNameOfSelectedElements(mySelector, myActor->getIO(), aListStr);
1278       }
1279       if (aNbItems > 0) {
1280         QStringList anElements = QStringList::split(" ", aListStr);
1281         QListBoxItem* anItem = 0;
1282         for (QStringList::iterator it = anElements.begin(); it != anElements.end(); ++it) {
1283           anItem = myElements->findItem(*it, Qt::ExactMatch);
1284           if (anItem) myElements->setSelected(anItem, true);
1285         }
1286       }
1287     }
1288   }
1289
1290   if (!myActor) {
1291     if (!myGroup->_is_nil())
1292       myActor = SMESH::FindActorByObject(myGroup);
1293     else if (!myGroupOnGeom->_is_nil())
1294       myActor = SMESH::FindActorByObject(myGroupOnGeom);
1295     else
1296       myActor = SMESH::FindActorByObject(myMesh);
1297   }
1298
1299   // somehow, if we display the mesh, while selecting from another actor,
1300   // the mesh becomes pickable, and there is no way to select any element
1301   if (myActor)
1302     SMESH::SetPickable(myActor);
1303
1304   myIsBusy = false;
1305 }
1306
1307 //=================================================================================
1308 // function : onSelectSubMesh()
1309 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
1310 //=================================================================================
1311 void SMESHGUI_GroupDlg::onSelectSubMesh(bool on)
1312 {
1313   if (on) {
1314     if (mySelectGroup->isChecked()) {
1315       mySelectGroup->setChecked(false);
1316     }
1317     //VSR: else if (mySelectGeomGroup->isChecked()) {
1318     //VSR:   mySelectGeomGroup->setChecked(false);
1319     //VSR: }
1320     myCurrentLineEdit = mySubMeshLine;
1321     setSelectionMode(4);
1322   }
1323   else {
1324     mySubMeshLine->setText("");
1325     myCurrentLineEdit = 0;
1326     if (myTypeId != -1)
1327       setSelectionMode(myTypeId);
1328   }
1329   mySubMeshBtn->setEnabled(on);
1330   mySubMeshLine->setEnabled(on);
1331 }
1332
1333
1334 //=================================================================================
1335 // function : (onSelectGroup)
1336 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
1337 //=================================================================================
1338 void SMESHGUI_GroupDlg::onSelectGroup(bool on)
1339 {
1340   if (on) {
1341     if (mySelectSubMesh->isChecked()) {
1342       mySelectSubMesh->setChecked(false);
1343     }
1344     myCurrentLineEdit = myGroupLine;
1345     setSelectionMode(5);
1346   }
1347   else {
1348     myGroupLine->setText("");
1349     myCurrentLineEdit = 0;
1350     if (myTypeId != -1)
1351       setSelectionMode(myTypeId);
1352   }
1353   myGroupBtn->setEnabled(on);
1354   myGroupLine->setEnabled(on);
1355 }
1356
1357
1358 //=================================================================================
1359 // function : (onSelectGeomGroup)
1360 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
1361 //=================================================================================
1362 void SMESHGUI_GroupDlg::onSelectGeomGroup(bool on)
1363 {
1364   if (on) {
1365     if (mySelectSubMesh->isChecked()) {
1366       mySelectSubMesh->setChecked(false);
1367     }
1368     else if (mySelectGroup->isChecked()) {
1369       mySelectGroup->setChecked(false);
1370     }
1371     myCurrentLineEdit = myGeomGroupLine;
1372     updateGeomPopup();
1373     setSelectionMode(8);
1374   }
1375   else {
1376     myGeomGroupBtn->setOn(false);
1377     myGeomObjects->length(0);
1378     myGeomGroupLine->setText("");
1379     myCurrentLineEdit = 0;
1380     if (myTypeId != -1)
1381       setSelectionMode(myTypeId);
1382   }
1383 }
1384
1385
1386 //=================================================================================
1387 // function : setCurrentSelection()
1388 // purpose  :
1389 //=================================================================================
1390 void SMESHGUI_GroupDlg::setCurrentSelection()
1391 {
1392   QPushButton* send = (QPushButton*)sender();
1393   myCurrentLineEdit = 0;
1394   if (send == myMeshGroupBtn) {
1395     myCurrentLineEdit = myMeshGroupLine;
1396     if (myCreate)
1397       setSelectionMode(6);
1398     else
1399       setSelectionMode(5);
1400     onObjectSelectionChanged();
1401   }
1402   else if (send == mySubMeshBtn) {
1403     myCurrentLineEdit = mySubMeshLine;
1404     onObjectSelectionChanged();
1405   }
1406   else if (send == myGroupBtn) {
1407     myCurrentLineEdit = myGroupLine;
1408     onObjectSelectionChanged();
1409   }
1410 }
1411
1412
1413 //=================================================================================
1414 // function : setFilters()
1415 // purpose  : SLOT. Called when "Filter" button pressed.
1416 //=================================================================================
1417 void SMESHGUI_GroupDlg::setFilters()
1418 {
1419   SMESH::ElementType aType = SMESH::ALL;
1420   switch ( myTypeId )
1421   {
1422     case 0 : aType = SMESH::NODE; break;
1423     case 1 : aType = SMESH::EDGE; break;
1424     case 2 : aType = SMESH::FACE; break;
1425     case 3 : aType = SMESH::VOLUME; break;
1426     default: return;
1427   }
1428
1429   if ( myFilterDlg == 0 )
1430   {
1431     myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, aType );
1432     connect( myFilterDlg, SIGNAL( Accepted() ), SLOT( onFilterAccepted() ) );
1433   }
1434   else
1435     myFilterDlg->Init( aType );
1436
1437   myFilterDlg->SetSelection();
1438   myFilterDlg->SetMesh( myMesh );
1439   myFilterDlg->SetSourceWg( myElements );
1440
1441   myFilterDlg->show();
1442 }
1443
1444 //=================================================================================
1445 // function : onFilterAccepted()
1446 // purpose  : SLOT. Called when Filter dlg closed with OK button.
1447 //            Uncheck "Select submesh" and "Select group" checkboxes
1448 //=================================================================================
1449 void SMESHGUI_GroupDlg::onFilterAccepted()
1450 {
1451   if ( mySelectSubMesh->isChecked() || mySelectGroup->isChecked() )
1452   {
1453     mySelectionMode = myTypeId;
1454     mySelectSubMesh->setChecked( false );
1455     mySelectGroup->setChecked( false );
1456   }
1457 }
1458
1459 //=================================================================================
1460 // function : onAdd()
1461 // purpose  :
1462 //=================================================================================
1463 void SMESHGUI_GroupDlg::onAdd()
1464 {
1465   SALOME_ListIO aList;
1466   mySelectionMgr->selectedObjects( aList );
1467
1468   int aNbSel = aList.Extent();
1469
1470   if (aNbSel == 0 || !myActor || myMesh->_is_nil()) return;
1471
1472   myIsBusy = true;
1473
1474   SMESH::ElementType aType = SMESH::ALL;
1475   switch(myTypeId) {
1476   case 0:
1477     aType = SMESH::NODE;
1478     mySelector->SetSelectionMode(NodeSelection);
1479     break;
1480   case 1:
1481     aType = SMESH::EDGE;
1482     mySelector->SetSelectionMode(EdgeSelection);
1483     break;
1484   case 2:
1485     aType = SMESH::FACE;
1486     mySelector->SetSelectionMode(FaceSelection);
1487     break;
1488   case 3:
1489     aType = SMESH::VOLUME;
1490     mySelector->SetSelectionMode(VolumeSelection);
1491     break;
1492   default:
1493     mySelector->SetSelectionMode(ActorSelection);
1494   }
1495
1496   if (myCurrentLineEdit == 0) {
1497     //if (aNbSel != 1) { myIsBusy = false; return; }
1498     QString aListStr = "";
1499     int aNbItems = 0;
1500     if (myTypeId == 0) {
1501       aNbItems = SMESH::GetNameOfSelectedNodes(mySelector, myActor->getIO(), aListStr);
1502     }
1503     else {
1504       aNbItems = SMESH::GetNameOfSelectedElements(mySelector, myActor->getIO(), aListStr);
1505     }
1506     if (aNbItems > 0) {
1507       QStringList anElements = QStringList::split(" ", aListStr);
1508       QListBoxItem* anItem = 0;
1509       for (QStringList::iterator it = anElements.begin(); it != anElements.end(); ++it) {
1510         anItem = myElements->findItem(*it, Qt::ExactMatch);
1511         if (!anItem) {
1512           anItem = new QListBoxText(*it);
1513           myElements->insertItem(anItem);
1514         }
1515         myElements->setSelected(anItem, true);
1516       }
1517     }
1518   } else if (myCurrentLineEdit == mySubMeshLine) {
1519     //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1520
1521     SALOME_ListIO aList;
1522     mySelectionMgr->selectedObjects( aList );
1523
1524     SALOME_ListIteratorOfListIO anIt (aList);
1525     for (; anIt.More(); anIt.Next()) {
1526       SMESH::SMESH_subMesh_var aSubMesh =
1527         SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIt.Value());
1528       if (!aSubMesh->_is_nil()) {
1529         // check if mesh is the same
1530         if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
1531           try {
1532             SMESH::long_array_var anElements = aSubMesh->GetElementsByType(aType);
1533             int k = anElements->length();
1534             QListBoxItem* anItem = 0;
1535             for (int i = 0; i < k; i++) {
1536               QString aText = QString::number(anElements[i]);
1537               anItem = myElements->findItem(aText, Qt::ExactMatch);
1538               if (!anItem) {
1539                 anItem = new QListBoxText(aText);
1540                 myElements->insertItem(anItem);
1541               }
1542               myElements->setSelected(anItem, true);
1543             }
1544           }
1545           catch (const SALOME::SALOME_Exception& ex) {
1546             SalomeApp_Tools::QtCatchCorbaException(ex);
1547           }
1548         }
1549       }
1550     }
1551     mySelectSubMesh->setChecked(false);
1552     myIsBusy = false;
1553     onListSelectionChanged();
1554
1555   } else if (myCurrentLineEdit == myGroupLine) {
1556     //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1557     SALOME_ListIO aList;
1558     mySelectionMgr->selectedObjects( aList );
1559
1560     SALOME_ListIteratorOfListIO anIt (aList);
1561     for (; anIt.More(); anIt.Next()) {
1562       SMESH::SMESH_Group_var aGroup =
1563         SMESH::IObjectToInterface<SMESH::SMESH_Group>(anIt.Value());
1564       if (!aGroup->_is_nil()) {
1565         // check if mesh is the same
1566         if (aGroup->GetType() == aType && aGroup->GetMesh()->GetId() == myMesh->GetId()) {
1567           SMESH::long_array_var anElements = aGroup->GetListOfID();
1568           int k = anElements->length();
1569           QListBoxItem* anItem = 0;
1570           for (int i = 0; i < k; i++) {
1571             QString aText = QString::number(anElements[i]);
1572             anItem = myElements->findItem(aText, Qt::ExactMatch);
1573             if (!anItem) {
1574               anItem = new QListBoxText(aText);
1575               myElements->insertItem(anItem);
1576             }
1577             myElements->setSelected(anItem, true);
1578           }
1579         }
1580       }
1581     }
1582     mySelectGroup->setChecked(false);
1583     myIsBusy = false;
1584     onListSelectionChanged();
1585
1586   } else if (myCurrentLineEdit == myGeomGroupLine && myGeomObjects->length() == 1) {
1587     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1588     GEOM::GEOM_IGroupOperations_var aGroupOp =
1589       SMESH::GetGEOMGen()->GetIGroupOperations(aStudy->StudyId());
1590
1591     SMESH::ElementType aGroupType = SMESH::ALL;
1592     switch(aGroupOp->GetType(myGeomObjects[0])) {
1593     case 7: aGroupType = SMESH::NODE; break;
1594     case 6: aGroupType = SMESH::EDGE; break;
1595     case 4: aGroupType = SMESH::FACE; break;
1596     case 2: aGroupType = SMESH::VOLUME; break;
1597     default: myIsBusy = false; return;
1598     }
1599
1600     if (aGroupType == aType) {
1601       _PTR(SObject) aGroupSO =
1602         //aStudy->FindObjectIOR(aStudy->ConvertObjectToIOR(myGeomGroup));
1603         aStudy->FindObjectID(myGeomObjects[0]->GetStudyEntry());
1604       // Construct filter
1605       SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
1606       SMESH::Filter_var aFilter = aFilterMgr->CreateFilter();
1607       SMESH::BelongToGeom_var aBelongToGeom = aFilterMgr->CreateBelongToGeom();;
1608       aBelongToGeom->SetGeom(myGeomObjects[0]);
1609       aBelongToGeom->SetShapeName(aGroupSO->GetName().c_str());
1610       aBelongToGeom->SetElementType(aType);
1611       aFilter->SetPredicate(aBelongToGeom);
1612
1613       SMESH::long_array_var anElements = aFilter->GetElementsId(myMesh);
1614
1615       int k = anElements->length();
1616       QListBoxItem* anItem = 0;
1617       for (int i = 0; i < k; i++) {
1618         QString aText = QString::number(anElements[i]);
1619         anItem = myElements->findItem(aText, Qt::ExactMatch);
1620         if (!anItem) {
1621           anItem = new QListBoxText(aText);
1622           myElements->insertItem(anItem);
1623         }
1624         myElements->setSelected(anItem, true);
1625       }
1626     }
1627
1628     //VSR: mySelectGeomGroup->setChecked(false);
1629     myIsBusy = false;
1630     onListSelectionChanged();
1631   }
1632   myIsBusy = false;
1633   //  mySelectionMgr->clearSelected();
1634   updateButtons();
1635 }
1636
1637 //=================================================================================
1638 // function : onRemove()
1639 // purpose  :
1640 //=================================================================================
1641 void SMESHGUI_GroupDlg::onRemove()
1642 {
1643   myIsBusy = true;
1644   if (myCurrentLineEdit == 0) {
1645     for (int i = myElements->count(); i > 0; i--) {
1646       if (myElements->isSelected(i-1)) {
1647         myElements->removeItem(i-1);
1648       }
1649     }
1650   } else {
1651     SALOME_ListIO aList;
1652     mySelectionMgr->selectedObjects( aList );
1653
1654     int aNbSel = aList.Extent();
1655
1656     if (aNbSel == 0) { myIsBusy = false; return; }
1657
1658     SMESH::ElementType aType = SMESH::ALL;
1659     switch(myTypeId) {
1660     case 0: aType = SMESH::NODE; break;
1661     case 1: aType = SMESH::EDGE; break;
1662     case 2: aType = SMESH::FACE; break;
1663     case 3: aType = SMESH::VOLUME; break;
1664     }
1665
1666     if (myCurrentLineEdit == mySubMeshLine) {
1667       //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1668       SALOME_ListIO aList;
1669       mySelectionMgr->selectedObjects( aList );
1670
1671       SALOME_ListIteratorOfListIO anIt (aList);
1672       for (; anIt.More(); anIt.Next()) {
1673         SMESH::SMESH_subMesh_var aSubMesh = SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIt.Value());
1674         if (!aSubMesh->_is_nil()) {
1675           // check if mesh is the same
1676           if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
1677             if (aType == SMESH::NODE) {
1678               try {
1679                 SMESH::long_array_var anElements = aSubMesh->GetNodesId();
1680                 int k = anElements->length();
1681                 QListBoxItem* anItem = 0;
1682                 for (int i = 0; i < k; i++) {
1683                   anItem = myElements->findItem(QString::number(anElements[i]), Qt::ExactMatch);
1684                   if (anItem) delete anItem;
1685                 }
1686               }
1687               catch (const SALOME::SALOME_Exception& ex) {
1688                 SalomeApp_Tools::QtCatchCorbaException(ex);
1689               }
1690             }
1691             else {
1692               try {
1693                 SMESH::long_array_var anElements = aSubMesh->GetElementsId();
1694                 int k = anElements->length();
1695                 QListBoxItem* anItem = 0;
1696                 for (int i = 0; i < k; i++) {
1697                   anItem = myElements->findItem(QString::number(anElements[i]), Qt::ExactMatch);
1698                   if (anItem) delete anItem;
1699                 }
1700               }
1701               catch (const SALOME::SALOME_Exception& ex) {
1702                 SalomeApp_Tools::QtCatchCorbaException(ex);
1703               }
1704             }
1705           }
1706         }
1707       }
1708     }
1709     else if (myCurrentLineEdit == myGroupLine) {
1710       Standard_Boolean aRes;
1711       //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1712       SALOME_ListIO aList;
1713       mySelectionMgr->selectedObjects( aList );
1714
1715       SALOME_ListIteratorOfListIO anIt (aList);
1716       for (; anIt.More(); anIt.Next()) {
1717         SMESH::SMESH_Group_var aGroup = SMESH::IObjectToInterface<SMESH::SMESH_Group>(anIt.Value());
1718         if (aRes && !aGroup->_is_nil()) {
1719           // check if mesh is the same
1720           if (aGroup->GetType() == aType && aGroup->GetMesh()->GetId() == myMesh->GetId()) {
1721             SMESH::long_array_var anElements = aGroup->GetListOfID();
1722             int k = anElements->length();
1723             QListBoxItem* anItem = 0;
1724             for (int i = 0; i < k; i++) {
1725               anItem = myElements->findItem(QString::number(anElements[i]), Qt::ExactMatch);
1726               if (anItem) delete anItem;
1727             }
1728           }
1729         }
1730       }
1731     }
1732   }
1733   myIsBusy = false;
1734   updateButtons();
1735 }
1736
1737 //=================================================================================
1738 // function : onSort()
1739 // purpose  :
1740 //=================================================================================
1741 void SMESHGUI_GroupDlg::onSort()
1742 {
1743   // PAL5412: sorts items in ascending by "string" value
1744   // myElements->sort(true);
1745   // myElements->update();
1746   int i, k = myElements->count();
1747   if (k > 0) {
1748     myIsBusy = true;
1749     QStringList aSelected;
1750     std::vector<int> anArray(k);
1751     //    QMemArray<int> anArray(k);
1752     QListBoxItem* anItem;
1753     // fill the array
1754     for (anItem = myElements->firstItem(), i = 0; anItem != 0; anItem = anItem->next(), i++) {
1755       anArray[i] = anItem->text().toInt();
1756       if (anItem->isSelected())
1757         aSelected.append(anItem->text());
1758     }
1759     // sort & update list
1760     std::sort(anArray.begin(), anArray.end());
1761     //    anArray.sort();
1762     myElements->clear();
1763     for (i = 0; i < k; i++) {
1764       myElements->insertItem(QString::number(anArray[i]));
1765     }
1766     for (QStringList::iterator it = aSelected.begin(); it != aSelected.end(); ++it) {
1767       anItem = myElements->findItem(*it, Qt::ExactMatch);
1768       if (anItem) myElements->setSelected(anItem, true);
1769     }
1770     myIsBusy = false;
1771   }
1772 }
1773
1774 //=================================================================================
1775 // function : closeEvent()
1776 // purpose  :
1777 //=================================================================================
1778 void SMESHGUI_GroupDlg::closeEvent (QCloseEvent*)
1779 {
1780   onClose();
1781 }
1782
1783 //=================================================================================
1784 // function : SMESHGUI_GroupDlg::onClose
1785 // purpose  : SLOT called when "Close" button pressed. Close dialog
1786 //=================================================================================
1787 void SMESHGUI_GroupDlg::onClose()
1788 {
1789   if (SMESH::GetCurrentVtkView()) {
1790     SMESH::RemoveFilters(); // PAL6938 -- clean all mesh entity filters
1791     SMESH::SetPointRepresentation(false);
1792     SMESH::SetPickable();
1793     restoreShowEntityMode();
1794   }
1795
1796   mySelectionMgr->clearSelected();
1797   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
1798     aViewWindow->SetSelectionMode(ActorSelection);
1799   mySelectionMgr->clearFilters();
1800   mySMESHGUI->ResetState();
1801
1802   reject();
1803 }
1804
1805 //=================================================================================
1806 // function : onHelp()
1807 // purpose  :
1808 //=================================================================================
1809 void SMESHGUI_GroupDlg::onHelp()
1810 {
1811   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
1812   if (app)
1813     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""),
1814                              myHelpFileName);
1815   else {
1816     QString platform;
1817 #ifdef WIN32
1818     platform = "winapplication";
1819 #else
1820     platform = "application";
1821 #endif
1822     SUIT_MessageBox::warn1
1823       (0, QObject::tr("WRN_WARNING"),
1824        QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
1825        arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(myHelpFileName),
1826        QObject::tr("BUT_OK"));
1827   }
1828 }
1829
1830 //=================================================================================
1831 // function : SMESHGUI_GroupDlg::onDeactivate
1832 // purpose  : SLOT called when dialog must be deativated
1833 //=================================================================================
1834 void SMESHGUI_GroupDlg::onDeactivate()
1835 {
1836   mySMESHGUI->ResetState();
1837   setEnabled(false);
1838 }
1839
1840 //=================================================================================
1841 // function : SMESHGUI_GroupDlg::enterEvent
1842 // purpose  : Event filter
1843 //=================================================================================
1844 void SMESHGUI_GroupDlg::enterEvent (QEvent*)
1845 {
1846   if (!isEnabled()) {
1847     mySMESHGUI->EmitSignalDeactivateDialog();
1848     setEnabled(true);
1849     mySelectionMode = -1;
1850     setSelectionMode(myTypeId);
1851     //mySMESHGUI->SetActiveDialogBox((QDialog*)this);
1852     mySMESHGUI->SetActiveDialogBox(this);
1853     mySMESHGUI->SetState(800);
1854   }
1855 }
1856
1857 //=================================================================================
1858 // function : hideEvent
1859 // purpose  : caused by ESC key
1860 //=================================================================================
1861 void SMESHGUI_GroupDlg::hideEvent (QHideEvent*)
1862 {
1863   if (!isMinimized() && !myIsBusy)
1864     onClose();
1865 }
1866
1867 //=================================================================================
1868 // function : keyPressEvent()
1869 // purpose  :
1870 //=================================================================================
1871 void SMESHGUI_GroupDlg::keyPressEvent( QKeyEvent* e )
1872 {
1873   QDialog::keyPressEvent( e );
1874   if ( e->isAccepted() )
1875     return;
1876
1877   if ( e->key() == Key_F1 )
1878     {
1879       e->accept();
1880       onHelp();
1881     }
1882 }
1883
1884 //================================================================================
1885 /*!
1886  * \brief Enable showing of the popup when Geometry selection btn is clicked
1887   * \param enable - true to enable
1888  */
1889 //================================================================================
1890
1891 enum { DIRECT_GEOM_INDEX = 0, GEOM_BY_MESH_INDEX };
1892
1893 void SMESHGUI_GroupDlg::updateGeomPopup()
1894 {
1895   bool enable = false;
1896
1897   if ( !myMesh->_is_nil() )
1898     enable = myMesh->NbEdges() > 0;
1899
1900   if ( myGeomGroupBtn )
1901   {
1902     disconnect( myGeomGroupBtn, SIGNAL( toggled(bool) ), this, SLOT( onGeomSelectionButton(bool) ));
1903     if ( enable ) {
1904       if ( !myGeomPopup ) {
1905         myGeomPopup = new QPopupMenu();
1906         myGeomPopup->insertItem( tr("DIRECT_GEOM_SELECTION"), DIRECT_GEOM_INDEX );
1907         myGeomPopup->insertItem( tr("GEOM_BY_MESH_ELEM_SELECTION"), GEOM_BY_MESH_INDEX );
1908         connect( myGeomPopup, SIGNAL( activated( int ) ), SLOT( onGeomPopup( int ) ) );
1909       }
1910       connect( myGeomGroupBtn, SIGNAL( toggled(bool) ), this, SLOT( onGeomSelectionButton(bool) ));
1911     }
1912   }
1913 }
1914
1915
1916 //=================================================================================
1917 // function : onGeomSelectionButton()
1918 // purpose  :
1919 //=================================================================================
1920 void SMESHGUI_GroupDlg::onGeomSelectionButton(bool isBtnOn)
1921 {
1922   if ( myGeomPopup && isBtnOn )
1923     {
1924       myCurrentLineEdit = myGeomGroupLine;
1925       int id = myGeomPopup->exec( QCursor::pos() );
1926       if (id == DIRECT_GEOM_INDEX || id == -1)
1927         setSelectionMode(7);
1928     }
1929   else if (!isBtnOn)
1930     {
1931       myCurrentLineEdit = 0;
1932       setSelectionMode(8);
1933     }
1934 }
1935
1936 //=================================================================================
1937 // function : onGeomPopup()
1938 // purpose  :
1939 //=================================================================================
1940 void SMESHGUI_GroupDlg::onGeomPopup( int index )
1941 {
1942   if ( index == GEOM_BY_MESH_INDEX )
1943     {
1944       mySelectionMode = -1;
1945       if ( !myShapeByMeshOp ) {
1946         myShapeByMeshOp = new SMESHGUI_ShapeByMeshOp(true);
1947         connect(myShapeByMeshOp, SIGNAL(committed(SUIT_Operation*)),
1948                 SLOT(onPublishShapeByMeshDlg(SUIT_Operation*)));
1949         connect(myShapeByMeshOp, SIGNAL(aborted(SUIT_Operation*)),
1950                 SLOT(onCloseShapeByMeshDlg(SUIT_Operation*)));
1951       }
1952       // set mesh object to SMESHGUI_ShapeByMeshOp and start it
1953       if ( !myMesh->_is_nil() ) {
1954         myIsBusy = true;
1955         hide(); // stop processing selection
1956         myIsBusy = false;
1957         myShapeByMeshOp->setModule( mySMESHGUI );
1958         myShapeByMeshOp->setStudy( 0 ); // it's really necessary
1959         myShapeByMeshOp->SetMesh( myMesh );
1960         myShapeByMeshOp->start();
1961       }
1962     }
1963 }
1964
1965 //================================================================================
1966 /*!
1967  * \brief SLOT. Is called when Ok is pressed in SMESHGUI_ShapeByMeshDlg
1968  */
1969 //================================================================================
1970
1971 void SMESHGUI_GroupDlg::onPublishShapeByMeshDlg(SUIT_Operation* op)
1972 {
1973   if ( myShapeByMeshOp == op ) {
1974     mySMESHGUI->getApp()->updateObjectBrowser();
1975     show();
1976     // Select a found geometry object
1977     GEOM::GEOM_Object_var aGeomVar = myShapeByMeshOp->GetShape();
1978     if ( !aGeomVar->_is_nil() )
1979     {
1980       QString ID = aGeomVar->GetStudyEntry();
1981       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1982       if ( _PTR(SObject) aGeomSO = aStudy->FindObjectID( ID.latin1() )) {
1983         SALOME_ListIO anIOList;
1984         Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject
1985           ( aGeomSO->GetID().c_str(), "SMESH", aGeomSO->GetName().c_str() );
1986         anIOList.Append( anIO );
1987               mySelectionMgr->setSelectedObjects( anIOList, false );
1988         onObjectSelectionChanged();
1989       }
1990     }
1991   }
1992 }
1993
1994 //================================================================================
1995 /*!
1996  * \brief SLOT. Is called when Close is pressed in SMESHGUI_ShapeByMeshDlg
1997  */
1998 //================================================================================
1999
2000 void SMESHGUI_GroupDlg::onCloseShapeByMeshDlg(SUIT_Operation* op)
2001 {
2002   if ( myShapeByMeshOp == op )
2003     {
2004       show();
2005       setSelectionMode(7);
2006     }
2007 }
2008
2009 //=================================================================================
2010 // function : setGroupColor()
2011 // purpose  :
2012 //=================================================================================
2013 void SMESHGUI_GroupDlg::setGroupColor( const SALOMEDS::Color& theColor )
2014 {
2015   QColor aQColor( (int)( theColor.R * 255.0 ),
2016                   (int)( theColor.G * 255.0 ),
2017                   (int)( theColor.B * 255.0 ) );
2018   setGroupQColor( aQColor );
2019 }
2020
2021 //=================================================================================
2022 // function : getGroupColor()
2023 // purpose  :
2024 //=================================================================================
2025 SALOMEDS::Color SMESHGUI_GroupDlg::getGroupColor() const
2026 {
2027   QColor aQColor = getGroupQColor();
2028
2029   SALOMEDS::Color aColor;
2030   aColor.R = (float)aQColor.red() / 255.0;
2031   aColor.G = (float)aQColor.green() / 255.0;
2032   aColor.B = (float)aQColor.blue() / 255.0;
2033
2034   return aColor;
2035 }
2036
2037 //=================================================================================
2038 // function : setGroupQColor()
2039 // purpose  :
2040 //=================================================================================
2041 void SMESHGUI_GroupDlg::setGroupQColor( const QColor& theColor )
2042 {
2043   if ( theColor.isValid() )
2044   {
2045     QPalette pal = myColorBtn->palette();
2046     pal.setColor(QColorGroup::Button, theColor);
2047     myColorBtn->setPalette(pal);
2048   }
2049 }
2050
2051 //=================================================================================
2052 // function : getGroupQColor()
2053 // purpose  :
2054 //=================================================================================
2055 QColor SMESHGUI_GroupDlg::getGroupQColor() const
2056 {
2057   QColor aColor = myColorBtn->palette().active().button();
2058   return aColor;
2059 }
2060
2061 //=================================================================================
2062 // function : setDefaultGroupColor()
2063 // purpose  :
2064 //=================================================================================
2065 void SMESHGUI_GroupDlg::setDefaultGroupColor()
2066 {
2067   if ( myMesh->_is_nil() )
2068     return;
2069
2070   bool isAutoColor = myMesh->GetAutoColor();
2071
2072   QColor aQColor;
2073   if ( !isAutoColor )
2074   {
2075     int r = 0, g = 0, b = 0;
2076     SMESH::GetColor( "SMESH", "fill_color", r, g, b, QColor( 0, 170, 255 ) );
2077     aQColor.setRgb( r, g, b );
2078   }
2079   else
2080   {
2081     SMESH::ListOfGroups aListOfGroups = *myMesh->GetGroups();
2082
2083     QValueList<SALOMEDS::Color> aReservedColors;
2084     for ( int i = 0, n = aListOfGroups.length(); i < n; i++ )
2085     {
2086       SMESH::SMESH_GroupBase_var aGroupObject = aListOfGroups[i];
2087       SALOMEDS::Color aReservedColor = aGroupObject->GetColor();
2088       aReservedColors.append( aReservedColor );
2089     }
2090
2091     SALOMEDS::Color aColor = SMESHGUI::getUniqueColor( aReservedColors );
2092     aQColor.setRgb( (int)( aColor.R * 255.0 ),
2093                     (int)( aColor.G * 255.0 ),
2094                     (int)( aColor.B * 255.0 ) );
2095
2096   }
2097
2098   setGroupQColor( aQColor );
2099 }
2100
2101 //=================================================================================
2102 // function : SetAppropriateActor()
2103 // purpose  : Find more appropriate of visible actors, set it to myActor, allow picking
2104 //            NPAL19389: create a group with a selection in another group.
2105 //            if mesh actor is not visible - find any first visible group or submesh
2106 //=================================================================================
2107 bool SMESHGUI_GroupDlg::SetAppropriateActor()
2108 {
2109   bool isActor = false;
2110
2111   if (myMesh->_is_nil()) return false;
2112
2113   SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView();
2114
2115   // try mesh actor
2116   myActor = SMESH::FindActorByObject(myMesh);
2117   if (myActor && myActor->hasIO())
2118   {
2119     isActor = true;
2120     if (aViewWindow && !aViewWindow->isVisible(myActor->getIO()))
2121         isActor = false;
2122   }
2123
2124   // try current group actor
2125   if (!isActor) {
2126     if (!myGroup->_is_nil()) {
2127       myActor = SMESH::FindActorByObject(myGroup);
2128       if (myActor && myActor->hasIO())
2129       {
2130         isActor = true;
2131         if (aViewWindow && !aViewWindow->isVisible(myActor->getIO()))
2132             isActor = false;
2133       }
2134     }
2135   }
2136
2137   // try current group on geometry actor
2138   if (!isActor) {
2139     if (!myGroupOnGeom->_is_nil()) {
2140       myActor = SMESH::FindActorByObject(myGroupOnGeom);
2141       if (myActor && myActor->hasIO())
2142       {
2143         isActor = true;
2144         if (aViewWindow && !aViewWindow->isVisible(myActor->getIO()))
2145           isActor = false;
2146       }
2147     }
2148   }
2149
2150   // try any visible actor of group or submesh of current mesh
2151   if (!isActor && aViewWindow) {
2152     // mesh entry
2153     _PTR(SObject) aSObject = SMESH::FindSObject(myMesh);
2154     if (aSObject) {
2155       CORBA::String_var meshEntry = aSObject->GetID().c_str();
2156       int len = strlen(meshEntry);
2157
2158       // iterate on all actors in current view window, search for
2159       // any visible actor, that belongs to group or submesh of current mesh
2160       vtkActorCollection *aCollection = aViewWindow->getRenderer()->GetActors();
2161       aCollection->InitTraversal();
2162       for (vtkActor *anAct = aCollection->GetNextActor();
2163            anAct && !isActor;
2164            anAct = aCollection->GetNextActor())
2165       {
2166         SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct);
2167         if (anActor && anActor->hasIO()) {
2168           Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
2169           if (aViewWindow->isVisible(anIO)) {
2170             if (anIO->hasEntry() && strncmp(anIO->getEntry(), meshEntry, len) == 0) {
2171               myActor = anActor;
2172               isActor = true;
2173             }
2174           }
2175         }
2176       }
2177     }
2178   }
2179
2180   if (isActor)
2181     SMESH::SetPickable(myActor);
2182
2183   return isActor;
2184 }
2185
2186 //=======================================================================
2187 //function : setShowEntityMode
2188 //purpose  : make shown only entity corresponding to my type
2189 //=======================================================================
2190 void SMESHGUI_GroupDlg::setShowEntityMode()
2191 {
2192   if ( !myMesh->_is_nil() ) {
2193     if ( SMESH_Actor* actor = SMESH::FindActorByObject(myMesh) ) {
2194       if (!myStoredShownEntity)
2195         myStoredShownEntity = actor->GetEntityMode();
2196       switch ( myTypeId ) {
2197       case 0: restoreShowEntityMode(); break;
2198       case 1: actor->SetEntityMode( SMESH_Actor::eEdges ); break;
2199       case 2: actor->SetEntityMode( SMESH_Actor::eFaces ); break;
2200       case 3: actor->SetEntityMode( SMESH_Actor::eVolumes ); break;
2201       }
2202     }
2203   }
2204 }
2205
2206 //=======================================================================
2207 //function : restoreShowEntityMode
2208 //purpose  : restore ShowEntity mode of myActor
2209 //=======================================================================
2210 void SMESHGUI_GroupDlg::restoreShowEntityMode()
2211 {
2212   if ( myStoredShownEntity && !myMesh->_is_nil() ) {
2213     if ( SMESH_Actor* actor = SMESH::FindActorByObject(myMesh) ) {
2214       actor->SetEntityMode(myStoredShownEntity);
2215     }
2216   }
2217   myStoredShownEntity = 0;
2218 }