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