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