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