Salome HOME
52846: It is impossible to select edge in OB as reversed edge for hypothesis
[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     myIsApplyAndClose( false ),
132     myNbChangesOfContents(0)
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   }
616   myTypeGroup->button(aType)->setChecked(true);
617
618   myGroup         = SMESH::SMESH_Group::_narrow( theGroup );
619   myGroupOnGeom   = SMESH::SMESH_GroupOnGeom::_narrow( theGroup );
620   myGroupOnFilter = SMESH::SMESH_GroupOnFilter::_narrow( theGroup );
621   myFilter        = SMESH::Filter::_nil();
622
623   if (myGroup->_is_nil() && myGroupOnGeom->_is_nil() && myGroupOnFilter->_is_nil() )
624     return;
625
626   // NPAL19389: create a group with a selection in another group
627   // set actor of myMesh, if it is visible, else set
628   // actor of theGroup, if it is visible, else try
629   // any visible actor of group or submesh of myMesh
630   // commented, because an attempt to set selection on not displayed cells leads to error
631   SetAppropriateActor();
632
633   /*  SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh);
634   if ( !anActor )
635     anActor = SMESH::FindActorByObject(theGroup);
636   SMESH::SetPickable(anActor);*/
637
638   int grpType = (!myGroup->_is_nil() ? 0 : (theIsConvert ? 0 : myGroupOnGeom->_is_nil() ? 2 : 1));
639   myGrpTypeGroup->button(grpType)->setChecked(true);
640   onGrpTypeChanged(grpType);
641
642   myTypeId = aType;
643   if ( grpType == 0 ) { // standalone group
644     myCurrentLineEdit = 0;
645     myElements->clear();
646     myAllowElemsModif->setChecked( true );
647
648     setSelectionMode(aType);
649
650     setShowEntityMode(); // depends on myTypeId
651
652     myIdList.clear();
653     if (!theGroup->IsEmpty()) {
654       SMESH::long_array_var anElements = theGroup->GetListOfID();
655       int k = anElements->length();
656       for (int i = 0; i < k; i++) {
657         myIdList.append(anElements[i]);
658         myElements->addItem(QString::number(anElements[i]));
659       }
660       myElements->selectAll();
661     }
662   }
663   else if ( grpType == 1 ) // group on geom
664   {
665     QString aShapeName( "" );
666     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
667     GEOM::GEOM_Object_var aGroupShape = myGroupOnGeom->GetShape();
668     if (!aGroupShape->_is_nil())
669     {
670       _PTR(SObject) aGroupShapeSO = aStudy->FindObjectID(aGroupShape->GetStudyEntry());
671       if ( aGroupShapeSO )
672         aShapeName = aGroupShapeSO->GetName().c_str();
673     }
674     myGeomGroupLine->setText( aShapeName );
675   }
676   else // group on filter
677   {
678     myFilter = myGroupOnFilter->GetFilter();
679     if ( !myFilter->_is_nil() ) {
680       SMESH::Predicate_var perdicate = myFilter->GetPredicate();
681       if ( perdicate->_is_nil() )
682         myFilter = SMESH::Filter::_nil();
683     }
684   }
685
686   if ( grpType != 0 )
687   {
688     myNameChanged = true;
689     myName->blockSignals(true);
690     myName->setText(SMESH::fromUtf8(theGroup->GetName()));
691     myName->blockSignals(false);
692   }
693
694   onSelectAll(); //updateButtons();
695 }
696
697 //=================================================================================
698 // function : updateButtons()
699 // purpose  :
700 //=================================================================================
701 void SMESHGUI_GroupDlg::updateButtons()
702 {
703   bool enable = !myName->text().trimmed().isEmpty();
704   if ( enable )
705   {
706     if (myGrpTypeId == 0) { // standalone
707       if ( !mySelectAll->isChecked() )
708       {
709         if ( myAllowElemsModif->isChecked() )
710         {
711           enable = ( myElements->count() > 0 );
712         }
713         else if ((enable = !myFilter->_is_nil() ))
714         {
715           SMESH::array_of_ElementType_var types = myFilter->GetTypes();
716           enable = types->length();
717         }
718       }
719       enable = enable && (!myGroup->_is_nil() || !myMesh->_is_nil());
720     }
721     else if (myGrpTypeId == 1) // on geom
722     {
723       if (CORBA::is_nil(myGroupOnGeom)) // creation mode
724         enable = ( myGeomObjects->length() > 0 && !myMesh->_is_nil() );
725     }
726     else if (myGrpTypeId == 2) // on filter
727     {
728       if (( enable = !myFilter->_is_nil() ))
729         if (CORBA::is_nil(myGroupOnFilter) )  // creation mode
730           enable = !myMesh->_is_nil();
731     }
732   }
733
734   bool meshHasGeom = ( myMesh->_is_nil() || myMesh->HasShapeToMesh() );
735   if ( myGrpTypeId != 1 )
736   {
737     myGrpTypeGroup->button(1)->setEnabled( meshHasGeom );
738   }
739   else
740   {
741     myGeomGroupBtn->setEnabled( meshHasGeom );
742     myGeomGroupLine->setEnabled( meshHasGeom );
743   }
744
745   myOKBtn->setEnabled(enable);
746   myApplyBtn->setEnabled(enable);
747 }
748
749 //=================================================================================
750 // function : onNameChanged()
751 // purpose  :
752 //=================================================================================
753 void SMESHGUI_GroupDlg::onNameChanged (const QString& text)
754 {
755   myOldName = myName->text();
756   updateButtons();
757   myNameChanged = !myName->text().trimmed().isEmpty();
758 }
759
760 //=================================================================================
761 // function : onTypeChanged()
762 // purpose  : Group elements type radio button management
763 //=================================================================================
764 void SMESHGUI_GroupDlg::onTypeChanged (int id)
765 {
766   if (myTypeId != id) {
767     myElements->clear();
768     myTypeId = id;
769     if ( myGrpTypeId == 0 && myCurrentLineEdit == 0)
770       setSelectionMode(id);
771     else
772       setSelectionMode( mySelectionMode++ ); // update according to mySelectionMode
773
774     onObjectSelectionChanged();
775     setShowEntityMode();
776   }
777 }
778
779 //=================================================================================
780 // function : onGrpTypeChanged()
781 // purpose  : Group type radio button management
782 //=================================================================================
783 void SMESHGUI_GroupDlg::onGrpTypeChanged (int id)
784 {
785   if (myGrpTypeId != id) {
786     myGrpTypeId = id;
787     myWGStack->setCurrentIndex( id );
788     myName->blockSignals(true);
789     myName->setText(myOldName);
790     myName->blockSignals(false);
791     onSelectGeomGroup(id != 0);
792   }
793   updateButtons();
794 }
795
796 //=================================================================================
797 // function : onColorChanged()
798 // purpose  : Color button management
799 //=================================================================================
800 void SMESHGUI_GroupDlg::onColorChanged(QColor theColor)
801 {
802   updateButtons();
803 }
804
805 //=================================================================================
806 // function : setSelectionMode()
807 // purpose  : Radio button management
808 //=================================================================================
809 void SMESHGUI_GroupDlg::setSelectionMode (int theMode)
810 {
811   // PAL7314
812   if (myMesh->_is_nil())
813     return;
814   SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI );
815   bool isSelectAll = mySelectAll->isChecked() || !myAllowElemsModif->isChecked() || myGrpTypeId != 0;
816   if (mySelectionMode != theMode) {
817     // [PAL10408] mySelectionMgr->clearSelected();
818     mySelectionMgr->clearFilters();
819     SMESH::RemoveFilters();
820
821     if (myActorsList.count() > 0)
822       for (QListIterator<SMESH_Actor*> it( myActorsList ); it.hasNext(); )
823         it.next()->SetPointRepresentation(false);
824     else
825       SMESH::SetPointRepresentation(false);
826
827     switch (theMode) {
828     case grpNodeSelection:
829       if ( myGrpTypeId == 0 ) // standalone
830       {
831         if (myActorsList.count() > 0)
832           for (QListIterator<SMESH_Actor*> it( myActorsList ); it.hasNext(); )
833             it.next()->SetPointRepresentation(true);
834         else
835           SMESH::SetPointRepresentation(true);
836       }
837       if ( aViewWindow ) aViewWindow->SetSelectionMode(isSelectAll ? ActorSelection : NodeSelection);
838       break;
839     case grpEdgeSelection:
840       if ( aViewWindow ) aViewWindow->SetSelectionMode(isSelectAll ? ActorSelection : EdgeSelection);
841       break;
842     case grpBallSelection:
843       if ( aViewWindow ) aViewWindow->SetSelectionMode(isSelectAll ? ActorSelection : BallSelection);
844       break;
845     case grp0DSelection:
846       if ( aViewWindow ) aViewWindow->SetSelectionMode(isSelectAll ? ActorSelection : Elem0DSelection);
847       break;
848     case grpFaceSelection:
849       if ( aViewWindow ) aViewWindow->SetSelectionMode(isSelectAll ? ActorSelection : FaceSelection);
850       break;
851     case grpVolumeSelection:
852       if ( aViewWindow ) aViewWindow->SetSelectionMode(isSelectAll ? ActorSelection : VolumeSelection);
853       break;
854     case grpSubMeshSelection: {
855
856       SMESH_TypeFilter* f = 0;
857       switch (myTypeId) {
858       case grpNodeSelection:   f = new SMESH_TypeFilter(SMESH::SUBMESH); break;
859       case grpEdgeSelection:   f = new SMESH_TypeFilter(SMESH::SUBMESH_EDGE); break;
860       case grpFaceSelection:   f = new SMESH_TypeFilter(SMESH::SUBMESH_FACE); break;
861       case grpVolumeSelection: f = new SMESH_TypeFilter(SMESH::SUBMESH_SOLID); break;
862       default:                 f = new SMESH_TypeFilter(SMESH::SUBMESH);
863       }
864       QList<SUIT_SelectionFilter*> filtList;
865       filtList.append( f );
866       filtList.append( new SMESH_TypeFilter(SMESH::SUBMESH_COMPOUND));
867       mySubMeshFilter->setFilters( filtList );
868
869       mySelectionMgr->installFilter( mySubMeshFilter );
870
871       if ( aViewWindow ) aViewWindow->SetSelectionMode(ActorSelection);
872       break;
873     }
874     case grpGroupSelection: {
875
876       SMESH_TypeFilter* f = 0;
877       switch (myTypeId) {
878       case grpNodeSelection:   f = new SMESH_TypeFilter(SMESH::GROUP_NODE);   break;
879       case grp0DSelection:     f = new SMESH_TypeFilter(SMESH::GROUP_0D);     break;
880       case grpBallSelection:   f = new SMESH_TypeFilter(SMESH::GROUP_BALL);   break;
881       case grpEdgeSelection:   f = new SMESH_TypeFilter(SMESH::GROUP_EDGE);   break;
882       case grpFaceSelection:   f = new SMESH_TypeFilter(SMESH::GROUP_FACE);   break;
883       case grpVolumeSelection: f = new SMESH_TypeFilter(SMESH::GROUP_VOLUME); break;
884       default:                 f = new SMESH_TypeFilter(SMESH::GROUP);
885       }
886       QList<SUIT_SelectionFilter*> filtList;
887       filtList.append( f );
888       myGroupFilter->setFilters( filtList );
889
890       mySelectionMgr->installFilter(myGroupFilter);
891       if ( aViewWindow ) aViewWindow->SetSelectionMode(ActorSelection);
892       break;
893     }
894     case grpMeshSelection:
895       mySelectionMgr->installFilter(myMeshFilter);
896       if ( aViewWindow ) aViewWindow->SetSelectionMode(ActorSelection);
897       break;
898     case grpGeomSelection:
899       mySelectionMgr->installFilter(myGeomFilter);
900       if ( aViewWindow ) aViewWindow->SetSelectionMode(ActorSelection);
901       break;
902     default:
903       if ( aViewWindow ) aViewWindow->SetSelectionMode(ActorSelection);
904       break;
905     }
906     if ( aViewWindow ) aViewWindow->Repaint();
907     mySelectionMode = theMode;
908   }
909 }
910
911 //=================================================================================
912 // function : onApply()
913 // purpose  :
914 //=================================================================================
915 bool SMESHGUI_GroupDlg::onApply()
916 {
917   if (mySMESHGUI->isActiveStudyLocked())
918     return false;
919
920   if (myName->text().trimmed().isEmpty())
921     return false;
922
923   SMESH::ElementType aType = SMESH::ALL;
924   switch (myTypeId) {
925   case grpNodeSelection:   aType = SMESH::NODE;   break;
926   case grp0DSelection:     aType = SMESH::ELEM0D; break;
927   case grpBallSelection:   aType = SMESH::BALL;   break;
928   case grpEdgeSelection:   aType = SMESH::EDGE;   break;
929   case grpFaceSelection:   aType = SMESH::FACE;   break;
930   case grpVolumeSelection: aType = SMESH::VOLUME; break;
931   }
932
933   bool anIsOk = false;
934   QStringList anEntryList;
935
936   SMESH::SMESH_GroupBase_var resultGroup;
937   bool isCreation, isConversion = false;
938     
939   SUIT_OverrideCursor wc;
940
941   if (myGrpTypeId == 0)  // standalone
942   {
943     if (!mySelectAll->isChecked() && !myElements->count() && myAllowElemsModif->isChecked())
944       return false;
945
946     mySelectionMgr->clearSelected();
947
948     if (myGroup->_is_nil()) { // creation or conversion
949       // check if group on geometry is not null
950       if (!myGroupOnGeom->_is_nil() || !myGroupOnFilter->_is_nil()) {
951         if (myMesh->_is_nil())
952           return false;
953         if ( myGroupOnGeom->_is_nil() )
954           myGroup = myMesh->ConvertToStandalone( myGroupOnFilter );
955         else
956           myGroup = myMesh->ConvertToStandalone( myGroupOnGeom );
957
958         myGroupOnGeom   = SMESH::SMESH_GroupOnGeom::_nil();
959         myGroupOnFilter = SMESH::SMESH_GroupOnFilter::_nil();
960         isConversion    = true;
961       }
962     }
963
964     if (myGroup->_is_nil()) { // creation
965       if (myMesh->_is_nil())
966         return false;
967
968       myGroup = SMESH::AddGroup(myMesh, aType, myName->text());
969
970       resultGroup = SMESH::SMESH_GroupBase::_narrow( myGroup );
971       isCreation = true;
972
973       if ( mySelectAll->isChecked() ) {
974         // select all
975         myGroup->AddFrom(myMesh.in());
976       }
977       else {
978         // select manually
979
980         if ( !myFilter->_is_nil() &&
981              ( myNbChangesOfContents == 1 || !myAllowElemsModif->isChecked()))
982         {
983           myGroup->AddFrom( myFilter );
984         }
985         else
986         {
987           SMESH::long_array_var anIdList = new SMESH::long_array;
988           int i, k = myElements->count();
989           anIdList->length(k);
990           for (i = 0; i < k; i++) {
991             anIdList[i] = myElements->item(i)->text().toInt();
992           }
993           myGroup->Add(anIdList.inout());
994         }
995       }
996
997
998     } else { // edition
999
1000       resultGroup = SMESH::SMESH_GroupBase::_narrow( myGroup );
1001       isCreation = false;
1002
1003       if ( mySelectAll->isChecked() ) {
1004         // select all
1005         myGroup->Clear();
1006         myGroup->AddFrom(myMesh.in());
1007       }
1008       else {
1009         QList<int> aAddList;
1010         
1011         int i, total = myElements->count();
1012         for (i = 0; i < total; i++) {
1013           int anId = myElements->item(i)->text().toInt();
1014           int idx = myIdList.indexOf(anId);
1015           if ( idx == -1 )
1016             aAddList.append(anId);
1017           else
1018             myIdList.removeAt(idx);
1019         }
1020         if (!aAddList.empty()) {
1021           SMESH::long_array_var anIdList = new SMESH::long_array;
1022           int added = aAddList.count();
1023           anIdList->length(added);
1024           for (i = 0; i < added; i++)
1025             anIdList[i] = aAddList[i];
1026           myGroup->Add(anIdList.inout());
1027         }
1028         if (!myIdList.empty()) {
1029           SMESH::long_array_var anIdList = new SMESH::long_array;
1030           int removed = myIdList.count();
1031           anIdList->length(removed);
1032           for (i = 0; i < removed; i++)
1033             anIdList[i] = myIdList[i];
1034           myGroup->Remove(anIdList.inout());
1035         }
1036         /* init for next operation */
1037         myIdList.clear();
1038         for (i = 0; i < total; i++) {
1039           myIdList.append(myElements->item(i)->text().toInt());
1040         }
1041       }
1042     }
1043
1044     anIsOk = true;
1045   }
1046   else if (myGrpTypeId == 1) // on geom object
1047   {
1048     if (CORBA::is_nil(myGroupOnGeom)) { // creation
1049       if (myMesh->_is_nil() || !myGeomObjects->length())
1050         return false;
1051
1052       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1053
1054       if (myGeomObjects->length() == 1) {
1055         myGroupOnGeom = myMesh->CreateGroupFromGEOM(aType,
1056                                                     SMESH::toUtf8(myName->text()),
1057                                                     myGeomObjects[0]);
1058       }
1059       else {
1060         SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1061         if ( aSMESHGen->_is_nil() )
1062           return false;
1063
1064         // create a geometry group
1065         GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
1066         _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1067
1068         if (geomGen->_is_nil() || !aStudy)
1069           return false;
1070
1071         GEOM::GEOM_IGroupOperations_wrap op = geomGen->GetIGroupOperations(aStudy->StudyId());
1072         if (op->_is_nil())
1073           return false;
1074
1075         // check and add all selected GEOM objects: they must be
1076         // a sub-shapes of the main GEOM and must be of one type
1077         TopAbs_ShapeEnum aGroupType = TopAbs_SHAPE;
1078         for ( int i =0; i < myGeomObjects->length(); i++) {
1079           TopAbs_ShapeEnum aSubShapeType = (TopAbs_ShapeEnum)myGeomObjects[i]->GetShapeType();
1080           if (i == 0)
1081             aGroupType = aSubShapeType;
1082           else if (aSubShapeType != aGroupType) {
1083             aGroupType = TopAbs_SHAPE;
1084             break;
1085           }
1086         }
1087
1088         GEOM::GEOM_Object_var  aMeshShape = myMesh->GetShapeToMesh();
1089         GEOM::GEOM_Object_wrap aGroupVar = op->CreateGroup(aMeshShape, aGroupType);
1090         op->UnionList(aGroupVar, myGeomObjects);
1091
1092         if (op->IsDone()) {
1093           // publish the GEOM group in study
1094           QString aNewGeomGroupName ( "Auto_group_for_" );
1095           aNewGeomGroupName += myName->text();
1096           SALOMEDS::SObject_var aNewGroupSO =
1097             geomGen->AddInStudy(aSMESHGen->GetCurrentStudy(), aGroupVar,
1098                                 SMESH::toUtf8(aNewGeomGroupName), aMeshShape);
1099         }
1100
1101         myGroupOnGeom = myMesh->CreateGroupFromGEOM(aType,
1102                                                     SMESH::toUtf8(myName->text()),
1103                                                     aGroupVar);
1104       }
1105       resultGroup = SMESH::SMESH_GroupBase::_narrow( myGroupOnGeom );
1106       isCreation = true;
1107
1108     }
1109     else { // edition
1110
1111       resultGroup = SMESH::SMESH_GroupBase::_narrow( myGroupOnGeom );
1112       isCreation = false;
1113     }      
1114     anIsOk = true;
1115   }
1116   if (myGrpTypeId == 2) // group on filter
1117   {
1118     if ( myFilter->_is_nil() ) return false;
1119
1120     if (CORBA::is_nil(myGroupOnFilter)) { // creation
1121       if (myMesh->_is_nil())
1122         return false;
1123
1124       myGroupOnFilter = myMesh->CreateGroupFromFilter(aType,
1125                                                       SMESH::toUtf8(myName->text()),
1126                                                       myFilter);
1127
1128       resultGroup = SMESH::SMESH_GroupBase::_narrow( myGroupOnFilter );
1129       isCreation = true;
1130     }
1131     else
1132     {
1133       myGroupOnFilter->SetFilter( myFilter );
1134
1135       resultGroup = SMESH::SMESH_GroupBase::_narrow( myGroupOnFilter );
1136       isCreation = false;
1137     }
1138     anIsOk = true;
1139   }
1140
1141   if( anIsOk )
1142   {
1143     SALOMEDS::Color aColor = getGroupColor();
1144     resultGroup->SetColor(aColor);
1145
1146     _PTR(SObject) aMeshGroupSO = SMESH::FindSObject( resultGroup );
1147     if( aMeshGroupSO )
1148       anEntryList.append( aMeshGroupSO->GetID().c_str() );
1149
1150     resultGroup->SetName(SMESH::toUtf8(myName->text().trimmed()));
1151
1152     if ( isCreation )
1153     {
1154       SMESH::setFileType ( aMeshGroupSO, "COULEURGROUP" );
1155
1156       /* init for the next operation */
1157       setDefaultName();
1158       myElements->clear();
1159       myGroup         = SMESH::SMESH_Group::_nil();
1160       myGroupOnGeom   = SMESH::SMESH_GroupOnGeom::_nil();
1161       myGroupOnFilter = SMESH::SMESH_GroupOnFilter::_nil();
1162       myFilter        = SMESH::Filter::_nil();
1163
1164       setDefaultGroupColor(); // reset color for case if 'auto-color' feature is enabled.
1165     }
1166     else
1167     {
1168       if ( aMeshGroupSO )
1169       {
1170         if ( SMESH_Actor *anActor = SMESH::FindActorByEntry(aMeshGroupSO->GetID().c_str()))
1171         {
1172           Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
1173           if ( isConversion ) { // need to reset TVisualObj and actor
1174             SMESH::RemoveVisualObjectWithActors( anIO->getEntry(), true );
1175             SMESH::Update( anIO,true);
1176             myActorsList.clear();
1177             anActor = SMESH::FindActorByEntry( anIO->getEntry() );
1178             if ( !anActor ) return false;
1179             myActorsList.append( anActor );
1180           }
1181           anActor->setName(SMESH::toUtf8(myName->text()));
1182           QColor c;
1183           int delta;
1184           switch ( myTypeId ) {
1185           case grpNodeSelection:   anActor->SetNodeColor( aColor.R, aColor.G, aColor.B ); break;
1186           case grp0DSelection:     anActor->Set0DColor  ( aColor.R, aColor.G, aColor.B ); break;
1187           case grpBallSelection:   anActor->SetBallColor( aColor.R, aColor.G, aColor.B ); break;
1188           case grpEdgeSelection:   anActor->SetEdgeColor( aColor.R, aColor.G, aColor.B ); break;
1189           case grpVolumeSelection: 
1190             SMESH::GetColor("SMESH", "volume_color", c , delta, "255,0,170|-100");
1191             anActor->SetVolumeColor( aColor.R, aColor.G, aColor.B, delta ); break;          
1192             break;
1193           case grpFaceSelection:   
1194           default:
1195             SMESH::GetColor("SMESH", "fill_color", c , delta, "0,170,255|-100");
1196             anActor->SetSufaceColor( aColor.R, aColor.G, aColor.B, delta ); break;          
1197             break;
1198           }
1199           // update a visible group accoding to a changed contents
1200           if ( !isConversion && anActor->GetVisibility() )
1201           {
1202             SMESH::Update( anIO, true );
1203             SMESH::RepaintCurrentView();
1204           }
1205         }
1206       }
1207     }
1208     SMESHGUI::Modified();
1209     mySMESHGUI->updateObjBrowser(true);
1210     mySelectionMgr->clearSelected();
1211
1212     if( LightApp_Application* anApp =
1213         dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
1214       myObjectToSelect = anApp->browseObjects( anEntryList, isApplyAndClose() );
1215   }
1216   return anIsOk;
1217 }
1218
1219 //=================================================================================
1220 // function : onOK()
1221 // purpose  :
1222 //=================================================================================
1223 void SMESHGUI_GroupDlg::onOK()
1224 {
1225   setIsApplyAndClose( true );
1226   if ( onApply() )
1227     reject();
1228   setIsApplyAndClose( false );
1229
1230   if ( myFilterDlg ) myFilterDlg->UnRegisterFilters();
1231 }
1232
1233 //=================================================================================
1234 // function : onListSelectionChanged()
1235 // purpose  : Called when selection in element list is changed
1236 //=================================================================================
1237 void SMESHGUI_GroupDlg::onListSelectionChanged()
1238 {
1239   //MESSAGE( "SMESHGUI_GroupDlg::onListSelectionChanged(); myActorsList.count() = " << myActorsList.count());
1240   if( myIsBusy || myActorsList.count() == 0 ) return;
1241   myIsBusy = true;
1242
1243   if (myCurrentLineEdit == 0) {
1244     mySelectionMgr->clearSelected();
1245     TColStd_MapOfInteger aIndexes;
1246     QList<QListWidgetItem*> selItems = myElements->selectedItems();
1247     QListWidgetItem* anItem;
1248     foreach(anItem, selItems) aIndexes.Add(anItem->text().toInt());
1249     mySelector->AddOrRemoveIndex(myActorsList.first()->getIO(), aIndexes, false);
1250     SALOME_ListIO aList;
1251     aList.Append(myActorsList.first()->getIO());
1252     mySelectionMgr->setSelectedObjects(aList,false);
1253   }
1254   myIsBusy = false;
1255 }
1256
1257 //=================================================================================
1258 // function : onObjectSelectionChanged()
1259 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
1260 //=================================================================================
1261 void SMESHGUI_GroupDlg::onObjectSelectionChanged()
1262 {
1263   if ( myIsBusy || !isEnabled()) return;
1264   if (myCurrentLineEdit == myGeomGroupLine && !myGeomGroupBtn->isChecked()) return;
1265
1266   myIsBusy = true;
1267
1268   SALOME_ListIO aList;
1269   mySelectionMgr->selectedObjects( aList );
1270
1271   int aNbSel = aList.Extent();
1272   myElements->clearSelection();
1273
1274   if (myCurrentLineEdit)
1275   {
1276     myCurrentLineEdit->setText( "" );
1277     QString aString = "";
1278
1279     if (myCurrentLineEdit == myMeshGroupLine)
1280     {
1281       mySelectSubMesh->setEnabled(false);
1282       mySelectGroup->setEnabled(false);
1283       myGroupLine->setText( "" );
1284       mySubMeshLine->setText( "" );
1285
1286       myGeomGroupBtn->setEnabled(false);
1287       myGeomGroupLine->setEnabled(false);
1288       myGeomGroupLine->setText( "" );
1289       myGeomObjects = new GEOM::ListOfGO();
1290       myGeomObjects->length(0);
1291
1292       if (myGeomGroupBtn->isChecked())
1293         myGeomGroupBtn->setChecked(false);
1294       if (!myCreate)
1295         myName->setText( "" );
1296
1297       myElements->clear();
1298
1299       if (aNbSel != 1 ) {
1300         myGroup = SMESH::SMESH_Group::_nil();
1301         myGroupOnGeom = SMESH::SMESH_GroupOnGeom::_nil();
1302         restoreShowEntityMode();
1303         myMesh = SMESH::SMESH_Mesh::_nil();
1304         updateGeomPopup();
1305         updateButtons();
1306         myIsBusy = false;
1307         return;
1308       }
1309       Handle(SALOME_InteractiveObject) IO = aList.First();
1310
1311       if (myCreate) {
1312         restoreShowEntityMode();
1313         myMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(IO);
1314         setShowEntityMode();
1315         updateGeomPopup();
1316         if (myMesh->_is_nil())
1317         {
1318           updateButtons();
1319           myIsBusy = false;
1320           return;
1321         }
1322
1323         if ( myFilterDlg && !myMesh->_is_nil()){
1324           myFilterDlg->SetMesh( myMesh );
1325         }
1326         myGroup = SMESH::SMESH_Group::_nil();
1327
1328         // NPAL19389: create a group with a selection in another group
1329         // set actor of myMesh, if it is visible, else try
1330         // any visible actor of group or submesh of myMesh
1331         SetAppropriateActor();
1332
1333         setDefaultGroupColor();
1334         if (myName->text().isEmpty())
1335           setDefaultName();
1336
1337         aString = aList.First()->getName();
1338         myMeshGroupLine->setText(aString);
1339         myMeshGroupLine->home( false );
1340
1341         mySelectSubMesh->setEnabled(true);
1342         mySelectGroup->setEnabled(true);
1343         myGeomGroupBtn->setEnabled(true);
1344         myGeomGroupLine->setEnabled(true);
1345         updateButtons();
1346       }
1347       else {
1348         SMESH::SMESH_GroupBase_var aGroup = SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(IO);
1349         if (aGroup->_is_nil())
1350         {
1351           myIsBusy = false;
1352           return;
1353         }
1354         myIsBusy = false;
1355
1356         myGroup = SMESH::SMESH_Group::_nil();
1357         myGroupOnGeom = SMESH::SMESH_GroupOnGeom::_nil();
1358
1359         init(aGroup);
1360         myIsBusy = true;
1361         mySelectSubMesh->setEnabled(true);
1362         mySelectGroup->setEnabled(true);
1363       }
1364       myCurrentLineEdit = 0;
1365       myIsBusy = false;
1366       if (!myCreate)
1367         return;
1368
1369       if (myGrpTypeId == 0)
1370       {
1371         if (myTypeId == -1)
1372           onTypeChanged(0);
1373         else
1374         {
1375           myElements->clear();
1376           setSelectionMode(myTypeId);
1377         }
1378       }
1379
1380       myIsBusy = false;
1381       return;
1382
1383     }
1384     else if (myCurrentLineEdit == myGeomGroupLine)
1385     {
1386       myGeomObjects = new GEOM::ListOfGO();
1387
1388       // The mesh SObject
1389       _PTR(SObject) aMeshSO = SMESH::FindSObject(myMesh);
1390
1391       if (aNbSel == 0 || !aMeshSO)
1392       {
1393         myGeomObjects->length(0);
1394         updateButtons();
1395         myIsBusy = false;
1396         return;
1397       }
1398
1399       myGeomObjects->length(aNbSel);
1400
1401       GEOM::GEOM_Object_var aGeomGroup;
1402       int i = 0;
1403
1404       SALOME_ListIteratorOfListIO anIt (aList);
1405       for (; anIt.More(); anIt.Next())
1406       {
1407         aGeomGroup = GEOMBase::ConvertIOinGEOMObject(anIt.Value());
1408
1409         // Check if the object is a geometry group
1410         if (CORBA::is_nil(aGeomGroup))
1411           continue;
1412
1413         // Check if group constructed on the same shape as a mesh or on its child
1414         _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1415
1416         // The main shape of the group
1417         GEOM::GEOM_Object_var aGroupMainShape;
1418         if (aGeomGroup->GetType() == 37) {
1419           GEOM::GEOM_IGroupOperations_wrap anOp =
1420             SMESH::GetGEOMGen()->GetIGroupOperations(aStudy->StudyId());
1421           aGroupMainShape = anOp->GetMainShape(aGeomGroup);
1422           // aGroupMainShape is an existing servant => GEOM_Object_var not GEOM_Object_wrap
1423         }
1424         else {
1425           aGroupMainShape = aGeomGroup;
1426           aGroupMainShape->Register();
1427         }
1428         _PTR(SObject) aGroupMainShapeSO =
1429           aStudy->FindObjectID(aGroupMainShape->GetStudyEntry());
1430
1431         _PTR(SObject) anObj, aRef;
1432         bool isRefOrSubShape = false;
1433         if (aMeshSO->FindSubObject(1, anObj) &&  anObj->ReferencedObject(aRef)) {
1434           if (aRef->GetID() == aGroupMainShapeSO->GetID()) {
1435             isRefOrSubShape = true;
1436           } else {
1437             _PTR(SObject) aFather = aGroupMainShapeSO->GetFather();
1438             _PTR(SComponent) aComponent = aGroupMainShapeSO->GetFatherComponent();
1439             while (!isRefOrSubShape && aFather->GetID() != aComponent->GetID()) {
1440               if (aRef->GetID() == aFather->GetID())
1441                 isRefOrSubShape = true;
1442               else
1443                 aFather = aFather->GetFather();
1444             }
1445           }
1446         }
1447         if (isRefOrSubShape)
1448           myGeomObjects[i++] = aGeomGroup;
1449       }
1450
1451       myGeomObjects->length(i);
1452       if ( i == 0 )
1453         {
1454           myIsBusy = false;
1455           return;
1456         }
1457
1458       aNbSel = i;
1459     }
1460
1461     if (aNbSel >= 1) {
1462       if (aNbSel > 1) {
1463         if (myCurrentLineEdit == mySubMeshLine)
1464           aString = tr( "SMESH_SUBMESH_SELECTED" ).arg(aNbSel);
1465         else if (myCurrentLineEdit == myGroupLine)
1466           aString = tr( "SMESH_GROUP_SELECTED" ).arg(aNbSel);
1467         else if (myCurrentLineEdit == myGeomGroupLine)
1468           aString = tr( "%1 Objects" ).arg(aNbSel);
1469       }
1470       else {
1471         aString = aList.First()->getName();
1472       }
1473     }
1474
1475     myCurrentLineEdit->setText(aString);
1476     myCurrentLineEdit->home(false);
1477     // 07.06.2008 skl for IPAL19574:
1478     // change name of group only if it is empty
1479     if( myName->text().trimmed().isEmpty() || !myNameChanged ) {
1480       myOldName = myName->text();
1481       myName->blockSignals(true);
1482       myName->setText(aString);
1483       myName->blockSignals(false);
1484     }
1485
1486     updateButtons();
1487   }
1488   else // !myCurrentLineEdit: local selection of nodes or elements
1489   {
1490     if (aNbSel == 1 && myActorsList.count() > 0 )
1491     {
1492       // NPAL19389: create a group with a selection in another group
1493       // Switch myActor to the newly selected one, if the last
1494       // is visible and belongs to group or submesh of myMesh
1495       /*      Handle(SALOME_InteractiveObject) curIO = myActor->getIO();
1496       Handle(SALOME_InteractiveObject) selIO = aList.First();
1497       if (curIO->hasEntry() && selIO->hasEntry()) {
1498         const char* selEntry = selIO->getEntry();
1499         if (strcmp(curIO->getEntry(), selEntry) != 0) {
1500           // different objects: selected and myActor
1501           SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView();
1502           if (aViewWindow && aViewWindow->isVisible(selIO)) {
1503             // newly selected actor is visible
1504
1505             // mesh entry
1506             _PTR(SObject) aSObject = SMESH::FindSObject(myMesh);
1507             if (aSObject) {
1508               CORBA::String_var meshEntry = aSObject->GetID().c_str();
1509               int len = strlen(meshEntry);
1510
1511               if (strncmp(selEntry, meshEntry, len) == 0) {
1512                 // selected object is myMesh or a part of it
1513                 SMESH_Actor* anActor = SMESH::FindActorByEntry(selEntry);
1514                 if (anActor) {
1515                   myActor = anActor;
1516                   SMESH::SetPickable(myActor);
1517                 }
1518               }
1519             }
1520           }
1521         }
1522       }*/
1523       // NPAL19389 END
1524
1525       QString aListStr = "";
1526       int aNbItems = 0;
1527       if (myTypeId == 0) {
1528         QListIterator<SMESH_Actor*> it( myActorsList );
1529         while ( it.hasNext() ) {
1530           QString tmpStr;
1531           aNbItems += SMESH::GetNameOfSelectedNodes(mySelector, it.next()->getIO(), tmpStr);
1532           aListStr += tmpStr;
1533         }
1534       } else {
1535         QListIterator<SMESH_Actor*> it( myActorsList );
1536         while ( it.hasNext() ) {
1537           QString tmpStr;
1538           aNbItems += SMESH::GetNameOfSelectedElements(mySelector, it.next()->getIO(), tmpStr);
1539           aListStr += tmpStr;
1540         }
1541       }
1542       if (aNbItems > 0) {
1543         QListWidgetItem* anItem;
1544         QList<QListWidgetItem*> listItemsToSel;
1545         QStringList anElements = aListStr.split( " ", QString::SkipEmptyParts);
1546         for (QStringList::iterator it = anElements.begin(); it != anElements.end(); ++it) {
1547           QList<QListWidgetItem*> found = myElements->findItems(*it, Qt::MatchExactly);
1548           foreach(anItem, found)
1549             if (!anItem->isSelected())
1550               listItemsToSel.push_back(anItem);
1551         }
1552         bool blocked = myElements->signalsBlocked();
1553         myElements->blockSignals(true);
1554         foreach(anItem, listItemsToSel) anItem->setSelected(true);
1555         myElements->blockSignals(blocked);
1556         onListSelectionChanged();
1557         listItemsToSel.clear();
1558       }
1559     }
1560   }
1561   
1562   if (myActorsList.count() == 0) {
1563     if (!myGroup->_is_nil()) {
1564       SMESH_Actor* anActor = SMESH::FindActorByObject(myGroup);
1565       if ( anActor )
1566         myActorsList.append( anActor  );
1567     }
1568     else if(!myGroupOnGeom->_is_nil()) {
1569       SMESH_Actor* anActor = SMESH::FindActorByObject(myGroupOnGeom);
1570       if ( anActor )
1571         myActorsList.append( anActor );
1572     }
1573     else {
1574       SMESH_Actor* anActor = SMESH::FindActorByObject( myMesh );
1575       if ( anActor )
1576         myActorsList.append( anActor );
1577     }
1578   }
1579
1580   // somehow, if we display the mesh, while selecting from another actor,
1581   // the mesh becomes pickable, and there is no way to select any element
1582   if (myActorsList.count() > 0) {
1583     QListIterator<SMESH_Actor*> it( myActorsList );
1584     while ( it.hasNext() ) {
1585       SMESH_Actor* anActor = it.next();
1586       if ( IsActorVisible(anActor) )
1587         anActor->SetPickable(true);
1588     }
1589   }
1590
1591   myIsBusy = false;
1592 }
1593
1594 //=================================================================================
1595 // function : onSelectAll()
1596 // purpose  : Called when "Select all" is checked
1597 //=================================================================================
1598 void SMESHGUI_GroupDlg::onSelectAll()
1599 {
1600   bool noElemsModif = ( mySelectAll->isChecked() || !myAllowElemsModif->isChecked() );
1601
1602   myElementsLab->setEnabled( !noElemsModif );
1603   myElements->setEnabled   ( !noElemsModif );
1604   myFilterBtn->setEnabled  ( !noElemsModif );
1605   myAddBtn->setEnabled     ( !noElemsModif );
1606   myRemoveBtn->setEnabled  ( !noElemsModif );
1607   mySortBtn->setEnabled    ( !noElemsModif );
1608   mySelectBox->setEnabled  ( !noElemsModif );
1609   myAllowElemsModif->setEnabled( !mySelectAll->isChecked() );
1610
1611   int selMode     = mySelectionMode;
1612   mySelectionMode = grpNoSelection;
1613   setSelectionMode( selMode );
1614   updateButtons();
1615 }
1616
1617 //=================================================================================
1618 // function : onSelectSubMesh()
1619 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
1620 //=================================================================================
1621 void SMESHGUI_GroupDlg::onSelectSubMesh(bool on)
1622 {
1623   if (on) {
1624     if (mySelectGroup->isChecked()) {
1625       mySelectGroup->setChecked(false);
1626     }
1627     //VSR: else if (mySelectGeomGroup->isChecked()) {
1628     //VSR:   mySelectGeomGroup->setChecked(false);
1629     //VSR: }
1630     myCurrentLineEdit = mySubMeshLine;
1631     setSelectionMode(grpSubMeshSelection);
1632   }
1633   else {
1634     mySubMeshLine->setText( "" );
1635     myCurrentLineEdit = 0;
1636     if (myTypeId != -1)
1637       setSelectionMode(myTypeId);
1638   }
1639   mySubMeshBtn->setEnabled(on);
1640   mySubMeshLine->setEnabled(on);
1641 }
1642
1643
1644 //=================================================================================
1645 // function : (onSelectGroup)
1646 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
1647 //=================================================================================
1648 void SMESHGUI_GroupDlg::onSelectGroup(bool on)
1649 {
1650   if (on) {
1651     if (mySelectSubMesh->isChecked()) {
1652       mySelectSubMesh->setChecked(false);
1653     }
1654     myCurrentLineEdit = myGroupLine;
1655     setSelectionMode(grpGroupSelection);
1656   }
1657   else {
1658     myGroupLine->setText( "" );
1659     myCurrentLineEdit = 0;
1660     if (myTypeId != -1)
1661       setSelectionMode(myTypeId);
1662   }
1663   myGroupBtn->setEnabled(on);
1664   myGroupLine->setEnabled(on);
1665 }
1666
1667
1668 //=================================================================================
1669 // function : (onSelectGeomGroup)
1670 // purpose  : Called when group type changed. on == "on geometry" or "on filter"
1671 //=================================================================================
1672 void SMESHGUI_GroupDlg::onSelectGeomGroup(bool on)
1673 {
1674   if (on) {
1675     if (mySelectSubMesh->isChecked()) {
1676       mySelectSubMesh->setChecked(false);
1677     }
1678     else if (mySelectGroup->isChecked()) {
1679       mySelectGroup->setChecked(false);
1680     }
1681     if ( myGrpTypeId == 1 ) { // on geometry
1682       myCurrentLineEdit = myGeomGroupLine;
1683       updateGeomPopup();
1684     }
1685     else { // on filter
1686       myCurrentLineEdit = 0;
1687     }
1688     setSelectionMode(grpAllSelection);
1689   }
1690   else {
1691     myGeomGroupBtn->setChecked(false);
1692     myGeomObjects->length(0);
1693     myGeomGroupLine->setText( "" );
1694     myCurrentLineEdit = 0;
1695     if (myTypeId != -1)
1696       setSelectionMode( myTypeId );
1697   }
1698 }
1699
1700 //=================================================================================
1701 // function : setCurrentSelection()
1702 // purpose  :
1703 //=================================================================================
1704 void SMESHGUI_GroupDlg::setCurrentSelection()
1705 {
1706   QPushButton* send = (QPushButton*)sender();
1707   myCurrentLineEdit = 0;
1708   if (send == myMeshGroupBtn) {
1709     disconnect(myMeshGroupBtn, SIGNAL(clicked()), this, SLOT(setCurrentSelection()));
1710     mySelectionMgr->clearSelected();
1711     if (myCreate)
1712       setSelectionMode(grpMeshSelection);
1713     else
1714       setSelectionMode(grpGroupSelection);
1715     connect(myMeshGroupBtn, SIGNAL(clicked()), this, SLOT(setCurrentSelection()));
1716     myCurrentLineEdit = myMeshGroupLine;
1717     onObjectSelectionChanged();
1718   }
1719   else if (send == mySubMeshBtn) {
1720     myCurrentLineEdit = mySubMeshLine;
1721     onObjectSelectionChanged();
1722   }
1723   else if (send == myGroupBtn) {
1724     myCurrentLineEdit = myGroupLine;
1725     onObjectSelectionChanged();
1726   }
1727 }
1728
1729
1730 //=================================================================================
1731 // function : setFilters()
1732 // purpose  : SLOT. Called when "Filter" button pressed.
1733 //=================================================================================
1734 void SMESHGUI_GroupDlg::setFilters()
1735 {
1736   if(myMesh->_is_nil()) {
1737     SUIT_MessageBox::critical(this,
1738                               tr("SMESH_ERROR"),
1739                               tr("NO_MESH_SELECTED"));
1740    return;
1741   }
1742
1743   SMESH::ElementType aType = SMESH::ALL;
1744   switch ( myTypeId )
1745   {
1746     case grpNodeSelection:   aType = SMESH::NODE;   break;
1747     case grp0DSelection:     aType = SMESH::ELEM0D; break;
1748     case grpBallSelection:   aType = SMESH::BALL;   break;
1749     case grpEdgeSelection:   aType = SMESH::EDGE;   break;
1750     case grpFaceSelection:   aType = SMESH::FACE;   break;
1751     case grpVolumeSelection: aType = SMESH::VOLUME; break;
1752     default:                 return;
1753   }
1754
1755   if ( myFilterDlg == 0 )
1756   {
1757     myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, aType );
1758     connect( myFilterDlg, SIGNAL( Accepted() ), SLOT( onFilterAccepted() ) );
1759   }
1760   else
1761     myFilterDlg->Init( aType );
1762
1763   if ( !myGroupOnFilter->_is_nil() )
1764   {
1765     myFilterDlg->SetFilter( myFilter, aType );
1766     myFilterDlg->Init( aType );
1767   }
1768
1769   bool isStandalone = ( sender() == myFilterBtn );
1770   myFilterDlg->SetEnabled( /*setInViewer=*/isStandalone,
1771                            /*diffSources=*/isStandalone );
1772   myFilterDlg->SetMesh( myMesh );
1773   myFilterDlg->SetGroup( myGroupOnFilter );
1774   myFilterDlg->SetSelection();
1775   myFilterDlg->SetSourceWg( myElements, false );
1776
1777   myFilterDlg->show();
1778 }
1779
1780 //=================================================================================
1781 // function : onFilterAccepted()
1782 // purpose  : SLOT. Called when Filter dlg closed with OK button.
1783 //            Uncheck "Select submesh" and "Select group" checkboxes
1784 //=================================================================================
1785 void SMESHGUI_GroupDlg::onFilterAccepted()
1786 {
1787   if ( mySelectSubMesh->isChecked() || mySelectGroup->isChecked() )
1788   {
1789     mySelectionMode = myTypeId;
1790     mySelectSubMesh->setChecked( false );
1791     mySelectGroup->setChecked( false );
1792   }
1793   // get a filter from myFilterDlg
1794   myFilter = myFilterDlg->GetFilter();
1795   if ( !myFilter->_is_nil() ) {
1796     SMESH::Predicate_var perdicate = myFilter->GetPredicate();
1797     if ( perdicate->_is_nil() )
1798       myFilter = SMESH::Filter::_nil();
1799   }
1800   // set mesh to myFilter
1801   if ( !myFilter->_is_nil() ) {
1802     SMESH::SMESH_Mesh_var mesh = myMesh;
1803     if ( mesh->_is_nil() ) {
1804       if ( !myGroup->_is_nil() )
1805         mesh = myGroup->GetMesh();
1806       else if ( !myGroupOnGeom->_is_nil() )
1807         mesh = myGroupOnGeom->GetMesh();
1808       else if ( !myGroupOnFilter->_is_nil() )
1809         mesh = myGroupOnFilter->GetMesh();
1810     }
1811     myFilter->SetMesh( mesh );
1812   }
1813
1814   updateButtons();
1815 }
1816
1817 //=================================================================================
1818 // function : onAdd()
1819 // purpose  :
1820 //=================================================================================
1821 void SMESHGUI_GroupDlg::onAdd()
1822 {
1823   SALOME_ListIO aList;
1824   mySelectionMgr->selectedObjects( aList );
1825
1826   int aNbSel = aList.Extent();
1827
1828   if (aNbSel == 0 || myActorsList.count() == 0 || myMesh->_is_nil()) return;
1829
1830   myIsBusy = true;
1831   int sizeBefore = myElements->count();
1832
1833   SMESH::ElementType aType = SMESH::ALL;
1834   switch(myTypeId) {
1835   case grpNodeSelection:
1836     aType = SMESH::NODE;
1837     mySelector->SetSelectionMode(NodeSelection);
1838     break;
1839   case grpBallSelection:
1840     aType = SMESH::BALL;
1841     mySelector->SetSelectionMode(BallSelection);
1842     break;
1843   case grp0DSelection:
1844     aType = SMESH::ELEM0D;
1845     mySelector->SetSelectionMode(Elem0DSelection);
1846     break;
1847   case grpEdgeSelection:
1848     aType = SMESH::EDGE;
1849     mySelector->SetSelectionMode(EdgeSelection);
1850     break;
1851   case grpFaceSelection:
1852     aType = SMESH::FACE;
1853     mySelector->SetSelectionMode(FaceSelection);
1854     break;
1855   case grpVolumeSelection:
1856     aType = SMESH::VOLUME;
1857     mySelector->SetSelectionMode(VolumeSelection);
1858     break;
1859   default:
1860     mySelector->SetSelectionMode(ActorSelection);
1861   }
1862
1863   QListWidgetItem* anItem = 0;
1864   QList<QListWidgetItem*> listItemsToSel;
1865
1866   if (myCurrentLineEdit == 0) {
1867     //if (aNbSel != 1) { myIsBusy = false; return; }
1868     QString aListStr = "";
1869     int aNbItems = 0;
1870     if (myTypeId == 0) {
1871       QListIterator<SMESH_Actor*> it( myActorsList );
1872       while ( it.hasNext() ) {
1873         QString tmpStr;
1874         aNbItems += SMESH::GetNameOfSelectedNodes(mySelector, it.next()->getIO(), tmpStr);
1875         aListStr += tmpStr;
1876       }
1877     }
1878     else {
1879       QListIterator<SMESH_Actor*> it( myActorsList );
1880       while ( it.hasNext() ) {
1881         QString tmpStr;
1882         aNbItems += SMESH::GetNameOfSelectedElements(mySelector, it.next()->getIO(), tmpStr);
1883         aListStr += tmpStr;
1884       }
1885     }
1886     if (aNbItems > 0) {
1887       QStringList anElements = aListStr.split( " ", QString::SkipEmptyParts);
1888       for (QStringList::iterator it = anElements.begin(); it != anElements.end(); ++it) {
1889         QList<QListWidgetItem*> found = myElements->findItems(*it, Qt::MatchExactly);
1890         if (found.count() == 0) {
1891           anItem = new QListWidgetItem(*it);
1892           myElements->addItem(anItem);
1893           if (!anItem->isSelected())
1894             listItemsToSel.push_back(anItem);
1895         }
1896         else {
1897           foreach(anItem, found)
1898             if (!anItem->isSelected())
1899               listItemsToSel.push_back(anItem);
1900         }
1901       }
1902       bool blocked = myElements->signalsBlocked();
1903       myElements->blockSignals(true);
1904       foreach(anItem, listItemsToSel) anItem->setSelected(true);
1905       myElements->blockSignals(blocked);
1906       onListSelectionChanged();
1907       listItemsToSel.clear();
1908     }
1909   } else if (myCurrentLineEdit == mySubMeshLine) {
1910     //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1911
1912     SALOME_ListIO aList;
1913     mySelectionMgr->selectedObjects( aList );
1914
1915     SALOME_ListIteratorOfListIO anIt (aList);
1916     for ( ; anIt.More(); anIt.Next()) {
1917       SMESH::SMESH_subMesh_var aSubMesh =
1918         SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIt.Value());
1919       if (!aSubMesh->_is_nil()) {
1920         // check if mesh is the same
1921         if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
1922           try {
1923             SMESH::long_array_var anElements = aSubMesh->GetElementsByType(aType);
1924             int k = anElements->length();
1925             for (int i = 0; i < k; i++) {
1926               QString aText = QString::number(anElements[i]);
1927               QList<QListWidgetItem*> found = myElements->findItems(aText, Qt::MatchExactly);
1928               if (found.count() == 0) {
1929                 anItem = new QListWidgetItem(aText);
1930                 myElements->addItem(anItem);
1931                 if (!anItem->isSelected())
1932                   listItemsToSel.push_back(anItem);
1933               }
1934               else {
1935                 foreach(anItem, found)
1936                   if (!anItem->isSelected())
1937                     listItemsToSel.push_back(anItem);
1938               }
1939             }
1940             bool blocked = myElements->signalsBlocked();
1941             myElements->blockSignals(true);
1942             foreach(anItem, listItemsToSel) anItem->setSelected(true);
1943             myElements->blockSignals(blocked);
1944             onListSelectionChanged();
1945             listItemsToSel.clear();
1946           }
1947           catch (const SALOME::SALOME_Exception& ex) {
1948             SalomeApp_Tools::QtCatchCorbaException(ex);
1949           }
1950         }
1951       }
1952     }
1953     mySelectSubMesh->setChecked(false);
1954     myIsBusy = false;
1955     onListSelectionChanged();
1956
1957   } else if (myCurrentLineEdit == myGroupLine) {
1958     //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1959     SALOME_ListIO aList;
1960     mySelectionMgr->selectedObjects( aList );
1961
1962     SALOME_ListIteratorOfListIO anIt (aList);
1963     for ( ; anIt.More(); anIt.Next()) {
1964       SMESH::SMESH_GroupBase_var aGroup =
1965         SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(anIt.Value());
1966       if (!aGroup->_is_nil()) {
1967         // check if mesh is the same
1968         if (aGroup->GetType() == aType && aGroup->GetMesh()->GetId() == myMesh->GetId()) {
1969           SMESH::long_array_var anElements = aGroup->GetListOfID();
1970           int k = anElements->length();
1971           for (int i = 0; i < k; i++) {
1972             QString aText = QString::number(anElements[i]);
1973             QList<QListWidgetItem*> found = myElements->findItems(aText, Qt::MatchExactly);
1974             if (found.count() == 0) {
1975               anItem = new QListWidgetItem(aText);
1976               myElements->addItem(anItem);
1977               if (!anItem->isSelected())
1978                 listItemsToSel.push_back(anItem);
1979             }
1980             else {
1981               foreach(anItem, found)
1982                 if (!anItem->isSelected())
1983                   listItemsToSel.push_back(anItem);
1984             }
1985           }
1986           bool blocked = myElements->signalsBlocked();
1987           myElements->blockSignals(true);
1988           foreach(anItem, listItemsToSel) anItem->setSelected(true);
1989           myElements->blockSignals(blocked);
1990           onListSelectionChanged();
1991           listItemsToSel.clear();
1992         }
1993       }
1994     }
1995     mySelectGroup->setChecked(false);
1996     myIsBusy = false;
1997     onListSelectionChanged();
1998
1999   } else if (myCurrentLineEdit == myGeomGroupLine && myGeomObjects->length() == 1) {
2000     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
2001     GEOM::GEOM_IGroupOperations_wrap aGroupOp =
2002       SMESH::GetGEOMGen()->GetIGroupOperations(aStudy->StudyId());
2003
2004     SMESH::ElementType aGroupType = SMESH::ALL;
2005     switch(aGroupOp->GetType(myGeomObjects[0])) {
2006     case TopAbs_VERTEX: aGroupType = SMESH::NODE; break;
2007     case TopAbs_EDGE:   aGroupType = SMESH::EDGE; break;
2008     case TopAbs_FACE:   aGroupType = SMESH::FACE; break;
2009     case TopAbs_SOLID:  aGroupType = SMESH::VOLUME; break;
2010     default: myIsBusy = false; return;
2011     }
2012
2013     if (aGroupType == aType) {
2014       _PTR(SObject) aGroupSO =
2015         //aStudy->FindObjectIOR(aStudy->ConvertObjectToIOR(myGeomGroup));
2016         aStudy->FindObjectID(myGeomObjects[0]->GetStudyEntry());
2017       // Construct filter
2018       SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
2019       SMESH::Filter_var aFilter = aFilterMgr->CreateFilter();
2020       SMESH::BelongToGeom_var aBelongToGeom = aFilterMgr->CreateBelongToGeom();
2021       aBelongToGeom->SetGeom(myGeomObjects[0]);
2022       aBelongToGeom->SetShapeName(aGroupSO->GetName().c_str());
2023       aBelongToGeom->SetElementType(aType);
2024       aFilter->SetPredicate(aBelongToGeom);
2025
2026       SMESH::long_array_var anElements = aFilter->GetElementsId(myMesh);
2027
2028       int k = anElements->length();
2029       for (int i = 0; i < k; i++) {
2030         QString aText = QString::number(anElements[i]);
2031         QList<QListWidgetItem*> found = myElements->findItems(aText, Qt::MatchExactly);
2032         if (found.count() == 0) {
2033           anItem = new QListWidgetItem(aText);
2034           myElements->addItem(anItem);
2035           if (!anItem->isSelected())
2036             listItemsToSel.push_back(anItem);
2037         }
2038         else {
2039           foreach(anItem, found)
2040             if (!anItem->isSelected())
2041               listItemsToSel.push_back(anItem);
2042         }
2043       }
2044       bool blocked = myElements->signalsBlocked();
2045       myElements->blockSignals(true);
2046       foreach(anItem, listItemsToSel) anItem->setSelected(true);
2047       myElements->blockSignals(blocked);
2048       onListSelectionChanged();
2049       listItemsToSel.clear();
2050     }
2051
2052     //VSR: mySelectGeomGroup->setChecked(false);
2053     myIsBusy = false;
2054     onListSelectionChanged();
2055   }
2056   myIsBusy = false;
2057   if ( sizeBefore < myElements->count() )
2058     ++myNbChangesOfContents;
2059   //  mySelectionMgr->clearSelected();
2060   updateButtons();
2061 }
2062
2063 //=================================================================================
2064 // function : onRemove()
2065 // purpose  :
2066 //=================================================================================
2067 void SMESHGUI_GroupDlg::onRemove()
2068 {
2069   myIsBusy = true;
2070   int sizeBefore = myElements->count();
2071
2072   if (myCurrentLineEdit == 0) {
2073     QList<QListWidgetItem*> selItems = myElements->selectedItems();
2074     QListWidgetItem* item;
2075     foreach(item, selItems) delete item;
2076   } else {
2077     SALOME_ListIO aList;
2078     mySelectionMgr->selectedObjects( aList );
2079
2080     int aNbSel = aList.Extent();
2081
2082     if (aNbSel == 0) { myIsBusy = false; return; }
2083
2084     SMESH::ElementType aType = SMESH::ALL;
2085     switch(myTypeId) {
2086     case grpNodeSelection:   aType = SMESH::NODE;   break;
2087     case grp0DSelection:     aType = SMESH::ELEM0D; break;
2088     case grpBallSelection:   aType = SMESH::BALL;   break;
2089     case grpEdgeSelection:   aType = SMESH::EDGE;   break;
2090     case grpFaceSelection:   aType = SMESH::FACE;   break;
2091     case grpVolumeSelection: aType = SMESH::VOLUME; break;
2092     }
2093
2094     if (myCurrentLineEdit == mySubMeshLine) {
2095       //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
2096       SALOME_ListIO aList;
2097       mySelectionMgr->selectedObjects( aList );
2098
2099       SALOME_ListIteratorOfListIO anIt (aList);
2100       for ( ; anIt.More(); anIt.Next()) {
2101         SMESH::SMESH_subMesh_var aSubMesh = SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIt.Value());
2102         if (!aSubMesh->_is_nil()) {
2103           // check if mesh is the same
2104           if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
2105             if (aType == SMESH::NODE) {
2106               try {
2107                 SMESH::long_array_var anElements = aSubMesh->GetNodesId();
2108                 int k = anElements->length();
2109                 for (int i = 0; i < k; i++) {
2110                   QList<QListWidgetItem*> found = 
2111                     myElements->findItems(QString::number(anElements[i]), Qt::MatchExactly);
2112                   QListWidgetItem* anItem;
2113                   foreach(anItem, found) delete anItem;
2114                 }
2115               }
2116               catch (const SALOME::SALOME_Exception& ex) {
2117                 SalomeApp_Tools::QtCatchCorbaException(ex);
2118               }
2119             }
2120             else {
2121               try {
2122                 SMESH::long_array_var anElements = aSubMesh->GetElementsId();
2123                 int k = anElements->length();
2124                 for (int i = 0; i < k; i++) {
2125                   QList<QListWidgetItem*> found = 
2126                     myElements->findItems(QString::number(anElements[i]), Qt::MatchExactly);
2127                   QListWidgetItem* anItem;
2128                   foreach(anItem, found) delete anItem;
2129                 }
2130               }
2131               catch (const SALOME::SALOME_Exception& ex) {
2132                 SalomeApp_Tools::QtCatchCorbaException(ex);
2133               }
2134             }
2135           }
2136         }
2137       }
2138     }
2139     else if (myCurrentLineEdit == myGroupLine) {
2140       Standard_Boolean aRes;
2141       //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
2142       SALOME_ListIO aList;
2143       mySelectionMgr->selectedObjects( aList );
2144
2145       SALOME_ListIteratorOfListIO anIt (aList);
2146       for ( ; anIt.More(); anIt.Next()) {
2147         SMESH::SMESH_Group_var aGroup = SMESH::IObjectToInterface<SMESH::SMESH_Group>(anIt.Value());
2148         if (aRes && !aGroup->_is_nil()) {
2149           // check if mesh is the same
2150           if (aGroup->GetType() == aType && aGroup->GetMesh()->GetId() == myMesh->GetId()) {
2151             SMESH::long_array_var anElements = aGroup->GetListOfID();
2152             int k = anElements->length();
2153             for (int i = 0; i < k; i++) {
2154               QList<QListWidgetItem*> found = 
2155                 myElements->findItems(QString::number(anElements[i]), Qt::MatchExactly);
2156               QListWidgetItem* anItem;
2157               foreach(anItem, found) delete anItem;
2158             }
2159           }
2160         }
2161       }
2162     }
2163   }
2164   myIsBusy = false;
2165   if ( sizeBefore > myElements->count() )
2166     myNbChangesOfContents += 2; // it's used to detect that "Add" was only once
2167   updateButtons();
2168 }
2169
2170 //=================================================================================
2171 // function : onSort()
2172 // purpose  :
2173 //=================================================================================
2174 void SMESHGUI_GroupDlg::onSort()
2175 {
2176   // PAL5412: sorts items in ascending by "string" value
2177   // myElements->sort(true);
2178   // myElements->update();
2179   int i, k = myElements->count();
2180   if (k > 0) {
2181     myIsBusy = true;
2182     QList<int> aSelected;
2183     std::vector<int> anArray(k);
2184     //    QMemArray<int> anArray(k);
2185     // fill the array
2186     for (i = 0; i < k; i++) {
2187       int id = myElements->item(i)->text().toInt();
2188       anArray[i] = id;
2189       if (myElements->item(i)->isSelected())
2190         aSelected.append(id);
2191     }
2192     // sort & update list
2193     std::sort(anArray.begin(), anArray.end());
2194     //    anArray.sort();
2195     myElements->clear();
2196     QListWidgetItem* anItem;
2197     QList<QListWidgetItem*> listItemsToSel;
2198     for (i = 0; i < k; i++) {
2199       anItem = new QListWidgetItem(QString::number(anArray[i]));
2200       myElements->addItem(anItem);
2201       if (aSelected.contains(anArray[i]))
2202         listItemsToSel.push_back(anItem);
2203     }
2204     bool blocked = myElements->signalsBlocked();
2205     myElements->blockSignals(true);
2206     foreach(anItem, listItemsToSel) anItem->setSelected(true);
2207     myElements->blockSignals(blocked);
2208     listItemsToSel.clear();
2209     myIsBusy = false;
2210   }
2211 }
2212
2213 //=================================================================================
2214 // function : onVisibilityChanged()
2215 // purpose  :
2216 //=================================================================================
2217 void SMESHGUI_GroupDlg::onVisibilityChanged()
2218 {
2219   SetAppropriateActor();
2220 }
2221
2222 //=================================================================================
2223 // function : SMESHGUI_GroupDlg::reject
2224 // purpose  : SLOT called when "Close" button pressed. Close dialog
2225 //=================================================================================
2226 void SMESHGUI_GroupDlg::reject()
2227 {
2228   if (SMESH::GetCurrentVtkView()) {
2229     SMESH::RemoveFilters(); // PAL6938 -- clean all mesh entity filters
2230     SMESH::SetPointRepresentation(false);
2231     SMESH::SetPickable();
2232     restoreShowEntityMode();
2233   }
2234
2235   if( isApplyAndClose() && !myObjectToSelect.isEmpty() ) {
2236     SUIT_DataOwnerPtrList aList;
2237     aList.append( new LightApp_DataOwner( myObjectToSelect ) );
2238     mySelectionMgr->setSelected( aList );
2239   }
2240   else
2241     mySelectionMgr->clearSelected();
2242   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
2243     aViewWindow->SetSelectionMode(ActorSelection);
2244   mySelectionMgr->clearFilters();
2245   mySMESHGUI->ResetState();
2246
2247   QDialog::reject();
2248
2249   if ( myFilterDlg ) myFilterDlg->UnRegisterFilters();
2250 }
2251
2252 //=================================================================================
2253 // function : onOpenView()
2254 // purpose  :
2255 //=================================================================================
2256 void SMESHGUI_GroupDlg::onOpenView()
2257 {
2258   if ( mySelector ) {
2259     SMESH::SetPointRepresentation(false);
2260   }
2261   else {
2262     mySelector = SMESH::GetViewWindow( mySMESHGUI )->GetSelector();
2263     mySMESHGUI->EmitSignalDeactivateDialog();
2264     setEnabled(true);
2265   }
2266 }
2267
2268 //=================================================================================
2269 // function : onCloseView()
2270 // purpose  :
2271 //=================================================================================
2272 void SMESHGUI_GroupDlg::onCloseView()
2273 {
2274   onDeactivate();
2275   mySelector = 0;
2276 }
2277
2278 //=================================================================================
2279 // function : onHelp()
2280 // purpose  :
2281 //=================================================================================
2282 void SMESHGUI_GroupDlg::onHelp()
2283 {
2284   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
2285   if (app)
2286     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString( "" ), myHelpFileName);
2287   else {
2288     QString platform;
2289 #ifdef WIN32
2290     platform = "winapplication";
2291 #else
2292     platform = "application";
2293 #endif
2294     SUIT_MessageBox::warning(this, tr( "WRN_WARNING" ),
2295                              tr( "EXTERNAL_BROWSER_CANNOT_SHOW_PAGE" ).
2296                              arg(app->resourceMgr()->stringValue( "ExternalBrowser",
2297                                                                  platform)).
2298                              arg(myHelpFileName));
2299   }
2300 }
2301
2302 //=================================================================================
2303 // function : SMESHGUI_GroupDlg::onDeactivate
2304 // purpose  : SLOT called when dialog must be deativated
2305 //=================================================================================
2306 void SMESHGUI_GroupDlg::onDeactivate()
2307 {
2308   mySMESHGUI->ResetState();
2309   setEnabled(false);
2310 }
2311
2312 //=================================================================================
2313 // function : SMESHGUI_GroupDlg::enterEvent
2314 // purpose  : Event filter
2315 //=================================================================================
2316 void SMESHGUI_GroupDlg::enterEvent (QEvent*)
2317 {
2318   if (!isEnabled()) {
2319     SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI );
2320     if ( aViewWindow && !mySelector) {
2321       mySelector = aViewWindow->GetSelector();
2322     }
2323     mySMESHGUI->EmitSignalDeactivateDialog();
2324     setEnabled(true);
2325     mySelectionMode = grpNoSelection;
2326     setSelectionMode(myTypeId);
2327     //mySMESHGUI->SetActiveDialogBox((QDialog*)this);
2328     mySMESHGUI->SetActiveDialogBox(this);
2329     mySMESHGUI->SetState(800);
2330   }
2331 }
2332
2333 //=================================================================================
2334 // function : keyPressEvent()
2335 // purpose  :
2336 //=================================================================================
2337 void SMESHGUI_GroupDlg::keyPressEvent( QKeyEvent* e )
2338 {
2339   QDialog::keyPressEvent( e );
2340   if ( e->isAccepted() )
2341     return;
2342
2343   if ( e->key() == Qt::Key_F1 )
2344     {
2345       e->accept();
2346       onHelp();
2347     }
2348 }
2349
2350 //================================================================================
2351 /*!
2352  * \brief Enable showing of the popup when Geometry selection btn is clicked
2353   * \param enable - true to enable
2354  */
2355 //================================================================================
2356
2357 enum { DIRECT_GEOM_INDEX = 0, GEOM_BY_MESH_INDEX };
2358
2359 void SMESHGUI_GroupDlg::updateGeomPopup()
2360 {
2361   bool enable = false;
2362
2363   if ( !myMesh->_is_nil() )
2364     enable = myMesh->NbEdges() > 0;
2365
2366   if ( myGeomGroupBtn )
2367   {
2368     disconnect( myGeomGroupBtn, SIGNAL( toggled(bool) ), this, SLOT( onGeomSelectionButton(bool) ));
2369     if ( enable ) {
2370       if ( !myGeomPopup ) {
2371         myGeomPopup = new QMenu(this);
2372         myActions[myGeomPopup->addAction( tr( "DIRECT_GEOM_SELECTION" ) )] = DIRECT_GEOM_INDEX;
2373         myActions[myGeomPopup->addAction( tr( "GEOM_BY_MESH_ELEM_SELECTION" ) )] = GEOM_BY_MESH_INDEX;
2374         connect( myGeomPopup, SIGNAL( triggered( QAction* ) ), SLOT( onGeomPopup( QAction* ) ) );
2375       }
2376       connect( myGeomGroupBtn, SIGNAL( toggled(bool) ), this, SLOT( onGeomSelectionButton(bool) ));
2377     }
2378   }
2379 }
2380
2381
2382 //=================================================================================
2383 // function : onGeomSelectionButton()
2384 // purpose  :
2385 //=================================================================================
2386 void SMESHGUI_GroupDlg::onGeomSelectionButton(bool isBtnOn)
2387 {
2388   if ( myGeomPopup && isBtnOn )
2389     {
2390       myCurrentLineEdit = myGeomGroupLine;
2391       QAction* a = myGeomPopup->exec( QCursor::pos() );
2392       if (!a || myActions[a] == DIRECT_GEOM_INDEX)
2393         setSelectionMode(grpGeomSelection);
2394     }
2395   else if (!isBtnOn)
2396     {
2397       myCurrentLineEdit = 0;
2398       setSelectionMode(grpAllSelection);
2399     }
2400 }
2401
2402 //=================================================================================
2403 // function : onGeomPopup()
2404 // purpose  :
2405 //=================================================================================
2406 void SMESHGUI_GroupDlg::onGeomPopup( QAction* a )
2407 {
2408   int index = myActions[a];
2409   if ( index == GEOM_BY_MESH_INDEX )
2410     {
2411       mySelectionMode = grpNoSelection;
2412       if ( !myShapeByMeshOp ) {
2413         myShapeByMeshOp = new SMESHGUI_ShapeByMeshOp(true);
2414         connect(myShapeByMeshOp, SIGNAL(committed(SUIT_Operation*)),
2415                 SLOT(onPublishShapeByMeshDlg(SUIT_Operation*)));
2416         connect(myShapeByMeshOp, SIGNAL(aborted(SUIT_Operation*)),
2417                 SLOT(onCloseShapeByMeshDlg(SUIT_Operation*)));
2418       }
2419       // set mesh object to SMESHGUI_ShapeByMeshOp and start it
2420       if ( !myMesh->_is_nil() ) {
2421         myIsBusy = true;
2422         hide(); // stop processing selection
2423         myIsBusy = false;
2424         myShapeByMeshOp->setModule( mySMESHGUI );
2425         myShapeByMeshOp->setStudy( 0 ); // it's really necessary
2426         myShapeByMeshOp->SetMesh( myMesh );
2427         myShapeByMeshOp->start();
2428       }
2429     }
2430 }
2431
2432 //================================================================================
2433 /*!
2434  * \brief SLOT. Is called when Ok is pressed in SMESHGUI_ShapeByMeshDlg
2435  */
2436 //================================================================================
2437
2438 void SMESHGUI_GroupDlg::onPublishShapeByMeshDlg(SUIT_Operation* op)
2439 {
2440   if ( myShapeByMeshOp == op ) {
2441     mySMESHGUI->getApp()->updateObjectBrowser();
2442     show();
2443     // Select a found geometry object
2444     GEOM::GEOM_Object_var aGeomVar = myShapeByMeshOp->GetShape();
2445     if ( !aGeomVar->_is_nil() )
2446     {
2447       QString ID = aGeomVar->GetStudyEntry();
2448       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
2449       if ( _PTR(SObject) aGeomSO = aStudy->FindObjectID( ID.toLatin1().data() )) {
2450         SALOME_ListIO anIOList;
2451         Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject
2452           ( aGeomSO->GetID().c_str(), "SMESH", aGeomSO->GetName().c_str() );
2453         anIOList.Append( anIO );
2454         mySelectionMgr->setSelectedObjects( anIOList, false );
2455         onObjectSelectionChanged();
2456       }
2457     }
2458   }
2459 }
2460
2461 //================================================================================
2462 /*!
2463  * \brief SLOT. Is called when Close is pressed in SMESHGUI_ShapeByMeshDlg
2464  */
2465 //================================================================================
2466
2467 void SMESHGUI_GroupDlg::onCloseShapeByMeshDlg(SUIT_Operation* op)
2468 {
2469   if ( myShapeByMeshOp == op )
2470     {
2471       show();
2472       setSelectionMode(grpGeomSelection);
2473     }
2474 }
2475
2476 //=================================================================================
2477 // function : setGroupColor()
2478 // purpose  :
2479 //=================================================================================
2480 void SMESHGUI_GroupDlg::setGroupColor( const SALOMEDS::Color& theColor )
2481 {
2482   QColor aQColor( (int)( theColor.R * 255.0 ),
2483                   (int)( theColor.G * 255.0 ),
2484                   (int)( theColor.B * 255.0 ) );
2485   setGroupQColor( aQColor );
2486 }
2487
2488 //=================================================================================
2489 // function : getGroupColor()
2490 // purpose  :
2491 //=================================================================================
2492 SALOMEDS::Color SMESHGUI_GroupDlg::getGroupColor() const
2493 {
2494   QColor aQColor = getGroupQColor();
2495
2496   SALOMEDS::Color aColor;
2497   aColor.R = (float)aQColor.red() / 255.0;
2498   aColor.G = (float)aQColor.green() / 255.0;
2499   aColor.B = (float)aQColor.blue() / 255.0;
2500
2501   return aColor;
2502 }
2503
2504 //=================================================================================
2505 // function : setGroupQColor()
2506 // purpose  :
2507 //=================================================================================
2508 void SMESHGUI_GroupDlg::setGroupQColor( const QColor& theColor )
2509 {
2510   if( theColor.isValid() )
2511     myColorBtn->setColor( theColor );
2512 }
2513
2514 //=================================================================================
2515 // function : getGroupQColor()
2516 // purpose  :
2517 //=================================================================================
2518 QColor SMESHGUI_GroupDlg::getGroupQColor() const
2519 {
2520   return myColorBtn->color();
2521 }
2522
2523 //=================================================================================
2524 // function : setDefaultGroupColor()
2525 // purpose  :
2526 //=================================================================================
2527 void SMESHGUI_GroupDlg::setDefaultGroupColor()
2528 {
2529   if( myMesh->_is_nil() )
2530     return;
2531
2532   bool isAutoColor = myMesh->GetAutoColor();
2533
2534   QColor aQColor = myColorBtn->color();
2535   if( !isAutoColor )
2536   {
2537     if ( !aQColor.isValid() ) {
2538       int r = 0, g = 0, b = 0;
2539       SMESH::GetColor( "SMESH", "default_grp_color", r, g, b, QColor( 255, 170, 0 ) );
2540       aQColor.setRgb( r, g, b );
2541     }
2542   }
2543   else
2544   {
2545 #ifdef SIMPLE_AUTOCOLOR   // simplified algorithm for auto-colors
2546     SALOMEDS::Color aColor = SMESHGUI::getPredefinedUniqueColor();
2547 #else                     // old algorithm  for auto-colors
2548     SMESH::ListOfGroups aListOfGroups = *myMesh->GetGroups();
2549
2550     QList<SALOMEDS::Color> aReservedColors;
2551     for( int i = 0, n = aListOfGroups.length(); i < n; i++ )
2552     {
2553       SMESH::SMESH_GroupBase_var aGroupObject = aListOfGroups[i];
2554       SALOMEDS::Color aReservedColor = aGroupObject->GetColor();
2555       aReservedColors.append( aReservedColor );
2556     }
2557
2558     SALOMEDS::Color aColor = SMESHGUI::getUniqueColor( aReservedColors );
2559 #endif                    // SIMPLE_AUTOCOLOR
2560
2561     aQColor.setRgb( (int)( aColor.R * 255.0 ),
2562                     (int)( aColor.G * 255.0 ),
2563                     (int)( aColor.B * 255.0 ) );
2564
2565   }
2566
2567   setGroupQColor( aQColor );
2568 }
2569
2570 //=================================================================================
2571 // function : SetAppropriateActor()
2572 // purpose  : Find more appropriate of visible actors, set it to myActor, allow picking
2573 //            NPAL19389: create a group with a selection in another group.
2574 //            if mesh actor is not visible - find any first visible group or sub-mesh
2575 //=================================================================================
2576 bool SMESHGUI_GroupDlg::SetAppropriateActor()
2577 {
2578   bool isActor = false;
2579   myActorsList.clear();
2580
2581   if (myMesh->_is_nil()) return false;
2582
2583   SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView();
2584
2585   if (myGrpTypeGroup->checkedId() > 0) {   // try current group on geometry actor
2586     SMESH_Actor* anActor = 0;
2587     if (!myGroupOnGeom->_is_nil())
2588       anActor = SMESH::FindActorByObject(myGroupOnGeom);
2589     if (!myGroupOnFilter->_is_nil())
2590       anActor = SMESH::FindActorByObject(myGroupOnFilter);
2591     if (anActor && anActor->hasIO())
2592     {
2593       isActor = true;
2594       if (aViewWindow && !aViewWindow->isVisible(anActor->getIO()))
2595         isActor = false;
2596       else
2597         myActorsList.append(anActor);
2598     }
2599     return anActor;
2600   }
2601   else {
2602     // try mesh actor
2603     SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh);
2604     if (anActor && anActor->hasIO()) {
2605       isActor = true;
2606       if (aViewWindow && !aViewWindow->isVisible(anActor->getIO()))
2607         isActor = false;
2608       else
2609         myActorsList.append(anActor);
2610     }
2611
2612     // try group actor
2613     SMESH_Actor* aGroupActor = 0;
2614     if (!isActor && !myGroup->_is_nil()) {
2615       aGroupActor = SMESH::FindActorByObject(myGroup);
2616       if (aGroupActor && aGroupActor->hasIO())
2617         myActorsList.append(aGroupActor);
2618     }
2619
2620     // try any visible actor of group or sub-mesh of current mesh
2621     if (aViewWindow) {
2622       // mesh entry
2623       _PTR(SObject) aSObject = SMESH::FindSObject(myMesh);
2624       if (aSObject) {
2625         CORBA::String_var meshEntry = aSObject->GetID().c_str();
2626         int len = strlen(meshEntry);
2627
2628         // iterate on all actors in current view window, search for
2629         // any visible actor, that belongs to group or submesh of current mesh
2630         VTK::ActorCollectionCopy aCopy(aViewWindow->getRenderer()->GetActors());
2631         vtkActorCollection *aCollection = aCopy.GetActors();
2632         int nbItems = aCollection->GetNumberOfItems();
2633         for (int i=0; i<nbItems && !isActor; i++)
2634         {
2635           SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(aCollection->GetItemAsObject(i));
2636           if (anActor && anActor->hasIO()) {
2637             Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
2638             if (aViewWindow->isVisible(anIO)) {
2639               if (anIO->hasEntry() && strncmp(anIO->getEntry(), meshEntry, len) == 0 && !myActorsList.contains(anActor) )
2640                 myActorsList.append(anActor);
2641             }
2642           }
2643         }
2644       }
2645     }
2646
2647     // Show a standalone group if nothing else is visible (IPAL52227)
2648     if ( myActorsList.count() == 1 &&
2649          myActorsList[0] == aGroupActor &&
2650          aViewWindow && !aViewWindow->isVisible(aGroupActor->getIO()))
2651       SMESH::UpdateView( aViewWindow, SMESH::eDisplay, aGroupActor->getIO()->getEntry() );
2652   }
2653
2654
2655   if (myActorsList.count() > 0) {
2656     QListIterator<SMESH_Actor*> it( myActorsList );
2657     while ( it.hasNext() ) {
2658       SMESH_Actor* anActor = it.next();
2659       if ( IsActorVisible(anActor) )
2660         anActor->SetPickable(true);
2661     }
2662   }
2663
2664   return ( isActor || (myActorsList.count() > 0) );
2665 }
2666
2667 //=======================================================================
2668 //function : setShowEntityMode
2669 //purpose  : make shown only entity corresponding to my type
2670 //=======================================================================
2671 void SMESHGUI_GroupDlg::setShowEntityMode()
2672 {
2673   if ( !myMesh->_is_nil() ) {
2674     if ( SMESH_Actor* actor = SMESH::FindActorByObject(myMesh) ) {
2675       if (!myStoredShownEntity)
2676         myStoredShownEntity = actor->GetEntityMode();
2677       switch ( myTypeId ) {
2678       case grpNodeSelection:   restoreShowEntityMode();                          break;
2679       case grp0DSelection:     actor->SetEntityMode( SMESH_Actor::e0DElements ); break;
2680       case grpBallSelection:   actor->SetEntityMode( SMESH_Actor::eBallElem );   break;
2681       case grpEdgeSelection:   actor->SetEntityMode( SMESH_Actor::eEdges );      break;
2682       case grpFaceSelection:   actor->SetEntityMode( SMESH_Actor::eFaces );      break;
2683       case grpVolumeSelection: actor->SetEntityMode( SMESH_Actor::eVolumes );    break;
2684       }
2685     }
2686   }
2687 }
2688
2689 //=======================================================================
2690 //function : restoreShowEntityMode
2691 //purpose  : restore ShowEntity mode of myActor
2692 //=======================================================================
2693 void SMESHGUI_GroupDlg::restoreShowEntityMode()
2694 {
2695   if ( myStoredShownEntity && !myMesh->_is_nil() ) {
2696     if ( SMESH_Actor* actor = SMESH::FindActorByObject(myMesh) ) {
2697       actor->SetEntityMode(myStoredShownEntity);
2698     }
2699   }
2700   myStoredShownEntity = 0;
2701 }
2702
2703 //=======================================================================
2704 //function : IsActorVisible
2705 //purpose  : return visibility of the actor
2706 //=======================================================================
2707 bool SMESHGUI_GroupDlg::IsActorVisible( SMESH_Actor* theActor )
2708 {
2709   SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView();
2710   if (theActor && aViewWindow)
2711     return aViewWindow->isVisible(theActor->getIO());
2712   return false;
2713 }
2714
2715 //================================================================
2716 //function : setIsApplyAndClose
2717 //purpose  : Set value of the flag indicating that the dialog is
2718 //           accepted by Apply & Close button
2719 //================================================================
2720 void SMESHGUI_GroupDlg::setIsApplyAndClose( const bool theFlag )
2721 {
2722   myIsApplyAndClose = theFlag;
2723 }
2724
2725 //================================================================
2726 //function : isApplyAndClose
2727 //purpose  : Get value of the flag indicating that the dialog is
2728 //           accepted by Apply & Close button
2729 //================================================================
2730 bool SMESHGUI_GroupDlg::isApplyAndClose() const
2731 {
2732   return myIsApplyAndClose;
2733 }