Salome HOME
Issue 0020406: Time compilation when recompiling
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_GroupDlg.cxx
1 //  Copyright (C) 2007-2008  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 //  SMESH SMESHGUI : GUI for SMESH component
23 //  File   : SMESHGUI_GroupDlg.cxx
24 //  Author : Natalia KOPNOVA
25 //  Module : SMESH
26 //  $Header$
27 //
28 #include "SMESHGUI_GroupDlg.h"
29 #include "SMESHGUI_FilterDlg.h"
30 #include "SMESHGUI_ShapeByMeshDlg.h"
31
32 #include "SMESHGUI.h"
33 #include "SMESHGUI_Utils.h"
34 #include "SMESHGUI_VTKUtils.h"
35 #include "SMESHGUI_MeshUtils.h"
36 #include "SMESHGUI_GroupUtils.h"
37 #include "SMESHGUI_FilterUtils.h"
38 #include "SMESHGUI_GEOMGenUtils.h"
39
40 #include "SMESH_TypeFilter.hxx"
41 #include "SMESH_Actor.h"
42 #include "SMESH_ActorUtils.h"
43
44 #include "GEOMBase.h"
45 #include "GEOM_SelectionFilter.h"
46
47 #include "SUIT_Desktop.h"
48 #include "SUIT_ResourceMgr.h"
49 #include "SUIT_Session.h"
50 #include "SUIT_MessageBox.h"
51
52 #include "SalomeApp_Tools.h"
53 #include "SalomeApp_Application.h"
54 #include "SalomeApp_Study.h"
55 #include "LightApp_Application.h"
56 #include "SALOMEDSClient_Study.hxx"
57 #include "SALOME_ListIO.hxx"
58 #include "SALOME_ListIteratorOfListIO.hxx"
59
60 #include "SVTK_ViewWindow.h"
61 #include "SVTK_Selector.h"
62
63 #include "VTKViewer_Algorithm.h"
64
65 #include "utilities.h"
66
67 // VTK Includes
68 #include <vtkRenderer.h>
69 #include <vtkActorCollection.h>
70
71 // OCCT Includes
72 #include <TColStd_MapOfInteger.hxx>
73
74 // QT Includes
75 #include <qbuttongroup.h>
76 #include <qcursor.h>
77 #include <qgroupbox.h>
78 #include <qhbox.h>
79 #include <qlabel.h>
80 #include <qlineedit.h>
81 #include <qpushbutton.h>
82 #include <qtoolbutton.h>
83 #include <qradiobutton.h>
84 #include <qcheckbox.h>
85 #include <qlayout.h>
86 #include <qlistbox.h>
87 #include <qimage.h>
88 #include <qpixmap.h>
89 #include <qmemarray.h>
90 #include <qwidgetstack.h>
91 #include <qcolordialog.h>
92
93 #include <QtxIntSpinBox.h>
94
95 // STL includes
96 #include <vector>
97 #include <algorithm>
98 #include <set>
99
100 using namespace std;
101
102 //=================================================================================
103 // function : SMESHGUI_GroupDlg()
104 // purpose  :
105 //=================================================================================
106 SMESHGUI_GroupDlg::SMESHGUI_GroupDlg( SMESHGUI* theModule, const char* name,
107                                       SMESH::SMESH_Mesh_ptr theMesh, bool modal, WFlags fl)
108      : QDialog( SMESH::GetDesktop( theModule ), name, modal, WStyle_Customize | WStyle_NormalBorder |
109                 WStyle_Title | WStyle_SysMenu | WDestructiveClose),
110      mySMESHGUI( theModule ),
111      mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
112      mySelector(SMESH::GetViewWindow( theModule )->GetSelector()),
113      myIsBusy( false ),
114      myNameChanged(false),
115      myActor( 0 )
116 {
117   if (!name) setName("SMESHGUI_GroupDlg");
118   initDialog(true);
119   if (!theMesh->_is_nil())
120     init(theMesh);
121   else {
122     mySelectSubMesh->setEnabled(false);
123     mySelectGroup->setEnabled(false);
124     myGeomGroupBtn->setEnabled(false);
125     myGeomGroupLine->setEnabled(false);
126   }
127 }
128
129 //=================================================================================
130 // function : SMESHGUI_GroupDlg()
131 // purpose  :
132 //=================================================================================
133 SMESHGUI_GroupDlg::SMESHGUI_GroupDlg( SMESHGUI* theModule, const char* name,
134                                       SMESH::SMESH_GroupBase_ptr theGroup, bool modal, WFlags fl)
135      : QDialog( SMESH::GetDesktop( theModule ), name, modal, WStyle_Customize | WStyle_NormalBorder |
136                 WStyle_Title | WStyle_SysMenu | WDestructiveClose),
137      mySMESHGUI( theModule ),
138      mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
139      mySelector(SMESH::GetViewWindow( theModule )->GetSelector()),
140      myIsBusy( false ),
141      myNameChanged(false)
142 {
143   if (!name) setName("SMESHGUI_GroupDlg");
144
145   initDialog(false);
146   if (!theGroup->_is_nil())
147     init(theGroup);
148   else {
149     mySelectSubMesh->setEnabled(false);
150     mySelectGroup->setEnabled(false);
151
152     myCurrentLineEdit = myMeshGroupLine;
153     setSelectionMode(5);
154   }
155 }
156
157 //=================================================================================
158 // function : SMESHGUI_GroupDlg()
159 // purpose  :
160 //=================================================================================
161 void SMESHGUI_GroupDlg::initDialog(bool create)
162 {
163   myFilterDlg = 0;
164   myCreate = create;
165   myCurrentLineEdit = 0;
166
167   myShapeByMeshOp = 0;
168   myGeomPopup = 0;
169   myGeomObjects = new GEOM::ListOfGO();
170   myGeomObjects->length(0);
171
172   QPixmap image0 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
173
174   if (create) {
175     setCaption(tr("SMESH_CREATE_GROUP_TITLE"));
176     myHelpFileName = "creating_groups_page.html";
177   }
178   else {
179     setCaption(tr("SMESH_EDIT_GROUP_TITLE"));
180     myHelpFileName = "editing_groups_page.html";
181   }
182
183   setSizeGripEnabled(TRUE);
184
185   QGridLayout* aMainLayout = new QGridLayout(this, 7, 3, 11, 6);
186
187   /***************************************************************/
188   QLabel* meshGroupLab = new QLabel(this, "mesh/group label");
189   if (create)
190     meshGroupLab->setText(tr("SMESH_MESH"));
191   else
192     meshGroupLab->setText(tr("SMESH_GROUP"));
193   myMeshGroupBtn = new QPushButton(this, "mesh/group button");
194   myMeshGroupBtn->setPixmap(image0);
195   myMeshGroupLine = new QLineEdit(this, "mesh/group line");
196   myMeshGroupLine->setReadOnly(true);
197
198   /***************************************************************/
199   myTypeGroup = new QButtonGroup(1, Qt::Vertical, this, "Group types");
200   myTypeGroup->setTitle(tr("SMESH_ELEMENTS_TYPE"));
201   myTypeGroup->setExclusive(true);
202
203   QStringList types;
204   types.append(tr("MESH_NODE"));
205   types.append(tr("SMESH_EDGE"));
206   types.append(tr("SMESH_FACE"));
207   types.append(tr("SMESH_VOLUME"));
208   QRadioButton* rb;
209   for (int i = 0; i < types.count(); i++) {
210     rb = new QRadioButton(types[i], myTypeGroup);
211   }
212   myTypeGroup->setEnabled(create);
213   myTypeId = -1;
214
215   /***************************************************************/
216   QLabel* aName = new QLabel(this, "name label");
217   aName->setText(tr("SMESH_NAME"));
218   aName->setMinimumSize(50,0);
219   myName = new QLineEdit(this, "name");
220
221   /***************************************************************/
222   myGrpTypeGroup = new QButtonGroup(1, Qt::Vertical, this, "Type of group");
223   myGrpTypeGroup->setTitle(tr("SMESH_GROUP_TYPE"));
224   myGrpTypeGroup->setExclusive(true);
225   QRadioButton* rb1 = new QRadioButton( tr("SMESH_GROUP_STANDALONE"), myGrpTypeGroup);
226   QRadioButton* rb2 = new QRadioButton( tr("SMESH_GROUP_GEOMETRY"),   myGrpTypeGroup);
227   myGrpTypeGroup->setEnabled(create);
228   myGrpTypeId = -1;
229
230   /***************************************************************/
231   myWGStack = new QWidgetStack( this, "widget stack");
232   QWidget* wg1 = new QFrame( myWGStack, "first widget" );
233   QWidget* wg2 = new QFrame( myWGStack, "second widget" );
234
235   /***************************************************************/
236   QGroupBox* aContentBox = new QGroupBox(1, Qt::Horizontal, wg1, "content box");
237   aContentBox->setTitle(tr("SMESH_CONTENT"));
238   QFrame* aContent = new QFrame(aContentBox, "content");
239   QGridLayout* aLayout = new QGridLayout(aContent, 7, 4);
240   aLayout->setSpacing(6);
241   aLayout->setAutoAdd(false);
242
243   QLabel* aLabel = new QLabel(aContent, "elements label");
244   aLabel->setText(tr("SMESH_ID_ELEMENTS"));
245   myElements = new QListBox(aContent, "elements list");
246   myElements->setSelectionMode(QListBox::Extended);
247
248   myFilter = new QPushButton(aContent, "filter");
249   myFilter->setText(tr("SMESH_BUT_FILTER"));
250   QPushButton* aAddBtn = new QPushButton(aContent, "add");
251   aAddBtn->setText(tr("SMESH_BUT_ADD"));
252   QPushButton* aRemoveBtn = new QPushButton(aContent, "remove");
253   aRemoveBtn->setText(tr("SMESH_BUT_REMOVE"));
254   QPushButton* aSortBtn = new QPushButton(aContent, "sort");
255   aSortBtn->setText(tr("SMESH_BUT_SORT"));
256
257   aLayout->addWidget(aLabel, 0, 0);
258   aLayout->addMultiCellWidget(myElements, 1, 6, 0, 0);
259   aLayout->addWidget(myFilter, 1, 2);
260   aLayout->addWidget(aAddBtn, 3, 2);
261   aLayout->addWidget(aRemoveBtn, 4, 2);
262   aLayout->addWidget(aSortBtn, 6, 2);
263
264   aLayout->setColStretch(0, 1);
265   aLayout->addColSpacing(1, 20);
266   aLayout->addColSpacing(3, 20);
267   aLayout->setRowStretch(2, 1);
268   aLayout->setRowStretch(5, 1);
269
270   /***************************************************************/
271   QGroupBox* aSelectBox = new QGroupBox(3, Qt::Horizontal, wg1, "select box");
272   aSelectBox->setTitle(tr("SMESH_SELECT_FROM"));
273
274   mySelectSubMesh = new QCheckBox(aSelectBox, "submesh checkbox");
275   mySelectSubMesh->setText(tr("SMESH_SUBMESH"));
276   mySelectSubMesh->setMinimumSize(50, 0);
277   mySubMeshBtn = new QPushButton(aSelectBox, "submesh button");
278   mySubMeshBtn->setText("");
279   mySubMeshBtn->setPixmap(image0);
280   mySubMeshLine = new QLineEdit(aSelectBox, "submesh line");
281   mySubMeshLine->setReadOnly(true);
282   onSelectSubMesh(false);
283
284   mySelectGroup = new QCheckBox(aSelectBox, "group checkbox");
285   mySelectGroup->setText(tr("SMESH_GROUP"));
286   mySelectGroup->setMinimumSize(50, 0);
287   myGroupBtn = new QPushButton(aSelectBox, "group button");
288   myGroupBtn->setText("");
289   myGroupBtn->setPixmap(image0);
290   myGroupLine = new QLineEdit(aSelectBox, "group line");
291   myGroupLine->setReadOnly(true);
292   onSelectGroup(false);
293
294   /***************************************************************/
295   QGridLayout* wg1Layout = new QGridLayout( wg1, 3, 1, 0, 6 );
296   wg1Layout->addWidget(aContentBox, 0, 0);
297   wg1Layout->addWidget(aSelectBox, 1, 0);
298   wg1Layout->setRowStretch(2, 5);
299
300   /***************************************************************/
301   QLabel* geomObject = new QLabel(wg2, "geometry object label");
302   geomObject->setText(tr("SMESH_OBJECT_GEOM"));
303   myGeomGroupBtn = new QToolButton(wg2, "geometry group button");
304   myGeomGroupBtn->setIconSet( QIconSet(image0) );
305   myGeomGroupBtn->setToggleButton(true);
306   myGeomGroupLine = new QLineEdit(wg2, "geometry group line");
307   myGeomGroupLine->setReadOnly(true); //VSR ???
308   onSelectGeomGroup(false);
309
310   if (!create)
311     {
312       myGeomGroupBtn->setEnabled(false);
313       myGeomGroupLine->setEnabled(false);
314     }
315
316   /***************************************************************/
317   QGridLayout* wg2Layout = new QGridLayout( wg2, 2, 3, 0, 6 );
318   wg2Layout->addWidget(geomObject,     0, 0);
319   wg2Layout->addWidget(myGeomGroupBtn, 0, 1);
320   wg2Layout->addWidget(myGeomGroupLine,0, 2);
321   wg2Layout->setRowStretch(1, 5);
322
323   /***************************************************************/
324   QVBoxLayout* dumb = new QVBoxLayout(myWGStack);
325   dumb->addWidget(wg1);
326   dumb->addWidget(wg2);
327   myWGStack->addWidget( wg1, myGrpTypeGroup->id(rb1) );
328   myWGStack->addWidget( wg2, myGrpTypeGroup->id(rb2) );
329
330   /***************************************************************/
331   QGroupBox* aColorBox = new QGroupBox(2, Qt::Horizontal, this, "color box");
332   aColorBox->setTitle(tr("SMESH_SET_COLOR"));
333
334   new QLabel( tr("SMESH_CHECK_COLOR"), aColorBox, "color label" );
335   myColorBtn = new QPushButton(aColorBox, "color button");
336   myColorBtn->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
337
338   /***************************************************************/
339
340   QFrame* aButtons = new QFrame(this, "button box");
341   aButtons->setFrameStyle(QFrame::Box | QFrame::Sunken);
342   QHBoxLayout* aBtnLayout = new QHBoxLayout(aButtons, 11, 6);
343   aBtnLayout->setAutoAdd(false);
344
345   QPushButton* aApplyBtn = new QPushButton(aButtons, "apply");
346   aApplyBtn->setText(tr("SMESH_BUT_APPLY"));
347   aApplyBtn->setAutoDefault(true);
348   QPushButton* aCloseBtn = new QPushButton(aButtons, "close");
349   aCloseBtn->setText(tr("SMESH_BUT_CLOSE"));
350   aCloseBtn->setAutoDefault(true);
351   QPushButton* aOKBtn = new QPushButton(aButtons, "ok");
352   aOKBtn->setText(tr("SMESH_BUT_APPLY_AND_CLOSE"));
353   aOKBtn->setAutoDefault(true);
354   aOKBtn->setDefault(true);
355   QPushButton* aHelpBtn = new QPushButton(aButtons, "help");
356   aHelpBtn->setText(tr("SMESH_BUT_HELP"));
357   aHelpBtn->setAutoDefault(true);
358
359   aBtnLayout->addWidget(aOKBtn);
360   aBtnLayout->addWidget(aApplyBtn);
361   aBtnLayout->addStretch();
362   aBtnLayout->addWidget(aCloseBtn);
363   aBtnLayout->addWidget(aHelpBtn);
364
365   /***************************************************************/
366   aMainLayout->addWidget(meshGroupLab,    0, 0);
367   aMainLayout->addWidget(myMeshGroupBtn,  0, 1);
368   aMainLayout->addWidget(myMeshGroupLine, 0, 2);
369   aMainLayout->addMultiCellWidget(myTypeGroup,    1, 1, 0, 2);
370   aMainLayout->addWidget(aName,      2, 0);
371   aMainLayout->addWidget(myName,     2, 2);
372   aMainLayout->addMultiCellWidget(myGrpTypeGroup, 3, 3, 0, 2);
373   aMainLayout->addMultiCellWidget(myWGStack,      4, 4, 0, 2);
374   aMainLayout->setRowStretch( 5, 5 );
375   aMainLayout->addMultiCellWidget(aColorBox,   6, 6, 0, 2);
376   aMainLayout->addMultiCellWidget(aButtons,       7, 7, 0, 2);
377
378   /* signals and slots connections */
379   connect(myMeshGroupBtn, SIGNAL(clicked()), this, SLOT(setCurrentSelection()));
380
381   connect(myGrpTypeGroup, SIGNAL(clicked(int)), this, SLOT(onGrpTypeChanged(int)));
382
383   connect(myTypeGroup, SIGNAL(clicked(int)), this, SLOT(onTypeChanged(int)));
384
385   connect(myName, SIGNAL(textChanged(const QString&)), this, SLOT(onNameChanged(const QString&)));
386   connect(myElements, SIGNAL(selectionChanged()), this, SLOT(onListSelectionChanged()));
387
388   connect(myFilter, SIGNAL(clicked()), this, SLOT(setFilters()));
389   connect(aAddBtn, SIGNAL(clicked()), this, SLOT(onAdd()));
390   connect(aRemoveBtn, SIGNAL(clicked()), this, SLOT(onRemove()));
391   connect(aSortBtn, SIGNAL(clicked()), this, SLOT(onSort()));
392
393   connect(mySelectSubMesh, SIGNAL(toggled(bool)), this, SLOT(onSelectSubMesh(bool)));
394   connect(mySelectGroup, SIGNAL(toggled(bool)), this, SLOT(onSelectGroup(bool)));
395   connect(mySubMeshBtn, SIGNAL(clicked()), this, SLOT(setCurrentSelection()));
396   connect(myGroupBtn, SIGNAL(clicked()), this, SLOT(setCurrentSelection()));
397   connect(myGeomGroupBtn, SIGNAL(toggled(bool)), this, SLOT(onGeomSelectionButton(bool)));
398   connect(myColorBtn, SIGNAL(clicked()), this, SLOT(onSelectColor()));
399
400   connect(aOKBtn, SIGNAL(clicked()), this, SLOT(onOK()));
401   connect(aApplyBtn, SIGNAL(clicked()), this, SLOT(onApply()));
402   connect(aCloseBtn, SIGNAL(clicked()), this, SLOT(onClose()));
403   connect(aHelpBtn, SIGNAL(clicked()), this, SLOT(onHelp()));
404
405   /* Init selection */
406   mySMESHGUI->SetActiveDialogBox(this);
407   mySMESHGUI->SetState(800);
408
409   mySelectionMode = -1;
410   myMeshFilter = new SMESH_TypeFilter(MESH);
411   mySubMeshFilter = new SMESH_TypeFilter(SUBMESH);
412   myGroupFilter = new SMESH_TypeFilter(GROUP);
413   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( mySMESHGUI->application()->activeStudy() );
414   myGeomFilter = new GEOM_SelectionFilter( aStudy, true );
415
416   connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(onDeactivate()));
417   connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(onClose()));
418   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(onObjectSelectionChanged()));
419
420   myGrpTypeGroup->setButton(myGrpTypeGroup->id(rb1)); // VSR !!!
421   onGrpTypeChanged(myGrpTypeGroup->id(rb1)); // VSR!!!
422
423   if (myMesh->_is_nil() )
424     myTypeGroup->setButton(0);
425
426   updateButtons();
427   //myName->setText(GetDefaultName(tr("SMESH_GROUP")));
428 }
429
430 //=================================================================================
431 // function : ~SMESHGUI_GroupDlg()
432 // purpose  : Destroys the object and frees any allocated resources
433 //=================================================================================
434 SMESHGUI_GroupDlg::~SMESHGUI_GroupDlg()
435 {
436   // no need to delete child widgets, Qt does it all for us
437   if ( myFilterDlg != 0 )
438   {
439     myFilterDlg->reparent( 0, QPoint() );
440     delete myFilterDlg;
441   }
442 }
443
444 //=================================================================================
445 // function : GetDefaultName()
446 // purpose  : Get the Group Name if Create new Group
447 //=================================================================================
448 QString SMESHGUI_GroupDlg::GetDefaultName(const QString& theOperation)
449 {
450     QString aName = "";
451
452     // collect all object names of SMESH component
453     SalomeApp_Study* appStudy =
454       dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
455     if ( !appStudy ) return aName;
456     _PTR(Study) aStudy = appStudy->studyDS();
457
458     std::set<std::string> aSet;
459     _PTR(SComponent) aMeshCompo (aStudy->FindComponent("SMESH"));
460     if (aMeshCompo) {
461       _PTR(ChildIterator) it (aStudy->NewChildIterator(aMeshCompo));
462       _PTR(SObject) obj;
463       for (it->InitEx(true); it->More(); it->Next()) {
464         obj = it->Value();
465         aSet.insert(obj->GetName());
466       }
467     }
468
469     // build a unique name
470     int aNumber = 0;
471     bool isUnique = false;
472     while (!isUnique) {
473       aName = theOperation + "_" + QString::number(++aNumber);
474       isUnique = (aSet.count(aName.latin1()) == 0);
475     }
476
477     return aName;
478 }
479
480 //=================================================================================
481 // function : Init()
482 // purpose  :
483 //=================================================================================
484 void SMESHGUI_GroupDlg::init (SMESH::SMESH_Mesh_ptr theMesh)
485 {
486   mySelectionMgr->installFilter(myMeshFilter);
487
488   /* init data from current selection */
489   restoreShowEntityMode();
490   myMesh = SMESH::SMESH_Mesh::_duplicate(theMesh);
491   setShowEntityMode();
492   myGroup = SMESH::SMESH_Group::_nil();
493   myGroupOnGeom = SMESH::SMESH_GroupOnGeom::_nil();
494
495   // NPAL19389: create a group with a selection in another group
496   // set actor of myMesh, if it is visible, else try
497   // any visible actor of group or submesh of myMesh
498   SetAppropriateActor();
499
500   setDefaultGroupColor();
501
502   SALOME_ListIO aList;
503   mySelectionMgr->selectedObjects( aList );
504   if ( !aList.IsEmpty() )
505   {
506     QString aName = aList.First()->getName();
507     myMeshGroupLine->setText(aName);
508     myMeshGroupLine->home( false );
509   }
510
511   myCurrentLineEdit = 0;
512
513   myTypeGroup->setButton(0);
514   onTypeChanged(0);
515 }
516
517 //=================================================================================
518 // function : Init()
519 // purpose  :
520 //=================================================================================
521 void SMESHGUI_GroupDlg::init (SMESH::SMESH_GroupBase_ptr theGroup)
522 {
523   restoreShowEntityMode();
524   myMesh = theGroup->GetMesh();
525   setShowEntityMode();
526
527   myNameChanged = true;
528   myName->blockSignals(true);
529   myName->setText(theGroup->GetName());
530   myName->blockSignals(false);
531   myName->home(false);
532
533   SALOMEDS::Color aColor = theGroup->GetColor();
534   setGroupColor( aColor );
535
536   myMeshGroupLine->setText(theGroup->GetName());
537
538   int aType = 0;
539   switch(theGroup->GetType()) {
540   case SMESH::NODE: aType= 0; break;
541   case SMESH::EDGE: aType = 1; break;
542   case SMESH::FACE: aType = 2; break;
543   case SMESH::VOLUME: aType = 3; break;
544   }
545   myTypeGroup->setButton(aType);
546
547   myGroup = SMESH::SMESH_Group::_narrow( theGroup );
548
549   if (!myGroup->_is_nil())
550   {
551     // NPAL19389: create a group with a selection in another group
552     // set actor of myMesh, if it is visible, else set
553     // actor of myGroup, if it is visible, else try
554     // any visible actor of group or submesh of myMesh
555     // commented, because an attempt to set selection on not displayed cells leads to error
556     //SetAppropriateActor();
557     myActor = SMESH::FindActorByObject(myMesh);
558     if ( !myActor )
559       myActor = SMESH::FindActorByObject(myGroup);
560     SMESH::SetPickable(myActor);
561
562     myGrpTypeGroup->setButton(0);
563     onGrpTypeChanged(0);
564
565     myCurrentLineEdit = 0;
566     myElements->clear();
567     setSelectionMode(aType);
568     myTypeId = aType;
569
570     setShowEntityMode(); // depends on myTypeId
571
572     myIdList.clear();
573     if (!myGroup->IsEmpty()) {
574       SMESH::long_array_var anElements = myGroup->GetListOfID();
575       int k = anElements->length();
576       for (int i = 0; i < k; i++) {
577         myIdList.append(anElements[i]);
578         myElements->insertItem(QString::number(anElements[i]));
579       }
580       myElements->selectAll(true);
581     }
582   }
583   else
584   {
585     myGroupOnGeom = SMESH::SMESH_GroupOnGeom::_narrow( theGroup );
586
587     if ( !myGroupOnGeom->_is_nil() )
588     {
589       // NPAL19389: create a group with a selection in another group
590       // set actor of myMesh, if it is visible, else set
591       // actor of myGroupOnGeom, if it is visible, else try
592       // any visible actor of group or submesh of myMesh
593       // commented, because an attempt to set selection on not displayed cells leads to error
594       //SetAppropriateActor();
595       myActor = SMESH::FindActorByObject(myMesh);
596       if ( !myActor )
597         myActor = SMESH::FindActorByObject(myGroupOnGeom);
598       SMESH::SetPickable(myActor);
599
600       myGrpTypeGroup->setButton(1);
601       onGrpTypeChanged(1);
602
603       QString aShapeName("");
604       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
605       GEOM::GEOM_Object_var aGroupShape = myGroupOnGeom->GetShape();
606       if (!aGroupShape->_is_nil())
607       {
608         _PTR(SObject) aGroupShapeSO = aStudy->FindObjectID(aGroupShape->GetStudyEntry());
609         aShapeName = aGroupShapeSO->GetName().c_str();
610       }
611       myGeomGroupLine->setText( aShapeName );
612       myNameChanged = true;
613       myName->blockSignals(true);
614       myName->setText(theGroup->GetName());
615       myName->blockSignals(false);
616     }
617   }
618 }
619
620 //=================================================================================
621 // function : updateButtons()
622 // purpose  :
623 //=================================================================================
624 void SMESHGUI_GroupDlg::updateButtons()
625 {
626   bool enable = !myName->text().stripWhiteSpace().isEmpty();
627
628   if (myGrpTypeId == 0) {
629     enable = enable && myElements->count() > 0;
630     enable = enable && (!myGroup->_is_nil() || !myMesh->_is_nil());
631   }
632   else if (myGrpTypeId == 1) {
633     if (CORBA::is_nil(myGroupOnGeom)) { // creation mode
634       enable = enable && myGeomObjects->length() > 0 && !myMesh->_is_nil();
635     }
636   }
637
638   QPushButton* aBtn;
639   aBtn = (QPushButton*) child("ok", "QPushButton");
640   if (aBtn) aBtn->setEnabled(enable);
641   aBtn = (QPushButton*) child("apply", "QPushButton");
642   if (aBtn) aBtn->setEnabled(enable);
643 }
644
645 //=================================================================================
646 // function : onNameChanged()
647 // purpose  :
648 //=================================================================================
649 void SMESHGUI_GroupDlg::onNameChanged (const QString& text)
650 {
651   myOldName = myName->text();
652   updateButtons();
653   QString tmp = myName->text();
654   tmp.stripWhiteSpace();
655   if( !tmp.isEmpty() )
656     myNameChanged = true;
657   else
658     myNameChanged = false;
659 }
660
661 //=================================================================================
662 // function : onTypeChanged()
663 // purpose  : Group elements type radio button management
664 //=================================================================================
665 void SMESHGUI_GroupDlg::onTypeChanged (int id)
666 {
667   if (myTypeId != id) {
668     myElements->clear();
669     if (myCurrentLineEdit == 0)
670       setSelectionMode(id);
671     myTypeId = id;
672     setShowEntityMode();
673   }
674 }
675
676 //=================================================================================
677 // function : onGrpTypeChanged()
678 // purpose  : Group type radio button management
679 //=================================================================================
680 void SMESHGUI_GroupDlg::onGrpTypeChanged (int id)
681 {
682   if (myGrpTypeId != id) {
683     myWGStack->raiseWidget( id );
684     myName->blockSignals(true);
685     myName->setText(myOldName);
686     myName->blockSignals(false);
687     onSelectGeomGroup(id == 1);
688   }
689   myGrpTypeId = id;
690 }
691
692 //=================================================================================
693 // function : onSelectColor()
694 // purpose  :
695 //=================================================================================
696 void SMESHGUI_GroupDlg::onSelectColor()
697 {
698   QColor color = getGroupQColor();
699   color = QColorDialog::getColor( color );
700   setGroupQColor( color );
701
702   updateButtons();
703 }
704
705 //=================================================================================
706 // function : setSelectionMode()
707 // purpose  : Radio button management
708 //=================================================================================
709 void SMESHGUI_GroupDlg::setSelectionMode (int theMode)
710 {
711   // PAL7314
712   if (myMesh->_is_nil())
713     return;
714   if (mySelectionMode != theMode) {
715     // [PAL10408] mySelectionMgr->clearSelected();
716     mySelectionMgr->clearFilters();
717     if (myActor)
718       myActor->SetPointRepresentation(false);
719     else
720       SMESH::SetPointRepresentation(false);
721     if (theMode < 4) {
722       switch (theMode) {
723       case 0:
724         if (myActor)
725           myActor->SetPointRepresentation(true);
726         else
727           SMESH::SetPointRepresentation(true);
728         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
729           aViewWindow->SetSelectionMode(NodeSelection);
730         break;
731       case 1:
732         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
733           aViewWindow->SetSelectionMode(EdgeSelection);
734         break;
735       case 2:
736         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
737           aViewWindow->SetSelectionMode(FaceSelection);
738         break;
739       default:
740         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
741           aViewWindow->SetSelectionMode(VolumeSelection);
742       }
743     } else {
744       if (theMode == 4)
745         mySelectionMgr->installFilter(mySubMeshFilter);
746       else if (theMode == 5)
747         mySelectionMgr->installFilter(myGroupFilter);
748       else if (theMode == 6)
749         mySelectionMgr->installFilter(myMeshFilter);
750       else if (theMode == 7)
751         mySelectionMgr->installFilter(myGeomFilter);
752
753       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
754         aViewWindow->SetSelectionMode(ActorSelection);
755     }
756     mySelectionMode = theMode;
757   }
758 }
759
760 //=================================================================================
761 // function : onApply()
762 // purpose  :
763 //=================================================================================
764 bool SMESHGUI_GroupDlg::onApply()
765 {
766   if (mySMESHGUI->isActiveStudyLocked())
767     return false;
768
769   if (myName->text().stripWhiteSpace().isEmpty())
770     return false;
771
772   if (myGrpTypeId == 0) { // on mesh elements
773     if (!myElements->count())
774       return false;
775
776     mySelectionMgr->clearSelected();
777
778     if (myGroup->_is_nil()) { // creation
779       if (myMesh->_is_nil())
780         return false;
781
782       SMESH::ElementType aType = SMESH::ALL;
783       switch (myTypeId) {
784       case 0: aType = SMESH::NODE; break;
785       case 1: aType = SMESH::EDGE; break;
786       case 2: aType = SMESH::FACE; break;
787       case 3: aType = SMESH::VOLUME; break;
788       }
789
790       SMESH::long_array_var anIdList = new SMESH::long_array;
791       int i, k = myElements->count();
792       anIdList->length(k);
793       QListBoxItem* anItem;
794       for (i = 0, anItem = myElements->firstItem(); anItem != 0; i++, anItem = anItem->next()) {
795         anIdList[i] = anItem->text().toInt();
796       }
797
798       myGroup = SMESH::AddGroup(myMesh, aType, myName->text());
799       myGroup->Add(anIdList.inout());
800
801       SALOMEDS::Color aColor = getGroupColor();
802       myGroup->SetColor(aColor);
803
804       _PTR(SObject) aMeshGroupSO = SMESH::FindSObject(myGroup);
805
806       //SMESH::setFileName ( aMeshGroupSO, QString::number(myColorSpinBox->value()) );
807       SMESH::setFileType ( aMeshGroupSO, "COULEURGROUP" );
808
809       /* init for next operation */
810       myName->setText("");
811       myElements->clear();
812       myGroup = SMESH::SMESH_Group::_nil();
813
814     } else { // edition
815       myGroup->SetName(myName->text());
816
817       SALOMEDS::Color aColor = getGroupColor();
818       myGroup->SetColor(aColor);
819
820       _PTR(SObject) aMeshGroupSO = SMESH::FindSObject(myGroup);
821
822       if (SMESH_Actor *anActor = SMESH::FindActorByEntry(aMeshGroupSO->GetID().c_str())) {
823         switch ( myTypeId ) {
824         case 0: anActor->SetNodeColor( aColor.R, aColor.G, aColor.B ); break;
825         case 1: anActor->SetEdgeColor( aColor.R, aColor.G, aColor.B ); break;
826         case 2:
827         case 3: anActor->SetSufaceColor( aColor.R, aColor.G, aColor.B ); break;
828         }
829       }
830
831       QValueList<int> aAddList;
832       QValueList<int>::iterator anIt;
833       QListBoxItem* anItem;
834
835       for (anItem = myElements->firstItem(); anItem != 0; anItem = anItem->next()) {
836         int anId = anItem->text().toInt();
837         if ((anIt = myIdList.find(anId)) == myIdList.end())
838           aAddList.append(anId);
839         else
840           myIdList.remove(anIt);
841       }
842       if (!aAddList.empty()) {
843         SMESH::long_array_var anIdList = new SMESH::long_array;
844         anIdList->length(aAddList.count());
845         int i;
846         for (i = 0, anIt = aAddList.begin(); anIt != aAddList.end(); anIt++, i++)
847           anIdList[i] = *anIt;
848         myGroup->Add(anIdList.inout());
849       }
850       if (!myIdList.empty()) {
851         SMESH::long_array_var anIdList = new SMESH::long_array;
852         anIdList->length(myIdList.count());
853         int i;
854         for (i = 0, anIt = myIdList.begin(); anIt != myIdList.end(); anIt++, i++)
855           anIdList[i] = *anIt;
856         myGroup->Remove(anIdList.inout());
857       }
858       /* init for next operation */
859       myIdList.clear();
860       for (anItem = myElements->firstItem(); anItem != 0; anItem = anItem->next())
861         myIdList.append(anItem->text().toInt());
862     }
863
864     mySMESHGUI->updateObjBrowser(true);
865     SMESH::UpdateView(); // asv: fix of BUG PAL5515
866     mySelectionMgr->clearSelected();
867     return true;
868   }
869   else if (myGrpTypeId == 1) { // on geom object
870     if (CORBA::is_nil(myGroupOnGeom)) { // creation
871       if (myMesh->_is_nil() || !myGeomObjects->length())
872         return false;
873
874       SMESH::ElementType aType = SMESH::ALL;
875       switch (myTypeId) {
876       case 0: aType = SMESH::NODE; break;
877       case 1: aType = SMESH::EDGE; break;
878       case 2: aType = SMESH::FACE; break;
879       case 3: aType = SMESH::VOLUME; break;
880       }
881
882       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
883       GEOM::GEOM_IGroupOperations_var aGroupOp =
884         SMESH::GetGEOMGen()->GetIGroupOperations(aStudy->StudyId());
885
886       if (myGeomObjects->length() == 1) {
887         myGroupOnGeom = myMesh->CreateGroupFromGEOM(aType, myName->text(),myGeomObjects[0]);
888       }
889       else {
890         SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
891         if ( aSMESHGen->_is_nil() )
892           return false;
893
894         // create a geometry group
895         GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
896         _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
897
898         if (geomGen->_is_nil() || !aStudy)
899           return false;
900
901         GEOM::GEOM_IGroupOperations_var op =
902           geomGen->GetIGroupOperations(aStudy->StudyId());
903         if (op->_is_nil())
904           return false;
905
906         // check and add all selected GEOM objects: they must be
907         // a sub-shapes of the main GEOM and must be of one type
908         TopAbs_ShapeEnum aGroupType = TopAbs_SHAPE;
909         for ( int i =0; i < myGeomObjects->length(); i++) {
910           TopAbs_ShapeEnum aSubShapeType = (TopAbs_ShapeEnum)myGeomObjects[i]->GetShapeType();
911           if (i == 0)
912             aGroupType = aSubShapeType;
913           else if (aSubShapeType != aGroupType) {
914             aGroupType = TopAbs_SHAPE;
915             break;
916           }
917         }
918
919         GEOM::GEOM_Object_var aMeshShape = myMesh->GetShapeToMesh();
920         GEOM::GEOM_Object_var aGroupVar = op->CreateGroup(aMeshShape, aGroupType);
921         op->UnionList(aGroupVar, myGeomObjects);
922
923         if (op->IsDone()) {
924           // publish the GEOM group in study
925           QString aNewGeomGroupName ("Auto_group_for_");
926           aNewGeomGroupName += myName->text();
927           SALOMEDS::SObject_var aNewGroupSO =
928             geomGen->AddInStudy(aSMESHGen->GetCurrentStudy(), aGroupVar, aNewGeomGroupName, aMeshShape);
929         }
930
931         myGroupOnGeom = myMesh->CreateGroupFromGEOM(aType, myName->text(), aGroupVar);
932       }
933
934       SALOMEDS::Color aColor = getGroupColor();
935       myGroupOnGeom->SetColor(aColor);
936
937       _PTR(SObject) aMeshGroupSO = SMESH::FindSObject(myGroupOnGeom);
938
939       //SMESH::setFileName ( aMeshGroupSO, QString::number(myColorSpinBox->value()) );
940       SMESH::setFileType ( aMeshGroupSO,"COULEURGROUP" );
941
942       /* init for next operation */
943       myName->setText("");
944       myGroupOnGeom = SMESH::SMESH_GroupOnGeom::_nil();
945     }
946     else { // edition
947       myGroupOnGeom->SetName(myName->text());
948
949       SALOMEDS::Color aColor = getGroupColor();
950       myGroupOnGeom->SetColor(aColor);
951
952       _PTR(SObject) aMeshGroupSO = SMESH::FindSObject(myGroupOnGeom);
953       if (SMESH_Actor *anActor = SMESH::FindActorByEntry(aMeshGroupSO->GetID().c_str())) {
954         switch ( myTypeId ) {
955         case 0: anActor->SetNodeColor( aColor.R, aColor.G, aColor.B ); break;
956         case 1: anActor->SetEdgeColor( aColor.R, aColor.G, aColor.B ); break;
957         case 2:
958         case 3: anActor->SetSufaceColor( aColor.R, aColor.G, aColor.B ); break;
959         }
960       }
961     }
962
963     mySMESHGUI->updateObjBrowser(true);
964     mySelectionMgr->clearSelected();
965     return true;
966   }
967
968   return false;
969 }
970
971 //=================================================================================
972 // function : onOK()
973 // purpose  :
974 //=================================================================================
975 void SMESHGUI_GroupDlg::onOK()
976 {
977   if ( onApply() )
978     onClose();
979 }
980
981 //=================================================================================
982 // function : onListSelectionChanged()
983 // purpose  : Called when selection in element list is changed
984 //=================================================================================
985 void SMESHGUI_GroupDlg::onListSelectionChanged()
986 {
987   //  MESSAGE("SMESHGUI_GroupDlg::onListSelectionChanged(); myActor = " << myActor);
988   if ( myIsBusy || !myActor) return;
989     myIsBusy = true;
990
991   if (myCurrentLineEdit == 0) {
992     mySelectionMgr->clearSelected();
993     TColStd_MapOfInteger aIndexes;
994     QListBoxItem* anItem;
995     for (anItem = myElements->firstItem(); anItem != 0; anItem = anItem->next()) {
996       if (anItem->isSelected()) {
997         int anId = anItem->text().toInt();
998         aIndexes.Add(anId);
999       }
1000     }
1001     mySelector->AddOrRemoveIndex(myActor->getIO(), aIndexes, false);
1002     SALOME_ListIO aList;
1003     aList.Append(myActor->getIO());
1004     mySelectionMgr->setSelectedObjects(aList,false);
1005   }
1006   myIsBusy = false;
1007 }
1008
1009 //=================================================================================
1010 // function : onObjectSelectionChanged()
1011 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
1012 //=================================================================================
1013 void SMESHGUI_GroupDlg::onObjectSelectionChanged()
1014 {
1015   if (myIsBusy || !isEnabled()) return;
1016   if (myCurrentLineEdit == myGeomGroupLine && !myGeomGroupBtn->isOn()) return;
1017
1018   myIsBusy = true;
1019
1020   SALOME_ListIO aList;
1021   mySelectionMgr->selectedObjects( aList );
1022
1023   int aNbSel = aList.Extent();
1024   myElements->clearSelection();
1025
1026   if (myCurrentLineEdit)
1027   {
1028     myCurrentLineEdit->setText("");
1029     QString aString = "";
1030
1031     if (myCurrentLineEdit == myMeshGroupLine)
1032     {
1033       mySelectSubMesh->setEnabled(false);
1034       mySelectGroup->setEnabled(false);
1035       myGroupLine->setText("");
1036       mySubMeshLine->setText("");
1037
1038       myGeomGroupBtn->setEnabled(false);
1039       myGeomGroupLine->setEnabled(false);
1040       myGeomGroupLine->setText("");
1041       myGeomObjects = new GEOM::ListOfGO();
1042       myGeomObjects->length(0);
1043
1044       if (myGeomGroupBtn->isOn())
1045         myGeomGroupBtn->setOn(false);
1046       if (!myCreate)
1047         myName->setText("");
1048
1049       myElements->clear();
1050
1051       if (aNbSel != 1 ) {
1052         myGroup = SMESH::SMESH_Group::_nil();
1053         myGroupOnGeom = SMESH::SMESH_GroupOnGeom::_nil();
1054         restoreShowEntityMode();
1055         myMesh = SMESH::SMESH_Mesh::_nil();
1056         updateGeomPopup();
1057         updateButtons();
1058         myIsBusy = false;
1059         return;
1060       }
1061       Handle(SALOME_InteractiveObject) IO = aList.First();
1062
1063       if (myCreate) {
1064         restoreShowEntityMode();
1065         myMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(IO);
1066         setShowEntityMode();
1067         updateGeomPopup();
1068         if (myMesh->_is_nil())
1069         {
1070           updateButtons();
1071           myIsBusy = false;
1072           return;
1073         }
1074         myGroup = SMESH::SMESH_Group::_nil();
1075
1076         // NPAL19389: create a group with a selection in another group
1077         // set actor of myMesh, if it is visible, else try
1078         // any visible actor of group or submesh of myMesh
1079         SetAppropriateActor();
1080
1081         aString = aList.First()->getName();
1082         myMeshGroupLine->setText(aString);
1083         myMeshGroupLine->home( false );
1084
1085         mySelectSubMesh->setEnabled(true);
1086         mySelectGroup->setEnabled(true);
1087         myGeomGroupBtn->setEnabled(true);
1088         myGeomGroupLine->setEnabled(true);
1089         updateButtons();
1090       } else {
1091         SMESH::SMESH_GroupBase_var aGroup = SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(IO);
1092         if (aGroup->_is_nil())
1093         {
1094           myIsBusy = false;
1095           return;
1096         }
1097         myIsBusy = false;
1098         myCurrentLineEdit = 0;
1099
1100         myGroup = SMESH::SMESH_Group::_nil();
1101         myGroupOnGeom = SMESH::SMESH_GroupOnGeom::_nil();
1102
1103         init(aGroup);
1104         myIsBusy = true;
1105         mySelectSubMesh->setEnabled(true);
1106         mySelectGroup->setEnabled(true);
1107       }
1108       myCurrentLineEdit = 0;
1109       myIsBusy = false;
1110       if (!myCreate)
1111         return;
1112
1113       if (myGrpTypeId == 0)
1114       {
1115         if (myTypeId == -1)
1116           onTypeChanged(0);
1117         else
1118         {
1119           myElements->clear();
1120           setSelectionMode(myTypeId);
1121         }
1122       }
1123
1124       myIsBusy = false;
1125       return;
1126     }
1127     else if (myCurrentLineEdit == myGeomGroupLine)
1128     {
1129       myGeomObjects = new GEOM::ListOfGO();
1130
1131       // The mesh SObject
1132       _PTR(SObject) aMeshSO = SMESH::FindSObject(myMesh);
1133
1134       if (aNbSel == 0 || !aMeshSO)
1135       {
1136         myGeomObjects->length(0);
1137         updateButtons();
1138         myIsBusy = false;
1139         return;
1140       }
1141
1142       myGeomObjects->length(aNbSel);
1143
1144       GEOM::GEOM_Object_var aGeomGroup;
1145       Standard_Boolean testResult;
1146       int i = 0;
1147
1148       SALOME_ListIteratorOfListIO anIt (aList);
1149       for (; anIt.More(); anIt.Next())
1150       {
1151         testResult = Standard_False;
1152         aGeomGroup = GEOMBase::ConvertIOinGEOMObject(anIt.Value(), testResult);
1153
1154         // Check if the object is a geometry group
1155         if (!testResult || CORBA::is_nil(aGeomGroup))
1156           continue;
1157
1158         // Check if group constructed on the same shape as a mesh or on its child
1159         _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1160         GEOM::GEOM_IGroupOperations_var anOp =
1161           SMESH::GetGEOMGen()->GetIGroupOperations(aStudy->StudyId());
1162
1163         // The main shape of the group
1164         GEOM::GEOM_Object_var aGroupMainShape;
1165         if (aGeomGroup->GetType() == 37)
1166           aGroupMainShape = anOp->GetMainShape(aGeomGroup);
1167         else
1168           aGroupMainShape = GEOM::GEOM_Object::_duplicate(aGeomGroup);
1169         _PTR(SObject) aGroupMainShapeSO =
1170           //aStudy->FindObjectIOR(aStudy->ConvertObjectToIOR(aGroupMainShape));
1171           aStudy->FindObjectID(aGroupMainShape->GetStudyEntry());
1172
1173         _PTR(SObject) anObj, aRef;
1174         bool isRefOrSubShape = false;
1175         if (aMeshSO->FindSubObject(1, anObj) &&  anObj->ReferencedObject(aRef)) {
1176           //if (strcmp(aRef->GetID(), aGroupMainShapeSO->GetID()) == 0) {
1177           if (aRef->GetID() == aGroupMainShapeSO->GetID()) {
1178             isRefOrSubShape = true;
1179           } else {
1180             _PTR(SObject) aFather = aGroupMainShapeSO->GetFather();
1181             _PTR(SComponent) aComponent = aGroupMainShapeSO->GetFatherComponent();
1182             //while (!isRefOrSubShape && strcmp(aFather->GetID(), aComponent->GetID()) != 0) {
1183             while (!isRefOrSubShape && aFather->GetID() != aComponent->GetID()) {
1184               //if (strcmp(aRef->GetID(), aFather->GetID()) == 0)
1185               if (aRef->GetID() == aFather->GetID())
1186                 isRefOrSubShape = true;
1187               else
1188                 aFather = aFather->GetFather();
1189             }
1190           }
1191         }
1192         if (isRefOrSubShape)
1193           myGeomObjects[i++] = aGeomGroup;
1194       }
1195
1196       myGeomObjects->length(i);
1197       if ( i == 0 )
1198         {
1199           myIsBusy = false;
1200           return;
1201         }
1202
1203       aNbSel = i;
1204     }
1205
1206     if (aNbSel >= 1) {
1207       if (aNbSel > 1) {
1208         if (myCurrentLineEdit == mySubMeshLine)
1209           aString = tr("SMESH_SUBMESH_SELECTED").arg(aNbSel);
1210         else if (myCurrentLineEdit == myGroupLine)
1211           aString = tr("SMESH_GROUP_SELECTED").arg(aNbSel);
1212         else if (myCurrentLineEdit == myGeomGroupLine)
1213           aString = tr("%1 Objects").arg(aNbSel);
1214       }
1215       else {
1216         aString = aList.First()->getName();
1217       }
1218     }
1219
1220     myCurrentLineEdit->setText(aString);
1221     myCurrentLineEdit->home(false);
1222     // 07.06.2008 skl for IPAL19574:
1223     // change name of group only if it is empty
1224     QString tmp = myName->text();
1225     tmp.stripWhiteSpace();
1226     if( tmp.isEmpty() || !myNameChanged ) {
1227       myOldName = myName->text();
1228       myName->blockSignals(true);
1229       myName->setText(aString);
1230       myName->blockSignals(false);
1231     }
1232
1233     updateButtons();
1234   }
1235   else // !myCurrentLineEdit: local selection of nodes or elements
1236   {
1237     if (aNbSel == 1 && myActor && myActor->hasIO())
1238     {
1239 #ifdef ENABLE_SWITCH_ACTOR_DURING_ELEMENTS_SELECTION
1240       // NPAL19389: create a group with a selection in another group
1241       // Switch myActor to the newly selected one, if the last
1242       // is visible and belongs to group or submesh of myMesh
1243       Handle(SALOME_InteractiveObject) curIO = myActor->getIO();
1244       Handle(SALOME_InteractiveObject) selIO = aList.First();
1245       if (curIO->hasEntry() && selIO->hasEntry()) {
1246         const char* selEntry = selIO->getEntry();
1247         if (strcmp(curIO->getEntry(), selEntry) != 0) {
1248           // different objects: selected and myActor
1249           SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView();
1250           if (aViewWindow && aViewWindow->isVisible(selIO)) {
1251             // newly selected actor is visible
1252
1253             // mesh entry
1254             _PTR(SObject) aSObject = SMESH::FindSObject(myMesh);
1255             if (aSObject) {
1256               CORBA::String_var meshEntry = aSObject->GetID().c_str();
1257               int len = strlen(meshEntry);
1258
1259               if (strncmp(selEntry, meshEntry, len) == 0) {
1260                 // selected object is myMesh or a part of it
1261                 SMESH_Actor* anActor = SMESH::FindActorByEntry(selEntry);
1262                 if (anActor) {
1263                   myActor = anActor;
1264                   SMESH::SetPickable(myActor);
1265                 }
1266               }
1267             }
1268           }
1269         }
1270       }
1271       // NPAL19389 END
1272 #endif // ENABLE_SWITCH_ACTOR_DURING_ELEMENTS_SELECTION
1273
1274       QString aListStr = "";
1275       int aNbItems = 0;
1276       if (myTypeId == 0) {
1277         aNbItems = SMESH::GetNameOfSelectedNodes(mySelector, myActor->getIO(), aListStr);
1278       } else {
1279         aNbItems = SMESH::GetNameOfSelectedElements(mySelector, myActor->getIO(), aListStr);
1280       }
1281       if (aNbItems > 0) {
1282         QStringList anElements = QStringList::split(" ", aListStr);
1283         QListBoxItem* anItem = 0;
1284         for (QStringList::iterator it = anElements.begin(); it != anElements.end(); ++it) {
1285           anItem = myElements->findItem(*it, Qt::ExactMatch);
1286           if (anItem) myElements->setSelected(anItem, true);
1287         }
1288       }
1289     }
1290   }
1291
1292   if (!myActor) {
1293     if (!myGroup->_is_nil())
1294       myActor = SMESH::FindActorByObject(myGroup);
1295     else if (!myGroupOnGeom->_is_nil())
1296       myActor = SMESH::FindActorByObject(myGroupOnGeom);
1297     else
1298       myActor = SMESH::FindActorByObject(myMesh);
1299   }
1300
1301   // somehow, if we display the mesh, while selecting from another actor,
1302   // the mesh becomes pickable, and there is no way to select any element
1303   if (myActor)
1304     SMESH::SetPickable(myActor);
1305
1306   myIsBusy = false;
1307 }
1308
1309 //=================================================================================
1310 // function : onSelectSubMesh()
1311 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
1312 //=================================================================================
1313 void SMESHGUI_GroupDlg::onSelectSubMesh(bool on)
1314 {
1315   if (on) {
1316     if (mySelectGroup->isChecked()) {
1317       mySelectGroup->setChecked(false);
1318     }
1319     //VSR: else if (mySelectGeomGroup->isChecked()) {
1320     //VSR:   mySelectGeomGroup->setChecked(false);
1321     //VSR: }
1322     myCurrentLineEdit = mySubMeshLine;
1323     setSelectionMode(4);
1324   }
1325   else {
1326     mySubMeshLine->setText("");
1327     myCurrentLineEdit = 0;
1328     if (myTypeId != -1)
1329       setSelectionMode(myTypeId);
1330   }
1331   mySubMeshBtn->setEnabled(on);
1332   mySubMeshLine->setEnabled(on);
1333 }
1334
1335
1336 //=================================================================================
1337 // function : (onSelectGroup)
1338 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
1339 //=================================================================================
1340 void SMESHGUI_GroupDlg::onSelectGroup(bool on)
1341 {
1342   if (on) {
1343     if (mySelectSubMesh->isChecked()) {
1344       mySelectSubMesh->setChecked(false);
1345     }
1346     myCurrentLineEdit = myGroupLine;
1347     setSelectionMode(5);
1348   }
1349   else {
1350     myGroupLine->setText("");
1351     myCurrentLineEdit = 0;
1352     if (myTypeId != -1)
1353       setSelectionMode(myTypeId);
1354   }
1355   myGroupBtn->setEnabled(on);
1356   myGroupLine->setEnabled(on);
1357 }
1358
1359
1360 //=================================================================================
1361 // function : (onSelectGeomGroup)
1362 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
1363 //=================================================================================
1364 void SMESHGUI_GroupDlg::onSelectGeomGroup(bool on)
1365 {
1366   if (on) {
1367     if (mySelectSubMesh->isChecked()) {
1368       mySelectSubMesh->setChecked(false);
1369     }
1370     else if (mySelectGroup->isChecked()) {
1371       mySelectGroup->setChecked(false);
1372     }
1373     myCurrentLineEdit = myGeomGroupLine;
1374     updateGeomPopup();
1375     setSelectionMode(8);
1376   }
1377   else {
1378     myGeomGroupBtn->setOn(false);
1379     myGeomObjects->length(0);
1380     myGeomGroupLine->setText("");
1381     myCurrentLineEdit = 0;
1382     if (myTypeId != -1)
1383       setSelectionMode(myTypeId);
1384   }
1385 }
1386
1387
1388 //=================================================================================
1389 // function : setCurrentSelection()
1390 // purpose  :
1391 //=================================================================================
1392 void SMESHGUI_GroupDlg::setCurrentSelection()
1393 {
1394   QPushButton* send = (QPushButton*)sender();
1395   myCurrentLineEdit = 0;
1396   if (send == myMeshGroupBtn) {
1397     myCurrentLineEdit = myMeshGroupLine;
1398     if (myCreate)
1399       setSelectionMode(6);
1400     else
1401       setSelectionMode(5);
1402     onObjectSelectionChanged();
1403   }
1404   else if (send == mySubMeshBtn) {
1405     myCurrentLineEdit = mySubMeshLine;
1406     onObjectSelectionChanged();
1407   }
1408   else if (send == myGroupBtn) {
1409     myCurrentLineEdit = myGroupLine;
1410     onObjectSelectionChanged();
1411   }
1412 }
1413
1414
1415 //=================================================================================
1416 // function : setFilters()
1417 // purpose  : SLOT. Called when "Filter" button pressed.
1418 //=================================================================================
1419 void SMESHGUI_GroupDlg::setFilters()
1420 {
1421   SMESH::ElementType aType = SMESH::ALL;
1422   switch ( myTypeId )
1423   {
1424     case 0 : aType = SMESH::NODE; break;
1425     case 1 : aType = SMESH::EDGE; break;
1426     case 2 : aType = SMESH::FACE; break;
1427     case 3 : aType = SMESH::VOLUME; break;
1428     default: return;
1429   }
1430
1431   if ( myFilterDlg == 0 )
1432   {
1433     myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, aType );
1434     connect( myFilterDlg, SIGNAL( Accepted() ), SLOT( onFilterAccepted() ) );
1435   }
1436   else
1437     myFilterDlg->Init( aType );
1438
1439   myFilterDlg->SetSelection();
1440   myFilterDlg->SetMesh( myMesh );
1441   myFilterDlg->SetSourceWg( myElements );
1442
1443   myFilterDlg->show();
1444 }
1445
1446 //=================================================================================
1447 // function : onFilterAccepted()
1448 // purpose  : SLOT. Called when Filter dlg closed with OK button.
1449 //            Uncheck "Select submesh" and "Select group" checkboxes
1450 //=================================================================================
1451 void SMESHGUI_GroupDlg::onFilterAccepted()
1452 {
1453   if ( mySelectSubMesh->isChecked() || mySelectGroup->isChecked() )
1454   {
1455     mySelectionMode = myTypeId;
1456     mySelectSubMesh->setChecked( false );
1457     mySelectGroup->setChecked( false );
1458   }
1459 }
1460
1461 //=================================================================================
1462 // function : onAdd()
1463 // purpose  :
1464 //=================================================================================
1465 void SMESHGUI_GroupDlg::onAdd()
1466 {
1467   SALOME_ListIO aList;
1468   mySelectionMgr->selectedObjects( aList );
1469
1470   int aNbSel = aList.Extent();
1471
1472   if (aNbSel == 0 || !myActor || myMesh->_is_nil()) return;
1473
1474   myIsBusy = true;
1475
1476   SMESH::ElementType aType = SMESH::ALL;
1477   switch(myTypeId) {
1478   case 0:
1479     aType = SMESH::NODE;
1480     mySelector->SetSelectionMode(NodeSelection);
1481     break;
1482   case 1:
1483     aType = SMESH::EDGE;
1484     mySelector->SetSelectionMode(EdgeSelection);
1485     break;
1486   case 2:
1487     aType = SMESH::FACE;
1488     mySelector->SetSelectionMode(FaceSelection);
1489     break;
1490   case 3:
1491     aType = SMESH::VOLUME;
1492     mySelector->SetSelectionMode(VolumeSelection);
1493     break;
1494   default:
1495     mySelector->SetSelectionMode(ActorSelection);
1496   }
1497
1498   if (myCurrentLineEdit == 0) {
1499     //if (aNbSel != 1) { myIsBusy = false; return; }
1500     QString aListStr = "";
1501     int aNbItems = 0;
1502     if (myTypeId == 0) {
1503       aNbItems = SMESH::GetNameOfSelectedNodes(mySelector, myActor->getIO(), aListStr);
1504     }
1505     else {
1506       aNbItems = SMESH::GetNameOfSelectedElements(mySelector, myActor->getIO(), aListStr);
1507     }
1508     if (aNbItems > 0) {
1509       QStringList anElements = QStringList::split(" ", aListStr);
1510       QListBoxItem* anItem = 0;
1511       for (QStringList::iterator it = anElements.begin(); it != anElements.end(); ++it) {
1512         anItem = myElements->findItem(*it, Qt::ExactMatch);
1513         if (!anItem) {
1514           anItem = new QListBoxText(*it);
1515           myElements->insertItem(anItem);
1516         }
1517         myElements->setSelected(anItem, true);
1518       }
1519     }
1520   } else if (myCurrentLineEdit == mySubMeshLine) {
1521     //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1522
1523     SALOME_ListIO aList;
1524     mySelectionMgr->selectedObjects( aList );
1525
1526     SALOME_ListIteratorOfListIO anIt (aList);
1527     for (; anIt.More(); anIt.Next()) {
1528       SMESH::SMESH_subMesh_var aSubMesh =
1529         SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIt.Value());
1530       if (!aSubMesh->_is_nil()) {
1531         // check if mesh is the same
1532         if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
1533           try {
1534             SMESH::long_array_var anElements = aSubMesh->GetElementsByType(aType);
1535             int k = anElements->length();
1536             QListBoxItem* anItem = 0;
1537             for (int i = 0; i < k; i++) {
1538               QString aText = QString::number(anElements[i]);
1539               anItem = myElements->findItem(aText, Qt::ExactMatch);
1540               if (!anItem) {
1541                 anItem = new QListBoxText(aText);
1542                 myElements->insertItem(anItem);
1543               }
1544               myElements->setSelected(anItem, true);
1545             }
1546           }
1547           catch (const SALOME::SALOME_Exception& ex) {
1548             SalomeApp_Tools::QtCatchCorbaException(ex);
1549           }
1550         }
1551       }
1552     }
1553     mySelectSubMesh->setChecked(false);
1554     myIsBusy = false;
1555     onListSelectionChanged();
1556
1557   } else if (myCurrentLineEdit == myGroupLine) {
1558     //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1559     SALOME_ListIO aList;
1560     mySelectionMgr->selectedObjects( aList );
1561
1562     SALOME_ListIteratorOfListIO anIt (aList);
1563     for (; anIt.More(); anIt.Next()) {
1564       SMESH::SMESH_Group_var aGroup =
1565         SMESH::IObjectToInterface<SMESH::SMESH_Group>(anIt.Value());
1566       if (!aGroup->_is_nil()) {
1567         // check if mesh is the same
1568         if (aGroup->GetType() == aType && aGroup->GetMesh()->GetId() == myMesh->GetId()) {
1569           SMESH::long_array_var anElements = aGroup->GetListOfID();
1570           int k = anElements->length();
1571           QListBoxItem* anItem = 0;
1572           for (int i = 0; i < k; i++) {
1573             QString aText = QString::number(anElements[i]);
1574             anItem = myElements->findItem(aText, Qt::ExactMatch);
1575             if (!anItem) {
1576               anItem = new QListBoxText(aText);
1577               myElements->insertItem(anItem);
1578             }
1579             myElements->setSelected(anItem, true);
1580           }
1581         }
1582       }
1583     }
1584     mySelectGroup->setChecked(false);
1585     myIsBusy = false;
1586     onListSelectionChanged();
1587
1588   } else if (myCurrentLineEdit == myGeomGroupLine && myGeomObjects->length() == 1) {
1589     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1590     GEOM::GEOM_IGroupOperations_var aGroupOp =
1591       SMESH::GetGEOMGen()->GetIGroupOperations(aStudy->StudyId());
1592
1593     SMESH::ElementType aGroupType = SMESH::ALL;
1594     switch(aGroupOp->GetType(myGeomObjects[0])) {
1595     case 7: aGroupType = SMESH::NODE; break;
1596     case 6: aGroupType = SMESH::EDGE; break;
1597     case 4: aGroupType = SMESH::FACE; break;
1598     case 2: aGroupType = SMESH::VOLUME; break;
1599     default: myIsBusy = false; return;
1600     }
1601
1602     if (aGroupType == aType) {
1603       _PTR(SObject) aGroupSO =
1604         //aStudy->FindObjectIOR(aStudy->ConvertObjectToIOR(myGeomGroup));
1605         aStudy->FindObjectID(myGeomObjects[0]->GetStudyEntry());
1606       // Construct filter
1607       SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
1608       SMESH::Filter_var aFilter = aFilterMgr->CreateFilter();
1609       SMESH::BelongToGeom_var aBelongToGeom = aFilterMgr->CreateBelongToGeom();;
1610       aBelongToGeom->SetGeom(myGeomObjects[0]);
1611       aBelongToGeom->SetShapeName(aGroupSO->GetName().c_str());
1612       aBelongToGeom->SetElementType(aType);
1613       aFilter->SetPredicate(aBelongToGeom);
1614
1615       SMESH::long_array_var anElements = aFilter->GetElementsId(myMesh);
1616
1617       int k = anElements->length();
1618       QListBoxItem* anItem = 0;
1619       for (int i = 0; i < k; i++) {
1620         QString aText = QString::number(anElements[i]);
1621         anItem = myElements->findItem(aText, Qt::ExactMatch);
1622         if (!anItem) {
1623           anItem = new QListBoxText(aText);
1624           myElements->insertItem(anItem);
1625         }
1626         myElements->setSelected(anItem, true);
1627       }
1628     }
1629
1630     //VSR: mySelectGeomGroup->setChecked(false);
1631     myIsBusy = false;
1632     onListSelectionChanged();
1633   }
1634   myIsBusy = false;
1635   //  mySelectionMgr->clearSelected();
1636   updateButtons();
1637 }
1638
1639 //=================================================================================
1640 // function : onRemove()
1641 // purpose  :
1642 //=================================================================================
1643 void SMESHGUI_GroupDlg::onRemove()
1644 {
1645   myIsBusy = true;
1646   if (myCurrentLineEdit == 0) {
1647     for (int i = myElements->count(); i > 0; i--) {
1648       if (myElements->isSelected(i-1)) {
1649         myElements->removeItem(i-1);
1650       }
1651     }
1652   } else {
1653     SALOME_ListIO aList;
1654     mySelectionMgr->selectedObjects( aList );
1655
1656     int aNbSel = aList.Extent();
1657
1658     if (aNbSel == 0) { myIsBusy = false; return; }
1659
1660     SMESH::ElementType aType = SMESH::ALL;
1661     switch(myTypeId) {
1662     case 0: aType = SMESH::NODE; break;
1663     case 1: aType = SMESH::EDGE; break;
1664     case 2: aType = SMESH::FACE; break;
1665     case 3: aType = SMESH::VOLUME; break;
1666     }
1667
1668     if (myCurrentLineEdit == mySubMeshLine) {
1669       //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1670       SALOME_ListIO aList;
1671       mySelectionMgr->selectedObjects( aList );
1672
1673       SALOME_ListIteratorOfListIO anIt (aList);
1674       for (; anIt.More(); anIt.Next()) {
1675         SMESH::SMESH_subMesh_var aSubMesh = SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIt.Value());
1676         if (!aSubMesh->_is_nil()) {
1677           // check if mesh is the same
1678           if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
1679             if (aType == SMESH::NODE) {
1680               try {
1681                 SMESH::long_array_var anElements = aSubMesh->GetNodesId();
1682                 int k = anElements->length();
1683                 QListBoxItem* anItem = 0;
1684                 for (int i = 0; i < k; i++) {
1685                   anItem = myElements->findItem(QString::number(anElements[i]), Qt::ExactMatch);
1686                   if (anItem) delete anItem;
1687                 }
1688               }
1689               catch (const SALOME::SALOME_Exception& ex) {
1690                 SalomeApp_Tools::QtCatchCorbaException(ex);
1691               }
1692             }
1693             else {
1694               try {
1695                 SMESH::long_array_var anElements = aSubMesh->GetElementsId();
1696                 int k = anElements->length();
1697                 QListBoxItem* anItem = 0;
1698                 for (int i = 0; i < k; i++) {
1699                   anItem = myElements->findItem(QString::number(anElements[i]), Qt::ExactMatch);
1700                   if (anItem) delete anItem;
1701                 }
1702               }
1703               catch (const SALOME::SALOME_Exception& ex) {
1704                 SalomeApp_Tools::QtCatchCorbaException(ex);
1705               }
1706             }
1707           }
1708         }
1709       }
1710     }
1711     else if (myCurrentLineEdit == myGroupLine) {
1712       Standard_Boolean aRes;
1713       //SALOME_ListIteratorOfListIO anIt (mySelectionMgr->StoredIObjects());
1714       SALOME_ListIO aList;
1715       mySelectionMgr->selectedObjects( aList );
1716
1717       SALOME_ListIteratorOfListIO anIt (aList);
1718       for (; anIt.More(); anIt.Next()) {
1719         SMESH::SMESH_Group_var aGroup = SMESH::IObjectToInterface<SMESH::SMESH_Group>(anIt.Value());
1720         if (aRes && !aGroup->_is_nil()) {
1721           // check if mesh is the same
1722           if (aGroup->GetType() == aType && aGroup->GetMesh()->GetId() == myMesh->GetId()) {
1723             SMESH::long_array_var anElements = aGroup->GetListOfID();
1724             int k = anElements->length();
1725             QListBoxItem* anItem = 0;
1726             for (int i = 0; i < k; i++) {
1727               anItem = myElements->findItem(QString::number(anElements[i]), Qt::ExactMatch);
1728               if (anItem) delete anItem;
1729             }
1730           }
1731         }
1732       }
1733     }
1734   }
1735   myIsBusy = false;
1736   updateButtons();
1737 }
1738
1739 //=================================================================================
1740 // function : onSort()
1741 // purpose  :
1742 //=================================================================================
1743 void SMESHGUI_GroupDlg::onSort()
1744 {
1745   // PAL5412: sorts items in ascending by "string" value
1746   // myElements->sort(true);
1747   // myElements->update();
1748   int i, k = myElements->count();
1749   if (k > 0) {
1750     myIsBusy = true;
1751     QStringList aSelected;
1752     std::vector<int> anArray(k);
1753     //    QMemArray<int> anArray(k);
1754     QListBoxItem* anItem;
1755     // fill the array
1756     for (anItem = myElements->firstItem(), i = 0; anItem != 0; anItem = anItem->next(), i++) {
1757       anArray[i] = anItem->text().toInt();
1758       if (anItem->isSelected())
1759         aSelected.append(anItem->text());
1760     }
1761     // sort & update list
1762     std::sort(anArray.begin(), anArray.end());
1763     //    anArray.sort();
1764     myElements->clear();
1765     for (i = 0; i < k; i++) {
1766       myElements->insertItem(QString::number(anArray[i]));
1767     }
1768     for (QStringList::iterator it = aSelected.begin(); it != aSelected.end(); ++it) {
1769       anItem = myElements->findItem(*it, Qt::ExactMatch);
1770       if (anItem) myElements->setSelected(anItem, true);
1771     }
1772     myIsBusy = false;
1773   }
1774 }
1775
1776 //=================================================================================
1777 // function : closeEvent()
1778 // purpose  :
1779 //=================================================================================
1780 void SMESHGUI_GroupDlg::closeEvent (QCloseEvent*)
1781 {
1782   onClose();
1783 }
1784
1785 //=================================================================================
1786 // function : SMESHGUI_GroupDlg::onClose
1787 // purpose  : SLOT called when "Close" button pressed. Close dialog
1788 //=================================================================================
1789 void SMESHGUI_GroupDlg::onClose()
1790 {
1791   if (SMESH::GetCurrentVtkView()) {
1792     SMESH::RemoveFilters(); // PAL6938 -- clean all mesh entity filters
1793     SMESH::SetPointRepresentation(false);
1794     SMESH::SetPickable();
1795     restoreShowEntityMode();
1796   }
1797
1798   mySelectionMgr->clearSelected();
1799   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
1800     aViewWindow->SetSelectionMode(ActorSelection);
1801   mySelectionMgr->clearFilters();
1802   mySMESHGUI->ResetState();
1803
1804   reject();
1805 }
1806
1807 //=================================================================================
1808 // function : onHelp()
1809 // purpose  :
1810 //=================================================================================
1811 void SMESHGUI_GroupDlg::onHelp()
1812 {
1813   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
1814   if (app)
1815     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""),
1816                              myHelpFileName);
1817   else {
1818     QString platform;
1819 #ifdef WIN32
1820     platform = "winapplication";
1821 #else
1822     platform = "application";
1823 #endif
1824     SUIT_MessageBox::warn1
1825       (0, QObject::tr("WRN_WARNING"),
1826        QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
1827        arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(myHelpFileName),
1828        QObject::tr("BUT_OK"));
1829   }
1830 }
1831
1832 //=================================================================================
1833 // function : SMESHGUI_GroupDlg::onDeactivate
1834 // purpose  : SLOT called when dialog must be deativated
1835 //=================================================================================
1836 void SMESHGUI_GroupDlg::onDeactivate()
1837 {
1838   mySMESHGUI->ResetState();
1839   setEnabled(false);
1840 }
1841
1842 //=================================================================================
1843 // function : SMESHGUI_GroupDlg::enterEvent
1844 // purpose  : Event filter
1845 //=================================================================================
1846 void SMESHGUI_GroupDlg::enterEvent (QEvent*)
1847 {
1848   if (!isEnabled()) {
1849     mySMESHGUI->EmitSignalDeactivateDialog();
1850     setEnabled(true);
1851     mySelectionMode = -1;
1852     setSelectionMode(myTypeId);
1853     //mySMESHGUI->SetActiveDialogBox((QDialog*)this);
1854     mySMESHGUI->SetActiveDialogBox(this);
1855     mySMESHGUI->SetState(800);
1856   }
1857 }
1858
1859 //=================================================================================
1860 // function : hideEvent
1861 // purpose  : caused by ESC key
1862 //=================================================================================
1863 void SMESHGUI_GroupDlg::hideEvent (QHideEvent*)
1864 {
1865   if (!isMinimized() && !myIsBusy)
1866     onClose();
1867 }
1868
1869 //=================================================================================
1870 // function : keyPressEvent()
1871 // purpose  :
1872 //=================================================================================
1873 void SMESHGUI_GroupDlg::keyPressEvent( QKeyEvent* e )
1874 {
1875   QDialog::keyPressEvent( e );
1876   if ( e->isAccepted() )
1877     return;
1878
1879   if ( e->key() == Key_F1 )
1880     {
1881       e->accept();
1882       onHelp();
1883     }
1884 }
1885
1886 //================================================================================
1887 /*!
1888  * \brief Enable showing of the popup when Geometry selection btn is clicked
1889   * \param enable - true to enable
1890  */
1891 //================================================================================
1892
1893 enum { DIRECT_GEOM_INDEX = 0, GEOM_BY_MESH_INDEX };
1894
1895 void SMESHGUI_GroupDlg::updateGeomPopup()
1896 {
1897   bool enable = false;
1898
1899   if ( !myMesh->_is_nil() )
1900     enable = myMesh->NbEdges() > 0;
1901
1902   if ( myGeomGroupBtn )
1903   {
1904     disconnect( myGeomGroupBtn, SIGNAL( toggled(bool) ), this, SLOT( onGeomSelectionButton(bool) ));
1905     if ( enable ) {
1906       if ( !myGeomPopup ) {
1907         myGeomPopup = new QPopupMenu();
1908         myGeomPopup->insertItem( tr("DIRECT_GEOM_SELECTION"), DIRECT_GEOM_INDEX );
1909         myGeomPopup->insertItem( tr("GEOM_BY_MESH_ELEM_SELECTION"), GEOM_BY_MESH_INDEX );
1910         connect( myGeomPopup, SIGNAL( activated( int ) ), SLOT( onGeomPopup( int ) ) );
1911       }
1912       connect( myGeomGroupBtn, SIGNAL( toggled(bool) ), this, SLOT( onGeomSelectionButton(bool) ));
1913     }
1914   }
1915 }
1916
1917
1918 //=================================================================================
1919 // function : onGeomSelectionButton()
1920 // purpose  :
1921 //=================================================================================
1922 void SMESHGUI_GroupDlg::onGeomSelectionButton(bool isBtnOn)
1923 {
1924   if ( myGeomPopup && isBtnOn )
1925     {
1926       myCurrentLineEdit = myGeomGroupLine;
1927       int id = myGeomPopup->exec( QCursor::pos() );
1928       if (id == DIRECT_GEOM_INDEX || id == -1)
1929         setSelectionMode(7);
1930     }
1931   else if (!isBtnOn)
1932     {
1933       myCurrentLineEdit = 0;
1934       setSelectionMode(8);
1935     }
1936 }
1937
1938 //=================================================================================
1939 // function : onGeomPopup()
1940 // purpose  :
1941 //=================================================================================
1942 void SMESHGUI_GroupDlg::onGeomPopup( int index )
1943 {
1944   if ( index == GEOM_BY_MESH_INDEX )
1945     {
1946       mySelectionMode = -1;
1947       if ( !myShapeByMeshOp ) {
1948         myShapeByMeshOp = new SMESHGUI_ShapeByMeshOp(true);
1949         connect(myShapeByMeshOp, SIGNAL(committed(SUIT_Operation*)),
1950                 SLOT(onPublishShapeByMeshDlg(SUIT_Operation*)));
1951         connect(myShapeByMeshOp, SIGNAL(aborted(SUIT_Operation*)),
1952                 SLOT(onCloseShapeByMeshDlg(SUIT_Operation*)));
1953       }
1954       // set mesh object to SMESHGUI_ShapeByMeshOp and start it
1955       if ( !myMesh->_is_nil() ) {
1956         myIsBusy = true;
1957         hide(); // stop processing selection
1958         myIsBusy = false;
1959         myShapeByMeshOp->setModule( mySMESHGUI );
1960         myShapeByMeshOp->setStudy( 0 ); // it's really necessary
1961         myShapeByMeshOp->SetMesh( myMesh );
1962         myShapeByMeshOp->start();
1963       }
1964     }
1965 }
1966
1967 //================================================================================
1968 /*!
1969  * \brief SLOT. Is called when Ok is pressed in SMESHGUI_ShapeByMeshDlg
1970  */
1971 //================================================================================
1972
1973 void SMESHGUI_GroupDlg::onPublishShapeByMeshDlg(SUIT_Operation* op)
1974 {
1975   if ( myShapeByMeshOp == op ) {
1976     mySMESHGUI->getApp()->updateObjectBrowser();
1977     show();
1978     // Select a found geometry object
1979     GEOM::GEOM_Object_var aGeomVar = myShapeByMeshOp->GetShape();
1980     if ( !aGeomVar->_is_nil() )
1981     {
1982       QString ID = aGeomVar->GetStudyEntry();
1983       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1984       if ( _PTR(SObject) aGeomSO = aStudy->FindObjectID( ID.latin1() )) {
1985         SALOME_ListIO anIOList;
1986         Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject
1987           ( aGeomSO->GetID().c_str(), "SMESH", aGeomSO->GetName().c_str() );
1988         anIOList.Append( anIO );
1989               mySelectionMgr->setSelectedObjects( anIOList, false );
1990         onObjectSelectionChanged();
1991       }
1992     }
1993   }
1994 }
1995
1996 //================================================================================
1997 /*!
1998  * \brief SLOT. Is called when Close is pressed in SMESHGUI_ShapeByMeshDlg
1999  */
2000 //================================================================================
2001
2002 void SMESHGUI_GroupDlg::onCloseShapeByMeshDlg(SUIT_Operation* op)
2003 {
2004   if ( myShapeByMeshOp == op )
2005     {
2006       show();
2007       setSelectionMode(7);
2008     }
2009 }
2010
2011 //=================================================================================
2012 // function : setGroupColor()
2013 // purpose  :
2014 //=================================================================================
2015 void SMESHGUI_GroupDlg::setGroupColor( const SALOMEDS::Color& theColor )
2016 {
2017   QColor aQColor( (int)( theColor.R * 255.0 ),
2018                   (int)( theColor.G * 255.0 ),
2019                   (int)( theColor.B * 255.0 ) );
2020   setGroupQColor( aQColor );
2021 }
2022
2023 //=================================================================================
2024 // function : getGroupColor()
2025 // purpose  :
2026 //=================================================================================
2027 SALOMEDS::Color SMESHGUI_GroupDlg::getGroupColor() const
2028 {
2029   QColor aQColor = getGroupQColor();
2030
2031   SALOMEDS::Color aColor;
2032   aColor.R = (float)aQColor.red() / 255.0;
2033   aColor.G = (float)aQColor.green() / 255.0;
2034   aColor.B = (float)aQColor.blue() / 255.0;
2035
2036   return aColor;
2037 }
2038
2039 //=================================================================================
2040 // function : setGroupQColor()
2041 // purpose  :
2042 //=================================================================================
2043 void SMESHGUI_GroupDlg::setGroupQColor( const QColor& theColor )
2044 {
2045   if ( theColor.isValid() )
2046   {
2047     QPalette pal = myColorBtn->palette();
2048     pal.setColor(QColorGroup::Button, theColor);
2049     myColorBtn->setPalette(pal);
2050   }
2051 }
2052
2053 //=================================================================================
2054 // function : getGroupQColor()
2055 // purpose  :
2056 //=================================================================================
2057 QColor SMESHGUI_GroupDlg::getGroupQColor() const
2058 {
2059   QColor aColor = myColorBtn->palette().active().button();
2060   return aColor;
2061 }
2062
2063 //=================================================================================
2064 // function : setDefaultGroupColor()
2065 // purpose  :
2066 //=================================================================================
2067 void SMESHGUI_GroupDlg::setDefaultGroupColor()
2068 {
2069   if ( myMesh->_is_nil() )
2070     return;
2071
2072   bool isAutoColor = myMesh->GetAutoColor();
2073
2074   QColor aQColor;
2075   if ( !isAutoColor )
2076   {
2077     int r = 0, g = 0, b = 0;
2078     SMESH::GetColor( "SMESH", "fill_color", r, g, b, QColor( 0, 170, 255 ) );
2079     aQColor.setRgb( r, g, b );
2080   }
2081   else
2082   {
2083     SMESH::ListOfGroups aListOfGroups = *myMesh->GetGroups();
2084
2085     QValueList<SALOMEDS::Color> aReservedColors;
2086     for ( int i = 0, n = aListOfGroups.length(); i < n; i++ )
2087     {
2088       SMESH::SMESH_GroupBase_var aGroupObject = aListOfGroups[i];
2089       SALOMEDS::Color aReservedColor = aGroupObject->GetColor();
2090       aReservedColors.append( aReservedColor );
2091     }
2092
2093     SALOMEDS::Color aColor = SMESHGUI::getUniqueColor( aReservedColors );
2094     aQColor.setRgb( (int)( aColor.R * 255.0 ),
2095                     (int)( aColor.G * 255.0 ),
2096                     (int)( aColor.B * 255.0 ) );
2097
2098   }
2099
2100   setGroupQColor( aQColor );
2101 }
2102
2103 //=================================================================================
2104 // function : SetAppropriateActor()
2105 // purpose  : Find more appropriate of visible actors, set it to myActor, allow picking
2106 //            NPAL19389: create a group with a selection in another group.
2107 //            if mesh actor is not visible - find any first visible group or submesh
2108 //=================================================================================
2109 bool SMESHGUI_GroupDlg::SetAppropriateActor()
2110 {
2111   bool isActor = false;
2112
2113   if (myMesh->_is_nil()) return false;
2114
2115   SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView();
2116
2117   // try mesh actor
2118   myActor = SMESH::FindActorByObject(myMesh);
2119   if (myActor && myActor->hasIO())
2120   {
2121     isActor = true;
2122     if (aViewWindow && !aViewWindow->isVisible(myActor->getIO()))
2123         isActor = false;
2124   }
2125
2126   // try current group actor
2127   if (!isActor) {
2128     if (!myGroup->_is_nil()) {
2129       myActor = SMESH::FindActorByObject(myGroup);
2130       if (myActor && myActor->hasIO())
2131       {
2132         isActor = true;
2133         if (aViewWindow && !aViewWindow->isVisible(myActor->getIO()))
2134             isActor = false;
2135       }
2136     }
2137   }
2138
2139   // try current group on geometry actor
2140   if (!isActor) {
2141     if (!myGroupOnGeom->_is_nil()) {
2142       myActor = SMESH::FindActorByObject(myGroupOnGeom);
2143       if (myActor && myActor->hasIO())
2144       {
2145         isActor = true;
2146         if (aViewWindow && !aViewWindow->isVisible(myActor->getIO()))
2147           isActor = false;
2148       }
2149     }
2150   }
2151
2152   // try any visible actor of group or submesh of current mesh
2153   if (!isActor && aViewWindow) {
2154     // mesh entry
2155     _PTR(SObject) aSObject = SMESH::FindSObject(myMesh);
2156     if (aSObject) {
2157       CORBA::String_var meshEntry = aSObject->GetID().c_str();
2158       int len = strlen(meshEntry);
2159
2160       // iterate on all actors in current view window, search for
2161       // any visible actor, that belongs to group or submesh of current mesh
2162       VTK::ActorCollectionCopy aCopy(aViewWindow->getRenderer()->GetActors());
2163       vtkActorCollection *aCollection = aCopy.GetActors();
2164       aCollection->InitTraversal();
2165       for (vtkActor *anAct = aCollection->GetNextActor();
2166            anAct && !isActor;
2167            anAct = aCollection->GetNextActor())
2168       {
2169         SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct);
2170         if (anActor && anActor->hasIO()) {
2171           Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
2172           if (aViewWindow->isVisible(anIO)) {
2173             if (anIO->hasEntry() && strncmp(anIO->getEntry(), meshEntry, len) == 0) {
2174               myActor = anActor;
2175               isActor = true;
2176             }
2177           }
2178         }
2179       }
2180     }
2181   }
2182
2183   if (isActor)
2184     SMESH::SetPickable(myActor);
2185
2186   return isActor;
2187 }
2188
2189 //=======================================================================
2190 //function : setShowEntityMode
2191 //purpose  : make shown only entity corresponding to my type
2192 //=======================================================================
2193 void SMESHGUI_GroupDlg::setShowEntityMode()
2194 {
2195   if ( !myMesh->_is_nil() ) {
2196     if ( SMESH_Actor* actor = SMESH::FindActorByObject(myMesh) ) {
2197       if (!myStoredShownEntity)
2198         myStoredShownEntity = actor->GetEntityMode();
2199       switch ( myTypeId ) {
2200       case 0: restoreShowEntityMode(); break;
2201       case 1: actor->SetEntityMode( SMESH_Actor::eEdges ); break;
2202       case 2: actor->SetEntityMode( SMESH_Actor::eFaces ); break;
2203       case 3: actor->SetEntityMode( SMESH_Actor::eVolumes ); break;
2204       }
2205     }
2206   }
2207 }
2208
2209 //=======================================================================
2210 //function : restoreShowEntityMode
2211 //purpose  : restore ShowEntity mode of myActor
2212 //=======================================================================
2213 void SMESHGUI_GroupDlg::restoreShowEntityMode()
2214 {
2215   if ( myStoredShownEntity && !myMesh->_is_nil() ) {
2216     if ( SMESH_Actor* actor = SMESH::FindActorByObject(myMesh) ) {
2217       actor->SetEntityMode(myStoredShownEntity);
2218     }
2219   }
2220   myStoredShownEntity = 0;
2221 }