Salome HOME
19af7e001c2d35d50e6668ab3d1438bf22b2a12c
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_GroupDlg.cxx
1 // SMESH SMESHGUI : GUI for SMESH component
2 //
3 // Copyright (C) 2003  CEA
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 //
19 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 //
21 // File   : SMESHGUI_GroupDlg.cxx
22 // Author : Natalia KOPNOVA, Open CASCADE S.A.S.
23 //
24
25 // SMESH includes
26 #include "SMESHGUI_GroupDlg.h"
27
28 #include "SMESHGUI.h"
29 #include "SMESHGUI_Utils.h"
30 #include "SMESHGUI_VTKUtils.h"
31 #include "SMESHGUI_GroupUtils.h"
32 #include "SMESHGUI_FilterUtils.h"
33 #include "SMESHGUI_GEOMGenUtils.h"
34 #include "SMESHGUI_FilterDlg.h"
35 #include "SMESHGUI_ShapeByMeshDlg.h"
36
37 #include <SMESH_TypeFilter.hxx>
38 #include <SMESH_Actor.h>
39 #include <SMESH_ActorUtils.h>
40
41 // SALOME GEOM includes
42 #include <GEOMBase.h>
43 #include <GEOM_SelectionFilter.h>
44
45 // SALOME GUI includes
46 #include <QtxColorButton.h>
47
48 #include <SUIT_Desktop.h>
49 #include <SUIT_ResourceMgr.h>
50 #include <SUIT_Session.h>
51 #include <SUIT_MessageBox.h>
52
53 #include <SalomeApp_Tools.h>
54 #include <SalomeApp_Application.h>
55 #include <SalomeApp_Study.h>
56 #include <LightApp_SelectionMgr.h>
57
58 #include <SALOME_ListIO.hxx>
59 #include <SALOME_ListIteratorOfListIO.hxx>
60
61 #include <SVTK_ViewWindow.h>
62
63 // SALOME KERNEL includes
64 #include <SALOMEDSClient_Study.hxx>
65
66 // VTK Includes
67 #include <vtkRenderer.h>
68 #include <vtkActorCollection.h>
69
70 // OCCT includes
71 #include <TColStd_MapOfInteger.hxx>
72
73 // Qt includes
74 #include <QButtonGroup>
75 #include <QGroupBox>
76 #include <QLabel>
77 #include <QLineEdit>
78 #include <QPushButton>
79 #include <QToolButton>
80 #include <QRadioButton>
81 #include <QCheckBox>
82 #include <QGridLayout>
83 #include <QHBoxLayout>
84 #include <QVBoxLayout>
85 #include <QListWidget>
86 #include <QStackedWidget>
87 #include <QKeyEvent>
88 #include <QMenu>
89
90 // STL includes
91 #include <vector>
92 #include <algorithm>
93 #include <set>
94
95 #define SPACING 6
96 #define MARGIN  11
97
98 //=================================================================================
99 // function : SMESHGUI_GroupDlg()
100 // purpose  :
101 //=================================================================================
102 SMESHGUI_GroupDlg::SMESHGUI_GroupDlg( SMESHGUI* theModule,
103                                       SMESH::SMESH_Mesh_ptr theMesh )
104   : QDialog( SMESH::GetDesktop( theModule ) ),
105     mySMESHGUI( theModule ),
106     mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
107     mySelector( SMESH::GetViewWindow( theModule )->GetSelector() ),
108     myIsBusy( false ),
109     myNameChanged( false ),
110     myActor( 0 )
111 {
112   initDialog( true );
113   if ( !theMesh->_is_nil() )
114     init( theMesh );
115   else
116   {
117     mySelectSubMesh->setEnabled( false );
118     mySelectGroup->setEnabled( false );
119     myGeomGroupBtn->setEnabled( false );
120     myGeomGroupLine->setEnabled( false );
121   }
122 }
123
124 //=================================================================================
125 // function : SMESHGUI_GroupDlg()
126 // purpose  :
127 //=================================================================================
128 SMESHGUI_GroupDlg::SMESHGUI_GroupDlg( SMESHGUI* theModule,
129                                       SMESH::SMESH_GroupBase_ptr theGroup )
130   : QDialog( SMESH::GetDesktop( theModule ) ),
131     mySMESHGUI( theModule ),
132     mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
133     mySelector( SMESH::GetViewWindow( theModule )->GetSelector() ),
134     myIsBusy( false ),
135     myNameChanged( false )
136 {
137   initDialog( false );
138   if ( !theGroup->_is_nil() )
139     init( theGroup );
140   else
141   {
142     mySelectSubMesh->setEnabled( false );
143     mySelectGroup->setEnabled( false );
144
145     myCurrentLineEdit = myMeshGroupLine;
146     setSelectionMode( 5 );
147   }
148 }
149
150 //=================================================================================
151 // function : SMESHGUI_GroupDlg()
152 // purpose  :
153 //=================================================================================
154 void SMESHGUI_GroupDlg::initDialog( bool create)
155 {
156   setModal( false );
157   setAttribute( Qt::WA_DeleteOnClose, true );
158
159   myFilterDlg = 0;
160   myCreate = create;
161   myCurrentLineEdit = 0;
162
163   myShapeByMeshOp = 0;
164   myGeomPopup = 0;
165   myGeomObjects = new GEOM::ListOfGO();
166   myGeomObjects->length( 0 );
167
168   QPixmap image0( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH", tr( "ICON_SELECT" ) ) );
169
170   setWindowTitle( create ? tr( "SMESH_CREATE_GROUP_TITLE" ) : tr( "SMESH_EDIT_GROUP_TITLE" ) );
171   myHelpFileName = create ? "creating_groups_page.html" : "editing_groups_page.html";
172
173   setSizeGripEnabled( true);
174
175   QGridLayout* aMainLayout = new QGridLayout( this );
176   aMainLayout->setMargin( MARGIN );
177   aMainLayout->setSpacing( SPACING );
178
179   /***************************************************************/
180   QLabel* meshGroupLab = new QLabel( create ? tr( "SMESH_MESH" ) : tr( "SMESH_GROUP" ), this );
181   myMeshGroupBtn = new QPushButton( this );
182   myMeshGroupBtn->setIcon( image0 );
183   myMeshGroupLine = new QLineEdit( this );
184   myMeshGroupLine->setReadOnly( true );
185
186   /***************************************************************/
187   QGroupBox* aTypeBox = new QGroupBox( tr( "SMESH_ELEMENTS_TYPE" ), this );
188   myTypeGroup = new QButtonGroup( this );
189   QHBoxLayout* aTypeBoxLayout = new QHBoxLayout( aTypeBox );
190   aTypeBoxLayout->setMargin( MARGIN );
191   aTypeBoxLayout->setSpacing( SPACING );
192
193   QStringList types;
194   types.append( tr( "MESH_NODE" ) );
195   types.append( tr( "SMESH_EDGE" ) );
196   types.append( tr( "SMESH_FACE" ) );
197   types.append( tr( "SMESH_VOLUME" ) );
198   QRadioButton* rb;
199   for ( int i = 0; i < types.count(); i++ )
200   {
201     rb = new QRadioButton( types[i], aTypeBox );
202     myTypeGroup->addButton( rb, i );
203     aTypeBoxLayout->addWidget( rb );
204   }
205   aTypeBox->setEnabled( create );
206   myTypeId = -1;
207
208   /***************************************************************/
209   QLabel* aName = new QLabel( tr( "SMESH_NAME" ), this );
210   aName->setMinimumWidth( 50 );
211   myName = new QLineEdit( this );
212
213   /***************************************************************/
214   QGroupBox* aGrpTypeBox = new QGroupBox( tr( "SMESH_GROUP_TYPE" ), this );
215   myGrpTypeGroup = new QButtonGroup( this );
216   QHBoxLayout* aGrpTypeBoxLayout = new QHBoxLayout( aGrpTypeBox );
217   aGrpTypeBoxLayout->setMargin( MARGIN );
218   aGrpTypeBoxLayout->setSpacing( SPACING );
219
220   QRadioButton* rb1 = new QRadioButton( tr( "SMESH_GROUP_STANDALONE" ), aGrpTypeBox );
221   QRadioButton* rb2 = new QRadioButton( tr( "SMESH_GROUP_GEOMETRY" ),   aGrpTypeBox );
222   myGrpTypeGroup->addButton( rb1, 0 );
223   myGrpTypeGroup->addButton( rb2, 1 );
224   aGrpTypeBoxLayout->addWidget( rb1 );
225   aGrpTypeBoxLayout->addWidget( rb2 );
226   aGrpTypeBox->setEnabled( create );
227   myGrpTypeId = -1;
228
229   /***************************************************************/
230   myWGStack = new QStackedWidget( this );
231   QWidget* wg1 = new QWidget( myWGStack );
232   QWidget* wg2 = new QWidget( myWGStack );
233
234   /***************************************************************/
235   QGroupBox* aContentBox = new QGroupBox( tr( "SMESH_CONTENT" ), wg1 );
236   QGridLayout* aContentBoxLayout = new QGridLayout( aContentBox );
237   aContentBoxLayout->setMargin( MARGIN );
238   aContentBoxLayout->setSpacing( SPACING );
239
240   QLabel* aLabel = new QLabel( tr( "SMESH_ID_ELEMENTS" ), aContentBox );
241   myElements = new QListWidget( aContentBox );
242   myElements->setSelectionMode( QListWidget::ExtendedSelection );
243
244   myFilter = new QPushButton( tr( "SMESH_BUT_FILTER" ), aContentBox );
245   QPushButton* aAddBtn = new QPushButton( tr( "SMESH_BUT_ADD" ), aContentBox );
246   QPushButton* aRemoveBtn = new QPushButton( tr( "SMESH_BUT_REMOVE" ), aContentBox );
247   QPushButton* aSortBtn = new QPushButton( tr( "SMESH_BUT_SORT" ), aContentBox );
248
249   aContentBoxLayout->addWidget( aLabel,     0, 0 );
250   aContentBoxLayout->addWidget( myElements, 1, 0, 6, 1 );
251   aContentBoxLayout->addWidget( myFilter,   1, 1 );
252   aContentBoxLayout->addWidget( aAddBtn,    3, 1 );
253   aContentBoxLayout->addWidget( aRemoveBtn, 4, 1 );
254   aContentBoxLayout->addWidget( aSortBtn,   6, 1 );
255
256   aContentBoxLayout->setColumnStretch( 0, 1 );
257   aContentBoxLayout->setRowStretch( 2, 1 );
258   aContentBoxLayout->setRowStretch( 5, 1 );
259
260   /***************************************************************/
261   QGroupBox* aSelectBox = new QGroupBox( tr( "SMESH_SELECT_FROM" ), wg1 );
262   QGridLayout* aSelectBoxLayout = new QGridLayout( aSelectBox );
263   aSelectBoxLayout->setMargin( MARGIN );
264   aSelectBoxLayout->setSpacing( SPACING );
265
266   mySelectSubMesh = new QCheckBox( tr( "SMESH_SUBMESH" ), aSelectBox );
267   mySubMeshBtn = new QPushButton( aSelectBox );
268   mySubMeshBtn->setIcon( image0 );
269   mySubMeshLine = new QLineEdit( aSelectBox );
270   mySubMeshLine->setReadOnly( true );
271   onSelectSubMesh( false );
272
273   mySelectGroup = new QCheckBox( tr( "SMESH_GROUP" ), aSelectBox );
274   myGroupBtn = new QPushButton( aSelectBox );
275   myGroupBtn->setIcon( image0 );
276   myGroupLine = new QLineEdit( aSelectBox );
277   myGroupLine->setReadOnly( true );
278   onSelectGroup( false );
279
280   aSelectBoxLayout->addWidget( mySelectSubMesh, 0, 0 );
281   aSelectBoxLayout->addWidget( mySubMeshBtn,    0, 1 );
282   aSelectBoxLayout->addWidget( mySubMeshLine,   0, 2 );
283   aSelectBoxLayout->addWidget( mySelectGroup,   1, 0 );
284   aSelectBoxLayout->addWidget( myGroupBtn,      1, 1 );
285   aSelectBoxLayout->addWidget( myGroupLine,     1, 2 );
286
287   /***************************************************************/
288   QVBoxLayout* wg1Layout = new QVBoxLayout( wg1 );
289   wg1Layout->setMargin( 0 );
290   wg1Layout->setSpacing( SPACING );
291   wg1Layout->addWidget( aContentBox );
292   wg1Layout->addWidget( aSelectBox );
293   wg1Layout->setStretchFactor( aContentBox, 10 );
294
295   /***************************************************************/
296   QLabel* geomObject = new QLabel( tr( "SMESH_OBJECT_GEOM" ), wg2 );
297   myGeomGroupBtn = new QToolButton( wg2 );
298   myGeomGroupBtn->setIcon( image0 );
299   myGeomGroupBtn->setCheckable( true );
300   myGeomGroupLine = new QLineEdit( wg2 );
301   myGeomGroupLine->setReadOnly( true ); //VSR ???
302   onSelectGeomGroup( false );
303
304   myGeomGroupBtn->setEnabled( create );
305   myGeomGroupLine->setEnabled( create );
306
307   /***************************************************************/
308   QGridLayout* wg2Layout = new QGridLayout( wg2 );
309   wg2Layout->setMargin( 0 );
310   wg1Layout->setSpacing( SPACING );
311   wg2Layout->addWidget( geomObject,     0, 0 );
312   wg2Layout->addWidget( myGeomGroupBtn, 0, 1 );
313   wg2Layout->addWidget( myGeomGroupLine,0, 2 );
314   wg2Layout->setRowStretch( 1, 5 );
315
316   /***************************************************************/
317   myWGStack->insertWidget( 0, wg1 );
318   myWGStack->insertWidget( 1, wg2 );
319
320   /***************************************************************/
321   QGroupBox* aColorBox = new QGroupBox(tr( "SMESH_SET_COLOR" ), this);
322   QHBoxLayout* aColorBoxLayout = new QHBoxLayout(aColorBox);
323   aColorBoxLayout->setMargin(MARGIN);
324   aColorBoxLayout->setSpacing(SPACING);
325
326   QLabel* aColorLab = new QLabel(tr( "SMESH_CHECK_COLOR" ), aColorBox );
327   myColorBtn = new QtxColorButton(aColorBox);
328   myColorBtn->setSizePolicy( QSizePolicy::MinimumExpanding, 
329                              myColorBtn->sizePolicy().verticalPolicy() );
330
331   aColorBoxLayout->addWidget(aColorLab);
332   aColorBoxLayout->addWidget(myColorBtn);
333
334   /***************************************************************/
335
336   QFrame* aButtons = new QFrame(this);
337   aButtons->setFrameStyle( QFrame::Box | QFrame::Sunken );
338   QHBoxLayout* aBtnLayout = new QHBoxLayout(aButtons);
339   aBtnLayout->setMargin(MARGIN);
340   aBtnLayout->setSpacing(SPACING);
341
342   myOKBtn = new QPushButton(tr( "SMESH_BUT_APPLY_AND_CLOSE" ), aButtons);
343   myOKBtn->setAutoDefault(true);
344   myOKBtn->setDefault(true);
345   myApplyBtn = new QPushButton(tr( "SMESH_BUT_APPLY" ), aButtons);
346   myApplyBtn->setAutoDefault(true);
347   myCloseBtn = new QPushButton(tr( "SMESH_BUT_CLOSE" ), aButtons);
348   myCloseBtn->setAutoDefault(true);
349   myHelpBtn = new QPushButton(tr( "SMESH_BUT_HELP" ), aButtons);
350   myHelpBtn->setAutoDefault(true);
351
352   aBtnLayout->addWidget(myOKBtn);
353   aBtnLayout->addSpacing(10);
354   aBtnLayout->addWidget(myApplyBtn);
355   aBtnLayout->addSpacing(10);
356   aBtnLayout->addStretch();
357   aBtnLayout->addWidget(myCloseBtn);
358   aBtnLayout->addWidget(myHelpBtn);
359
360   /***************************************************************/
361   aMainLayout->addWidget(meshGroupLab,    0, 0);
362   aMainLayout->addWidget(myMeshGroupBtn,  0, 1);
363   aMainLayout->addWidget(myMeshGroupLine, 0, 2);
364   aMainLayout->addWidget(aTypeBox,        1, 0, 1, 3);
365   aMainLayout->addWidget(aName,           2, 0);
366   aMainLayout->addWidget(myName,          2, 2);
367   aMainLayout->addWidget(aGrpTypeBox,     3, 0, 1, 3);
368   aMainLayout->addWidget(myWGStack,       4, 0, 1, 3);
369   aMainLayout->addWidget(aColorBox,       5, 0, 1, 3);
370   aMainLayout->addWidget(aButtons,        6, 0, 1, 3);
371
372   /* signals and slots connections */
373   connect(myMeshGroupBtn, SIGNAL(clicked()),          this, SLOT(setCurrentSelection()));
374   connect(myGrpTypeGroup, SIGNAL(buttonClicked(int)), this, SLOT(onGrpTypeChanged(int)));
375   connect(myTypeGroup,    SIGNAL(buttonClicked(int)), this, SLOT(onTypeChanged(int)));
376
377   connect(myName,     SIGNAL(textChanged(const QString&)), this, SLOT(onNameChanged(const QString&)));
378   connect(myElements, SIGNAL(itemSelectionChanged()),      this, SLOT(onListSelectionChanged()));
379
380   connect(myFilter,   SIGNAL(clicked()), this, SLOT(setFilters()));
381   connect(aAddBtn,    SIGNAL(clicked()), this, SLOT(onAdd()));
382   connect(aRemoveBtn, SIGNAL(clicked()), this, SLOT(onRemove()));
383   connect(aSortBtn,   SIGNAL(clicked()), this, SLOT(onSort()));
384
385   connect(mySelectSubMesh, SIGNAL(toggled(bool)), this, SLOT(onSelectSubMesh(bool)));
386   connect(mySelectGroup,   SIGNAL(toggled(bool)), this, SLOT(onSelectGroup(bool)));
387   connect(mySubMeshBtn,    SIGNAL(clicked()), this, SLOT(setCurrentSelection()));
388   connect(myGroupBtn,      SIGNAL(clicked()), this, SLOT(setCurrentSelection()));
389   connect(myGeomGroupBtn,  SIGNAL(toggled(bool)), this, SLOT(onGeomSelectionButton(bool)));
390
391   connect(myColorBtn, SIGNAL(changed( QColor )), this, SLOT(onColorChanged( QColor )));
392
393   connect(myOKBtn,    SIGNAL(clicked()), this, SLOT(onOK()));
394   connect(myApplyBtn, SIGNAL(clicked()), this, SLOT(onApply()));
395   connect(myCloseBtn, SIGNAL(clicked()), this, SLOT(onClose()));
396   connect(myHelpBtn,  SIGNAL(clicked()), this, SLOT(onHelp()));
397
398   /* Init selection */
399   mySMESHGUI->SetActiveDialogBox(this);
400   mySMESHGUI->SetState(800);
401
402   mySelectionMode = -1;
403   myMeshFilter = new SMESH_TypeFilter(MESH);
404   mySubMeshFilter = new SMESH_TypeFilter(SUBMESH);
405   myGroupFilter = new SMESH_TypeFilter(GROUP);
406   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( mySMESHGUI->application()->activeStudy() );
407   myGeomFilter = new GEOM_SelectionFilter( aStudy, true );
408
409   connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(onDeactivate()));
410   connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()),        this, SLOT(onClose()));
411   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()),  this, SLOT(onObjectSelectionChanged()));
412
413   rb1->setChecked(true); // VSR !!!
414   onGrpTypeChanged(0); // VSR!!!
415
416   if (myMesh->_is_nil() )
417     myTypeGroup->button(0)->setChecked(true);
418
419   updateButtons();
420   //myName->setText(GetDefaultName(tr( "SMESH_GROUP" )));
421 }
422
423 //=================================================================================
424 // function : ~SMESHGUI_GroupDlg()
425 // purpose  : Destroys the object and frees any allocated resources
426 //=================================================================================
427 SMESHGUI_GroupDlg::~SMESHGUI_GroupDlg()
428 {
429   // no need to delete child widgets, Qt does it all for us
430   if ( myFilterDlg != 0 ) {
431     myFilterDlg->setParent( 0 );
432     delete myFilterDlg;
433   }
434 }
435
436 //=================================================================================
437 // function : GetDefaultName()
438 // purpose  : Get the Group Name if Create new Group
439 //=================================================================================
440 QString SMESHGUI_GroupDlg::GetDefaultName(const QString& theOperation)
441 {
442   QString aName = "";
443
444   // collect all object names of SMESH component
445   SalomeApp_Study* appStudy =
446     dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
447   if ( !appStudy ) return aName;
448   _PTR(Study) aStudy = appStudy->studyDS();
449
450   std::set<std::string> aSet;
451   _PTR(SComponent) aMeshCompo (aStudy->FindComponent( "SMESH" ));
452   if (aMeshCompo) {
453     _PTR(ChildIterator) it (aStudy->NewChildIterator(aMeshCompo));
454     _PTR(SObject) obj;
455     for (it->InitEx(true); it->More(); it->Next()) {
456       obj = it->Value();
457       aSet.insert(obj->GetName());
458     }
459   }
460
461   // build a unique name
462   int aNumber = 0;
463   bool isUnique = false;
464   while (!isUnique) {
465     aName = theOperation + "_" + QString::number(++aNumber);
466     isUnique = (aSet.count(aName.toLatin1().data()) == 0);
467   }
468
469   return aName;
470 }
471
472 //=================================================================================
473 // function : Init()
474 // purpose  :
475 //=================================================================================
476 void SMESHGUI_GroupDlg::init (SMESH::SMESH_Mesh_ptr theMesh)
477 {
478   mySelectionMgr->installFilter(myMeshFilter);
479
480   /* init data from current selection */
481   restoreShowEntityMode();
482   myMesh = SMESH::SMESH_Mesh::_duplicate(theMesh);
483   setShowEntityMode();
484   myGroup = SMESH::SMESH_Group::_nil();
485   myGroupOnGeom = SMESH::SMESH_GroupOnGeom::_nil();
486
487   // NPAL19389: create a group with a selection in another group
488   // set actor of myMesh, if it is visible, else try
489   // any visible actor of group or submesh of myMesh
490   SetAppropriateActor();
491
492   setDefaultGroupColor();
493
494   SALOME_ListIO aList;
495   mySelectionMgr->selectedObjects( aList );
496   if( !aList.IsEmpty() )
497   {
498     QString aName = aList.First()->getName();
499     myMeshGroupLine->setText(aName);
500     myMeshGroupLine->home( false );
501   }
502
503   myCurrentLineEdit = 0;
504
505   myTypeGroup->button(0)->setChecked(true);
506   onTypeChanged(0);
507 }
508
509 //=================================================================================
510 // function : Init()
511 // purpose  :
512 //=================================================================================
513 void SMESHGUI_GroupDlg::init (SMESH::SMESH_GroupBase_ptr theGroup)
514 {
515   restoreShowEntityMode();
516   myMesh = theGroup->GetMesh();
517   setShowEntityMode();
518
519   myNameChanged = true;
520   myName->blockSignals(true);
521   myName->setText(theGroup->GetName());
522   myName->blockSignals(false);
523   myName->home(false);
524
525   SALOMEDS::Color aColor = theGroup->GetColor();
526   setGroupColor( aColor );
527
528   myMeshGroupLine->setText(theGroup->GetName());
529
530   int aType = 0;
531   switch(theGroup->GetType()) {
532   case SMESH::NODE: aType= 0; break;
533   case SMESH::EDGE: aType = 1; break;
534   case SMESH::FACE: aType = 2; break;
535   case SMESH::VOLUME: aType = 3; break;
536   }
537   myTypeGroup->button(aType)->setChecked(true);
538
539   myGroup = SMESH::SMESH_Group::_narrow( theGroup );
540
541   if (!myGroup->_is_nil())
542   {
543     // NPAL19389: create a group with a selection in another group
544     // set actor of myMesh, if it is visible, else set
545     // actor of myGroup, if it is visible, else try
546     // any visible actor of group or submesh of myMesh
547     // commented, because an attempt to set selection on not displayed cells leads to error
548     //SetAppropriateActor();
549     myActor = SMESH::FindActorByObject(myMesh);
550     if ( !myActor )
551       myActor = SMESH::FindActorByObject(myGroup);
552     SMESH::SetPickable(myActor);
553
554     myGrpTypeGroup->button(0)->setChecked(true);
555     onGrpTypeChanged(0);
556
557     myCurrentLineEdit = 0;
558     myElements->clear();
559     setSelectionMode(aType);
560     myTypeId = aType;
561
562     setShowEntityMode(); // depends on myTypeId
563
564     myIdList.clear();
565     if (!myGroup->IsEmpty()) {
566       SMESH::long_array_var anElements = myGroup->GetListOfID();
567       int k = anElements->length();
568       for (int i = 0; i < k; i++) {
569         myIdList.append(anElements[i]);
570         myElements->addItem(QString::number(anElements[i]));
571       }
572       myElements->selectAll();
573     }
574   }
575   else
576   {
577     myGroupOnGeom = SMESH::SMESH_GroupOnGeom::_narrow( theGroup );
578
579     if ( !myGroupOnGeom->_is_nil() )
580     {
581       // NPAL19389: create a group with a selection in another group
582       // set actor of myMesh, if it is visible, else set
583       // actor of myGroupOnGeom, if it is visible, else try
584       // any visible actor of group or submesh of myMesh
585       // commented, because an attempt to set selection on not displayed cells leads to error
586       //SetAppropriateActor();
587       myActor = SMESH::FindActorByObject(myMesh);
588       if ( !myActor )
589         myActor = SMESH::FindActorByObject(myGroupOnGeom);
590       SMESH::SetPickable(myActor);
591
592       myGrpTypeGroup->button(1)->setChecked(true);
593       onGrpTypeChanged(1);
594
595       QString aShapeName( "" );
596       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
597       GEOM::GEOM_Object_var aGroupShape = myGroupOnGeom->GetShape();
598       if (!aGroupShape->_is_nil())
599       {
600         _PTR(SObject) aGroupShapeSO = aStudy->FindObjectID(aGroupShape->GetStudyEntry());
601         aShapeName = aGroupShapeSO->GetName().c_str();
602       }
603       myGeomGroupLine->setText( aShapeName );
604       myNameChanged = true;
605       myName->blockSignals(true);
606       myName->setText( "Group On " + aShapeName);
607       myName->blockSignals(false);
608     }
609   }
610 }
611
612 //=================================================================================
613 // function : updateButtons()
614 // purpose  :
615 //=================================================================================
616 void SMESHGUI_GroupDlg::updateButtons()
617 {
618   bool enable = !myName->text().trimmed().isEmpty();
619
620   if (myGrpTypeId == 0) {
621     enable = enable && myElements->count() > 0;
622     enable = enable && (!myGroup->_is_nil() || !myMesh->_is_nil());
623   }
624   else if (myGrpTypeId == 1) {
625     if (CORBA::is_nil(myGroupOnGeom)) { // creation mode
626       enable = enable && myGeomObjects->length() > 0 && !myMesh->_is_nil();
627     }
628   }
629
630   myOKBtn->setEnabled(enable);
631   myApplyBtn->setEnabled(enable);
632 }
633
634 //=================================================================================
635 // function : onNameChanged()
636 // purpose  :
637 //=================================================================================
638 void SMESHGUI_GroupDlg::onNameChanged (const QString& text)
639 {
640   myOldName = myName->text();
641   updateButtons();
642   myNameChanged = !myName->text().trimmed().isEmpty();
643 }
644
645 //=================================================================================
646 // function : onTypeChanged()
647 // purpose  : Group elements type radio button management
648 //=================================================================================
649 void SMESHGUI_GroupDlg::onTypeChanged (int id)
650 {
651   if (myTypeId != id) {
652     myElements->clear();
653     if (myCurrentLineEdit == 0)
654       setSelectionMode(id);
655     myTypeId = id;
656     setShowEntityMode();
657   }
658 }
659
660 //=================================================================================
661 // function : onGrpTypeChanged()
662 // purpose  : Group type radio button management
663 //=================================================================================
664 void SMESHGUI_GroupDlg::onGrpTypeChanged (int id)
665 {
666   if (myGrpTypeId != id) {
667     myWGStack->setCurrentIndex( id );
668     myName->blockSignals(true);
669     myName->setText(myOldName);
670     myName->blockSignals(false);
671     onSelectGeomGroup(id == 1);
672   }
673   myGrpTypeId = id;
674 }
675
676 //=================================================================================
677 // function : onColorChanged()
678 // purpose  : Color button management
679 //=================================================================================
680 void SMESHGUI_GroupDlg::onColorChanged(QColor theColor)
681 {
682   updateButtons();
683 }
684
685 //=================================================================================
686 // function : setSelectionMode()
687 // purpose  : Radio button management
688 //=================================================================================
689 void SMESHGUI_GroupDlg::setSelectionMode (int theMode)
690 {
691   // PAL7314
692   if (myMesh->_is_nil())
693     return;
694   if (mySelectionMode != theMode) {
695     // [PAL10408] mySelectionMgr->clearSelected();
696     mySelectionMgr->clearFilters();
697     if (myActor)
698       myActor->SetPointRepresentation(false);
699     else
700       SMESH::SetPointRepresentation(false);
701     if (theMode < 4) {
702       switch (theMode) {
703       case 0:
704         if (myActor)
705           myActor->SetPointRepresentation(true);
706         else
707           SMESH::SetPointRepresentation(true);
708         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
709           aViewWindow->SetSelectionMode(NodeSelection);
710         break;
711       case 1:
712         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
713           aViewWindow->SetSelectionMode(EdgeSelection);
714         break;
715       case 2:
716         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
717           aViewWindow->SetSelectionMode(FaceSelection);
718         break;
719       default:
720         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
721           aViewWindow->SetSelectionMode(VolumeSelection);
722       }
723     } else {
724       if (theMode == 4)
725         mySelectionMgr->installFilter(mySubMeshFilter);
726       else if (theMode == 5)
727         mySelectionMgr->installFilter(myGroupFilter);
728       else if (theMode == 6)
729         mySelectionMgr->installFilter(myMeshFilter);
730       else if (theMode == 7)
731         mySelectionMgr->installFilter(myGeomFilter);
732
733       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
734         aViewWindow->SetSelectionMode(ActorSelection);
735     }
736     mySelectionMode = theMode;
737   }
738 }
739
740 //=================================================================================
741 // function : onApply()
742 // purpose  :
743 //=================================================================================
744 bool SMESHGUI_GroupDlg::onApply()
745 {
746   if (mySMESHGUI->isActiveStudyLocked())
747     return false;
748
749   if (myName->text().trimmed().isEmpty())
750     return false;
751
752   if (myGrpTypeId == 0) { // on mesh elements
753     if (!myElements->count())
754       return false;
755
756     mySelectionMgr->clearSelected();
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_Group_var aGroup =
1567         SMESH::IObjectToInterface<SMESH::SMESH_Group>(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 }