Salome HOME
63b5d395674b05664ab5df3866b6f63bb63f8dd2
[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   if(myMesh->_is_nil()) {
1399     SUIT_MessageBox::critical(this,
1400                               tr("SMESH_ERROR"),
1401                               tr("NO_MESH_SELECTED"));
1402    return;
1403   }
1404
1405   SMESH::ElementType aType = SMESH::ALL;
1406   switch ( myTypeId )
1407   {
1408     case 0 : aType = SMESH::NODE; break;
1409     case 1 : aType = SMESH::EDGE; break;
1410     case 2 : aType = SMESH::FACE; break;
1411     case 3 : aType = SMESH::VOLUME; break;
1412     default: return;
1413   }
1414
1415   if ( myFilterDlg == 0 )
1416   {
1417     myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, aType );
1418     connect( myFilterDlg, SIGNAL( Accepted() ), SLOT( onFilterAccepted() ) );
1419   }
1420   else
1421     myFilterDlg->Init( aType );
1422
1423   myFilterDlg->SetSelection();
1424   myFilterDlg->SetMesh( myMesh );
1425   myFilterDlg->SetSourceWg( myElements, false );
1426
1427   myFilterDlg->show();
1428 }
1429
1430 //=================================================================================
1431 // function : onFilterAccepted()
1432 // purpose  : SLOT. Called when Filter dlg closed with OK button.
1433 //            Uncheck "Select submesh" and "Select group" checkboxes
1434 //=================================================================================
1435 void SMESHGUI_GroupDlg::onFilterAccepted()
1436 {
1437   if ( mySelectSubMesh->isChecked() || mySelectGroup->isChecked() )
1438   {
1439     mySelectionMode = myTypeId;
1440     mySelectSubMesh->setChecked( false );
1441     mySelectGroup->setChecked( false );
1442   }
1443 }
1444
1445 //=================================================================================
1446 // function : onAdd()
1447 // purpose  :
1448 //=================================================================================
1449 void SMESHGUI_GroupDlg::onAdd()
1450 {
1451   SALOME_ListIO aList;
1452   mySelectionMgr->selectedObjects( aList );
1453
1454   int aNbSel = aList.Extent();
1455
1456   if (aNbSel == 0 || !myActor || myMesh->_is_nil()) return;
1457
1458   myIsBusy = true;
1459
1460   SMESH::ElementType aType = SMESH::ALL;
1461   switch(myTypeId) {
1462   case 0:
1463     aType = SMESH::NODE;
1464     mySelector->SetSelectionMode(NodeSelection);
1465     break;
1466   case 1:
1467     aType = SMESH::EDGE;
1468     mySelector->SetSelectionMode(EdgeSelection);
1469     break;
1470   case 2:
1471     aType = SMESH::FACE;
1472     mySelector->SetSelectionMode(FaceSelection);
1473     break;
1474   case 3:
1475     aType = SMESH::VOLUME;
1476     mySelector->SetSelectionMode(VolumeSelection);
1477     break;
1478   default:
1479     mySelector->SetSelectionMode(ActorSelection);
1480   }
1481
1482   QListWidgetItem* anItem = 0;
1483   QList<QListWidgetItem*> listItemsToSel;
1484
1485   if (myCurrentLineEdit == 0) {
1486     //if (aNbSel != 1) { myIsBusy = false; return; }
1487     QString aListStr = "";
1488     int aNbItems = 0;
1489     if (myTypeId == 0) {
1490       aNbItems = SMESH::GetNameOfSelectedNodes(mySelector, myActor->getIO(), aListStr);
1491     }
1492     else {
1493       aNbItems = SMESH::GetNameOfSelectedElements(mySelector, myActor->getIO(), aListStr);
1494     }
1495     if (aNbItems > 0) {
1496       QStringList anElements = aListStr.split( " ", QString::SkipEmptyParts);
1497       for (QStringList::iterator it = anElements.begin(); it != anElements.end(); ++it) {
1498         QList<QListWidgetItem*> found = myElements->findItems(*it, Qt::MatchExactly);
1499         if (found.count() == 0) {
1500           anItem = new QListWidgetItem(*it);
1501           myElements->addItem(anItem);
1502           if (!anItem->isSelected())
1503             listItemsToSel.push_back(anItem);
1504         }
1505         else {
1506           foreach(anItem, found)
1507             if (!anItem->isSelected())
1508               listItemsToSel.push_back(anItem);
1509         }
1510       }
1511       bool blocked = myElements->signalsBlocked();
1512       myElements->blockSignals(true);
1513       foreach(anItem, listItemsToSel) anItem->setSelected(true);
1514       myElements->blockSignals(blocked);
1515       onListSelectionChanged();
1516       listItemsToSel.clear();
1517     }
1518   } else if (myCurrentLineEdit == mySubMeshLine) {
1519     //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1520
1521     SALOME_ListIO aList;
1522     mySelectionMgr->selectedObjects( aList );
1523
1524     SALOME_ListIteratorOfListIO anIt (aList);
1525     for ( ; anIt.More(); anIt.Next()) {
1526       SMESH::SMESH_subMesh_var aSubMesh =
1527         SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIt.Value());
1528       if (!aSubMesh->_is_nil()) {
1529         // check if mesh is the same
1530         if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
1531           try {
1532             SMESH::long_array_var anElements = aSubMesh->GetElementsByType(aType);
1533             int k = anElements->length();
1534             for (int i = 0; i < k; i++) {
1535               QString aText = QString::number(anElements[i]);
1536               QList<QListWidgetItem*> found = myElements->findItems(aText, Qt::MatchExactly);
1537               if (found.count() == 0) {
1538                 anItem = new QListWidgetItem(aText);
1539                 myElements->addItem(anItem);
1540                 if (!anItem->isSelected())
1541                   listItemsToSel.push_back(anItem);
1542               }
1543               else {
1544                 foreach(anItem, found)
1545                   if (!anItem->isSelected())
1546                     listItemsToSel.push_back(anItem);
1547               }
1548             }
1549             bool blocked = myElements->signalsBlocked();
1550             myElements->blockSignals(true);
1551             foreach(anItem, listItemsToSel) anItem->setSelected(true);
1552             myElements->blockSignals(blocked);
1553             onListSelectionChanged();
1554             listItemsToSel.clear();
1555           }
1556           catch (const SALOME::SALOME_Exception& ex) {
1557             SalomeApp_Tools::QtCatchCorbaException(ex);
1558           }
1559         }
1560       }
1561     }
1562     mySelectSubMesh->setChecked(false);
1563     myIsBusy = false;
1564     onListSelectionChanged();
1565
1566   } else if (myCurrentLineEdit == myGroupLine) {
1567     //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1568     SALOME_ListIO aList;
1569     mySelectionMgr->selectedObjects( aList );
1570
1571     SALOME_ListIteratorOfListIO anIt (aList);
1572     for ( ; anIt.More(); anIt.Next()) {
1573       SMESH::SMESH_GroupBase_var aGroup =
1574         SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(anIt.Value());
1575       if (!aGroup->_is_nil()) {
1576         // check if mesh is the same
1577         if (aGroup->GetType() == aType && aGroup->GetMesh()->GetId() == myMesh->GetId()) {
1578           SMESH::long_array_var anElements = aGroup->GetListOfID();
1579           int k = anElements->length();
1580           for (int i = 0; i < k; i++) {
1581             QString aText = QString::number(anElements[i]);
1582             QList<QListWidgetItem*> found = myElements->findItems(aText, Qt::MatchExactly);
1583             if (found.count() == 0) {
1584               anItem = new QListWidgetItem(aText);
1585               myElements->addItem(anItem);
1586               if (!anItem->isSelected())
1587                 listItemsToSel.push_back(anItem);
1588             }
1589             else {
1590               foreach(anItem, found)
1591                 if (!anItem->isSelected())
1592                   listItemsToSel.push_back(anItem);
1593             }
1594           }
1595           bool blocked = myElements->signalsBlocked();
1596           myElements->blockSignals(true);
1597           foreach(anItem, listItemsToSel) anItem->setSelected(true);
1598           myElements->blockSignals(blocked);
1599           onListSelectionChanged();
1600           listItemsToSel.clear();
1601         }
1602       }
1603     }
1604     mySelectGroup->setChecked(false);
1605     myIsBusy = false;
1606     onListSelectionChanged();
1607
1608   } else if (myCurrentLineEdit == myGeomGroupLine && myGeomObjects->length() == 1) {
1609     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1610     GEOM::GEOM_IGroupOperations_var aGroupOp =
1611       SMESH::GetGEOMGen()->GetIGroupOperations(aStudy->StudyId());
1612
1613     SMESH::ElementType aGroupType = SMESH::ALL;
1614     switch(aGroupOp->GetType(myGeomObjects[0])) {
1615     case 7: aGroupType = SMESH::NODE; break;
1616     case 6: aGroupType = SMESH::EDGE; break;
1617     case 4: aGroupType = SMESH::FACE; break;
1618     case 2: aGroupType = SMESH::VOLUME; break;
1619     default: myIsBusy = false; return;
1620     }
1621
1622     if (aGroupType == aType) {
1623       _PTR(SObject) aGroupSO =
1624         //aStudy->FindObjectIOR(aStudy->ConvertObjectToIOR(myGeomGroup));
1625         aStudy->FindObjectID(myGeomObjects[0]->GetStudyEntry());
1626       // Construct filter
1627       SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
1628       SMESH::Filter_var aFilter = aFilterMgr->CreateFilter();
1629       SMESH::BelongToGeom_var aBelongToGeom = aFilterMgr->CreateBelongToGeom();;
1630       aBelongToGeom->SetGeom(myGeomObjects[0]);
1631       aBelongToGeom->SetShapeName(aGroupSO->GetName().c_str());
1632       aBelongToGeom->SetElementType(aType);
1633       aFilter->SetPredicate(aBelongToGeom);
1634
1635       SMESH::long_array_var anElements = aFilter->GetElementsId(myMesh);
1636
1637       int k = anElements->length();
1638       for (int i = 0; i < k; i++) {
1639         QString aText = QString::number(anElements[i]);
1640         QList<QListWidgetItem*> found = myElements->findItems(aText, Qt::MatchExactly);
1641         if (found.count() == 0) {
1642           anItem = new QListWidgetItem(aText);
1643           myElements->addItem(anItem);
1644           if (!anItem->isSelected())
1645             listItemsToSel.push_back(anItem);
1646         }
1647         else {
1648           foreach(anItem, found)
1649             if (!anItem->isSelected())
1650               listItemsToSel.push_back(anItem);
1651         }
1652       }
1653       bool blocked = myElements->signalsBlocked();
1654       myElements->blockSignals(true);
1655       foreach(anItem, listItemsToSel) anItem->setSelected(true);
1656       myElements->blockSignals(blocked);
1657       onListSelectionChanged();
1658       listItemsToSel.clear();
1659     }
1660
1661     //VSR: mySelectGeomGroup->setChecked(false);
1662     myIsBusy = false;
1663     onListSelectionChanged();
1664   }
1665   myIsBusy = false;
1666   //  mySelectionMgr->clearSelected();
1667   updateButtons();
1668 }
1669
1670 //=================================================================================
1671 // function : onRemove()
1672 // purpose  :
1673 //=================================================================================
1674 void SMESHGUI_GroupDlg::onRemove()
1675 {
1676   myIsBusy = true;
1677   if (myCurrentLineEdit == 0) {
1678     QList<QListWidgetItem*> selItems = myElements->selectedItems();
1679     QListWidgetItem* item;
1680     foreach(item, selItems) delete item;
1681   } else {
1682     SALOME_ListIO aList;
1683     mySelectionMgr->selectedObjects( aList );
1684
1685     int aNbSel = aList.Extent();
1686
1687     if (aNbSel == 0) { myIsBusy = false; return; }
1688
1689     SMESH::ElementType aType = SMESH::ALL;
1690     switch(myTypeId) {
1691     case 0: aType = SMESH::NODE; break;
1692     case 1: aType = SMESH::EDGE; break;
1693     case 2: aType = SMESH::FACE; break;
1694     case 3: aType = SMESH::VOLUME; break;
1695     }
1696
1697     if (myCurrentLineEdit == mySubMeshLine) {
1698       //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1699       SALOME_ListIO aList;
1700       mySelectionMgr->selectedObjects( aList );
1701
1702       SALOME_ListIteratorOfListIO anIt (aList);
1703       for ( ; anIt.More(); anIt.Next()) {
1704         SMESH::SMESH_subMesh_var aSubMesh = SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIt.Value());
1705         if (!aSubMesh->_is_nil()) {
1706           // check if mesh is the same
1707           if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
1708             if (aType == SMESH::NODE) {
1709               try {
1710                 SMESH::long_array_var anElements = aSubMesh->GetNodesId();
1711                 int k = anElements->length();
1712                 for (int i = 0; i < k; i++) {
1713                   QList<QListWidgetItem*> found = 
1714                     myElements->findItems(QString::number(anElements[i]), Qt::MatchExactly);
1715                   QListWidgetItem* anItem;
1716                   foreach(anItem, found) delete anItem;
1717                 }
1718               }
1719               catch (const SALOME::SALOME_Exception& ex) {
1720                 SalomeApp_Tools::QtCatchCorbaException(ex);
1721               }
1722             }
1723             else {
1724               try {
1725                 SMESH::long_array_var anElements = aSubMesh->GetElementsId();
1726                 int k = anElements->length();
1727                 for (int i = 0; i < k; i++) {
1728                   QList<QListWidgetItem*> found = 
1729                     myElements->findItems(QString::number(anElements[i]), Qt::MatchExactly);
1730                   QListWidgetItem* anItem;
1731                   foreach(anItem, found) delete anItem;
1732                 }
1733               }
1734               catch (const SALOME::SALOME_Exception& ex) {
1735                 SalomeApp_Tools::QtCatchCorbaException(ex);
1736               }
1737             }
1738           }
1739         }
1740       }
1741     }
1742     else if (myCurrentLineEdit == myGroupLine) {
1743       Standard_Boolean aRes;
1744       //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1745       SALOME_ListIO aList;
1746       mySelectionMgr->selectedObjects( aList );
1747
1748       SALOME_ListIteratorOfListIO anIt (aList);
1749       for ( ; anIt.More(); anIt.Next()) {
1750         SMESH::SMESH_Group_var aGroup = SMESH::IObjectToInterface<SMESH::SMESH_Group>(anIt.Value());
1751         if (aRes && !aGroup->_is_nil()) {
1752           // check if mesh is the same
1753           if (aGroup->GetType() == aType && aGroup->GetMesh()->GetId() == myMesh->GetId()) {
1754             SMESH::long_array_var anElements = aGroup->GetListOfID();
1755             int k = anElements->length();
1756             for (int i = 0; i < k; i++) {
1757               QList<QListWidgetItem*> found = 
1758                 myElements->findItems(QString::number(anElements[i]), Qt::MatchExactly);
1759               QListWidgetItem* anItem;
1760               foreach(anItem, found) delete anItem;
1761             }
1762           }
1763         }
1764       }
1765     }
1766   }
1767   myIsBusy = false;
1768   updateButtons();
1769 }
1770
1771 //=================================================================================
1772 // function : onSort()
1773 // purpose  :
1774 //=================================================================================
1775 void SMESHGUI_GroupDlg::onSort()
1776 {
1777   // PAL5412: sorts items in ascending by "string" value
1778   // myElements->sort(true);
1779   // myElements->update();
1780   int i, k = myElements->count();
1781   if (k > 0) {
1782     myIsBusy = true;
1783     QList<int> aSelected;
1784     std::vector<int> anArray(k);
1785     //    QMemArray<int> anArray(k);
1786     // fill the array
1787     for (i = 0; i < k; i++) {
1788       int id = myElements->item(i)->text().toInt();
1789       anArray[i] = id;
1790       if (myElements->item(i)->isSelected())
1791         aSelected.append(id);
1792     }
1793     // sort & update list
1794     std::sort(anArray.begin(), anArray.end());
1795     //    anArray.sort();
1796     myElements->clear();
1797     QListWidgetItem* anItem;
1798     QList<QListWidgetItem*> listItemsToSel;
1799     for (i = 0; i < k; i++) {
1800       anItem = new QListWidgetItem(QString::number(anArray[i]));
1801       myElements->addItem(anItem);
1802       if (aSelected.contains(anArray[i]))
1803         listItemsToSel.push_back(anItem);
1804     }
1805     bool blocked = myElements->signalsBlocked();
1806     myElements->blockSignals(true);
1807     foreach(anItem, listItemsToSel) anItem->setSelected(true);
1808     myElements->blockSignals(blocked);
1809     listItemsToSel.clear();
1810     myIsBusy = false;
1811   }
1812 }
1813
1814 //=================================================================================
1815 // function : closeEvent()
1816 // purpose  :
1817 //=================================================================================
1818 void SMESHGUI_GroupDlg::closeEvent (QCloseEvent*)
1819 {
1820   onClose();
1821 }
1822
1823 //=================================================================================
1824 // function : SMESHGUI_GroupDlg::onClose
1825 // purpose  : SLOT called when "Close" button pressed. Close dialog
1826 //=================================================================================
1827 void SMESHGUI_GroupDlg::onClose()
1828 {
1829   if (SMESH::GetCurrentVtkView()) {
1830     SMESH::RemoveFilters(); // PAL6938 -- clean all mesh entity filters
1831     SMESH::SetPointRepresentation(false);
1832     SMESH::SetPickable();
1833     restoreShowEntityMode();
1834   }
1835
1836   mySelectionMgr->clearSelected();
1837   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
1838     aViewWindow->SetSelectionMode(ActorSelection);
1839   mySelectionMgr->clearFilters();
1840   mySMESHGUI->ResetState();
1841
1842   reject();
1843 }
1844
1845 //=================================================================================
1846 // function : onHelp()
1847 // purpose  :
1848 //=================================================================================
1849 void SMESHGUI_GroupDlg::onHelp()
1850 {
1851   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
1852   if (app)
1853     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString( "" ), myHelpFileName);
1854   else {
1855     QString platform;
1856 #ifdef WIN32
1857     platform = "winapplication";
1858 #else
1859     platform = "application";
1860 #endif
1861     SUIT_MessageBox::warning(this, tr( "WRN_WARNING" ),
1862                              tr( "EXTERNAL_BROWSER_CANNOT_SHOW_PAGE" ).
1863                              arg(app->resourceMgr()->stringValue( "ExternalBrowser",
1864                                                                  platform)).
1865                              arg(myHelpFileName));
1866   }
1867 }
1868
1869 //=================================================================================
1870 // function : SMESHGUI_GroupDlg::onDeactivate
1871 // purpose  : SLOT called when dialog must be deativated
1872 //=================================================================================
1873 void SMESHGUI_GroupDlg::onDeactivate()
1874 {
1875   mySMESHGUI->ResetState();
1876   setEnabled(false);
1877 }
1878
1879 //=================================================================================
1880 // function : SMESHGUI_GroupDlg::enterEvent
1881 // purpose  : Event filter
1882 //=================================================================================
1883 void SMESHGUI_GroupDlg::enterEvent (QEvent*)
1884 {
1885   if (!isEnabled()) {
1886     mySMESHGUI->EmitSignalDeactivateDialog();
1887     setEnabled(true);
1888     mySelectionMode = -1;
1889     setSelectionMode(myTypeId);
1890     //mySMESHGUI->SetActiveDialogBox((QDialog*)this);
1891     mySMESHGUI->SetActiveDialogBox(this);
1892     mySMESHGUI->SetState(800);
1893   }
1894 }
1895
1896 //=================================================================================
1897 // function : hideEvent
1898 // purpose  : caused by ESC key
1899 //=================================================================================
1900 void SMESHGUI_GroupDlg::hideEvent (QHideEvent*)
1901 {
1902   if (!isMinimized() && !myIsBusy)
1903     onClose();
1904 }
1905
1906 //=================================================================================
1907 // function : keyPressEvent()
1908 // purpose  :
1909 //=================================================================================
1910 void SMESHGUI_GroupDlg::keyPressEvent( QKeyEvent* e )
1911 {
1912   QDialog::keyPressEvent( e );
1913   if ( e->isAccepted() )
1914     return;
1915
1916   if ( e->key() == Qt::Key_F1 )
1917     {
1918       e->accept();
1919       onHelp();
1920     }
1921 }
1922
1923 //================================================================================
1924 /*!
1925  * \brief Enable showing of the popup when Geometry selection btn is clicked
1926   * \param enable - true to enable
1927  */
1928 //================================================================================
1929
1930 enum { DIRECT_GEOM_INDEX = 0, GEOM_BY_MESH_INDEX };
1931
1932 void SMESHGUI_GroupDlg::updateGeomPopup()
1933 {
1934   bool enable = false;
1935
1936   if ( !myMesh->_is_nil() )
1937     enable = myMesh->NbEdges() > 0;
1938
1939   if ( myGeomGroupBtn )
1940   {
1941     disconnect( myGeomGroupBtn, SIGNAL( toggled(bool) ), this, SLOT( onGeomSelectionButton(bool) ));
1942     if ( enable ) {
1943       if ( !myGeomPopup ) {
1944         myGeomPopup = new QMenu(this);
1945         myActions[myGeomPopup->addAction( tr( "DIRECT_GEOM_SELECTION" ) )] = DIRECT_GEOM_INDEX;
1946         myActions[myGeomPopup->addAction( tr( "GEOM_BY_MESH_ELEM_SELECTION" ) )] = GEOM_BY_MESH_INDEX;
1947         connect( myGeomPopup, SIGNAL( triggered( QAction* ) ), SLOT( onGeomPopup( QAction* ) ) );
1948       }
1949       connect( myGeomGroupBtn, SIGNAL( toggled(bool) ), this, SLOT( onGeomSelectionButton(bool) ));
1950     }
1951   }
1952 }
1953
1954
1955 //=================================================================================
1956 // function : onGeomSelectionButton()
1957 // purpose  :
1958 //=================================================================================
1959 void SMESHGUI_GroupDlg::onGeomSelectionButton(bool isBtnOn)
1960 {
1961   if ( myGeomPopup && isBtnOn )
1962     {
1963       myCurrentLineEdit = myGeomGroupLine;
1964       QAction* a = myGeomPopup->exec( QCursor::pos() );
1965       if (!a || myActions[a] == DIRECT_GEOM_INDEX)
1966         setSelectionMode(7);
1967     }
1968   else if (!isBtnOn)
1969     {
1970       myCurrentLineEdit = 0;
1971       setSelectionMode(8);
1972     }
1973 }
1974
1975 //=================================================================================
1976 // function : onGeomPopup()
1977 // purpose  :
1978 //=================================================================================
1979 void SMESHGUI_GroupDlg::onGeomPopup( QAction* a )
1980 {
1981   int index = myActions[a];
1982   if ( index == GEOM_BY_MESH_INDEX )
1983     {
1984       mySelectionMode = -1;
1985       if ( !myShapeByMeshOp ) {
1986         myShapeByMeshOp = new SMESHGUI_ShapeByMeshOp(true);
1987         connect(myShapeByMeshOp, SIGNAL(committed(SUIT_Operation*)),
1988                 SLOT(onPublishShapeByMeshDlg(SUIT_Operation*)));
1989         connect(myShapeByMeshOp, SIGNAL(aborted(SUIT_Operation*)),
1990                 SLOT(onCloseShapeByMeshDlg(SUIT_Operation*)));
1991       }
1992       // set mesh object to SMESHGUI_ShapeByMeshOp and start it
1993       if ( !myMesh->_is_nil() ) {
1994         myIsBusy = true;
1995         hide(); // stop processing selection
1996         myIsBusy = false;
1997         myShapeByMeshOp->setModule( mySMESHGUI );
1998         myShapeByMeshOp->setStudy( 0 ); // it's really necessary
1999         myShapeByMeshOp->SetMesh( myMesh );
2000         myShapeByMeshOp->start();
2001       }
2002     }
2003 }
2004
2005 //================================================================================
2006 /*!
2007  * \brief SLOT. Is called when Ok is pressed in SMESHGUI_ShapeByMeshDlg
2008  */
2009 //================================================================================
2010
2011 void SMESHGUI_GroupDlg::onPublishShapeByMeshDlg(SUIT_Operation* op)
2012 {
2013   if ( myShapeByMeshOp == op ) {
2014     mySMESHGUI->getApp()->updateObjectBrowser();
2015     show();
2016     // Select a found geometry object
2017     GEOM::GEOM_Object_var aGeomVar = myShapeByMeshOp->GetShape();
2018     if ( !aGeomVar->_is_nil() )
2019     {
2020       QString ID = aGeomVar->GetStudyEntry();
2021       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
2022       if ( _PTR(SObject) aGeomSO = aStudy->FindObjectID( ID.toLatin1().data() )) {
2023         SALOME_ListIO anIOList;
2024         Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject
2025           ( aGeomSO->GetID().c_str(), "SMESH", aGeomSO->GetName().c_str() );
2026         anIOList.Append( anIO );
2027         mySelectionMgr->setSelectedObjects( anIOList, false );
2028         onObjectSelectionChanged();
2029       }
2030     }
2031   }
2032 }
2033
2034 //================================================================================
2035 /*!
2036  * \brief SLOT. Is called when Close is pressed in SMESHGUI_ShapeByMeshDlg
2037  */
2038 //================================================================================
2039
2040 void SMESHGUI_GroupDlg::onCloseShapeByMeshDlg(SUIT_Operation* op)
2041 {
2042   if ( myShapeByMeshOp == op )
2043     {
2044       show();
2045       setSelectionMode(7);
2046     }
2047 }
2048
2049 //=================================================================================
2050 // function : setGroupColor()
2051 // purpose  :
2052 //=================================================================================
2053 void SMESHGUI_GroupDlg::setGroupColor( const SALOMEDS::Color& theColor )
2054 {
2055   QColor aQColor( (int)( theColor.R * 255.0 ),
2056                   (int)( theColor.G * 255.0 ),
2057                   (int)( theColor.B * 255.0 ) );
2058   setGroupQColor( aQColor );
2059 }
2060
2061 //=================================================================================
2062 // function : getGroupColor()
2063 // purpose  :
2064 //=================================================================================
2065 SALOMEDS::Color SMESHGUI_GroupDlg::getGroupColor() const
2066 {
2067   QColor aQColor = getGroupQColor();
2068
2069   SALOMEDS::Color aColor;
2070   aColor.R = (float)aQColor.red() / 255.0;
2071   aColor.G = (float)aQColor.green() / 255.0;
2072   aColor.B = (float)aQColor.blue() / 255.0;
2073
2074   return aColor;
2075 }
2076
2077 //=================================================================================
2078 // function : setGroupQColor()
2079 // purpose  :
2080 //=================================================================================
2081 void SMESHGUI_GroupDlg::setGroupQColor( const QColor& theColor )
2082 {
2083   if( theColor.isValid() )
2084     myColorBtn->setColor( theColor );
2085 }
2086
2087 //=================================================================================
2088 // function : getGroupQColor()
2089 // purpose  :
2090 //=================================================================================
2091 QColor SMESHGUI_GroupDlg::getGroupQColor() const
2092 {
2093   return myColorBtn->color();
2094 }
2095
2096 //=================================================================================
2097 // function : setDefaultGroupColor()
2098 // purpose  :
2099 //=================================================================================
2100 void SMESHGUI_GroupDlg::setDefaultGroupColor()
2101 {
2102   if( myMesh->_is_nil() )
2103     return;
2104
2105   bool isAutoColor = myMesh->GetAutoColor();
2106
2107   QColor aQColor;
2108   if( !isAutoColor )
2109   {
2110     int r = 0, g = 0, b = 0;
2111     SMESH::GetColor( "SMESH", "fill_color", r, g, b, QColor( 0, 170, 255 ) );
2112     aQColor.setRgb( r, g, b );
2113   }
2114   else
2115   {
2116     SMESH::ListOfGroups aListOfGroups = *myMesh->GetGroups();
2117
2118     QList<SALOMEDS::Color> aReservedColors;
2119     for( int i = 0, n = aListOfGroups.length(); i < n; i++ )
2120     {
2121       SMESH::SMESH_GroupBase_var aGroupObject = aListOfGroups[i];
2122       SALOMEDS::Color aReservedColor = aGroupObject->GetColor();
2123       aReservedColors.append( aReservedColor );
2124     }
2125
2126     SALOMEDS::Color aColor = SMESHGUI::getUniqueColor( aReservedColors );
2127     aQColor.setRgb( (int)( aColor.R * 255.0 ),
2128                     (int)( aColor.G * 255.0 ),
2129                     (int)( aColor.B * 255.0 ) );
2130
2131   }
2132
2133   setGroupQColor( aQColor );
2134 }
2135
2136 //=================================================================================
2137 // function : SetAppropriateActor()
2138 // purpose  : Find more appropriate of visible actors, set it to myActor, allow picking
2139 //            NPAL19389: create a group with a selection in another group.
2140 //            if mesh actor is not visible - find any first visible group or submesh
2141 //=================================================================================
2142 bool SMESHGUI_GroupDlg::SetAppropriateActor()
2143 {
2144   bool isActor = false;
2145
2146   if (myMesh->_is_nil()) return false;
2147
2148   SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView();
2149
2150   // try mesh actor
2151   myActor = SMESH::FindActorByObject(myMesh);
2152   if (myActor && myActor->hasIO())
2153   {
2154     isActor = true;
2155     if (aViewWindow && !aViewWindow->isVisible(myActor->getIO()))
2156         isActor = false;
2157   }
2158
2159   // try current group actor
2160   if (!isActor) {
2161     if (!myGroup->_is_nil()) {
2162       myActor = SMESH::FindActorByObject(myGroup);
2163       if (myActor && myActor->hasIO())
2164       {
2165         isActor = true;
2166         if (aViewWindow && !aViewWindow->isVisible(myActor->getIO()))
2167             isActor = false;
2168       }
2169     }
2170   }
2171
2172   // try current group on geometry actor
2173   if (!isActor) {
2174     if (!myGroupOnGeom->_is_nil()) {
2175       myActor = SMESH::FindActorByObject(myGroupOnGeom);
2176       if (myActor && myActor->hasIO())
2177       {
2178         isActor = true;
2179         if (aViewWindow && !aViewWindow->isVisible(myActor->getIO()))
2180           isActor = false;
2181       }
2182     }
2183   }
2184
2185   // try any visible actor of group or submesh of current mesh
2186   if (!isActor && aViewWindow) {
2187     // mesh entry
2188     _PTR(SObject) aSObject = SMESH::FindSObject(myMesh);
2189     if (aSObject) {
2190       CORBA::String_var meshEntry = aSObject->GetID().c_str();
2191       int len = strlen(meshEntry);
2192
2193       // iterate on all actors in current view window, search for
2194       // any visible actor, that belongs to group or submesh of current mesh
2195       vtkActorCollection *aCollection = aViewWindow->getRenderer()->GetActors();
2196       aCollection->InitTraversal();
2197       for (vtkActor *anAct = aCollection->GetNextActor();
2198            anAct && !isActor;
2199            anAct = aCollection->GetNextActor())
2200       {
2201         SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct);
2202         if (anActor && anActor->hasIO()) {
2203           Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
2204           if (aViewWindow->isVisible(anIO)) {
2205             if (anIO->hasEntry() && strncmp(anIO->getEntry(), meshEntry, len) == 0) {
2206               myActor = anActor;
2207               isActor = true;
2208             }
2209           }
2210         }
2211       }
2212     }
2213   }
2214
2215   if (isActor)
2216     SMESH::SetPickable(myActor);
2217
2218   return isActor;
2219 }
2220
2221 //=======================================================================
2222 //function : setShowEntityMode
2223 //purpose  : make shown only entity corresponding to my type
2224 //=======================================================================
2225 void SMESHGUI_GroupDlg::setShowEntityMode()
2226 {
2227   if ( !myMesh->_is_nil() ) {
2228     if ( SMESH_Actor* actor = SMESH::FindActorByObject(myMesh) ) {
2229       if (!myStoredShownEntity)
2230         myStoredShownEntity = actor->GetEntityMode();
2231       switch ( myTypeId ) {
2232       case 0: restoreShowEntityMode(); break;
2233       case 1: actor->SetEntityMode( SMESH_Actor::eEdges ); break;
2234       case 2: actor->SetEntityMode( SMESH_Actor::eFaces ); break;
2235       case 3: actor->SetEntityMode( SMESH_Actor::eVolumes ); break;
2236       }
2237     }
2238   }
2239 }
2240
2241 //=======================================================================
2242 //function : restoreShowEntityMode
2243 //purpose  : restore ShowEntity mode of myActor
2244 //=======================================================================
2245 void SMESHGUI_GroupDlg::restoreShowEntityMode()
2246 {
2247   if ( myStoredShownEntity && !myMesh->_is_nil() ) {
2248     if ( SMESH_Actor* actor = SMESH::FindActorByObject(myMesh) ) {
2249       actor->SetEntityMode(myStoredShownEntity);
2250     }
2251   }
2252   myStoredShownEntity = 0;
2253 }