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