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