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