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