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