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