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