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