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