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