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