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