Salome HOME
0021014: EDF 1583 SMESH: Improvement of the Python Dump for the creation of groups
[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 //=================================================================================
1617 // function : setCurrentSelection()
1618 // purpose  :
1619 //=================================================================================
1620 void SMESHGUI_GroupDlg::setCurrentSelection()
1621 {
1622   QPushButton* send = (QPushButton*)sender();
1623   myCurrentLineEdit = 0;
1624   if (send == myMeshGroupBtn) {
1625     disconnect(myMeshGroupBtn, SIGNAL(clicked()), this, SLOT(setCurrentSelection()));
1626     mySelectionMgr->clearSelected();
1627     if (myCreate)
1628       setSelectionMode(grpMeshSelection);
1629     else
1630       setSelectionMode(grpGroupSelection);
1631     connect(myMeshGroupBtn, SIGNAL(clicked()), this, SLOT(setCurrentSelection()));
1632     myCurrentLineEdit = myMeshGroupLine;
1633     onObjectSelectionChanged();
1634   }
1635   else if (send == mySubMeshBtn) {
1636     myCurrentLineEdit = mySubMeshLine;
1637     onObjectSelectionChanged();
1638   }
1639   else if (send == myGroupBtn) {
1640     myCurrentLineEdit = myGroupLine;
1641     onObjectSelectionChanged();
1642   }
1643 }
1644
1645
1646 //=================================================================================
1647 // function : setFilters()
1648 // purpose  : SLOT. Called when "Filter" button pressed.
1649 //=================================================================================
1650 void SMESHGUI_GroupDlg::setFilters()
1651 {
1652   if(myMesh->_is_nil()) {
1653     SUIT_MessageBox::critical(this,
1654                               tr("SMESH_ERROR"),
1655                               tr("NO_MESH_SELECTED"));
1656    return;
1657   }
1658
1659   SMESH::ElementType aType = SMESH::ALL;
1660   switch ( myTypeId )
1661   {
1662     case 0 : aType = SMESH::NODE; break;
1663     case 1 : aType = SMESH::EDGE; break;
1664     case 2 : aType = SMESH::FACE; break;
1665     case 3 : aType = SMESH::VOLUME; break;
1666     default: return;
1667   }
1668
1669   if ( myFilterDlg == 0 )
1670   {
1671     myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, aType );
1672     connect( myFilterDlg, SIGNAL( Accepted() ), SLOT( onFilterAccepted() ) );
1673   }
1674   else
1675     myFilterDlg->Init( aType );
1676
1677   if ( !myGroupOnFilter->_is_nil() )
1678   {
1679     myFilterDlg->SetFilter( myFilter, aType );
1680     myFilterDlg->Init( aType );
1681   }
1682
1683   myFilterDlg->SetSelection();
1684   myFilterDlg->SetMesh( myMesh );
1685   myFilterDlg->SetSourceWg( myElements, false );
1686
1687   myFilterDlg->show();
1688 }
1689
1690 //=================================================================================
1691 // function : onFilterAccepted()
1692 // purpose  : SLOT. Called when Filter dlg closed with OK button.
1693 //            Uncheck "Select submesh" and "Select group" checkboxes
1694 //=================================================================================
1695 void SMESHGUI_GroupDlg::onFilterAccepted()
1696 {
1697   if ( mySelectSubMesh->isChecked() || mySelectGroup->isChecked() )
1698   {
1699     mySelectionMode = myTypeId;
1700     mySelectSubMesh->setChecked( false );
1701     mySelectGroup->setChecked( false );
1702   }
1703   // get a filter from myFilterDlg
1704   myFilter = myFilterDlg->GetFilter();
1705   if ( !myFilter->_is_nil() ) {
1706     SMESH::Predicate_var perdicate = myFilter->GetPredicate();
1707     if ( perdicate->_is_nil() )
1708       myFilter = SMESH::Filter::_nil();
1709   }
1710   // set mesh to myFilter
1711   if ( !myFilter->_is_nil() ) {
1712     SMESH::SMESH_Mesh_var mesh = myMesh;
1713     if ( mesh->_is_nil() ) {
1714       if ( !myGroup->_is_nil() )
1715         mesh = myGroup->GetMesh();
1716       else if ( !myGroupOnGeom->_is_nil() )
1717         mesh = myGroupOnGeom->GetMesh();
1718       else if ( !myGroupOnFilter->_is_nil() )
1719         mesh = myGroupOnFilter->GetMesh();
1720     }
1721     myFilter->SetMesh( mesh );
1722   }
1723
1724   updateButtons();
1725 }
1726
1727 //=================================================================================
1728 // function : onAdd()
1729 // purpose  :
1730 //=================================================================================
1731 void SMESHGUI_GroupDlg::onAdd()
1732 {
1733   SALOME_ListIO aList;
1734   mySelectionMgr->selectedObjects( aList );
1735
1736   int aNbSel = aList.Extent();
1737
1738   if (aNbSel == 0 || myActorsList.count() == 0 || myMesh->_is_nil()) return;
1739
1740   myIsBusy = true;
1741   int sizeBefore = myElements->count();
1742
1743   SMESH::ElementType aType = SMESH::ALL;
1744   switch(myTypeId) {
1745   case 0:
1746     aType = SMESH::NODE;
1747     mySelector->SetSelectionMode(NodeSelection);
1748     break;
1749   case 1:
1750     aType = SMESH::EDGE;
1751     mySelector->SetSelectionMode(EdgeSelection);
1752     break;
1753   case 2:
1754     aType = SMESH::FACE;
1755     mySelector->SetSelectionMode(FaceSelection);
1756     break;
1757   case 3:
1758     aType = SMESH::VOLUME;
1759     mySelector->SetSelectionMode(VolumeSelection);
1760     break;
1761   default:
1762     mySelector->SetSelectionMode(ActorSelection);
1763   }
1764
1765   QListWidgetItem* anItem = 0;
1766   QList<QListWidgetItem*> listItemsToSel;
1767
1768   if (myCurrentLineEdit == 0) {
1769     //if (aNbSel != 1) { myIsBusy = false; return; }
1770     QString aListStr = "";
1771     int aNbItems = 0;
1772     if (myTypeId == 0) {
1773       QListIterator<SMESH_Actor*> it( myActorsList );
1774       while ( it.hasNext() ) {
1775         QString tmpStr;
1776         aNbItems += SMESH::GetNameOfSelectedNodes(mySelector, it.next()->getIO(), tmpStr);
1777         aListStr += tmpStr;
1778       }
1779     }
1780     else {
1781       QListIterator<SMESH_Actor*> it( myActorsList );
1782       while ( it.hasNext() ) {
1783         QString tmpStr;
1784         aNbItems += SMESH::GetNameOfSelectedElements(mySelector, it.next()->getIO(), tmpStr);
1785         aListStr += tmpStr;
1786       }
1787     }
1788     if (aNbItems > 0) {
1789       QStringList anElements = aListStr.split( " ", QString::SkipEmptyParts);
1790       for (QStringList::iterator it = anElements.begin(); it != anElements.end(); ++it) {
1791         QList<QListWidgetItem*> found = myElements->findItems(*it, Qt::MatchExactly);
1792         if (found.count() == 0) {
1793           anItem = new QListWidgetItem(*it);
1794           myElements->addItem(anItem);
1795           if (!anItem->isSelected())
1796             listItemsToSel.push_back(anItem);
1797         }
1798         else {
1799           foreach(anItem, found)
1800             if (!anItem->isSelected())
1801               listItemsToSel.push_back(anItem);
1802         }
1803       }
1804       bool blocked = myElements->signalsBlocked();
1805       myElements->blockSignals(true);
1806       foreach(anItem, listItemsToSel) anItem->setSelected(true);
1807       myElements->blockSignals(blocked);
1808       onListSelectionChanged();
1809       listItemsToSel.clear();
1810     }
1811   } else if (myCurrentLineEdit == mySubMeshLine) {
1812     //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1813
1814     SALOME_ListIO aList;
1815     mySelectionMgr->selectedObjects( aList );
1816
1817     SALOME_ListIteratorOfListIO anIt (aList);
1818     for ( ; anIt.More(); anIt.Next()) {
1819       SMESH::SMESH_subMesh_var aSubMesh =
1820         SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIt.Value());
1821       if (!aSubMesh->_is_nil()) {
1822         // check if mesh is the same
1823         if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
1824           try {
1825             SMESH::long_array_var anElements = aSubMesh->GetElementsByType(aType);
1826             int k = anElements->length();
1827             for (int i = 0; i < k; i++) {
1828               QString aText = QString::number(anElements[i]);
1829               QList<QListWidgetItem*> found = myElements->findItems(aText, Qt::MatchExactly);
1830               if (found.count() == 0) {
1831                 anItem = new QListWidgetItem(aText);
1832                 myElements->addItem(anItem);
1833                 if (!anItem->isSelected())
1834                   listItemsToSel.push_back(anItem);
1835               }
1836               else {
1837                 foreach(anItem, found)
1838                   if (!anItem->isSelected())
1839                     listItemsToSel.push_back(anItem);
1840               }
1841             }
1842             bool blocked = myElements->signalsBlocked();
1843             myElements->blockSignals(true);
1844             foreach(anItem, listItemsToSel) anItem->setSelected(true);
1845             myElements->blockSignals(blocked);
1846             onListSelectionChanged();
1847             listItemsToSel.clear();
1848           }
1849           catch (const SALOME::SALOME_Exception& ex) {
1850             SalomeApp_Tools::QtCatchCorbaException(ex);
1851           }
1852         }
1853       }
1854     }
1855     mySelectSubMesh->setChecked(false);
1856     myIsBusy = false;
1857     onListSelectionChanged();
1858
1859   } else if (myCurrentLineEdit == myGroupLine) {
1860     //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1861     SALOME_ListIO aList;
1862     mySelectionMgr->selectedObjects( aList );
1863
1864     SALOME_ListIteratorOfListIO anIt (aList);
1865     for ( ; anIt.More(); anIt.Next()) {
1866       SMESH::SMESH_GroupBase_var aGroup =
1867         SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(anIt.Value());
1868       if (!aGroup->_is_nil()) {
1869         // check if mesh is the same
1870         if (aGroup->GetType() == aType && aGroup->GetMesh()->GetId() == myMesh->GetId()) {
1871           SMESH::long_array_var anElements = aGroup->GetListOfID();
1872           int k = anElements->length();
1873           for (int i = 0; i < k; i++) {
1874             QString aText = QString::number(anElements[i]);
1875             QList<QListWidgetItem*> found = myElements->findItems(aText, Qt::MatchExactly);
1876             if (found.count() == 0) {
1877               anItem = new QListWidgetItem(aText);
1878               myElements->addItem(anItem);
1879               if (!anItem->isSelected())
1880                 listItemsToSel.push_back(anItem);
1881             }
1882             else {
1883               foreach(anItem, found)
1884                 if (!anItem->isSelected())
1885                   listItemsToSel.push_back(anItem);
1886             }
1887           }
1888           bool blocked = myElements->signalsBlocked();
1889           myElements->blockSignals(true);
1890           foreach(anItem, listItemsToSel) anItem->setSelected(true);
1891           myElements->blockSignals(blocked);
1892           onListSelectionChanged();
1893           listItemsToSel.clear();
1894         }
1895       }
1896     }
1897     mySelectGroup->setChecked(false);
1898     myIsBusy = false;
1899     onListSelectionChanged();
1900
1901   } else if (myCurrentLineEdit == myGeomGroupLine && myGeomObjects->length() == 1) {
1902     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1903     GEOM::GEOM_IGroupOperations_var aGroupOp =
1904       SMESH::GetGEOMGen()->GetIGroupOperations(aStudy->StudyId());
1905
1906     SMESH::ElementType aGroupType = SMESH::ALL;
1907     switch(aGroupOp->GetType(myGeomObjects[0])) {
1908     case 7: aGroupType = SMESH::NODE; break;
1909     case 6: aGroupType = SMESH::EDGE; break;
1910     case 4: aGroupType = SMESH::FACE; break;
1911     case 2: aGroupType = SMESH::VOLUME; break;
1912     default: myIsBusy = false; return;
1913     }
1914
1915     if (aGroupType == aType) {
1916       _PTR(SObject) aGroupSO =
1917         //aStudy->FindObjectIOR(aStudy->ConvertObjectToIOR(myGeomGroup));
1918         aStudy->FindObjectID(myGeomObjects[0]->GetStudyEntry());
1919       // Construct filter
1920       SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
1921       SMESH::Filter_var aFilter = aFilterMgr->CreateFilter();
1922       SMESH::BelongToGeom_var aBelongToGeom = aFilterMgr->CreateBelongToGeom();
1923       aBelongToGeom->SetGeom(myGeomObjects[0]);
1924       aBelongToGeom->SetShapeName(aGroupSO->GetName().c_str());
1925       aBelongToGeom->SetElementType(aType);
1926       aFilter->SetPredicate(aBelongToGeom);
1927
1928       SMESH::long_array_var anElements = aFilter->GetElementsId(myMesh);
1929
1930       int k = anElements->length();
1931       for (int i = 0; i < k; i++) {
1932         QString aText = QString::number(anElements[i]);
1933         QList<QListWidgetItem*> found = myElements->findItems(aText, Qt::MatchExactly);
1934         if (found.count() == 0) {
1935           anItem = new QListWidgetItem(aText);
1936           myElements->addItem(anItem);
1937           if (!anItem->isSelected())
1938             listItemsToSel.push_back(anItem);
1939         }
1940         else {
1941           foreach(anItem, found)
1942             if (!anItem->isSelected())
1943               listItemsToSel.push_back(anItem);
1944         }
1945       }
1946       bool blocked = myElements->signalsBlocked();
1947       myElements->blockSignals(true);
1948       foreach(anItem, listItemsToSel) anItem->setSelected(true);
1949       myElements->blockSignals(blocked);
1950       onListSelectionChanged();
1951       listItemsToSel.clear();
1952     }
1953
1954     //VSR: mySelectGeomGroup->setChecked(false);
1955     myIsBusy = false;
1956     onListSelectionChanged();
1957   }
1958   myIsBusy = false;
1959   if ( sizeBefore < myElements->count() )
1960     ++myNbChangesOfContents;
1961   //  mySelectionMgr->clearSelected();
1962   updateButtons();
1963 }
1964
1965 //=================================================================================
1966 // function : onRemove()
1967 // purpose  :
1968 //=================================================================================
1969 void SMESHGUI_GroupDlg::onRemove()
1970 {
1971   myIsBusy = true;
1972   int sizeBefore = myElements->count();
1973
1974   if (myCurrentLineEdit == 0) {
1975     QList<QListWidgetItem*> selItems = myElements->selectedItems();
1976     QListWidgetItem* item;
1977     foreach(item, selItems) delete item;
1978   } else {
1979     SALOME_ListIO aList;
1980     mySelectionMgr->selectedObjects( aList );
1981
1982     int aNbSel = aList.Extent();
1983
1984     if (aNbSel == 0) { myIsBusy = false; return; }
1985
1986     SMESH::ElementType aType = SMESH::ALL;
1987     switch(myTypeId) {
1988     case 0: aType = SMESH::NODE; break;
1989     case 1: aType = SMESH::EDGE; break;
1990     case 2: aType = SMESH::FACE; break;
1991     case 3: aType = SMESH::VOLUME; break;
1992     }
1993
1994     if (myCurrentLineEdit == mySubMeshLine) {
1995       //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1996       SALOME_ListIO aList;
1997       mySelectionMgr->selectedObjects( aList );
1998
1999       SALOME_ListIteratorOfListIO anIt (aList);
2000       for ( ; anIt.More(); anIt.Next()) {
2001         SMESH::SMESH_subMesh_var aSubMesh = SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIt.Value());
2002         if (!aSubMesh->_is_nil()) {
2003           // check if mesh is the same
2004           if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
2005             if (aType == SMESH::NODE) {
2006               try {
2007                 SMESH::long_array_var anElements = aSubMesh->GetNodesId();
2008                 int k = anElements->length();
2009                 for (int i = 0; i < k; i++) {
2010                   QList<QListWidgetItem*> found = 
2011                     myElements->findItems(QString::number(anElements[i]), Qt::MatchExactly);
2012                   QListWidgetItem* anItem;
2013                   foreach(anItem, found) delete anItem;
2014                 }
2015               }
2016               catch (const SALOME::SALOME_Exception& ex) {
2017                 SalomeApp_Tools::QtCatchCorbaException(ex);
2018               }
2019             }
2020             else {
2021               try {
2022                 SMESH::long_array_var anElements = aSubMesh->GetElementsId();
2023                 int k = anElements->length();
2024                 for (int i = 0; i < k; i++) {
2025                   QList<QListWidgetItem*> found = 
2026                     myElements->findItems(QString::number(anElements[i]), Qt::MatchExactly);
2027                   QListWidgetItem* anItem;
2028                   foreach(anItem, found) delete anItem;
2029                 }
2030               }
2031               catch (const SALOME::SALOME_Exception& ex) {
2032                 SalomeApp_Tools::QtCatchCorbaException(ex);
2033               }
2034             }
2035           }
2036         }
2037       }
2038     }
2039     else if (myCurrentLineEdit == myGroupLine) {
2040       Standard_Boolean aRes;
2041       //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
2042       SALOME_ListIO aList;
2043       mySelectionMgr->selectedObjects( aList );
2044
2045       SALOME_ListIteratorOfListIO anIt (aList);
2046       for ( ; anIt.More(); anIt.Next()) {
2047         SMESH::SMESH_Group_var aGroup = SMESH::IObjectToInterface<SMESH::SMESH_Group>(anIt.Value());
2048         if (aRes && !aGroup->_is_nil()) {
2049           // check if mesh is the same
2050           if (aGroup->GetType() == aType && aGroup->GetMesh()->GetId() == myMesh->GetId()) {
2051             SMESH::long_array_var anElements = aGroup->GetListOfID();
2052             int k = anElements->length();
2053             for (int i = 0; i < k; i++) {
2054               QList<QListWidgetItem*> found = 
2055                 myElements->findItems(QString::number(anElements[i]), Qt::MatchExactly);
2056               QListWidgetItem* anItem;
2057               foreach(anItem, found) delete anItem;
2058             }
2059           }
2060         }
2061       }
2062     }
2063   }
2064   myIsBusy = false;
2065   if ( sizeBefore > myElements->count() )
2066     myNbChangesOfContents += 2; // it's used to detect that "Add" was only once
2067   updateButtons();
2068 }
2069
2070 //=================================================================================
2071 // function : onSort()
2072 // purpose  :
2073 //=================================================================================
2074 void SMESHGUI_GroupDlg::onSort()
2075 {
2076   // PAL5412: sorts items in ascending by "string" value
2077   // myElements->sort(true);
2078   // myElements->update();
2079   int i, k = myElements->count();
2080   if (k > 0) {
2081     myIsBusy = true;
2082     QList<int> aSelected;
2083     std::vector<int> anArray(k);
2084     //    QMemArray<int> anArray(k);
2085     // fill the array
2086     for (i = 0; i < k; i++) {
2087       int id = myElements->item(i)->text().toInt();
2088       anArray[i] = id;
2089       if (myElements->item(i)->isSelected())
2090         aSelected.append(id);
2091     }
2092     // sort & update list
2093     std::sort(anArray.begin(), anArray.end());
2094     //    anArray.sort();
2095     myElements->clear();
2096     QListWidgetItem* anItem;
2097     QList<QListWidgetItem*> listItemsToSel;
2098     for (i = 0; i < k; i++) {
2099       anItem = new QListWidgetItem(QString::number(anArray[i]));
2100       myElements->addItem(anItem);
2101       if (aSelected.contains(anArray[i]))
2102         listItemsToSel.push_back(anItem);
2103     }
2104     bool blocked = myElements->signalsBlocked();
2105     myElements->blockSignals(true);
2106     foreach(anItem, listItemsToSel) anItem->setSelected(true);
2107     myElements->blockSignals(blocked);
2108     listItemsToSel.clear();
2109     myIsBusy = false;
2110   }
2111 }
2112
2113 //=================================================================================
2114 // function : closeEvent()
2115 // purpose  :
2116 //=================================================================================
2117 void SMESHGUI_GroupDlg::closeEvent (QCloseEvent*)
2118 {
2119   onClose();
2120 }
2121
2122 //=================================================================================
2123 // function : onVisibilityChanged()
2124 // purpose  :
2125 //=================================================================================
2126 void SMESHGUI_GroupDlg::onVisibilityChanged()
2127 {
2128   SetAppropriateActor();
2129 }
2130
2131 //=================================================================================
2132 // function : SMESHGUI_GroupDlg::onClose
2133 // purpose  : SLOT called when "Close" button pressed. Close dialog
2134 //=================================================================================
2135 void SMESHGUI_GroupDlg::onClose()
2136 {
2137   if (SMESH::GetCurrentVtkView()) {
2138     SMESH::RemoveFilters(); // PAL6938 -- clean all mesh entity filters
2139     SMESH::SetPointRepresentation(false);
2140     SMESH::SetPickable();
2141     restoreShowEntityMode();
2142   }
2143
2144   if( isApplyAndClose() && !myObjectToSelect.isEmpty() ) {
2145     SUIT_DataOwnerPtrList aList;
2146     aList.append( new LightApp_DataOwner( myObjectToSelect ) );
2147     mySelectionMgr->setSelected( aList );
2148   }
2149   else
2150     mySelectionMgr->clearSelected();
2151   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
2152     aViewWindow->SetSelectionMode(ActorSelection);
2153   mySelectionMgr->clearFilters();
2154   mySMESHGUI->ResetState();
2155
2156   reject();
2157 }
2158
2159 //=================================================================================
2160 // function : onHelp()
2161 // purpose  :
2162 //=================================================================================
2163 void SMESHGUI_GroupDlg::onHelp()
2164 {
2165   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
2166   if (app)
2167     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString( "" ), myHelpFileName);
2168   else {
2169     QString platform;
2170 #ifdef WIN32
2171     platform = "winapplication";
2172 #else
2173     platform = "application";
2174 #endif
2175     SUIT_MessageBox::warning(this, tr( "WRN_WARNING" ),
2176                              tr( "EXTERNAL_BROWSER_CANNOT_SHOW_PAGE" ).
2177                              arg(app->resourceMgr()->stringValue( "ExternalBrowser",
2178                                                                  platform)).
2179                              arg(myHelpFileName));
2180   }
2181 }
2182
2183 //=================================================================================
2184 // function : SMESHGUI_GroupDlg::onDeactivate
2185 // purpose  : SLOT called when dialog must be deativated
2186 //=================================================================================
2187 void SMESHGUI_GroupDlg::onDeactivate()
2188 {
2189   mySMESHGUI->ResetState();
2190   setEnabled(false);
2191 }
2192
2193 //=================================================================================
2194 // function : SMESHGUI_GroupDlg::enterEvent
2195 // purpose  : Event filter
2196 //=================================================================================
2197 void SMESHGUI_GroupDlg::enterEvent (QEvent*)
2198 {
2199   if (!isEnabled()) {
2200     mySMESHGUI->EmitSignalDeactivateDialog();
2201     setEnabled(true);
2202     mySelectionMode = grpNoSelection;
2203     setSelectionMode(myTypeId);
2204     //mySMESHGUI->SetActiveDialogBox((QDialog*)this);
2205     mySMESHGUI->SetActiveDialogBox(this);
2206     mySMESHGUI->SetState(800);
2207   }
2208 }
2209
2210 //=================================================================================
2211 // function : hideEvent
2212 // purpose  : caused by ESC key
2213 //=================================================================================
2214 void SMESHGUI_GroupDlg::hideEvent (QHideEvent*)
2215 {
2216   if (!isMinimized() && !myIsBusy)
2217     onClose();
2218 }
2219
2220 //=================================================================================
2221 // function : keyPressEvent()
2222 // purpose  :
2223 //=================================================================================
2224 void SMESHGUI_GroupDlg::keyPressEvent( QKeyEvent* e )
2225 {
2226   QDialog::keyPressEvent( e );
2227   if ( e->isAccepted() )
2228     return;
2229
2230   if ( e->key() == Qt::Key_F1 )
2231     {
2232       e->accept();
2233       onHelp();
2234     }
2235 }
2236
2237 //================================================================================
2238 /*!
2239  * \brief Enable showing of the popup when Geometry selection btn is clicked
2240   * \param enable - true to enable
2241  */
2242 //================================================================================
2243
2244 enum { DIRECT_GEOM_INDEX = 0, GEOM_BY_MESH_INDEX };
2245
2246 void SMESHGUI_GroupDlg::updateGeomPopup()
2247 {
2248   bool enable = false;
2249
2250   if ( !myMesh->_is_nil() )
2251     enable = myMesh->NbEdges() > 0;
2252
2253   if ( myGeomGroupBtn )
2254   {
2255     disconnect( myGeomGroupBtn, SIGNAL( toggled(bool) ), this, SLOT( onGeomSelectionButton(bool) ));
2256     if ( enable ) {
2257       if ( !myGeomPopup ) {
2258         myGeomPopup = new QMenu(this);
2259         myActions[myGeomPopup->addAction( tr( "DIRECT_GEOM_SELECTION" ) )] = DIRECT_GEOM_INDEX;
2260         myActions[myGeomPopup->addAction( tr( "GEOM_BY_MESH_ELEM_SELECTION" ) )] = GEOM_BY_MESH_INDEX;
2261         connect( myGeomPopup, SIGNAL( triggered( QAction* ) ), SLOT( onGeomPopup( QAction* ) ) );
2262       }
2263       connect( myGeomGroupBtn, SIGNAL( toggled(bool) ), this, SLOT( onGeomSelectionButton(bool) ));
2264     }
2265   }
2266 }
2267
2268
2269 //=================================================================================
2270 // function : onGeomSelectionButton()
2271 // purpose  :
2272 //=================================================================================
2273 void SMESHGUI_GroupDlg::onGeomSelectionButton(bool isBtnOn)
2274 {
2275   if ( myGeomPopup && isBtnOn )
2276     {
2277       myCurrentLineEdit = myGeomGroupLine;
2278       QAction* a = myGeomPopup->exec( QCursor::pos() );
2279       if (!a || myActions[a] == DIRECT_GEOM_INDEX)
2280         setSelectionMode(grpGeomSelection);
2281     }
2282   else if (!isBtnOn)
2283     {
2284       myCurrentLineEdit = 0;
2285       setSelectionMode(grpAllSelection);
2286     }
2287 }
2288
2289 //=================================================================================
2290 // function : onGeomPopup()
2291 // purpose  :
2292 //=================================================================================
2293 void SMESHGUI_GroupDlg::onGeomPopup( QAction* a )
2294 {
2295   int index = myActions[a];
2296   if ( index == GEOM_BY_MESH_INDEX )
2297     {
2298       mySelectionMode = grpNoSelection;
2299       if ( !myShapeByMeshOp ) {
2300         myShapeByMeshOp = new SMESHGUI_ShapeByMeshOp(true);
2301         connect(myShapeByMeshOp, SIGNAL(committed(SUIT_Operation*)),
2302                 SLOT(onPublishShapeByMeshDlg(SUIT_Operation*)));
2303         connect(myShapeByMeshOp, SIGNAL(aborted(SUIT_Operation*)),
2304                 SLOT(onCloseShapeByMeshDlg(SUIT_Operation*)));
2305       }
2306       // set mesh object to SMESHGUI_ShapeByMeshOp and start it
2307       if ( !myMesh->_is_nil() ) {
2308         myIsBusy = true;
2309         hide(); // stop processing selection
2310         myIsBusy = false;
2311         myShapeByMeshOp->setModule( mySMESHGUI );
2312         myShapeByMeshOp->setStudy( 0 ); // it's really necessary
2313         myShapeByMeshOp->SetMesh( myMesh );
2314         myShapeByMeshOp->start();
2315       }
2316     }
2317 }
2318
2319 //================================================================================
2320 /*!
2321  * \brief SLOT. Is called when Ok is pressed in SMESHGUI_ShapeByMeshDlg
2322  */
2323 //================================================================================
2324
2325 void SMESHGUI_GroupDlg::onPublishShapeByMeshDlg(SUIT_Operation* op)
2326 {
2327   if ( myShapeByMeshOp == op ) {
2328     mySMESHGUI->getApp()->updateObjectBrowser();
2329     show();
2330     // Select a found geometry object
2331     GEOM::GEOM_Object_var aGeomVar = myShapeByMeshOp->GetShape();
2332     if ( !aGeomVar->_is_nil() )
2333     {
2334       QString ID = aGeomVar->GetStudyEntry();
2335       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
2336       if ( _PTR(SObject) aGeomSO = aStudy->FindObjectID( ID.toLatin1().data() )) {
2337         SALOME_ListIO anIOList;
2338         Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject
2339           ( aGeomSO->GetID().c_str(), "SMESH", aGeomSO->GetName().c_str() );
2340         anIOList.Append( anIO );
2341         mySelectionMgr->setSelectedObjects( anIOList, false );
2342         onObjectSelectionChanged();
2343       }
2344     }
2345   }
2346 }
2347
2348 //================================================================================
2349 /*!
2350  * \brief SLOT. Is called when Close is pressed in SMESHGUI_ShapeByMeshDlg
2351  */
2352 //================================================================================
2353
2354 void SMESHGUI_GroupDlg::onCloseShapeByMeshDlg(SUIT_Operation* op)
2355 {
2356   if ( myShapeByMeshOp == op )
2357     {
2358       show();
2359       setSelectionMode(grpGeomSelection);
2360     }
2361 }
2362
2363 //=================================================================================
2364 // function : setGroupColor()
2365 // purpose  :
2366 //=================================================================================
2367 void SMESHGUI_GroupDlg::setGroupColor( const SALOMEDS::Color& theColor )
2368 {
2369   QColor aQColor( (int)( theColor.R * 255.0 ),
2370                   (int)( theColor.G * 255.0 ),
2371                   (int)( theColor.B * 255.0 ) );
2372   setGroupQColor( aQColor );
2373 }
2374
2375 //=================================================================================
2376 // function : getGroupColor()
2377 // purpose  :
2378 //=================================================================================
2379 SALOMEDS::Color SMESHGUI_GroupDlg::getGroupColor() const
2380 {
2381   QColor aQColor = getGroupQColor();
2382
2383   SALOMEDS::Color aColor;
2384   aColor.R = (float)aQColor.red() / 255.0;
2385   aColor.G = (float)aQColor.green() / 255.0;
2386   aColor.B = (float)aQColor.blue() / 255.0;
2387
2388   return aColor;
2389 }
2390
2391 //=================================================================================
2392 // function : setGroupQColor()
2393 // purpose  :
2394 //=================================================================================
2395 void SMESHGUI_GroupDlg::setGroupQColor( const QColor& theColor )
2396 {
2397   if( theColor.isValid() )
2398     myColorBtn->setColor( theColor );
2399 }
2400
2401 //=================================================================================
2402 // function : getGroupQColor()
2403 // purpose  :
2404 //=================================================================================
2405 QColor SMESHGUI_GroupDlg::getGroupQColor() const
2406 {
2407   return myColorBtn->color();
2408 }
2409
2410 //=================================================================================
2411 // function : setDefaultGroupColor()
2412 // purpose  :
2413 //=================================================================================
2414 void SMESHGUI_GroupDlg::setDefaultGroupColor()
2415 {
2416   if( myMesh->_is_nil() )
2417     return;
2418
2419   bool isAutoColor = myMesh->GetAutoColor();
2420
2421   QColor aQColor;
2422   if( !isAutoColor )
2423   {
2424     int r = 0, g = 0, b = 0;
2425     SMESH::GetColor( "SMESH", "fill_color", r, g, b, QColor( 0, 170, 255 ) );
2426     aQColor.setRgb( r, g, b );
2427   }
2428   else
2429   {
2430     SMESH::ListOfGroups aListOfGroups = *myMesh->GetGroups();
2431
2432     QList<SALOMEDS::Color> aReservedColors;
2433     for( int i = 0, n = aListOfGroups.length(); i < n; i++ )
2434     {
2435       SMESH::SMESH_GroupBase_var aGroupObject = aListOfGroups[i];
2436       SALOMEDS::Color aReservedColor = aGroupObject->GetColor();
2437       aReservedColors.append( aReservedColor );
2438     }
2439
2440     SALOMEDS::Color aColor = SMESHGUI::getUniqueColor( aReservedColors );
2441     aQColor.setRgb( (int)( aColor.R * 255.0 ),
2442                     (int)( aColor.G * 255.0 ),
2443                     (int)( aColor.B * 255.0 ) );
2444
2445   }
2446
2447   setGroupQColor( aQColor );
2448 }
2449
2450 //=================================================================================
2451 // function : SetAppropriateActor()
2452 // purpose  : Find more appropriate of visible actors, set it to myActor, allow picking
2453 //            NPAL19389: create a group with a selection in another group.
2454 //            if mesh actor is not visible - find any first visible group or submesh
2455 //=================================================================================
2456 bool SMESHGUI_GroupDlg::SetAppropriateActor()
2457 {
2458   bool isActor = false;
2459   myActorsList.clear();
2460
2461   if (myMesh->_is_nil()) return false;
2462
2463   SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView();
2464
2465   if (myGeomGroupBtn->isChecked()) {   // try current group on geometry actor
2466     if (!isActor) {
2467       if (!myGroupOnGeom->_is_nil()) {
2468         SMESH_Actor* anActor = SMESH::FindActorByObject(myGroupOnGeom);
2469         if (anActor && anActor->hasIO())
2470           {
2471             isActor = true;
2472             if (aViewWindow && !aViewWindow->isVisible(anActor->getIO()))
2473               isActor = false;
2474             else
2475               myActorsList.append(anActor);
2476           }
2477       }
2478     }
2479   } else {
2480     // try mesh actor
2481     SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh);
2482     if (anActor && anActor->hasIO()) {
2483       isActor = true;
2484       if (aViewWindow && !aViewWindow->isVisible(anActor->getIO()))
2485         isActor = false;
2486       else
2487         myActorsList.append(anActor);
2488     }
2489     
2490     // try group actor
2491     if (!isActor && !myGroup->_is_nil()) {
2492       SMESH_Actor* anActor = SMESH::FindActorByObject(myGroup);
2493       if (anActor && anActor->hasIO())
2494         myActorsList.append(anActor);
2495     }
2496     
2497     // try any visible actor of group or submesh of current mesh
2498     if (aViewWindow) {
2499       // mesh entry
2500       _PTR(SObject) aSObject = SMESH::FindSObject(myMesh);
2501       if (aSObject) {
2502         CORBA::String_var meshEntry = aSObject->GetID().c_str();
2503         int len = strlen(meshEntry);
2504         
2505         // iterate on all actors in current view window, search for
2506         // any visible actor, that belongs to group or submesh of current mesh
2507         VTK::ActorCollectionCopy aCopy(aViewWindow->getRenderer()->GetActors());
2508         vtkActorCollection *aCollection = aCopy.GetActors();
2509         int nbItems = aCollection->GetNumberOfItems();
2510         for (int i=0; i<nbItems && !isActor; i++)
2511           {
2512             SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(aCollection->GetItemAsObject(i));
2513             if (anActor && anActor->hasIO()) {
2514               Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
2515               if (aViewWindow->isVisible(anIO)) {
2516                 if (anIO->hasEntry() && strncmp(anIO->getEntry(), meshEntry, len) == 0 && !myActorsList.contains(anActor) )
2517                   myActorsList.append(anActor);
2518               }
2519             }
2520           }
2521       }
2522     }
2523   }
2524   
2525   if (myActorsList.count() > 0) {
2526     QListIterator<SMESH_Actor*> it( myActorsList );
2527     while ( it.hasNext() ) {
2528       SMESH_Actor* anActor = it.next();
2529       if ( IsActorVisible(anActor) )
2530         anActor->SetPickable(true);
2531     }
2532   }
2533   
2534   return ( isActor || (myActorsList.count() > 0) );
2535 }
2536   
2537 //=======================================================================
2538 //function : setShowEntityMode
2539 //purpose  : make shown only entity corresponding to my type
2540 //=======================================================================
2541 void SMESHGUI_GroupDlg::setShowEntityMode()
2542 {
2543   if ( !myMesh->_is_nil() ) {
2544     if ( SMESH_Actor* actor = SMESH::FindActorByObject(myMesh) ) {
2545       if (!myStoredShownEntity)
2546         myStoredShownEntity = actor->GetEntityMode();
2547       switch ( myTypeId ) {
2548       case 0: restoreShowEntityMode(); break;
2549       case 1: actor->SetEntityMode( SMESH_Actor::eEdges ); break;
2550       case 2: actor->SetEntityMode( SMESH_Actor::eFaces ); break;
2551       case 3: actor->SetEntityMode( SMESH_Actor::eVolumes ); break;
2552       }
2553     }
2554   }
2555 }
2556
2557 //=======================================================================
2558 //function : restoreShowEntityMode
2559 //purpose  : restore ShowEntity mode of myActor
2560 //=======================================================================
2561 void SMESHGUI_GroupDlg::restoreShowEntityMode()
2562 {
2563   if ( myStoredShownEntity && !myMesh->_is_nil() ) {
2564     if ( SMESH_Actor* actor = SMESH::FindActorByObject(myMesh) ) {
2565       actor->SetEntityMode(myStoredShownEntity);
2566     }
2567   }
2568   myStoredShownEntity = 0;
2569 }
2570
2571 //=======================================================================
2572 //function : IsActorVisible
2573 //purpose  : return visibility of the actor
2574 //=======================================================================
2575 bool SMESHGUI_GroupDlg::IsActorVisible( SMESH_Actor* theActor )
2576 {
2577   SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView();
2578   if (theActor && aViewWindow)
2579     return aViewWindow->isVisible(theActor->getIO());
2580   return false;
2581 }
2582
2583 //================================================================
2584 //function : setIsApplyAndClose
2585 //purpose  : Set value of the flag indicating that the dialog is
2586 //           accepted by Apply & Close button
2587 //================================================================
2588 void SMESHGUI_GroupDlg::setIsApplyAndClose( const bool theFlag )
2589 {
2590   myIsApplyAndClose = theFlag;
2591 }
2592
2593 //================================================================
2594 //function : isApplyAndClose
2595 //purpose  : Get value of the flag indicating that the dialog is
2596 //           accepted by Apply & Close button
2597 //================================================================
2598 bool SMESHGUI_GroupDlg::isApplyAndClose() const
2599 {
2600   return myIsApplyAndClose;
2601 }