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