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