Salome HOME
Merge from V5_1_3_BR branch (07/12/09)
[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
1066         if ( myFilterDlg && !myMesh->_is_nil()){
1067           myFilterDlg->SetMesh( myMesh );
1068         }
1069         myGroup = SMESH::SMESH_Group::_nil();
1070
1071         // NPAL19389: create a group with a selection in another group
1072         // set actor of myMesh, if it is visible, else try
1073         // any visible actor of group or submesh of myMesh
1074         SetAppropriateActor();
1075
1076         aString = aList.First()->getName();
1077         myMeshGroupLine->setText(aString);
1078         myMeshGroupLine->home( false );
1079
1080         mySelectSubMesh->setEnabled(true);
1081         mySelectGroup->setEnabled(true);
1082         myGeomGroupBtn->setEnabled(true);
1083         myGeomGroupLine->setEnabled(true);
1084         updateButtons();
1085       }
1086       else {
1087         SMESH::SMESH_GroupBase_var aGroup = SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(IO);
1088         if (aGroup->_is_nil())
1089         {
1090           myIsBusy = false;
1091           return;
1092         }
1093         myIsBusy = false;
1094
1095         myGroup = SMESH::SMESH_Group::_nil();
1096         myGroupOnGeom = SMESH::SMESH_GroupOnGeom::_nil();
1097
1098         init(aGroup);
1099         myIsBusy = true;
1100         mySelectSubMesh->setEnabled(true);
1101         mySelectGroup->setEnabled(true);
1102       }
1103       myCurrentLineEdit = 0;
1104       myIsBusy = false;
1105       if (!myCreate)
1106         return;
1107
1108       if (myGrpTypeId == 0)
1109       {
1110         if (myTypeId == -1)
1111           onTypeChanged(0);
1112         else
1113         {
1114           myElements->clear();
1115           setSelectionMode(myTypeId);
1116         }
1117       }
1118
1119       myIsBusy = false;
1120       return;
1121
1122     }
1123     else if (myCurrentLineEdit == myGeomGroupLine)
1124     {
1125       myGeomObjects = new GEOM::ListOfGO();
1126
1127       // The mesh SObject
1128       _PTR(SObject) aMeshSO = SMESH::FindSObject(myMesh);
1129
1130       if (aNbSel == 0 || !aMeshSO)
1131       {
1132         myGeomObjects->length(0);
1133         updateButtons();
1134         myIsBusy = false;
1135         return;
1136       }
1137
1138       myGeomObjects->length(aNbSel);
1139
1140       GEOM::GEOM_Object_var aGeomGroup;
1141       Standard_Boolean testResult;
1142       int i = 0;
1143
1144       SALOME_ListIteratorOfListIO anIt (aList);
1145       for (; anIt.More(); anIt.Next())
1146       {
1147         testResult = Standard_False;
1148         aGeomGroup = GEOMBase::ConvertIOinGEOMObject(anIt.Value(), testResult);
1149
1150         // Check if the object is a geometry group
1151         if (!testResult || CORBA::is_nil(aGeomGroup))
1152           continue;
1153
1154         // Check if group constructed on the same shape as a mesh or on its child
1155         _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1156         GEOM::GEOM_IGroupOperations_var anOp =
1157           SMESH::GetGEOMGen()->GetIGroupOperations(aStudy->StudyId());
1158
1159         // The main shape of the group
1160         GEOM::GEOM_Object_var aGroupMainShape;
1161         if (aGeomGroup->GetType() == 37)
1162           aGroupMainShape = anOp->GetMainShape(aGeomGroup);
1163         else
1164           aGroupMainShape = GEOM::GEOM_Object::_duplicate(aGeomGroup);
1165         _PTR(SObject) aGroupMainShapeSO =
1166           //aStudy->FindObjectIOR(aStudy->ConvertObjectToIOR(aGroupMainShape));
1167           aStudy->FindObjectID(aGroupMainShape->GetStudyEntry());
1168
1169         _PTR(SObject) anObj, aRef;
1170         bool isRefOrSubShape = false;
1171         if (aMeshSO->FindSubObject(1, anObj) &&  anObj->ReferencedObject(aRef)) {
1172           //if (strcmp(aRef->GetID(), aGroupMainShapeSO->GetID()) == 0) {
1173           if (aRef->GetID() == aGroupMainShapeSO->GetID()) {
1174             isRefOrSubShape = true;
1175           } else {
1176             _PTR(SObject) aFather = aGroupMainShapeSO->GetFather();
1177             _PTR(SComponent) aComponent = aGroupMainShapeSO->GetFatherComponent();
1178             //while (!isRefOrSubShape && strcmp(aFather->GetID(), aComponent->GetID()) != 0) {
1179             while (!isRefOrSubShape && aFather->GetID() != aComponent->GetID()) {
1180               //if (strcmp(aRef->GetID(), aFather->GetID()) == 0)
1181               if (aRef->GetID() == aFather->GetID())
1182                 isRefOrSubShape = true;
1183               else
1184                 aFather = aFather->GetFather();
1185             }
1186           }
1187         }
1188         if (isRefOrSubShape)
1189           myGeomObjects[i++] = aGeomGroup;
1190       }
1191
1192       myGeomObjects->length(i);
1193       if ( i == 0 )
1194         {
1195           myIsBusy = false;
1196           return;
1197         }
1198
1199       aNbSel = i;
1200     }
1201
1202     if (aNbSel >= 1) {
1203       if (aNbSel > 1) {
1204         if (myCurrentLineEdit == mySubMeshLine)
1205           aString = tr( "SMESH_SUBMESH_SELECTED" ).arg(aNbSel);
1206         else if (myCurrentLineEdit == myGroupLine)
1207           aString = tr( "SMESH_GROUP_SELECTED" ).arg(aNbSel);
1208         else if (myCurrentLineEdit == myGeomGroupLine)
1209           aString = tr( "%1 Objects" ).arg(aNbSel);
1210       }
1211       else {
1212         aString = aList.First()->getName();
1213       }
1214     }
1215
1216     myCurrentLineEdit->setText(aString);
1217     myCurrentLineEdit->home(false);
1218     // 07.06.2008 skl for IPAL19574:
1219     // change name of group only if it is empty
1220     if( myName->text().trimmed().isEmpty() || !myNameChanged ) {
1221       myOldName = myName->text();
1222       myName->blockSignals(true);
1223       myName->setText(aString);
1224       myName->blockSignals(false);
1225     }
1226
1227     updateButtons();
1228   }
1229   else // !myCurrentLineEdit: local selection of nodes or elements
1230   {
1231     if (aNbSel == 1 && myActorsList.count() > 0 )
1232     {
1233       // NPAL19389: create a group with a selection in another group
1234       // Switch myActor to the newly selected one, if the last
1235       // is visible and belongs to group or submesh of myMesh
1236       /*      Handle(SALOME_InteractiveObject) curIO = myActor->getIO();
1237       Handle(SALOME_InteractiveObject) selIO = aList.First();
1238       if (curIO->hasEntry() && selIO->hasEntry()) {
1239         const char* selEntry = selIO->getEntry();
1240         if (strcmp(curIO->getEntry(), selEntry) != 0) {
1241           // different objects: selected and myActor
1242           SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView();
1243           if (aViewWindow && aViewWindow->isVisible(selIO)) {
1244             // newly selected actor is visible
1245
1246             // mesh entry
1247             _PTR(SObject) aSObject = SMESH::FindSObject(myMesh);
1248             if (aSObject) {
1249               CORBA::String_var meshEntry = aSObject->GetID().c_str();
1250               int len = strlen(meshEntry);
1251
1252               if (strncmp(selEntry, meshEntry, len) == 0) {
1253                 // selected object is myMesh or a part of it
1254                 SMESH_Actor* anActor = SMESH::FindActorByEntry(selEntry);
1255                 if (anActor) {
1256                   myActor = anActor;
1257                   SMESH::SetPickable(myActor);
1258                 }
1259               }
1260             }
1261           }
1262         }
1263       }*/
1264       // NPAL19389 END
1265
1266       QString aListStr = "";
1267       int aNbItems = 0;
1268       if (myTypeId == 0) {
1269         QListIterator<SMESH_Actor*> it( myActorsList );
1270         while ( it.hasNext() ) {
1271           QString tmpStr;
1272           aNbItems += SMESH::GetNameOfSelectedNodes(mySelector, it.next()->getIO(), tmpStr);
1273           aListStr += tmpStr;
1274         }
1275       } else {
1276         QListIterator<SMESH_Actor*> it( myActorsList );
1277         while ( it.hasNext() ) {
1278           QString tmpStr;
1279           aNbItems += SMESH::GetNameOfSelectedElements(mySelector, it.next()->getIO(), tmpStr);
1280           aListStr += tmpStr;
1281         }
1282       }
1283       if (aNbItems > 0) {
1284         QListWidgetItem* anItem;
1285         QList<QListWidgetItem*> listItemsToSel;
1286         QStringList anElements = aListStr.split( " ", QString::SkipEmptyParts);
1287         for (QStringList::iterator it = anElements.begin(); it != anElements.end(); ++it) {
1288           QList<QListWidgetItem*> found = myElements->findItems(*it, Qt::MatchExactly);
1289           foreach(anItem, found)
1290             if (!anItem->isSelected())
1291               listItemsToSel.push_back(anItem);
1292         }
1293         bool blocked = myElements->signalsBlocked();
1294         myElements->blockSignals(true);
1295         foreach(anItem, listItemsToSel) anItem->setSelected(true);
1296         myElements->blockSignals(blocked);
1297         onListSelectionChanged();
1298         listItemsToSel.clear();
1299       }
1300     }
1301   }
1302   
1303   if (myActorsList.count() == 0) {
1304     if (!myGroup->_is_nil()) {
1305       SMESH_Actor* anActor = SMESH::FindActorByObject(myGroup);
1306       if ( anActor )
1307         myActorsList.append( anActor  );
1308     }
1309     else if(!myGroupOnGeom->_is_nil()) {
1310       SMESH_Actor* anActor = SMESH::FindActorByObject(myGroupOnGeom);
1311       if ( anActor )
1312         myActorsList.append( anActor );
1313     }
1314     else {
1315       SMESH_Actor* anActor = SMESH::FindActorByObject( myMesh );
1316       if ( anActor )
1317         myActorsList.append( anActor );
1318     }
1319   }
1320
1321   // somehow, if we display the mesh, while selecting from another actor,
1322   // the mesh becomes pickable, and there is no way to select any element
1323   if (myActorsList.count() > 0) {
1324     QListIterator<SMESH_Actor*> it( myActorsList );
1325     while ( it.hasNext() ) {
1326       SMESH_Actor* anActor = it.next();
1327       if ( IsActorVisible(anActor) )
1328         anActor->SetPickable(true);
1329     }
1330   }
1331
1332   myIsBusy = false;
1333 }
1334
1335 //=================================================================================
1336 // function : onSelectSubMesh()
1337 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
1338 //=================================================================================
1339 void SMESHGUI_GroupDlg::onSelectSubMesh(bool on)
1340 {
1341   if (on) {
1342     if (mySelectGroup->isChecked()) {
1343       mySelectGroup->setChecked(false);
1344     }
1345     //VSR: else if (mySelectGeomGroup->isChecked()) {
1346     //VSR:   mySelectGeomGroup->setChecked(false);
1347     //VSR: }
1348     myCurrentLineEdit = mySubMeshLine;
1349     setSelectionMode(4);
1350   }
1351   else {
1352     mySubMeshLine->setText( "" );
1353     myCurrentLineEdit = 0;
1354     if (myTypeId != -1)
1355       setSelectionMode(myTypeId);
1356   }
1357   mySubMeshBtn->setEnabled(on);
1358   mySubMeshLine->setEnabled(on);
1359 }
1360
1361
1362 //=================================================================================
1363 // function : (onSelectGroup)
1364 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
1365 //=================================================================================
1366 void SMESHGUI_GroupDlg::onSelectGroup(bool on)
1367 {
1368   if (on) {
1369     if (mySelectSubMesh->isChecked()) {
1370       mySelectSubMesh->setChecked(false);
1371     }
1372     myCurrentLineEdit = myGroupLine;
1373     setSelectionMode(5);
1374   }
1375   else {
1376     myGroupLine->setText( "" );
1377     myCurrentLineEdit = 0;
1378     if (myTypeId != -1)
1379       setSelectionMode(myTypeId);
1380   }
1381   myGroupBtn->setEnabled(on);
1382   myGroupLine->setEnabled(on);
1383 }
1384
1385
1386 //=================================================================================
1387 // function : (onSelectGeomGroup)
1388 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
1389 //=================================================================================
1390 void SMESHGUI_GroupDlg::onSelectGeomGroup(bool on)
1391 {
1392   if (on) {
1393     if (mySelectSubMesh->isChecked()) {
1394       mySelectSubMesh->setChecked(false);
1395     }
1396     else if (mySelectGroup->isChecked()) {
1397       mySelectGroup->setChecked(false);
1398     }
1399     myCurrentLineEdit = myGeomGroupLine;
1400     updateGeomPopup();
1401     setSelectionMode(8);
1402   }
1403   else {
1404     myGeomGroupBtn->setChecked(false);
1405     myGeomObjects->length(0);
1406     myGeomGroupLine->setText( "" );
1407     myCurrentLineEdit = 0;
1408     if (myTypeId != -1)
1409       setSelectionMode(myTypeId);
1410   }
1411 }
1412
1413
1414 //=================================================================================
1415 // function : setCurrentSelection()
1416 // purpose  :
1417 //=================================================================================
1418 void SMESHGUI_GroupDlg::setCurrentSelection()
1419 {
1420   QPushButton* send = (QPushButton*)sender();
1421   myCurrentLineEdit = 0;
1422   if (send == myMeshGroupBtn) {
1423     disconnect(myMeshGroupBtn, SIGNAL(clicked()), this, SLOT(setCurrentSelection()));
1424     mySelectionMgr->clearSelected();
1425     if (myCreate)
1426       setSelectionMode(6);
1427     else
1428       setSelectionMode(5);
1429     connect(myMeshGroupBtn, SIGNAL(clicked()), this, SLOT(setCurrentSelection()));
1430     myCurrentLineEdit = myMeshGroupLine;
1431     onObjectSelectionChanged();
1432   }
1433   else if (send == mySubMeshBtn) {
1434     myCurrentLineEdit = mySubMeshLine;
1435     onObjectSelectionChanged();
1436   }
1437   else if (send == myGroupBtn) {
1438     myCurrentLineEdit = myGroupLine;
1439     onObjectSelectionChanged();
1440   }
1441 }
1442
1443
1444 //=================================================================================
1445 // function : setFilters()
1446 // purpose  : SLOT. Called when "Filter" button pressed.
1447 //=================================================================================
1448 void SMESHGUI_GroupDlg::setFilters()
1449 {
1450   if(myMesh->_is_nil()) {
1451     SUIT_MessageBox::critical(this,
1452                               tr("SMESH_ERROR"),
1453                               tr("NO_MESH_SELECTED"));
1454    return;
1455   }
1456
1457   SMESH::ElementType aType = SMESH::ALL;
1458   switch ( myTypeId )
1459   {
1460     case 0 : aType = SMESH::NODE; break;
1461     case 1 : aType = SMESH::EDGE; break;
1462     case 2 : aType = SMESH::FACE; break;
1463     case 3 : aType = SMESH::VOLUME; break;
1464     default: return;
1465   }
1466
1467   if ( myFilterDlg == 0 )
1468   {
1469     myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, aType );
1470     connect( myFilterDlg, SIGNAL( Accepted() ), SLOT( onFilterAccepted() ) );
1471   }
1472   else
1473     myFilterDlg->Init( aType );
1474
1475   myFilterDlg->SetSelection();
1476   myFilterDlg->SetMesh( myMesh );
1477   myFilterDlg->SetSourceWg( myElements, false );
1478
1479   myFilterDlg->show();
1480 }
1481
1482 //=================================================================================
1483 // function : onFilterAccepted()
1484 // purpose  : SLOT. Called when Filter dlg closed with OK button.
1485 //            Uncheck "Select submesh" and "Select group" checkboxes
1486 //=================================================================================
1487 void SMESHGUI_GroupDlg::onFilterAccepted()
1488 {
1489   if ( mySelectSubMesh->isChecked() || mySelectGroup->isChecked() )
1490   {
1491     mySelectionMode = myTypeId;
1492     mySelectSubMesh->setChecked( false );
1493     mySelectGroup->setChecked( false );
1494   }
1495 }
1496
1497 //=================================================================================
1498 // function : onAdd()
1499 // purpose  :
1500 //=================================================================================
1501 void SMESHGUI_GroupDlg::onAdd()
1502 {
1503   SALOME_ListIO aList;
1504   mySelectionMgr->selectedObjects( aList );
1505
1506   int aNbSel = aList.Extent();
1507
1508   if (aNbSel == 0 || myActorsList.count() == 0 || myMesh->_is_nil()) return;
1509
1510   myIsBusy = true;
1511
1512   SMESH::ElementType aType = SMESH::ALL;
1513   switch(myTypeId) {
1514   case 0:
1515     aType = SMESH::NODE;
1516     mySelector->SetSelectionMode(NodeSelection);
1517     break;
1518   case 1:
1519     aType = SMESH::EDGE;
1520     mySelector->SetSelectionMode(EdgeSelection);
1521     break;
1522   case 2:
1523     aType = SMESH::FACE;
1524     mySelector->SetSelectionMode(FaceSelection);
1525     break;
1526   case 3:
1527     aType = SMESH::VOLUME;
1528     mySelector->SetSelectionMode(VolumeSelection);
1529     break;
1530   default:
1531     mySelector->SetSelectionMode(ActorSelection);
1532   }
1533
1534   QListWidgetItem* anItem = 0;
1535   QList<QListWidgetItem*> listItemsToSel;
1536
1537   if (myCurrentLineEdit == 0) {
1538     //if (aNbSel != 1) { myIsBusy = false; return; }
1539     QString aListStr = "";
1540     int aNbItems = 0;
1541     if (myTypeId == 0) {
1542       QListIterator<SMESH_Actor*> it( myActorsList );
1543       while ( it.hasNext() ) {
1544         QString tmpStr;
1545         aNbItems += SMESH::GetNameOfSelectedNodes(mySelector, it.next()->getIO(), tmpStr);
1546         aListStr += tmpStr;
1547       }
1548     }
1549     else {
1550       QListIterator<SMESH_Actor*> it( myActorsList );
1551       while ( it.hasNext() ) {
1552         QString tmpStr;
1553         aNbItems += SMESH::GetNameOfSelectedElements(mySelector, it.next()->getIO(), tmpStr);
1554         aListStr += tmpStr;
1555       }
1556     }
1557     if (aNbItems > 0) {
1558       QStringList anElements = aListStr.split( " ", QString::SkipEmptyParts);
1559       for (QStringList::iterator it = anElements.begin(); it != anElements.end(); ++it) {
1560         QList<QListWidgetItem*> found = myElements->findItems(*it, Qt::MatchExactly);
1561         if (found.count() == 0) {
1562           anItem = new QListWidgetItem(*it);
1563           myElements->addItem(anItem);
1564           if (!anItem->isSelected())
1565             listItemsToSel.push_back(anItem);
1566         }
1567         else {
1568           foreach(anItem, found)
1569             if (!anItem->isSelected())
1570               listItemsToSel.push_back(anItem);
1571         }
1572       }
1573       bool blocked = myElements->signalsBlocked();
1574       myElements->blockSignals(true);
1575       foreach(anItem, listItemsToSel) anItem->setSelected(true);
1576       myElements->blockSignals(blocked);
1577       onListSelectionChanged();
1578       listItemsToSel.clear();
1579     }
1580   } else if (myCurrentLineEdit == mySubMeshLine) {
1581     //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1582
1583     SALOME_ListIO aList;
1584     mySelectionMgr->selectedObjects( aList );
1585
1586     SALOME_ListIteratorOfListIO anIt (aList);
1587     for ( ; anIt.More(); anIt.Next()) {
1588       SMESH::SMESH_subMesh_var aSubMesh =
1589         SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIt.Value());
1590       if (!aSubMesh->_is_nil()) {
1591         // check if mesh is the same
1592         if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
1593           try {
1594             SMESH::long_array_var anElements = aSubMesh->GetElementsByType(aType);
1595             int k = anElements->length();
1596             for (int i = 0; i < k; i++) {
1597               QString aText = QString::number(anElements[i]);
1598               QList<QListWidgetItem*> found = myElements->findItems(aText, Qt::MatchExactly);
1599               if (found.count() == 0) {
1600                 anItem = new QListWidgetItem(aText);
1601                 myElements->addItem(anItem);
1602                 if (!anItem->isSelected())
1603                   listItemsToSel.push_back(anItem);
1604               }
1605               else {
1606                 foreach(anItem, found)
1607                   if (!anItem->isSelected())
1608                     listItemsToSel.push_back(anItem);
1609               }
1610             }
1611             bool blocked = myElements->signalsBlocked();
1612             myElements->blockSignals(true);
1613             foreach(anItem, listItemsToSel) anItem->setSelected(true);
1614             myElements->blockSignals(blocked);
1615             onListSelectionChanged();
1616             listItemsToSel.clear();
1617           }
1618           catch (const SALOME::SALOME_Exception& ex) {
1619             SalomeApp_Tools::QtCatchCorbaException(ex);
1620           }
1621         }
1622       }
1623     }
1624     mySelectSubMesh->setChecked(false);
1625     myIsBusy = false;
1626     onListSelectionChanged();
1627
1628   } else if (myCurrentLineEdit == myGroupLine) {
1629     //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1630     SALOME_ListIO aList;
1631     mySelectionMgr->selectedObjects( aList );
1632
1633     SALOME_ListIteratorOfListIO anIt (aList);
1634     for ( ; anIt.More(); anIt.Next()) {
1635       SMESH::SMESH_GroupBase_var aGroup =
1636         SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(anIt.Value());
1637       if (!aGroup->_is_nil()) {
1638         // check if mesh is the same
1639         if (aGroup->GetType() == aType && aGroup->GetMesh()->GetId() == myMesh->GetId()) {
1640           SMESH::long_array_var anElements = aGroup->GetListOfID();
1641           int k = anElements->length();
1642           for (int i = 0; i < k; i++) {
1643             QString aText = QString::number(anElements[i]);
1644             QList<QListWidgetItem*> found = myElements->findItems(aText, Qt::MatchExactly);
1645             if (found.count() == 0) {
1646               anItem = new QListWidgetItem(aText);
1647               myElements->addItem(anItem);
1648               if (!anItem->isSelected())
1649                 listItemsToSel.push_back(anItem);
1650             }
1651             else {
1652               foreach(anItem, found)
1653                 if (!anItem->isSelected())
1654                   listItemsToSel.push_back(anItem);
1655             }
1656           }
1657           bool blocked = myElements->signalsBlocked();
1658           myElements->blockSignals(true);
1659           foreach(anItem, listItemsToSel) anItem->setSelected(true);
1660           myElements->blockSignals(blocked);
1661           onListSelectionChanged();
1662           listItemsToSel.clear();
1663         }
1664       }
1665     }
1666     mySelectGroup->setChecked(false);
1667     myIsBusy = false;
1668     onListSelectionChanged();
1669
1670   } else if (myCurrentLineEdit == myGeomGroupLine && myGeomObjects->length() == 1) {
1671     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1672     GEOM::GEOM_IGroupOperations_var aGroupOp =
1673       SMESH::GetGEOMGen()->GetIGroupOperations(aStudy->StudyId());
1674
1675     SMESH::ElementType aGroupType = SMESH::ALL;
1676     switch(aGroupOp->GetType(myGeomObjects[0])) {
1677     case 7: aGroupType = SMESH::NODE; break;
1678     case 6: aGroupType = SMESH::EDGE; break;
1679     case 4: aGroupType = SMESH::FACE; break;
1680     case 2: aGroupType = SMESH::VOLUME; break;
1681     default: myIsBusy = false; return;
1682     }
1683
1684     if (aGroupType == aType) {
1685       _PTR(SObject) aGroupSO =
1686         //aStudy->FindObjectIOR(aStudy->ConvertObjectToIOR(myGeomGroup));
1687         aStudy->FindObjectID(myGeomObjects[0]->GetStudyEntry());
1688       // Construct filter
1689       SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
1690       SMESH::Filter_var aFilter = aFilterMgr->CreateFilter();
1691       SMESH::BelongToGeom_var aBelongToGeom = aFilterMgr->CreateBelongToGeom();;
1692       aBelongToGeom->SetGeom(myGeomObjects[0]);
1693       aBelongToGeom->SetShapeName(aGroupSO->GetName().c_str());
1694       aBelongToGeom->SetElementType(aType);
1695       aFilter->SetPredicate(aBelongToGeom);
1696
1697       SMESH::long_array_var anElements = aFilter->GetElementsId(myMesh);
1698
1699       int k = anElements->length();
1700       for (int i = 0; i < k; i++) {
1701         QString aText = QString::number(anElements[i]);
1702         QList<QListWidgetItem*> found = myElements->findItems(aText, Qt::MatchExactly);
1703         if (found.count() == 0) {
1704           anItem = new QListWidgetItem(aText);
1705           myElements->addItem(anItem);
1706           if (!anItem->isSelected())
1707             listItemsToSel.push_back(anItem);
1708         }
1709         else {
1710           foreach(anItem, found)
1711             if (!anItem->isSelected())
1712               listItemsToSel.push_back(anItem);
1713         }
1714       }
1715       bool blocked = myElements->signalsBlocked();
1716       myElements->blockSignals(true);
1717       foreach(anItem, listItemsToSel) anItem->setSelected(true);
1718       myElements->blockSignals(blocked);
1719       onListSelectionChanged();
1720       listItemsToSel.clear();
1721     }
1722
1723     //VSR: mySelectGeomGroup->setChecked(false);
1724     myIsBusy = false;
1725     onListSelectionChanged();
1726   }
1727   myIsBusy = false;
1728   //  mySelectionMgr->clearSelected();
1729   updateButtons();
1730 }
1731
1732 //=================================================================================
1733 // function : onRemove()
1734 // purpose  :
1735 //=================================================================================
1736 void SMESHGUI_GroupDlg::onRemove()
1737 {
1738   myIsBusy = true;
1739   if (myCurrentLineEdit == 0) {
1740     QList<QListWidgetItem*> selItems = myElements->selectedItems();
1741     QListWidgetItem* item;
1742     foreach(item, selItems) delete item;
1743   } else {
1744     SALOME_ListIO aList;
1745     mySelectionMgr->selectedObjects( aList );
1746
1747     int aNbSel = aList.Extent();
1748
1749     if (aNbSel == 0) { myIsBusy = false; return; }
1750
1751     SMESH::ElementType aType = SMESH::ALL;
1752     switch(myTypeId) {
1753     case 0: aType = SMESH::NODE; break;
1754     case 1: aType = SMESH::EDGE; break;
1755     case 2: aType = SMESH::FACE; break;
1756     case 3: aType = SMESH::VOLUME; break;
1757     }
1758
1759     if (myCurrentLineEdit == mySubMeshLine) {
1760       //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1761       SALOME_ListIO aList;
1762       mySelectionMgr->selectedObjects( aList );
1763
1764       SALOME_ListIteratorOfListIO anIt (aList);
1765       for ( ; anIt.More(); anIt.Next()) {
1766         SMESH::SMESH_subMesh_var aSubMesh = SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIt.Value());
1767         if (!aSubMesh->_is_nil()) {
1768           // check if mesh is the same
1769           if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
1770             if (aType == SMESH::NODE) {
1771               try {
1772                 SMESH::long_array_var anElements = aSubMesh->GetNodesId();
1773                 int k = anElements->length();
1774                 for (int i = 0; i < k; i++) {
1775                   QList<QListWidgetItem*> found = 
1776                     myElements->findItems(QString::number(anElements[i]), Qt::MatchExactly);
1777                   QListWidgetItem* anItem;
1778                   foreach(anItem, found) delete anItem;
1779                 }
1780               }
1781               catch (const SALOME::SALOME_Exception& ex) {
1782                 SalomeApp_Tools::QtCatchCorbaException(ex);
1783               }
1784             }
1785             else {
1786               try {
1787                 SMESH::long_array_var anElements = aSubMesh->GetElementsId();
1788                 int k = anElements->length();
1789                 for (int i = 0; i < k; i++) {
1790                   QList<QListWidgetItem*> found = 
1791                     myElements->findItems(QString::number(anElements[i]), Qt::MatchExactly);
1792                   QListWidgetItem* anItem;
1793                   foreach(anItem, found) delete anItem;
1794                 }
1795               }
1796               catch (const SALOME::SALOME_Exception& ex) {
1797                 SalomeApp_Tools::QtCatchCorbaException(ex);
1798               }
1799             }
1800           }
1801         }
1802       }
1803     }
1804     else if (myCurrentLineEdit == myGroupLine) {
1805       Standard_Boolean aRes;
1806       //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1807       SALOME_ListIO aList;
1808       mySelectionMgr->selectedObjects( aList );
1809
1810       SALOME_ListIteratorOfListIO anIt (aList);
1811       for ( ; anIt.More(); anIt.Next()) {
1812         SMESH::SMESH_Group_var aGroup = SMESH::IObjectToInterface<SMESH::SMESH_Group>(anIt.Value());
1813         if (aRes && !aGroup->_is_nil()) {
1814           // check if mesh is the same
1815           if (aGroup->GetType() == aType && aGroup->GetMesh()->GetId() == myMesh->GetId()) {
1816             SMESH::long_array_var anElements = aGroup->GetListOfID();
1817             int k = anElements->length();
1818             for (int i = 0; i < k; i++) {
1819               QList<QListWidgetItem*> found = 
1820                 myElements->findItems(QString::number(anElements[i]), Qt::MatchExactly);
1821               QListWidgetItem* anItem;
1822               foreach(anItem, found) delete anItem;
1823             }
1824           }
1825         }
1826       }
1827     }
1828   }
1829   myIsBusy = false;
1830   updateButtons();
1831 }
1832
1833 //=================================================================================
1834 // function : onSort()
1835 // purpose  :
1836 //=================================================================================
1837 void SMESHGUI_GroupDlg::onSort()
1838 {
1839   // PAL5412: sorts items in ascending by "string" value
1840   // myElements->sort(true);
1841   // myElements->update();
1842   int i, k = myElements->count();
1843   if (k > 0) {
1844     myIsBusy = true;
1845     QList<int> aSelected;
1846     std::vector<int> anArray(k);
1847     //    QMemArray<int> anArray(k);
1848     // fill the array
1849     for (i = 0; i < k; i++) {
1850       int id = myElements->item(i)->text().toInt();
1851       anArray[i] = id;
1852       if (myElements->item(i)->isSelected())
1853         aSelected.append(id);
1854     }
1855     // sort & update list
1856     std::sort(anArray.begin(), anArray.end());
1857     //    anArray.sort();
1858     myElements->clear();
1859     QListWidgetItem* anItem;
1860     QList<QListWidgetItem*> listItemsToSel;
1861     for (i = 0; i < k; i++) {
1862       anItem = new QListWidgetItem(QString::number(anArray[i]));
1863       myElements->addItem(anItem);
1864       if (aSelected.contains(anArray[i]))
1865         listItemsToSel.push_back(anItem);
1866     }
1867     bool blocked = myElements->signalsBlocked();
1868     myElements->blockSignals(true);
1869     foreach(anItem, listItemsToSel) anItem->setSelected(true);
1870     myElements->blockSignals(blocked);
1871     listItemsToSel.clear();
1872     myIsBusy = false;
1873   }
1874 }
1875
1876 //=================================================================================
1877 // function : closeEvent()
1878 // purpose  :
1879 //=================================================================================
1880 void SMESHGUI_GroupDlg::closeEvent (QCloseEvent*)
1881 {
1882   onClose();
1883 }
1884
1885 //=================================================================================
1886 // function : onVisibilityChanged()
1887 // purpose  :
1888 //=================================================================================
1889 void SMESHGUI_GroupDlg::onVisibilityChanged()
1890 {
1891   SetAppropriateActor();
1892 }
1893
1894 //=================================================================================
1895 // function : SMESHGUI_GroupDlg::onClose
1896 // purpose  : SLOT called when "Close" button pressed. Close dialog
1897 //=================================================================================
1898 void SMESHGUI_GroupDlg::onClose()
1899 {
1900   if (SMESH::GetCurrentVtkView()) {
1901     SMESH::RemoveFilters(); // PAL6938 -- clean all mesh entity filters
1902     SMESH::SetPointRepresentation(false);
1903     SMESH::SetPickable();
1904     restoreShowEntityMode();
1905   }
1906
1907   mySelectionMgr->clearSelected();
1908   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
1909     aViewWindow->SetSelectionMode(ActorSelection);
1910   mySelectionMgr->clearFilters();
1911   mySMESHGUI->ResetState();
1912
1913   reject();
1914 }
1915
1916 //=================================================================================
1917 // function : onHelp()
1918 // purpose  :
1919 //=================================================================================
1920 void SMESHGUI_GroupDlg::onHelp()
1921 {
1922   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
1923   if (app)
1924     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString( "" ), myHelpFileName);
1925   else {
1926     QString platform;
1927 #ifdef WIN32
1928     platform = "winapplication";
1929 #else
1930     platform = "application";
1931 #endif
1932     SUIT_MessageBox::warning(this, tr( "WRN_WARNING" ),
1933                              tr( "EXTERNAL_BROWSER_CANNOT_SHOW_PAGE" ).
1934                              arg(app->resourceMgr()->stringValue( "ExternalBrowser",
1935                                                                  platform)).
1936                              arg(myHelpFileName));
1937   }
1938 }
1939
1940 //=================================================================================
1941 // function : SMESHGUI_GroupDlg::onDeactivate
1942 // purpose  : SLOT called when dialog must be deativated
1943 //=================================================================================
1944 void SMESHGUI_GroupDlg::onDeactivate()
1945 {
1946   mySMESHGUI->ResetState();
1947   setEnabled(false);
1948 }
1949
1950 //=================================================================================
1951 // function : SMESHGUI_GroupDlg::enterEvent
1952 // purpose  : Event filter
1953 //=================================================================================
1954 void SMESHGUI_GroupDlg::enterEvent (QEvent*)
1955 {
1956   if (!isEnabled()) {
1957     mySMESHGUI->EmitSignalDeactivateDialog();
1958     setEnabled(true);
1959     mySelectionMode = -1;
1960     setSelectionMode(myTypeId);
1961     //mySMESHGUI->SetActiveDialogBox((QDialog*)this);
1962     mySMESHGUI->SetActiveDialogBox(this);
1963     mySMESHGUI->SetState(800);
1964   }
1965 }
1966
1967 //=================================================================================
1968 // function : hideEvent
1969 // purpose  : caused by ESC key
1970 //=================================================================================
1971 void SMESHGUI_GroupDlg::hideEvent (QHideEvent*)
1972 {
1973   if (!isMinimized() && !myIsBusy)
1974     onClose();
1975 }
1976
1977 //=================================================================================
1978 // function : keyPressEvent()
1979 // purpose  :
1980 //=================================================================================
1981 void SMESHGUI_GroupDlg::keyPressEvent( QKeyEvent* e )
1982 {
1983   QDialog::keyPressEvent( e );
1984   if ( e->isAccepted() )
1985     return;
1986
1987   if ( e->key() == Qt::Key_F1 )
1988     {
1989       e->accept();
1990       onHelp();
1991     }
1992 }
1993
1994 //================================================================================
1995 /*!
1996  * \brief Enable showing of the popup when Geometry selection btn is clicked
1997   * \param enable - true to enable
1998  */
1999 //================================================================================
2000
2001 enum { DIRECT_GEOM_INDEX = 0, GEOM_BY_MESH_INDEX };
2002
2003 void SMESHGUI_GroupDlg::updateGeomPopup()
2004 {
2005   bool enable = false;
2006
2007   if ( !myMesh->_is_nil() )
2008     enable = myMesh->NbEdges() > 0;
2009
2010   if ( myGeomGroupBtn )
2011   {
2012     disconnect( myGeomGroupBtn, SIGNAL( toggled(bool) ), this, SLOT( onGeomSelectionButton(bool) ));
2013     if ( enable ) {
2014       if ( !myGeomPopup ) {
2015         myGeomPopup = new QMenu(this);
2016         myActions[myGeomPopup->addAction( tr( "DIRECT_GEOM_SELECTION" ) )] = DIRECT_GEOM_INDEX;
2017         myActions[myGeomPopup->addAction( tr( "GEOM_BY_MESH_ELEM_SELECTION" ) )] = GEOM_BY_MESH_INDEX;
2018         connect( myGeomPopup, SIGNAL( triggered( QAction* ) ), SLOT( onGeomPopup( QAction* ) ) );
2019       }
2020       connect( myGeomGroupBtn, SIGNAL( toggled(bool) ), this, SLOT( onGeomSelectionButton(bool) ));
2021     }
2022   }
2023 }
2024
2025
2026 //=================================================================================
2027 // function : onGeomSelectionButton()
2028 // purpose  :
2029 //=================================================================================
2030 void SMESHGUI_GroupDlg::onGeomSelectionButton(bool isBtnOn)
2031 {
2032   if ( myGeomPopup && isBtnOn )
2033     {
2034       myCurrentLineEdit = myGeomGroupLine;
2035       QAction* a = myGeomPopup->exec( QCursor::pos() );
2036       if (!a || myActions[a] == DIRECT_GEOM_INDEX)
2037         setSelectionMode(7);
2038     }
2039   else if (!isBtnOn)
2040     {
2041       myCurrentLineEdit = 0;
2042       setSelectionMode(8);
2043     }
2044 }
2045
2046 //=================================================================================
2047 // function : onGeomPopup()
2048 // purpose  :
2049 //=================================================================================
2050 void SMESHGUI_GroupDlg::onGeomPopup( QAction* a )
2051 {
2052   int index = myActions[a];
2053   if ( index == GEOM_BY_MESH_INDEX )
2054     {
2055       mySelectionMode = -1;
2056       if ( !myShapeByMeshOp ) {
2057         myShapeByMeshOp = new SMESHGUI_ShapeByMeshOp(true);
2058         connect(myShapeByMeshOp, SIGNAL(committed(SUIT_Operation*)),
2059                 SLOT(onPublishShapeByMeshDlg(SUIT_Operation*)));
2060         connect(myShapeByMeshOp, SIGNAL(aborted(SUIT_Operation*)),
2061                 SLOT(onCloseShapeByMeshDlg(SUIT_Operation*)));
2062       }
2063       // set mesh object to SMESHGUI_ShapeByMeshOp and start it
2064       if ( !myMesh->_is_nil() ) {
2065         myIsBusy = true;
2066         hide(); // stop processing selection
2067         myIsBusy = false;
2068         myShapeByMeshOp->setModule( mySMESHGUI );
2069         myShapeByMeshOp->setStudy( 0 ); // it's really necessary
2070         myShapeByMeshOp->SetMesh( myMesh );
2071         myShapeByMeshOp->start();
2072       }
2073     }
2074 }
2075
2076 //================================================================================
2077 /*!
2078  * \brief SLOT. Is called when Ok is pressed in SMESHGUI_ShapeByMeshDlg
2079  */
2080 //================================================================================
2081
2082 void SMESHGUI_GroupDlg::onPublishShapeByMeshDlg(SUIT_Operation* op)
2083 {
2084   if ( myShapeByMeshOp == op ) {
2085     mySMESHGUI->getApp()->updateObjectBrowser();
2086     show();
2087     // Select a found geometry object
2088     GEOM::GEOM_Object_var aGeomVar = myShapeByMeshOp->GetShape();
2089     if ( !aGeomVar->_is_nil() )
2090     {
2091       QString ID = aGeomVar->GetStudyEntry();
2092       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
2093       if ( _PTR(SObject) aGeomSO = aStudy->FindObjectID( ID.toLatin1().data() )) {
2094         SALOME_ListIO anIOList;
2095         Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject
2096           ( aGeomSO->GetID().c_str(), "SMESH", aGeomSO->GetName().c_str() );
2097         anIOList.Append( anIO );
2098         mySelectionMgr->setSelectedObjects( anIOList, false );
2099         onObjectSelectionChanged();
2100       }
2101     }
2102   }
2103 }
2104
2105 //================================================================================
2106 /*!
2107  * \brief SLOT. Is called when Close is pressed in SMESHGUI_ShapeByMeshDlg
2108  */
2109 //================================================================================
2110
2111 void SMESHGUI_GroupDlg::onCloseShapeByMeshDlg(SUIT_Operation* op)
2112 {
2113   if ( myShapeByMeshOp == op )
2114     {
2115       show();
2116       setSelectionMode(7);
2117     }
2118 }
2119
2120 //=================================================================================
2121 // function : setGroupColor()
2122 // purpose  :
2123 //=================================================================================
2124 void SMESHGUI_GroupDlg::setGroupColor( const SALOMEDS::Color& theColor )
2125 {
2126   QColor aQColor( (int)( theColor.R * 255.0 ),
2127                   (int)( theColor.G * 255.0 ),
2128                   (int)( theColor.B * 255.0 ) );
2129   setGroupQColor( aQColor );
2130 }
2131
2132 //=================================================================================
2133 // function : getGroupColor()
2134 // purpose  :
2135 //=================================================================================
2136 SALOMEDS::Color SMESHGUI_GroupDlg::getGroupColor() const
2137 {
2138   QColor aQColor = getGroupQColor();
2139
2140   SALOMEDS::Color aColor;
2141   aColor.R = (float)aQColor.red() / 255.0;
2142   aColor.G = (float)aQColor.green() / 255.0;
2143   aColor.B = (float)aQColor.blue() / 255.0;
2144
2145   return aColor;
2146 }
2147
2148 //=================================================================================
2149 // function : setGroupQColor()
2150 // purpose  :
2151 //=================================================================================
2152 void SMESHGUI_GroupDlg::setGroupQColor( const QColor& theColor )
2153 {
2154   if( theColor.isValid() )
2155     myColorBtn->setColor( theColor );
2156 }
2157
2158 //=================================================================================
2159 // function : getGroupQColor()
2160 // purpose  :
2161 //=================================================================================
2162 QColor SMESHGUI_GroupDlg::getGroupQColor() const
2163 {
2164   return myColorBtn->color();
2165 }
2166
2167 //=================================================================================
2168 // function : setDefaultGroupColor()
2169 // purpose  :
2170 //=================================================================================
2171 void SMESHGUI_GroupDlg::setDefaultGroupColor()
2172 {
2173   if( myMesh->_is_nil() )
2174     return;
2175
2176   bool isAutoColor = myMesh->GetAutoColor();
2177
2178   QColor aQColor;
2179   if( !isAutoColor )
2180   {
2181     int r = 0, g = 0, b = 0;
2182     SMESH::GetColor( "SMESH", "fill_color", r, g, b, QColor( 0, 170, 255 ) );
2183     aQColor.setRgb( r, g, b );
2184   }
2185   else
2186   {
2187     SMESH::ListOfGroups aListOfGroups = *myMesh->GetGroups();
2188
2189     QList<SALOMEDS::Color> aReservedColors;
2190     for( int i = 0, n = aListOfGroups.length(); i < n; i++ )
2191     {
2192       SMESH::SMESH_GroupBase_var aGroupObject = aListOfGroups[i];
2193       SALOMEDS::Color aReservedColor = aGroupObject->GetColor();
2194       aReservedColors.append( aReservedColor );
2195     }
2196
2197     SALOMEDS::Color aColor = SMESHGUI::getUniqueColor( aReservedColors );
2198     aQColor.setRgb( (int)( aColor.R * 255.0 ),
2199                     (int)( aColor.G * 255.0 ),
2200                     (int)( aColor.B * 255.0 ) );
2201
2202   }
2203
2204   setGroupQColor( aQColor );
2205 }
2206
2207 //=================================================================================
2208 // function : SetAppropriateActor()
2209 // purpose  : Find more appropriate of visible actors, set it to myActor, allow picking
2210 //            NPAL19389: create a group with a selection in another group.
2211 //            if mesh actor is not visible - find any first visible group or submesh
2212 //=================================================================================
2213 bool SMESHGUI_GroupDlg::SetAppropriateActor()
2214 {
2215   bool isActor = false;
2216   myActorsList.clear();
2217
2218   if (myMesh->_is_nil()) return false;
2219
2220   SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView();
2221
2222   if (myGeomGroupBtn->isChecked()) {   // try current group on geometry actor
2223     if (!isActor) {
2224       if (!myGroupOnGeom->_is_nil()) {
2225         SMESH_Actor* anActor = SMESH::FindActorByObject(myGroupOnGeom);
2226         if (anActor && anActor->hasIO())
2227           {
2228             isActor = true;
2229             if (aViewWindow && !aViewWindow->isVisible(anActor->getIO()))
2230               isActor = false;
2231             else
2232               myActorsList.append(anActor);
2233           }
2234       }
2235     }
2236   } else {
2237     // try mesh actor
2238     SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh);
2239     if (anActor && anActor->hasIO())
2240       {
2241         isActor = true;
2242         if (aViewWindow && !aViewWindow->isVisible(anActor->getIO()))
2243           isActor = false;
2244         else
2245           myActorsList.append(anActor);
2246       }
2247     
2248     // try any visible actor of group or submesh of current mesh
2249     if (aViewWindow) {
2250       // mesh entry
2251       _PTR(SObject) aSObject = SMESH::FindSObject(myMesh);
2252       if (aSObject) {
2253         CORBA::String_var meshEntry = aSObject->GetID().c_str();
2254         int len = strlen(meshEntry);
2255         
2256         // iterate on all actors in current view window, search for
2257         // any visible actor, that belongs to group or submesh of current mesh
2258         VTK::ActorCollectionCopy aCopy(aViewWindow->getRenderer()->GetActors());
2259         vtkActorCollection *aCollection = aCopy.GetActors();
2260         int nbItems = aCollection->GetNumberOfItems();
2261         for (int i=0; i<nbItems && !isActor; i++)
2262           {
2263             SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(aCollection->GetItemAsObject(i));
2264             if (anActor && anActor->hasIO()) {
2265               Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
2266               if (aViewWindow->isVisible(anIO)) {
2267                 if (anIO->hasEntry() && strncmp(anIO->getEntry(), meshEntry, len) == 0)
2268                   myActorsList.append(anActor);
2269               }
2270             }
2271           }
2272       }
2273     }
2274   }
2275   
2276   if (myActorsList.count() > 0) {
2277     QListIterator<SMESH_Actor*> it( myActorsList );
2278     while ( it.hasNext() ) {
2279       SMESH_Actor* anActor = it.next();
2280       if ( IsActorVisible(anActor) )
2281         anActor->SetPickable(true);
2282     }
2283   }
2284   
2285   return ( isActor || (myActorsList.count() > 0) );
2286 }
2287   
2288 //=======================================================================
2289 //function : setShowEntityMode
2290 //purpose  : make shown only entity corresponding to my type
2291 //=======================================================================
2292 void SMESHGUI_GroupDlg::setShowEntityMode()
2293 {
2294   if ( !myMesh->_is_nil() ) {
2295     if ( SMESH_Actor* actor = SMESH::FindActorByObject(myMesh) ) {
2296       if (!myStoredShownEntity)
2297         myStoredShownEntity = actor->GetEntityMode();
2298       switch ( myTypeId ) {
2299       case 0: restoreShowEntityMode(); break;
2300       case 1: actor->SetEntityMode( SMESH_Actor::eEdges ); break;
2301       case 2: actor->SetEntityMode( SMESH_Actor::eFaces ); break;
2302       case 3: actor->SetEntityMode( SMESH_Actor::eVolumes ); break;
2303       }
2304     }
2305   }
2306 }
2307
2308 //=======================================================================
2309 //function : restoreShowEntityMode
2310 //purpose  : restore ShowEntity mode of myActor
2311 //=======================================================================
2312 void SMESHGUI_GroupDlg::restoreShowEntityMode()
2313 {
2314   if ( myStoredShownEntity && !myMesh->_is_nil() ) {
2315     if ( SMESH_Actor* actor = SMESH::FindActorByObject(myMesh) ) {
2316       actor->SetEntityMode(myStoredShownEntity);
2317     }
2318   }
2319   myStoredShownEntity = 0;
2320 }
2321
2322 //=======================================================================
2323 //function : IsActorVisible
2324 //purpose  : return visibility of the actor
2325 //=======================================================================
2326 bool SMESHGUI_GroupDlg::IsActorVisible( SMESH_Actor* theActor )
2327 {
2328   SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView();
2329   if (theActor && aViewWindow)
2330     return aViewWindow->isVisible(theActor->getIO());
2331   return false;
2332 }