Salome HOME
BUG: SMESH::GetGEOMGen() returns GEOM_Engine instead of SHAPERSTUDY
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_GroupDlg.cxx
1 // Copyright (C) 2007-2019  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_ptr 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         op->UnionList(aGroupVar, myGeomObjects);
1087
1088         if (op->IsDone()) {
1089           // publish the GEOM group in study
1090           QString aNewGeomGroupName ( "Auto_group_for_" );
1091           aNewGeomGroupName += myName->text();
1092           SALOMEDS::SObject_var aNewGroupSO =
1093             geomGen->AddInStudy(aGroupVar,
1094                                 aNewGeomGroupName.toUtf8(), aMeshShape);
1095         }
1096
1097         myGroupOnGeom = myMesh->CreateGroupFromGEOM(aType,
1098                                                     myName->text().toUtf8(),
1099                                                     aGroupVar);
1100       }
1101       resultGroup = SMESH::SMESH_GroupBase::_narrow( myGroupOnGeom );
1102       isCreation = true;
1103
1104     }
1105     else { // edition
1106
1107       resultGroup = SMESH::SMESH_GroupBase::_narrow( myGroupOnGeom );
1108       isCreation = false;
1109     }
1110     anIsOk = true;
1111   }
1112   if (myGrpTypeId == 2) // group on filter
1113   {
1114     if ( myFilter->_is_nil() ) return false;
1115
1116     if (CORBA::is_nil(myGroupOnFilter)) // creation
1117     {
1118       if (myMesh->_is_nil())
1119         return false;
1120
1121       myGroupOnFilter = myMesh->CreateGroupFromFilter(aType,
1122                                                       myName->text().toUtf8(),
1123                                                       myFilter);
1124       resultGroup = SMESH::SMESH_GroupBase::_narrow( myGroupOnFilter );
1125       isCreation = true;
1126     }
1127     else
1128     {
1129       myGroupOnFilter->SetFilter( myFilter );
1130
1131       resultGroup = SMESH::SMESH_GroupBase::_narrow( myGroupOnFilter );
1132       isCreation = false;
1133     }
1134     anIsOk = true;
1135   }
1136
1137   if ( anIsOk )
1138   {
1139     SALOMEDS::Color aColor = getGroupColor();
1140     resultGroup->SetColor(aColor);
1141
1142     _PTR(SObject) aMeshGroupSO = SMESH::FindSObject( resultGroup );
1143     if( aMeshGroupSO )
1144       anEntryList.append( aMeshGroupSO->GetID().c_str() );
1145
1146     resultGroup->SetName(myName->text().trimmed().toUtf8());
1147
1148     if ( isCreation )
1149     {
1150       SMESH::setFileType ( aMeshGroupSO, "COULEURGROUP" );
1151
1152       /* init for the next operation */
1153       setDefaultName();
1154       myElements->clear();
1155       myGroup         = SMESH::SMESH_Group::_nil();
1156       myGroupOnGeom   = SMESH::SMESH_GroupOnGeom::_nil();
1157       myGroupOnFilter = SMESH::SMESH_GroupOnFilter::_nil();
1158       myFilter        = SMESH::Filter::_nil();
1159
1160       setDefaultGroupColor(); // reset color for case if 'auto-color' feature is enabled.
1161     }
1162     else
1163     {
1164       if ( aMeshGroupSO )
1165       {
1166         if ( SMESH_Actor *anActor = SMESH::FindActorByEntry(aMeshGroupSO->GetID().c_str()))
1167         {
1168           Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
1169           if ( isConversion ) { // need to reset TVisualObj and actor
1170             SMESH::RemoveVisualObjectWithActors( anIO->getEntry(), true );
1171             SMESH::Update( anIO,true);
1172             myActorsList.clear();
1173             anActor = SMESH::FindActorByEntry( anIO->getEntry() );
1174             if ( !anActor ) return false;
1175             myActorsList.append( anActor );
1176           }
1177           anActor->setName(myName->text().toUtf8());
1178           QColor c;
1179           int delta;
1180           switch ( myTypeId ) {
1181           case grpNodeSelection:   anActor->SetNodeColor( aColor.R, aColor.G, aColor.B ); break;
1182           case grp0DSelection:     anActor->Set0DColor  ( aColor.R, aColor.G, aColor.B ); break;
1183           case grpBallSelection:   anActor->SetBallColor( aColor.R, aColor.G, aColor.B ); break;
1184           case grpEdgeSelection:   anActor->SetEdgeColor( aColor.R, aColor.G, aColor.B ); break;
1185           case grpVolumeSelection: 
1186             SMESH::GetColor("SMESH", "volume_color", c , delta, "255,0,170|-100");
1187             anActor->SetVolumeColor( aColor.R, aColor.G, aColor.B, delta ); break;          
1188             break;
1189           case grpFaceSelection:   
1190           default:
1191             SMESH::GetColor("SMESH", "fill_color", c , delta, "0,170,255|-100");
1192             anActor->SetSufaceColor( aColor.R, aColor.G, aColor.B, delta ); break;          
1193             break;
1194           }
1195           // update a visible group according to a changed contents
1196           if ( !isConversion && anActor->GetVisibility() )
1197           {
1198             SMESH::Update( anIO, true );
1199             SMESH::RepaintCurrentView();
1200           }
1201         }
1202       }
1203     }
1204     SMESHGUI::Modified();
1205     mySMESHGUI->updateObjBrowser(true);
1206     mySelectionMgr->clearSelected();
1207
1208     if( LightApp_Application* anApp =
1209         dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
1210       myObjectToSelect = anApp->browseObjects( anEntryList, isApplyAndClose() );
1211   }
1212   return anIsOk;
1213 }
1214
1215 //=================================================================================
1216 // function : onOK()
1217 // purpose  :
1218 //=================================================================================
1219 void SMESHGUI_GroupDlg::onOK()
1220 {
1221   setIsApplyAndClose( true );
1222   if ( onApply() )
1223     reject();
1224   setIsApplyAndClose( false );
1225
1226   if ( myFilterDlg ) myFilterDlg->UnRegisterFilters();
1227 }
1228
1229 //=================================================================================
1230 // function : onListSelectionChanged()
1231 // purpose  : Called when selection in element list is changed
1232 //=================================================================================
1233 void SMESHGUI_GroupDlg::onListSelectionChanged()
1234 {
1235   //MESSAGE( "SMESHGUI_GroupDlg::onListSelectionChanged(); myActorsList.count() = " << myActorsList.count());
1236   if( myIsBusy || myActorsList.count() == 0 ) return;
1237   myIsBusy = true;
1238
1239   if (myCurrentLineEdit == 0) {
1240     mySelectionMgr->clearSelected();
1241     TColStd_MapOfInteger aIndexes;
1242     QList<QListWidgetItem*> selItems = myElements->selectedItems();
1243     QListWidgetItem* anItem;
1244     foreach(anItem, selItems) aIndexes.Add(anItem->text().toInt());
1245     mySelector->AddOrRemoveIndex(myActorsList.first()->getIO(), aIndexes, false);
1246     SALOME_ListIO aList;
1247     aList.Append(myActorsList.first()->getIO());
1248     mySelectionMgr->setSelectedObjects(aList,false);
1249   }
1250   myIsBusy = false;
1251 }
1252
1253 //=================================================================================
1254 // function : onObjectSelectionChanged()
1255 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
1256 //=================================================================================
1257 void SMESHGUI_GroupDlg::onObjectSelectionChanged()
1258 {
1259   if ( myIsBusy || !isEnabled()) return;
1260   if (myCurrentLineEdit == myGeomGroupLine && !myGeomGroupBtn->isChecked()) return;
1261
1262   myIsBusy = true;
1263
1264   SALOME_ListIO aList;
1265   mySelectionMgr->selectedObjects( aList );
1266
1267   int aNbSel = aList.Extent();
1268   myElements->clearSelection();
1269
1270   if (myCurrentLineEdit)
1271   {
1272     myCurrentLineEdit->setText( "" );
1273     QString aString = "";
1274
1275     if (myCurrentLineEdit == myMeshGroupLine)
1276     {
1277       mySelectSubMesh->setEnabled(false);
1278       mySelectGroup->setEnabled(false);
1279       myGroupLine->setText( "" );
1280       mySubMeshLine->setText( "" );
1281
1282       myGeomGroupBtn->setEnabled(false);
1283       myGeomGroupLine->setEnabled(false);
1284       myGeomGroupLine->setText( "" );
1285       myGeomObjects = new GEOM::ListOfGO();
1286       myGeomObjects->length(0);
1287
1288       if (myGeomGroupBtn->isChecked())
1289         myGeomGroupBtn->setChecked(false);
1290       if (!myCreate)
1291         myName->setText( "" );
1292
1293       myElements->clear();
1294
1295       if (aNbSel != 1 ) {
1296         myGroup = SMESH::SMESH_Group::_nil();
1297         myGroupOnGeom = SMESH::SMESH_GroupOnGeom::_nil();
1298         restoreShowEntityMode();
1299         myMesh = SMESH::SMESH_Mesh::_nil();
1300         updateGeomPopup();
1301         updateButtons();
1302         myIsBusy = false;
1303         return;
1304       }
1305       Handle(SALOME_InteractiveObject) IO = aList.First();
1306
1307       if (myCreate) {
1308         restoreShowEntityMode();
1309         myMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(IO);
1310         setShowEntityMode();
1311         updateGeomPopup();
1312         if (myMesh->_is_nil())
1313         {
1314           updateButtons();
1315           myIsBusy = false;
1316           return;
1317         }
1318
1319         if ( myFilterDlg && !myMesh->_is_nil()){
1320           myFilterDlg->SetMesh( myMesh );
1321         }
1322         myGroup = SMESH::SMESH_Group::_nil();
1323
1324         // NPAL19389: create a group with a selection in another group
1325         // set actor of myMesh, if it is visible, else try
1326         // any visible actor of group or submesh of myMesh
1327         SetAppropriateActor();
1328
1329         setDefaultGroupColor();
1330         if (myName->text().isEmpty())
1331           setDefaultName();
1332
1333         aString = aList.First()->getName();
1334         myMeshGroupLine->setText(aString);
1335         myMeshGroupLine->home( false );
1336
1337         mySelectSubMesh->setEnabled(true);
1338         mySelectGroup->setEnabled(true);
1339         myGeomGroupBtn->setEnabled(true);
1340         myGeomGroupLine->setEnabled(true);
1341         updateButtons();
1342       }
1343       else {
1344         SMESH::SMESH_GroupBase_var aGroup = SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(IO);
1345         if (aGroup->_is_nil())
1346         {
1347           myIsBusy = false;
1348           return;
1349         }
1350         myIsBusy = false;
1351
1352         myGroup = SMESH::SMESH_Group::_nil();
1353         myGroupOnGeom = SMESH::SMESH_GroupOnGeom::_nil();
1354
1355         init(aGroup);
1356         myIsBusy = true;
1357         mySelectSubMesh->setEnabled(true);
1358         mySelectGroup->setEnabled(true);
1359       }
1360       myCurrentLineEdit = 0;
1361       myIsBusy = false;
1362       if (!myCreate)
1363         return;
1364
1365       if (myGrpTypeId == 0)
1366       {
1367         if (myTypeId == -1)
1368           onTypeChanged(0);
1369         else
1370         {
1371           myElements->clear();
1372           setSelectionMode(myTypeId);
1373         }
1374       }
1375
1376       myIsBusy = false;
1377       return;
1378
1379     }
1380     else if (myCurrentLineEdit == myGeomGroupLine)
1381     {
1382       myGeomObjects = new GEOM::ListOfGO();
1383
1384       // The mesh SObject
1385       _PTR(SObject) aMeshSO = SMESH::FindSObject(myMesh);
1386
1387       if (aNbSel == 0 || !aMeshSO)
1388       {
1389         myGeomObjects->length(0);
1390         updateButtons();
1391         myIsBusy = false;
1392         return;
1393       }
1394
1395       myGeomObjects->length(aNbSel);
1396
1397       GEOM::GEOM_Object_var aGeomGroup;
1398       int i = 0;
1399
1400       SALOME_ListIteratorOfListIO anIt (aList);
1401       for (; anIt.More(); anIt.Next())
1402       {
1403         aGeomGroup = GEOMBase::ConvertIOinGEOMObject(anIt.Value());
1404
1405         // Check if the object is a geometry group
1406         if (CORBA::is_nil(aGeomGroup))
1407           continue;
1408
1409         // Check if group constructed on the same shape as a mesh or on its child
1410
1411         // The main shape of the group
1412         GEOM::GEOM_Object_var aGroupMainShape;
1413         if (aGeomGroup->GetType() == 37)
1414         {
1415           GEOM::GEOM_IGroupOperations_ptr anOp =
1416             SMESH::GetGEOMGen( aGeomGroup )->GetIGroupOperations();
1417           aGroupMainShape = anOp->GetMainShape( aGeomGroup );
1418           // aGroupMainShape is an existing servant => GEOM_Object_var not GEOM_Object_wrap
1419         }
1420         else
1421         {
1422           aGroupMainShape = aGeomGroup;
1423           aGroupMainShape->Register();
1424         }
1425         CORBA::String_var entry = aGroupMainShape->GetStudyEntry();
1426         _PTR(SObject) aGroupMainShapeSO =
1427           SMESH::getStudy()->FindObjectID( entry.in() );
1428
1429         _PTR(SObject) anObj, aRef;
1430         bool isRefOrSubShape = false;
1431         if (aMeshSO->FindSubObject(1, anObj) &&  anObj->ReferencedObject(aRef)) {
1432           if (aRef->GetID() == aGroupMainShapeSO->GetID()) {
1433             isRefOrSubShape = true;
1434           } else {
1435             _PTR(SObject) aFather = aGroupMainShapeSO->GetFather();
1436             _PTR(SComponent) aComponent = aGroupMainShapeSO->GetFatherComponent();
1437             while (!isRefOrSubShape && aFather->GetID() != aComponent->GetID()) {
1438               if (aRef->GetID() == aFather->GetID())
1439                 isRefOrSubShape = true;
1440               else
1441                 aFather = aFather->GetFather();
1442             }
1443           }
1444         }
1445         if (isRefOrSubShape)
1446           myGeomObjects[i++] = aGeomGroup;
1447       }
1448
1449       myGeomObjects->length(i);
1450       if ( i == 0 )
1451       {
1452         myIsBusy = false;
1453         return;
1454       }
1455
1456       aNbSel = i;
1457     }
1458
1459     if (aNbSel >= 1) {
1460       if (aNbSel > 1) {
1461         if (myCurrentLineEdit == mySubMeshLine)
1462           aString = tr( "SMESH_SUBMESH_SELECTED" ).arg(aNbSel);
1463         else if (myCurrentLineEdit == myGroupLine)
1464           aString = tr( "SMESH_GROUP_SELECTED" ).arg(aNbSel);
1465         else if (myCurrentLineEdit == myGeomGroupLine)
1466           aString = tr( "%1 Objects" ).arg(aNbSel);
1467       }
1468       else {
1469         aString = aList.First()->getName();
1470       }
1471     }
1472
1473     myCurrentLineEdit->setText(aString);
1474     myCurrentLineEdit->home(false);
1475     // 07.06.2008 skl for IPAL19574:
1476     // change name of group only if it is empty
1477     if( myName->text().trimmed().isEmpty() || !myNameChanged ) {
1478       myOldName = myName->text();
1479       myName->blockSignals(true);
1480       myName->setText(aString);
1481       myName->blockSignals(false);
1482     }
1483
1484     updateButtons();
1485   }
1486   else // !myCurrentLineEdit: local selection of nodes or elements
1487   {
1488     if (aNbSel == 1 && myActorsList.count() > 0 )
1489     {
1490       // NPAL19389: create a group with a selection in another group
1491       // Switch myActor to the newly selected one, if the last
1492       // is visible and belongs to group or submesh of myMesh
1493       /*      Handle(SALOME_InteractiveObject) curIO = myActor->getIO();
1494       Handle(SALOME_InteractiveObject) selIO = aList.First();
1495       if (curIO->hasEntry() && selIO->hasEntry()) {
1496         const char* selEntry = selIO->getEntry();
1497         if (strcmp(curIO->getEntry(), selEntry) != 0) {
1498           // different objects: selected and myActor
1499           SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView();
1500           if (aViewWindow && aViewWindow->isVisible(selIO)) {
1501             // newly selected actor is visible
1502
1503             // mesh entry
1504             _PTR(SObject) aSObject = SMESH::FindSObject(myMesh);
1505             if (aSObject) {
1506               CORBA::String_var meshEntry = aSObject->GetID().c_str();
1507               int len = strlen(meshEntry);
1508
1509               if (strncmp(selEntry, meshEntry, len) == 0) {
1510                 // selected object is myMesh or a part of it
1511                 SMESH_Actor* anActor = SMESH::FindActorByEntry(selEntry);
1512                 if (anActor) {
1513                   myActor = anActor;
1514                   SMESH::SetPickable(myActor);
1515                 }
1516               }
1517             }
1518           }
1519         }
1520       }*/
1521       // NPAL19389 END
1522
1523       QString aListStr = "";
1524       int aNbItems = 0;
1525       if (myTypeId == 0) {
1526         QListIterator<SMESH_Actor*> it( myActorsList );
1527         while ( it.hasNext() ) {
1528           QString tmpStr;
1529           aNbItems += SMESH::GetNameOfSelectedNodes(mySelector, it.next()->getIO(), tmpStr);
1530           aListStr += tmpStr;
1531         }
1532       } else {
1533         QListIterator<SMESH_Actor*> it( myActorsList );
1534         while ( it.hasNext() ) {
1535           QString tmpStr;
1536           aNbItems += SMESH::GetNameOfSelectedElements(mySelector, it.next()->getIO(), tmpStr);
1537           aListStr += tmpStr;
1538         }
1539       }
1540       if (aNbItems > 0) {
1541         QListWidgetItem* anItem;
1542         QList<QListWidgetItem*> listItemsToSel;
1543         QStringList anElements = aListStr.split( " ", QString::SkipEmptyParts);
1544         for (QStringList::iterator it = anElements.begin(); it != anElements.end(); ++it) {
1545           QList<QListWidgetItem*> found = myElements->findItems(*it, Qt::MatchExactly);
1546           foreach(anItem, found)
1547             if (!anItem->isSelected())
1548               listItemsToSel.push_back(anItem);
1549         }
1550         bool blocked = myElements->signalsBlocked();
1551         myElements->blockSignals(true);
1552         foreach(anItem, listItemsToSel) anItem->setSelected(true);
1553         myElements->blockSignals(blocked);
1554         onListSelectionChanged();
1555         listItemsToSel.clear();
1556       }
1557     }
1558   }
1559   
1560   if (myActorsList.count() == 0) {
1561     if (!myGroup->_is_nil()) {
1562       SMESH_Actor* anActor = SMESH::FindActorByObject(myGroup);
1563       if ( anActor )
1564         myActorsList.append( anActor  );
1565     }
1566     else if(!myGroupOnGeom->_is_nil()) {
1567       SMESH_Actor* anActor = SMESH::FindActorByObject(myGroupOnGeom);
1568       if ( anActor )
1569         myActorsList.append( anActor );
1570     }
1571     else {
1572       SMESH_Actor* anActor = SMESH::FindActorByObject( myMesh );
1573       if ( anActor )
1574         myActorsList.append( anActor );
1575     }
1576   }
1577
1578   // somehow, if we display the mesh, while selecting from another actor,
1579   // the mesh becomes pickable, and there is no way to select any element
1580   if (myActorsList.count() > 0) {
1581     QListIterator<SMESH_Actor*> it( myActorsList );
1582     while ( it.hasNext() ) {
1583       SMESH_Actor* anActor = it.next();
1584       if ( IsActorVisible(anActor) )
1585         anActor->SetPickable(true);
1586     }
1587   }
1588
1589   myIsBusy = false;
1590 }
1591
1592 //=================================================================================
1593 // function : onSelectAll()
1594 // purpose  : Called when "Select all" is checked
1595 //=================================================================================
1596 void SMESHGUI_GroupDlg::onSelectAll()
1597 {
1598   bool noElemsModif = ( mySelectAll->isChecked() || !myAllowElemsModif->isChecked() );
1599
1600   myElementsLab->setEnabled( !noElemsModif );
1601   myElements->setEnabled   ( !noElemsModif );
1602   myFilterBtn->setEnabled  ( !noElemsModif );
1603   myAddBtn->setEnabled     ( !noElemsModif );
1604   myRemoveBtn->setEnabled  ( !noElemsModif );
1605   mySortBtn->setEnabled    ( !noElemsModif );
1606   mySelectBox->setEnabled  ( !noElemsModif );
1607   myAllowElemsModif->setEnabled( !mySelectAll->isChecked() );
1608   if ( noElemsModif ) mySMESHGUI->ResetState();
1609   else                mySMESHGUI->SetState(800);
1610
1611   int selMode     = mySelectionMode;
1612   mySelectionMode = grpNoSelection;
1613   setSelectionMode( selMode );
1614   updateButtons();
1615 }
1616
1617 //=================================================================================
1618 // function : onSelectSubMesh()
1619 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
1620 //=================================================================================
1621 void SMESHGUI_GroupDlg::onSelectSubMesh(bool on)
1622 {
1623   if (on) {
1624     if (mySelectGroup->isChecked()) {
1625       mySelectGroup->setChecked(false);
1626     }
1627     //VSR: else if (mySelectGeomGroup->isChecked()) {
1628     //VSR:   mySelectGeomGroup->setChecked(false);
1629     //VSR: }
1630     myCurrentLineEdit = mySubMeshLine;
1631     setSelectionMode(grpSubMeshSelection);
1632   }
1633   else {
1634     mySubMeshLine->setText( "" );
1635     myCurrentLineEdit = 0;
1636     if (myTypeId != -1)
1637       setSelectionMode(myTypeId);
1638   }
1639   mySubMeshBtn->setEnabled(on);
1640   mySubMeshLine->setEnabled(on);
1641 }
1642
1643
1644 //=================================================================================
1645 // function : (onSelectGroup)
1646 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
1647 //=================================================================================
1648 void SMESHGUI_GroupDlg::onSelectGroup(bool on)
1649 {
1650   if (on) {
1651     if (mySelectSubMesh->isChecked()) {
1652       mySelectSubMesh->setChecked(false);
1653     }
1654     myCurrentLineEdit = myGroupLine;
1655     setSelectionMode(grpGroupSelection);
1656   }
1657   else {
1658     myGroupLine->setText( "" );
1659     myCurrentLineEdit = 0;
1660     if (myTypeId != -1)
1661       setSelectionMode(myTypeId);
1662   }
1663   myGroupBtn->setEnabled(on);
1664   myGroupLine->setEnabled(on);
1665 }
1666
1667
1668 //=================================================================================
1669 // function : (onSelectGeomGroup)
1670 // purpose  : Called when group type changed. on == "on geometry" or "on filter"
1671 //=================================================================================
1672 void SMESHGUI_GroupDlg::onSelectGeomGroup(bool on)
1673 {
1674   if (on) {
1675     if (mySelectSubMesh->isChecked()) {
1676       mySelectSubMesh->setChecked(false);
1677     }
1678     else if (mySelectGroup->isChecked()) {
1679       mySelectGroup->setChecked(false);
1680     }
1681     if ( myGrpTypeId == 1 ) { // on geometry
1682       myCurrentLineEdit = myGeomGroupLine;
1683       updateGeomPopup();
1684     }
1685     else { // on filter
1686       myCurrentLineEdit = 0;
1687     }
1688     setSelectionMode(grpAllSelection);
1689   }
1690   else {
1691     myGeomGroupBtn->setChecked(false);
1692     myGeomObjects->length(0);
1693     myGeomGroupLine->setText( "" );
1694     myCurrentLineEdit = 0;
1695     if (myTypeId != -1)
1696       setSelectionMode( myTypeId );
1697   }
1698 }
1699
1700 //=================================================================================
1701 // function : setCurrentSelection()
1702 // purpose  :
1703 //=================================================================================
1704 void SMESHGUI_GroupDlg::setCurrentSelection()
1705 {
1706   QPushButton* send = (QPushButton*)sender();
1707   myCurrentLineEdit = 0;
1708   if (send == myMeshGroupBtn) {
1709     disconnect(myMeshGroupBtn, SIGNAL(clicked()), this, SLOT(setCurrentSelection()));
1710     mySelectionMgr->clearSelected();
1711     if (myCreate)
1712       setSelectionMode(grpMeshSelection);
1713     else
1714       setSelectionMode(grpGroupSelection);
1715     connect(myMeshGroupBtn, SIGNAL(clicked()), this, SLOT(setCurrentSelection()));
1716     myCurrentLineEdit = myMeshGroupLine;
1717     onObjectSelectionChanged();
1718   }
1719   else if (send == mySubMeshBtn) {
1720     myCurrentLineEdit = mySubMeshLine;
1721     onObjectSelectionChanged();
1722   }
1723   else if (send == myGroupBtn) {
1724     myCurrentLineEdit = myGroupLine;
1725     onObjectSelectionChanged();
1726   }
1727 }
1728
1729
1730 //=================================================================================
1731 // function : setFilters()
1732 // purpose  : SLOT. Called when "Filter" button pressed.
1733 //=================================================================================
1734 void SMESHGUI_GroupDlg::setFilters()
1735 {
1736   if(myMesh->_is_nil()) {
1737     SUIT_MessageBox::critical(this,
1738                               tr("SMESH_ERROR"),
1739                               tr("NO_MESH_SELECTED"));
1740    return;
1741   }
1742
1743   SMESH::ElementType aType = SMESH::ALL;
1744   switch ( myTypeId )
1745   {
1746     case grpNodeSelection:   aType = SMESH::NODE;   break;
1747     case grp0DSelection:     aType = SMESH::ELEM0D; break;
1748     case grpBallSelection:   aType = SMESH::BALL;   break;
1749     case grpEdgeSelection:   aType = SMESH::EDGE;   break;
1750     case grpFaceSelection:   aType = SMESH::FACE;   break;
1751     case grpVolumeSelection: aType = SMESH::VOLUME; break;
1752     default:                 return;
1753   }
1754
1755   if ( myFilterDlg == 0 )
1756   {
1757     myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, aType );
1758     connect( myFilterDlg, SIGNAL( Accepted() ), SLOT( onFilterAccepted() ) );
1759   }
1760   else
1761     myFilterDlg->Init( aType );
1762
1763   if ( !myGroupOnFilter->_is_nil() )
1764   {
1765     myFilterDlg->SetFilter( myFilter, aType );
1766     myFilterDlg->Init( aType );
1767   }
1768
1769   bool isStandalone = ( sender() == myFilterBtn );
1770   myFilterDlg->SetEnabled( /*setInViewer=*/isStandalone,
1771                            /*diffSources=*/isStandalone );
1772   myFilterDlg->SetMesh( myMesh );
1773   myFilterDlg->SetGroup( myGroupOnFilter );
1774   myFilterDlg->SetSelection();
1775   myFilterDlg->SetSourceWg( myElements, false );
1776
1777   myFilterDlg->show();
1778 }
1779
1780 //=================================================================================
1781 // function : onFilterAccepted()
1782 // purpose  : SLOT. Called when Filter dlg closed with OK button.
1783 //            Uncheck "Select submesh" and "Select group" checkboxes
1784 //=================================================================================
1785 void SMESHGUI_GroupDlg::onFilterAccepted()
1786 {
1787   if ( mySelectSubMesh->isChecked() || mySelectGroup->isChecked() )
1788   {
1789     mySelectionMode = myTypeId;
1790     mySelectSubMesh->setChecked( false );
1791     mySelectGroup->setChecked( false );
1792   }
1793   // get a filter from myFilterDlg
1794   myFilter = myFilterDlg->GetFilter();
1795   if ( !myFilter->_is_nil() ) {
1796     SMESH::Predicate_var perdicate = myFilter->GetPredicate();
1797     if ( perdicate->_is_nil() )
1798       myFilter = SMESH::Filter::_nil();
1799   }
1800   // set mesh to myFilter
1801   if ( !myFilter->_is_nil() ) {
1802     SMESH::SMESH_Mesh_var mesh = myMesh;
1803     if ( mesh->_is_nil() ) {
1804       if ( !myGroup->_is_nil() )
1805         mesh = myGroup->GetMesh();
1806       else if ( !myGroupOnGeom->_is_nil() )
1807         mesh = myGroupOnGeom->GetMesh();
1808       else if ( !myGroupOnFilter->_is_nil() )
1809         mesh = myGroupOnFilter->GetMesh();
1810     }
1811     myFilter->SetMesh( mesh );
1812
1813     // highlight ids if selection changed in the Viewer (IPAL52924)
1814     myCurrentLineEdit = 0;
1815     onObjectSelectionChanged();
1816   }
1817
1818   updateButtons();
1819 }
1820
1821 //=================================================================================
1822 // function : onAdd()
1823 // purpose  :
1824 //=================================================================================
1825 void SMESHGUI_GroupDlg::onAdd()
1826 {
1827   SALOME_ListIO aList;
1828   mySelectionMgr->selectedObjects( aList );
1829
1830   int aNbSel = aList.Extent();
1831
1832   if (aNbSel == 0 || myActorsList.count() == 0 || myMesh->_is_nil()) return;
1833
1834   SUIT_OverrideCursor wc;
1835
1836   myIsBusy = true;
1837   int sizeBefore = myElements->count();
1838
1839   SMESH::ElementType aType = SMESH::ALL;
1840   switch(myTypeId) {
1841   case grpNodeSelection:
1842     aType = SMESH::NODE;
1843     mySelector->SetSelectionMode(NodeSelection);
1844     break;
1845   case grpBallSelection:
1846     aType = SMESH::BALL;
1847     mySelector->SetSelectionMode(BallSelection);
1848     break;
1849   case grp0DSelection:
1850     aType = SMESH::ELEM0D;
1851     mySelector->SetSelectionMode(Elem0DSelection);
1852     break;
1853   case grpEdgeSelection:
1854     aType = SMESH::EDGE;
1855     mySelector->SetSelectionMode(EdgeSelection);
1856     break;
1857   case grpFaceSelection:
1858     aType = SMESH::FACE;
1859     mySelector->SetSelectionMode(FaceSelection);
1860     break;
1861   case grpVolumeSelection:
1862     aType = SMESH::VOLUME;
1863     mySelector->SetSelectionMode(VolumeSelection);
1864     break;
1865   default:
1866     mySelector->SetSelectionMode(ActorSelection);
1867   }
1868
1869   QListWidgetItem* anItem = 0;
1870   QList<QListWidgetItem*> listItemsToSel;
1871
1872   if ( myCurrentLineEdit == 0 )
1873   {
1874     //if (aNbSel != 1) { myIsBusy = false; return; }
1875     QString aListStr = "";
1876     int aNbItems = 0;
1877     if (myTypeId == 0) {
1878       QListIterator<SMESH_Actor*> it( myActorsList );
1879       while ( it.hasNext() ) {
1880         QString tmpStr;
1881         aNbItems += SMESH::GetNameOfSelectedNodes(mySelector, it.next()->getIO(), tmpStr);
1882         aListStr += tmpStr;
1883       }
1884     }
1885     else {
1886       QListIterator<SMESH_Actor*> it( myActorsList );
1887       while ( it.hasNext() ) {
1888         QString tmpStr;
1889         aNbItems += SMESH::GetNameOfSelectedElements(mySelector, it.next()->getIO(), tmpStr);
1890         aListStr += tmpStr;
1891       }
1892     }
1893     if (aNbItems > 0) {
1894       QStringList anElements = aListStr.split( " ", QString::SkipEmptyParts);
1895       for (QStringList::iterator it = anElements.begin(); it != anElements.end(); ++it) {
1896         QList<QListWidgetItem*> found = myElements->findItems(*it, Qt::MatchExactly);
1897         if (found.count() == 0) {
1898           anItem = new QListWidgetItem(*it);
1899           myElements->addItem(anItem);
1900           if (!anItem->isSelected())
1901             listItemsToSel.push_back(anItem);
1902         }
1903         else {
1904           foreach(anItem, found)
1905             if (!anItem->isSelected())
1906               listItemsToSel.push_back(anItem);
1907         }
1908       }
1909       bool blocked = myElements->signalsBlocked();
1910       myElements->blockSignals(true);
1911       foreach(anItem, listItemsToSel) anItem->setSelected(true);
1912       myElements->blockSignals(blocked);
1913       onListSelectionChanged();
1914       listItemsToSel.clear();
1915     }
1916   }
1917   else if ( myCurrentLineEdit == mySubMeshLine )
1918   {
1919     //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1920
1921     SALOME_ListIO aList;
1922     mySelectionMgr->selectedObjects( aList );
1923
1924     SALOME_ListIteratorOfListIO anIt (aList);
1925     for ( ; anIt.More(); anIt.Next()) {
1926       SMESH::SMESH_subMesh_var aSubMesh =
1927         SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIt.Value());
1928       if (!aSubMesh->_is_nil()) {
1929         // check if mesh is the same
1930         if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
1931           try {
1932             SMESH::long_array_var anElements = aSubMesh->GetElementsByType(aType);
1933             int k = anElements->length();
1934             for (int i = 0; i < k; i++) {
1935               QString aText = QString::number(anElements[i]);
1936               QList<QListWidgetItem*> found = myElements->findItems(aText, Qt::MatchExactly);
1937               if (found.count() == 0) {
1938                 anItem = new QListWidgetItem(aText);
1939                 myElements->addItem(anItem);
1940                 if (!anItem->isSelected())
1941                   listItemsToSel.push_back(anItem);
1942               }
1943               else {
1944                 foreach(anItem, found)
1945                   if (!anItem->isSelected())
1946                     listItemsToSel.push_back(anItem);
1947               }
1948             }
1949             bool blocked = myElements->signalsBlocked();
1950             myElements->blockSignals(true);
1951             foreach(anItem, listItemsToSel) anItem->setSelected(true);
1952             myElements->blockSignals(blocked);
1953             onListSelectionChanged();
1954             listItemsToSel.clear();
1955           }
1956           catch (const SALOME::SALOME_Exception& ex) {
1957             SalomeApp_Tools::QtCatchCorbaException(ex);
1958           }
1959         }
1960       }
1961     }
1962     mySelectSubMesh->setChecked(false);
1963     myIsBusy = false;
1964     onListSelectionChanged();
1965
1966   }
1967   else if ( myCurrentLineEdit == myGroupLine )
1968   {
1969     //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1970     SALOME_ListIO aList;
1971     mySelectionMgr->selectedObjects( aList );
1972
1973     SALOME_ListIteratorOfListIO anIt (aList);
1974     for ( ; anIt.More(); anIt.Next()) {
1975       SMESH::SMESH_GroupBase_var aGroup =
1976         SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(anIt.Value());
1977       if (!aGroup->_is_nil()) {
1978         // check if mesh is the same
1979         if (aGroup->GetType() == aType && aGroup->GetMesh()->GetId() == myMesh->GetId()) {
1980           SMESH::long_array_var anElements = aGroup->GetListOfID();
1981           int k = anElements->length();
1982           for (int i = 0; i < k; i++) {
1983             QString aText = QString::number(anElements[i]);
1984             QList<QListWidgetItem*> found = myElements->findItems(aText, Qt::MatchExactly);
1985             if (found.count() == 0) {
1986               anItem = new QListWidgetItem(aText);
1987               myElements->addItem(anItem);
1988               if (!anItem->isSelected())
1989                 listItemsToSel.push_back(anItem);
1990             }
1991             else {
1992               foreach(anItem, found)
1993                 if (!anItem->isSelected())
1994                   listItemsToSel.push_back(anItem);
1995             }
1996           }
1997           bool blocked = myElements->signalsBlocked();
1998           myElements->blockSignals(true);
1999           foreach(anItem, listItemsToSel) anItem->setSelected(true);
2000           myElements->blockSignals(blocked);
2001           onListSelectionChanged();
2002           listItemsToSel.clear();
2003         }
2004       }
2005     }
2006     mySelectGroup->setChecked(false);
2007     myIsBusy = false;
2008     onListSelectionChanged();
2009
2010   }
2011   else if ( myCurrentLineEdit == myGeomGroupLine && myGeomObjects->length() == 1 )
2012   {
2013     GEOM::GEOM_IGroupOperations_ptr aGroupOp =
2014       SMESH::GetGEOMGen( myGeomObjects[0] )->GetIGroupOperations();
2015
2016     SMESH::ElementType aGroupType = SMESH::ALL;
2017     switch(aGroupOp->GetType(myGeomObjects[0])) {
2018     case TopAbs_VERTEX: aGroupType = SMESH::NODE; break;
2019     case TopAbs_EDGE:   aGroupType = SMESH::EDGE; break;
2020     case TopAbs_FACE:   aGroupType = SMESH::FACE; break;
2021     case TopAbs_SOLID:  aGroupType = SMESH::VOLUME; break;
2022     default: myIsBusy = false; return;
2023     }
2024
2025     if (aGroupType == aType) {
2026       _PTR(SObject) aGroupSO =
2027         //SMESH::getStudy()->FindObjectIOR(SMESH::getStudy()->ConvertObjectToIOR(myGeomGroup));
2028         SMESH::getStudy()->FindObjectID(myGeomObjects[0]->GetStudyEntry());
2029       // Construct filter
2030       SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
2031       SMESH::Filter_var aFilter = aFilterMgr->CreateFilter();
2032       SMESH::BelongToGeom_var aBelongToGeom = aFilterMgr->CreateBelongToGeom();
2033       aBelongToGeom->SetGeom(myGeomObjects[0]);
2034       aBelongToGeom->SetShapeName(aGroupSO->GetName().c_str());
2035       aBelongToGeom->SetElementType(aType);
2036       aFilter->SetPredicate(aBelongToGeom);
2037
2038       SMESH::long_array_var anElements = aFilter->GetElementsId(myMesh);
2039
2040       int k = anElements->length();
2041       for (int i = 0; i < k; i++) {
2042         QString aText = QString::number(anElements[i]);
2043         QList<QListWidgetItem*> found = myElements->findItems(aText, Qt::MatchExactly);
2044         if (found.count() == 0) {
2045           anItem = new QListWidgetItem(aText);
2046           myElements->addItem(anItem);
2047           if (!anItem->isSelected())
2048             listItemsToSel.push_back(anItem);
2049         }
2050         else {
2051           foreach(anItem, found)
2052             if (!anItem->isSelected())
2053               listItemsToSel.push_back(anItem);
2054         }
2055       }
2056       bool blocked = myElements->signalsBlocked();
2057       myElements->blockSignals(true);
2058       foreach(anItem, listItemsToSel) anItem->setSelected(true);
2059       myElements->blockSignals(blocked);
2060       onListSelectionChanged();
2061       listItemsToSel.clear();
2062     }
2063
2064     //VSR: mySelectGeomGroup->setChecked(false);
2065     myIsBusy = false;
2066     onListSelectionChanged();
2067   }
2068   myIsBusy = false;
2069   if ( sizeBefore < myElements->count() )
2070     ++myNbChangesOfContents;
2071   //  mySelectionMgr->clearSelected();
2072   updateButtons();
2073 }
2074
2075 //=================================================================================
2076 // function : onRemove()
2077 // purpose  :
2078 //=================================================================================
2079 void SMESHGUI_GroupDlg::onRemove()
2080 {
2081   myIsBusy = true;
2082   int sizeBefore = myElements->count();
2083
2084   if (myCurrentLineEdit == 0) {
2085     QList<QListWidgetItem*> selItems = myElements->selectedItems();
2086     QListWidgetItem* item;
2087     foreach(item, selItems) delete item;
2088   } else {
2089     SALOME_ListIO aList;
2090     mySelectionMgr->selectedObjects( aList );
2091
2092     int aNbSel = aList.Extent();
2093
2094     if (aNbSel == 0) { myIsBusy = false; return; }
2095
2096     SMESH::ElementType aType = SMESH::ALL;
2097     switch(myTypeId) {
2098     case grpNodeSelection:   aType = SMESH::NODE;   break;
2099     case grp0DSelection:     aType = SMESH::ELEM0D; break;
2100     case grpBallSelection:   aType = SMESH::BALL;   break;
2101     case grpEdgeSelection:   aType = SMESH::EDGE;   break;
2102     case grpFaceSelection:   aType = SMESH::FACE;   break;
2103     case grpVolumeSelection: aType = SMESH::VOLUME; break;
2104     }
2105
2106     if (myCurrentLineEdit == mySubMeshLine) {
2107       //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
2108       SALOME_ListIO aList;
2109       mySelectionMgr->selectedObjects( aList );
2110
2111       SALOME_ListIteratorOfListIO anIt (aList);
2112       for ( ; anIt.More(); anIt.Next()) {
2113         SMESH::SMESH_subMesh_var aSubMesh = SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIt.Value());
2114         if (!aSubMesh->_is_nil()) {
2115           // check if mesh is the same
2116           if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
2117             if (aType == SMESH::NODE) {
2118               try {
2119                 SMESH::long_array_var anElements = aSubMesh->GetNodesId();
2120                 int k = anElements->length();
2121                 for (int i = 0; i < k; i++) {
2122                   QList<QListWidgetItem*> found = 
2123                     myElements->findItems(QString::number(anElements[i]), Qt::MatchExactly);
2124                   QListWidgetItem* anItem;
2125                   foreach(anItem, found) delete anItem;
2126                 }
2127               }
2128               catch (const SALOME::SALOME_Exception& ex) {
2129                 SalomeApp_Tools::QtCatchCorbaException(ex);
2130               }
2131             }
2132             else {
2133               try {
2134                 SMESH::long_array_var anElements = aSubMesh->GetElementsId();
2135                 int k = anElements->length();
2136                 for (int i = 0; i < k; i++) {
2137                   QList<QListWidgetItem*> found = 
2138                     myElements->findItems(QString::number(anElements[i]), Qt::MatchExactly);
2139                   QListWidgetItem* anItem;
2140                   foreach(anItem, found) delete anItem;
2141                 }
2142               }
2143               catch (const SALOME::SALOME_Exception& ex) {
2144                 SalomeApp_Tools::QtCatchCorbaException(ex);
2145               }
2146             }
2147           }
2148         }
2149       }
2150     }
2151     else if (myCurrentLineEdit == myGroupLine) {
2152       SALOME_ListIO aList;
2153       mySelectionMgr->selectedObjects( aList );
2154
2155       SALOME_ListIteratorOfListIO anIt (aList);
2156       for ( ; anIt.More(); anIt.Next()) {
2157         SMESH::SMESH_Group_var aGroup = SMESH::IObjectToInterface<SMESH::SMESH_Group>(anIt.Value());
2158         if (!aGroup->_is_nil()) {
2159           // check if mesh is the same
2160           if (aGroup->GetType() == aType && aGroup->GetMesh()->GetId() == myMesh->GetId()) {
2161             SMESH::long_array_var anElements = aGroup->GetListOfID();
2162             int k = anElements->length();
2163             for (int i = 0; i < k; i++) {
2164               QList<QListWidgetItem*> found = 
2165                 myElements->findItems(QString::number(anElements[i]), Qt::MatchExactly);
2166               QListWidgetItem* anItem;
2167               foreach(anItem, found) delete anItem;
2168             }
2169           }
2170         }
2171       }
2172     }
2173   }
2174   myIsBusy = false;
2175   if ( sizeBefore > myElements->count() )
2176     myNbChangesOfContents += 2; // it's used to detect that "Add" was only once
2177   updateButtons();
2178 }
2179
2180 //=================================================================================
2181 // function : onSort()
2182 // purpose  :
2183 //=================================================================================
2184 void SMESHGUI_GroupDlg::onSort()
2185 {
2186   // PAL5412: sorts items in ascending by "string" value
2187   // myElements->sort(true);
2188   // myElements->update();
2189   int i, k = myElements->count();
2190   if (k > 0) {
2191     myIsBusy = true;
2192     QList<int> aSelected;
2193     std::vector<int> anArray(k);
2194     //    QMemArray<int> anArray(k);
2195     // fill the array
2196     for (i = 0; i < k; i++) {
2197       int id = myElements->item(i)->text().toInt();
2198       anArray[i] = id;
2199       if (myElements->item(i)->isSelected())
2200         aSelected.append(id);
2201     }
2202     // sort & update list
2203     std::sort(anArray.begin(), anArray.end());
2204     //    anArray.sort();
2205     myElements->clear();
2206     QListWidgetItem* anItem;
2207     QList<QListWidgetItem*> listItemsToSel;
2208     for (i = 0; i < k; i++) {
2209       anItem = new QListWidgetItem(QString::number(anArray[i]));
2210       myElements->addItem(anItem);
2211       if (aSelected.contains(anArray[i]))
2212         listItemsToSel.push_back(anItem);
2213     }
2214     bool blocked = myElements->signalsBlocked();
2215     myElements->blockSignals(true);
2216     foreach(anItem, listItemsToSel) anItem->setSelected(true);
2217     myElements->blockSignals(blocked);
2218     listItemsToSel.clear();
2219     myIsBusy = false;
2220   }
2221 }
2222
2223 //=================================================================================
2224 // function : onVisibilityChanged()
2225 // purpose  :
2226 //=================================================================================
2227 void SMESHGUI_GroupDlg::onVisibilityChanged()
2228 {
2229   SetAppropriateActor();
2230 }
2231
2232 //=================================================================================
2233 // function : SMESHGUI_GroupDlg::reject
2234 // purpose  : SLOT called when "Close" button pressed. Close dialog
2235 //=================================================================================
2236 void SMESHGUI_GroupDlg::reject()
2237 {
2238   if (SMESH::GetCurrentVtkView()) {
2239     SMESH::RemoveFilters(); // PAL6938 -- clean all mesh entity filters
2240     SMESH::SetPointRepresentation(false);
2241     SMESH::SetPickable();
2242     restoreShowEntityMode();
2243   }
2244
2245   if( isApplyAndClose() && !myObjectToSelect.isEmpty() ) {
2246     SUIT_DataOwnerPtrList aList;
2247     aList.append( new LightApp_DataOwner( myObjectToSelect ) );
2248     mySelectionMgr->setSelected( aList );
2249   }
2250   else
2251     mySelectionMgr->clearSelected();
2252   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
2253     aViewWindow->SetSelectionMode(ActorSelection);
2254   mySelectionMgr->clearFilters();
2255   mySMESHGUI->ResetState();
2256
2257   QDialog::reject();
2258
2259   if ( myFilterDlg ) myFilterDlg->UnRegisterFilters();
2260 }
2261
2262 //=================================================================================
2263 // function : onOpenView()
2264 // purpose  :
2265 //=================================================================================
2266 void SMESHGUI_GroupDlg::onOpenView()
2267 {
2268   if ( mySelector ) {
2269     SMESH::SetPointRepresentation(false);
2270   }
2271   else {
2272     mySelector = SMESH::GetViewWindow( mySMESHGUI )->GetSelector();
2273     mySMESHGUI->EmitSignalDeactivateDialog();
2274     setEnabled(true);
2275   }
2276 }
2277
2278 //=================================================================================
2279 // function : onCloseView()
2280 // purpose  :
2281 //=================================================================================
2282 void SMESHGUI_GroupDlg::onCloseView()
2283 {
2284   onDeactivate();
2285   mySelector = 0;
2286 }
2287
2288 //=================================================================================
2289 // function : onHelp()
2290 // purpose  :
2291 //=================================================================================
2292 void SMESHGUI_GroupDlg::onHelp()
2293 {
2294   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
2295   if (app)
2296   {
2297     app->onHelpContextModule
2298       ( mySMESHGUI ? app->moduleName( mySMESHGUI->moduleName() ) : QString(""), myHelpFileName );
2299   }
2300   else
2301   {
2302 #ifdef WIN32
2303     QString platform = "winapplication";
2304 #else
2305     QString platform = "application";
2306 #endif
2307     SUIT_MessageBox::warning(this, tr( "WRN_WARNING" ),
2308                              tr( "EXTERNAL_BROWSER_CANNOT_SHOW_PAGE" ).
2309                              arg(app->resourceMgr()->stringValue( "ExternalBrowser", platform)).
2310                              arg(myHelpFileName));
2311   }
2312 }
2313
2314 //=================================================================================
2315 // function : SMESHGUI_GroupDlg::onDeactivate
2316 // purpose  : SLOT called when dialog must be deactivated
2317 //=================================================================================
2318 void SMESHGUI_GroupDlg::onDeactivate()
2319 {
2320   mySMESHGUI->ResetState();
2321   setEnabled(false);
2322 }
2323
2324 //=================================================================================
2325 // function : SMESHGUI_GroupDlg::enterEvent
2326 // purpose  : Event filter
2327 //=================================================================================
2328 void SMESHGUI_GroupDlg::enterEvent (QEvent*)
2329 {
2330   if (!isEnabled()) {
2331     SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI );
2332     if ( aViewWindow && !mySelector) {
2333       mySelector = aViewWindow->GetSelector();
2334     }
2335     mySMESHGUI->EmitSignalDeactivateDialog();
2336     setEnabled(true);
2337     mySelectionMode = grpNoSelection;
2338     setSelectionMode(myTypeId);
2339     mySMESHGUI->SetActiveDialogBox(this);
2340     if ( mySelectBox->isEnabled() ) mySMESHGUI->SetState(800);
2341     else                            mySMESHGUI->ResetState();
2342   }
2343 }
2344
2345 //=================================================================================
2346 // function : keyPressEvent()
2347 // purpose  :
2348 //=================================================================================
2349 void SMESHGUI_GroupDlg::keyPressEvent( QKeyEvent* e )
2350 {
2351   QDialog::keyPressEvent( e );
2352   if ( e->isAccepted() )
2353     return;
2354
2355   if ( e->key() == Qt::Key_F1 )
2356   {
2357     e->accept();
2358     onHelp();
2359   }
2360 }
2361
2362 //================================================================================
2363 /*!
2364  * \brief Enable showing of the popup when Geometry selection btn is clicked
2365  * \param enable - true to enable
2366  */
2367 //================================================================================
2368
2369 enum { DIRECT_GEOM_INDEX = 0, GEOM_BY_MESH_INDEX };
2370
2371 void SMESHGUI_GroupDlg::updateGeomPopup()
2372 {
2373   bool enable = false;
2374
2375   if ( !myMesh->_is_nil() )
2376     enable = myMesh->NbEdges() > 0;
2377
2378   if ( myGeomGroupBtn )
2379   {
2380     disconnect( myGeomGroupBtn, SIGNAL( toggled(bool) ), this, SLOT( onGeomSelectionButton(bool) ));
2381     if ( enable ) {
2382       if ( !myGeomPopup ) {
2383         myGeomPopup = new QMenu(this);
2384         myActions[myGeomPopup->addAction( tr( "DIRECT_GEOM_SELECTION" ) )] = DIRECT_GEOM_INDEX;
2385         myActions[myGeomPopup->addAction( tr( "GEOM_BY_MESH_ELEM_SELECTION" ) )] = GEOM_BY_MESH_INDEX;
2386         connect( myGeomPopup, SIGNAL( triggered( QAction* ) ), SLOT( onGeomPopup( QAction* ) ) );
2387       }
2388       connect( myGeomGroupBtn, SIGNAL( toggled(bool) ), this, SLOT( onGeomSelectionButton(bool) ));
2389     }
2390   }
2391 }
2392
2393
2394 //=================================================================================
2395 // function : onGeomSelectionButton()
2396 // purpose  :
2397 //=================================================================================
2398 void SMESHGUI_GroupDlg::onGeomSelectionButton(bool isBtnOn)
2399 {
2400   if ( myGeomPopup && isBtnOn )
2401   {
2402     myCurrentLineEdit = myGeomGroupLine;
2403     QAction* a = myGeomPopup->exec( QCursor::pos() );
2404     if (!a || myActions[a] == DIRECT_GEOM_INDEX)
2405       setSelectionMode(grpGeomSelection);
2406   }
2407   else if (!isBtnOn)
2408   {
2409     myCurrentLineEdit = 0;
2410     setSelectionMode(grpAllSelection);
2411   }
2412 }
2413
2414 //=================================================================================
2415 // function : onGeomPopup()
2416 // purpose  :
2417 //=================================================================================
2418 void SMESHGUI_GroupDlg::onGeomPopup( QAction* a )
2419 {
2420   int index = myActions[a];
2421   if ( index == GEOM_BY_MESH_INDEX )
2422   {
2423     mySelectionMode = grpNoSelection;
2424     if ( !myShapeByMeshOp ) {
2425       myShapeByMeshOp = new SMESHGUI_ShapeByMeshOp(true);
2426       connect(myShapeByMeshOp, SIGNAL(committed(SUIT_Operation*)),
2427               SLOT(onPublishShapeByMeshDlg(SUIT_Operation*)));
2428       connect(myShapeByMeshOp, SIGNAL(aborted(SUIT_Operation*)),
2429               SLOT(onCloseShapeByMeshDlg(SUIT_Operation*)));
2430     }
2431     // set mesh object to SMESHGUI_ShapeByMeshOp and start it
2432     if ( !myMesh->_is_nil() ) {
2433       myIsBusy = true;
2434       hide(); // stop processing selection
2435       myIsBusy = false;
2436       myShapeByMeshOp->setModule( mySMESHGUI );
2437       myShapeByMeshOp->setStudy( 0 ); // it's really necessary
2438       myShapeByMeshOp->SetMesh( myMesh );
2439       myShapeByMeshOp->start();
2440     }
2441   }
2442 }
2443
2444 //================================================================================
2445 /*!
2446  * \brief SLOT. Is called when Ok is pressed in SMESHGUI_ShapeByMeshDlg
2447  */
2448 //================================================================================
2449
2450 void SMESHGUI_GroupDlg::onPublishShapeByMeshDlg(SUIT_Operation* op)
2451 {
2452   if ( myShapeByMeshOp == op ) {
2453     mySMESHGUI->getApp()->updateObjectBrowser();
2454     show();
2455     // Select a found geometry object
2456     GEOM::GEOM_Object_var aGeomVar = myShapeByMeshOp->GetShape();
2457     if ( !aGeomVar->_is_nil() )
2458     {
2459       QString ID = aGeomVar->GetStudyEntry();
2460       if ( _PTR(SObject) aGeomSO = SMESH::getStudy()->FindObjectID( ID.toLatin1().data() )) {
2461         SALOME_ListIO anIOList;
2462         Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject
2463           ( aGeomSO->GetID().c_str(), "SMESH", aGeomSO->GetName().c_str() );
2464         anIOList.Append( anIO );
2465         mySelectionMgr->setSelectedObjects( anIOList, false );
2466         onObjectSelectionChanged();
2467       }
2468     }
2469   }
2470 }
2471
2472 //================================================================================
2473 /*!
2474  * \brief SLOT. Is called when Close is pressed in SMESHGUI_ShapeByMeshDlg
2475  */
2476 //================================================================================
2477
2478 void SMESHGUI_GroupDlg::onCloseShapeByMeshDlg(SUIT_Operation* op)
2479 {
2480   if ( myShapeByMeshOp == op )
2481   {
2482     show();
2483     setSelectionMode(grpGeomSelection);
2484   }
2485 }
2486
2487 //=================================================================================
2488 // function : setGroupColor()
2489 // purpose  :
2490 //=================================================================================
2491 void SMESHGUI_GroupDlg::setGroupColor( const SALOMEDS::Color& theColor )
2492 {
2493   QColor aQColor( (int)( theColor.R * 255.0 ),
2494                   (int)( theColor.G * 255.0 ),
2495                   (int)( theColor.B * 255.0 ) );
2496   setGroupQColor( aQColor );
2497 }
2498
2499 //=================================================================================
2500 // function : getGroupColor()
2501 // purpose  :
2502 //=================================================================================
2503 SALOMEDS::Color SMESHGUI_GroupDlg::getGroupColor() const
2504 {
2505   QColor aQColor = getGroupQColor();
2506
2507   SALOMEDS::Color aColor;
2508   aColor.R = (float)aQColor.red() / 255.0;
2509   aColor.G = (float)aQColor.green() / 255.0;
2510   aColor.B = (float)aQColor.blue() / 255.0;
2511
2512   return aColor;
2513 }
2514
2515 //=================================================================================
2516 // function : setGroupQColor()
2517 // purpose  :
2518 //=================================================================================
2519 void SMESHGUI_GroupDlg::setGroupQColor( const QColor& theColor )
2520 {
2521   if( theColor.isValid() )
2522     myColorBtn->setColor( theColor );
2523 }
2524
2525 //=================================================================================
2526 // function : getGroupQColor()
2527 // purpose  :
2528 //=================================================================================
2529 QColor SMESHGUI_GroupDlg::getGroupQColor() const
2530 {
2531   return myColorBtn->color();
2532 }
2533
2534 //=================================================================================
2535 // function : setDefaultGroupColor()
2536 // purpose  :
2537 //=================================================================================
2538 void SMESHGUI_GroupDlg::setDefaultGroupColor()
2539 {
2540   if( myMesh->_is_nil() )
2541     return;
2542
2543   bool isAutoColor = myMesh->GetAutoColor();
2544
2545   QColor aQColor = myColorBtn->color();
2546   if( !isAutoColor )
2547   {
2548     if ( !aQColor.isValid() ) {
2549       int r = 0, g = 0, b = 0;
2550       SMESH::GetColor( "SMESH", "default_grp_color", r, g, b, QColor( 255, 170, 0 ) );
2551       aQColor.setRgb( r, g, b );
2552     }
2553   }
2554   else
2555   {
2556 #ifdef SIMPLE_AUTOCOLOR   // simplified algorithm for auto-colors
2557     SALOMEDS::Color aColor = SMESHGUI::getPredefinedUniqueColor();
2558 #else                     // old algorithm  for auto-colors
2559     SMESH::ListOfGroups aListOfGroups = *myMesh->GetGroups();
2560
2561     QList<SALOMEDS::Color> aReservedColors;
2562     for( int i = 0, n = aListOfGroups.length(); i < n; i++ )
2563     {
2564       SMESH::SMESH_GroupBase_var aGroupObject = aListOfGroups[i];
2565       SALOMEDS::Color aReservedColor = aGroupObject->GetColor();
2566       aReservedColors.append( aReservedColor );
2567     }
2568
2569     SALOMEDS::Color aColor = SMESHGUI::getUniqueColor( aReservedColors );
2570 #endif                    // SIMPLE_AUTOCOLOR
2571
2572     aQColor.setRgb( (int)( aColor.R * 255.0 ),
2573                     (int)( aColor.G * 255.0 ),
2574                     (int)( aColor.B * 255.0 ) );
2575
2576   }
2577
2578   setGroupQColor( aQColor );
2579 }
2580
2581 //=================================================================================
2582 // function : SetAppropriateActor()
2583 // purpose  : Find more appropriate of visible actors, set it to myActor, allow picking
2584 //            NPAL19389: create a group with a selection in another group.
2585 //            if mesh actor is not visible - find any first visible group or sub-mesh
2586 //=================================================================================
2587 bool SMESHGUI_GroupDlg::SetAppropriateActor()
2588 {
2589   bool isActor = false;
2590   myActorsList.clear();
2591
2592   if (myMesh->_is_nil()) return false;
2593
2594   SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView();
2595
2596   if (myGrpTypeGroup->checkedId() > 0) {   // try current group on geometry actor
2597     SMESH_Actor* anActor = 0;
2598     if (!myGroupOnGeom->_is_nil())
2599       anActor = SMESH::FindActorByObject(myGroupOnGeom);
2600     if (!myGroupOnFilter->_is_nil())
2601       anActor = SMESH::FindActorByObject(myGroupOnFilter);
2602     if (anActor && anActor->hasIO())
2603     {
2604       isActor = true;
2605       if (aViewWindow && !aViewWindow->isVisible(anActor->getIO()))
2606         isActor = false;
2607       else
2608         myActorsList.append(anActor);
2609     }
2610     return anActor;
2611   }
2612   else {
2613     // try mesh actor
2614     SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh);
2615     if (anActor && anActor->hasIO()) {
2616       isActor = true;
2617       if (aViewWindow && !aViewWindow->isVisible(anActor->getIO()))
2618         isActor = false;
2619       else
2620         myActorsList.append(anActor);
2621     }
2622
2623     // try group actor
2624     SMESH_Actor* aGroupActor = 0;
2625     if (!isActor && !myGroup->_is_nil()) {
2626       aGroupActor = SMESH::FindActorByObject(myGroup);
2627       if (aGroupActor && aGroupActor->hasIO())
2628         myActorsList.append(aGroupActor);
2629     }
2630
2631     // try any visible actor of group or sub-mesh of current mesh
2632     if (aViewWindow) {
2633       // mesh entry
2634       _PTR(SObject) aSObject = SMESH::FindSObject(myMesh);
2635       if (aSObject) {
2636         CORBA::String_var meshEntry = aSObject->GetID().c_str();
2637         int len = strlen(meshEntry);
2638
2639         // iterate on all actors in current view window, search for
2640         // any visible actor, that belongs to group or submesh of current mesh
2641         VTK::ActorCollectionCopy aCopy(aViewWindow->getRenderer()->GetActors());
2642         vtkActorCollection *aCollection = aCopy.GetActors();
2643         int nbItems = aCollection->GetNumberOfItems();
2644         for (int i=0; i<nbItems && !isActor; i++)
2645         {
2646           SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(aCollection->GetItemAsObject(i));
2647           if (anActor && anActor->hasIO()) {
2648             Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
2649             if (aViewWindow->isVisible(anIO)) {
2650               if (anIO->hasEntry() && strncmp(anIO->getEntry(), meshEntry, len) == 0 && !myActorsList.contains(anActor) )
2651                 myActorsList.append(anActor);
2652             }
2653           }
2654         }
2655       }
2656     }
2657
2658     // Show a standalone group if nothing else is visible (IPAL52227)
2659     if ( myActorsList.count() == 1 &&
2660          myActorsList[0] == aGroupActor &&
2661          aViewWindow && !aViewWindow->isVisible(aGroupActor->getIO()))
2662       SMESH::UpdateView( aViewWindow, SMESH::eDisplay, aGroupActor->getIO()->getEntry() );
2663   }
2664
2665
2666   if (myActorsList.count() > 0) {
2667     QListIterator<SMESH_Actor*> it( myActorsList );
2668     while ( it.hasNext() ) {
2669       SMESH_Actor* anActor = it.next();
2670       if ( IsActorVisible(anActor) )
2671         anActor->SetPickable(true);
2672     }
2673   }
2674
2675   return ( isActor || (myActorsList.count() > 0) );
2676 }
2677
2678 //=======================================================================
2679 //function : setShowEntityMode
2680 //purpose  : make shown only entity corresponding to my type
2681 //=======================================================================
2682 void SMESHGUI_GroupDlg::setShowEntityMode()
2683 {
2684   if ( !myMesh->_is_nil() ) {
2685     if ( SMESH_Actor* actor = SMESH::FindActorByObject(myMesh) ) {
2686       if (!myStoredShownEntity)
2687         myStoredShownEntity = actor->GetEntityMode();
2688       switch ( myTypeId ) {
2689       case grpNodeSelection:   restoreShowEntityMode();                          break;
2690       case grp0DSelection:     actor->SetEntityMode( SMESH_Actor::e0DElements ); break;
2691       case grpBallSelection:   actor->SetEntityMode( SMESH_Actor::eBallElem );   break;
2692       case grpEdgeSelection:   actor->SetEntityMode( SMESH_Actor::eEdges );      break;
2693       case grpFaceSelection:   actor->SetEntityMode( SMESH_Actor::eFaces );      break;
2694       case grpVolumeSelection: actor->SetEntityMode( SMESH_Actor::eVolumes );    break;
2695       }
2696     }
2697   }
2698 }
2699
2700 //=======================================================================
2701 //function : restoreShowEntityMode
2702 //purpose  : restore ShowEntity mode of myActor
2703 //=======================================================================
2704 void SMESHGUI_GroupDlg::restoreShowEntityMode()
2705 {
2706   if ( myStoredShownEntity && !myMesh->_is_nil() ) {
2707     if ( SMESH_Actor* actor = SMESH::FindActorByObject(myMesh) ) {
2708       actor->SetEntityMode(myStoredShownEntity);
2709     }
2710   }
2711   myStoredShownEntity = 0;
2712 }
2713
2714 //=======================================================================
2715 //function : IsActorVisible
2716 //purpose  : return visibility of the actor
2717 //=======================================================================
2718 bool SMESHGUI_GroupDlg::IsActorVisible( SMESH_Actor* theActor )
2719 {
2720   SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView();
2721   if (theActor && aViewWindow)
2722     return aViewWindow->isVisible(theActor->getIO());
2723   return false;
2724 }
2725
2726 //================================================================
2727 //function : setIsApplyAndClose
2728 //purpose  : Set value of the flag indicating that the dialog is
2729 //           accepted by Apply & Close button
2730 //================================================================
2731 void SMESHGUI_GroupDlg::setIsApplyAndClose( const bool theFlag )
2732 {
2733   myIsApplyAndClose = theFlag;
2734 }
2735
2736 //================================================================
2737 //function : isApplyAndClose
2738 //purpose  : Get value of the flag indicating that the dialog is
2739 //           accepted by Apply & Close button
2740 //================================================================
2741 bool SMESHGUI_GroupDlg::isApplyAndClose() const
2742 {
2743   return myIsApplyAndClose;
2744 }