Salome HOME
IPAL21668 Call of Edit Group leads to Crash.
[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       if ( aGroupShapeSO )
594         aShapeName = aGroupShapeSO->GetName().c_str();
595     }
596     myGeomGroupLine->setText( aShapeName );
597     myNameChanged = true;
598     myName->blockSignals(true);
599     myName->setText(theGroup->GetName());
600     myName->blockSignals(false);
601   }
602   updateButtons();
603 }
604
605 //=================================================================================
606 // function : updateButtons()
607 // purpose  :
608 //=================================================================================
609 void SMESHGUI_GroupDlg::updateButtons()
610 {
611   bool enable = !myName->text().trimmed().isEmpty();
612
613   if (myGrpTypeId == 0) {
614     enable = enable && myElements->count() > 0;
615     enable = enable && (!myGroup->_is_nil() || !myMesh->_is_nil());
616   }
617   else if (myGrpTypeId == 1) {
618     if (CORBA::is_nil(myGroupOnGeom)) { // creation mode
619       enable = enable && myGeomObjects->length() > 0 && !myMesh->_is_nil();
620     }
621   }
622
623   myOKBtn->setEnabled(enable);
624   myApplyBtn->setEnabled(enable);
625 }
626
627 //=================================================================================
628 // function : onNameChanged()
629 // purpose  :
630 //=================================================================================
631 void SMESHGUI_GroupDlg::onNameChanged (const QString& text)
632 {
633   myOldName = myName->text();
634   updateButtons();
635   myNameChanged = !myName->text().trimmed().isEmpty();
636 }
637
638 //=================================================================================
639 // function : onTypeChanged()
640 // purpose  : Group elements type radio button management
641 //=================================================================================
642 void SMESHGUI_GroupDlg::onTypeChanged (int id)
643 {
644   if (myTypeId != id) {
645     myElements->clear();
646     if (myCurrentLineEdit == 0)
647       setSelectionMode(id);
648     myTypeId = id;
649     setShowEntityMode();
650   }
651 }
652
653 //=================================================================================
654 // function : onGrpTypeChanged()
655 // purpose  : Group type radio button management
656 //=================================================================================
657 void SMESHGUI_GroupDlg::onGrpTypeChanged (int id)
658 {
659   if (myGrpTypeId != id) {
660     myWGStack->setCurrentIndex( id );
661     myName->blockSignals(true);
662     myName->setText(myOldName);
663     myName->blockSignals(false);
664     onSelectGeomGroup(id == 1);
665   }
666   myGrpTypeId = id;
667 }
668
669 //=================================================================================
670 // function : onColorChanged()
671 // purpose  : Color button management
672 //=================================================================================
673 void SMESHGUI_GroupDlg::onColorChanged(QColor theColor)
674 {
675   updateButtons();
676 }
677
678 //=================================================================================
679 // function : setSelectionMode()
680 // purpose  : Radio button management
681 //=================================================================================
682 void SMESHGUI_GroupDlg::setSelectionMode (int theMode)
683 {
684   // PAL7314
685   if (myMesh->_is_nil())
686     return;
687   if (mySelectionMode != theMode) {
688     // [PAL10408] mySelectionMgr->clearSelected();
689     mySelectionMgr->clearFilters();
690     if (myActorsList.count() > 0) {
691       QListIterator<SMESH_Actor*> it( myActorsList );
692       while ( it.hasNext() )
693         it.next()->SetPointRepresentation(false);
694     }
695     else
696       SMESH::SetPointRepresentation(false);
697     if (theMode < 4) {
698       switch (theMode) {
699       case 0:
700         if (myActorsList.count() > 0) {
701           QListIterator<SMESH_Actor*> it( myActorsList );
702           while ( it.hasNext() )
703             it.next()->SetPointRepresentation(true);
704         }
705         else
706           SMESH::SetPointRepresentation(true);
707         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
708           aViewWindow->SetSelectionMode(NodeSelection);
709         break;
710       case 1:
711         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
712           aViewWindow->SetSelectionMode(EdgeSelection);
713         break;
714       case 2:
715         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
716           aViewWindow->SetSelectionMode(FaceSelection);
717         break;
718       default:
719         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
720           aViewWindow->SetSelectionMode(VolumeSelection);
721       }
722     } else {
723       if (theMode == 4)
724         mySelectionMgr->installFilter(mySubMeshFilter);
725       else if (theMode == 5)
726         mySelectionMgr->installFilter(myGroupFilter);
727       else if (theMode == 6)
728         mySelectionMgr->installFilter(myMeshFilter);
729       else if (theMode == 7)
730         mySelectionMgr->installFilter(myGeomFilter);
731
732       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
733         aViewWindow->SetSelectionMode(ActorSelection);
734     }
735     mySelectionMode = theMode;
736   }
737 }
738
739 //=================================================================================
740 // function : onApply()
741 // purpose  :
742 //=================================================================================
743 bool SMESHGUI_GroupDlg::onApply()
744 {
745   if (mySMESHGUI->isActiveStudyLocked())
746     return false;
747
748   if (myName->text().trimmed().isEmpty())
749     return false;
750
751   if (myGrpTypeId == 0) { // on mesh elements
752     if (!myElements->count())
753       return false;
754
755     mySelectionMgr->clearSelected();
756
757     if (myGroup->_is_nil()) { // creation or conversion
758       // check if group on geometry is not null
759       if (!CORBA::is_nil(myGroupOnGeom)) {
760         if (myMesh->_is_nil())
761           return false;
762         myGroup = myMesh->ConvertToStandalone( myGroupOnGeom );
763         // nullify pointer, because object become dead
764         myGroupOnGeom = SMESH::SMESH_GroupOnGeom::_nil();
765       }
766     }
767
768     if (myGroup->_is_nil()) { // creation
769       if (myMesh->_is_nil())
770         return false;
771
772       SMESH::ElementType aType = SMESH::ALL;
773       switch (myTypeId) {
774       case 0: aType = SMESH::NODE; break;
775       case 1: aType = SMESH::EDGE; break;
776       case 2: aType = SMESH::FACE; break;
777       case 3: aType = SMESH::VOLUME; break;
778       }
779
780       SMESH::long_array_var anIdList = new SMESH::long_array;
781       int i, k = myElements->count();
782       anIdList->length(k);
783       for (i = 0; i < k; i++) {
784         anIdList[i] = myElements->item(i)->text().toInt();
785       }
786
787       myGroup = SMESH::AddGroup(myMesh, aType, myName->text());
788       myGroup->Add(anIdList.inout());
789
790       SALOMEDS::Color aColor = getGroupColor();
791       myGroup->SetColor(aColor);
792
793       _PTR(SObject) aMeshGroupSO = SMESH::FindSObject(myGroup);
794
795       //SMESH::setFileName ( aMeshGroupSO, QString::number(myColorSpinBox->value()) );
796       SMESH::setFileType ( aMeshGroupSO, "COULEURGROUP" );
797
798       /* init for next operation */
799       myName->setText( "" );
800       myElements->clear();
801       myGroup = SMESH::SMESH_Group::_nil();
802
803     } else { // edition
804       myGroup->SetName(myName->text().toLatin1().data());
805
806       SALOMEDS::Color aColor = getGroupColor();
807       myGroup->SetColor(aColor);
808
809       _PTR(SObject) aMeshGroupSO = SMESH::FindSObject(myGroup);
810       if(SMESH_Actor *anActor = SMESH::FindActorByEntry(aMeshGroupSO->GetID().c_str())) {
811         anActor->setName(myName->text().toLatin1().data());
812         switch ( myTypeId ) {
813         case 0: anActor->SetNodeColor( aColor.R, aColor.G, aColor.B ); break;
814         case 1: anActor->SetEdgeColor( aColor.R, aColor.G, aColor.B ); break;
815         case 2:
816         case 3: anActor->SetSufaceColor( aColor.R, aColor.G, aColor.B ); break;
817         }
818       }
819
820       QList<int> aAddList;
821
822       int i, total = myElements->count();
823       for (i = 0; i < total; i++) {
824         int anId = myElements->item(i)->text().toInt();
825         int idx = myIdList.indexOf(anId);
826         if ( idx == -1 )
827           aAddList.append(anId);
828         else
829           myIdList.removeAt(idx);
830       }
831       if (!aAddList.empty()) {
832         SMESH::long_array_var anIdList = new SMESH::long_array;
833         int added = aAddList.count();
834         anIdList->length(added);
835         for (i = 0; i < added; i++)
836           anIdList[i] = aAddList[i];
837         myGroup->Add(anIdList.inout());
838       }
839       if (!myIdList.empty()) {
840         SMESH::long_array_var anIdList = new SMESH::long_array;
841         int removed = myIdList.count();
842         anIdList->length(removed);
843         for (i = 0; i < removed; i++)
844           anIdList[i] = myIdList[i];
845         myGroup->Remove(anIdList.inout());
846       }
847       /* init for next operation */
848       myIdList.clear();
849       for (i = 0; i < total; i++) {
850         myIdList.append(myElements->item(i)->text().toInt());
851       }
852     }
853
854     mySMESHGUI->updateObjBrowser(true);
855     SMESH::UpdateView(); // asv: fix of BUG PAL5515
856     mySelectionMgr->clearSelected();
857     return true;
858   }
859   else if (myGrpTypeId == 1) { // on geom object
860     if (CORBA::is_nil(myGroupOnGeom)) { // creation
861       if (myMesh->_is_nil() || !myGeomObjects->length())
862         return false;
863
864       SMESH::ElementType aType = SMESH::ALL;
865       switch (myTypeId) {
866       case 0: aType = SMESH::NODE; break;
867       case 1: aType = SMESH::EDGE; break;
868       case 2: aType = SMESH::FACE; break;
869       case 3: aType = SMESH::VOLUME; break;
870       }
871
872       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
873       GEOM::GEOM_IGroupOperations_var aGroupOp =
874         SMESH::GetGEOMGen()->GetIGroupOperations(aStudy->StudyId());
875
876       if (myGeomObjects->length() == 1) {
877         myGroupOnGeom = myMesh->CreateGroupFromGEOM(aType,
878                                                     myName->text().toLatin1().data(),
879                                                     myGeomObjects[0]);
880       }
881       else {
882         SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
883         if ( aSMESHGen->_is_nil() )
884           return false;
885
886         // create a geometry group
887         GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
888         _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
889
890         if (geomGen->_is_nil() || !aStudy)
891           return false;
892
893         GEOM::GEOM_IGroupOperations_var op =
894           geomGen->GetIGroupOperations(aStudy->StudyId());
895         if (op->_is_nil())
896           return false;
897
898         // check and add all selected GEOM objects: they must be
899         // a sub-shapes of the main GEOM and must be of one type
900         TopAbs_ShapeEnum aGroupType = TopAbs_SHAPE;
901         for ( int i =0; i < myGeomObjects->length(); i++) {
902           TopAbs_ShapeEnum aSubShapeType = (TopAbs_ShapeEnum)myGeomObjects[i]->GetShapeType();
903           if (i == 0)
904             aGroupType = aSubShapeType;
905           else if (aSubShapeType != aGroupType) {
906             aGroupType = TopAbs_SHAPE;
907             break;
908           }
909         }
910
911         GEOM::GEOM_Object_var aMeshShape = myMesh->GetShapeToMesh();
912         GEOM::GEOM_Object_var aGroupVar = op->CreateGroup(aMeshShape, aGroupType);
913         op->UnionList(aGroupVar, myGeomObjects);
914
915         if (op->IsDone()) {
916           // publish the GEOM group in study
917           QString aNewGeomGroupName ( "Auto_group_for_" );
918           aNewGeomGroupName += myName->text();
919           SALOMEDS::SObject_var aNewGroupSO =
920             geomGen->AddInStudy(aSMESHGen->GetCurrentStudy(), aGroupVar,
921                                 aNewGeomGroupName.toLatin1().data(), aMeshShape);
922         }
923
924         myGroupOnGeom = myMesh->CreateGroupFromGEOM(aType,
925                                                     myName->text().toLatin1().data(),
926                                                     aGroupVar);
927       }
928
929       SALOMEDS::Color aColor = getGroupColor();
930       myGroupOnGeom->SetColor(aColor);
931
932       _PTR(SObject) aMeshGroupSO = SMESH::FindSObject(myGroupOnGeom);
933
934       //SMESH::setFileName ( aMeshGroupSO, QString::number(myColorSpinBox->value()) );
935       SMESH::setFileType ( aMeshGroupSO,"COULEURGROUP" );
936
937       /* init for next operation */
938       myName->setText( "" );
939       myGroupOnGeom = SMESH::SMESH_GroupOnGeom::_nil();
940     }
941     else { // edition
942       myGroupOnGeom->SetName(myName->text().toLatin1().data());
943
944       SALOMEDS::Color aColor = getGroupColor();
945       myGroupOnGeom->SetColor(aColor);
946
947       _PTR(SObject) aMeshGroupSO = SMESH::FindSObject(myGroupOnGeom);
948       if(SMESH_Actor *anActor = SMESH::FindActorByEntry(aMeshGroupSO->GetID().c_str())) {
949         anActor->setName(myName->text().toLatin1().data());
950         switch ( myTypeId ) {
951         case 0: anActor->SetNodeColor( aColor.R, aColor.G, aColor.B ); break;
952         case 1: anActor->SetEdgeColor( aColor.R, aColor.G, aColor.B ); break;
953         case 2:
954         case 3: anActor->SetSufaceColor( aColor.R, aColor.G, aColor.B ); break;
955         }
956       }
957     }
958
959     mySMESHGUI->updateObjBrowser(true);
960     mySelectionMgr->clearSelected();
961     return true;
962   }
963
964   return false;
965 }
966
967 //=================================================================================
968 // function : onOK()
969 // purpose  :
970 //=================================================================================
971 void SMESHGUI_GroupDlg::onOK()
972 {
973   if ( onApply() )
974     onClose();
975 }
976
977 //=================================================================================
978 // function : onListSelectionChanged()
979 // purpose  : Called when selection in element list is changed
980 //=================================================================================
981 void SMESHGUI_GroupDlg::onListSelectionChanged()
982 {
983   //MESSAGE( "SMESHGUI_GroupDlg::onListSelectionChanged(); myActorsList.count() = " << myActorsList.count());
984   if( myIsBusy || myActorsList.count() == 0 ) return;
985   myIsBusy = true;
986
987   if (myCurrentLineEdit == 0) {
988     mySelectionMgr->clearSelected();
989     TColStd_MapOfInteger aIndexes;
990     QList<QListWidgetItem*> selItems = myElements->selectedItems();
991     QListWidgetItem* anItem;
992     foreach(anItem, selItems) aIndexes.Add(anItem->text().toInt());
993     mySelector->AddOrRemoveIndex(myActorsList.first()->getIO(), aIndexes, false);
994     SALOME_ListIO aList;
995     aList.Append(myActorsList.first()->getIO());
996     mySelectionMgr->setSelectedObjects(aList,false);
997   }
998   myIsBusy = false;
999 }
1000
1001 //=================================================================================
1002 // function : onObjectSelectionChanged()
1003 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
1004 //=================================================================================
1005 void SMESHGUI_GroupDlg::onObjectSelectionChanged()
1006 {
1007   if ( myIsBusy || !isEnabled()) return;
1008   if (myCurrentLineEdit == myGeomGroupLine && !myGeomGroupBtn->isChecked()) return;
1009
1010   myIsBusy = true;
1011
1012   SALOME_ListIO aList;
1013   mySelectionMgr->selectedObjects( aList );
1014
1015   int aNbSel = aList.Extent();
1016   myElements->clearSelection();
1017
1018   if (myCurrentLineEdit)
1019   {
1020     myCurrentLineEdit->setText( "" );
1021     QString aString = "";
1022
1023     if (myCurrentLineEdit == myMeshGroupLine)
1024     {
1025       mySelectSubMesh->setEnabled(false);
1026       mySelectGroup->setEnabled(false);
1027       myGroupLine->setText( "" );
1028       mySubMeshLine->setText( "" );
1029
1030       myGeomGroupBtn->setEnabled(false);
1031       myGeomGroupLine->setEnabled(false);
1032       myGeomGroupLine->setText( "" );
1033       myGeomObjects = new GEOM::ListOfGO();
1034       myGeomObjects->length(0);
1035
1036       if (myGeomGroupBtn->isChecked())
1037         myGeomGroupBtn->setChecked(false);
1038       if (!myCreate)
1039         myName->setText( "" );
1040
1041       myElements->clear();
1042
1043       if (aNbSel != 1 ) {
1044         myGroup = SMESH::SMESH_Group::_nil();
1045         myGroupOnGeom = SMESH::SMESH_GroupOnGeom::_nil();
1046         restoreShowEntityMode();
1047         myMesh = SMESH::SMESH_Mesh::_nil();
1048         updateGeomPopup();
1049         updateButtons();
1050         myIsBusy = false;
1051         return;
1052       }
1053       Handle(SALOME_InteractiveObject) IO = aList.First();
1054
1055       if (myCreate) {
1056         restoreShowEntityMode();
1057         myMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(IO);
1058         setShowEntityMode();
1059         updateGeomPopup();
1060         if (myMesh->_is_nil())
1061         {
1062           updateButtons();
1063           myIsBusy = false;
1064           return;
1065         }
1066
1067         if ( myFilterDlg && !myMesh->_is_nil()){
1068           myFilterDlg->SetMesh( myMesh );
1069         }
1070         myGroup = SMESH::SMESH_Group::_nil();
1071
1072         // NPAL19389: create a group with a selection in another group
1073         // set actor of myMesh, if it is visible, else try
1074         // any visible actor of group or submesh of myMesh
1075         SetAppropriateActor();
1076
1077         aString = aList.First()->getName();
1078         myMeshGroupLine->setText(aString);
1079         myMeshGroupLine->home( false );
1080
1081         mySelectSubMesh->setEnabled(true);
1082         mySelectGroup->setEnabled(true);
1083         myGeomGroupBtn->setEnabled(true);
1084         myGeomGroupLine->setEnabled(true);
1085         updateButtons();
1086       }
1087       else {
1088         SMESH::SMESH_GroupBase_var aGroup = SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(IO);
1089         if (aGroup->_is_nil())
1090         {
1091           myIsBusy = false;
1092           return;
1093         }
1094         myIsBusy = false;
1095
1096         myGroup = SMESH::SMESH_Group::_nil();
1097         myGroupOnGeom = SMESH::SMESH_GroupOnGeom::_nil();
1098
1099         init(aGroup);
1100         myIsBusy = true;
1101         mySelectSubMesh->setEnabled(true);
1102         mySelectGroup->setEnabled(true);
1103       }
1104       myCurrentLineEdit = 0;
1105       myIsBusy = false;
1106       if (!myCreate)
1107         return;
1108
1109       if (myGrpTypeId == 0)
1110       {
1111         if (myTypeId == -1)
1112           onTypeChanged(0);
1113         else
1114         {
1115           myElements->clear();
1116           setSelectionMode(myTypeId);
1117         }
1118       }
1119
1120       myIsBusy = false;
1121       return;
1122
1123     }
1124     else if (myCurrentLineEdit == myGeomGroupLine)
1125     {
1126       myGeomObjects = new GEOM::ListOfGO();
1127
1128       // The mesh SObject
1129       _PTR(SObject) aMeshSO = SMESH::FindSObject(myMesh);
1130
1131       if (aNbSel == 0 || !aMeshSO)
1132       {
1133         myGeomObjects->length(0);
1134         updateButtons();
1135         myIsBusy = false;
1136         return;
1137       }
1138
1139       myGeomObjects->length(aNbSel);
1140
1141       GEOM::GEOM_Object_var aGeomGroup;
1142       Standard_Boolean testResult;
1143       int i = 0;
1144
1145       SALOME_ListIteratorOfListIO anIt (aList);
1146       for (; anIt.More(); anIt.Next())
1147       {
1148         testResult = Standard_False;
1149         aGeomGroup = GEOMBase::ConvertIOinGEOMObject(anIt.Value(), testResult);
1150
1151         // Check if the object is a geometry group
1152         if (!testResult || CORBA::is_nil(aGeomGroup))
1153           continue;
1154
1155         // Check if group constructed on the same shape as a mesh or on its child
1156         _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1157         GEOM::GEOM_IGroupOperations_var anOp =
1158           SMESH::GetGEOMGen()->GetIGroupOperations(aStudy->StudyId());
1159
1160         // The main shape of the group
1161         GEOM::GEOM_Object_var aGroupMainShape;
1162         if (aGeomGroup->GetType() == 37)
1163           aGroupMainShape = anOp->GetMainShape(aGeomGroup);
1164         else
1165           aGroupMainShape = GEOM::GEOM_Object::_duplicate(aGeomGroup);
1166         _PTR(SObject) aGroupMainShapeSO =
1167           //aStudy->FindObjectIOR(aStudy->ConvertObjectToIOR(aGroupMainShape));
1168           aStudy->FindObjectID(aGroupMainShape->GetStudyEntry());
1169
1170         _PTR(SObject) anObj, aRef;
1171         bool isRefOrSubShape = false;
1172         if (aMeshSO->FindSubObject(1, anObj) &&  anObj->ReferencedObject(aRef)) {
1173           //if (strcmp(aRef->GetID(), aGroupMainShapeSO->GetID()) == 0) {
1174           if (aRef->GetID() == aGroupMainShapeSO->GetID()) {
1175             isRefOrSubShape = true;
1176           } else {
1177             _PTR(SObject) aFather = aGroupMainShapeSO->GetFather();
1178             _PTR(SComponent) aComponent = aGroupMainShapeSO->GetFatherComponent();
1179             //while (!isRefOrSubShape && strcmp(aFather->GetID(), aComponent->GetID()) != 0) {
1180             while (!isRefOrSubShape && aFather->GetID() != aComponent->GetID()) {
1181               //if (strcmp(aRef->GetID(), aFather->GetID()) == 0)
1182               if (aRef->GetID() == aFather->GetID())
1183                 isRefOrSubShape = true;
1184               else
1185                 aFather = aFather->GetFather();
1186             }
1187           }
1188         }
1189         if (isRefOrSubShape)
1190           myGeomObjects[i++] = aGeomGroup;
1191       }
1192
1193       myGeomObjects->length(i);
1194       if ( i == 0 )
1195         {
1196           myIsBusy = false;
1197           return;
1198         }
1199
1200       aNbSel = i;
1201     }
1202
1203     if (aNbSel >= 1) {
1204       if (aNbSel > 1) {
1205         if (myCurrentLineEdit == mySubMeshLine)
1206           aString = tr( "SMESH_SUBMESH_SELECTED" ).arg(aNbSel);
1207         else if (myCurrentLineEdit == myGroupLine)
1208           aString = tr( "SMESH_GROUP_SELECTED" ).arg(aNbSel);
1209         else if (myCurrentLineEdit == myGeomGroupLine)
1210           aString = tr( "%1 Objects" ).arg(aNbSel);
1211       }
1212       else {
1213         aString = aList.First()->getName();
1214       }
1215     }
1216
1217     myCurrentLineEdit->setText(aString);
1218     myCurrentLineEdit->home(false);
1219     // 07.06.2008 skl for IPAL19574:
1220     // change name of group only if it is empty
1221     if( myName->text().trimmed().isEmpty() || !myNameChanged ) {
1222       myOldName = myName->text();
1223       myName->blockSignals(true);
1224       myName->setText(aString);
1225       myName->blockSignals(false);
1226     }
1227
1228     updateButtons();
1229   }
1230   else // !myCurrentLineEdit: local selection of nodes or elements
1231   {
1232     if (aNbSel == 1 && myActorsList.count() > 0 )
1233     {
1234       // NPAL19389: create a group with a selection in another group
1235       // Switch myActor to the newly selected one, if the last
1236       // is visible and belongs to group or submesh of myMesh
1237       /*      Handle(SALOME_InteractiveObject) curIO = myActor->getIO();
1238       Handle(SALOME_InteractiveObject) selIO = aList.First();
1239       if (curIO->hasEntry() && selIO->hasEntry()) {
1240         const char* selEntry = selIO->getEntry();
1241         if (strcmp(curIO->getEntry(), selEntry) != 0) {
1242           // different objects: selected and myActor
1243           SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView();
1244           if (aViewWindow && aViewWindow->isVisible(selIO)) {
1245             // newly selected actor is visible
1246
1247             // mesh entry
1248             _PTR(SObject) aSObject = SMESH::FindSObject(myMesh);
1249             if (aSObject) {
1250               CORBA::String_var meshEntry = aSObject->GetID().c_str();
1251               int len = strlen(meshEntry);
1252
1253               if (strncmp(selEntry, meshEntry, len) == 0) {
1254                 // selected object is myMesh or a part of it
1255                 SMESH_Actor* anActor = SMESH::FindActorByEntry(selEntry);
1256                 if (anActor) {
1257                   myActor = anActor;
1258                   SMESH::SetPickable(myActor);
1259                 }
1260               }
1261             }
1262           }
1263         }
1264       }*/
1265       // NPAL19389 END
1266
1267       QString aListStr = "";
1268       int aNbItems = 0;
1269       if (myTypeId == 0) {
1270         QListIterator<SMESH_Actor*> it( myActorsList );
1271         while ( it.hasNext() ) {
1272           QString tmpStr;
1273           aNbItems += SMESH::GetNameOfSelectedNodes(mySelector, it.next()->getIO(), tmpStr);
1274           aListStr += tmpStr;
1275         }
1276       } else {
1277         QListIterator<SMESH_Actor*> it( myActorsList );
1278         while ( it.hasNext() ) {
1279           QString tmpStr;
1280           aNbItems += SMESH::GetNameOfSelectedElements(mySelector, it.next()->getIO(), tmpStr);
1281           aListStr += tmpStr;
1282         }
1283       }
1284       if (aNbItems > 0) {
1285         QListWidgetItem* anItem;
1286         QList<QListWidgetItem*> listItemsToSel;
1287         QStringList anElements = aListStr.split( " ", QString::SkipEmptyParts);
1288         for (QStringList::iterator it = anElements.begin(); it != anElements.end(); ++it) {
1289           QList<QListWidgetItem*> found = myElements->findItems(*it, Qt::MatchExactly);
1290           foreach(anItem, found)
1291             if (!anItem->isSelected())
1292               listItemsToSel.push_back(anItem);
1293         }
1294         bool blocked = myElements->signalsBlocked();
1295         myElements->blockSignals(true);
1296         foreach(anItem, listItemsToSel) anItem->setSelected(true);
1297         myElements->blockSignals(blocked);
1298         onListSelectionChanged();
1299         listItemsToSel.clear();
1300       }
1301     }
1302   }
1303   
1304   if (myActorsList.count() == 0) {
1305     if (!myGroup->_is_nil()) {
1306       SMESH_Actor* anActor = SMESH::FindActorByObject(myGroup);
1307       if ( anActor )
1308         myActorsList.append( anActor  );
1309     }
1310     else if(!myGroupOnGeom->_is_nil()) {
1311       SMESH_Actor* anActor = SMESH::FindActorByObject(myGroupOnGeom);
1312       if ( anActor )
1313         myActorsList.append( anActor );
1314     }
1315     else {
1316       SMESH_Actor* anActor = SMESH::FindActorByObject( myMesh );
1317       if ( anActor )
1318         myActorsList.append( anActor );
1319     }
1320   }
1321
1322   // somehow, if we display the mesh, while selecting from another actor,
1323   // the mesh becomes pickable, and there is no way to select any element
1324   if (myActorsList.count() > 0) {
1325     QListIterator<SMESH_Actor*> it( myActorsList );
1326     while ( it.hasNext() ) {
1327       SMESH_Actor* anActor = it.next();
1328       if ( IsActorVisible(anActor) )
1329         anActor->SetPickable(true);
1330     }
1331   }
1332
1333   myIsBusy = false;
1334 }
1335
1336 //=================================================================================
1337 // function : onSelectSubMesh()
1338 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
1339 //=================================================================================
1340 void SMESHGUI_GroupDlg::onSelectSubMesh(bool on)
1341 {
1342   if (on) {
1343     if (mySelectGroup->isChecked()) {
1344       mySelectGroup->setChecked(false);
1345     }
1346     //VSR: else if (mySelectGeomGroup->isChecked()) {
1347     //VSR:   mySelectGeomGroup->setChecked(false);
1348     //VSR: }
1349     myCurrentLineEdit = mySubMeshLine;
1350     setSelectionMode(4);
1351   }
1352   else {
1353     mySubMeshLine->setText( "" );
1354     myCurrentLineEdit = 0;
1355     if (myTypeId != -1)
1356       setSelectionMode(myTypeId);
1357   }
1358   mySubMeshBtn->setEnabled(on);
1359   mySubMeshLine->setEnabled(on);
1360 }
1361
1362
1363 //=================================================================================
1364 // function : (onSelectGroup)
1365 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
1366 //=================================================================================
1367 void SMESHGUI_GroupDlg::onSelectGroup(bool on)
1368 {
1369   if (on) {
1370     if (mySelectSubMesh->isChecked()) {
1371       mySelectSubMesh->setChecked(false);
1372     }
1373     myCurrentLineEdit = myGroupLine;
1374     setSelectionMode(5);
1375   }
1376   else {
1377     myGroupLine->setText( "" );
1378     myCurrentLineEdit = 0;
1379     if (myTypeId != -1)
1380       setSelectionMode(myTypeId);
1381   }
1382   myGroupBtn->setEnabled(on);
1383   myGroupLine->setEnabled(on);
1384 }
1385
1386
1387 //=================================================================================
1388 // function : (onSelectGeomGroup)
1389 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
1390 //=================================================================================
1391 void SMESHGUI_GroupDlg::onSelectGeomGroup(bool on)
1392 {
1393   if (on) {
1394     if (mySelectSubMesh->isChecked()) {
1395       mySelectSubMesh->setChecked(false);
1396     }
1397     else if (mySelectGroup->isChecked()) {
1398       mySelectGroup->setChecked(false);
1399     }
1400     myCurrentLineEdit = myGeomGroupLine;
1401     updateGeomPopup();
1402     setSelectionMode(8);
1403   }
1404   else {
1405     myGeomGroupBtn->setChecked(false);
1406     myGeomObjects->length(0);
1407     myGeomGroupLine->setText( "" );
1408     myCurrentLineEdit = 0;
1409     if (myTypeId != -1)
1410       setSelectionMode(myTypeId);
1411   }
1412 }
1413
1414
1415 //=================================================================================
1416 // function : setCurrentSelection()
1417 // purpose  :
1418 //=================================================================================
1419 void SMESHGUI_GroupDlg::setCurrentSelection()
1420 {
1421   QPushButton* send = (QPushButton*)sender();
1422   myCurrentLineEdit = 0;
1423   if (send == myMeshGroupBtn) {
1424     disconnect(myMeshGroupBtn, SIGNAL(clicked()), this, SLOT(setCurrentSelection()));
1425     mySelectionMgr->clearSelected();
1426     if (myCreate)
1427       setSelectionMode(6);
1428     else
1429       setSelectionMode(5);
1430     connect(myMeshGroupBtn, SIGNAL(clicked()), this, SLOT(setCurrentSelection()));
1431     myCurrentLineEdit = myMeshGroupLine;
1432     onObjectSelectionChanged();
1433   }
1434   else if (send == mySubMeshBtn) {
1435     myCurrentLineEdit = mySubMeshLine;
1436     onObjectSelectionChanged();
1437   }
1438   else if (send == myGroupBtn) {
1439     myCurrentLineEdit = myGroupLine;
1440     onObjectSelectionChanged();
1441   }
1442 }
1443
1444
1445 //=================================================================================
1446 // function : setFilters()
1447 // purpose  : SLOT. Called when "Filter" button pressed.
1448 //=================================================================================
1449 void SMESHGUI_GroupDlg::setFilters()
1450 {
1451   if(myMesh->_is_nil()) {
1452     SUIT_MessageBox::critical(this,
1453                               tr("SMESH_ERROR"),
1454                               tr("NO_MESH_SELECTED"));
1455    return;
1456   }
1457
1458   SMESH::ElementType aType = SMESH::ALL;
1459   switch ( myTypeId )
1460   {
1461     case 0 : aType = SMESH::NODE; break;
1462     case 1 : aType = SMESH::EDGE; break;
1463     case 2 : aType = SMESH::FACE; break;
1464     case 3 : aType = SMESH::VOLUME; break;
1465     default: return;
1466   }
1467
1468   if ( myFilterDlg == 0 )
1469   {
1470     myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, aType );
1471     connect( myFilterDlg, SIGNAL( Accepted() ), SLOT( onFilterAccepted() ) );
1472   }
1473   else
1474     myFilterDlg->Init( aType );
1475
1476   myFilterDlg->SetSelection();
1477   myFilterDlg->SetMesh( myMesh );
1478   myFilterDlg->SetSourceWg( myElements, false );
1479
1480   myFilterDlg->show();
1481 }
1482
1483 //=================================================================================
1484 // function : onFilterAccepted()
1485 // purpose  : SLOT. Called when Filter dlg closed with OK button.
1486 //            Uncheck "Select submesh" and "Select group" checkboxes
1487 //=================================================================================
1488 void SMESHGUI_GroupDlg::onFilterAccepted()
1489 {
1490   if ( mySelectSubMesh->isChecked() || mySelectGroup->isChecked() )
1491   {
1492     mySelectionMode = myTypeId;
1493     mySelectSubMesh->setChecked( false );
1494     mySelectGroup->setChecked( false );
1495   }
1496 }
1497
1498 //=================================================================================
1499 // function : onAdd()
1500 // purpose  :
1501 //=================================================================================
1502 void SMESHGUI_GroupDlg::onAdd()
1503 {
1504   SALOME_ListIO aList;
1505   mySelectionMgr->selectedObjects( aList );
1506
1507   int aNbSel = aList.Extent();
1508
1509   if (aNbSel == 0 || myActorsList.count() == 0 || myMesh->_is_nil()) return;
1510
1511   myIsBusy = true;
1512
1513   SMESH::ElementType aType = SMESH::ALL;
1514   switch(myTypeId) {
1515   case 0:
1516     aType = SMESH::NODE;
1517     mySelector->SetSelectionMode(NodeSelection);
1518     break;
1519   case 1:
1520     aType = SMESH::EDGE;
1521     mySelector->SetSelectionMode(EdgeSelection);
1522     break;
1523   case 2:
1524     aType = SMESH::FACE;
1525     mySelector->SetSelectionMode(FaceSelection);
1526     break;
1527   case 3:
1528     aType = SMESH::VOLUME;
1529     mySelector->SetSelectionMode(VolumeSelection);
1530     break;
1531   default:
1532     mySelector->SetSelectionMode(ActorSelection);
1533   }
1534
1535   QListWidgetItem* anItem = 0;
1536   QList<QListWidgetItem*> listItemsToSel;
1537
1538   if (myCurrentLineEdit == 0) {
1539     //if (aNbSel != 1) { myIsBusy = false; return; }
1540     QString aListStr = "";
1541     int aNbItems = 0;
1542     if (myTypeId == 0) {
1543       QListIterator<SMESH_Actor*> it( myActorsList );
1544       while ( it.hasNext() ) {
1545         QString tmpStr;
1546         aNbItems += SMESH::GetNameOfSelectedNodes(mySelector, it.next()->getIO(), tmpStr);
1547         aListStr += tmpStr;
1548       }
1549     }
1550     else {
1551       QListIterator<SMESH_Actor*> it( myActorsList );
1552       while ( it.hasNext() ) {
1553         QString tmpStr;
1554         aNbItems += SMESH::GetNameOfSelectedElements(mySelector, it.next()->getIO(), tmpStr);
1555         aListStr += tmpStr;
1556       }
1557     }
1558     if (aNbItems > 0) {
1559       QStringList anElements = aListStr.split( " ", QString::SkipEmptyParts);
1560       for (QStringList::iterator it = anElements.begin(); it != anElements.end(); ++it) {
1561         QList<QListWidgetItem*> found = myElements->findItems(*it, Qt::MatchExactly);
1562         if (found.count() == 0) {
1563           anItem = new QListWidgetItem(*it);
1564           myElements->addItem(anItem);
1565           if (!anItem->isSelected())
1566             listItemsToSel.push_back(anItem);
1567         }
1568         else {
1569           foreach(anItem, found)
1570             if (!anItem->isSelected())
1571               listItemsToSel.push_back(anItem);
1572         }
1573       }
1574       bool blocked = myElements->signalsBlocked();
1575       myElements->blockSignals(true);
1576       foreach(anItem, listItemsToSel) anItem->setSelected(true);
1577       myElements->blockSignals(blocked);
1578       onListSelectionChanged();
1579       listItemsToSel.clear();
1580     }
1581   } else if (myCurrentLineEdit == mySubMeshLine) {
1582     //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1583
1584     SALOME_ListIO aList;
1585     mySelectionMgr->selectedObjects( aList );
1586
1587     SALOME_ListIteratorOfListIO anIt (aList);
1588     for ( ; anIt.More(); anIt.Next()) {
1589       SMESH::SMESH_subMesh_var aSubMesh =
1590         SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIt.Value());
1591       if (!aSubMesh->_is_nil()) {
1592         // check if mesh is the same
1593         if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
1594           try {
1595             SMESH::long_array_var anElements = aSubMesh->GetElementsByType(aType);
1596             int k = anElements->length();
1597             for (int i = 0; i < k; i++) {
1598               QString aText = QString::number(anElements[i]);
1599               QList<QListWidgetItem*> found = myElements->findItems(aText, Qt::MatchExactly);
1600               if (found.count() == 0) {
1601                 anItem = new QListWidgetItem(aText);
1602                 myElements->addItem(anItem);
1603                 if (!anItem->isSelected())
1604                   listItemsToSel.push_back(anItem);
1605               }
1606               else {
1607                 foreach(anItem, found)
1608                   if (!anItem->isSelected())
1609                     listItemsToSel.push_back(anItem);
1610               }
1611             }
1612             bool blocked = myElements->signalsBlocked();
1613             myElements->blockSignals(true);
1614             foreach(anItem, listItemsToSel) anItem->setSelected(true);
1615             myElements->blockSignals(blocked);
1616             onListSelectionChanged();
1617             listItemsToSel.clear();
1618           }
1619           catch (const SALOME::SALOME_Exception& ex) {
1620             SalomeApp_Tools::QtCatchCorbaException(ex);
1621           }
1622         }
1623       }
1624     }
1625     mySelectSubMesh->setChecked(false);
1626     myIsBusy = false;
1627     onListSelectionChanged();
1628
1629   } else if (myCurrentLineEdit == myGroupLine) {
1630     //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1631     SALOME_ListIO aList;
1632     mySelectionMgr->selectedObjects( aList );
1633
1634     SALOME_ListIteratorOfListIO anIt (aList);
1635     for ( ; anIt.More(); anIt.Next()) {
1636       SMESH::SMESH_GroupBase_var aGroup =
1637         SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(anIt.Value());
1638       if (!aGroup->_is_nil()) {
1639         // check if mesh is the same
1640         if (aGroup->GetType() == aType && aGroup->GetMesh()->GetId() == myMesh->GetId()) {
1641           SMESH::long_array_var anElements = aGroup->GetListOfID();
1642           int k = anElements->length();
1643           for (int i = 0; i < k; i++) {
1644             QString aText = QString::number(anElements[i]);
1645             QList<QListWidgetItem*> found = myElements->findItems(aText, Qt::MatchExactly);
1646             if (found.count() == 0) {
1647               anItem = new QListWidgetItem(aText);
1648               myElements->addItem(anItem);
1649               if (!anItem->isSelected())
1650                 listItemsToSel.push_back(anItem);
1651             }
1652             else {
1653               foreach(anItem, found)
1654                 if (!anItem->isSelected())
1655                   listItemsToSel.push_back(anItem);
1656             }
1657           }
1658           bool blocked = myElements->signalsBlocked();
1659           myElements->blockSignals(true);
1660           foreach(anItem, listItemsToSel) anItem->setSelected(true);
1661           myElements->blockSignals(blocked);
1662           onListSelectionChanged();
1663           listItemsToSel.clear();
1664         }
1665       }
1666     }
1667     mySelectGroup->setChecked(false);
1668     myIsBusy = false;
1669     onListSelectionChanged();
1670
1671   } else if (myCurrentLineEdit == myGeomGroupLine && myGeomObjects->length() == 1) {
1672     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1673     GEOM::GEOM_IGroupOperations_var aGroupOp =
1674       SMESH::GetGEOMGen()->GetIGroupOperations(aStudy->StudyId());
1675
1676     SMESH::ElementType aGroupType = SMESH::ALL;
1677     switch(aGroupOp->GetType(myGeomObjects[0])) {
1678     case 7: aGroupType = SMESH::NODE; break;
1679     case 6: aGroupType = SMESH::EDGE; break;
1680     case 4: aGroupType = SMESH::FACE; break;
1681     case 2: aGroupType = SMESH::VOLUME; break;
1682     default: myIsBusy = false; return;
1683     }
1684
1685     if (aGroupType == aType) {
1686       _PTR(SObject) aGroupSO =
1687         //aStudy->FindObjectIOR(aStudy->ConvertObjectToIOR(myGeomGroup));
1688         aStudy->FindObjectID(myGeomObjects[0]->GetStudyEntry());
1689       // Construct filter
1690       SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
1691       SMESH::Filter_var aFilter = aFilterMgr->CreateFilter();
1692       SMESH::BelongToGeom_var aBelongToGeom = aFilterMgr->CreateBelongToGeom();;
1693       aBelongToGeom->SetGeom(myGeomObjects[0]);
1694       aBelongToGeom->SetShapeName(aGroupSO->GetName().c_str());
1695       aBelongToGeom->SetElementType(aType);
1696       aFilter->SetPredicate(aBelongToGeom);
1697
1698       SMESH::long_array_var anElements = aFilter->GetElementsId(myMesh);
1699
1700       int k = anElements->length();
1701       for (int i = 0; i < k; i++) {
1702         QString aText = QString::number(anElements[i]);
1703         QList<QListWidgetItem*> found = myElements->findItems(aText, Qt::MatchExactly);
1704         if (found.count() == 0) {
1705           anItem = new QListWidgetItem(aText);
1706           myElements->addItem(anItem);
1707           if (!anItem->isSelected())
1708             listItemsToSel.push_back(anItem);
1709         }
1710         else {
1711           foreach(anItem, found)
1712             if (!anItem->isSelected())
1713               listItemsToSel.push_back(anItem);
1714         }
1715       }
1716       bool blocked = myElements->signalsBlocked();
1717       myElements->blockSignals(true);
1718       foreach(anItem, listItemsToSel) anItem->setSelected(true);
1719       myElements->blockSignals(blocked);
1720       onListSelectionChanged();
1721       listItemsToSel.clear();
1722     }
1723
1724     //VSR: mySelectGeomGroup->setChecked(false);
1725     myIsBusy = false;
1726     onListSelectionChanged();
1727   }
1728   myIsBusy = false;
1729   //  mySelectionMgr->clearSelected();
1730   updateButtons();
1731 }
1732
1733 //=================================================================================
1734 // function : onRemove()
1735 // purpose  :
1736 //=================================================================================
1737 void SMESHGUI_GroupDlg::onRemove()
1738 {
1739   myIsBusy = true;
1740   if (myCurrentLineEdit == 0) {
1741     QList<QListWidgetItem*> selItems = myElements->selectedItems();
1742     QListWidgetItem* item;
1743     foreach(item, selItems) delete item;
1744   } else {
1745     SALOME_ListIO aList;
1746     mySelectionMgr->selectedObjects( aList );
1747
1748     int aNbSel = aList.Extent();
1749
1750     if (aNbSel == 0) { myIsBusy = false; return; }
1751
1752     SMESH::ElementType aType = SMESH::ALL;
1753     switch(myTypeId) {
1754     case 0: aType = SMESH::NODE; break;
1755     case 1: aType = SMESH::EDGE; break;
1756     case 2: aType = SMESH::FACE; break;
1757     case 3: aType = SMESH::VOLUME; break;
1758     }
1759
1760     if (myCurrentLineEdit == mySubMeshLine) {
1761       //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1762       SALOME_ListIO aList;
1763       mySelectionMgr->selectedObjects( aList );
1764
1765       SALOME_ListIteratorOfListIO anIt (aList);
1766       for ( ; anIt.More(); anIt.Next()) {
1767         SMESH::SMESH_subMesh_var aSubMesh = SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIt.Value());
1768         if (!aSubMesh->_is_nil()) {
1769           // check if mesh is the same
1770           if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
1771             if (aType == SMESH::NODE) {
1772               try {
1773                 SMESH::long_array_var anElements = aSubMesh->GetNodesId();
1774                 int k = anElements->length();
1775                 for (int i = 0; i < k; i++) {
1776                   QList<QListWidgetItem*> found = 
1777                     myElements->findItems(QString::number(anElements[i]), Qt::MatchExactly);
1778                   QListWidgetItem* anItem;
1779                   foreach(anItem, found) delete anItem;
1780                 }
1781               }
1782               catch (const SALOME::SALOME_Exception& ex) {
1783                 SalomeApp_Tools::QtCatchCorbaException(ex);
1784               }
1785             }
1786             else {
1787               try {
1788                 SMESH::long_array_var anElements = aSubMesh->GetElementsId();
1789                 int k = anElements->length();
1790                 for (int i = 0; i < k; i++) {
1791                   QList<QListWidgetItem*> found = 
1792                     myElements->findItems(QString::number(anElements[i]), Qt::MatchExactly);
1793                   QListWidgetItem* anItem;
1794                   foreach(anItem, found) delete anItem;
1795                 }
1796               }
1797               catch (const SALOME::SALOME_Exception& ex) {
1798                 SalomeApp_Tools::QtCatchCorbaException(ex);
1799               }
1800             }
1801           }
1802         }
1803       }
1804     }
1805     else if (myCurrentLineEdit == myGroupLine) {
1806       Standard_Boolean aRes;
1807       //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1808       SALOME_ListIO aList;
1809       mySelectionMgr->selectedObjects( aList );
1810
1811       SALOME_ListIteratorOfListIO anIt (aList);
1812       for ( ; anIt.More(); anIt.Next()) {
1813         SMESH::SMESH_Group_var aGroup = SMESH::IObjectToInterface<SMESH::SMESH_Group>(anIt.Value());
1814         if (aRes && !aGroup->_is_nil()) {
1815           // check if mesh is the same
1816           if (aGroup->GetType() == aType && aGroup->GetMesh()->GetId() == myMesh->GetId()) {
1817             SMESH::long_array_var anElements = aGroup->GetListOfID();
1818             int k = anElements->length();
1819             for (int i = 0; i < k; i++) {
1820               QList<QListWidgetItem*> found = 
1821                 myElements->findItems(QString::number(anElements[i]), Qt::MatchExactly);
1822               QListWidgetItem* anItem;
1823               foreach(anItem, found) delete anItem;
1824             }
1825           }
1826         }
1827       }
1828     }
1829   }
1830   myIsBusy = false;
1831   updateButtons();
1832 }
1833
1834 //=================================================================================
1835 // function : onSort()
1836 // purpose  :
1837 //=================================================================================
1838 void SMESHGUI_GroupDlg::onSort()
1839 {
1840   // PAL5412: sorts items in ascending by "string" value
1841   // myElements->sort(true);
1842   // myElements->update();
1843   int i, k = myElements->count();
1844   if (k > 0) {
1845     myIsBusy = true;
1846     QList<int> aSelected;
1847     std::vector<int> anArray(k);
1848     //    QMemArray<int> anArray(k);
1849     // fill the array
1850     for (i = 0; i < k; i++) {
1851       int id = myElements->item(i)->text().toInt();
1852       anArray[i] = id;
1853       if (myElements->item(i)->isSelected())
1854         aSelected.append(id);
1855     }
1856     // sort & update list
1857     std::sort(anArray.begin(), anArray.end());
1858     //    anArray.sort();
1859     myElements->clear();
1860     QListWidgetItem* anItem;
1861     QList<QListWidgetItem*> listItemsToSel;
1862     for (i = 0; i < k; i++) {
1863       anItem = new QListWidgetItem(QString::number(anArray[i]));
1864       myElements->addItem(anItem);
1865       if (aSelected.contains(anArray[i]))
1866         listItemsToSel.push_back(anItem);
1867     }
1868     bool blocked = myElements->signalsBlocked();
1869     myElements->blockSignals(true);
1870     foreach(anItem, listItemsToSel) anItem->setSelected(true);
1871     myElements->blockSignals(blocked);
1872     listItemsToSel.clear();
1873     myIsBusy = false;
1874   }
1875 }
1876
1877 //=================================================================================
1878 // function : closeEvent()
1879 // purpose  :
1880 //=================================================================================
1881 void SMESHGUI_GroupDlg::closeEvent (QCloseEvent*)
1882 {
1883   onClose();
1884 }
1885
1886 //=================================================================================
1887 // function : onVisibilityChanged()
1888 // purpose  :
1889 //=================================================================================
1890 void SMESHGUI_GroupDlg::onVisibilityChanged()
1891 {
1892   SetAppropriateActor();
1893 }
1894
1895 //=================================================================================
1896 // function : SMESHGUI_GroupDlg::onClose
1897 // purpose  : SLOT called when "Close" button pressed. Close dialog
1898 //=================================================================================
1899 void SMESHGUI_GroupDlg::onClose()
1900 {
1901   if (SMESH::GetCurrentVtkView()) {
1902     SMESH::RemoveFilters(); // PAL6938 -- clean all mesh entity filters
1903     SMESH::SetPointRepresentation(false);
1904     SMESH::SetPickable();
1905     restoreShowEntityMode();
1906   }
1907
1908   mySelectionMgr->clearSelected();
1909   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
1910     aViewWindow->SetSelectionMode(ActorSelection);
1911   mySelectionMgr->clearFilters();
1912   mySMESHGUI->ResetState();
1913
1914   reject();
1915 }
1916
1917 //=================================================================================
1918 // function : onHelp()
1919 // purpose  :
1920 //=================================================================================
1921 void SMESHGUI_GroupDlg::onHelp()
1922 {
1923   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
1924   if (app)
1925     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString( "" ), myHelpFileName);
1926   else {
1927     QString platform;
1928 #ifdef WIN32
1929     platform = "winapplication";
1930 #else
1931     platform = "application";
1932 #endif
1933     SUIT_MessageBox::warning(this, tr( "WRN_WARNING" ),
1934                              tr( "EXTERNAL_BROWSER_CANNOT_SHOW_PAGE" ).
1935                              arg(app->resourceMgr()->stringValue( "ExternalBrowser",
1936                                                                  platform)).
1937                              arg(myHelpFileName));
1938   }
1939 }
1940
1941 //=================================================================================
1942 // function : SMESHGUI_GroupDlg::onDeactivate
1943 // purpose  : SLOT called when dialog must be deativated
1944 //=================================================================================
1945 void SMESHGUI_GroupDlg::onDeactivate()
1946 {
1947   mySMESHGUI->ResetState();
1948   setEnabled(false);
1949 }
1950
1951 //=================================================================================
1952 // function : SMESHGUI_GroupDlg::enterEvent
1953 // purpose  : Event filter
1954 //=================================================================================
1955 void SMESHGUI_GroupDlg::enterEvent (QEvent*)
1956 {
1957   if (!isEnabled()) {
1958     mySMESHGUI->EmitSignalDeactivateDialog();
1959     setEnabled(true);
1960     mySelectionMode = -1;
1961     setSelectionMode(myTypeId);
1962     //mySMESHGUI->SetActiveDialogBox((QDialog*)this);
1963     mySMESHGUI->SetActiveDialogBox(this);
1964     mySMESHGUI->SetState(800);
1965   }
1966 }
1967
1968 //=================================================================================
1969 // function : hideEvent
1970 // purpose  : caused by ESC key
1971 //=================================================================================
1972 void SMESHGUI_GroupDlg::hideEvent (QHideEvent*)
1973 {
1974   if (!isMinimized() && !myIsBusy)
1975     onClose();
1976 }
1977
1978 //=================================================================================
1979 // function : keyPressEvent()
1980 // purpose  :
1981 //=================================================================================
1982 void SMESHGUI_GroupDlg::keyPressEvent( QKeyEvent* e )
1983 {
1984   QDialog::keyPressEvent( e );
1985   if ( e->isAccepted() )
1986     return;
1987
1988   if ( e->key() == Qt::Key_F1 )
1989     {
1990       e->accept();
1991       onHelp();
1992     }
1993 }
1994
1995 //================================================================================
1996 /*!
1997  * \brief Enable showing of the popup when Geometry selection btn is clicked
1998   * \param enable - true to enable
1999  */
2000 //================================================================================
2001
2002 enum { DIRECT_GEOM_INDEX = 0, GEOM_BY_MESH_INDEX };
2003
2004 void SMESHGUI_GroupDlg::updateGeomPopup()
2005 {
2006   bool enable = false;
2007
2008   if ( !myMesh->_is_nil() )
2009     enable = myMesh->NbEdges() > 0;
2010
2011   if ( myGeomGroupBtn )
2012   {
2013     disconnect( myGeomGroupBtn, SIGNAL( toggled(bool) ), this, SLOT( onGeomSelectionButton(bool) ));
2014     if ( enable ) {
2015       if ( !myGeomPopup ) {
2016         myGeomPopup = new QMenu(this);
2017         myActions[myGeomPopup->addAction( tr( "DIRECT_GEOM_SELECTION" ) )] = DIRECT_GEOM_INDEX;
2018         myActions[myGeomPopup->addAction( tr( "GEOM_BY_MESH_ELEM_SELECTION" ) )] = GEOM_BY_MESH_INDEX;
2019         connect( myGeomPopup, SIGNAL( triggered( QAction* ) ), SLOT( onGeomPopup( QAction* ) ) );
2020       }
2021       connect( myGeomGroupBtn, SIGNAL( toggled(bool) ), this, SLOT( onGeomSelectionButton(bool) ));
2022     }
2023   }
2024 }
2025
2026
2027 //=================================================================================
2028 // function : onGeomSelectionButton()
2029 // purpose  :
2030 //=================================================================================
2031 void SMESHGUI_GroupDlg::onGeomSelectionButton(bool isBtnOn)
2032 {
2033   if ( myGeomPopup && isBtnOn )
2034     {
2035       myCurrentLineEdit = myGeomGroupLine;
2036       QAction* a = myGeomPopup->exec( QCursor::pos() );
2037       if (!a || myActions[a] == DIRECT_GEOM_INDEX)
2038         setSelectionMode(7);
2039     }
2040   else if (!isBtnOn)
2041     {
2042       myCurrentLineEdit = 0;
2043       setSelectionMode(8);
2044     }
2045 }
2046
2047 //=================================================================================
2048 // function : onGeomPopup()
2049 // purpose  :
2050 //=================================================================================
2051 void SMESHGUI_GroupDlg::onGeomPopup( QAction* a )
2052 {
2053   int index = myActions[a];
2054   if ( index == GEOM_BY_MESH_INDEX )
2055     {
2056       mySelectionMode = -1;
2057       if ( !myShapeByMeshOp ) {
2058         myShapeByMeshOp = new SMESHGUI_ShapeByMeshOp(true);
2059         connect(myShapeByMeshOp, SIGNAL(committed(SUIT_Operation*)),
2060                 SLOT(onPublishShapeByMeshDlg(SUIT_Operation*)));
2061         connect(myShapeByMeshOp, SIGNAL(aborted(SUIT_Operation*)),
2062                 SLOT(onCloseShapeByMeshDlg(SUIT_Operation*)));
2063       }
2064       // set mesh object to SMESHGUI_ShapeByMeshOp and start it
2065       if ( !myMesh->_is_nil() ) {
2066         myIsBusy = true;
2067         hide(); // stop processing selection
2068         myIsBusy = false;
2069         myShapeByMeshOp->setModule( mySMESHGUI );
2070         myShapeByMeshOp->setStudy( 0 ); // it's really necessary
2071         myShapeByMeshOp->SetMesh( myMesh );
2072         myShapeByMeshOp->start();
2073       }
2074     }
2075 }
2076
2077 //================================================================================
2078 /*!
2079  * \brief SLOT. Is called when Ok is pressed in SMESHGUI_ShapeByMeshDlg
2080  */
2081 //================================================================================
2082
2083 void SMESHGUI_GroupDlg::onPublishShapeByMeshDlg(SUIT_Operation* op)
2084 {
2085   if ( myShapeByMeshOp == op ) {
2086     mySMESHGUI->getApp()->updateObjectBrowser();
2087     show();
2088     // Select a found geometry object
2089     GEOM::GEOM_Object_var aGeomVar = myShapeByMeshOp->GetShape();
2090     if ( !aGeomVar->_is_nil() )
2091     {
2092       QString ID = aGeomVar->GetStudyEntry();
2093       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
2094       if ( _PTR(SObject) aGeomSO = aStudy->FindObjectID( ID.toLatin1().data() )) {
2095         SALOME_ListIO anIOList;
2096         Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject
2097           ( aGeomSO->GetID().c_str(), "SMESH", aGeomSO->GetName().c_str() );
2098         anIOList.Append( anIO );
2099         mySelectionMgr->setSelectedObjects( anIOList, false );
2100         onObjectSelectionChanged();
2101       }
2102     }
2103   }
2104 }
2105
2106 //================================================================================
2107 /*!
2108  * \brief SLOT. Is called when Close is pressed in SMESHGUI_ShapeByMeshDlg
2109  */
2110 //================================================================================
2111
2112 void SMESHGUI_GroupDlg::onCloseShapeByMeshDlg(SUIT_Operation* op)
2113 {
2114   if ( myShapeByMeshOp == op )
2115     {
2116       show();
2117       setSelectionMode(7);
2118     }
2119 }
2120
2121 //=================================================================================
2122 // function : setGroupColor()
2123 // purpose  :
2124 //=================================================================================
2125 void SMESHGUI_GroupDlg::setGroupColor( const SALOMEDS::Color& theColor )
2126 {
2127   QColor aQColor( (int)( theColor.R * 255.0 ),
2128                   (int)( theColor.G * 255.0 ),
2129                   (int)( theColor.B * 255.0 ) );
2130   setGroupQColor( aQColor );
2131 }
2132
2133 //=================================================================================
2134 // function : getGroupColor()
2135 // purpose  :
2136 //=================================================================================
2137 SALOMEDS::Color SMESHGUI_GroupDlg::getGroupColor() const
2138 {
2139   QColor aQColor = getGroupQColor();
2140
2141   SALOMEDS::Color aColor;
2142   aColor.R = (float)aQColor.red() / 255.0;
2143   aColor.G = (float)aQColor.green() / 255.0;
2144   aColor.B = (float)aQColor.blue() / 255.0;
2145
2146   return aColor;
2147 }
2148
2149 //=================================================================================
2150 // function : setGroupQColor()
2151 // purpose  :
2152 //=================================================================================
2153 void SMESHGUI_GroupDlg::setGroupQColor( const QColor& theColor )
2154 {
2155   if( theColor.isValid() )
2156     myColorBtn->setColor( theColor );
2157 }
2158
2159 //=================================================================================
2160 // function : getGroupQColor()
2161 // purpose  :
2162 //=================================================================================
2163 QColor SMESHGUI_GroupDlg::getGroupQColor() const
2164 {
2165   return myColorBtn->color();
2166 }
2167
2168 //=================================================================================
2169 // function : setDefaultGroupColor()
2170 // purpose  :
2171 //=================================================================================
2172 void SMESHGUI_GroupDlg::setDefaultGroupColor()
2173 {
2174   if( myMesh->_is_nil() )
2175     return;
2176
2177   bool isAutoColor = myMesh->GetAutoColor();
2178
2179   QColor aQColor;
2180   if( !isAutoColor )
2181   {
2182     int r = 0, g = 0, b = 0;
2183     SMESH::GetColor( "SMESH", "fill_color", r, g, b, QColor( 0, 170, 255 ) );
2184     aQColor.setRgb( r, g, b );
2185   }
2186   else
2187   {
2188     SMESH::ListOfGroups aListOfGroups = *myMesh->GetGroups();
2189
2190     QList<SALOMEDS::Color> aReservedColors;
2191     for( int i = 0, n = aListOfGroups.length(); i < n; i++ )
2192     {
2193       SMESH::SMESH_GroupBase_var aGroupObject = aListOfGroups[i];
2194       SALOMEDS::Color aReservedColor = aGroupObject->GetColor();
2195       aReservedColors.append( aReservedColor );
2196     }
2197
2198     SALOMEDS::Color aColor = SMESHGUI::getUniqueColor( aReservedColors );
2199     aQColor.setRgb( (int)( aColor.R * 255.0 ),
2200                     (int)( aColor.G * 255.0 ),
2201                     (int)( aColor.B * 255.0 ) );
2202
2203   }
2204
2205   setGroupQColor( aQColor );
2206 }
2207
2208 //=================================================================================
2209 // function : SetAppropriateActor()
2210 // purpose  : Find more appropriate of visible actors, set it to myActor, allow picking
2211 //            NPAL19389: create a group with a selection in another group.
2212 //            if mesh actor is not visible - find any first visible group or submesh
2213 //=================================================================================
2214 bool SMESHGUI_GroupDlg::SetAppropriateActor()
2215 {
2216   bool isActor = false;
2217   myActorsList.clear();
2218
2219   if (myMesh->_is_nil()) return false;
2220
2221   SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView();
2222
2223   if (myGeomGroupBtn->isChecked()) {   // try current group on geometry actor
2224     if (!isActor) {
2225       if (!myGroupOnGeom->_is_nil()) {
2226         SMESH_Actor* anActor = SMESH::FindActorByObject(myGroupOnGeom);
2227         if (anActor && anActor->hasIO())
2228           {
2229             isActor = true;
2230             if (aViewWindow && !aViewWindow->isVisible(anActor->getIO()))
2231               isActor = false;
2232             else
2233               myActorsList.append(anActor);
2234           }
2235       }
2236     }
2237   } else {
2238     // try mesh actor
2239     SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh);
2240     if (anActor && anActor->hasIO()) {
2241       isActor = true;
2242       if (aViewWindow && !aViewWindow->isVisible(anActor->getIO()))
2243         isActor = false;
2244       else
2245         myActorsList.append(anActor);
2246     }
2247     
2248     // try group actor
2249     if (!isActor && !myGroup->_is_nil()) {
2250       SMESH_Actor* anActor = SMESH::FindActorByObject(myGroup);
2251       if (anActor && anActor->hasIO())
2252         myActorsList.append(anActor);
2253     }
2254     
2255     // try any visible actor of group or submesh of current mesh
2256     if (aViewWindow) {
2257       // mesh entry
2258       _PTR(SObject) aSObject = SMESH::FindSObject(myMesh);
2259       if (aSObject) {
2260         CORBA::String_var meshEntry = aSObject->GetID().c_str();
2261         int len = strlen(meshEntry);
2262         
2263         // iterate on all actors in current view window, search for
2264         // any visible actor, that belongs to group or submesh of current mesh
2265         VTK::ActorCollectionCopy aCopy(aViewWindow->getRenderer()->GetActors());
2266         vtkActorCollection *aCollection = aCopy.GetActors();
2267         int nbItems = aCollection->GetNumberOfItems();
2268         for (int i=0; i<nbItems && !isActor; i++)
2269           {
2270             SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(aCollection->GetItemAsObject(i));
2271             if (anActor && anActor->hasIO()) {
2272               Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
2273               if (aViewWindow->isVisible(anIO)) {
2274                 if (anIO->hasEntry() && strncmp(anIO->getEntry(), meshEntry, len) == 0 && !myActorsList.contains(anActor) )
2275                   myActorsList.append(anActor);
2276               }
2277             }
2278           }
2279       }
2280     }
2281   }
2282   
2283   if (myActorsList.count() > 0) {
2284     QListIterator<SMESH_Actor*> it( myActorsList );
2285     while ( it.hasNext() ) {
2286       SMESH_Actor* anActor = it.next();
2287       if ( IsActorVisible(anActor) )
2288         anActor->SetPickable(true);
2289     }
2290   }
2291   
2292   return ( isActor || (myActorsList.count() > 0) );
2293 }
2294   
2295 //=======================================================================
2296 //function : setShowEntityMode
2297 //purpose  : make shown only entity corresponding to my type
2298 //=======================================================================
2299 void SMESHGUI_GroupDlg::setShowEntityMode()
2300 {
2301   if ( !myMesh->_is_nil() ) {
2302     if ( SMESH_Actor* actor = SMESH::FindActorByObject(myMesh) ) {
2303       if (!myStoredShownEntity)
2304         myStoredShownEntity = actor->GetEntityMode();
2305       switch ( myTypeId ) {
2306       case 0: restoreShowEntityMode(); break;
2307       case 1: actor->SetEntityMode( SMESH_Actor::eEdges ); break;
2308       case 2: actor->SetEntityMode( SMESH_Actor::eFaces ); break;
2309       case 3: actor->SetEntityMode( SMESH_Actor::eVolumes ); break;
2310       }
2311     }
2312   }
2313 }
2314
2315 //=======================================================================
2316 //function : restoreShowEntityMode
2317 //purpose  : restore ShowEntity mode of myActor
2318 //=======================================================================
2319 void SMESHGUI_GroupDlg::restoreShowEntityMode()
2320 {
2321   if ( myStoredShownEntity && !myMesh->_is_nil() ) {
2322     if ( SMESH_Actor* actor = SMESH::FindActorByObject(myMesh) ) {
2323       actor->SetEntityMode(myStoredShownEntity);
2324     }
2325   }
2326   myStoredShownEntity = 0;
2327 }
2328
2329 //=======================================================================
2330 //function : IsActorVisible
2331 //purpose  : return visibility of the actor
2332 //=======================================================================
2333 bool SMESHGUI_GroupDlg::IsActorVisible( SMESH_Actor* theActor )
2334 {
2335   SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView();
2336   if (theActor && aViewWindow)
2337     return aViewWindow->isVisible(theActor->getIO());
2338   return false;
2339 }