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