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