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