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