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