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