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