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