Salome HOME
Copyright update 2020
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_GroupDlg.cxx
1 // Copyright (C) 2007-2020  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, or (at your option) any later version.
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 #include <SMESH_LogicalFilter.hxx>
43
44 // SALOME GEOM includes
45 #include <GEOMBase.h>
46 #include <GEOM_SelectionFilter.h>
47 #include <GEOM_wrap.hxx>
48
49 // SALOME GUI includes
50 #include <QtxColorButton.h>
51
52 #include <SUIT_Desktop.h>
53 #include <SUIT_ResourceMgr.h>
54 #include <SUIT_Session.h>
55 #include <SUIT_MessageBox.h>
56 #include <SUIT_OverrideCursor.h>
57
58 #include <SalomeApp_Tools.h>
59 #include <SalomeApp_Application.h>
60 #include <SalomeApp_Study.h>
61 #include <LightApp_SelectionMgr.h>
62
63 #include <SALOME_ListIO.hxx>
64
65 #include <SVTK_ViewWindow.h>
66
67 #include <VTKViewer_Algorithm.h>
68
69 // SALOME KERNEL includes
70 #include <SALOMEDSClient_Study.hxx>
71
72 // VTK Includes
73 #include <vtkRenderer.h>
74 #include <vtkActorCollection.h>
75
76 // OCCT includes
77 #include <TColStd_MapOfInteger.hxx>
78
79 // Qt includes
80 #include <QButtonGroup>
81 #include <QGroupBox>
82 #include <QLabel>
83 #include <QLineEdit>
84 #include <QPushButton>
85 #include <QToolButton>
86 #include <QRadioButton>
87 #include <QCheckBox>
88 #include <QGridLayout>
89 #include <QHBoxLayout>
90 #include <QVBoxLayout>
91 #include <QListWidget>
92 #include <QStackedWidget>
93 #include <QKeyEvent>
94 #include <QMenu>
95
96 // STL includes
97 #include <vector>
98 #include <algorithm>
99 #include <set>
100
101 #define SPACING 6
102 #define MARGIN  11
103
104 enum grpSelectionMode {
105   grpNoSelection       = -1,
106   grpNodeSelection     = 0,
107   grp0DSelection       = 1,
108   grpBallSelection     = 2,
109   grpEdgeSelection     = 3,
110   grpFaceSelection     = 4,
111   grpVolumeSelection   = 5,
112   grpSubMeshSelection  = 6,
113   grpGroupSelection    = 7,
114   grpMeshSelection     = 8,
115   grpGeomSelection     = 9,
116   grpAllSelection      = 10,
117 };
118
119 //=================================================================================
120 // function : SMESHGUI_GroupDlg()
121 // purpose  :
122 //=================================================================================
123 SMESHGUI_GroupDlg::SMESHGUI_GroupDlg( SMESHGUI* theModule,
124                                       SMESH::SMESH_Mesh_ptr theMesh )
125   : QDialog( SMESH::GetDesktop( theModule ) ),
126     mySMESHGUI( theModule ),
127     mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ), myStoredShownEntity(0),
128     mySelector( SMESH::GetViewWindow( theModule )->GetSelector() ),
129     myIsBusy( false ),
130     myNameChanged( false ),
131     myNbChangesOfContents(0),
132     myIsApplyAndClose( false )
133 {
134   initDialog( true );
135   if ( !theMesh->_is_nil() )
136     init( theMesh );
137   else
138   {
139     mySelectSubMesh->setEnabled( false );
140     mySelectGroup->setEnabled( false );
141     myGeomGroupBtn->setEnabled( false );
142     myGeomGroupLine->setEnabled( false );
143   }
144 }
145
146 //=================================================================================
147 // function : SMESHGUI_GroupDlg()
148 // purpose  :
149 //=================================================================================
150 SMESHGUI_GroupDlg::SMESHGUI_GroupDlg( SMESHGUI* theModule,
151                                       SMESH::SMESH_GroupBase_ptr theGroup,
152                                       const bool theIsConvert )
153   : QDialog( SMESH::GetDesktop( theModule ) ),
154     mySMESHGUI( theModule ),
155     mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ), myStoredShownEntity(0),
156     mySelector( SMESH::GetViewWindow( theModule )->GetSelector() ),
157     myIsBusy( false ),
158     myNameChanged( false ),
159     myNbChangesOfContents(0) // just not to use uninitialized variable
160 {
161   initDialog( false );
162   if ( !theGroup->_is_nil() )
163     init( theGroup, theIsConvert );
164   else
165   {
166     mySelectSubMesh->setEnabled( false );
167     mySelectGroup->setEnabled( false );
168
169     myCurrentLineEdit = myMeshGroupLine;
170     setSelectionMode( grpGroupSelection );
171   }
172 }
173
174 //=================================================================================
175 // function : SMESHGUI_GroupDlg()
176 // purpose  :
177 //=================================================================================
178 void SMESHGUI_GroupDlg::initDialog( bool create)
179 {
180   setModal( false );
181   setAttribute( Qt::WA_DeleteOnClose, true );
182
183   myFilterDlg = 0;
184   myCreate = create;
185   myCurrentLineEdit = 0;
186
187   myShapeByMeshOp = 0;
188   myGeomPopup = 0;
189   myGeomObjects = new GEOM::ListOfGO();
190   myGeomObjects->length( 0 );
191
192   QPixmap image0( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH", tr( "ICON_SELECT" ) ) );
193
194   setWindowTitle( create ? tr( "SMESH_CREATE_GROUP_TITLE" ) : tr( "SMESH_EDIT_GROUP_TITLE" ) );
195   myHelpFileName = create ? "creating_groups.html" : "editing_groups.html";
196
197   setSizeGripEnabled( true);
198
199   QGridLayout* aMainLayout = new QGridLayout( this );
200   aMainLayout->setMargin( MARGIN );
201   aMainLayout->setSpacing( SPACING );
202
203   /***************************************************************/
204   QLabel* meshGroupLab = new QLabel( create ? tr( "SMESH_MESH" ) : tr( "SMESH_GROUP" ), this );
205   myMeshGroupBtn = new QPushButton( this );
206   myMeshGroupBtn->setIcon( image0 );
207   myMeshGroupLine = new QLineEdit( this );
208   myMeshGroupLine->setReadOnly( true );
209
210   /***************************************************************/
211   QGroupBox* aTypeBox = new QGroupBox( tr( "SMESH_ELEMENTS_TYPE" ), this );
212   myTypeGroup = new QButtonGroup( this );
213   QHBoxLayout* aTypeBoxLayout = new QHBoxLayout( aTypeBox );
214   aTypeBoxLayout->setMargin( MARGIN );
215   aTypeBoxLayout->setSpacing( SPACING );
216
217   QStringList types;
218   types.append( tr( "MESH_NODE" ) );
219   types.append( tr( "SMESH_ELEM0D" ) );
220   types.append( tr( "SMESH_BALL_ELEM" ) );
221   types.append( tr( "SMESH_EDGE" ) );
222   types.append( tr( "SMESH_FACE" ) );
223   types.append( tr( "SMESH_VOLUME" ) );
224   QRadioButton* rb;
225   for ( int i = 0; i < types.count(); i++ )
226   {
227     rb = new QRadioButton( types[i], aTypeBox );
228     myTypeGroup->addButton( rb, i );
229     aTypeBoxLayout->addWidget( rb );
230   }
231   aTypeBox->setEnabled( create );
232   myTypeId = -1;
233
234   /***************************************************************/
235   QLabel* aName = new QLabel( tr( "SMESH_NAME" ), this );
236   aName->setMinimumWidth( 50 );
237   myName = new QLineEdit( this );
238
239   /***************************************************************/
240   QGroupBox* aGrpTypeBox = new QGroupBox( tr( "SMESH_GROUP_TYPE" ), this );
241   myGrpTypeGroup = new QButtonGroup( this );
242   QHBoxLayout* aGrpTypeBoxLayout = new QHBoxLayout( aGrpTypeBox );
243   aGrpTypeBoxLayout->setMargin( MARGIN );
244   aGrpTypeBoxLayout->setSpacing( SPACING );
245
246   QRadioButton* rb1 = new QRadioButton( tr( "SMESH_GROUP_STANDALONE" ), aGrpTypeBox );
247   QRadioButton* rb2 = new QRadioButton( tr( "SMESH_GROUP_GEOMETRY" ),   aGrpTypeBox );
248   QRadioButton* rb3 = new QRadioButton( tr( "SMESH_GROUP_FILTER" ),     aGrpTypeBox );
249   myGrpTypeGroup->addButton( rb1, 0 );
250   myGrpTypeGroup->addButton( rb2, 1 );
251   myGrpTypeGroup->addButton( rb3, 2 );
252   aGrpTypeBoxLayout->addWidget( rb1 );
253   aGrpTypeBoxLayout->addWidget( rb2 );
254   aGrpTypeBoxLayout->addWidget( rb3 );
255   aGrpTypeBox->setEnabled( create );
256   myGrpTypeId = -1;
257
258   /***************************************************************/
259   myWGStack = new QStackedWidget( this );
260   QWidget* wg1 = new QWidget( myWGStack );
261   QWidget* wg2 = new QWidget( myWGStack );
262   QWidget* wg3 = new QWidget( myWGStack );
263
264   /***************************************************************/
265   QGroupBox* aContentBox         = new QGroupBox( tr( "SMESH_CONTENT" ), wg1 );
266   QGridLayout* aContentBoxLayout = new QGridLayout( aContentBox );
267   aContentBoxLayout->setMargin( MARGIN );
268   aContentBoxLayout->setSpacing( SPACING );
269
270   mySelectAll       = new QCheckBox( tr( "SELECT_ALL" ), aContentBox );
271   myAllowElemsModif = new QCheckBox( tr( "ALLOW_ELEM_LIST_MODIF" ), aContentBox );
272
273   myElementsLab = new QLabel( tr( "SMESH_ID_ELEMENTS" ), aContentBox );
274   myElements    = new QListWidget( aContentBox );
275   myElements->setSelectionMode( QListWidget::ExtendedSelection );
276
277   myFilterBtn = new QPushButton( tr( "SMESH_BUT_FILTER" ), aContentBox );
278   myAddBtn    = new QPushButton( tr( "SMESH_BUT_ADD" ), aContentBox );
279   myRemoveBtn = new QPushButton( tr( "SMESH_BUT_REMOVE" ), aContentBox );
280   mySortBtn   = new QPushButton( tr( "SMESH_BUT_SORT" ), aContentBox );
281
282   aContentBoxLayout->addWidget( mySelectAll,       0, 0 );
283   aContentBoxLayout->addWidget( myAllowElemsModif, 1, 0 );
284   aContentBoxLayout->addWidget( myFilterBtn,       1, 1 );
285   aContentBoxLayout->addWidget( myElementsLab,     2, 0 );
286   aContentBoxLayout->addWidget( myElements,        3, 0, 6, 1 );
287   aContentBoxLayout->addWidget( myAddBtn,          3, 1 );
288   aContentBoxLayout->addWidget( myRemoveBtn,       4, 1 );
289   aContentBoxLayout->addWidget( mySortBtn,         8, 1 );
290
291   aContentBoxLayout->setColumnStretch( 0, 1 );
292   aContentBoxLayout->setRowStretch( 3, 1 );
293   aContentBoxLayout->setRowStretch( 6, 1 );
294
295   /***************************************************************/
296   mySelectBox = new QGroupBox( tr( "SMESH_SELECT_FROM" ), wg1 );
297   QGridLayout* mySelectBoxLayout = new QGridLayout( mySelectBox );
298   mySelectBoxLayout->setMargin( MARGIN );
299   mySelectBoxLayout->setSpacing( SPACING );
300
301   mySelectSubMesh = new QCheckBox( tr( "SMESH_SUBMESH" ), mySelectBox );
302   mySubMeshBtn = new QPushButton( mySelectBox );
303   mySubMeshBtn->setIcon( image0 );
304   mySubMeshLine = new QLineEdit( mySelectBox );
305   mySubMeshLine->setReadOnly( true );
306   onSelectSubMesh( false );
307
308   mySelectGroup = new QCheckBox( tr( "SMESH_GROUP" ), mySelectBox );
309   myGroupBtn = new QPushButton( mySelectBox );
310   myGroupBtn->setIcon( image0 );
311   myGroupLine = new QLineEdit( mySelectBox );
312   myGroupLine->setReadOnly( true );
313   onSelectGroup( false );
314
315   mySelectBoxLayout->addWidget( mySelectSubMesh, 0, 0 );
316   mySelectBoxLayout->addWidget( mySubMeshBtn,    0, 1 );
317   mySelectBoxLayout->addWidget( mySubMeshLine,   0, 2 );
318   mySelectBoxLayout->addWidget( mySelectGroup,   1, 0 );
319   mySelectBoxLayout->addWidget( myGroupBtn,      1, 1 );
320   mySelectBoxLayout->addWidget( myGroupLine,     1, 2 );
321
322   /***************************************************************/
323   QVBoxLayout* wg1Layout = new QVBoxLayout( wg1 );
324   wg1Layout->setMargin( 0 );
325   wg1Layout->setSpacing( SPACING );
326   wg1Layout->addWidget( aContentBox );
327   wg1Layout->addWidget( mySelectBox );
328   wg1Layout->setStretchFactor( aContentBox, 10 );
329
330   /***************************************************************/
331   QLabel* geomObject = new QLabel( tr( "SMESH_OBJECT_GEOM" ), wg2 );
332   myGeomGroupBtn = new QToolButton( wg2 );
333   myGeomGroupBtn->setIcon( image0 );
334   myGeomGroupBtn->setCheckable( true );
335   myGeomGroupLine = new QLineEdit( wg2 );
336   myGeomGroupLine->setReadOnly( true ); //VSR ???
337   onSelectGeomGroup( false );
338
339   myGeomGroupBtn->setEnabled( create );
340   myGeomGroupLine->setEnabled( create );
341
342   /***************************************************************/
343   QGridLayout* wg2Layout = new QGridLayout( wg2 );
344   wg2Layout->setMargin( 0 );
345   wg2Layout->setSpacing( SPACING );
346   wg2Layout->addWidget( geomObject,     0, 0 );
347   wg2Layout->addWidget( myGeomGroupBtn, 0, 1 );
348   wg2Layout->addWidget( myGeomGroupLine,0, 2 );
349   wg2Layout->setRowStretch( 1, 5 );
350
351   /***************************************************************/
352   QPushButton * aFilter2 = new QPushButton( tr( "SMESH_BUT_FILTER" ), wg3 );
353   QGridLayout* wg3Layout = new QGridLayout( wg3 );
354   wg3Layout->setMargin( 0 );
355   wg3Layout->setSpacing( SPACING );
356   wg3Layout->addWidget( aFilter2, 0, 0 );
357   wg3Layout->setRowStretch( 1, 5 );
358
359   /***************************************************************/
360   myWGStack->insertWidget( 0, wg1 );
361   myWGStack->insertWidget( 1, wg2 );
362   myWGStack->insertWidget( 2, wg3 );
363
364   /***************************************************************/
365   QGroupBox* aColorBox = new QGroupBox(tr( "SMESH_SET_COLOR" ), this);
366   QHBoxLayout* aColorBoxLayout = new QHBoxLayout(aColorBox);
367   aColorBoxLayout->setMargin(MARGIN);
368   aColorBoxLayout->setSpacing(SPACING);
369
370   QLabel* aColorLab = new QLabel(tr( "SMESH_CHECK_COLOR" ), aColorBox );
371   myColorBtn = new QtxColorButton(aColorBox);
372   myColorBtn->setSizePolicy( QSizePolicy::MinimumExpanding, 
373                              myColorBtn->sizePolicy().verticalPolicy() );
374
375   aColorBoxLayout->addWidget(aColorLab);
376   aColorBoxLayout->addWidget(myColorBtn);
377
378   /***************************************************************/
379
380   QFrame* aButtons = new QFrame(this);
381   aButtons->setFrameStyle( QFrame::Box | QFrame::Sunken );
382   QHBoxLayout* aBtnLayout = new QHBoxLayout(aButtons);
383   aBtnLayout->setMargin(MARGIN);
384   aBtnLayout->setSpacing(SPACING);
385
386   myOKBtn = new QPushButton(tr( "SMESH_BUT_APPLY_AND_CLOSE" ), aButtons);
387   myOKBtn->setAutoDefault(true);
388   myOKBtn->setDefault(true);
389   myApplyBtn = new QPushButton(tr( "SMESH_BUT_APPLY" ), aButtons);
390   myApplyBtn->setAutoDefault(true);
391   myCloseBtn = new QPushButton(tr( "SMESH_BUT_CLOSE" ), aButtons);
392   myCloseBtn->setAutoDefault(true);
393   myHelpBtn = new QPushButton(tr( "SMESH_BUT_HELP" ), aButtons);
394   myHelpBtn->setAutoDefault(true);
395
396   aBtnLayout->addWidget(myOKBtn);
397   aBtnLayout->addSpacing(10);
398   aBtnLayout->addWidget(myApplyBtn);
399   aBtnLayout->addSpacing(10);
400   aBtnLayout->addStretch();
401   aBtnLayout->addWidget(myCloseBtn);
402   aBtnLayout->addWidget(myHelpBtn);
403
404   /***************************************************************/
405   aMainLayout->addWidget(meshGroupLab,    0, 0);
406   aMainLayout->addWidget(myMeshGroupBtn,  0, 1);
407   aMainLayout->addWidget(myMeshGroupLine, 0, 2);
408   aMainLayout->addWidget(aTypeBox,        1, 0, 1, 3);
409   aMainLayout->addWidget(aName,           2, 0);
410   aMainLayout->addWidget(myName,          2, 2);
411   aMainLayout->addWidget(aGrpTypeBox,     3, 0, 1, 3);
412   aMainLayout->addWidget(myWGStack,       4, 0, 1, 3);
413   aMainLayout->addWidget(aColorBox,       5, 0, 1, 3);
414   aMainLayout->addWidget(aButtons,        6, 0, 1, 3);
415
416   /* signals and slots connections */
417   connect(myMeshGroupBtn,  SIGNAL(clicked()),          this, SLOT(setCurrentSelection()));
418   connect(myGrpTypeGroup,  SIGNAL(buttonClicked(int)), this, SLOT(onGrpTypeChanged(int)));
419   connect(myTypeGroup,     SIGNAL(buttonClicked(int)), this, SLOT(onTypeChanged(int)));
420
421   connect(myName,          SIGNAL(textChanged(const QString&)), this, SLOT(onNameChanged(const QString&)));
422   connect(myElements,      SIGNAL(itemSelectionChanged()),      this, SLOT(onListSelectionChanged()));
423
424   connect(myFilterBtn,     SIGNAL(clicked()),     this, SLOT(setFilters()));
425   connect(aFilter2,        SIGNAL(clicked()),     this, SLOT(setFilters()));
426   connect(mySelectAll,     SIGNAL(toggled(bool)), this, SLOT(onSelectAll()));
427   connect(myAllowElemsModif,SIGNAL(toggled(bool)), this, SLOT(onSelectAll()));
428   connect(myAddBtn,        SIGNAL(clicked()),     this, SLOT(onAdd()));
429   connect(myRemoveBtn,     SIGNAL(clicked()),     this, SLOT(onRemove()));
430   connect(mySortBtn,       SIGNAL(clicked()),     this, SLOT(onSort()));
431
432   connect(mySelectSubMesh, SIGNAL(toggled(bool)), this, SLOT(onSelectSubMesh(bool)));
433   connect(mySelectGroup,   SIGNAL(toggled(bool)), this, SLOT(onSelectGroup(bool)));
434   connect(mySubMeshBtn,    SIGNAL(clicked()),     this, SLOT(setCurrentSelection()));
435   connect(myGroupBtn,      SIGNAL(clicked()),     this, SLOT(setCurrentSelection()));
436   connect(myGeomGroupBtn,  SIGNAL(toggled(bool)), this, SLOT(onGeomSelectionButton(bool)));
437
438   connect(myColorBtn,      SIGNAL(changed( QColor )),  this, SLOT(onColorChanged( QColor )));
439
440   connect(myOKBtn,         SIGNAL(clicked()), this, SLOT(onOK()));
441   connect(myApplyBtn,      SIGNAL(clicked()), this, SLOT(onApply()));
442   connect(myCloseBtn,      SIGNAL(clicked()), this, SLOT(reject()));
443   connect(myHelpBtn,       SIGNAL(clicked()), this, SLOT(onHelp()));
444
445   /* Init selection */
446   mySMESHGUI->SetActiveDialogBox(this);
447
448   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( mySMESHGUI->application()->activeStudy() );
449   mySelectionMode = grpNoSelection;
450
451   myMeshFilter    = new SMESH_TypeFilter(SMESH::MESH);
452   mySubMeshFilter = new SMESH_LogicalFilter(QList<SUIT_SelectionFilter*>(),
453                                             SMESH_LogicalFilter::LO_OR,
454                                             /*takeOwnership=*/true);
455   myGroupFilter   = new SMESH_LogicalFilter(QList<SUIT_SelectionFilter*>(),
456                                             SMESH_LogicalFilter::LO_OR,
457                                             /*takeOwnership=*/true);
458   myGeomFilter    = new GEOM_SelectionFilter( aStudy, true );
459
460   connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(onDeactivate()));
461   connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()),        this, SLOT(reject()));
462   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()),  this, SLOT(onObjectSelectionChanged()));
463   connect(mySMESHGUI, SIGNAL(SignalVisibilityChanged()),      this, SLOT(onVisibilityChanged()));
464   connect(mySMESHGUI, SIGNAL(SignalActivatedViewManager()),   this, SLOT(onOpenView()));
465   connect(mySMESHGUI, SIGNAL(SignalCloseView()),              this, SLOT(onCloseView()));
466   rb1->setChecked(true); // VSR !!!
467   onGrpTypeChanged(0); // VSR!!!
468
469   if (myMesh->_is_nil() )
470     myTypeGroup->button(0)->setChecked(true);
471
472   onSelectAll(); //updateButtons();
473 }
474
475 //=================================================================================
476 // function : ~SMESHGUI_GroupDlg()
477 // purpose  : Destroys the object and frees any allocated resources
478 //=================================================================================
479 SMESHGUI_GroupDlg::~SMESHGUI_GroupDlg()
480 {
481   // no need to delete child widgets, Qt does it all for us
482   if ( myFilterDlg != 0 ) {
483     myFilterDlg->setParent( 0 );
484     delete myFilterDlg;
485   }
486   if ( myMeshFilter )    delete myMeshFilter;
487   if ( mySubMeshFilter ) delete mySubMeshFilter;
488   if ( myGroupFilter )   delete myGroupFilter;
489   if ( myGeomFilter )    delete myGeomFilter;
490 }
491
492 //=================================================================================
493 // function : GetDefaultName()
494 // purpose  : Get the Group Name if Create new Group
495 //=================================================================================
496 QString SMESHGUI_GroupDlg::GetDefaultName(const QString& theOperation)
497 {
498   QString aName = "";
499
500   // collect all object names of SMESH component
501   _PTR(Study) aStudy = SMESH::getStudy();
502
503   std::set<std::string> aSet;
504   _PTR(SComponent) aMeshCompo (aStudy->FindComponent( "SMESH" ));
505   if (aMeshCompo) {
506     _PTR(ChildIterator) it (aStudy->NewChildIterator(aMeshCompo));
507     _PTR(SObject) obj;
508     for (it->InitEx(true); it->More(); it->Next()) {
509       obj = it->Value();
510       aSet.insert(obj->GetName());
511     }
512   }
513
514   // build a unique name
515   int aNumber = 0;
516   bool isUnique = false;
517   while (!isUnique) {
518     aName = theOperation + "_" + QString::number(++aNumber);
519     isUnique = (aSet.count(std::string(aName.toUtf8().constData())) == 0);
520   }
521
522   return aName;
523 }
524
525 void  SMESHGUI_GroupDlg::setDefaultName() const
526 {
527   QString aResName;
528   int i=1;
529   QString aPrefix ="Group_";
530   _PTR(SObject) anObj;
531   do
532   {
533     aResName = aPrefix + QString::number( i++ );
534     anObj = SMESH::getStudy()->FindObject( aResName.toUtf8().constData() );
535   }
536   while ( anObj );
537   myName->setText(aResName); 
538 }
539
540 //=================================================================================
541 // function : Init()
542 // purpose  :
543 //=================================================================================
544 void SMESHGUI_GroupDlg::init (SMESH::SMESH_Mesh_ptr theMesh)
545 {
546   mySelectionMgr->installFilter(myMeshFilter);
547
548   /* init data from current selection */
549   restoreShowEntityMode();
550   myMesh = SMESH::SMESH_Mesh::_duplicate(theMesh);
551   setShowEntityMode();
552   myGroup = SMESH::SMESH_Group::_nil();
553   myGroupOnGeom = SMESH::SMESH_GroupOnGeom::_nil();
554   myGroupOnFilter = SMESH::SMESH_GroupOnFilter::_nil();
555
556   // NPAL19389: create a group with a selection in another group
557   // set actor of myMesh, if it is visible, else try
558   // any visible actor of group or submesh of myMesh
559   SetAppropriateActor();
560
561   setDefaultGroupColor();
562   setDefaultName();
563
564
565   SALOME_ListIO aList;
566   mySelectionMgr->selectedObjects( aList );
567   if( !aList.IsEmpty() )
568   {
569     QString aName = aList.First()->getName();
570     myMeshGroupLine->setText(aName);//??????
571     myMeshGroupLine->home( false );
572   }
573
574   myCurrentLineEdit = 0;
575
576   myTypeGroup->button(0)->setChecked(true);
577   onTypeChanged(0);
578 }
579
580 //=================================================================================
581 // function : Init()
582 // purpose  :
583 //=================================================================================
584 void SMESHGUI_GroupDlg::init (SMESH::SMESH_GroupBase_ptr theGroup,
585                               const bool                 theIsConvert)
586 {
587   restoreShowEntityMode();
588   myMesh = theGroup->GetMesh();
589   setShowEntityMode();
590
591   myNameChanged = true;
592   myName->blockSignals(true);
593   myName->setText(SMESH::fromUtf8(theGroup->GetName()));
594   myName->blockSignals(false);
595   myName->home(false);
596
597   SALOMEDS::Color aColor = theGroup->GetColor();
598   setGroupColor( aColor );
599
600   myMeshGroupLine->setText(theGroup->GetName());
601
602   int aType = 0;
603   switch(theGroup->GetType()) {
604   case SMESH::NODE:   aType = grpNodeSelection;   break;
605   case SMESH::ELEM0D: aType = grp0DSelection;     break;
606   case SMESH::BALL:   aType = grpBallSelection;   break;
607   case SMESH::EDGE:   aType = grpEdgeSelection;   break;
608   case SMESH::FACE:   aType = grpFaceSelection;   break;
609   case SMESH::VOLUME: aType = grpVolumeSelection; break;
610   case SMESH::ALL:
611   case SMESH::NB_ELEMENT_TYPES: break;
612   }
613   myTypeGroup->button(aType)->setChecked(true);
614
615   myGroup         = SMESH::SMESH_Group::_narrow( theGroup );
616   myGroupOnGeom   = SMESH::SMESH_GroupOnGeom::_narrow( theGroup );
617   myGroupOnFilter = SMESH::SMESH_GroupOnFilter::_narrow( theGroup );
618   myFilter        = SMESH::Filter::_nil();
619
620   if (myGroup->_is_nil() && myGroupOnGeom->_is_nil() && myGroupOnFilter->_is_nil() )
621     return;
622
623   // NPAL19389: create a group with a selection in another group
624   // set actor of myMesh, if it is visible, else set
625   // actor of theGroup, if it is visible, else try
626   // any visible actor of group or submesh of myMesh
627   // commented, because an attempt to set selection on not displayed cells leads to error
628   SetAppropriateActor();
629
630   /*  SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh);
631   if ( !anActor )
632     anActor = SMESH::FindActorByObject(theGroup);
633   SMESH::SetPickable(anActor);*/
634
635   int grpType = (!myGroup->_is_nil() ? 0 : (theIsConvert ? 0 : myGroupOnGeom->_is_nil() ? 2 : 1));
636   myGrpTypeGroup->button(grpType)->setChecked(true);
637   onGrpTypeChanged(grpType);
638
639   myTypeId = aType;
640   if ( grpType == 0 ) { // standalone group
641     myCurrentLineEdit = 0;
642     myElements->clear();
643     myAllowElemsModif->setChecked( true );
644
645     setSelectionMode(aType);
646
647     setShowEntityMode(); // depends on myTypeId
648
649     myIdList.clear();
650     if (!theGroup->IsEmpty()) {
651       SMESH::long_array_var anElements = theGroup->GetListOfID();
652       int k = anElements->length();
653       for (int i = 0; i < k; i++) {
654         myIdList.append(anElements[i]);
655         myElements->addItem(QString::number(anElements[i]));
656       }
657       myElements->selectAll();
658     }
659   }
660   else if ( grpType == 1 ) // group on geom
661   {
662     QString aShapeName( "" );
663     _PTR(Study) aStudy = SMESH::getStudy();
664     GEOM::GEOM_Object_var aGroupShape = myGroupOnGeom->GetShape();
665     if (!aGroupShape->_is_nil())
666     {
667       _PTR(SObject) aGroupShapeSO = aStudy->FindObjectID(aGroupShape->GetStudyEntry());
668       if ( aGroupShapeSO )
669         aShapeName = aGroupShapeSO->GetName().c_str();
670     }
671     myGeomGroupLine->setText( aShapeName );
672   }
673   else // group on filter
674   {
675     myFilter = myGroupOnFilter->GetFilter();
676     if ( !myFilter->_is_nil() ) {
677       SMESH::Predicate_var perdicate = myFilter->GetPredicate();
678       if ( perdicate->_is_nil() )
679         myFilter = SMESH::Filter::_nil();
680     }
681   }
682
683   if ( grpType != 0 )
684   {
685     myNameChanged = true;
686     myName->blockSignals(true);
687     myName->setText(theGroup->GetName());
688     myName->blockSignals(false);
689   }
690
691   onSelectAll(); //updateButtons();
692 }
693
694 //=================================================================================
695 // function : updateButtons()
696 // purpose  :
697 //=================================================================================
698 void SMESHGUI_GroupDlg::updateButtons()
699 {
700   bool enable = !myName->text().trimmed().isEmpty();
701   if ( enable )
702   {
703     if (myGrpTypeId == 0) { // standalone
704       if ( !mySelectAll->isChecked() )
705       {
706         if ( myAllowElemsModif->isChecked() )
707         {
708           enable = ( myElements->count() > 0 );
709         }
710         else if ((enable = !myFilter->_is_nil() ))
711         {
712           SMESH::array_of_ElementType_var types = myFilter->GetTypes();
713           enable = types->length();
714         }
715       }
716       enable = enable && (!myGroup->_is_nil() || !myMesh->_is_nil());
717     }
718     else if (myGrpTypeId == 1) // on geom
719     {
720       if (CORBA::is_nil(myGroupOnGeom)) // creation mode
721         enable = ( myGeomObjects->length() > 0 && !myMesh->_is_nil() );
722     }
723     else if (myGrpTypeId == 2) // on filter
724     {
725       if (( enable = !myFilter->_is_nil() ))
726         if (CORBA::is_nil(myGroupOnFilter) )  // creation mode
727           enable = !myMesh->_is_nil();
728     }
729   }
730
731   bool meshHasGeom = ( myMesh->_is_nil() || myMesh->HasShapeToMesh() );
732   if ( myGrpTypeId != 1 )
733   {
734     myGrpTypeGroup->button(1)->setEnabled( meshHasGeom );
735   }
736   else
737   {
738     myGeomGroupBtn->setEnabled( meshHasGeom );
739     myGeomGroupLine->setEnabled( meshHasGeom );
740   }
741
742   myOKBtn->setEnabled(enable);
743   myApplyBtn->setEnabled(enable);
744 }
745
746 //=================================================================================
747 // function : onNameChanged()
748 // purpose  :
749 //=================================================================================
750 void SMESHGUI_GroupDlg::onNameChanged (const QString& text)
751 {
752   myOldName = myName->text();
753   updateButtons();
754   myNameChanged = !myName->text().trimmed().isEmpty();
755 }
756
757 //=================================================================================
758 // function : onTypeChanged()
759 // purpose  : Group elements type radio button management
760 //=================================================================================
761 void SMESHGUI_GroupDlg::onTypeChanged (int id)
762 {
763   if (myTypeId != id) {
764     myElements->clear();
765     myTypeId = id;
766     if ( myGrpTypeId == 0 && myCurrentLineEdit == 0)
767       setSelectionMode(id);
768     else
769       setSelectionMode( mySelectionMode++ ); // update according to mySelectionMode
770
771     onObjectSelectionChanged();
772     setShowEntityMode();
773   }
774 }
775
776 //=================================================================================
777 // function : onGrpTypeChanged()
778 // purpose  : Group type radio button management
779 //=================================================================================
780 void SMESHGUI_GroupDlg::onGrpTypeChanged (int id)
781 {
782   if (myGrpTypeId != id) {
783     myGrpTypeId = id;
784     myWGStack->setCurrentIndex( id );
785     myName->blockSignals(true);
786     myName->setText(myOldName);
787     myName->blockSignals(false);
788     onSelectGeomGroup(id != 0);
789   }
790   updateButtons();
791 }
792
793 //=================================================================================
794 // function : onColorChanged()
795 // purpose  : Color button management
796 //=================================================================================
797 void SMESHGUI_GroupDlg::onColorChanged(QColor theColor)
798 {
799   updateButtons();
800 }
801
802 //=================================================================================
803 // function : setSelectionMode()
804 // purpose  : Radio button management
805 //=================================================================================
806 void SMESHGUI_GroupDlg::setSelectionMode (int theMode)
807 {
808   // PAL7314
809   if (myMesh->_is_nil())
810     return;
811   SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI );
812   bool isSelectAll = mySelectAll->isChecked() || !myAllowElemsModif->isChecked() || myGrpTypeId != 0;
813   if (mySelectionMode != theMode) {
814     // [PAL10408] mySelectionMgr->clearSelected();
815     mySelectionMgr->clearFilters();
816     SMESH::RemoveFilters();
817
818     if (myActorsList.count() > 0)
819       for (QListIterator<SMESH_Actor*> it( myActorsList ); it.hasNext(); )
820         it.next()->SetPointRepresentation(false);
821     else
822       SMESH::SetPointRepresentation(false);
823
824     switch (theMode) {
825     case grpNodeSelection:
826       if ( myGrpTypeId == 0 ) // standalone
827       {
828         if (myActorsList.count() > 0)
829           for (QListIterator<SMESH_Actor*> it( myActorsList ); it.hasNext(); )
830             it.next()->SetPointRepresentation(true);
831         else
832           SMESH::SetPointRepresentation(true);
833       }
834       if ( aViewWindow ) aViewWindow->SetSelectionMode(isSelectAll ? ActorSelection : NodeSelection);
835       break;
836     case grpEdgeSelection:
837       if ( aViewWindow ) aViewWindow->SetSelectionMode(isSelectAll ? ActorSelection : EdgeSelection);
838       break;
839     case grpBallSelection:
840       if ( aViewWindow ) aViewWindow->SetSelectionMode(isSelectAll ? ActorSelection : BallSelection);
841       break;
842     case grp0DSelection:
843       if ( aViewWindow ) aViewWindow->SetSelectionMode(isSelectAll ? ActorSelection : Elem0DSelection);
844       break;
845     case grpFaceSelection:
846       if ( aViewWindow ) aViewWindow->SetSelectionMode(isSelectAll ? ActorSelection : FaceSelection);
847       break;
848     case grpVolumeSelection:
849       if ( aViewWindow ) aViewWindow->SetSelectionMode(isSelectAll ? ActorSelection : VolumeSelection);
850       break;
851     case grpSubMeshSelection: {
852
853       SMESH_TypeFilter* f = 0;
854       switch (myTypeId) {
855       case grpNodeSelection:   f = new SMESH_TypeFilter(SMESH::SUBMESH); break;
856       case grpEdgeSelection:   f = new SMESH_TypeFilter(SMESH::SUBMESH_EDGE); break;
857       case grpFaceSelection:   f = new SMESH_TypeFilter(SMESH::SUBMESH_FACE); break;
858       case grpVolumeSelection: f = new SMESH_TypeFilter(SMESH::SUBMESH_SOLID); break;
859       default:                 f = new SMESH_TypeFilter(SMESH::SUBMESH);
860       }
861       QList<SUIT_SelectionFilter*> filtList;
862       filtList.append( f );
863       filtList.append( new SMESH_TypeFilter(SMESH::SUBMESH_COMPOUND));
864       mySubMeshFilter->setFilters( filtList );
865
866       mySelectionMgr->installFilter( mySubMeshFilter );
867
868       if ( aViewWindow ) aViewWindow->SetSelectionMode(ActorSelection);
869       break;
870     }
871     case grpGroupSelection: {
872
873       SMESH_TypeFilter* f = 0;
874       switch (myTypeId) {
875       case grpNodeSelection:   f = new SMESH_TypeFilter(SMESH::GROUP_NODE);   break;
876       case grp0DSelection:     f = new SMESH_TypeFilter(SMESH::GROUP_0D);     break;
877       case grpBallSelection:   f = new SMESH_TypeFilter(SMESH::GROUP_BALL);   break;
878       case grpEdgeSelection:   f = new SMESH_TypeFilter(SMESH::GROUP_EDGE);   break;
879       case grpFaceSelection:   f = new SMESH_TypeFilter(SMESH::GROUP_FACE);   break;
880       case grpVolumeSelection: f = new SMESH_TypeFilter(SMESH::GROUP_VOLUME); break;
881       default:                 f = new SMESH_TypeFilter(SMESH::GROUP);
882       }
883       QList<SUIT_SelectionFilter*> filtList;
884       filtList.append( f );
885       myGroupFilter->setFilters( filtList );
886
887       mySelectionMgr->installFilter(myGroupFilter);
888       if ( aViewWindow ) aViewWindow->SetSelectionMode(ActorSelection);
889       break;
890     }
891     case grpMeshSelection:
892       mySelectionMgr->installFilter(myMeshFilter);
893       if ( aViewWindow ) aViewWindow->SetSelectionMode(ActorSelection);
894       break;
895     case grpGeomSelection:
896       mySelectionMgr->installFilter(myGeomFilter);
897       if ( aViewWindow ) aViewWindow->SetSelectionMode(ActorSelection);
898       break;
899     default:
900       if ( aViewWindow ) aViewWindow->SetSelectionMode(ActorSelection);
901       break;
902     }
903     if ( aViewWindow ) aViewWindow->Repaint();
904     mySelectionMode = theMode;
905   }
906 }
907
908 //=================================================================================
909 // function : onApply()
910 // purpose  :
911 //=================================================================================
912 bool SMESHGUI_GroupDlg::onApply()
913 {
914   if (SMESHGUI::isStudyLocked())
915     return false;
916
917   if (myName->text().trimmed().isEmpty())
918     return false;
919
920   SMESH::ElementType aType = SMESH::ALL;
921   switch (myTypeId) {
922   case grpNodeSelection:   aType = SMESH::NODE;   break;
923   case grp0DSelection:     aType = SMESH::ELEM0D; break;
924   case grpBallSelection:   aType = SMESH::BALL;   break;
925   case grpEdgeSelection:   aType = SMESH::EDGE;   break;
926   case grpFaceSelection:   aType = SMESH::FACE;   break;
927   case grpVolumeSelection: aType = SMESH::VOLUME; break;
928   }
929
930   bool anIsOk = false;
931   QStringList anEntryList;
932
933   SMESH::SMESH_GroupBase_var resultGroup;
934   bool isCreation = false, isConversion = false;
935     
936   SUIT_OverrideCursor wc;
937
938   if (myGrpTypeId == 0)  // standalone
939   {
940     if (!mySelectAll->isChecked() && !myElements->count() && myAllowElemsModif->isChecked())
941       return false;
942
943     mySelectionMgr->clearSelected();
944
945     if (myGroup->_is_nil()) { // creation or conversion
946       // check if group on geometry is not null
947       if (!myGroupOnGeom->_is_nil() || !myGroupOnFilter->_is_nil()) {
948         if (myMesh->_is_nil())
949           return false;
950         if ( myGroupOnGeom->_is_nil() )
951           myGroup = myMesh->ConvertToStandalone( myGroupOnFilter );
952         else
953           myGroup = myMesh->ConvertToStandalone( myGroupOnGeom );
954
955         myGroupOnGeom   = SMESH::SMESH_GroupOnGeom::_nil();
956         myGroupOnFilter = SMESH::SMESH_GroupOnFilter::_nil();
957         isConversion    = true;
958       }
959     }
960
961     if (myGroup->_is_nil()) { // creation
962       if (myMesh->_is_nil())
963         return false;
964
965       myGroup = SMESH::AddGroup(myMesh, aType, myName->text());
966
967       resultGroup = SMESH::SMESH_GroupBase::_narrow( myGroup );
968       isCreation = true;
969
970       if ( mySelectAll->isChecked() ) {
971         // select all
972         myGroup->AddFrom(myMesh.in());
973       }
974       else {
975         // select manually
976
977         if ( !myFilter->_is_nil() &&
978              ( myNbChangesOfContents == 1 || !myAllowElemsModif->isChecked()))
979         {
980           myGroup->AddFrom( myFilter );
981         }
982         else
983         {
984           SMESH::long_array_var anIdList = new SMESH::long_array;
985           int i, k = myElements->count();
986           anIdList->length(k);
987           for (i = 0; i < k; i++) {
988             anIdList[i] = myElements->item(i)->text().toInt();
989           }
990           myGroup->Add(anIdList.inout());
991         }
992       }
993
994
995     } else { // edition
996
997       resultGroup = SMESH::SMESH_GroupBase::_narrow( myGroup );
998       isCreation = false;
999
1000       if ( mySelectAll->isChecked() ) {
1001         // select all
1002         myGroup->Clear();
1003         myGroup->AddFrom(myMesh.in());
1004       }
1005       else {
1006         QList<int> aAddList;
1007         
1008         int i, total = myElements->count();
1009         for (i = 0; i < total; i++) {
1010           int anId = myElements->item(i)->text().toInt();
1011           int idx = myIdList.indexOf(anId);
1012           if ( idx == -1 )
1013             aAddList.append(anId);
1014           else
1015             myIdList.removeAt(idx);
1016         }
1017         if (!aAddList.empty()) {
1018           SMESH::long_array_var anIdList = new SMESH::long_array;
1019           int added = aAddList.count();
1020           anIdList->length(added);
1021           for (i = 0; i < added; i++)
1022             anIdList[i] = aAddList[i];
1023           myGroup->Add(anIdList.inout());
1024         }
1025         if (!myIdList.empty()) {
1026           SMESH::long_array_var anIdList = new SMESH::long_array;
1027           int removed = myIdList.count();
1028           anIdList->length(removed);
1029           for (i = 0; i < removed; i++)
1030             anIdList[i] = myIdList[i];
1031           myGroup->Remove(anIdList.inout());
1032         }
1033         /* init for next operation */
1034         myIdList.clear();
1035         for (i = 0; i < total; i++) {
1036           myIdList.append(myElements->item(i)->text().toInt());
1037         }
1038       }
1039     }
1040
1041     anIsOk = true;
1042   }
1043   else if (myGrpTypeId == 1) // on geom object
1044   {
1045     if (CORBA::is_nil(myGroupOnGeom)) { // creation
1046       if (myMesh->_is_nil() || !myGeomObjects->length())
1047         return false;
1048
1049       _PTR(Study) aStudy = SMESH::getStudy();
1050
1051       if (myGeomObjects->length() == 1) {
1052         myGroupOnGeom = myMesh->CreateGroupFromGEOM(aType,
1053                                                     myName->text().toUtf8(),
1054                                                     myGeomObjects[0]);
1055       }
1056       else {
1057         SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1058         if ( aSMESHGen->_is_nil() || myGeomObjects->length() == 0 )
1059           return false;
1060
1061         // create a geometry group
1062         GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen( myGeomObjects[0] );
1063         if (geomGen->_is_nil())
1064           return false;
1065
1066         GEOM::GEOM_IGroupOperations_wrap op = geomGen->GetIGroupOperations();
1067         if (op->_is_nil())
1068           return false;
1069
1070         // check and add all selected GEOM objects: they must be
1071         // a sub-shapes of the main GEOM and must be of one type
1072         TopAbs_ShapeEnum aGroupType = TopAbs_SHAPE;
1073         for ( CORBA::ULong i =0; i < myGeomObjects->length(); i++)
1074         {
1075           TopAbs_ShapeEnum aSubShapeType = (TopAbs_ShapeEnum)myGeomObjects[i]->GetShapeType();
1076           if ( i == 0 )
1077             aGroupType = aSubShapeType;
1078           else if ( aSubShapeType != aGroupType ) {
1079             aGroupType = TopAbs_SHAPE;
1080             break;
1081           }
1082         }
1083
1084         GEOM::GEOM_Object_var  aMeshShape = myMesh->GetShapeToMesh();
1085         GEOM::GEOM_Object_wrap aGroupVar = op->CreateGroup(aMeshShape, aGroupType);
1086         if ( aGroupVar->_is_nil() )
1087           return false;
1088         op->UnionList(aGroupVar, myGeomObjects);
1089
1090         if (op->IsDone()) {
1091           // publish the GEOM group in study
1092           QString aNewGeomGroupName ( "Auto_group_for_" );
1093           aNewGeomGroupName += myName->text();
1094           SALOMEDS::SObject_var aNewGroupSO =
1095             geomGen->AddInStudy(aGroupVar,
1096                                 aNewGeomGroupName.toUtf8(), aMeshShape);
1097         }
1098
1099         myGroupOnGeom = myMesh->CreateGroupFromGEOM(aType,
1100                                                     myName->text().toUtf8(),
1101                                                     aGroupVar);
1102       }
1103       resultGroup = SMESH::SMESH_GroupBase::_narrow( myGroupOnGeom );
1104       isCreation = true;
1105
1106     }
1107     else { // edition
1108
1109       resultGroup = SMESH::SMESH_GroupBase::_narrow( myGroupOnGeom );
1110       isCreation = false;
1111     }
1112     anIsOk = true;
1113   }
1114   if (myGrpTypeId == 2) // group on filter
1115   {
1116     if ( myFilter->_is_nil() ) return false;
1117
1118     if (CORBA::is_nil(myGroupOnFilter)) // creation
1119     {
1120       if (myMesh->_is_nil())
1121         return false;
1122
1123       myGroupOnFilter = myMesh->CreateGroupFromFilter(aType,
1124                                                       myName->text().toUtf8(),
1125                                                       myFilter);
1126       resultGroup = SMESH::SMESH_GroupBase::_narrow( myGroupOnFilter );
1127       isCreation = true;
1128     }
1129     else
1130     {
1131       myGroupOnFilter->SetFilter( myFilter );
1132
1133       resultGroup = SMESH::SMESH_GroupBase::_narrow( myGroupOnFilter );
1134       isCreation = false;
1135     }
1136     anIsOk = true;
1137   }
1138
1139   if ( anIsOk )
1140   {
1141     SALOMEDS::Color aColor = getGroupColor();
1142     resultGroup->SetColor(aColor);
1143
1144     _PTR(SObject) aMeshGroupSO = SMESH::FindSObject( resultGroup );
1145     if( aMeshGroupSO )
1146       anEntryList.append( aMeshGroupSO->GetID().c_str() );
1147
1148     resultGroup->SetName(myName->text().trimmed().toUtf8());
1149
1150     if ( isCreation )
1151     {
1152       SMESH::setFileType ( aMeshGroupSO, "COULEURGROUP" );
1153
1154       /* init for the next operation */
1155       setDefaultName();
1156       myElements->clear();
1157       myGroup         = SMESH::SMESH_Group::_nil();
1158       myGroupOnGeom   = SMESH::SMESH_GroupOnGeom::_nil();
1159       myGroupOnFilter = SMESH::SMESH_GroupOnFilter::_nil();
1160       myFilter        = SMESH::Filter::_nil();
1161
1162       setDefaultGroupColor(); // reset color for case if 'auto-color' feature is enabled.
1163     }
1164     else
1165     {
1166       if ( aMeshGroupSO )
1167       {
1168         if ( SMESH_Actor *anActor = SMESH::FindActorByEntry(aMeshGroupSO->GetID().c_str()))
1169         {
1170           Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
1171           if ( isConversion ) { // need to reset TVisualObj and actor
1172             SMESH::RemoveVisualObjectWithActors( anIO->getEntry(), true );
1173             SMESH::Update( anIO,true);
1174             myActorsList.clear();
1175             anActor = SMESH::FindActorByEntry( anIO->getEntry() );
1176             if ( !anActor ) return false;
1177             myActorsList.append( anActor );
1178           }
1179           anActor->setName(myName->text().toUtf8());
1180           QColor c;
1181           int delta;
1182           switch ( myTypeId ) {
1183           case grpNodeSelection:   anActor->SetNodeColor( aColor.R, aColor.G, aColor.B ); break;
1184           case grp0DSelection:     anActor->Set0DColor  ( aColor.R, aColor.G, aColor.B ); break;
1185           case grpBallSelection:   anActor->SetBallColor( aColor.R, aColor.G, aColor.B ); break;
1186           case grpEdgeSelection:   anActor->SetEdgeColor( aColor.R, aColor.G, aColor.B ); break;
1187           case grpVolumeSelection: 
1188             SMESH::GetColor("SMESH", "volume_color", c , delta, "255,0,170|-100");
1189             anActor->SetVolumeColor( aColor.R, aColor.G, aColor.B, delta ); break;          
1190             break;
1191           case grpFaceSelection:   
1192           default:
1193             SMESH::GetColor("SMESH", "fill_color", c , delta, "0,170,255|-100");
1194             anActor->SetSufaceColor( aColor.R, aColor.G, aColor.B, delta ); break;          
1195             break;
1196           }
1197           // update a visible group according to a changed contents
1198           if ( !isConversion && anActor->GetVisibility() )
1199           {
1200             SMESH::Update( anIO, true );
1201             SMESH::RepaintCurrentView();
1202           }
1203         }
1204       }
1205     }
1206     SMESHGUI::Modified();
1207     mySMESHGUI->updateObjBrowser(true);
1208     mySelectionMgr->clearSelected();
1209
1210     if( LightApp_Application* anApp =
1211         dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
1212       myObjectToSelect = anApp->browseObjects( anEntryList, isApplyAndClose() );
1213   }
1214   return anIsOk;
1215 }
1216
1217 //=================================================================================
1218 // function : onOK()
1219 // purpose  :
1220 //=================================================================================
1221 void SMESHGUI_GroupDlg::onOK()
1222 {
1223   setIsApplyAndClose( true );
1224   if ( onApply() )
1225     reject();
1226   setIsApplyAndClose( false );
1227
1228   if ( myFilterDlg ) myFilterDlg->UnRegisterFilters();
1229 }
1230
1231 //=================================================================================
1232 // function : onListSelectionChanged()
1233 // purpose  : Called when selection in element list is changed
1234 //=================================================================================
1235 void SMESHGUI_GroupDlg::onListSelectionChanged()
1236 {
1237   //MESSAGE( "SMESHGUI_GroupDlg::onListSelectionChanged(); myActorsList.count() = " << myActorsList.count());
1238   if( myIsBusy || myActorsList.count() == 0 ) return;
1239   myIsBusy = true;
1240
1241   if (myCurrentLineEdit == 0) {
1242     mySelectionMgr->clearSelected();
1243     TColStd_MapOfInteger aIndexes;
1244     QList<QListWidgetItem*> selItems = myElements->selectedItems();
1245     QListWidgetItem* anItem;
1246     foreach(anItem, selItems) aIndexes.Add(anItem->text().toInt());
1247     mySelector->AddOrRemoveIndex(myActorsList.first()->getIO(), aIndexes, false);
1248     SALOME_ListIO aList;
1249     aList.Append(myActorsList.first()->getIO());
1250     mySelectionMgr->setSelectedObjects(aList,false);
1251   }
1252   myIsBusy = false;
1253 }
1254
1255 //=================================================================================
1256 // function : onObjectSelectionChanged()
1257 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
1258 //=================================================================================
1259 void SMESHGUI_GroupDlg::onObjectSelectionChanged()
1260 {
1261   if ( myIsBusy || !isEnabled()) return;
1262   if (myCurrentLineEdit == myGeomGroupLine && !myGeomGroupBtn->isChecked()) return;
1263
1264   myIsBusy = true;
1265
1266   SALOME_ListIO aList;
1267   mySelectionMgr->selectedObjects( aList );
1268
1269   int aNbSel = aList.Extent();
1270   myElements->clearSelection();
1271
1272   if (myCurrentLineEdit)
1273   {
1274     myCurrentLineEdit->setText( "" );
1275     QString aString = "";
1276
1277     if (myCurrentLineEdit == myMeshGroupLine)
1278     {
1279       mySelectSubMesh->setEnabled(false);
1280       mySelectGroup->setEnabled(false);
1281       myGroupLine->setText( "" );
1282       mySubMeshLine->setText( "" );
1283
1284       myGeomGroupBtn->setEnabled(false);
1285       myGeomGroupLine->setEnabled(false);
1286       myGeomGroupLine->setText( "" );
1287       myGeomObjects = new GEOM::ListOfGO();
1288       myGeomObjects->length(0);
1289
1290       if (myGeomGroupBtn->isChecked())
1291         myGeomGroupBtn->setChecked(false);
1292       if (!myCreate)
1293         myName->setText( "" );
1294
1295       myElements->clear();
1296
1297       if (aNbSel != 1 ) {
1298         myGroup = SMESH::SMESH_Group::_nil();
1299         myGroupOnGeom = SMESH::SMESH_GroupOnGeom::_nil();
1300         restoreShowEntityMode();
1301         myMesh = SMESH::SMESH_Mesh::_nil();
1302         updateGeomPopup();
1303         updateButtons();
1304         myIsBusy = false;
1305         return;
1306       }
1307       Handle(SALOME_InteractiveObject) IO = aList.First();
1308
1309       if (myCreate) {
1310         restoreShowEntityMode();
1311         myMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(IO);
1312         setShowEntityMode();
1313         updateGeomPopup();
1314         if (myMesh->_is_nil())
1315         {
1316           updateButtons();
1317           myIsBusy = false;
1318           return;
1319         }
1320
1321         if ( myFilterDlg && !myMesh->_is_nil()){
1322           myFilterDlg->SetMesh( myMesh );
1323         }
1324         myGroup = SMESH::SMESH_Group::_nil();
1325
1326         // NPAL19389: create a group with a selection in another group
1327         // set actor of myMesh, if it is visible, else try
1328         // any visible actor of group or submesh of myMesh
1329         SetAppropriateActor();
1330
1331         setDefaultGroupColor();
1332         if (myName->text().isEmpty())
1333           setDefaultName();
1334
1335         aString = aList.First()->getName();
1336         myMeshGroupLine->setText(aString);
1337         myMeshGroupLine->home( false );
1338
1339         mySelectSubMesh->setEnabled(true);
1340         mySelectGroup->setEnabled(true);
1341         myGeomGroupBtn->setEnabled(true);
1342         myGeomGroupLine->setEnabled(true);
1343         updateButtons();
1344       }
1345       else {
1346         SMESH::SMESH_GroupBase_var aGroup = SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(IO);
1347         if (aGroup->_is_nil())
1348         {
1349           myIsBusy = false;
1350           return;
1351         }
1352         myIsBusy = false;
1353
1354         myGroup = SMESH::SMESH_Group::_nil();
1355         myGroupOnGeom = SMESH::SMESH_GroupOnGeom::_nil();
1356
1357         init(aGroup);
1358         myIsBusy = true;
1359         mySelectSubMesh->setEnabled(true);
1360         mySelectGroup->setEnabled(true);
1361       }
1362       myCurrentLineEdit = 0;
1363       myIsBusy = false;
1364       if (!myCreate)
1365         return;
1366
1367       if (myGrpTypeId == 0)
1368       {
1369         if (myTypeId == -1)
1370           onTypeChanged(0);
1371         else
1372         {
1373           myElements->clear();
1374           setSelectionMode(myTypeId);
1375         }
1376       }
1377
1378       myIsBusy = false;
1379       return;
1380
1381     }
1382     else if (myCurrentLineEdit == myGeomGroupLine)
1383     {
1384       myGeomObjects = new GEOM::ListOfGO();
1385
1386       // The mesh SObject
1387       _PTR(SObject) aMeshSO = SMESH::FindSObject(myMesh);
1388
1389       if (aNbSel == 0 || !aMeshSO)
1390       {
1391         myGeomObjects->length(0);
1392         updateButtons();
1393         myIsBusy = false;
1394         return;
1395       }
1396
1397       myGeomObjects->length(aNbSel);
1398
1399       GEOM::GEOM_Object_var aGeomGroup;
1400       int i = 0;
1401
1402       SALOME_ListIteratorOfListIO anIt (aList);
1403       for (; anIt.More(); anIt.Next())
1404       {
1405         aGeomGroup = GEOMBase::ConvertIOinGEOMObject(anIt.Value());
1406
1407         // Check if the object is a geometry group
1408         if (CORBA::is_nil(aGeomGroup))
1409           continue;
1410
1411         // Check if group constructed on the same shape as a mesh or on its child
1412
1413         // The main shape of the group
1414         GEOM::GEOM_Object_var aGroupMainShape;
1415         if (aGeomGroup->GetType() == 37)
1416         {
1417           GEOM::GEOM_IGroupOperations_wrap anOp =
1418             SMESH::GetGEOMGen( aGeomGroup )->GetIGroupOperations();
1419           aGroupMainShape = anOp->GetMainShape( aGeomGroup );
1420           // aGroupMainShape is an existing servant => GEOM_Object_var not GEOM_Object_wrap
1421         }
1422         else
1423         {
1424           aGroupMainShape = aGeomGroup;
1425           aGroupMainShape->Register();
1426         }
1427         CORBA::String_var entry = aGroupMainShape->GetStudyEntry();
1428         _PTR(SObject) aGroupMainShapeSO =
1429           SMESH::getStudy()->FindObjectID( entry.in() );
1430
1431         _PTR(SObject) anObj, aRef;
1432         bool isRefOrSubShape = false;
1433         if (aMeshSO->FindSubObject(1, anObj) &&  anObj->ReferencedObject(aRef)) {
1434           if (aRef->GetID() == aGroupMainShapeSO->GetID()) {
1435             isRefOrSubShape = true;
1436           } else {
1437             _PTR(SObject) aFather = aGroupMainShapeSO->GetFather();
1438             _PTR(SComponent) aComponent = aGroupMainShapeSO->GetFatherComponent();
1439             while (!isRefOrSubShape && aFather->GetID() != aComponent->GetID()) {
1440               if (aRef->GetID() == aFather->GetID())
1441                 isRefOrSubShape = true;
1442               else
1443                 aFather = aFather->GetFather();
1444             }
1445           }
1446         }
1447         if (isRefOrSubShape)
1448           myGeomObjects[i++] = aGeomGroup;
1449       }
1450
1451       myGeomObjects->length(i);
1452       if ( i == 0 )
1453       {
1454         myIsBusy = false;
1455         return;
1456       }
1457
1458       aNbSel = i;
1459     }
1460
1461     if (aNbSel >= 1) {
1462       if (aNbSel > 1) {
1463         if (myCurrentLineEdit == mySubMeshLine)
1464           aString = tr( "SMESH_SUBMESH_SELECTED" ).arg(aNbSel);
1465         else if (myCurrentLineEdit == myGroupLine)
1466           aString = tr( "SMESH_GROUP_SELECTED" ).arg(aNbSel);
1467         else if (myCurrentLineEdit == myGeomGroupLine)
1468           aString = tr( "%1 Objects" ).arg(aNbSel);
1469       }
1470       else {
1471         aString = aList.First()->getName();
1472       }
1473     }
1474
1475     myCurrentLineEdit->setText(aString);
1476     myCurrentLineEdit->home(false);
1477     // 07.06.2008 skl for IPAL19574:
1478     // change name of group only if it is empty
1479     if( myName->text().trimmed().isEmpty() || !myNameChanged ) {
1480       myOldName = myName->text();
1481       myName->blockSignals(true);
1482       myName->setText(aString);
1483       myName->blockSignals(false);
1484     }
1485
1486     updateButtons();
1487   }
1488   else // !myCurrentLineEdit: local selection of nodes or elements
1489   {
1490     if (aNbSel == 1 && myActorsList.count() > 0 )
1491     {
1492       // NPAL19389: create a group with a selection in another group
1493       // Switch myActor to the newly selected one, if the last
1494       // is visible and belongs to group or submesh of myMesh
1495       /*      Handle(SALOME_InteractiveObject) curIO = myActor->getIO();
1496       Handle(SALOME_InteractiveObject) selIO = aList.First();
1497       if (curIO->hasEntry() && selIO->hasEntry()) {
1498         const char* selEntry = selIO->getEntry();
1499         if (strcmp(curIO->getEntry(), selEntry) != 0) {
1500           // different objects: selected and myActor
1501           SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView();
1502           if (aViewWindow && aViewWindow->isVisible(selIO)) {
1503             // newly selected actor is visible
1504
1505             // mesh entry
1506             _PTR(SObject) aSObject = SMESH::FindSObject(myMesh);
1507             if (aSObject) {
1508               CORBA::String_var meshEntry = aSObject->GetID().c_str();
1509               int len = strlen(meshEntry);
1510
1511               if (strncmp(selEntry, meshEntry, len) == 0) {
1512                 // selected object is myMesh or a part of it
1513                 SMESH_Actor* anActor = SMESH::FindActorByEntry(selEntry);
1514                 if (anActor) {
1515                   myActor = anActor;
1516                   SMESH::SetPickable(myActor);
1517                 }
1518               }
1519             }
1520           }
1521         }
1522       }*/
1523       // NPAL19389 END
1524
1525       QString aListStr = "";
1526       int aNbItems = 0;
1527       if (myTypeId == 0) {
1528         QListIterator<SMESH_Actor*> it( myActorsList );
1529         while ( it.hasNext() ) {
1530           QString tmpStr;
1531           aNbItems += SMESH::GetNameOfSelectedNodes(mySelector, it.next()->getIO(), tmpStr);
1532           aListStr += tmpStr;
1533         }
1534       } else {
1535         QListIterator<SMESH_Actor*> it( myActorsList );
1536         while ( it.hasNext() ) {
1537           QString tmpStr;
1538           aNbItems += SMESH::GetNameOfSelectedElements(mySelector, it.next()->getIO(), tmpStr);
1539           aListStr += tmpStr;
1540         }
1541       }
1542       if (aNbItems > 0) {
1543         QListWidgetItem* anItem;
1544         QList<QListWidgetItem*> listItemsToSel;
1545         QStringList anElements = aListStr.split( " ", QString::SkipEmptyParts);
1546         for (QStringList::iterator it = anElements.begin(); it != anElements.end(); ++it) {
1547           QList<QListWidgetItem*> found = myElements->findItems(*it, Qt::MatchExactly);
1548           foreach(anItem, found)
1549             if (!anItem->isSelected())
1550               listItemsToSel.push_back(anItem);
1551         }
1552         bool blocked = myElements->signalsBlocked();
1553         myElements->blockSignals(true);
1554         foreach(anItem, listItemsToSel) anItem->setSelected(true);
1555         myElements->blockSignals(blocked);
1556         onListSelectionChanged();
1557         listItemsToSel.clear();
1558       }
1559     }
1560   }
1561   
1562   if (myActorsList.count() == 0) {
1563     if (!myGroup->_is_nil()) {
1564       SMESH_Actor* anActor = SMESH::FindActorByObject(myGroup);
1565       if ( anActor )
1566         myActorsList.append( anActor  );
1567     }
1568     else if(!myGroupOnGeom->_is_nil()) {
1569       SMESH_Actor* anActor = SMESH::FindActorByObject(myGroupOnGeom);
1570       if ( anActor )
1571         myActorsList.append( anActor );
1572     }
1573     else {
1574       SMESH_Actor* anActor = SMESH::FindActorByObject( myMesh );
1575       if ( anActor )
1576         myActorsList.append( anActor );
1577     }
1578   }
1579
1580   // somehow, if we display the mesh, while selecting from another actor,
1581   // the mesh becomes pickable, and there is no way to select any element
1582   if (myActorsList.count() > 0) {
1583     QListIterator<SMESH_Actor*> it( myActorsList );
1584     while ( it.hasNext() ) {
1585       SMESH_Actor* anActor = it.next();
1586       if ( IsActorVisible(anActor) )
1587         anActor->SetPickable(true);
1588     }
1589   }
1590
1591   myIsBusy = false;
1592 }
1593
1594 //=================================================================================
1595 // function : onSelectAll()
1596 // purpose  : Called when "Select all" is checked
1597 //=================================================================================
1598 void SMESHGUI_GroupDlg::onSelectAll()
1599 {
1600   bool noElemsModif = ( mySelectAll->isChecked() || !myAllowElemsModif->isChecked() );
1601
1602   myElementsLab->setEnabled( !noElemsModif );
1603   myElements->setEnabled   ( !noElemsModif );
1604   myFilterBtn->setEnabled  ( !noElemsModif );
1605   myAddBtn->setEnabled     ( !noElemsModif );
1606   myRemoveBtn->setEnabled  ( !noElemsModif );
1607   mySortBtn->setEnabled    ( !noElemsModif );
1608   mySelectBox->setEnabled  ( !noElemsModif );
1609   myAllowElemsModif->setEnabled( !mySelectAll->isChecked() );
1610   if ( noElemsModif ) mySMESHGUI->ResetState();
1611   else                mySMESHGUI->SetState(800);
1612
1613   int selMode     = mySelectionMode;
1614   mySelectionMode = grpNoSelection;
1615   setSelectionMode( selMode );
1616   updateButtons();
1617 }
1618
1619 //=================================================================================
1620 // function : onSelectSubMesh()
1621 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
1622 //=================================================================================
1623 void SMESHGUI_GroupDlg::onSelectSubMesh(bool on)
1624 {
1625   if (on) {
1626     if (mySelectGroup->isChecked()) {
1627       mySelectGroup->setChecked(false);
1628     }
1629     //VSR: else if (mySelectGeomGroup->isChecked()) {
1630     //VSR:   mySelectGeomGroup->setChecked(false);
1631     //VSR: }
1632     myCurrentLineEdit = mySubMeshLine;
1633     setSelectionMode(grpSubMeshSelection);
1634   }
1635   else {
1636     mySubMeshLine->setText( "" );
1637     myCurrentLineEdit = 0;
1638     if (myTypeId != -1)
1639       setSelectionMode(myTypeId);
1640   }
1641   mySubMeshBtn->setEnabled(on);
1642   mySubMeshLine->setEnabled(on);
1643 }
1644
1645
1646 //=================================================================================
1647 // function : (onSelectGroup)
1648 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
1649 //=================================================================================
1650 void SMESHGUI_GroupDlg::onSelectGroup(bool on)
1651 {
1652   if (on) {
1653     if (mySelectSubMesh->isChecked()) {
1654       mySelectSubMesh->setChecked(false);
1655     }
1656     myCurrentLineEdit = myGroupLine;
1657     setSelectionMode(grpGroupSelection);
1658   }
1659   else {
1660     myGroupLine->setText( "" );
1661     myCurrentLineEdit = 0;
1662     if (myTypeId != -1)
1663       setSelectionMode(myTypeId);
1664   }
1665   myGroupBtn->setEnabled(on);
1666   myGroupLine->setEnabled(on);
1667 }
1668
1669
1670 //=================================================================================
1671 // function : (onSelectGeomGroup)
1672 // purpose  : Called when group type changed. on == "on geometry" or "on filter"
1673 //=================================================================================
1674 void SMESHGUI_GroupDlg::onSelectGeomGroup(bool on)
1675 {
1676   if (on) {
1677     if (mySelectSubMesh->isChecked()) {
1678       mySelectSubMesh->setChecked(false);
1679     }
1680     else if (mySelectGroup->isChecked()) {
1681       mySelectGroup->setChecked(false);
1682     }
1683     if ( myGrpTypeId == 1 ) { // on geometry
1684       myCurrentLineEdit = myGeomGroupLine;
1685       updateGeomPopup();
1686     }
1687     else { // on filter
1688       myCurrentLineEdit = 0;
1689     }
1690     setSelectionMode(grpAllSelection);
1691   }
1692   else {
1693     myGeomGroupBtn->setChecked(false);
1694     myGeomObjects->length(0);
1695     myGeomGroupLine->setText( "" );
1696     myCurrentLineEdit = 0;
1697     if (myTypeId != -1)
1698       setSelectionMode( myTypeId );
1699   }
1700 }
1701
1702 //=================================================================================
1703 // function : setCurrentSelection()
1704 // purpose  :
1705 //=================================================================================
1706 void SMESHGUI_GroupDlg::setCurrentSelection()
1707 {
1708   QPushButton* send = (QPushButton*)sender();
1709   myCurrentLineEdit = 0;
1710   if (send == myMeshGroupBtn) {
1711     disconnect(myMeshGroupBtn, SIGNAL(clicked()), this, SLOT(setCurrentSelection()));
1712     mySelectionMgr->clearSelected();
1713     if (myCreate)
1714       setSelectionMode(grpMeshSelection);
1715     else
1716       setSelectionMode(grpGroupSelection);
1717     connect(myMeshGroupBtn, SIGNAL(clicked()), this, SLOT(setCurrentSelection()));
1718     myCurrentLineEdit = myMeshGroupLine;
1719     onObjectSelectionChanged();
1720   }
1721   else if (send == mySubMeshBtn) {
1722     myCurrentLineEdit = mySubMeshLine;
1723     onObjectSelectionChanged();
1724   }
1725   else if (send == myGroupBtn) {
1726     myCurrentLineEdit = myGroupLine;
1727     onObjectSelectionChanged();
1728   }
1729 }
1730
1731
1732 //=================================================================================
1733 // function : setFilters()
1734 // purpose  : SLOT. Called when "Filter" button pressed.
1735 //=================================================================================
1736 void SMESHGUI_GroupDlg::setFilters()
1737 {
1738   if(myMesh->_is_nil()) {
1739     SUIT_MessageBox::critical(this,
1740                               tr("SMESH_ERROR"),
1741                               tr("NO_MESH_SELECTED"));
1742    return;
1743   }
1744
1745   SMESH::ElementType aType = SMESH::ALL;
1746   switch ( myTypeId )
1747   {
1748     case grpNodeSelection:   aType = SMESH::NODE;   break;
1749     case grp0DSelection:     aType = SMESH::ELEM0D; break;
1750     case grpBallSelection:   aType = SMESH::BALL;   break;
1751     case grpEdgeSelection:   aType = SMESH::EDGE;   break;
1752     case grpFaceSelection:   aType = SMESH::FACE;   break;
1753     case grpVolumeSelection: aType = SMESH::VOLUME; break;
1754     default:                 return;
1755   }
1756
1757   if ( myFilterDlg == 0 )
1758   {
1759     myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, aType );
1760     connect( myFilterDlg, SIGNAL( Accepted() ), SLOT( onFilterAccepted() ) );
1761   }
1762   else
1763     myFilterDlg->Init( aType );
1764
1765   if ( !myGroupOnFilter->_is_nil() )
1766   {
1767     myFilterDlg->SetFilter( myFilter, aType );
1768     myFilterDlg->Init( aType );
1769   }
1770
1771   bool isStandalone = ( sender() == myFilterBtn );
1772   myFilterDlg->SetEnabled( /*setInViewer=*/isStandalone,
1773                            /*diffSources=*/isStandalone );
1774   myFilterDlg->SetMesh( myMesh );
1775   myFilterDlg->SetGroup( myGroupOnFilter );
1776   myFilterDlg->SetSelection();
1777   myFilterDlg->SetSourceWg( myElements, false );
1778
1779   myFilterDlg->show();
1780 }
1781
1782 //=================================================================================
1783 // function : onFilterAccepted()
1784 // purpose  : SLOT. Called when Filter dlg closed with OK button.
1785 //            Uncheck "Select submesh" and "Select group" checkboxes
1786 //=================================================================================
1787 void SMESHGUI_GroupDlg::onFilterAccepted()
1788 {
1789   if ( mySelectSubMesh->isChecked() || mySelectGroup->isChecked() )
1790   {
1791     mySelectionMode = myTypeId;
1792     mySelectSubMesh->setChecked( false );
1793     mySelectGroup->setChecked( false );
1794   }
1795   // get a filter from myFilterDlg
1796   myFilter = myFilterDlg->GetFilter();
1797   if ( !myFilter->_is_nil() ) {
1798     SMESH::Predicate_var perdicate = myFilter->GetPredicate();
1799     if ( perdicate->_is_nil() )
1800       myFilter = SMESH::Filter::_nil();
1801   }
1802   // set mesh to myFilter
1803   if ( !myFilter->_is_nil() ) {
1804     SMESH::SMESH_Mesh_var mesh = myMesh;
1805     if ( mesh->_is_nil() ) {
1806       if ( !myGroup->_is_nil() )
1807         mesh = myGroup->GetMesh();
1808       else if ( !myGroupOnGeom->_is_nil() )
1809         mesh = myGroupOnGeom->GetMesh();
1810       else if ( !myGroupOnFilter->_is_nil() )
1811         mesh = myGroupOnFilter->GetMesh();
1812     }
1813     myFilter->SetMesh( mesh );
1814
1815     // highlight ids if selection changed in the Viewer (IPAL52924)
1816     myCurrentLineEdit = 0;
1817     onObjectSelectionChanged();
1818   }
1819
1820   updateButtons();
1821 }
1822
1823 //=================================================================================
1824 // function : onAdd()
1825 // purpose  :
1826 //=================================================================================
1827 void SMESHGUI_GroupDlg::onAdd()
1828 {
1829   SALOME_ListIO aList;
1830   mySelectionMgr->selectedObjects( aList );
1831
1832   int aNbSel = aList.Extent();
1833
1834   if (aNbSel == 0 || myActorsList.count() == 0 || myMesh->_is_nil()) return;
1835
1836   SUIT_OverrideCursor wc;
1837
1838   myIsBusy = true;
1839   int sizeBefore = myElements->count();
1840
1841   SMESH::ElementType aType = SMESH::ALL;
1842   switch(myTypeId) {
1843   case grpNodeSelection:
1844     aType = SMESH::NODE;
1845     mySelector->SetSelectionMode(NodeSelection);
1846     break;
1847   case grpBallSelection:
1848     aType = SMESH::BALL;
1849     mySelector->SetSelectionMode(BallSelection);
1850     break;
1851   case grp0DSelection:
1852     aType = SMESH::ELEM0D;
1853     mySelector->SetSelectionMode(Elem0DSelection);
1854     break;
1855   case grpEdgeSelection:
1856     aType = SMESH::EDGE;
1857     mySelector->SetSelectionMode(EdgeSelection);
1858     break;
1859   case grpFaceSelection:
1860     aType = SMESH::FACE;
1861     mySelector->SetSelectionMode(FaceSelection);
1862     break;
1863   case grpVolumeSelection:
1864     aType = SMESH::VOLUME;
1865     mySelector->SetSelectionMode(VolumeSelection);
1866     break;
1867   default:
1868     mySelector->SetSelectionMode(ActorSelection);
1869   }
1870
1871   QListWidgetItem* anItem = 0;
1872   QList<QListWidgetItem*> listItemsToSel;
1873
1874   if ( myCurrentLineEdit == 0 )
1875   {
1876     //if (aNbSel != 1) { myIsBusy = false; return; }
1877     QString aListStr = "";
1878     int aNbItems = 0;
1879     if (myTypeId == 0) {
1880       QListIterator<SMESH_Actor*> it( myActorsList );
1881       while ( it.hasNext() ) {
1882         QString tmpStr;
1883         aNbItems += SMESH::GetNameOfSelectedNodes(mySelector, it.next()->getIO(), tmpStr);
1884         aListStr += tmpStr;
1885       }
1886     }
1887     else {
1888       QListIterator<SMESH_Actor*> it( myActorsList );
1889       while ( it.hasNext() ) {
1890         QString tmpStr;
1891         aNbItems += SMESH::GetNameOfSelectedElements(mySelector, it.next()->getIO(), tmpStr);
1892         aListStr += tmpStr;
1893       }
1894     }
1895     if (aNbItems > 0) {
1896       QStringList anElements = aListStr.split( " ", QString::SkipEmptyParts);
1897       for (QStringList::iterator it = anElements.begin(); it != anElements.end(); ++it) {
1898         QList<QListWidgetItem*> found = myElements->findItems(*it, Qt::MatchExactly);
1899         if (found.count() == 0) {
1900           anItem = new QListWidgetItem(*it);
1901           myElements->addItem(anItem);
1902           if (!anItem->isSelected())
1903             listItemsToSel.push_back(anItem);
1904         }
1905         else {
1906           foreach(anItem, found)
1907             if (!anItem->isSelected())
1908               listItemsToSel.push_back(anItem);
1909         }
1910       }
1911       bool blocked = myElements->signalsBlocked();
1912       myElements->blockSignals(true);
1913       foreach(anItem, listItemsToSel) anItem->setSelected(true);
1914       myElements->blockSignals(blocked);
1915       onListSelectionChanged();
1916       listItemsToSel.clear();
1917     }
1918   }
1919   else if ( myCurrentLineEdit == mySubMeshLine )
1920   {
1921     //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1922
1923     SALOME_ListIO aList;
1924     mySelectionMgr->selectedObjects( aList );
1925
1926     SALOME_ListIteratorOfListIO anIt (aList);
1927     for ( ; anIt.More(); anIt.Next()) {
1928       SMESH::SMESH_subMesh_var aSubMesh =
1929         SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIt.Value());
1930       if (!aSubMesh->_is_nil()) {
1931         // check if mesh is the same
1932         if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
1933           try {
1934             SMESH::long_array_var anElements = aSubMesh->GetElementsByType(aType);
1935             int k = anElements->length();
1936             for (int i = 0; i < k; i++) {
1937               QString aText = QString::number(anElements[i]);
1938               QList<QListWidgetItem*> found = myElements->findItems(aText, Qt::MatchExactly);
1939               if (found.count() == 0) {
1940                 anItem = new QListWidgetItem(aText);
1941                 myElements->addItem(anItem);
1942                 if (!anItem->isSelected())
1943                   listItemsToSel.push_back(anItem);
1944               }
1945               else {
1946                 foreach(anItem, found)
1947                   if (!anItem->isSelected())
1948                     listItemsToSel.push_back(anItem);
1949               }
1950             }
1951             bool blocked = myElements->signalsBlocked();
1952             myElements->blockSignals(true);
1953             foreach(anItem, listItemsToSel) anItem->setSelected(true);
1954             myElements->blockSignals(blocked);
1955             onListSelectionChanged();
1956             listItemsToSel.clear();
1957           }
1958           catch (const SALOME::SALOME_Exception& ex) {
1959             SalomeApp_Tools::QtCatchCorbaException(ex);
1960           }
1961         }
1962       }
1963     }
1964     mySelectSubMesh->setChecked(false);
1965     myIsBusy = false;
1966     onListSelectionChanged();
1967
1968   }
1969   else if ( myCurrentLineEdit == myGroupLine )
1970   {
1971     //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1972     SALOME_ListIO aList;
1973     mySelectionMgr->selectedObjects( aList );
1974
1975     SALOME_ListIteratorOfListIO anIt (aList);
1976     for ( ; anIt.More(); anIt.Next()) {
1977       SMESH::SMESH_GroupBase_var aGroup =
1978         SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(anIt.Value());
1979       if (!aGroup->_is_nil()) {
1980         // check if mesh is the same
1981         if (aGroup->GetType() == aType && aGroup->GetMesh()->GetId() == myMesh->GetId()) {
1982           SMESH::long_array_var anElements = aGroup->GetListOfID();
1983           int k = anElements->length();
1984           for (int i = 0; i < k; i++) {
1985             QString aText = QString::number(anElements[i]);
1986             QList<QListWidgetItem*> found = myElements->findItems(aText, Qt::MatchExactly);
1987             if (found.count() == 0) {
1988               anItem = new QListWidgetItem(aText);
1989               myElements->addItem(anItem);
1990               if (!anItem->isSelected())
1991                 listItemsToSel.push_back(anItem);
1992             }
1993             else {
1994               foreach(anItem, found)
1995                 if (!anItem->isSelected())
1996                   listItemsToSel.push_back(anItem);
1997             }
1998           }
1999           bool blocked = myElements->signalsBlocked();
2000           myElements->blockSignals(true);
2001           foreach(anItem, listItemsToSel) anItem->setSelected(true);
2002           myElements->blockSignals(blocked);
2003           onListSelectionChanged();
2004           listItemsToSel.clear();
2005         }
2006       }
2007     }
2008     mySelectGroup->setChecked(false);
2009     myIsBusy = false;
2010     onListSelectionChanged();
2011
2012   }
2013   else if ( myCurrentLineEdit == myGeomGroupLine && myGeomObjects->length() == 1 )
2014   {
2015     GEOM::GEOM_IGroupOperations_wrap aGroupOp =
2016       SMESH::GetGEOMGen( myGeomObjects[0] )->GetIGroupOperations();
2017
2018     SMESH::ElementType aGroupType = SMESH::ALL;
2019     switch(aGroupOp->GetType(myGeomObjects[0])) {
2020     case TopAbs_VERTEX: aGroupType = SMESH::NODE; break;
2021     case TopAbs_EDGE:   aGroupType = SMESH::EDGE; break;
2022     case TopAbs_FACE:   aGroupType = SMESH::FACE; break;
2023     case TopAbs_SOLID:  aGroupType = SMESH::VOLUME; break;
2024     default: myIsBusy = false; return;
2025     }
2026
2027     if (aGroupType == aType) {
2028       _PTR(SObject) aGroupSO =
2029         //SMESH::getStudy()->FindObjectIOR(SMESH::getStudy()->ConvertObjectToIOR(myGeomGroup));
2030         SMESH::getStudy()->FindObjectID(myGeomObjects[0]->GetStudyEntry());
2031       // Construct filter
2032       SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
2033       SMESH::Filter_var aFilter = aFilterMgr->CreateFilter();
2034       SMESH::BelongToGeom_var aBelongToGeom = aFilterMgr->CreateBelongToGeom();
2035       aBelongToGeom->SetGeom(myGeomObjects[0]);
2036       aBelongToGeom->SetShapeName(aGroupSO->GetName().c_str());
2037       aBelongToGeom->SetElementType(aType);
2038       aFilter->SetPredicate(aBelongToGeom);
2039
2040       SMESH::long_array_var anElements = aFilter->GetElementsId(myMesh);
2041
2042       int k = anElements->length();
2043       for (int i = 0; i < k; i++) {
2044         QString aText = QString::number(anElements[i]);
2045         QList<QListWidgetItem*> found = myElements->findItems(aText, Qt::MatchExactly);
2046         if (found.count() == 0) {
2047           anItem = new QListWidgetItem(aText);
2048           myElements->addItem(anItem);
2049           if (!anItem->isSelected())
2050             listItemsToSel.push_back(anItem);
2051         }
2052         else {
2053           foreach(anItem, found)
2054             if (!anItem->isSelected())
2055               listItemsToSel.push_back(anItem);
2056         }
2057       }
2058       bool blocked = myElements->signalsBlocked();
2059       myElements->blockSignals(true);
2060       foreach(anItem, listItemsToSel) anItem->setSelected(true);
2061       myElements->blockSignals(blocked);
2062       onListSelectionChanged();
2063       listItemsToSel.clear();
2064     }
2065
2066     //VSR: mySelectGeomGroup->setChecked(false);
2067     myIsBusy = false;
2068     onListSelectionChanged();
2069   }
2070   myIsBusy = false;
2071   if ( sizeBefore < myElements->count() )
2072     ++myNbChangesOfContents;
2073   //  mySelectionMgr->clearSelected();
2074   updateButtons();
2075 }
2076
2077 //=================================================================================
2078 // function : onRemove()
2079 // purpose  :
2080 //=================================================================================
2081 void SMESHGUI_GroupDlg::onRemove()
2082 {
2083   myIsBusy = true;
2084   int sizeBefore = myElements->count();
2085
2086   if (myCurrentLineEdit == 0) {
2087     QList<QListWidgetItem*> selItems = myElements->selectedItems();
2088     QListWidgetItem* item;
2089     foreach(item, selItems) delete item;
2090   } else {
2091     SALOME_ListIO aList;
2092     mySelectionMgr->selectedObjects( aList );
2093
2094     int aNbSel = aList.Extent();
2095
2096     if (aNbSel == 0) { myIsBusy = false; return; }
2097
2098     SMESH::ElementType aType = SMESH::ALL;
2099     switch(myTypeId) {
2100     case grpNodeSelection:   aType = SMESH::NODE;   break;
2101     case grp0DSelection:     aType = SMESH::ELEM0D; break;
2102     case grpBallSelection:   aType = SMESH::BALL;   break;
2103     case grpEdgeSelection:   aType = SMESH::EDGE;   break;
2104     case grpFaceSelection:   aType = SMESH::FACE;   break;
2105     case grpVolumeSelection: aType = SMESH::VOLUME; break;
2106     }
2107
2108     if (myCurrentLineEdit == mySubMeshLine) {
2109       //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
2110       SALOME_ListIO aList;
2111       mySelectionMgr->selectedObjects( aList );
2112
2113       SALOME_ListIteratorOfListIO anIt (aList);
2114       for ( ; anIt.More(); anIt.Next()) {
2115         SMESH::SMESH_subMesh_var aSubMesh = SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIt.Value());
2116         if (!aSubMesh->_is_nil()) {
2117           // check if mesh is the same
2118           if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
2119             if (aType == SMESH::NODE) {
2120               try {
2121                 SMESH::long_array_var anElements = aSubMesh->GetNodesId();
2122                 int k = anElements->length();
2123                 for (int i = 0; i < k; i++) {
2124                   QList<QListWidgetItem*> found = 
2125                     myElements->findItems(QString::number(anElements[i]), Qt::MatchExactly);
2126                   QListWidgetItem* anItem;
2127                   foreach(anItem, found) delete anItem;
2128                 }
2129               }
2130               catch (const SALOME::SALOME_Exception& ex) {
2131                 SalomeApp_Tools::QtCatchCorbaException(ex);
2132               }
2133             }
2134             else {
2135               try {
2136                 SMESH::long_array_var anElements = aSubMesh->GetElementsId();
2137                 int k = anElements->length();
2138                 for (int i = 0; i < k; i++) {
2139                   QList<QListWidgetItem*> found = 
2140                     myElements->findItems(QString::number(anElements[i]), Qt::MatchExactly);
2141                   QListWidgetItem* anItem;
2142                   foreach(anItem, found) delete anItem;
2143                 }
2144               }
2145               catch (const SALOME::SALOME_Exception& ex) {
2146                 SalomeApp_Tools::QtCatchCorbaException(ex);
2147               }
2148             }
2149           }
2150         }
2151       }
2152     }
2153     else if (myCurrentLineEdit == myGroupLine) {
2154       SALOME_ListIO aList;
2155       mySelectionMgr->selectedObjects( aList );
2156
2157       SALOME_ListIteratorOfListIO anIt (aList);
2158       for ( ; anIt.More(); anIt.Next()) {
2159         SMESH::SMESH_Group_var aGroup = SMESH::IObjectToInterface<SMESH::SMESH_Group>(anIt.Value());
2160         if (!aGroup->_is_nil()) {
2161           // check if mesh is the same
2162           if (aGroup->GetType() == aType && aGroup->GetMesh()->GetId() == myMesh->GetId()) {
2163             SMESH::long_array_var anElements = aGroup->GetListOfID();
2164             int k = anElements->length();
2165             for (int i = 0; i < k; i++) {
2166               QList<QListWidgetItem*> found = 
2167                 myElements->findItems(QString::number(anElements[i]), Qt::MatchExactly);
2168               QListWidgetItem* anItem;
2169               foreach(anItem, found) delete anItem;
2170             }
2171           }
2172         }
2173       }
2174     }
2175   }
2176   myIsBusy = false;
2177   if ( sizeBefore > myElements->count() )
2178     myNbChangesOfContents += 2; // it's used to detect that "Add" was only once
2179   updateButtons();
2180 }
2181
2182 //=================================================================================
2183 // function : onSort()
2184 // purpose  :
2185 //=================================================================================
2186 void SMESHGUI_GroupDlg::onSort()
2187 {
2188   // PAL5412: sorts items in ascending by "string" value
2189   // myElements->sort(true);
2190   // myElements->update();
2191   int i, k = myElements->count();
2192   if (k > 0) {
2193     myIsBusy = true;
2194     QList<int> aSelected;
2195     std::vector<int> anArray(k);
2196     //    QMemArray<int> anArray(k);
2197     // fill the array
2198     for (i = 0; i < k; i++) {
2199       int id = myElements->item(i)->text().toInt();
2200       anArray[i] = id;
2201       if (myElements->item(i)->isSelected())
2202         aSelected.append(id);
2203     }
2204     // sort & update list
2205     std::sort(anArray.begin(), anArray.end());
2206     //    anArray.sort();
2207     myElements->clear();
2208     QListWidgetItem* anItem;
2209     QList<QListWidgetItem*> listItemsToSel;
2210     for (i = 0; i < k; i++) {
2211       anItem = new QListWidgetItem(QString::number(anArray[i]));
2212       myElements->addItem(anItem);
2213       if (aSelected.contains(anArray[i]))
2214         listItemsToSel.push_back(anItem);
2215     }
2216     bool blocked = myElements->signalsBlocked();
2217     myElements->blockSignals(true);
2218     foreach(anItem, listItemsToSel) anItem->setSelected(true);
2219     myElements->blockSignals(blocked);
2220     listItemsToSel.clear();
2221     myIsBusy = false;
2222   }
2223 }
2224
2225 //=================================================================================
2226 // function : onVisibilityChanged()
2227 // purpose  :
2228 //=================================================================================
2229 void SMESHGUI_GroupDlg::onVisibilityChanged()
2230 {
2231   SetAppropriateActor();
2232 }
2233
2234 //=================================================================================
2235 // function : SMESHGUI_GroupDlg::reject
2236 // purpose  : SLOT called when "Close" button pressed. Close dialog
2237 //=================================================================================
2238 void SMESHGUI_GroupDlg::reject()
2239 {
2240   if (SMESH::GetCurrentVtkView()) {
2241     SMESH::RemoveFilters(); // PAL6938 -- clean all mesh entity filters
2242     SMESH::SetPointRepresentation(false);
2243     SMESH::SetPickable();
2244     restoreShowEntityMode();
2245   }
2246
2247   if( isApplyAndClose() && !myObjectToSelect.isEmpty() ) {
2248     SUIT_DataOwnerPtrList aList;
2249     aList.append( new LightApp_DataOwner( myObjectToSelect ) );
2250     mySelectionMgr->setSelected( aList );
2251   }
2252   else
2253     mySelectionMgr->clearSelected();
2254   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
2255     aViewWindow->SetSelectionMode(ActorSelection);
2256   mySelectionMgr->clearFilters();
2257   mySMESHGUI->ResetState();
2258
2259   QDialog::reject();
2260
2261   if ( myFilterDlg ) myFilterDlg->UnRegisterFilters();
2262 }
2263
2264 //=================================================================================
2265 // function : onOpenView()
2266 // purpose  :
2267 //=================================================================================
2268 void SMESHGUI_GroupDlg::onOpenView()
2269 {
2270   if ( mySelector ) {
2271     SMESH::SetPointRepresentation(false);
2272   }
2273   else {
2274     mySelector = SMESH::GetViewWindow( mySMESHGUI )->GetSelector();
2275     mySMESHGUI->EmitSignalDeactivateDialog();
2276     setEnabled(true);
2277   }
2278 }
2279
2280 //=================================================================================
2281 // function : onCloseView()
2282 // purpose  :
2283 //=================================================================================
2284 void SMESHGUI_GroupDlg::onCloseView()
2285 {
2286   onDeactivate();
2287   mySelector = 0;
2288 }
2289
2290 //=================================================================================
2291 // function : onHelp()
2292 // purpose  :
2293 //=================================================================================
2294 void SMESHGUI_GroupDlg::onHelp()
2295 {
2296   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
2297   if (app)
2298   {
2299     app->onHelpContextModule
2300       ( mySMESHGUI ? app->moduleName( mySMESHGUI->moduleName() ) : QString(""), myHelpFileName );
2301   }
2302   else
2303   {
2304 #ifdef WIN32
2305     QString platform = "winapplication";
2306 #else
2307     QString platform = "application";
2308 #endif
2309     SUIT_MessageBox::warning(this, tr( "WRN_WARNING" ),
2310                              tr( "EXTERNAL_BROWSER_CANNOT_SHOW_PAGE" ).
2311                              arg(app->resourceMgr()->stringValue( "ExternalBrowser", platform)).
2312                              arg(myHelpFileName));
2313   }
2314 }
2315
2316 //=================================================================================
2317 // function : SMESHGUI_GroupDlg::onDeactivate
2318 // purpose  : SLOT called when dialog must be deactivated
2319 //=================================================================================
2320 void SMESHGUI_GroupDlg::onDeactivate()
2321 {
2322   mySMESHGUI->ResetState();
2323   setEnabled(false);
2324 }
2325
2326 //=================================================================================
2327 // function : SMESHGUI_GroupDlg::enterEvent
2328 // purpose  : Event filter
2329 //=================================================================================
2330 void SMESHGUI_GroupDlg::enterEvent (QEvent*)
2331 {
2332   if (!isEnabled()) {
2333     SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI );
2334     if ( aViewWindow && !mySelector) {
2335       mySelector = aViewWindow->GetSelector();
2336     }
2337     mySMESHGUI->EmitSignalDeactivateDialog();
2338     setEnabled(true);
2339     mySelectionMode = grpNoSelection;
2340     setSelectionMode(myTypeId);
2341     mySMESHGUI->SetActiveDialogBox(this);
2342     if ( mySelectBox->isEnabled() ) mySMESHGUI->SetState(800);
2343     else                            mySMESHGUI->ResetState();
2344   }
2345 }
2346
2347 //=================================================================================
2348 // function : keyPressEvent()
2349 // purpose  :
2350 //=================================================================================
2351 void SMESHGUI_GroupDlg::keyPressEvent( QKeyEvent* e )
2352 {
2353   QDialog::keyPressEvent( e );
2354   if ( e->isAccepted() )
2355     return;
2356
2357   if ( e->key() == Qt::Key_F1 )
2358   {
2359     e->accept();
2360     onHelp();
2361   }
2362 }
2363
2364 //================================================================================
2365 /*!
2366  * \brief Enable showing of the popup when Geometry selection btn is clicked
2367  * \param enable - true to enable
2368  */
2369 //================================================================================
2370
2371 enum { DIRECT_GEOM_INDEX = 0, GEOM_BY_MESH_INDEX };
2372
2373 void SMESHGUI_GroupDlg::updateGeomPopup()
2374 {
2375   bool enable = false;
2376
2377   if ( !myMesh->_is_nil() )
2378     enable = myMesh->NbEdges() > 0;
2379
2380   if ( myGeomGroupBtn )
2381   {
2382     disconnect( myGeomGroupBtn, SIGNAL( toggled(bool) ), this, SLOT( onGeomSelectionButton(bool) ));
2383     if ( enable ) {
2384       if ( !myGeomPopup ) {
2385         myGeomPopup = new QMenu(this);
2386         myActions[myGeomPopup->addAction( tr( "DIRECT_GEOM_SELECTION" ) )] = DIRECT_GEOM_INDEX;
2387         myActions[myGeomPopup->addAction( tr( "GEOM_BY_MESH_ELEM_SELECTION" ) )] = GEOM_BY_MESH_INDEX;
2388         connect( myGeomPopup, SIGNAL( triggered( QAction* ) ), SLOT( onGeomPopup( QAction* ) ) );
2389       }
2390       connect( myGeomGroupBtn, SIGNAL( toggled(bool) ), this, SLOT( onGeomSelectionButton(bool) ));
2391     }
2392   }
2393 }
2394
2395
2396 //=================================================================================
2397 // function : onGeomSelectionButton()
2398 // purpose  :
2399 //=================================================================================
2400 void SMESHGUI_GroupDlg::onGeomSelectionButton(bool isBtnOn)
2401 {
2402   if ( myGeomPopup && isBtnOn )
2403   {
2404     myCurrentLineEdit = myGeomGroupLine;
2405     QAction* a = myGeomPopup->exec( QCursor::pos() );
2406     if (!a || myActions[a] == DIRECT_GEOM_INDEX)
2407       setSelectionMode(grpGeomSelection);
2408   }
2409   else if (!isBtnOn)
2410   {
2411     myCurrentLineEdit = 0;
2412     setSelectionMode(grpAllSelection);
2413   }
2414 }
2415
2416 //=================================================================================
2417 // function : onGeomPopup()
2418 // purpose  :
2419 //=================================================================================
2420 void SMESHGUI_GroupDlg::onGeomPopup( QAction* a )
2421 {
2422   int index = myActions[a];
2423   if ( index == GEOM_BY_MESH_INDEX )
2424   {
2425     mySelectionMode = grpNoSelection;
2426     if ( !myShapeByMeshOp ) {
2427       myShapeByMeshOp = new SMESHGUI_ShapeByMeshOp(true);
2428       connect(myShapeByMeshOp, SIGNAL(committed(SUIT_Operation*)),
2429               SLOT(onPublishShapeByMeshDlg(SUIT_Operation*)));
2430       connect(myShapeByMeshOp, SIGNAL(aborted(SUIT_Operation*)),
2431               SLOT(onCloseShapeByMeshDlg(SUIT_Operation*)));
2432     }
2433     // set mesh object to SMESHGUI_ShapeByMeshOp and start it
2434     if ( !myMesh->_is_nil() ) {
2435       myIsBusy = true;
2436       hide(); // stop processing selection
2437       myIsBusy = false;
2438       myShapeByMeshOp->setModule( mySMESHGUI );
2439       myShapeByMeshOp->setStudy( 0 ); // it's really necessary
2440       myShapeByMeshOp->SetMesh( myMesh );
2441       myShapeByMeshOp->start();
2442     }
2443   }
2444 }
2445
2446 //================================================================================
2447 /*!
2448  * \brief SLOT. Is called when Ok is pressed in SMESHGUI_ShapeByMeshDlg
2449  */
2450 //================================================================================
2451
2452 void SMESHGUI_GroupDlg::onPublishShapeByMeshDlg(SUIT_Operation* op)
2453 {
2454   if ( myShapeByMeshOp == op ) {
2455     mySMESHGUI->getApp()->updateObjectBrowser();
2456     show();
2457     // Select a found geometry object
2458     GEOM::GEOM_Object_var aGeomVar = myShapeByMeshOp->GetShape();
2459     if ( !aGeomVar->_is_nil() )
2460     {
2461       QString ID = aGeomVar->GetStudyEntry();
2462       if ( _PTR(SObject) aGeomSO = SMESH::getStudy()->FindObjectID( ID.toLatin1().data() )) {
2463         SALOME_ListIO anIOList;
2464         Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject
2465           ( aGeomSO->GetID().c_str(), "SMESH", aGeomSO->GetName().c_str() );
2466         anIOList.Append( anIO );
2467         mySelectionMgr->setSelectedObjects( anIOList, false );
2468         onObjectSelectionChanged();
2469       }
2470     }
2471   }
2472 }
2473
2474 //================================================================================
2475 /*!
2476  * \brief SLOT. Is called when Close is pressed in SMESHGUI_ShapeByMeshDlg
2477  */
2478 //================================================================================
2479
2480 void SMESHGUI_GroupDlg::onCloseShapeByMeshDlg(SUIT_Operation* op)
2481 {
2482   if ( myShapeByMeshOp == op )
2483   {
2484     show();
2485     setSelectionMode(grpGeomSelection);
2486   }
2487 }
2488
2489 //=================================================================================
2490 // function : setGroupColor()
2491 // purpose  :
2492 //=================================================================================
2493 void SMESHGUI_GroupDlg::setGroupColor( const SALOMEDS::Color& theColor )
2494 {
2495   QColor aQColor( (int)( theColor.R * 255.0 ),
2496                   (int)( theColor.G * 255.0 ),
2497                   (int)( theColor.B * 255.0 ) );
2498   setGroupQColor( aQColor );
2499 }
2500
2501 //=================================================================================
2502 // function : getGroupColor()
2503 // purpose  :
2504 //=================================================================================
2505 SALOMEDS::Color SMESHGUI_GroupDlg::getGroupColor() const
2506 {
2507   QColor aQColor = getGroupQColor();
2508
2509   SALOMEDS::Color aColor;
2510   aColor.R = (float)aQColor.red() / 255.0;
2511   aColor.G = (float)aQColor.green() / 255.0;
2512   aColor.B = (float)aQColor.blue() / 255.0;
2513
2514   return aColor;
2515 }
2516
2517 //=================================================================================
2518 // function : setGroupQColor()
2519 // purpose  :
2520 //=================================================================================
2521 void SMESHGUI_GroupDlg::setGroupQColor( const QColor& theColor )
2522 {
2523   if( theColor.isValid() )
2524     myColorBtn->setColor( theColor );
2525 }
2526
2527 //=================================================================================
2528 // function : getGroupQColor()
2529 // purpose  :
2530 //=================================================================================
2531 QColor SMESHGUI_GroupDlg::getGroupQColor() const
2532 {
2533   return myColorBtn->color();
2534 }
2535
2536 //=================================================================================
2537 // function : setDefaultGroupColor()
2538 // purpose  :
2539 //=================================================================================
2540 void SMESHGUI_GroupDlg::setDefaultGroupColor()
2541 {
2542   if( myMesh->_is_nil() )
2543     return;
2544
2545   bool isAutoColor = myMesh->GetAutoColor();
2546
2547   QColor aQColor = myColorBtn->color();
2548   if( !isAutoColor )
2549   {
2550     if ( !aQColor.isValid() ) {
2551       int r = 0, g = 0, b = 0;
2552       SMESH::GetColor( "SMESH", "default_grp_color", r, g, b, QColor( 255, 170, 0 ) );
2553       aQColor.setRgb( r, g, b );
2554     }
2555   }
2556   else
2557   {
2558 #ifdef SIMPLE_AUTOCOLOR   // simplified algorithm for auto-colors
2559     SALOMEDS::Color aColor = SMESHGUI::getPredefinedUniqueColor();
2560 #else                     // old algorithm  for auto-colors
2561     SMESH::ListOfGroups aListOfGroups = *myMesh->GetGroups();
2562
2563     QList<SALOMEDS::Color> aReservedColors;
2564     for( int i = 0, n = aListOfGroups.length(); i < n; i++ )
2565     {
2566       SMESH::SMESH_GroupBase_var aGroupObject = aListOfGroups[i];
2567       SALOMEDS::Color aReservedColor = aGroupObject->GetColor();
2568       aReservedColors.append( aReservedColor );
2569     }
2570
2571     SALOMEDS::Color aColor = SMESHGUI::getUniqueColor( aReservedColors );
2572 #endif                    // SIMPLE_AUTOCOLOR
2573
2574     aQColor.setRgb( (int)( aColor.R * 255.0 ),
2575                     (int)( aColor.G * 255.0 ),
2576                     (int)( aColor.B * 255.0 ) );
2577
2578   }
2579
2580   setGroupQColor( aQColor );
2581 }
2582
2583 //=================================================================================
2584 // function : SetAppropriateActor()
2585 // purpose  : Find more appropriate of visible actors, set it to myActor, allow picking
2586 //            NPAL19389: create a group with a selection in another group.
2587 //            if mesh actor is not visible - find any first visible group or sub-mesh
2588 //=================================================================================
2589 bool SMESHGUI_GroupDlg::SetAppropriateActor()
2590 {
2591   bool isActor = false;
2592   myActorsList.clear();
2593
2594   if (myMesh->_is_nil()) return false;
2595
2596   SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView();
2597
2598   if (myGrpTypeGroup->checkedId() > 0) {   // try current group on geometry actor
2599     SMESH_Actor* anActor = 0;
2600     if (!myGroupOnGeom->_is_nil())
2601       anActor = SMESH::FindActorByObject(myGroupOnGeom);
2602     if (!myGroupOnFilter->_is_nil())
2603       anActor = SMESH::FindActorByObject(myGroupOnFilter);
2604     if (anActor && anActor->hasIO())
2605     {
2606       isActor = true;
2607       if (aViewWindow && !aViewWindow->isVisible(anActor->getIO()))
2608         isActor = false;
2609       else
2610         myActorsList.append(anActor);
2611     }
2612     return anActor;
2613   }
2614   else {
2615     // try mesh actor
2616     SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh);
2617     if (anActor && anActor->hasIO()) {
2618       isActor = true;
2619       if (aViewWindow && !aViewWindow->isVisible(anActor->getIO()))
2620         isActor = false;
2621       else
2622         myActorsList.append(anActor);
2623     }
2624
2625     // try group actor
2626     SMESH_Actor* aGroupActor = 0;
2627     if (!isActor && !myGroup->_is_nil()) {
2628       aGroupActor = SMESH::FindActorByObject(myGroup);
2629       if (aGroupActor && aGroupActor->hasIO())
2630         myActorsList.append(aGroupActor);
2631     }
2632
2633     // try any visible actor of group or sub-mesh of current mesh
2634     if (aViewWindow) {
2635       // mesh entry
2636       _PTR(SObject) aSObject = SMESH::FindSObject(myMesh);
2637       if (aSObject) {
2638         CORBA::String_var meshEntry = aSObject->GetID().c_str();
2639         int len = strlen(meshEntry);
2640
2641         // iterate on all actors in current view window, search for
2642         // any visible actor, that belongs to group or submesh of current mesh
2643         VTK::ActorCollectionCopy aCopy(aViewWindow->getRenderer()->GetActors());
2644         vtkActorCollection *aCollection = aCopy.GetActors();
2645         int nbItems = aCollection->GetNumberOfItems();
2646         for (int i=0; i<nbItems && !isActor; i++)
2647         {
2648           SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(aCollection->GetItemAsObject(i));
2649           if (anActor && anActor->hasIO()) {
2650             Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
2651             if (aViewWindow->isVisible(anIO)) {
2652               if (anIO->hasEntry() && strncmp(anIO->getEntry(), meshEntry, len) == 0 && !myActorsList.contains(anActor) )
2653                 myActorsList.append(anActor);
2654             }
2655           }
2656         }
2657       }
2658     }
2659
2660     // Show a standalone group if nothing else is visible (IPAL52227)
2661     if ( myActorsList.count() == 1 &&
2662          myActorsList[0] == aGroupActor &&
2663          aViewWindow && !aViewWindow->isVisible(aGroupActor->getIO()))
2664       SMESH::UpdateView( aViewWindow, SMESH::eDisplay, aGroupActor->getIO()->getEntry() );
2665   }
2666
2667
2668   if (myActorsList.count() > 0) {
2669     QListIterator<SMESH_Actor*> it( myActorsList );
2670     while ( it.hasNext() ) {
2671       SMESH_Actor* anActor = it.next();
2672       if ( IsActorVisible(anActor) )
2673         anActor->SetPickable(true);
2674     }
2675   }
2676
2677   return ( isActor || (myActorsList.count() > 0) );
2678 }
2679
2680 //=======================================================================
2681 //function : setShowEntityMode
2682 //purpose  : make shown only entity corresponding to my type
2683 //=======================================================================
2684 void SMESHGUI_GroupDlg::setShowEntityMode()
2685 {
2686   if ( !myMesh->_is_nil() ) {
2687     if ( SMESH_Actor* actor = SMESH::FindActorByObject(myMesh) ) {
2688       if (!myStoredShownEntity)
2689         myStoredShownEntity = actor->GetEntityMode();
2690       switch ( myTypeId ) {
2691       case grpNodeSelection:   restoreShowEntityMode();                          break;
2692       case grp0DSelection:     actor->SetEntityMode( SMESH_Actor::e0DElements ); break;
2693       case grpBallSelection:   actor->SetEntityMode( SMESH_Actor::eBallElem );   break;
2694       case grpEdgeSelection:   actor->SetEntityMode( SMESH_Actor::eEdges );      break;
2695       case grpFaceSelection:   actor->SetEntityMode( SMESH_Actor::eFaces );      break;
2696       case grpVolumeSelection: actor->SetEntityMode( SMESH_Actor::eVolumes );    break;
2697       }
2698     }
2699   }
2700 }
2701
2702 //=======================================================================
2703 //function : restoreShowEntityMode
2704 //purpose  : restore ShowEntity mode of myActor
2705 //=======================================================================
2706 void SMESHGUI_GroupDlg::restoreShowEntityMode()
2707 {
2708   if ( myStoredShownEntity && !myMesh->_is_nil() ) {
2709     if ( SMESH_Actor* actor = SMESH::FindActorByObject(myMesh) ) {
2710       actor->SetEntityMode(myStoredShownEntity);
2711     }
2712   }
2713   myStoredShownEntity = 0;
2714 }
2715
2716 //=======================================================================
2717 //function : IsActorVisible
2718 //purpose  : return visibility of the actor
2719 //=======================================================================
2720 bool SMESHGUI_GroupDlg::IsActorVisible( SMESH_Actor* theActor )
2721 {
2722   SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView();
2723   if (theActor && aViewWindow)
2724     return aViewWindow->isVisible(theActor->getIO());
2725   return false;
2726 }
2727
2728 //================================================================
2729 //function : setIsApplyAndClose
2730 //purpose  : Set value of the flag indicating that the dialog is
2731 //           accepted by Apply & Close button
2732 //================================================================
2733 void SMESHGUI_GroupDlg::setIsApplyAndClose( const bool theFlag )
2734 {
2735   myIsApplyAndClose = theFlag;
2736 }
2737
2738 //================================================================
2739 //function : isApplyAndClose
2740 //purpose  : Get value of the flag indicating that the dialog is
2741 //           accepted by Apply & Close button
2742 //================================================================
2743 bool SMESHGUI_GroupDlg::isApplyAndClose() const
2744 {
2745   return myIsApplyAndClose;
2746 }