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