Salome HOME
IMP 22792: EDF 8159 SMESH: Multi-dimensional extrusion/extrusion along a path/revolution
[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   myOKBtn->setEnabled(enable);
734   myApplyBtn->setEnabled(enable);
735 }
736
737 //=================================================================================
738 // function : onNameChanged()
739 // purpose  :
740 //=================================================================================
741 void SMESHGUI_GroupDlg::onNameChanged (const QString& text)
742 {
743   myOldName = myName->text();
744   updateButtons();
745   myNameChanged = !myName->text().trimmed().isEmpty();
746 }
747
748 //=================================================================================
749 // function : onTypeChanged()
750 // purpose  : Group elements type radio button management
751 //=================================================================================
752 void SMESHGUI_GroupDlg::onTypeChanged (int id)
753 {
754   if (myTypeId != id) {
755     myElements->clear();
756     myTypeId = id;
757     if ( myGrpTypeId == 0 && myCurrentLineEdit == 0)
758       setSelectionMode(id);
759     else
760       setSelectionMode( mySelectionMode++ ); // update according to mySelectionMode
761
762     onObjectSelectionChanged();
763     setShowEntityMode();
764   }
765 }
766
767 //=================================================================================
768 // function : onGrpTypeChanged()
769 // purpose  : Group type radio button management
770 //=================================================================================
771 void SMESHGUI_GroupDlg::onGrpTypeChanged (int id)
772 {
773   if (myGrpTypeId != id) {
774     myGrpTypeId = id;
775     myWGStack->setCurrentIndex( id );
776     myName->blockSignals(true);
777     myName->setText(myOldName);
778     myName->blockSignals(false);
779     onSelectGeomGroup(id != 0);
780   }
781   updateButtons();
782 }
783
784 //=================================================================================
785 // function : onColorChanged()
786 // purpose  : Color button management
787 //=================================================================================
788 void SMESHGUI_GroupDlg::onColorChanged(QColor theColor)
789 {
790   updateButtons();
791 }
792
793 //=================================================================================
794 // function : setSelectionMode()
795 // purpose  : Radio button management
796 //=================================================================================
797 void SMESHGUI_GroupDlg::setSelectionMode (int theMode)
798 {
799   // PAL7314
800   if (myMesh->_is_nil())
801     return;
802   SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI );
803   bool isSelectAll = mySelectAll->isChecked() || !myAllowElemsModif->isChecked() || myGrpTypeId != 0;
804   if (mySelectionMode != theMode) {
805     // [PAL10408] mySelectionMgr->clearSelected();
806     mySelectionMgr->clearFilters();
807     SMESH::RemoveFilters();
808
809     if (myActorsList.count() > 0)
810       for (QListIterator<SMESH_Actor*> it( myActorsList ); it.hasNext(); )
811         it.next()->SetPointRepresentation(false);
812     else
813       SMESH::SetPointRepresentation(false);
814
815     switch (theMode) {
816     case grpNodeSelection:
817       if ( myGrpTypeId == 0 ) // standalone
818       {
819         if (myActorsList.count() > 0)
820           for (QListIterator<SMESH_Actor*> it( myActorsList ); it.hasNext(); )
821             it.next()->SetPointRepresentation(true);
822         else
823           SMESH::SetPointRepresentation(true);
824       }
825       if ( aViewWindow ) aViewWindow->SetSelectionMode(isSelectAll ? ActorSelection : NodeSelection);
826       break;
827     case grpEdgeSelection:
828       if ( aViewWindow ) aViewWindow->SetSelectionMode(isSelectAll ? ActorSelection : EdgeSelection);
829       break;
830     case grpBallSelection:
831       if ( aViewWindow ) aViewWindow->SetSelectionMode(isSelectAll ? ActorSelection : BallSelection);
832       break;
833     case grp0DSelection:
834       if ( aViewWindow ) aViewWindow->SetSelectionMode(isSelectAll ? ActorSelection : Elem0DSelection);
835       break;
836     case grpFaceSelection:
837       if ( aViewWindow ) aViewWindow->SetSelectionMode(isSelectAll ? ActorSelection : FaceSelection);
838       break;
839     case grpVolumeSelection:
840       if ( aViewWindow ) aViewWindow->SetSelectionMode(isSelectAll ? ActorSelection : VolumeSelection);
841       break;
842     case grpSubMeshSelection: {
843
844       SMESH_TypeFilter* f = 0;
845       switch (myTypeId) {
846       case grpNodeSelection:   f = new SMESH_TypeFilter(SMESH::SUBMESH); break;
847       case grpEdgeSelection:   f = new SMESH_TypeFilter(SMESH::SUBMESH_EDGE); break;
848       case grpFaceSelection:   f = new SMESH_TypeFilter(SMESH::SUBMESH_FACE); break;
849       case grpVolumeSelection: f = new SMESH_TypeFilter(SMESH::SUBMESH_SOLID); break;
850       default:                 f = new SMESH_TypeFilter(SMESH::SUBMESH);
851       }
852       QList<SUIT_SelectionFilter*> filtList;
853       filtList.append( f );
854       filtList.append( new SMESH_TypeFilter(SMESH::SUBMESH_COMPOUND));
855       mySubMeshFilter->setFilters( filtList );
856
857       mySelectionMgr->installFilter( mySubMeshFilter );
858
859       if ( aViewWindow ) aViewWindow->SetSelectionMode(ActorSelection);
860       break;
861     }
862     case grpGroupSelection: {
863
864       SMESH_TypeFilter* f = 0;
865       switch (myTypeId) {
866       case grpNodeSelection:   f = new SMESH_TypeFilter(SMESH::GROUP_NODE);   break;
867       case grp0DSelection:     f = new SMESH_TypeFilter(SMESH::GROUP_0D);     break;
868       case grpBallSelection:   f = new SMESH_TypeFilter(SMESH::GROUP_BALL);   break;
869       case grpEdgeSelection:   f = new SMESH_TypeFilter(SMESH::GROUP_EDGE);   break;
870       case grpFaceSelection:   f = new SMESH_TypeFilter(SMESH::GROUP_FACE);   break;
871       case grpVolumeSelection: f = new SMESH_TypeFilter(SMESH::GROUP_VOLUME); break;
872       default:                 f = new SMESH_TypeFilter(SMESH::GROUP);
873       }
874       QList<SUIT_SelectionFilter*> filtList;
875       filtList.append( f );
876       myGroupFilter->setFilters( filtList );
877
878       mySelectionMgr->installFilter(myGroupFilter);
879       if ( aViewWindow ) aViewWindow->SetSelectionMode(ActorSelection);
880       break;
881     }
882     case grpMeshSelection:
883       mySelectionMgr->installFilter(myMeshFilter);
884       if ( aViewWindow ) aViewWindow->SetSelectionMode(ActorSelection);
885       break;
886     case grpGeomSelection:
887       mySelectionMgr->installFilter(myGeomFilter);
888       if ( aViewWindow ) aViewWindow->SetSelectionMode(ActorSelection);
889       break;
890     default:
891       if ( aViewWindow ) aViewWindow->SetSelectionMode(ActorSelection);
892       break;
893     }
894     if ( aViewWindow ) aViewWindow->Repaint();
895     mySelectionMode = theMode;
896   }
897 }
898
899 //=================================================================================
900 // function : onApply()
901 // purpose  :
902 //=================================================================================
903 bool SMESHGUI_GroupDlg::onApply()
904 {
905   if (mySMESHGUI->isActiveStudyLocked())
906     return false;
907
908   if (myName->text().trimmed().isEmpty())
909     return false;
910
911   SMESH::ElementType aType = SMESH::ALL;
912   switch (myTypeId) {
913   case grpNodeSelection:   aType = SMESH::NODE;   break;
914   case grp0DSelection:     aType = SMESH::ELEM0D; break;
915   case grpBallSelection:   aType = SMESH::BALL;   break;
916   case grpEdgeSelection:   aType = SMESH::EDGE;   break;
917   case grpFaceSelection:   aType = SMESH::FACE;   break;
918   case grpVolumeSelection: aType = SMESH::VOLUME; break;
919   }
920
921   bool anIsOk = false;
922   QStringList anEntryList;
923
924   SMESH::SMESH_GroupBase_var resultGroup;
925   bool isCreation, isConversion = false;
926     
927   SUIT_OverrideCursor wc;
928
929   if (myGrpTypeId == 0)  // standalone
930   {
931     if (!mySelectAll->isChecked() && !myElements->count() && myAllowElemsModif->isChecked())
932       return false;
933
934     mySelectionMgr->clearSelected();
935
936     if (myGroup->_is_nil()) { // creation or conversion
937       // check if group on geometry is not null
938       if (!myGroupOnGeom->_is_nil() || !myGroupOnFilter->_is_nil()) {
939         if (myMesh->_is_nil())
940           return false;
941         if ( myGroupOnGeom->_is_nil() )
942           myGroup = myMesh->ConvertToStandalone( myGroupOnFilter );
943         else
944           myGroup = myMesh->ConvertToStandalone( myGroupOnGeom );
945
946         myGroupOnGeom   = SMESH::SMESH_GroupOnGeom::_nil();
947         myGroupOnFilter = SMESH::SMESH_GroupOnFilter::_nil();
948         isConversion    = true;
949       }
950     }
951
952     if (myGroup->_is_nil()) { // creation
953       if (myMesh->_is_nil())
954         return false;
955
956       myGroup = SMESH::AddGroup(myMesh, aType, myName->text());
957
958       resultGroup = SMESH::SMESH_GroupBase::_narrow( myGroup );
959       isCreation = true;
960
961       if ( mySelectAll->isChecked() ) {
962         // select all
963         myGroup->AddFrom(myMesh.in());
964       }
965       else {
966         // select manually
967
968         if ( !myFilter->_is_nil() &&
969              ( myNbChangesOfContents == 1 || !myAllowElemsModif->isChecked()))
970         {
971           myGroup->AddFrom( myFilter );
972         }
973         else
974         {
975           SMESH::long_array_var anIdList = new SMESH::long_array;
976           int i, k = myElements->count();
977           anIdList->length(k);
978           for (i = 0; i < k; i++) {
979             anIdList[i] = myElements->item(i)->text().toInt();
980           }
981           myGroup->Add(anIdList.inout());
982         }
983       }
984
985
986     } else { // edition
987
988       resultGroup = SMESH::SMESH_GroupBase::_narrow( myGroup );
989       isCreation = false;
990
991       if ( mySelectAll->isChecked() ) {
992         // select all
993         myGroup->Clear();
994         myGroup->AddFrom(myMesh.in());
995       }
996       else {
997         QList<int> aAddList;
998         
999         int i, total = myElements->count();
1000         for (i = 0; i < total; i++) {
1001           int anId = myElements->item(i)->text().toInt();
1002           int idx = myIdList.indexOf(anId);
1003           if ( idx == -1 )
1004             aAddList.append(anId);
1005           else
1006             myIdList.removeAt(idx);
1007         }
1008         if (!aAddList.empty()) {
1009           SMESH::long_array_var anIdList = new SMESH::long_array;
1010           int added = aAddList.count();
1011           anIdList->length(added);
1012           for (i = 0; i < added; i++)
1013             anIdList[i] = aAddList[i];
1014           myGroup->Add(anIdList.inout());
1015         }
1016         if (!myIdList.empty()) {
1017           SMESH::long_array_var anIdList = new SMESH::long_array;
1018           int removed = myIdList.count();
1019           anIdList->length(removed);
1020           for (i = 0; i < removed; i++)
1021             anIdList[i] = myIdList[i];
1022           myGroup->Remove(anIdList.inout());
1023         }
1024         /* init for next operation */
1025         myIdList.clear();
1026         for (i = 0; i < total; i++) {
1027           myIdList.append(myElements->item(i)->text().toInt());
1028         }
1029       }
1030     }
1031
1032     anIsOk = true;
1033   }
1034   else if (myGrpTypeId == 1) // on geom object
1035   {
1036     if (CORBA::is_nil(myGroupOnGeom)) { // creation
1037       if (myMesh->_is_nil() || !myGeomObjects->length())
1038         return false;
1039
1040       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1041
1042       if (myGeomObjects->length() == 1) {
1043         myGroupOnGeom = myMesh->CreateGroupFromGEOM(aType,
1044                                                     SMESH::toUtf8(myName->text()),
1045                                                     myGeomObjects[0]);
1046       }
1047       else {
1048         SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1049         if ( aSMESHGen->_is_nil() )
1050           return false;
1051
1052         // create a geometry group
1053         GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
1054         _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1055
1056         if (geomGen->_is_nil() || !aStudy)
1057           return false;
1058
1059         GEOM::GEOM_IGroupOperations_wrap op = geomGen->GetIGroupOperations(aStudy->StudyId());
1060         if (op->_is_nil())
1061           return false;
1062
1063         // check and add all selected GEOM objects: they must be
1064         // a sub-shapes of the main GEOM and must be of one type
1065         TopAbs_ShapeEnum aGroupType = TopAbs_SHAPE;
1066         for ( int i =0; i < myGeomObjects->length(); i++) {
1067           TopAbs_ShapeEnum aSubShapeType = (TopAbs_ShapeEnum)myGeomObjects[i]->GetShapeType();
1068           if (i == 0)
1069             aGroupType = aSubShapeType;
1070           else if (aSubShapeType != aGroupType) {
1071             aGroupType = TopAbs_SHAPE;
1072             break;
1073           }
1074         }
1075
1076         GEOM::GEOM_Object_var  aMeshShape = myMesh->GetShapeToMesh();
1077         GEOM::GEOM_Object_wrap aGroupVar = op->CreateGroup(aMeshShape, aGroupType);
1078         op->UnionList(aGroupVar, myGeomObjects);
1079
1080         if (op->IsDone()) {
1081           // publish the GEOM group in study
1082           QString aNewGeomGroupName ( "Auto_group_for_" );
1083           aNewGeomGroupName += myName->text();
1084           SALOMEDS::SObject_var aNewGroupSO =
1085             geomGen->AddInStudy(aSMESHGen->GetCurrentStudy(), aGroupVar,
1086                                 SMESH::toUtf8(aNewGeomGroupName), aMeshShape);
1087         }
1088
1089         myGroupOnGeom = myMesh->CreateGroupFromGEOM(aType,
1090                                                     SMESH::toUtf8(myName->text()),
1091                                                     aGroupVar);
1092       }
1093       resultGroup = SMESH::SMESH_GroupBase::_narrow( myGroupOnGeom );
1094       isCreation = true;
1095
1096     }
1097     else { // edition
1098
1099       resultGroup = SMESH::SMESH_GroupBase::_narrow( myGroupOnGeom );
1100       isCreation = false;
1101     }      
1102     anIsOk = true;
1103   }
1104   if (myGrpTypeId == 2) // group on filter
1105   {
1106     if ( myFilter->_is_nil() ) return false;
1107
1108     if (CORBA::is_nil(myGroupOnFilter)) { // creation
1109       if (myMesh->_is_nil())
1110         return false;
1111
1112       myGroupOnFilter = myMesh->CreateGroupFromFilter(aType,
1113                                                       SMESH::toUtf8(myName->text()),
1114                                                       myFilter);
1115
1116       resultGroup = SMESH::SMESH_GroupBase::_narrow( myGroupOnFilter );
1117       isCreation = true;
1118     }
1119     else
1120     {
1121       myGroupOnFilter->SetFilter( myFilter );
1122
1123       resultGroup = SMESH::SMESH_GroupBase::_narrow( myGroupOnFilter );
1124       isCreation = false;
1125     }
1126     anIsOk = true;
1127   }
1128
1129   if( anIsOk )
1130   {
1131     SALOMEDS::Color aColor = getGroupColor();
1132     resultGroup->SetColor(aColor);
1133
1134     _PTR(SObject) aMeshGroupSO = SMESH::FindSObject( resultGroup );
1135     if( aMeshGroupSO )
1136       anEntryList.append( aMeshGroupSO->GetID().c_str() );
1137
1138     resultGroup->SetName(SMESH::toUtf8(myName->text().trimmed()));
1139
1140     if ( isCreation )
1141     {
1142       SMESH::setFileType ( aMeshGroupSO, "COULEURGROUP" );
1143
1144       /* init for the next operation */
1145       setDefaultName();
1146       myElements->clear();
1147       myGroup         = SMESH::SMESH_Group::_nil();
1148       myGroupOnGeom   = SMESH::SMESH_GroupOnGeom::_nil();
1149       myGroupOnFilter = SMESH::SMESH_GroupOnFilter::_nil();
1150       myFilter        = SMESH::Filter::_nil();
1151
1152       setDefaultGroupColor(); // reset color for case if 'auto-color' feature is enabled.
1153     }
1154     else
1155     {
1156       if ( aMeshGroupSO )
1157       {
1158         if ( SMESH_Actor *anActor = SMESH::FindActorByEntry(aMeshGroupSO->GetID().c_str()))
1159         {
1160           Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
1161           if ( isConversion ) { // need to reset TVisualObj and actor
1162             SMESH::RemoveVisualObjectWithActors( anIO->getEntry(), true );
1163             SMESH::Update( anIO,true);
1164             myActorsList.clear();
1165             anActor = SMESH::FindActorByEntry( anIO->getEntry() );
1166             if ( !anActor ) return false;
1167             myActorsList.append( anActor );
1168           }
1169           anActor->setName(SMESH::toUtf8(myName->text()));
1170           QColor c;
1171           int delta;
1172           switch ( myTypeId ) {
1173           case grpNodeSelection:   anActor->SetNodeColor( aColor.R, aColor.G, aColor.B ); break;
1174           case grp0DSelection:     anActor->Set0DColor  ( aColor.R, aColor.G, aColor.B ); break;
1175           case grpBallSelection:   anActor->SetBallColor( aColor.R, aColor.G, aColor.B ); break;
1176           case grpEdgeSelection:   anActor->SetEdgeColor( aColor.R, aColor.G, aColor.B ); break;
1177           case grpVolumeSelection: 
1178             SMESH::GetColor("SMESH", "volume_color", c , delta, "255,0,170|-100");
1179             anActor->SetVolumeColor( aColor.R, aColor.G, aColor.B, delta ); break;          
1180             break;
1181           case grpFaceSelection:   
1182           default:
1183             SMESH::GetColor("SMESH", "fill_color", c , delta, "0,170,255|-100");
1184             anActor->SetSufaceColor( aColor.R, aColor.G, aColor.B, delta ); break;          
1185             break;
1186           }
1187           // update a visible group accoding to a changed contents
1188           if ( !isConversion && anActor->GetVisibility() )
1189             SMESH::Update( anIO, true );
1190         }
1191       }
1192     }
1193     SMESHGUI::Modified();
1194     mySMESHGUI->updateObjBrowser(true);
1195     mySelectionMgr->clearSelected();
1196
1197     if( LightApp_Application* anApp =
1198         dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
1199       myObjectToSelect = anApp->browseObjects( anEntryList, isApplyAndClose() );
1200   }
1201   return anIsOk;
1202 }
1203
1204 //=================================================================================
1205 // function : onOK()
1206 // purpose  :
1207 //=================================================================================
1208 void SMESHGUI_GroupDlg::onOK()
1209 {
1210   setIsApplyAndClose( true );
1211   if ( onApply() )
1212     reject();
1213   setIsApplyAndClose( false );
1214
1215   if ( myFilterDlg ) myFilterDlg->UnRegisterFilters();
1216 }
1217
1218 //=================================================================================
1219 // function : onListSelectionChanged()
1220 // purpose  : Called when selection in element list is changed
1221 //=================================================================================
1222 void SMESHGUI_GroupDlg::onListSelectionChanged()
1223 {
1224   //MESSAGE( "SMESHGUI_GroupDlg::onListSelectionChanged(); myActorsList.count() = " << myActorsList.count());
1225   if( myIsBusy || myActorsList.count() == 0 ) return;
1226   myIsBusy = true;
1227
1228   if (myCurrentLineEdit == 0) {
1229     mySelectionMgr->clearSelected();
1230     TColStd_MapOfInteger aIndexes;
1231     QList<QListWidgetItem*> selItems = myElements->selectedItems();
1232     QListWidgetItem* anItem;
1233     foreach(anItem, selItems) aIndexes.Add(anItem->text().toInt());
1234     mySelector->AddOrRemoveIndex(myActorsList.first()->getIO(), aIndexes, false);
1235     SALOME_ListIO aList;
1236     aList.Append(myActorsList.first()->getIO());
1237     mySelectionMgr->setSelectedObjects(aList,false);
1238   }
1239   myIsBusy = false;
1240 }
1241
1242 //=================================================================================
1243 // function : onObjectSelectionChanged()
1244 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
1245 //=================================================================================
1246 void SMESHGUI_GroupDlg::onObjectSelectionChanged()
1247 {
1248   if ( myIsBusy || !isEnabled()) return;
1249   if (myCurrentLineEdit == myGeomGroupLine && !myGeomGroupBtn->isChecked()) return;
1250
1251   myIsBusy = true;
1252
1253   SALOME_ListIO aList;
1254   mySelectionMgr->selectedObjects( aList );
1255
1256   int aNbSel = aList.Extent();
1257   myElements->clearSelection();
1258
1259   if (myCurrentLineEdit)
1260   {
1261     myCurrentLineEdit->setText( "" );
1262     QString aString = "";
1263
1264     if (myCurrentLineEdit == myMeshGroupLine)
1265     {
1266       mySelectSubMesh->setEnabled(false);
1267       mySelectGroup->setEnabled(false);
1268       myGroupLine->setText( "" );
1269       mySubMeshLine->setText( "" );
1270
1271       myGeomGroupBtn->setEnabled(false);
1272       myGeomGroupLine->setEnabled(false);
1273       myGeomGroupLine->setText( "" );
1274       myGeomObjects = new GEOM::ListOfGO();
1275       myGeomObjects->length(0);
1276
1277       if (myGeomGroupBtn->isChecked())
1278         myGeomGroupBtn->setChecked(false);
1279       if (!myCreate)
1280         myName->setText( "" );
1281
1282       myElements->clear();
1283
1284       if (aNbSel != 1 ) {
1285         myGroup = SMESH::SMESH_Group::_nil();
1286         myGroupOnGeom = SMESH::SMESH_GroupOnGeom::_nil();
1287         restoreShowEntityMode();
1288         myMesh = SMESH::SMESH_Mesh::_nil();
1289         updateGeomPopup();
1290         updateButtons();
1291         myIsBusy = false;
1292         return;
1293       }
1294       Handle(SALOME_InteractiveObject) IO = aList.First();
1295
1296       if (myCreate) {
1297         restoreShowEntityMode();
1298         myMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(IO);
1299         setShowEntityMode();
1300         updateGeomPopup();
1301         if (myMesh->_is_nil())
1302         {
1303           updateButtons();
1304           myIsBusy = false;
1305           return;
1306         }
1307
1308         if ( myFilterDlg && !myMesh->_is_nil()){
1309           myFilterDlg->SetMesh( myMesh );
1310         }
1311         myGroup = SMESH::SMESH_Group::_nil();
1312
1313         // NPAL19389: create a group with a selection in another group
1314         // set actor of myMesh, if it is visible, else try
1315         // any visible actor of group or submesh of myMesh
1316         SetAppropriateActor();
1317
1318         aString = aList.First()->getName();
1319         myMeshGroupLine->setText(aString);
1320         myMeshGroupLine->home( false );
1321
1322         mySelectSubMesh->setEnabled(true);
1323         mySelectGroup->setEnabled(true);
1324         myGeomGroupBtn->setEnabled(true);
1325         myGeomGroupLine->setEnabled(true);
1326         updateButtons();
1327       }
1328       else {
1329         SMESH::SMESH_GroupBase_var aGroup = SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(IO);
1330         if (aGroup->_is_nil())
1331         {
1332           myIsBusy = false;
1333           return;
1334         }
1335         myIsBusy = false;
1336
1337         myGroup = SMESH::SMESH_Group::_nil();
1338         myGroupOnGeom = SMESH::SMESH_GroupOnGeom::_nil();
1339
1340         init(aGroup);
1341         myIsBusy = true;
1342         mySelectSubMesh->setEnabled(true);
1343         mySelectGroup->setEnabled(true);
1344       }
1345       myCurrentLineEdit = 0;
1346       myIsBusy = false;
1347       if (!myCreate)
1348         return;
1349
1350       if (myGrpTypeId == 0)
1351       {
1352         if (myTypeId == -1)
1353           onTypeChanged(0);
1354         else
1355         {
1356           myElements->clear();
1357           setSelectionMode(myTypeId);
1358         }
1359       }
1360
1361       myIsBusy = false;
1362       return;
1363
1364     }
1365     else if (myCurrentLineEdit == myGeomGroupLine)
1366     {
1367       myGeomObjects = new GEOM::ListOfGO();
1368
1369       // The mesh SObject
1370       _PTR(SObject) aMeshSO = SMESH::FindSObject(myMesh);
1371
1372       if (aNbSel == 0 || !aMeshSO)
1373       {
1374         myGeomObjects->length(0);
1375         updateButtons();
1376         myIsBusy = false;
1377         return;
1378       }
1379
1380       myGeomObjects->length(aNbSel);
1381
1382       GEOM::GEOM_Object_var aGeomGroup;
1383       int i = 0;
1384
1385       SALOME_ListIteratorOfListIO anIt (aList);
1386       for (; anIt.More(); anIt.Next())
1387       {
1388         aGeomGroup = GEOMBase::ConvertIOinGEOMObject(anIt.Value());
1389
1390         // Check if the object is a geometry group
1391         if (CORBA::is_nil(aGeomGroup))
1392           continue;
1393
1394         // Check if group constructed on the same shape as a mesh or on its child
1395         _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1396
1397         // The main shape of the group
1398         GEOM::GEOM_Object_var aGroupMainShape;
1399         if (aGeomGroup->GetType() == 37) {
1400           GEOM::GEOM_IGroupOperations_wrap anOp =
1401             SMESH::GetGEOMGen()->GetIGroupOperations(aStudy->StudyId());
1402           aGroupMainShape = anOp->GetMainShape(aGeomGroup);
1403           // aGroupMainShape is an existing servant => GEOM_Object_var not GEOM_Object_wrap
1404         }
1405         else {
1406           aGroupMainShape = aGeomGroup;
1407           aGroupMainShape->Register();
1408         }
1409         _PTR(SObject) aGroupMainShapeSO =
1410           aStudy->FindObjectID(aGroupMainShape->GetStudyEntry());
1411
1412         _PTR(SObject) anObj, aRef;
1413         bool isRefOrSubShape = false;
1414         if (aMeshSO->FindSubObject(1, anObj) &&  anObj->ReferencedObject(aRef)) {
1415           if (aRef->GetID() == aGroupMainShapeSO->GetID()) {
1416             isRefOrSubShape = true;
1417           } else {
1418             _PTR(SObject) aFather = aGroupMainShapeSO->GetFather();
1419             _PTR(SComponent) aComponent = aGroupMainShapeSO->GetFatherComponent();
1420             while (!isRefOrSubShape && aFather->GetID() != aComponent->GetID()) {
1421               if (aRef->GetID() == aFather->GetID())
1422                 isRefOrSubShape = true;
1423               else
1424                 aFather = aFather->GetFather();
1425             }
1426           }
1427         }
1428         if (isRefOrSubShape)
1429           myGeomObjects[i++] = aGeomGroup;
1430       }
1431
1432       myGeomObjects->length(i);
1433       if ( i == 0 )
1434         {
1435           myIsBusy = false;
1436           return;
1437         }
1438
1439       aNbSel = i;
1440     }
1441
1442     if (aNbSel >= 1) {
1443       if (aNbSel > 1) {
1444         if (myCurrentLineEdit == mySubMeshLine)
1445           aString = tr( "SMESH_SUBMESH_SELECTED" ).arg(aNbSel);
1446         else if (myCurrentLineEdit == myGroupLine)
1447           aString = tr( "SMESH_GROUP_SELECTED" ).arg(aNbSel);
1448         else if (myCurrentLineEdit == myGeomGroupLine)
1449           aString = tr( "%1 Objects" ).arg(aNbSel);
1450       }
1451       else {
1452         aString = aList.First()->getName();
1453       }
1454     }
1455
1456     myCurrentLineEdit->setText(aString);
1457     myCurrentLineEdit->home(false);
1458     // 07.06.2008 skl for IPAL19574:
1459     // change name of group only if it is empty
1460     if( myName->text().trimmed().isEmpty() || !myNameChanged ) {
1461       myOldName = myName->text();
1462       myName->blockSignals(true);
1463       myName->setText(aString);
1464       myName->blockSignals(false);
1465     }
1466
1467     updateButtons();
1468   }
1469   else // !myCurrentLineEdit: local selection of nodes or elements
1470   {
1471     if (aNbSel == 1 && myActorsList.count() > 0 )
1472     {
1473       // NPAL19389: create a group with a selection in another group
1474       // Switch myActor to the newly selected one, if the last
1475       // is visible and belongs to group or submesh of myMesh
1476       /*      Handle(SALOME_InteractiveObject) curIO = myActor->getIO();
1477       Handle(SALOME_InteractiveObject) selIO = aList.First();
1478       if (curIO->hasEntry() && selIO->hasEntry()) {
1479         const char* selEntry = selIO->getEntry();
1480         if (strcmp(curIO->getEntry(), selEntry) != 0) {
1481           // different objects: selected and myActor
1482           SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView();
1483           if (aViewWindow && aViewWindow->isVisible(selIO)) {
1484             // newly selected actor is visible
1485
1486             // mesh entry
1487             _PTR(SObject) aSObject = SMESH::FindSObject(myMesh);
1488             if (aSObject) {
1489               CORBA::String_var meshEntry = aSObject->GetID().c_str();
1490               int len = strlen(meshEntry);
1491
1492               if (strncmp(selEntry, meshEntry, len) == 0) {
1493                 // selected object is myMesh or a part of it
1494                 SMESH_Actor* anActor = SMESH::FindActorByEntry(selEntry);
1495                 if (anActor) {
1496                   myActor = anActor;
1497                   SMESH::SetPickable(myActor);
1498                 }
1499               }
1500             }
1501           }
1502         }
1503       }*/
1504       // NPAL19389 END
1505
1506       QString aListStr = "";
1507       int aNbItems = 0;
1508       if (myTypeId == 0) {
1509         QListIterator<SMESH_Actor*> it( myActorsList );
1510         while ( it.hasNext() ) {
1511           QString tmpStr;
1512           aNbItems += SMESH::GetNameOfSelectedNodes(mySelector, it.next()->getIO(), tmpStr);
1513           aListStr += tmpStr;
1514         }
1515       } else {
1516         QListIterator<SMESH_Actor*> it( myActorsList );
1517         while ( it.hasNext() ) {
1518           QString tmpStr;
1519           aNbItems += SMESH::GetNameOfSelectedElements(mySelector, it.next()->getIO(), tmpStr);
1520           aListStr += tmpStr;
1521         }
1522       }
1523       if (aNbItems > 0) {
1524         QListWidgetItem* anItem;
1525         QList<QListWidgetItem*> listItemsToSel;
1526         QStringList anElements = aListStr.split( " ", QString::SkipEmptyParts);
1527         for (QStringList::iterator it = anElements.begin(); it != anElements.end(); ++it) {
1528           QList<QListWidgetItem*> found = myElements->findItems(*it, Qt::MatchExactly);
1529           foreach(anItem, found)
1530             if (!anItem->isSelected())
1531               listItemsToSel.push_back(anItem);
1532         }
1533         bool blocked = myElements->signalsBlocked();
1534         myElements->blockSignals(true);
1535         foreach(anItem, listItemsToSel) anItem->setSelected(true);
1536         myElements->blockSignals(blocked);
1537         onListSelectionChanged();
1538         listItemsToSel.clear();
1539       }
1540     }
1541   }
1542   
1543   if (myActorsList.count() == 0) {
1544     if (!myGroup->_is_nil()) {
1545       SMESH_Actor* anActor = SMESH::FindActorByObject(myGroup);
1546       if ( anActor )
1547         myActorsList.append( anActor  );
1548     }
1549     else if(!myGroupOnGeom->_is_nil()) {
1550       SMESH_Actor* anActor = SMESH::FindActorByObject(myGroupOnGeom);
1551       if ( anActor )
1552         myActorsList.append( anActor );
1553     }
1554     else {
1555       SMESH_Actor* anActor = SMESH::FindActorByObject( myMesh );
1556       if ( anActor )
1557         myActorsList.append( anActor );
1558     }
1559   }
1560
1561   // somehow, if we display the mesh, while selecting from another actor,
1562   // the mesh becomes pickable, and there is no way to select any element
1563   if (myActorsList.count() > 0) {
1564     QListIterator<SMESH_Actor*> it( myActorsList );
1565     while ( it.hasNext() ) {
1566       SMESH_Actor* anActor = it.next();
1567       if ( IsActorVisible(anActor) )
1568         anActor->SetPickable(true);
1569     }
1570   }
1571
1572   myIsBusy = false;
1573 }
1574
1575 //=================================================================================
1576 // function : onSelectAll()
1577 // purpose  : Called when "Select all" is checked
1578 //=================================================================================
1579 void SMESHGUI_GroupDlg::onSelectAll()
1580 {
1581   bool noElemsModif = ( mySelectAll->isChecked() || !myAllowElemsModif->isChecked() );
1582
1583   myElementsLab->setEnabled( !noElemsModif );
1584   myElements->setEnabled   ( !noElemsModif );
1585   myFilterBtn->setEnabled  ( !noElemsModif );
1586   myAddBtn->setEnabled     ( !noElemsModif );
1587   myRemoveBtn->setEnabled  ( !noElemsModif );
1588   mySortBtn->setEnabled    ( !noElemsModif );
1589   mySelectBox->setEnabled  ( !noElemsModif );
1590   myAllowElemsModif->setEnabled( !mySelectAll->isChecked() );
1591
1592   int selMode     = mySelectionMode;
1593   mySelectionMode = grpNoSelection;
1594   setSelectionMode( selMode );
1595   updateButtons();
1596 }
1597
1598 //=================================================================================
1599 // function : onSelectSubMesh()
1600 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
1601 //=================================================================================
1602 void SMESHGUI_GroupDlg::onSelectSubMesh(bool on)
1603 {
1604   if (on) {
1605     if (mySelectGroup->isChecked()) {
1606       mySelectGroup->setChecked(false);
1607     }
1608     //VSR: else if (mySelectGeomGroup->isChecked()) {
1609     //VSR:   mySelectGeomGroup->setChecked(false);
1610     //VSR: }
1611     myCurrentLineEdit = mySubMeshLine;
1612     setSelectionMode(grpSubMeshSelection);
1613   }
1614   else {
1615     mySubMeshLine->setText( "" );
1616     myCurrentLineEdit = 0;
1617     if (myTypeId != -1)
1618       setSelectionMode(myTypeId);
1619   }
1620   mySubMeshBtn->setEnabled(on);
1621   mySubMeshLine->setEnabled(on);
1622 }
1623
1624
1625 //=================================================================================
1626 // function : (onSelectGroup)
1627 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
1628 //=================================================================================
1629 void SMESHGUI_GroupDlg::onSelectGroup(bool on)
1630 {
1631   if (on) {
1632     if (mySelectSubMesh->isChecked()) {
1633       mySelectSubMesh->setChecked(false);
1634     }
1635     myCurrentLineEdit = myGroupLine;
1636     setSelectionMode(grpGroupSelection);
1637   }
1638   else {
1639     myGroupLine->setText( "" );
1640     myCurrentLineEdit = 0;
1641     if (myTypeId != -1)
1642       setSelectionMode(myTypeId);
1643   }
1644   myGroupBtn->setEnabled(on);
1645   myGroupLine->setEnabled(on);
1646 }
1647
1648
1649 //=================================================================================
1650 // function : (onSelectGeomGroup)
1651 // purpose  : Called when group type changed. on == "on geometry" or "on filter"
1652 //=================================================================================
1653 void SMESHGUI_GroupDlg::onSelectGeomGroup(bool on)
1654 {
1655   if (on) {
1656     if (mySelectSubMesh->isChecked()) {
1657       mySelectSubMesh->setChecked(false);
1658     }
1659     else if (mySelectGroup->isChecked()) {
1660       mySelectGroup->setChecked(false);
1661     }
1662     if ( myGrpTypeId == 1 ) { // on geometry
1663       myCurrentLineEdit = myGeomGroupLine;
1664       updateGeomPopup();
1665     }
1666     else { // on filter
1667       myCurrentLineEdit = 0;
1668     }
1669     setSelectionMode(grpAllSelection);
1670   }
1671   else {
1672     myGeomGroupBtn->setChecked(false);
1673     myGeomObjects->length(0);
1674     myGeomGroupLine->setText( "" );
1675     myCurrentLineEdit = 0;
1676     if (myTypeId != -1)
1677       setSelectionMode( myTypeId );
1678   }
1679 }
1680
1681 //=================================================================================
1682 // function : setCurrentSelection()
1683 // purpose  :
1684 //=================================================================================
1685 void SMESHGUI_GroupDlg::setCurrentSelection()
1686 {
1687   QPushButton* send = (QPushButton*)sender();
1688   myCurrentLineEdit = 0;
1689   if (send == myMeshGroupBtn) {
1690     disconnect(myMeshGroupBtn, SIGNAL(clicked()), this, SLOT(setCurrentSelection()));
1691     mySelectionMgr->clearSelected();
1692     if (myCreate)
1693       setSelectionMode(grpMeshSelection);
1694     else
1695       setSelectionMode(grpGroupSelection);
1696     connect(myMeshGroupBtn, SIGNAL(clicked()), this, SLOT(setCurrentSelection()));
1697     myCurrentLineEdit = myMeshGroupLine;
1698     onObjectSelectionChanged();
1699   }
1700   else if (send == mySubMeshBtn) {
1701     myCurrentLineEdit = mySubMeshLine;
1702     onObjectSelectionChanged();
1703   }
1704   else if (send == myGroupBtn) {
1705     myCurrentLineEdit = myGroupLine;
1706     onObjectSelectionChanged();
1707   }
1708 }
1709
1710
1711 //=================================================================================
1712 // function : setFilters()
1713 // purpose  : SLOT. Called when "Filter" button pressed.
1714 //=================================================================================
1715 void SMESHGUI_GroupDlg::setFilters()
1716 {
1717   if(myMesh->_is_nil()) {
1718     SUIT_MessageBox::critical(this,
1719                               tr("SMESH_ERROR"),
1720                               tr("NO_MESH_SELECTED"));
1721    return;
1722   }
1723
1724   SMESH::ElementType aType = SMESH::ALL;
1725   switch ( myTypeId )
1726   {
1727     case grpNodeSelection:   aType = SMESH::NODE;   break;
1728     case grp0DSelection:     aType = SMESH::ELEM0D; break;
1729     case grpBallSelection:   aType = SMESH::BALL;   break;
1730     case grpEdgeSelection:   aType = SMESH::EDGE;   break;
1731     case grpFaceSelection:   aType = SMESH::FACE;   break;
1732     case grpVolumeSelection: aType = SMESH::VOLUME; break;
1733     default:                 return;
1734   }
1735
1736   if ( myFilterDlg == 0 )
1737   {
1738     myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, aType );
1739     connect( myFilterDlg, SIGNAL( Accepted() ), SLOT( onFilterAccepted() ) );
1740   }
1741   else
1742     myFilterDlg->Init( aType );
1743
1744   if ( !myGroupOnFilter->_is_nil() )
1745   {
1746     myFilterDlg->SetFilter( myFilter, aType );
1747     myFilterDlg->Init( aType );
1748   }
1749
1750   bool isStandalone = ( sender() == myFilterBtn );
1751   myFilterDlg->SetEnabled( /*setInViewer=*/isStandalone,
1752                            /*diffSources=*/isStandalone );
1753   myFilterDlg->SetMesh( myMesh );
1754   myFilterDlg->SetSelection();
1755   myFilterDlg->SetSourceWg( myElements, false );
1756
1757
1758   myFilterDlg->show();
1759 }
1760
1761 //=================================================================================
1762 // function : onFilterAccepted()
1763 // purpose  : SLOT. Called when Filter dlg closed with OK button.
1764 //            Uncheck "Select submesh" and "Select group" checkboxes
1765 //=================================================================================
1766 void SMESHGUI_GroupDlg::onFilterAccepted()
1767 {
1768   if ( mySelectSubMesh->isChecked() || mySelectGroup->isChecked() )
1769   {
1770     mySelectionMode = myTypeId;
1771     mySelectSubMesh->setChecked( false );
1772     mySelectGroup->setChecked( false );
1773   }
1774   // get a filter from myFilterDlg
1775   myFilter = myFilterDlg->GetFilter();
1776   if ( !myFilter->_is_nil() ) {
1777     SMESH::Predicate_var perdicate = myFilter->GetPredicate();
1778     if ( perdicate->_is_nil() )
1779       myFilter = SMESH::Filter::_nil();
1780   }
1781   // set mesh to myFilter
1782   if ( !myFilter->_is_nil() ) {
1783     SMESH::SMESH_Mesh_var mesh = myMesh;
1784     if ( mesh->_is_nil() ) {
1785       if ( !myGroup->_is_nil() )
1786         mesh = myGroup->GetMesh();
1787       else if ( !myGroupOnGeom->_is_nil() )
1788         mesh = myGroupOnGeom->GetMesh();
1789       else if ( !myGroupOnFilter->_is_nil() )
1790         mesh = myGroupOnFilter->GetMesh();
1791     }
1792     myFilter->SetMesh( mesh );
1793   }
1794
1795   updateButtons();
1796 }
1797
1798 //=================================================================================
1799 // function : onAdd()
1800 // purpose  :
1801 //=================================================================================
1802 void SMESHGUI_GroupDlg::onAdd()
1803 {
1804   SALOME_ListIO aList;
1805   mySelectionMgr->selectedObjects( aList );
1806
1807   int aNbSel = aList.Extent();
1808
1809   if (aNbSel == 0 || myActorsList.count() == 0 || myMesh->_is_nil()) return;
1810
1811   myIsBusy = true;
1812   int sizeBefore = myElements->count();
1813
1814   SMESH::ElementType aType = SMESH::ALL;
1815   switch(myTypeId) {
1816   case grpNodeSelection:
1817     aType = SMESH::NODE;
1818     mySelector->SetSelectionMode(NodeSelection);
1819     break;
1820   case grpBallSelection:
1821     aType = SMESH::BALL;
1822     mySelector->SetSelectionMode(BallSelection);
1823     break;
1824   case grp0DSelection:
1825     aType = SMESH::ELEM0D;
1826     mySelector->SetSelectionMode(Elem0DSelection);
1827     break;
1828   case grpEdgeSelection:
1829     aType = SMESH::EDGE;
1830     mySelector->SetSelectionMode(EdgeSelection);
1831     break;
1832   case grpFaceSelection:
1833     aType = SMESH::FACE;
1834     mySelector->SetSelectionMode(FaceSelection);
1835     break;
1836   case grpVolumeSelection:
1837     aType = SMESH::VOLUME;
1838     mySelector->SetSelectionMode(VolumeSelection);
1839     break;
1840   default:
1841     mySelector->SetSelectionMode(ActorSelection);
1842   }
1843
1844   QListWidgetItem* anItem = 0;
1845   QList<QListWidgetItem*> listItemsToSel;
1846
1847   if (myCurrentLineEdit == 0) {
1848     //if (aNbSel != 1) { myIsBusy = false; return; }
1849     QString aListStr = "";
1850     int aNbItems = 0;
1851     if (myTypeId == 0) {
1852       QListIterator<SMESH_Actor*> it( myActorsList );
1853       while ( it.hasNext() ) {
1854         QString tmpStr;
1855         aNbItems += SMESH::GetNameOfSelectedNodes(mySelector, it.next()->getIO(), tmpStr);
1856         aListStr += tmpStr;
1857       }
1858     }
1859     else {
1860       QListIterator<SMESH_Actor*> it( myActorsList );
1861       while ( it.hasNext() ) {
1862         QString tmpStr;
1863         aNbItems += SMESH::GetNameOfSelectedElements(mySelector, it.next()->getIO(), tmpStr);
1864         aListStr += tmpStr;
1865       }
1866     }
1867     if (aNbItems > 0) {
1868       QStringList anElements = aListStr.split( " ", QString::SkipEmptyParts);
1869       for (QStringList::iterator it = anElements.begin(); it != anElements.end(); ++it) {
1870         QList<QListWidgetItem*> found = myElements->findItems(*it, Qt::MatchExactly);
1871         if (found.count() == 0) {
1872           anItem = new QListWidgetItem(*it);
1873           myElements->addItem(anItem);
1874           if (!anItem->isSelected())
1875             listItemsToSel.push_back(anItem);
1876         }
1877         else {
1878           foreach(anItem, found)
1879             if (!anItem->isSelected())
1880               listItemsToSel.push_back(anItem);
1881         }
1882       }
1883       bool blocked = myElements->signalsBlocked();
1884       myElements->blockSignals(true);
1885       foreach(anItem, listItemsToSel) anItem->setSelected(true);
1886       myElements->blockSignals(blocked);
1887       onListSelectionChanged();
1888       listItemsToSel.clear();
1889     }
1890   } else if (myCurrentLineEdit == mySubMeshLine) {
1891     //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1892
1893     SALOME_ListIO aList;
1894     mySelectionMgr->selectedObjects( aList );
1895
1896     SALOME_ListIteratorOfListIO anIt (aList);
1897     for ( ; anIt.More(); anIt.Next()) {
1898       SMESH::SMESH_subMesh_var aSubMesh =
1899         SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIt.Value());
1900       if (!aSubMesh->_is_nil()) {
1901         // check if mesh is the same
1902         if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
1903           try {
1904             SMESH::long_array_var anElements = aSubMesh->GetElementsByType(aType);
1905             int k = anElements->length();
1906             for (int i = 0; i < k; i++) {
1907               QString aText = QString::number(anElements[i]);
1908               QList<QListWidgetItem*> found = myElements->findItems(aText, Qt::MatchExactly);
1909               if (found.count() == 0) {
1910                 anItem = new QListWidgetItem(aText);
1911                 myElements->addItem(anItem);
1912                 if (!anItem->isSelected())
1913                   listItemsToSel.push_back(anItem);
1914               }
1915               else {
1916                 foreach(anItem, found)
1917                   if (!anItem->isSelected())
1918                     listItemsToSel.push_back(anItem);
1919               }
1920             }
1921             bool blocked = myElements->signalsBlocked();
1922             myElements->blockSignals(true);
1923             foreach(anItem, listItemsToSel) anItem->setSelected(true);
1924             myElements->blockSignals(blocked);
1925             onListSelectionChanged();
1926             listItemsToSel.clear();
1927           }
1928           catch (const SALOME::SALOME_Exception& ex) {
1929             SalomeApp_Tools::QtCatchCorbaException(ex);
1930           }
1931         }
1932       }
1933     }
1934     mySelectSubMesh->setChecked(false);
1935     myIsBusy = false;
1936     onListSelectionChanged();
1937
1938   } else if (myCurrentLineEdit == myGroupLine) {
1939     //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1940     SALOME_ListIO aList;
1941     mySelectionMgr->selectedObjects( aList );
1942
1943     SALOME_ListIteratorOfListIO anIt (aList);
1944     for ( ; anIt.More(); anIt.Next()) {
1945       SMESH::SMESH_GroupBase_var aGroup =
1946         SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(anIt.Value());
1947       if (!aGroup->_is_nil()) {
1948         // check if mesh is the same
1949         if (aGroup->GetType() == aType && aGroup->GetMesh()->GetId() == myMesh->GetId()) {
1950           SMESH::long_array_var anElements = aGroup->GetListOfID();
1951           int k = anElements->length();
1952           for (int i = 0; i < k; i++) {
1953             QString aText = QString::number(anElements[i]);
1954             QList<QListWidgetItem*> found = myElements->findItems(aText, Qt::MatchExactly);
1955             if (found.count() == 0) {
1956               anItem = new QListWidgetItem(aText);
1957               myElements->addItem(anItem);
1958               if (!anItem->isSelected())
1959                 listItemsToSel.push_back(anItem);
1960             }
1961             else {
1962               foreach(anItem, found)
1963                 if (!anItem->isSelected())
1964                   listItemsToSel.push_back(anItem);
1965             }
1966           }
1967           bool blocked = myElements->signalsBlocked();
1968           myElements->blockSignals(true);
1969           foreach(anItem, listItemsToSel) anItem->setSelected(true);
1970           myElements->blockSignals(blocked);
1971           onListSelectionChanged();
1972           listItemsToSel.clear();
1973         }
1974       }
1975     }
1976     mySelectGroup->setChecked(false);
1977     myIsBusy = false;
1978     onListSelectionChanged();
1979
1980   } else if (myCurrentLineEdit == myGeomGroupLine && myGeomObjects->length() == 1) {
1981     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1982     GEOM::GEOM_IGroupOperations_wrap aGroupOp =
1983       SMESH::GetGEOMGen()->GetIGroupOperations(aStudy->StudyId());
1984
1985     SMESH::ElementType aGroupType = SMESH::ALL;
1986     switch(aGroupOp->GetType(myGeomObjects[0])) {
1987     case TopAbs_VERTEX: aGroupType = SMESH::NODE; break;
1988     case TopAbs_EDGE:   aGroupType = SMESH::EDGE; break;
1989     case TopAbs_FACE:   aGroupType = SMESH::FACE; break;
1990     case TopAbs_SOLID:  aGroupType = SMESH::VOLUME; break;
1991     default: myIsBusy = false; return;
1992     }
1993
1994     if (aGroupType == aType) {
1995       _PTR(SObject) aGroupSO =
1996         //aStudy->FindObjectIOR(aStudy->ConvertObjectToIOR(myGeomGroup));
1997         aStudy->FindObjectID(myGeomObjects[0]->GetStudyEntry());
1998       // Construct filter
1999       SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
2000       SMESH::Filter_var aFilter = aFilterMgr->CreateFilter();
2001       SMESH::BelongToGeom_var aBelongToGeom = aFilterMgr->CreateBelongToGeom();
2002       aBelongToGeom->SetGeom(myGeomObjects[0]);
2003       aBelongToGeom->SetShapeName(aGroupSO->GetName().c_str());
2004       aBelongToGeom->SetElementType(aType);
2005       aFilter->SetPredicate(aBelongToGeom);
2006
2007       SMESH::long_array_var anElements = aFilter->GetElementsId(myMesh);
2008
2009       int k = anElements->length();
2010       for (int i = 0; i < k; i++) {
2011         QString aText = QString::number(anElements[i]);
2012         QList<QListWidgetItem*> found = myElements->findItems(aText, Qt::MatchExactly);
2013         if (found.count() == 0) {
2014           anItem = new QListWidgetItem(aText);
2015           myElements->addItem(anItem);
2016           if (!anItem->isSelected())
2017             listItemsToSel.push_back(anItem);
2018         }
2019         else {
2020           foreach(anItem, found)
2021             if (!anItem->isSelected())
2022               listItemsToSel.push_back(anItem);
2023         }
2024       }
2025       bool blocked = myElements->signalsBlocked();
2026       myElements->blockSignals(true);
2027       foreach(anItem, listItemsToSel) anItem->setSelected(true);
2028       myElements->blockSignals(blocked);
2029       onListSelectionChanged();
2030       listItemsToSel.clear();
2031     }
2032
2033     //VSR: mySelectGeomGroup->setChecked(false);
2034     myIsBusy = false;
2035     onListSelectionChanged();
2036   }
2037   myIsBusy = false;
2038   if ( sizeBefore < myElements->count() )
2039     ++myNbChangesOfContents;
2040   //  mySelectionMgr->clearSelected();
2041   updateButtons();
2042 }
2043
2044 //=================================================================================
2045 // function : onRemove()
2046 // purpose  :
2047 //=================================================================================
2048 void SMESHGUI_GroupDlg::onRemove()
2049 {
2050   myIsBusy = true;
2051   int sizeBefore = myElements->count();
2052
2053   if (myCurrentLineEdit == 0) {
2054     QList<QListWidgetItem*> selItems = myElements->selectedItems();
2055     QListWidgetItem* item;
2056     foreach(item, selItems) delete item;
2057   } else {
2058     SALOME_ListIO aList;
2059     mySelectionMgr->selectedObjects( aList );
2060
2061     int aNbSel = aList.Extent();
2062
2063     if (aNbSel == 0) { myIsBusy = false; return; }
2064
2065     SMESH::ElementType aType = SMESH::ALL;
2066     switch(myTypeId) {
2067     case grpNodeSelection:   aType = SMESH::NODE;   break;
2068     case grp0DSelection:     aType = SMESH::ELEM0D; break;
2069     case grpBallSelection:   aType = SMESH::BALL;   break;
2070     case grpEdgeSelection:   aType = SMESH::EDGE;   break;
2071     case grpFaceSelection:   aType = SMESH::FACE;   break;
2072     case grpVolumeSelection: aType = SMESH::VOLUME; break;
2073     }
2074
2075     if (myCurrentLineEdit == mySubMeshLine) {
2076       //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
2077       SALOME_ListIO aList;
2078       mySelectionMgr->selectedObjects( aList );
2079
2080       SALOME_ListIteratorOfListIO anIt (aList);
2081       for ( ; anIt.More(); anIt.Next()) {
2082         SMESH::SMESH_subMesh_var aSubMesh = SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIt.Value());
2083         if (!aSubMesh->_is_nil()) {
2084           // check if mesh is the same
2085           if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
2086             if (aType == SMESH::NODE) {
2087               try {
2088                 SMESH::long_array_var anElements = aSubMesh->GetNodesId();
2089                 int k = anElements->length();
2090                 for (int i = 0; i < k; i++) {
2091                   QList<QListWidgetItem*> found = 
2092                     myElements->findItems(QString::number(anElements[i]), Qt::MatchExactly);
2093                   QListWidgetItem* anItem;
2094                   foreach(anItem, found) delete anItem;
2095                 }
2096               }
2097               catch (const SALOME::SALOME_Exception& ex) {
2098                 SalomeApp_Tools::QtCatchCorbaException(ex);
2099               }
2100             }
2101             else {
2102               try {
2103                 SMESH::long_array_var anElements = aSubMesh->GetElementsId();
2104                 int k = anElements->length();
2105                 for (int i = 0; i < k; i++) {
2106                   QList<QListWidgetItem*> found = 
2107                     myElements->findItems(QString::number(anElements[i]), Qt::MatchExactly);
2108                   QListWidgetItem* anItem;
2109                   foreach(anItem, found) delete anItem;
2110                 }
2111               }
2112               catch (const SALOME::SALOME_Exception& ex) {
2113                 SalomeApp_Tools::QtCatchCorbaException(ex);
2114               }
2115             }
2116           }
2117         }
2118       }
2119     }
2120     else if (myCurrentLineEdit == myGroupLine) {
2121       Standard_Boolean aRes;
2122       //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
2123       SALOME_ListIO aList;
2124       mySelectionMgr->selectedObjects( aList );
2125
2126       SALOME_ListIteratorOfListIO anIt (aList);
2127       for ( ; anIt.More(); anIt.Next()) {
2128         SMESH::SMESH_Group_var aGroup = SMESH::IObjectToInterface<SMESH::SMESH_Group>(anIt.Value());
2129         if (aRes && !aGroup->_is_nil()) {
2130           // check if mesh is the same
2131           if (aGroup->GetType() == aType && aGroup->GetMesh()->GetId() == myMesh->GetId()) {
2132             SMESH::long_array_var anElements = aGroup->GetListOfID();
2133             int k = anElements->length();
2134             for (int i = 0; i < k; i++) {
2135               QList<QListWidgetItem*> found = 
2136                 myElements->findItems(QString::number(anElements[i]), Qt::MatchExactly);
2137               QListWidgetItem* anItem;
2138               foreach(anItem, found) delete anItem;
2139             }
2140           }
2141         }
2142       }
2143     }
2144   }
2145   myIsBusy = false;
2146   if ( sizeBefore > myElements->count() )
2147     myNbChangesOfContents += 2; // it's used to detect that "Add" was only once
2148   updateButtons();
2149 }
2150
2151 //=================================================================================
2152 // function : onSort()
2153 // purpose  :
2154 //=================================================================================
2155 void SMESHGUI_GroupDlg::onSort()
2156 {
2157   // PAL5412: sorts items in ascending by "string" value
2158   // myElements->sort(true);
2159   // myElements->update();
2160   int i, k = myElements->count();
2161   if (k > 0) {
2162     myIsBusy = true;
2163     QList<int> aSelected;
2164     std::vector<int> anArray(k);
2165     //    QMemArray<int> anArray(k);
2166     // fill the array
2167     for (i = 0; i < k; i++) {
2168       int id = myElements->item(i)->text().toInt();
2169       anArray[i] = id;
2170       if (myElements->item(i)->isSelected())
2171         aSelected.append(id);
2172     }
2173     // sort & update list
2174     std::sort(anArray.begin(), anArray.end());
2175     //    anArray.sort();
2176     myElements->clear();
2177     QListWidgetItem* anItem;
2178     QList<QListWidgetItem*> listItemsToSel;
2179     for (i = 0; i < k; i++) {
2180       anItem = new QListWidgetItem(QString::number(anArray[i]));
2181       myElements->addItem(anItem);
2182       if (aSelected.contains(anArray[i]))
2183         listItemsToSel.push_back(anItem);
2184     }
2185     bool blocked = myElements->signalsBlocked();
2186     myElements->blockSignals(true);
2187     foreach(anItem, listItemsToSel) anItem->setSelected(true);
2188     myElements->blockSignals(blocked);
2189     listItemsToSel.clear();
2190     myIsBusy = false;
2191   }
2192 }
2193
2194 //=================================================================================
2195 // function : onVisibilityChanged()
2196 // purpose  :
2197 //=================================================================================
2198 void SMESHGUI_GroupDlg::onVisibilityChanged()
2199 {
2200   SetAppropriateActor();
2201 }
2202
2203 //=================================================================================
2204 // function : SMESHGUI_GroupDlg::reject
2205 // purpose  : SLOT called when "Close" button pressed. Close dialog
2206 //=================================================================================
2207 void SMESHGUI_GroupDlg::reject()
2208 {
2209   if (SMESH::GetCurrentVtkView()) {
2210     SMESH::RemoveFilters(); // PAL6938 -- clean all mesh entity filters
2211     SMESH::SetPointRepresentation(false);
2212     SMESH::SetPickable();
2213     restoreShowEntityMode();
2214   }
2215
2216   if( isApplyAndClose() && !myObjectToSelect.isEmpty() ) {
2217     SUIT_DataOwnerPtrList aList;
2218     aList.append( new LightApp_DataOwner( myObjectToSelect ) );
2219     mySelectionMgr->setSelected( aList );
2220   }
2221   else
2222     mySelectionMgr->clearSelected();
2223   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
2224     aViewWindow->SetSelectionMode(ActorSelection);
2225   mySelectionMgr->clearFilters();
2226   mySMESHGUI->ResetState();
2227
2228   QDialog::reject();
2229
2230   if ( myFilterDlg ) myFilterDlg->UnRegisterFilters();
2231 }
2232
2233 //=================================================================================
2234 // function : onHelp()
2235 // purpose  :
2236 //=================================================================================
2237 void SMESHGUI_GroupDlg::onHelp()
2238 {
2239   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
2240   if (app)
2241     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString( "" ), myHelpFileName);
2242   else {
2243     QString platform;
2244 #ifdef WIN32
2245     platform = "winapplication";
2246 #else
2247     platform = "application";
2248 #endif
2249     SUIT_MessageBox::warning(this, tr( "WRN_WARNING" ),
2250                              tr( "EXTERNAL_BROWSER_CANNOT_SHOW_PAGE" ).
2251                              arg(app->resourceMgr()->stringValue( "ExternalBrowser",
2252                                                                  platform)).
2253                              arg(myHelpFileName));
2254   }
2255 }
2256
2257 //=================================================================================
2258 // function : SMESHGUI_GroupDlg::onDeactivate
2259 // purpose  : SLOT called when dialog must be deativated
2260 //=================================================================================
2261 void SMESHGUI_GroupDlg::onDeactivate()
2262 {
2263   mySMESHGUI->ResetState();
2264   setEnabled(false);
2265 }
2266
2267 //=================================================================================
2268 // function : SMESHGUI_GroupDlg::enterEvent
2269 // purpose  : Event filter
2270 //=================================================================================
2271 void SMESHGUI_GroupDlg::enterEvent (QEvent*)
2272 {
2273   if (!isEnabled()) {
2274     mySMESHGUI->EmitSignalDeactivateDialog();
2275     setEnabled(true);
2276     mySelectionMode = grpNoSelection;
2277     setSelectionMode(myTypeId);
2278     //mySMESHGUI->SetActiveDialogBox((QDialog*)this);
2279     mySMESHGUI->SetActiveDialogBox(this);
2280     mySMESHGUI->SetState(800);
2281   }
2282 }
2283
2284 //=================================================================================
2285 // function : keyPressEvent()
2286 // purpose  :
2287 //=================================================================================
2288 void SMESHGUI_GroupDlg::keyPressEvent( QKeyEvent* e )
2289 {
2290   QDialog::keyPressEvent( e );
2291   if ( e->isAccepted() )
2292     return;
2293
2294   if ( e->key() == Qt::Key_F1 )
2295     {
2296       e->accept();
2297       onHelp();
2298     }
2299 }
2300
2301 //================================================================================
2302 /*!
2303  * \brief Enable showing of the popup when Geometry selection btn is clicked
2304   * \param enable - true to enable
2305  */
2306 //================================================================================
2307
2308 enum { DIRECT_GEOM_INDEX = 0, GEOM_BY_MESH_INDEX };
2309
2310 void SMESHGUI_GroupDlg::updateGeomPopup()
2311 {
2312   bool enable = false;
2313
2314   if ( !myMesh->_is_nil() )
2315     enable = myMesh->NbEdges() > 0;
2316
2317   if ( myGeomGroupBtn )
2318   {
2319     disconnect( myGeomGroupBtn, SIGNAL( toggled(bool) ), this, SLOT( onGeomSelectionButton(bool) ));
2320     if ( enable ) {
2321       if ( !myGeomPopup ) {
2322         myGeomPopup = new QMenu(this);
2323         myActions[myGeomPopup->addAction( tr( "DIRECT_GEOM_SELECTION" ) )] = DIRECT_GEOM_INDEX;
2324         myActions[myGeomPopup->addAction( tr( "GEOM_BY_MESH_ELEM_SELECTION" ) )] = GEOM_BY_MESH_INDEX;
2325         connect( myGeomPopup, SIGNAL( triggered( QAction* ) ), SLOT( onGeomPopup( QAction* ) ) );
2326       }
2327       connect( myGeomGroupBtn, SIGNAL( toggled(bool) ), this, SLOT( onGeomSelectionButton(bool) ));
2328     }
2329   }
2330 }
2331
2332
2333 //=================================================================================
2334 // function : onGeomSelectionButton()
2335 // purpose  :
2336 //=================================================================================
2337 void SMESHGUI_GroupDlg::onGeomSelectionButton(bool isBtnOn)
2338 {
2339   if ( myGeomPopup && isBtnOn )
2340     {
2341       myCurrentLineEdit = myGeomGroupLine;
2342       QAction* a = myGeomPopup->exec( QCursor::pos() );
2343       if (!a || myActions[a] == DIRECT_GEOM_INDEX)
2344         setSelectionMode(grpGeomSelection);
2345     }
2346   else if (!isBtnOn)
2347     {
2348       myCurrentLineEdit = 0;
2349       setSelectionMode(grpAllSelection);
2350     }
2351 }
2352
2353 //=================================================================================
2354 // function : onGeomPopup()
2355 // purpose  :
2356 //=================================================================================
2357 void SMESHGUI_GroupDlg::onGeomPopup( QAction* a )
2358 {
2359   int index = myActions[a];
2360   if ( index == GEOM_BY_MESH_INDEX )
2361     {
2362       mySelectionMode = grpNoSelection;
2363       if ( !myShapeByMeshOp ) {
2364         myShapeByMeshOp = new SMESHGUI_ShapeByMeshOp(true);
2365         connect(myShapeByMeshOp, SIGNAL(committed(SUIT_Operation*)),
2366                 SLOT(onPublishShapeByMeshDlg(SUIT_Operation*)));
2367         connect(myShapeByMeshOp, SIGNAL(aborted(SUIT_Operation*)),
2368                 SLOT(onCloseShapeByMeshDlg(SUIT_Operation*)));
2369       }
2370       // set mesh object to SMESHGUI_ShapeByMeshOp and start it
2371       if ( !myMesh->_is_nil() ) {
2372         myIsBusy = true;
2373         hide(); // stop processing selection
2374         myIsBusy = false;
2375         myShapeByMeshOp->setModule( mySMESHGUI );
2376         myShapeByMeshOp->setStudy( 0 ); // it's really necessary
2377         myShapeByMeshOp->SetMesh( myMesh );
2378         myShapeByMeshOp->start();
2379       }
2380     }
2381 }
2382
2383 //================================================================================
2384 /*!
2385  * \brief SLOT. Is called when Ok is pressed in SMESHGUI_ShapeByMeshDlg
2386  */
2387 //================================================================================
2388
2389 void SMESHGUI_GroupDlg::onPublishShapeByMeshDlg(SUIT_Operation* op)
2390 {
2391   if ( myShapeByMeshOp == op ) {
2392     mySMESHGUI->getApp()->updateObjectBrowser();
2393     show();
2394     // Select a found geometry object
2395     GEOM::GEOM_Object_var aGeomVar = myShapeByMeshOp->GetShape();
2396     if ( !aGeomVar->_is_nil() )
2397     {
2398       QString ID = aGeomVar->GetStudyEntry();
2399       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
2400       if ( _PTR(SObject) aGeomSO = aStudy->FindObjectID( ID.toLatin1().data() )) {
2401         SALOME_ListIO anIOList;
2402         Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject
2403           ( aGeomSO->GetID().c_str(), "SMESH", aGeomSO->GetName().c_str() );
2404         anIOList.Append( anIO );
2405         mySelectionMgr->setSelectedObjects( anIOList, false );
2406         onObjectSelectionChanged();
2407       }
2408     }
2409   }
2410 }
2411
2412 //================================================================================
2413 /*!
2414  * \brief SLOT. Is called when Close is pressed in SMESHGUI_ShapeByMeshDlg
2415  */
2416 //================================================================================
2417
2418 void SMESHGUI_GroupDlg::onCloseShapeByMeshDlg(SUIT_Operation* op)
2419 {
2420   if ( myShapeByMeshOp == op )
2421     {
2422       show();
2423       setSelectionMode(grpGeomSelection);
2424     }
2425 }
2426
2427 //=================================================================================
2428 // function : setGroupColor()
2429 // purpose  :
2430 //=================================================================================
2431 void SMESHGUI_GroupDlg::setGroupColor( const SALOMEDS::Color& theColor )
2432 {
2433   QColor aQColor( (int)( theColor.R * 255.0 ),
2434                   (int)( theColor.G * 255.0 ),
2435                   (int)( theColor.B * 255.0 ) );
2436   setGroupQColor( aQColor );
2437 }
2438
2439 //=================================================================================
2440 // function : getGroupColor()
2441 // purpose  :
2442 //=================================================================================
2443 SALOMEDS::Color SMESHGUI_GroupDlg::getGroupColor() const
2444 {
2445   QColor aQColor = getGroupQColor();
2446
2447   SALOMEDS::Color aColor;
2448   aColor.R = (float)aQColor.red() / 255.0;
2449   aColor.G = (float)aQColor.green() / 255.0;
2450   aColor.B = (float)aQColor.blue() / 255.0;
2451
2452   return aColor;
2453 }
2454
2455 //=================================================================================
2456 // function : setGroupQColor()
2457 // purpose  :
2458 //=================================================================================
2459 void SMESHGUI_GroupDlg::setGroupQColor( const QColor& theColor )
2460 {
2461   if( theColor.isValid() )
2462     myColorBtn->setColor( theColor );
2463 }
2464
2465 //=================================================================================
2466 // function : getGroupQColor()
2467 // purpose  :
2468 //=================================================================================
2469 QColor SMESHGUI_GroupDlg::getGroupQColor() const
2470 {
2471   return myColorBtn->color();
2472 }
2473
2474 //=================================================================================
2475 // function : setDefaultGroupColor()
2476 // purpose  :
2477 //=================================================================================
2478 void SMESHGUI_GroupDlg::setDefaultGroupColor()
2479 {
2480   if( myMesh->_is_nil() )
2481     return;
2482
2483   bool isAutoColor = myMesh->GetAutoColor();
2484
2485   QColor aQColor = myColorBtn->color();
2486   if( !isAutoColor )
2487   {
2488     if ( !aQColor.isValid() ) {
2489       int r = 0, g = 0, b = 0;
2490       SMESH::GetColor( "SMESH", "default_grp_color", r, g, b, QColor( 255, 170, 0 ) );
2491       aQColor.setRgb( r, g, b );
2492     }
2493   }
2494   else
2495   {
2496 #ifdef SIMPLE_AUTOCOLOR   // simplified algorithm for auto-colors
2497     SALOMEDS::Color aColor = SMESHGUI::getPredefinedUniqueColor();
2498 #else                     // old algorithm  for auto-colors
2499     SMESH::ListOfGroups aListOfGroups = *myMesh->GetGroups();
2500
2501     QList<SALOMEDS::Color> aReservedColors;
2502     for( int i = 0, n = aListOfGroups.length(); i < n; i++ )
2503     {
2504       SMESH::SMESH_GroupBase_var aGroupObject = aListOfGroups[i];
2505       SALOMEDS::Color aReservedColor = aGroupObject->GetColor();
2506       aReservedColors.append( aReservedColor );
2507     }
2508
2509     SALOMEDS::Color aColor = SMESHGUI::getUniqueColor( aReservedColors );
2510 #endif                    // SIMPLE_AUTOCOLOR
2511
2512     aQColor.setRgb( (int)( aColor.R * 255.0 ),
2513                     (int)( aColor.G * 255.0 ),
2514                     (int)( aColor.B * 255.0 ) );
2515
2516   }
2517
2518   setGroupQColor( aQColor );
2519 }
2520
2521 //=================================================================================
2522 // function : SetAppropriateActor()
2523 // purpose  : Find more appropriate of visible actors, set it to myActor, allow picking
2524 //            NPAL19389: create a group with a selection in another group.
2525 //            if mesh actor is not visible - find any first visible group or submesh
2526 //=================================================================================
2527 bool SMESHGUI_GroupDlg::SetAppropriateActor()
2528 {
2529   bool isActor = false;
2530   myActorsList.clear();
2531
2532   if (myMesh->_is_nil()) return false;
2533
2534   SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView();
2535
2536   if (myGeomGroupBtn->isChecked()) {   // try current group on geometry actor
2537     if (!isActor) {
2538       if (!myGroupOnGeom->_is_nil()) {
2539         SMESH_Actor* anActor = SMESH::FindActorByObject(myGroupOnGeom);
2540         if (anActor && anActor->hasIO())
2541           {
2542             isActor = true;
2543             if (aViewWindow && !aViewWindow->isVisible(anActor->getIO()))
2544               isActor = false;
2545             else
2546               myActorsList.append(anActor);
2547           }
2548       }
2549     }
2550   } else {
2551     // try mesh actor
2552     SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh);
2553     if (anActor && anActor->hasIO()) {
2554       isActor = true;
2555       if (aViewWindow && !aViewWindow->isVisible(anActor->getIO()))
2556         isActor = false;
2557       else
2558         myActorsList.append(anActor);
2559     }
2560     
2561     // try group actor
2562     if (!isActor && !myGroup->_is_nil()) {
2563       SMESH_Actor* anActor = SMESH::FindActorByObject(myGroup);
2564       if (anActor && anActor->hasIO())
2565         myActorsList.append(anActor);
2566     }
2567     
2568     // try any visible actor of group or submesh of current mesh
2569     if (aViewWindow) {
2570       // mesh entry
2571       _PTR(SObject) aSObject = SMESH::FindSObject(myMesh);
2572       if (aSObject) {
2573         CORBA::String_var meshEntry = aSObject->GetID().c_str();
2574         int len = strlen(meshEntry);
2575         
2576         // iterate on all actors in current view window, search for
2577         // any visible actor, that belongs to group or submesh of current mesh
2578         VTK::ActorCollectionCopy aCopy(aViewWindow->getRenderer()->GetActors());
2579         vtkActorCollection *aCollection = aCopy.GetActors();
2580         int nbItems = aCollection->GetNumberOfItems();
2581         for (int i=0; i<nbItems && !isActor; i++)
2582           {
2583             SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(aCollection->GetItemAsObject(i));
2584             if (anActor && anActor->hasIO()) {
2585               Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
2586               if (aViewWindow->isVisible(anIO)) {
2587                 if (anIO->hasEntry() && strncmp(anIO->getEntry(), meshEntry, len) == 0 && !myActorsList.contains(anActor) )
2588                   myActorsList.append(anActor);
2589               }
2590             }
2591           }
2592       }
2593     }
2594   }
2595   
2596   if (myActorsList.count() > 0) {
2597     QListIterator<SMESH_Actor*> it( myActorsList );
2598     while ( it.hasNext() ) {
2599       SMESH_Actor* anActor = it.next();
2600       if ( IsActorVisible(anActor) )
2601         anActor->SetPickable(true);
2602     }
2603   }
2604   
2605   return ( isActor || (myActorsList.count() > 0) );
2606 }
2607   
2608 //=======================================================================
2609 //function : setShowEntityMode
2610 //purpose  : make shown only entity corresponding to my type
2611 //=======================================================================
2612 void SMESHGUI_GroupDlg::setShowEntityMode()
2613 {
2614   if ( !myMesh->_is_nil() ) {
2615     if ( SMESH_Actor* actor = SMESH::FindActorByObject(myMesh) ) {
2616       if (!myStoredShownEntity)
2617         myStoredShownEntity = actor->GetEntityMode();
2618       switch ( myTypeId ) {
2619       case grpNodeSelection:   restoreShowEntityMode();                          break;
2620       case grp0DSelection:     actor->SetEntityMode( SMESH_Actor::e0DElements ); break;
2621       case grpBallSelection:   actor->SetEntityMode( SMESH_Actor::eBallElem );   break;
2622       case grpEdgeSelection:   actor->SetEntityMode( SMESH_Actor::eEdges );      break;
2623       case grpFaceSelection:   actor->SetEntityMode( SMESH_Actor::eFaces );      break;
2624       case grpVolumeSelection: actor->SetEntityMode( SMESH_Actor::eVolumes );    break;
2625       }
2626     }
2627   }
2628 }
2629
2630 //=======================================================================
2631 //function : restoreShowEntityMode
2632 //purpose  : restore ShowEntity mode of myActor
2633 //=======================================================================
2634 void SMESHGUI_GroupDlg::restoreShowEntityMode()
2635 {
2636   if ( myStoredShownEntity && !myMesh->_is_nil() ) {
2637     if ( SMESH_Actor* actor = SMESH::FindActorByObject(myMesh) ) {
2638       actor->SetEntityMode(myStoredShownEntity);
2639     }
2640   }
2641   myStoredShownEntity = 0;
2642 }
2643
2644 //=======================================================================
2645 //function : IsActorVisible
2646 //purpose  : return visibility of the actor
2647 //=======================================================================
2648 bool SMESHGUI_GroupDlg::IsActorVisible( SMESH_Actor* theActor )
2649 {
2650   SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView();
2651   if (theActor && aViewWindow)
2652     return aViewWindow->isVisible(theActor->getIO());
2653   return false;
2654 }
2655
2656 //================================================================
2657 //function : setIsApplyAndClose
2658 //purpose  : Set value of the flag indicating that the dialog is
2659 //           accepted by Apply & Close button
2660 //================================================================
2661 void SMESHGUI_GroupDlg::setIsApplyAndClose( const bool theFlag )
2662 {
2663   myIsApplyAndClose = theFlag;
2664 }
2665
2666 //================================================================
2667 //function : isApplyAndClose
2668 //purpose  : Get value of the flag indicating that the dialog is
2669 //           accepted by Apply & Close button
2670 //================================================================
2671 bool SMESHGUI_GroupDlg::isApplyAndClose() const
2672 {
2673   return myIsApplyAndClose;
2674 }