Salome HOME
7b1ad6ec92a35cdd376a4c38424dd473b9f45d21
[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   SUIT_OverrideCursor wc;
1831
1832   myIsBusy = true;
1833   int sizeBefore = myElements->count();
1834
1835   SMESH::ElementType aType = SMESH::ALL;
1836   switch(myTypeId) {
1837   case grpNodeSelection:
1838     aType = SMESH::NODE;
1839     mySelector->SetSelectionMode(NodeSelection);
1840     break;
1841   case grpBallSelection:
1842     aType = SMESH::BALL;
1843     mySelector->SetSelectionMode(BallSelection);
1844     break;
1845   case grp0DSelection:
1846     aType = SMESH::ELEM0D;
1847     mySelector->SetSelectionMode(Elem0DSelection);
1848     break;
1849   case grpEdgeSelection:
1850     aType = SMESH::EDGE;
1851     mySelector->SetSelectionMode(EdgeSelection);
1852     break;
1853   case grpFaceSelection:
1854     aType = SMESH::FACE;
1855     mySelector->SetSelectionMode(FaceSelection);
1856     break;
1857   case grpVolumeSelection:
1858     aType = SMESH::VOLUME;
1859     mySelector->SetSelectionMode(VolumeSelection);
1860     break;
1861   default:
1862     mySelector->SetSelectionMode(ActorSelection);
1863   }
1864
1865   QListWidgetItem* anItem = 0;
1866   QList<QListWidgetItem*> listItemsToSel;
1867
1868   if (myCurrentLineEdit == 0) {
1869     //if (aNbSel != 1) { myIsBusy = false; return; }
1870     QString aListStr = "";
1871     int aNbItems = 0;
1872     if (myTypeId == 0) {
1873       QListIterator<SMESH_Actor*> it( myActorsList );
1874       while ( it.hasNext() ) {
1875         QString tmpStr;
1876         aNbItems += SMESH::GetNameOfSelectedNodes(mySelector, it.next()->getIO(), tmpStr);
1877         aListStr += tmpStr;
1878       }
1879     }
1880     else {
1881       QListIterator<SMESH_Actor*> it( myActorsList );
1882       while ( it.hasNext() ) {
1883         QString tmpStr;
1884         aNbItems += SMESH::GetNameOfSelectedElements(mySelector, it.next()->getIO(), tmpStr);
1885         aListStr += tmpStr;
1886       }
1887     }
1888     if (aNbItems > 0) {
1889       QStringList anElements = aListStr.split( " ", QString::SkipEmptyParts);
1890       for (QStringList::iterator it = anElements.begin(); it != anElements.end(); ++it) {
1891         QList<QListWidgetItem*> found = myElements->findItems(*it, Qt::MatchExactly);
1892         if (found.count() == 0) {
1893           anItem = new QListWidgetItem(*it);
1894           myElements->addItem(anItem);
1895           if (!anItem->isSelected())
1896             listItemsToSel.push_back(anItem);
1897         }
1898         else {
1899           foreach(anItem, found)
1900             if (!anItem->isSelected())
1901               listItemsToSel.push_back(anItem);
1902         }
1903       }
1904       bool blocked = myElements->signalsBlocked();
1905       myElements->blockSignals(true);
1906       foreach(anItem, listItemsToSel) anItem->setSelected(true);
1907       myElements->blockSignals(blocked);
1908       onListSelectionChanged();
1909       listItemsToSel.clear();
1910     }
1911   } else if (myCurrentLineEdit == mySubMeshLine) {
1912     //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1913
1914     SALOME_ListIO aList;
1915     mySelectionMgr->selectedObjects( aList );
1916
1917     SALOME_ListIteratorOfListIO anIt (aList);
1918     for ( ; anIt.More(); anIt.Next()) {
1919       SMESH::SMESH_subMesh_var aSubMesh =
1920         SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIt.Value());
1921       if (!aSubMesh->_is_nil()) {
1922         // check if mesh is the same
1923         if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
1924           try {
1925             SMESH::long_array_var anElements = aSubMesh->GetElementsByType(aType);
1926             int k = anElements->length();
1927             for (int i = 0; i < k; i++) {
1928               QString aText = QString::number(anElements[i]);
1929               QList<QListWidgetItem*> found = myElements->findItems(aText, Qt::MatchExactly);
1930               if (found.count() == 0) {
1931                 anItem = new QListWidgetItem(aText);
1932                 myElements->addItem(anItem);
1933                 if (!anItem->isSelected())
1934                   listItemsToSel.push_back(anItem);
1935               }
1936               else {
1937                 foreach(anItem, found)
1938                   if (!anItem->isSelected())
1939                     listItemsToSel.push_back(anItem);
1940               }
1941             }
1942             bool blocked = myElements->signalsBlocked();
1943             myElements->blockSignals(true);
1944             foreach(anItem, listItemsToSel) anItem->setSelected(true);
1945             myElements->blockSignals(blocked);
1946             onListSelectionChanged();
1947             listItemsToSel.clear();
1948           }
1949           catch (const SALOME::SALOME_Exception& ex) {
1950             SalomeApp_Tools::QtCatchCorbaException(ex);
1951           }
1952         }
1953       }
1954     }
1955     mySelectSubMesh->setChecked(false);
1956     myIsBusy = false;
1957     onListSelectionChanged();
1958
1959   } else if (myCurrentLineEdit == myGroupLine) {
1960     //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1961     SALOME_ListIO aList;
1962     mySelectionMgr->selectedObjects( aList );
1963
1964     SALOME_ListIteratorOfListIO anIt (aList);
1965     for ( ; anIt.More(); anIt.Next()) {
1966       SMESH::SMESH_GroupBase_var aGroup =
1967         SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(anIt.Value());
1968       if (!aGroup->_is_nil()) {
1969         // check if mesh is the same
1970         if (aGroup->GetType() == aType && aGroup->GetMesh()->GetId() == myMesh->GetId()) {
1971           SMESH::long_array_var anElements = aGroup->GetListOfID();
1972           int k = anElements->length();
1973           for (int i = 0; i < k; i++) {
1974             QString aText = QString::number(anElements[i]);
1975             QList<QListWidgetItem*> found = myElements->findItems(aText, Qt::MatchExactly);
1976             if (found.count() == 0) {
1977               anItem = new QListWidgetItem(aText);
1978               myElements->addItem(anItem);
1979               if (!anItem->isSelected())
1980                 listItemsToSel.push_back(anItem);
1981             }
1982             else {
1983               foreach(anItem, found)
1984                 if (!anItem->isSelected())
1985                   listItemsToSel.push_back(anItem);
1986             }
1987           }
1988           bool blocked = myElements->signalsBlocked();
1989           myElements->blockSignals(true);
1990           foreach(anItem, listItemsToSel) anItem->setSelected(true);
1991           myElements->blockSignals(blocked);
1992           onListSelectionChanged();
1993           listItemsToSel.clear();
1994         }
1995       }
1996     }
1997     mySelectGroup->setChecked(false);
1998     myIsBusy = false;
1999     onListSelectionChanged();
2000
2001   } else if (myCurrentLineEdit == myGeomGroupLine && myGeomObjects->length() == 1) {
2002     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
2003     GEOM::GEOM_IGroupOperations_wrap aGroupOp =
2004       SMESH::GetGEOMGen()->GetIGroupOperations(aStudy->StudyId());
2005
2006     SMESH::ElementType aGroupType = SMESH::ALL;
2007     switch(aGroupOp->GetType(myGeomObjects[0])) {
2008     case TopAbs_VERTEX: aGroupType = SMESH::NODE; break;
2009     case TopAbs_EDGE:   aGroupType = SMESH::EDGE; break;
2010     case TopAbs_FACE:   aGroupType = SMESH::FACE; break;
2011     case TopAbs_SOLID:  aGroupType = SMESH::VOLUME; break;
2012     default: myIsBusy = false; return;
2013     }
2014
2015     if (aGroupType == aType) {
2016       _PTR(SObject) aGroupSO =
2017         //aStudy->FindObjectIOR(aStudy->ConvertObjectToIOR(myGeomGroup));
2018         aStudy->FindObjectID(myGeomObjects[0]->GetStudyEntry());
2019       // Construct filter
2020       SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
2021       SMESH::Filter_var aFilter = aFilterMgr->CreateFilter();
2022       SMESH::BelongToGeom_var aBelongToGeom = aFilterMgr->CreateBelongToGeom();
2023       aBelongToGeom->SetGeom(myGeomObjects[0]);
2024       aBelongToGeom->SetShapeName(aGroupSO->GetName().c_str());
2025       aBelongToGeom->SetElementType(aType);
2026       aFilter->SetPredicate(aBelongToGeom);
2027
2028       SMESH::long_array_var anElements = aFilter->GetElementsId(myMesh);
2029
2030       int k = anElements->length();
2031       for (int i = 0; i < k; i++) {
2032         QString aText = QString::number(anElements[i]);
2033         QList<QListWidgetItem*> found = myElements->findItems(aText, Qt::MatchExactly);
2034         if (found.count() == 0) {
2035           anItem = new QListWidgetItem(aText);
2036           myElements->addItem(anItem);
2037           if (!anItem->isSelected())
2038             listItemsToSel.push_back(anItem);
2039         }
2040         else {
2041           foreach(anItem, found)
2042             if (!anItem->isSelected())
2043               listItemsToSel.push_back(anItem);
2044         }
2045       }
2046       bool blocked = myElements->signalsBlocked();
2047       myElements->blockSignals(true);
2048       foreach(anItem, listItemsToSel) anItem->setSelected(true);
2049       myElements->blockSignals(blocked);
2050       onListSelectionChanged();
2051       listItemsToSel.clear();
2052     }
2053
2054     //VSR: mySelectGeomGroup->setChecked(false);
2055     myIsBusy = false;
2056     onListSelectionChanged();
2057   }
2058   myIsBusy = false;
2059   if ( sizeBefore < myElements->count() )
2060     ++myNbChangesOfContents;
2061   //  mySelectionMgr->clearSelected();
2062   updateButtons();
2063 }
2064
2065 //=================================================================================
2066 // function : onRemove()
2067 // purpose  :
2068 //=================================================================================
2069 void SMESHGUI_GroupDlg::onRemove()
2070 {
2071   myIsBusy = true;
2072   int sizeBefore = myElements->count();
2073
2074   if (myCurrentLineEdit == 0) {
2075     QList<QListWidgetItem*> selItems = myElements->selectedItems();
2076     QListWidgetItem* item;
2077     foreach(item, selItems) delete item;
2078   } else {
2079     SALOME_ListIO aList;
2080     mySelectionMgr->selectedObjects( aList );
2081
2082     int aNbSel = aList.Extent();
2083
2084     if (aNbSel == 0) { myIsBusy = false; return; }
2085
2086     SMESH::ElementType aType = SMESH::ALL;
2087     switch(myTypeId) {
2088     case grpNodeSelection:   aType = SMESH::NODE;   break;
2089     case grp0DSelection:     aType = SMESH::ELEM0D; break;
2090     case grpBallSelection:   aType = SMESH::BALL;   break;
2091     case grpEdgeSelection:   aType = SMESH::EDGE;   break;
2092     case grpFaceSelection:   aType = SMESH::FACE;   break;
2093     case grpVolumeSelection: aType = SMESH::VOLUME; break;
2094     }
2095
2096     if (myCurrentLineEdit == mySubMeshLine) {
2097       //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
2098       SALOME_ListIO aList;
2099       mySelectionMgr->selectedObjects( aList );
2100
2101       SALOME_ListIteratorOfListIO anIt (aList);
2102       for ( ; anIt.More(); anIt.Next()) {
2103         SMESH::SMESH_subMesh_var aSubMesh = SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIt.Value());
2104         if (!aSubMesh->_is_nil()) {
2105           // check if mesh is the same
2106           if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
2107             if (aType == SMESH::NODE) {
2108               try {
2109                 SMESH::long_array_var anElements = aSubMesh->GetNodesId();
2110                 int k = anElements->length();
2111                 for (int i = 0; i < k; i++) {
2112                   QList<QListWidgetItem*> found = 
2113                     myElements->findItems(QString::number(anElements[i]), Qt::MatchExactly);
2114                   QListWidgetItem* anItem;
2115                   foreach(anItem, found) delete anItem;
2116                 }
2117               }
2118               catch (const SALOME::SALOME_Exception& ex) {
2119                 SalomeApp_Tools::QtCatchCorbaException(ex);
2120               }
2121             }
2122             else {
2123               try {
2124                 SMESH::long_array_var anElements = aSubMesh->GetElementsId();
2125                 int k = anElements->length();
2126                 for (int i = 0; i < k; i++) {
2127                   QList<QListWidgetItem*> found = 
2128                     myElements->findItems(QString::number(anElements[i]), Qt::MatchExactly);
2129                   QListWidgetItem* anItem;
2130                   foreach(anItem, found) delete anItem;
2131                 }
2132               }
2133               catch (const SALOME::SALOME_Exception& ex) {
2134                 SalomeApp_Tools::QtCatchCorbaException(ex);
2135               }
2136             }
2137           }
2138         }
2139       }
2140     }
2141     else if (myCurrentLineEdit == myGroupLine) {
2142       Standard_Boolean aRes;
2143       //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
2144       SALOME_ListIO aList;
2145       mySelectionMgr->selectedObjects( aList );
2146
2147       SALOME_ListIteratorOfListIO anIt (aList);
2148       for ( ; anIt.More(); anIt.Next()) {
2149         SMESH::SMESH_Group_var aGroup = SMESH::IObjectToInterface<SMESH::SMESH_Group>(anIt.Value());
2150         if (aRes && !aGroup->_is_nil()) {
2151           // check if mesh is the same
2152           if (aGroup->GetType() == aType && aGroup->GetMesh()->GetId() == myMesh->GetId()) {
2153             SMESH::long_array_var anElements = aGroup->GetListOfID();
2154             int k = anElements->length();
2155             for (int i = 0; i < k; i++) {
2156               QList<QListWidgetItem*> found = 
2157                 myElements->findItems(QString::number(anElements[i]), Qt::MatchExactly);
2158               QListWidgetItem* anItem;
2159               foreach(anItem, found) delete anItem;
2160             }
2161           }
2162         }
2163       }
2164     }
2165   }
2166   myIsBusy = false;
2167   if ( sizeBefore > myElements->count() )
2168     myNbChangesOfContents += 2; // it's used to detect that "Add" was only once
2169   updateButtons();
2170 }
2171
2172 //=================================================================================
2173 // function : onSort()
2174 // purpose  :
2175 //=================================================================================
2176 void SMESHGUI_GroupDlg::onSort()
2177 {
2178   // PAL5412: sorts items in ascending by "string" value
2179   // myElements->sort(true);
2180   // myElements->update();
2181   int i, k = myElements->count();
2182   if (k > 0) {
2183     myIsBusy = true;
2184     QList<int> aSelected;
2185     std::vector<int> anArray(k);
2186     //    QMemArray<int> anArray(k);
2187     // fill the array
2188     for (i = 0; i < k; i++) {
2189       int id = myElements->item(i)->text().toInt();
2190       anArray[i] = id;
2191       if (myElements->item(i)->isSelected())
2192         aSelected.append(id);
2193     }
2194     // sort & update list
2195     std::sort(anArray.begin(), anArray.end());
2196     //    anArray.sort();
2197     myElements->clear();
2198     QListWidgetItem* anItem;
2199     QList<QListWidgetItem*> listItemsToSel;
2200     for (i = 0; i < k; i++) {
2201       anItem = new QListWidgetItem(QString::number(anArray[i]));
2202       myElements->addItem(anItem);
2203       if (aSelected.contains(anArray[i]))
2204         listItemsToSel.push_back(anItem);
2205     }
2206     bool blocked = myElements->signalsBlocked();
2207     myElements->blockSignals(true);
2208     foreach(anItem, listItemsToSel) anItem->setSelected(true);
2209     myElements->blockSignals(blocked);
2210     listItemsToSel.clear();
2211     myIsBusy = false;
2212   }
2213 }
2214
2215 //=================================================================================
2216 // function : onVisibilityChanged()
2217 // purpose  :
2218 //=================================================================================
2219 void SMESHGUI_GroupDlg::onVisibilityChanged()
2220 {
2221   SetAppropriateActor();
2222 }
2223
2224 //=================================================================================
2225 // function : SMESHGUI_GroupDlg::reject
2226 // purpose  : SLOT called when "Close" button pressed. Close dialog
2227 //=================================================================================
2228 void SMESHGUI_GroupDlg::reject()
2229 {
2230   if (SMESH::GetCurrentVtkView()) {
2231     SMESH::RemoveFilters(); // PAL6938 -- clean all mesh entity filters
2232     SMESH::SetPointRepresentation(false);
2233     SMESH::SetPickable();
2234     restoreShowEntityMode();
2235   }
2236
2237   if( isApplyAndClose() && !myObjectToSelect.isEmpty() ) {
2238     SUIT_DataOwnerPtrList aList;
2239     aList.append( new LightApp_DataOwner( myObjectToSelect ) );
2240     mySelectionMgr->setSelected( aList );
2241   }
2242   else
2243     mySelectionMgr->clearSelected();
2244   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
2245     aViewWindow->SetSelectionMode(ActorSelection);
2246   mySelectionMgr->clearFilters();
2247   mySMESHGUI->ResetState();
2248
2249   QDialog::reject();
2250
2251   if ( myFilterDlg ) myFilterDlg->UnRegisterFilters();
2252 }
2253
2254 //=================================================================================
2255 // function : onOpenView()
2256 // purpose  :
2257 //=================================================================================
2258 void SMESHGUI_GroupDlg::onOpenView()
2259 {
2260   if ( mySelector ) {
2261     SMESH::SetPointRepresentation(false);
2262   }
2263   else {
2264     mySelector = SMESH::GetViewWindow( mySMESHGUI )->GetSelector();
2265     mySMESHGUI->EmitSignalDeactivateDialog();
2266     setEnabled(true);
2267   }
2268 }
2269
2270 //=================================================================================
2271 // function : onCloseView()
2272 // purpose  :
2273 //=================================================================================
2274 void SMESHGUI_GroupDlg::onCloseView()
2275 {
2276   onDeactivate();
2277   mySelector = 0;
2278 }
2279
2280 //=================================================================================
2281 // function : onHelp()
2282 // purpose  :
2283 //=================================================================================
2284 void SMESHGUI_GroupDlg::onHelp()
2285 {
2286   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
2287   if (app)
2288     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString( "" ), myHelpFileName);
2289   else {
2290     QString platform;
2291 #ifdef WIN32
2292     platform = "winapplication";
2293 #else
2294     platform = "application";
2295 #endif
2296     SUIT_MessageBox::warning(this, tr( "WRN_WARNING" ),
2297                              tr( "EXTERNAL_BROWSER_CANNOT_SHOW_PAGE" ).
2298                              arg(app->resourceMgr()->stringValue( "ExternalBrowser",
2299                                                                  platform)).
2300                              arg(myHelpFileName));
2301   }
2302 }
2303
2304 //=================================================================================
2305 // function : SMESHGUI_GroupDlg::onDeactivate
2306 // purpose  : SLOT called when dialog must be deativated
2307 //=================================================================================
2308 void SMESHGUI_GroupDlg::onDeactivate()
2309 {
2310   mySMESHGUI->ResetState();
2311   setEnabled(false);
2312 }
2313
2314 //=================================================================================
2315 // function : SMESHGUI_GroupDlg::enterEvent
2316 // purpose  : Event filter
2317 //=================================================================================
2318 void SMESHGUI_GroupDlg::enterEvent (QEvent*)
2319 {
2320   if (!isEnabled()) {
2321     SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI );
2322     if ( aViewWindow && !mySelector) {
2323       mySelector = aViewWindow->GetSelector();
2324     }
2325     mySMESHGUI->EmitSignalDeactivateDialog();
2326     setEnabled(true);
2327     mySelectionMode = grpNoSelection;
2328     setSelectionMode(myTypeId);
2329     //mySMESHGUI->SetActiveDialogBox((QDialog*)this);
2330     mySMESHGUI->SetActiveDialogBox(this);
2331     mySMESHGUI->SetState(800);
2332   }
2333 }
2334
2335 //=================================================================================
2336 // function : keyPressEvent()
2337 // purpose  :
2338 //=================================================================================
2339 void SMESHGUI_GroupDlg::keyPressEvent( QKeyEvent* e )
2340 {
2341   QDialog::keyPressEvent( e );
2342   if ( e->isAccepted() )
2343     return;
2344
2345   if ( e->key() == Qt::Key_F1 )
2346     {
2347       e->accept();
2348       onHelp();
2349     }
2350 }
2351
2352 //================================================================================
2353 /*!
2354  * \brief Enable showing of the popup when Geometry selection btn is clicked
2355   * \param enable - true to enable
2356  */
2357 //================================================================================
2358
2359 enum { DIRECT_GEOM_INDEX = 0, GEOM_BY_MESH_INDEX };
2360
2361 void SMESHGUI_GroupDlg::updateGeomPopup()
2362 {
2363   bool enable = false;
2364
2365   if ( !myMesh->_is_nil() )
2366     enable = myMesh->NbEdges() > 0;
2367
2368   if ( myGeomGroupBtn )
2369   {
2370     disconnect( myGeomGroupBtn, SIGNAL( toggled(bool) ), this, SLOT( onGeomSelectionButton(bool) ));
2371     if ( enable ) {
2372       if ( !myGeomPopup ) {
2373         myGeomPopup = new QMenu(this);
2374         myActions[myGeomPopup->addAction( tr( "DIRECT_GEOM_SELECTION" ) )] = DIRECT_GEOM_INDEX;
2375         myActions[myGeomPopup->addAction( tr( "GEOM_BY_MESH_ELEM_SELECTION" ) )] = GEOM_BY_MESH_INDEX;
2376         connect( myGeomPopup, SIGNAL( triggered( QAction* ) ), SLOT( onGeomPopup( QAction* ) ) );
2377       }
2378       connect( myGeomGroupBtn, SIGNAL( toggled(bool) ), this, SLOT( onGeomSelectionButton(bool) ));
2379     }
2380   }
2381 }
2382
2383
2384 //=================================================================================
2385 // function : onGeomSelectionButton()
2386 // purpose  :
2387 //=================================================================================
2388 void SMESHGUI_GroupDlg::onGeomSelectionButton(bool isBtnOn)
2389 {
2390   if ( myGeomPopup && isBtnOn )
2391     {
2392       myCurrentLineEdit = myGeomGroupLine;
2393       QAction* a = myGeomPopup->exec( QCursor::pos() );
2394       if (!a || myActions[a] == DIRECT_GEOM_INDEX)
2395         setSelectionMode(grpGeomSelection);
2396     }
2397   else if (!isBtnOn)
2398     {
2399       myCurrentLineEdit = 0;
2400       setSelectionMode(grpAllSelection);
2401     }
2402 }
2403
2404 //=================================================================================
2405 // function : onGeomPopup()
2406 // purpose  :
2407 //=================================================================================
2408 void SMESHGUI_GroupDlg::onGeomPopup( QAction* a )
2409 {
2410   int index = myActions[a];
2411   if ( index == GEOM_BY_MESH_INDEX )
2412     {
2413       mySelectionMode = grpNoSelection;
2414       if ( !myShapeByMeshOp ) {
2415         myShapeByMeshOp = new SMESHGUI_ShapeByMeshOp(true);
2416         connect(myShapeByMeshOp, SIGNAL(committed(SUIT_Operation*)),
2417                 SLOT(onPublishShapeByMeshDlg(SUIT_Operation*)));
2418         connect(myShapeByMeshOp, SIGNAL(aborted(SUIT_Operation*)),
2419                 SLOT(onCloseShapeByMeshDlg(SUIT_Operation*)));
2420       }
2421       // set mesh object to SMESHGUI_ShapeByMeshOp and start it
2422       if ( !myMesh->_is_nil() ) {
2423         myIsBusy = true;
2424         hide(); // stop processing selection
2425         myIsBusy = false;
2426         myShapeByMeshOp->setModule( mySMESHGUI );
2427         myShapeByMeshOp->setStudy( 0 ); // it's really necessary
2428         myShapeByMeshOp->SetMesh( myMesh );
2429         myShapeByMeshOp->start();
2430       }
2431     }
2432 }
2433
2434 //================================================================================
2435 /*!
2436  * \brief SLOT. Is called when Ok is pressed in SMESHGUI_ShapeByMeshDlg
2437  */
2438 //================================================================================
2439
2440 void SMESHGUI_GroupDlg::onPublishShapeByMeshDlg(SUIT_Operation* op)
2441 {
2442   if ( myShapeByMeshOp == op ) {
2443     mySMESHGUI->getApp()->updateObjectBrowser();
2444     show();
2445     // Select a found geometry object
2446     GEOM::GEOM_Object_var aGeomVar = myShapeByMeshOp->GetShape();
2447     if ( !aGeomVar->_is_nil() )
2448     {
2449       QString ID = aGeomVar->GetStudyEntry();
2450       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
2451       if ( _PTR(SObject) aGeomSO = aStudy->FindObjectID( ID.toLatin1().data() )) {
2452         SALOME_ListIO anIOList;
2453         Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject
2454           ( aGeomSO->GetID().c_str(), "SMESH", aGeomSO->GetName().c_str() );
2455         anIOList.Append( anIO );
2456         mySelectionMgr->setSelectedObjects( anIOList, false );
2457         onObjectSelectionChanged();
2458       }
2459     }
2460   }
2461 }
2462
2463 //================================================================================
2464 /*!
2465  * \brief SLOT. Is called when Close is pressed in SMESHGUI_ShapeByMeshDlg
2466  */
2467 //================================================================================
2468
2469 void SMESHGUI_GroupDlg::onCloseShapeByMeshDlg(SUIT_Operation* op)
2470 {
2471   if ( myShapeByMeshOp == op )
2472     {
2473       show();
2474       setSelectionMode(grpGeomSelection);
2475     }
2476 }
2477
2478 //=================================================================================
2479 // function : setGroupColor()
2480 // purpose  :
2481 //=================================================================================
2482 void SMESHGUI_GroupDlg::setGroupColor( const SALOMEDS::Color& theColor )
2483 {
2484   QColor aQColor( (int)( theColor.R * 255.0 ),
2485                   (int)( theColor.G * 255.0 ),
2486                   (int)( theColor.B * 255.0 ) );
2487   setGroupQColor( aQColor );
2488 }
2489
2490 //=================================================================================
2491 // function : getGroupColor()
2492 // purpose  :
2493 //=================================================================================
2494 SALOMEDS::Color SMESHGUI_GroupDlg::getGroupColor() const
2495 {
2496   QColor aQColor = getGroupQColor();
2497
2498   SALOMEDS::Color aColor;
2499   aColor.R = (float)aQColor.red() / 255.0;
2500   aColor.G = (float)aQColor.green() / 255.0;
2501   aColor.B = (float)aQColor.blue() / 255.0;
2502
2503   return aColor;
2504 }
2505
2506 //=================================================================================
2507 // function : setGroupQColor()
2508 // purpose  :
2509 //=================================================================================
2510 void SMESHGUI_GroupDlg::setGroupQColor( const QColor& theColor )
2511 {
2512   if( theColor.isValid() )
2513     myColorBtn->setColor( theColor );
2514 }
2515
2516 //=================================================================================
2517 // function : getGroupQColor()
2518 // purpose  :
2519 //=================================================================================
2520 QColor SMESHGUI_GroupDlg::getGroupQColor() const
2521 {
2522   return myColorBtn->color();
2523 }
2524
2525 //=================================================================================
2526 // function : setDefaultGroupColor()
2527 // purpose  :
2528 //=================================================================================
2529 void SMESHGUI_GroupDlg::setDefaultGroupColor()
2530 {
2531   if( myMesh->_is_nil() )
2532     return;
2533
2534   bool isAutoColor = myMesh->GetAutoColor();
2535
2536   QColor aQColor = myColorBtn->color();
2537   if( !isAutoColor )
2538   {
2539     if ( !aQColor.isValid() ) {
2540       int r = 0, g = 0, b = 0;
2541       SMESH::GetColor( "SMESH", "default_grp_color", r, g, b, QColor( 255, 170, 0 ) );
2542       aQColor.setRgb( r, g, b );
2543     }
2544   }
2545   else
2546   {
2547 #ifdef SIMPLE_AUTOCOLOR   // simplified algorithm for auto-colors
2548     SALOMEDS::Color aColor = SMESHGUI::getPredefinedUniqueColor();
2549 #else                     // old algorithm  for auto-colors
2550     SMESH::ListOfGroups aListOfGroups = *myMesh->GetGroups();
2551
2552     QList<SALOMEDS::Color> aReservedColors;
2553     for( int i = 0, n = aListOfGroups.length(); i < n; i++ )
2554     {
2555       SMESH::SMESH_GroupBase_var aGroupObject = aListOfGroups[i];
2556       SALOMEDS::Color aReservedColor = aGroupObject->GetColor();
2557       aReservedColors.append( aReservedColor );
2558     }
2559
2560     SALOMEDS::Color aColor = SMESHGUI::getUniqueColor( aReservedColors );
2561 #endif                    // SIMPLE_AUTOCOLOR
2562
2563     aQColor.setRgb( (int)( aColor.R * 255.0 ),
2564                     (int)( aColor.G * 255.0 ),
2565                     (int)( aColor.B * 255.0 ) );
2566
2567   }
2568
2569   setGroupQColor( aQColor );
2570 }
2571
2572 //=================================================================================
2573 // function : SetAppropriateActor()
2574 // purpose  : Find more appropriate of visible actors, set it to myActor, allow picking
2575 //            NPAL19389: create a group with a selection in another group.
2576 //            if mesh actor is not visible - find any first visible group or sub-mesh
2577 //=================================================================================
2578 bool SMESHGUI_GroupDlg::SetAppropriateActor()
2579 {
2580   bool isActor = false;
2581   myActorsList.clear();
2582
2583   if (myMesh->_is_nil()) return false;
2584
2585   SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView();
2586
2587   if (myGrpTypeGroup->checkedId() > 0) {   // try current group on geometry actor
2588     SMESH_Actor* anActor = 0;
2589     if (!myGroupOnGeom->_is_nil())
2590       anActor = SMESH::FindActorByObject(myGroupOnGeom);
2591     if (!myGroupOnFilter->_is_nil())
2592       anActor = SMESH::FindActorByObject(myGroupOnFilter);
2593     if (anActor && anActor->hasIO())
2594     {
2595       isActor = true;
2596       if (aViewWindow && !aViewWindow->isVisible(anActor->getIO()))
2597         isActor = false;
2598       else
2599         myActorsList.append(anActor);
2600     }
2601     return anActor;
2602   }
2603   else {
2604     // try mesh actor
2605     SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh);
2606     if (anActor && anActor->hasIO()) {
2607       isActor = true;
2608       if (aViewWindow && !aViewWindow->isVisible(anActor->getIO()))
2609         isActor = false;
2610       else
2611         myActorsList.append(anActor);
2612     }
2613
2614     // try group actor
2615     SMESH_Actor* aGroupActor = 0;
2616     if (!isActor && !myGroup->_is_nil()) {
2617       aGroupActor = SMESH::FindActorByObject(myGroup);
2618       if (aGroupActor && aGroupActor->hasIO())
2619         myActorsList.append(aGroupActor);
2620     }
2621
2622     // try any visible actor of group or sub-mesh of current mesh
2623     if (aViewWindow) {
2624       // mesh entry
2625       _PTR(SObject) aSObject = SMESH::FindSObject(myMesh);
2626       if (aSObject) {
2627         CORBA::String_var meshEntry = aSObject->GetID().c_str();
2628         int len = strlen(meshEntry);
2629
2630         // iterate on all actors in current view window, search for
2631         // any visible actor, that belongs to group or submesh of current mesh
2632         VTK::ActorCollectionCopy aCopy(aViewWindow->getRenderer()->GetActors());
2633         vtkActorCollection *aCollection = aCopy.GetActors();
2634         int nbItems = aCollection->GetNumberOfItems();
2635         for (int i=0; i<nbItems && !isActor; i++)
2636         {
2637           SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(aCollection->GetItemAsObject(i));
2638           if (anActor && anActor->hasIO()) {
2639             Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
2640             if (aViewWindow->isVisible(anIO)) {
2641               if (anIO->hasEntry() && strncmp(anIO->getEntry(), meshEntry, len) == 0 && !myActorsList.contains(anActor) )
2642                 myActorsList.append(anActor);
2643             }
2644           }
2645         }
2646       }
2647     }
2648
2649     // Show a standalone group if nothing else is visible (IPAL52227)
2650     if ( myActorsList.count() == 1 &&
2651          myActorsList[0] == aGroupActor &&
2652          aViewWindow && !aViewWindow->isVisible(aGroupActor->getIO()))
2653       SMESH::UpdateView( aViewWindow, SMESH::eDisplay, aGroupActor->getIO()->getEntry() );
2654   }
2655
2656
2657   if (myActorsList.count() > 0) {
2658     QListIterator<SMESH_Actor*> it( myActorsList );
2659     while ( it.hasNext() ) {
2660       SMESH_Actor* anActor = it.next();
2661       if ( IsActorVisible(anActor) )
2662         anActor->SetPickable(true);
2663     }
2664   }
2665
2666   return ( isActor || (myActorsList.count() > 0) );
2667 }
2668
2669 //=======================================================================
2670 //function : setShowEntityMode
2671 //purpose  : make shown only entity corresponding to my type
2672 //=======================================================================
2673 void SMESHGUI_GroupDlg::setShowEntityMode()
2674 {
2675   if ( !myMesh->_is_nil() ) {
2676     if ( SMESH_Actor* actor = SMESH::FindActorByObject(myMesh) ) {
2677       if (!myStoredShownEntity)
2678         myStoredShownEntity = actor->GetEntityMode();
2679       switch ( myTypeId ) {
2680       case grpNodeSelection:   restoreShowEntityMode();                          break;
2681       case grp0DSelection:     actor->SetEntityMode( SMESH_Actor::e0DElements ); break;
2682       case grpBallSelection:   actor->SetEntityMode( SMESH_Actor::eBallElem );   break;
2683       case grpEdgeSelection:   actor->SetEntityMode( SMESH_Actor::eEdges );      break;
2684       case grpFaceSelection:   actor->SetEntityMode( SMESH_Actor::eFaces );      break;
2685       case grpVolumeSelection: actor->SetEntityMode( SMESH_Actor::eVolumes );    break;
2686       }
2687     }
2688   }
2689 }
2690
2691 //=======================================================================
2692 //function : restoreShowEntityMode
2693 //purpose  : restore ShowEntity mode of myActor
2694 //=======================================================================
2695 void SMESHGUI_GroupDlg::restoreShowEntityMode()
2696 {
2697   if ( myStoredShownEntity && !myMesh->_is_nil() ) {
2698     if ( SMESH_Actor* actor = SMESH::FindActorByObject(myMesh) ) {
2699       actor->SetEntityMode(myStoredShownEntity);
2700     }
2701   }
2702   myStoredShownEntity = 0;
2703 }
2704
2705 //=======================================================================
2706 //function : IsActorVisible
2707 //purpose  : return visibility of the actor
2708 //=======================================================================
2709 bool SMESHGUI_GroupDlg::IsActorVisible( SMESH_Actor* theActor )
2710 {
2711   SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView();
2712   if (theActor && aViewWindow)
2713     return aViewWindow->isVisible(theActor->getIO());
2714   return false;
2715 }
2716
2717 //================================================================
2718 //function : setIsApplyAndClose
2719 //purpose  : Set value of the flag indicating that the dialog is
2720 //           accepted by Apply & Close button
2721 //================================================================
2722 void SMESHGUI_GroupDlg::setIsApplyAndClose( const bool theFlag )
2723 {
2724   myIsApplyAndClose = theFlag;
2725 }
2726
2727 //================================================================
2728 //function : isApplyAndClose
2729 //purpose  : Get value of the flag indicating that the dialog is
2730 //           accepted by Apply & Close button
2731 //================================================================
2732 bool SMESHGUI_GroupDlg::isApplyAndClose() const
2733 {
2734   return myIsApplyAndClose;
2735 }