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