Salome HOME
Integrate patch from G.David (for vtk 5.2 and newer)
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_EditMeshDlg.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // SMESH SMESHGUI : GUI for SMESH component
23 // File   : SMESHGUI_EditMeshDlg.cxx
24 // Author : Open CASCADE S.A.S.
25 // SMESH includes
26 //
27 #include "SMESHGUI_EditMeshDlg.h"
28
29 #include "SMESHGUI.h"
30 #include "SMESHGUI_Utils.h"
31 #include "SMESHGUI_VTKUtils.h"
32 #include "SMESHGUI_MeshUtils.h"
33 #include "SMESHGUI_SpinBox.h"
34
35 #include <SMESH_Actor.h>
36 #include <SMESH_TypeFilter.hxx>
37 #include <SMESH_LogicalFilter.hxx>
38 #include <SMDS_Mesh.hxx>
39
40 // SALOME GUI includes
41 #include <SUIT_Desktop.h>
42 #include <SUIT_ResourceMgr.h>
43 #include <SUIT_Session.h>
44 #include <SUIT_MessageBox.h>
45 #include <SUIT_OverrideCursor.h>
46
47 #include <LightApp_Application.h>
48 #include <LightApp_SelectionMgr.h>
49
50 #include <SVTK_ViewModel.h>
51 #include <SVTK_ViewWindow.h>
52 #include <SALOME_ListIO.hxx>
53
54 // OCCT includes
55 #include <TColStd_MapOfInteger.hxx>
56 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
57
58 // IDL includes
59 #include <SALOMEconfig.h>
60 #include CORBA_SERVER_HEADER(SMESH_Group)
61 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
62
63 // VTK includes
64 #include <vtkUnstructuredGrid.h>
65 #include <vtkRenderer.h>
66 #include <vtkActor2D.h>
67 #include <vtkPoints.h>
68 #include <vtkDataSetMapper.h>
69 #include <vtkMaskPoints.h>
70 #include <vtkSelectVisiblePoints.h>
71 #include <vtkLabeledDataMapper.h>
72 #include <vtkTextProperty.h>
73 #include <vtkIntArray.h>
74 #include <vtkProperty2D.h>
75 #include <vtkPointData.h>
76 #include <vtkConfigure.h>
77 #if !defined(VTK_XVERSION)
78 #define VTK_XVERSION (VTK_MAJOR_VERSION<<16)+(VTK_MINOR_VERSION<<8)+(VTK_BUILD_VERSION)
79 #endif
80
81 // Qt includes
82 #include <QApplication>
83 #include <QGroupBox>
84 #include <QLabel>
85 #include <QLineEdit>
86 #include <QListWidget>
87 #include <QPushButton>
88 #include <QRadioButton>
89 #include <QCheckBox>
90 #include <QHBoxLayout>
91 #include <QVBoxLayout>
92 #include <QGridLayout>
93 #include <QKeyEvent>
94 #include <QButtonGroup>
95
96 #define SPACING 6
97 #define MARGIN  11
98
99 namespace SMESH
100 {
101   class TIdPreview
102   { // to display in the viewer IDs of the selected elements
103     SVTK_ViewWindow* myViewWindow;
104
105     vtkUnstructuredGrid* myIdGrid;
106     SALOME_Actor* myIdActor;
107
108     vtkUnstructuredGrid* myPointsNumDataSet;
109     vtkMaskPoints* myPtsMaskPoints;
110     vtkSelectVisiblePoints* myPtsSelectVisiblePoints;
111     vtkLabeledDataMapper* myPtsLabeledDataMapper;
112     vtkTextProperty* aPtsTextProp;
113     bool myIsPointsLabeled;
114     vtkActor2D* myPointLabels;
115
116     std::vector<int> myIDs;
117
118   public:
119     TIdPreview(SVTK_ViewWindow* theViewWindow):
120       myViewWindow(theViewWindow)
121     {
122       myIdGrid = vtkUnstructuredGrid::New();
123
124       // Create and display actor
125       vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
126       aMapper->SetInput( myIdGrid );
127
128       myIdActor = SALOME_Actor::New();
129       myIdActor->SetInfinitive(true);
130       myIdActor->VisibilityOff();
131       myIdActor->PickableOff();
132
133       myIdActor->SetMapper( aMapper );
134       aMapper->Delete();
135
136       myViewWindow->AddActor(myIdActor);
137
138       //Definition of points numbering pipeline
139       myPointsNumDataSet = vtkUnstructuredGrid::New();
140
141       myPtsMaskPoints = vtkMaskPoints::New();
142       myPtsMaskPoints->SetInput(myPointsNumDataSet);
143       myPtsMaskPoints->SetOnRatio(1);
144
145       myPtsSelectVisiblePoints = vtkSelectVisiblePoints::New();
146       myPtsSelectVisiblePoints->SetInput(myPtsMaskPoints->GetOutput());
147       myPtsSelectVisiblePoints->SelectInvisibleOff();
148       myPtsSelectVisiblePoints->SetTolerance(0.1);
149     
150       myPtsLabeledDataMapper = vtkLabeledDataMapper::New();
151       myPtsLabeledDataMapper->SetInput(myPtsSelectVisiblePoints->GetOutput());
152 #if (VTK_XVERSION < 0x050200)
153       myPtsLabeledDataMapper->SetLabelFormat("%g");
154 #endif
155       myPtsLabeledDataMapper->SetLabelModeToLabelScalars();
156     
157       vtkTextProperty* aPtsTextProp = vtkTextProperty::New();
158       aPtsTextProp->SetFontFamilyToTimes();
159       static int aPointsFontSize = 12;
160       aPtsTextProp->SetFontSize(aPointsFontSize);
161       aPtsTextProp->SetBold(1);
162       aPtsTextProp->SetItalic(0);
163       aPtsTextProp->SetShadow(0);
164       myPtsLabeledDataMapper->SetLabelTextProperty(aPtsTextProp);
165       aPtsTextProp->Delete();
166   
167       myIsPointsLabeled = false;
168
169       myPointLabels = vtkActor2D::New();
170       myPointLabels->SetMapper(myPtsLabeledDataMapper);
171       myPointLabels->GetProperty()->SetColor(1,1,1);
172       myPointLabels->SetVisibility(myIsPointsLabeled);
173
174       AddToRender(myViewWindow->getRenderer());
175     }
176
177     void SetPointsData ( SMDS_Mesh* theMesh, 
178                          TColStd_MapOfInteger & theNodesIdMap )
179     {
180       vtkPoints* aPoints = vtkPoints::New();
181       aPoints->SetNumberOfPoints(theNodesIdMap.Extent());
182       myIDs.clear();
183       
184       TColStd_MapIteratorOfMapOfInteger idIter( theNodesIdMap );
185       for( int i = 0; idIter.More(); idIter.Next(), i++ ) {
186         const SMDS_MeshNode* aNode = theMesh->FindNode(idIter.Key());
187         aPoints->SetPoint( i, aNode->X(), aNode->Y(), aNode->Z() );
188         myIDs.push_back(idIter.Key());
189       }
190
191       myIdGrid->SetPoints(aPoints);
192
193       aPoints->Delete();
194
195       myIdActor->GetMapper()->Update();
196     }
197
198     void SetElemsData( TColStd_MapOfInteger & theElemsIdMap, 
199                        std::list<gp_XYZ> & aGrCentersXYZ )
200     {
201       vtkPoints* aPoints = vtkPoints::New();
202       aPoints->SetNumberOfPoints(theElemsIdMap.Extent());
203       myIDs.clear();
204       
205       TColStd_MapIteratorOfMapOfInteger idIter( theElemsIdMap );
206       for( ; idIter.More(); idIter.Next() ) {
207         myIDs.push_back(idIter.Key());
208       }
209
210       gp_XYZ aXYZ;
211       std::list<gp_XYZ>::iterator coordIt = aGrCentersXYZ.begin();
212       for( int i = 0; coordIt != aGrCentersXYZ.end(); coordIt++, i++ ) {
213         aXYZ = *coordIt;
214         aPoints->SetPoint( i, aXYZ.X(), aXYZ.Y(), aXYZ.Z() );
215       }
216       myIdGrid->SetPoints(aPoints);
217       aPoints->Delete();
218       
219       myIdActor->GetMapper()->Update();
220     }
221
222     void AddToRender(vtkRenderer* theRenderer)
223     {
224       myIdActor->AddToRender(theRenderer);
225
226       myPtsSelectVisiblePoints->SetRenderer(theRenderer);
227       theRenderer->AddActor2D(myPointLabels);
228     }
229
230     void RemoveFromRender(vtkRenderer* theRenderer)
231     {
232       myIdActor->RemoveFromRender(theRenderer);
233
234       myPtsSelectVisiblePoints->SetRenderer(theRenderer);
235       theRenderer->RemoveActor(myPointLabels);
236     }
237
238     void SetPointsLabeled( bool theIsPointsLabeled, bool theIsActorVisible = true )
239     {
240       myIsPointsLabeled = theIsPointsLabeled && myIdGrid->GetNumberOfPoints();
241       
242       if ( myIsPointsLabeled ) {
243         myPointsNumDataSet->ShallowCopy(myIdGrid);
244         vtkDataSet *aDataSet = myPointsNumDataSet;
245         int aNbElem = myIDs.size();
246         vtkIntArray *anArray = vtkIntArray::New();
247         anArray->SetNumberOfValues( aNbElem );
248         for ( int i = 0; i < aNbElem; i++ )
249           anArray->SetValue( i, myIDs[i] );
250         aDataSet->GetPointData()->SetScalars( anArray );
251         anArray->Delete();
252         myPtsMaskPoints->SetInput( aDataSet );
253         myPointLabels->SetVisibility( theIsActorVisible );
254       }
255       else {
256         myPointLabels->SetVisibility( false );
257       }
258     }
259     
260     ~TIdPreview()
261     {
262       RemoveFromRender(myViewWindow->getRenderer());
263
264       myIdGrid->Delete();
265
266       myViewWindow->RemoveActor(myIdActor);
267       myIdActor->Delete();
268
269       //Deleting of points numbering pipeline
270       //---------------------------------------
271       myPointsNumDataSet->Delete();
272       
273       //myPtsLabeledDataMapper->RemoveAllInputs();        //vtk 5.0 porting
274       myPtsLabeledDataMapper->Delete();
275
276       //myPtsSelectVisiblePoints->UnRegisterAllOutputs(); //vtk 5.0 porting
277       myPtsSelectVisiblePoints->Delete();
278
279       //myPtsMaskPoints->UnRegisterAllOutputs();          //vtk 5.0 porting
280       myPtsMaskPoints->Delete();
281
282       myPointLabels->Delete();
283
284 //       myTimeStamp->Delete();
285     }
286   };
287 }
288
289 static const char * IconFirst[] = {
290 "18 10 2 1",
291 "       g None",
292 ".      g #000000",
293 "         .     .  ",
294 "  ..    ..    ..  ",
295 "  ..   ...   ...  ",
296 "  ..  ....  ....  ",
297 "  .. ..... .....  ",
298 "  .. ..... .....  ",
299 "  ..  ....  ....  ",
300 "  ..   ...   ...  ",
301 "  ..    ..    ..  ",
302 "         .     .  "};
303
304 //=================================================================================
305 // class    : SMESHGUI_EditMeshDlg()
306 // purpose  :
307 //=================================================================================
308 SMESHGUI_EditMeshDlg::SMESHGUI_EditMeshDlg (SMESHGUI* theModule, 
309                                             int theAction)
310   : QDialog(SMESH::GetDesktop(theModule)),
311     mySMESHGUI(theModule),
312     mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
313     myAction(theAction)
314 {
315   setModal(false);
316   setAttribute(Qt::WA_DeleteOnClose, true);
317   setWindowTitle(myAction == 1 ? tr("SMESH_MERGE_ELEMENTS") : tr("SMESH_MERGE_NODES"));
318
319   myIdPreview = new SMESH::TIdPreview(SMESH::GetViewWindow( mySMESHGUI ));
320
321   SUIT_ResourceMgr* aResMgr = SMESH::GetResourceMgr( mySMESHGUI );
322   QPixmap IconMergeNodes (aResMgr->loadPixmap("SMESH", tr("ICON_SMESH_MERGE_NODES")));
323   QPixmap IconMergeElems (aResMgr->loadPixmap("SMESH", tr("ICON_DLG_MERGE_ELEMENTS")));
324   QPixmap IconSelect     (aResMgr->loadPixmap("SMESH", tr("ICON_SELECT")));
325   QPixmap IconAdd        (aResMgr->loadPixmap("SMESH", tr("ICON_APPEND")));
326   QPixmap IconRemove     (aResMgr->loadPixmap("SMESH", tr("ICON_REMOVE")));
327
328   setSizeGripEnabled(true);
329
330   QVBoxLayout* DlgLayout = new QVBoxLayout(this);
331   DlgLayout->setSpacing(SPACING);
332   DlgLayout->setMargin(MARGIN);
333
334   /***************************************************************/
335   GroupConstructors = new QGroupBox(myAction == 1 ? 
336                                     tr("SMESH_MERGE_ELEMENTS") : 
337                                     tr("SMESH_MERGE_NODES"), 
338                                     this);
339
340   QButtonGroup* ButtonGroup = new QButtonGroup(this);
341   QHBoxLayout* GroupConstructorsLayout = new QHBoxLayout(GroupConstructors);
342   GroupConstructorsLayout->setSpacing(SPACING);
343   GroupConstructorsLayout->setMargin(MARGIN);
344
345   RadioButton = new QRadioButton(GroupConstructors);
346   RadioButton->setIcon(myAction == 1 ? IconMergeElems : IconMergeNodes);
347   RadioButton->setChecked(true);
348   GroupConstructorsLayout->addWidget(RadioButton);
349   ButtonGroup->addButton(RadioButton, 0);
350
351   /***************************************************************/
352   // Controls for mesh defining
353   GroupMesh = new QGroupBox(tr("SMESH_SELECT_WHOLE_MESH"), this);
354   QHBoxLayout* GroupMeshLayout = new QHBoxLayout(GroupMesh);
355   GroupMeshLayout->setSpacing(SPACING);
356   GroupMeshLayout->setMargin(MARGIN);
357
358   TextLabelName = new QLabel(tr("SMESH_NAME"), GroupMesh);
359   SelectMeshButton = new QPushButton(GroupMesh);
360   SelectMeshButton->setIcon(IconSelect);
361   LineEditMesh = new QLineEdit(GroupMesh);
362   LineEditMesh->setReadOnly(true);
363
364   GroupMeshLayout->addWidget(TextLabelName);
365   GroupMeshLayout->addWidget(SelectMeshButton);
366   GroupMeshLayout->addWidget(LineEditMesh);
367
368   /***************************************************************/
369   // Controls for coincident elements detecting
370   GroupCoincident = new QGroupBox(myAction == 1 ? 
371                                   tr("COINCIDENT_ELEMENTS") : 
372                                   tr("COINCIDENT_NODES"), 
373                                   this);
374
375   QGridLayout* GroupCoincidentLayout = new QGridLayout(GroupCoincident);
376   GroupCoincidentLayout->setSpacing(SPACING);
377   GroupCoincidentLayout->setMargin(MARGIN);
378   
379   if (myAction == 0) { // case merge nodes
380     TextLabelTolerance = new QLabel(tr("SMESH_TOLERANCE"), GroupCoincident);
381     SpinBoxTolerance = new SMESHGUI_SpinBox(GroupCoincident);
382     SpinBoxTolerance->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
383
384     GroupCoincidentLayout->addWidget(TextLabelTolerance, 0, 0);
385     GroupCoincidentLayout->addWidget(SpinBoxTolerance,   0, 1);
386   }
387   else {
388     TextLabelTolerance = 0;
389     SpinBoxTolerance = 0;
390   }
391
392   int row = GroupCoincidentLayout->rowCount();
393
394   ListCoincident = new QListWidget(GroupCoincident);
395   ListCoincident->setSelectionMode(QListWidget::ExtendedSelection);
396
397   DetectButton      = new QPushButton(tr("DETECT"),           GroupCoincident);
398   AddGroupButton    = new QPushButton(tr("SMESH_BUT_ADD"),    GroupCoincident);
399   RemoveGroupButton = new QPushButton(tr("SMESH_BUT_REMOVE"), GroupCoincident);
400
401   SelectAllCB = new QCheckBox(tr("SELECT_ALL"), GroupCoincident);
402
403   GroupCoincidentLayout->addWidget(ListCoincident,    row,   0, 4, 2);
404   GroupCoincidentLayout->addWidget(DetectButton,      row,   2);
405   GroupCoincidentLayout->addWidget(AddGroupButton,    row+2, 2);
406   GroupCoincidentLayout->addWidget(RemoveGroupButton, row+3, 2);
407   GroupCoincidentLayout->addWidget(SelectAllCB,       row+4, 0, 1, 3);
408   GroupCoincidentLayout->setRowMinimumHeight(row+1, 10);
409   GroupCoincidentLayout->setRowStretch(row+1, 5);
410
411   /***************************************************************/
412   // Controls for editing the selected group
413   GroupEdit = new QGroupBox(tr("EDIT_SELECTED_GROUP"), this);
414   QGridLayout* GroupEditLayout = new QGridLayout(GroupEdit);
415   GroupEditLayout->setSpacing(SPACING);
416   GroupEditLayout->setMargin(MARGIN);
417
418   ListEdit = new QListWidget(GroupEdit);
419   //ListEdit->setRowMode(QListBox::FixedNumber);
420   //ListEdit->setHScrollBarMode(QScrollView::AlwaysOn);
421   //ListEdit->setVScrollBarMode(QScrollView::AlwaysOff);
422   ListEdit->setFlow( QListView::LeftToRight );
423   ListEdit->setSelectionMode(QListWidget::ExtendedSelection);
424
425   AddElemButton = new QPushButton(GroupEdit);
426   AddElemButton->setIcon(IconAdd);
427   RemoveElemButton = new QPushButton(GroupEdit);
428   RemoveElemButton->setIcon(IconRemove);
429   SetFirstButton = new QPushButton(GroupEdit);
430   SetFirstButton->setIcon(QPixmap(IconFirst));
431
432   GroupEditLayout->addWidget(ListEdit,         0, 0, 2, 1);
433   GroupEditLayout->addWidget(AddElemButton,    0, 1);
434   GroupEditLayout->addWidget(RemoveElemButton, 0, 2);
435   GroupEditLayout->addWidget(SetFirstButton,   1, 1, 1, 2);
436
437   /***************************************************************/
438   GroupButtons = new QGroupBox(this);
439   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
440   GroupButtonsLayout->setSpacing(SPACING);
441   GroupButtonsLayout->setMargin(MARGIN);
442
443   buttonOk = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), GroupButtons);
444   buttonOk->setAutoDefault(true);
445   buttonOk->setDefault(true);
446   buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
447   buttonApply->setAutoDefault(true);
448   buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
449   buttonCancel->setAutoDefault(true);
450   buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
451   buttonHelp->setAutoDefault(true);
452
453   GroupButtonsLayout->addWidget(buttonOk);
454   GroupButtonsLayout->addSpacing(10);
455   GroupButtonsLayout->addWidget(buttonApply);
456   GroupButtonsLayout->addSpacing(10);
457   GroupButtonsLayout->addStretch();
458   GroupButtonsLayout->addWidget(buttonCancel);
459   GroupButtonsLayout->addWidget(buttonHelp);
460
461   /***************************************************************/
462   DlgLayout->addWidget(GroupConstructors);
463   DlgLayout->addWidget(GroupMesh);
464   DlgLayout->addWidget(GroupCoincident);
465   DlgLayout->addWidget(GroupEdit);
466   DlgLayout->addWidget(GroupButtons);
467
468   Init(); // Initialisations
469 }
470
471 //=================================================================================
472 // function : ~SMESHGUI_EditMeshDlg()
473 // purpose  : Destroys the object and frees any allocated resources
474 //=================================================================================
475 SMESHGUI_EditMeshDlg::~SMESHGUI_EditMeshDlg()
476 {
477   delete myIdPreview;
478 }
479
480 //=================================================================================
481 // function : Init()
482 // purpose  :
483 //=================================================================================
484 void SMESHGUI_EditMeshDlg::Init()
485 {
486   if (myAction == 0) {
487     SpinBoxTolerance->RangeStepAndValidator(0.0, COORD_MAX, 0.00001, 5);
488     SpinBoxTolerance->SetValue(1e-05);
489   }
490
491   RadioButton->setChecked(true);
492
493   myEditCurrentArgument = (QWidget*)LineEditMesh; 
494
495   myActor = 0;
496   mySubMeshOrGroup = SMESH::SMESH_subMesh::_nil();
497
498   mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
499
500   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
501   myIsBusy = false;
502   
503   // Costruction of the logical filter
504   SMESH_TypeFilter* aMeshOrSubMeshFilter = new SMESH_TypeFilter (MESHorSUBMESH);
505   SMESH_TypeFilter* aSmeshGroupFilter    = new SMESH_TypeFilter (GROUP);
506   
507   QList<SUIT_SelectionFilter*> aListOfFilters;
508   if (aMeshOrSubMeshFilter) aListOfFilters.append(aMeshOrSubMeshFilter);
509   if (aSmeshGroupFilter)    aListOfFilters.append(aSmeshGroupFilter);
510
511   myMeshOrSubMeshOrGroupFilter =
512     new SMESH_LogicalFilter (aListOfFilters, SMESH_LogicalFilter::LO_OR);
513   
514   /* signals and slots connections */
515   connect(buttonOk,     SIGNAL(clicked()), this, SLOT(ClickOnOk()));
516   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
517   connect(buttonApply,  SIGNAL(clicked()), this, SLOT(ClickOnApply()));
518   connect(buttonHelp,   SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
519
520   connect(SelectMeshButton, SIGNAL (clicked()), this, SLOT(SetEditCurrentArgument()));
521   connect(DetectButton, SIGNAL (clicked()), this, SLOT(onDetect()));
522   connect(ListCoincident, SIGNAL (itemSelectionChanged()), this, SLOT(onSelectGroup()));
523   connect(AddGroupButton, SIGNAL (clicked()), this, SLOT(onAddGroup()));
524   connect(RemoveGroupButton, SIGNAL (clicked()), this, SLOT(onRemoveGroup()));
525   connect(SelectAllCB, SIGNAL(toggled(bool)), this, SLOT(onSelectAll(bool)));
526   connect(ListEdit, SIGNAL (itemSelectionChanged()), this, SLOT(onSelectElementFromGroup()));
527   connect(AddElemButton, SIGNAL (clicked()), this, SLOT(onAddElement()));
528   connect(RemoveElemButton, SIGNAL (clicked()), this, SLOT(onRemoveElement()));
529   connect(SetFirstButton, SIGNAL( clicked() ), this, SLOT( onSetFirst() ) );
530
531   connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
532   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
533   /* to close dialog if study change */
534   connect(mySMESHGUI, SIGNAL (SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
535
536   SetFirstButton->setEnabled(false);
537   buttonOk->setEnabled(false);
538   buttonApply->setEnabled(false);
539
540   // Init Mesh field from selection
541   SelectionIntoArgument();
542
543   myHelpFileName = "merging_elements_page.html";
544 }
545
546 //=================================================================================
547 // function : FindGravityCenter()
548 // purpose  :
549 //=================================================================================
550 void SMESHGUI_EditMeshDlg::FindGravityCenter(TColStd_MapOfInteger & theElemsIdMap, 
551                                              std::list< gp_XYZ > & theGrCentersXYZ)
552 {
553   if (!myActor)
554     return;
555
556   SMDS_Mesh* aMesh = 0;
557   aMesh = myActor->GetObject()->GetMesh();
558   if (!aMesh)
559     return;
560
561   int nbNodes;
562
563   TColStd_MapIteratorOfMapOfInteger idIter( theElemsIdMap );
564   for( ; idIter.More(); idIter.Next() ) {
565     const SMDS_MeshElement* anElem = aMesh->FindElement(idIter.Key());
566     if ( !anElem )
567       continue;
568
569     gp_XYZ anXYZ(0., 0., 0.);
570     SMDS_ElemIteratorPtr nodeIt = anElem->nodesIterator();
571     for ( nbNodes = 0; nodeIt->more(); nbNodes++ ) {
572       const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
573       anXYZ.Add( gp_XYZ( node->X(), node->Y(), node->Z() ) );
574     }
575     anXYZ.Divide( nbNodes );
576     
577     theGrCentersXYZ.push_back( anXYZ );
578   }
579 }
580
581 //=================================================================================
582 // function : ClickOnApply()
583 // purpose  :
584 //=================================================================================
585 bool SMESHGUI_EditMeshDlg::ClickOnApply()
586 {
587   if (mySMESHGUI->isActiveStudyLocked() || myMesh->_is_nil())
588     return false;
589
590   try {
591     SUIT_OverrideCursor aWaitCursor;
592     SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
593
594     SMESH::long_array_var anIds = new SMESH::long_array;
595     SMESH::array_of_long_array_var aGroupsOfElements = new SMESH::array_of_long_array;
596
597     aGroupsOfElements->length(ListCoincident->count());
598
599     int anArrayNum = 0;
600     for (int i = 0; i < ListCoincident->count(); i++) {
601       QStringList aListIds = ListCoincident->item(i)->text().split(" ", QString::SkipEmptyParts);
602
603       anIds->length(aListIds.count());
604       for (int i = 0; i < aListIds.count(); i++)
605         anIds[i] = aListIds[i].toInt();
606
607       aGroupsOfElements[anArrayNum++] = anIds.inout();
608     }
609
610     if( myAction == 0 )
611       aMeshEditor->MergeNodes (aGroupsOfElements.inout());
612     else
613       aMeshEditor->MergeElements (aGroupsOfElements.inout());
614
615   } catch(...) {
616   }
617   
618   SMESH::UpdateView();
619
620   onDetect();
621   return true;
622 }
623
624 //=================================================================================
625 // function : ClickOnOk()
626 // purpose  :
627 //=================================================================================
628 void SMESHGUI_EditMeshDlg::ClickOnOk()
629 {
630   if (ClickOnApply())
631     ClickOnCancel();
632 }
633
634 //=================================================================================
635 // function : ClickOnCancel()
636 // purpose  :
637 //=================================================================================
638 void SMESHGUI_EditMeshDlg::ClickOnCancel()
639 {
640   myIdPreview->SetPointsLabeled(false);
641   mySelectionMgr->clearFilters();
642   //mySelectionMgr->clearSelected();
643   SMESH::SetPointRepresentation(false);
644   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
645     aViewWindow->SetSelectionMode(ActorSelection);
646   disconnect(mySelectionMgr, 0, this, 0);
647   mySMESHGUI->ResetState();
648   reject();
649 }
650
651 //=================================================================================
652 // function : ClickOnHelp()
653 // purpose  :
654 //=================================================================================
655 void SMESHGUI_EditMeshDlg::ClickOnHelp()
656 {
657   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
658   if (app) 
659     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
660   else {
661     QString platform;
662 #ifdef WIN32
663     platform = "winapplication";
664 #else
665     platform = "application";
666 #endif
667     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
668                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
669                              arg(app->resourceMgr()->stringValue("ExternalBrowser", 
670                                                                  platform)).
671                              arg(myHelpFileName));
672   }
673 }
674
675 //=================================================================================
676 // function : onEditGroup()
677 // purpose  :
678 //=================================================================================
679 void SMESHGUI_EditMeshDlg::onEditGroup()
680 {
681   QList<QListWidgetItem*> selItems = ListCoincident->selectedItems();
682   if ( selItems.count() != 1 ) {
683     ListEdit->clear();
684     return;
685   }
686
687   QStringList aNewIds;
688
689   for (int i = 0; i < ListEdit->count(); i++ )
690     aNewIds.append(ListEdit->item(i)->text());
691
692   ListCoincident->clearSelection();
693   selItems.first()->setText(aNewIds.join(" "));
694   selItems.first()->setSelected(true);
695 }
696
697 //=================================================================================
698 // function : updateControls()
699 // purpose  :
700 //=================================================================================
701 void SMESHGUI_EditMeshDlg::updateControls()
702 {
703   if (ListEdit->count() == 0)
704     SetFirstButton->setEnabled(false);
705   bool enable = !(myMesh->_is_nil()) && ListCoincident->count();
706   buttonOk->setEnabled(enable);
707   buttonApply->setEnabled(enable);
708 }
709
710 //=================================================================================
711 // function : onDetect()
712 // purpose  :
713 //=================================================================================
714 void SMESHGUI_EditMeshDlg::onDetect()
715 {
716   if ( myMesh->_is_nil() || LineEditMesh->text().isEmpty() )
717     return;
718
719   try {
720     SUIT_OverrideCursor aWaitCursor;
721     SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
722
723     ListCoincident->clear();
724     ListEdit->clear();
725
726     SMESH::array_of_long_array_var aGroupsArray;
727
728     switch (myAction) {
729     case 0 :
730       if(!mySubMeshOrGroup->_is_nil())
731         aMeshEditor->FindCoincidentNodesOnPart(mySubMeshOrGroup, SpinBoxTolerance->GetValue(), aGroupsArray);
732       else
733         aMeshEditor->FindCoincidentNodes(SpinBoxTolerance->GetValue(), aGroupsArray);
734       break;
735     case 1 :
736       if(!mySubMeshOrGroup->_is_nil())
737         aMeshEditor->FindEqualElements(mySubMeshOrGroup, aGroupsArray);
738       else
739         aMeshEditor->FindEqualElements(myMesh, aGroupsArray);
740       break;
741     }
742     
743     for (int i = 0; i < aGroupsArray->length(); i++) {
744       SMESH::long_array& aGroup = aGroupsArray[i];
745
746       QStringList anIDs;
747       for (int j = 0; j < aGroup.length(); j++)
748         anIDs.append(QString::number(aGroup[j]));
749
750       ListCoincident->addItem(anIDs.join(" "));
751     }
752    } catch(...) {
753   }
754
755   ListCoincident->selectAll();
756   updateControls();
757 }
758
759 //=================================================================================
760 // function : onSelectGroup()
761 // purpose  :
762 //=================================================================================
763 void SMESHGUI_EditMeshDlg::onSelectGroup()
764 {
765   if (myIsBusy || !myActor)
766     return;
767   myEditCurrentArgument = (QWidget*)ListCoincident;
768
769   ListEdit->clear();
770   
771   TColStd_MapOfInteger anIndices;
772   QList<QListWidgetItem*> selItems = ListCoincident->selectedItems();
773   QListWidgetItem* anItem;
774   QStringList aListIds;
775
776   ListEdit->clear();
777
778   foreach(anItem, selItems) {
779     aListIds = anItem->text().split(" ", QString::SkipEmptyParts);
780     for (int i = 0; i < aListIds.count(); i++)
781       anIndices.Add(aListIds[i].toInt());
782   }
783   
784   if (selItems.count() == 1) {
785     ListEdit->addItems(aListIds);
786     ListEdit->selectAll();
787   }
788
789   mySelector->AddOrRemoveIndex(myActor->getIO(), anIndices, false);
790   SALOME_ListIO aList;
791   aList.Append(myActor->getIO());
792   mySelectionMgr->setSelectedObjects(aList,false);
793   
794   if (myAction == 0) {
795     myIdPreview->SetPointsData(myActor->GetObject()->GetMesh(), anIndices);
796     myIdPreview->SetPointsLabeled(!anIndices.IsEmpty(), myActor->GetVisibility());
797   }
798   else {
799     std::list< gp_XYZ > aGrCentersXYZ;
800     FindGravityCenter(anIndices, aGrCentersXYZ);
801     myIdPreview->SetElemsData( anIndices, aGrCentersXYZ);
802     myIdPreview->SetPointsLabeled(!anIndices.IsEmpty(), myActor->GetVisibility());
803   }
804
805   updateControls();
806 }
807
808 //=================================================================================
809 // function : onSelectAll()
810 // purpose  :
811 //=================================================================================
812 void SMESHGUI_EditMeshDlg::onSelectAll (bool isToggled)
813 {
814   if ( isToggled )
815     ListCoincident->selectAll();
816   else
817     ListCoincident->clearSelection();
818 }
819
820 //=================================================================================
821 // function : onSelectElementFromGroup()
822 // purpose  :
823 //=================================================================================
824 void SMESHGUI_EditMeshDlg::onSelectElementFromGroup()
825 {
826   if (myIsBusy || !myActor)
827     return;
828
829   TColStd_MapOfInteger anIndices;
830   QList<QListWidgetItem*> selItems = ListEdit->selectedItems();
831   QListWidgetItem* anItem;
832
833   foreach(anItem, selItems)
834     anIndices.Add(anItem->text().toInt());
835
836   SetFirstButton->setEnabled(selItems.count() == 1);
837
838   mySelector->AddOrRemoveIndex(myActor->getIO(), anIndices, false);
839   SALOME_ListIO aList;
840   aList.Append(myActor->getIO());
841   mySelectionMgr->setSelectedObjects(aList);
842
843   if (myAction == 0) {
844     myIdPreview->SetPointsData(myActor->GetObject()->GetMesh(), anIndices);
845     myIdPreview->SetPointsLabeled(!anIndices.IsEmpty(), myActor->GetVisibility());
846   }
847   else {
848     std::list< gp_XYZ > aGrCentersXYZ;
849     FindGravityCenter(anIndices, aGrCentersXYZ);
850     myIdPreview->SetElemsData(anIndices, aGrCentersXYZ);
851     myIdPreview->SetPointsLabeled(!anIndices.IsEmpty(), myActor->GetVisibility());
852   }
853 }
854
855 //=================================================================================
856 // function : onAddGroup()
857 // purpose  :
858 //=================================================================================
859 void SMESHGUI_EditMeshDlg::onAddGroup()
860 {
861   if ( myMesh->_is_nil() || LineEditMesh->text().isEmpty() )
862     return;
863
864   QString anIDs = "";
865   SMESH::GetNameOfSelectedNodes(mySelector, myActor->getIO(), anIDs);
866   
867   ListCoincident->clearSelection();
868   ListCoincident->addItem(anIDs);
869   int nbGroups = ListCoincident->count();
870   if (nbGroups) {
871     ListCoincident->setCurrentRow(nbGroups-1);
872     ListCoincident->item(nbGroups-1)->setSelected(true);
873   }
874   else {
875     // VSR ? this code seems to be never executed!!!
876     ListCoincident->setCurrentRow(0);
877     //ListCoincident->setSelected(0, true); // VSR: no items - no selection
878   }
879
880   updateControls();
881 }
882
883 //=================================================================================
884 // function : onRemoveGroup()
885 // purpose  :
886 //=================================================================================
887 void SMESHGUI_EditMeshDlg::onRemoveGroup()
888 {
889   if (myEditCurrentArgument != (QWidget*)ListCoincident)
890     return;
891   myIsBusy = true;
892
893   QList<QListWidgetItem*> selItems = ListCoincident->selectedItems();
894   QListWidgetItem* anItem;
895
896   foreach(anItem, selItems)
897     delete anItem;
898
899   ListEdit->clear();
900   updateControls();
901
902   myIsBusy = false;
903 }
904
905 //=================================================================================
906 // function : onAddElement()
907 // purpose  :
908 //=================================================================================
909 void SMESHGUI_EditMeshDlg::onAddElement()
910 {
911   if (!myActor)
912     return;
913   myIsBusy = true;
914
915   QString aListStr = "";
916   int aNbNnodes = 0;
917
918   aNbNnodes = SMESH::GetNameOfSelectedNodes(mySelector, myActor->getIO(), aListStr);
919   if (aNbNnodes < 1)
920     return;
921
922   QStringList aNodes = aListStr.split(" ", QString::SkipEmptyParts);
923
924   for (QStringList::iterator it = aNodes.begin(); it != aNodes.end(); ++it) {
925     QList<QListWidgetItem*> found = ListEdit->findItems(*it, Qt::MatchExactly);
926     if ( found.count() == 0 ) {
927       QListWidgetItem* anItem = new QListWidgetItem(*it);
928       ListEdit->addItem(anItem);
929       anItem->setSelected(true);
930     }
931     else {
932       QListWidgetItem* anItem;
933       foreach(anItem, found) anItem->setSelected(true);
934     }
935   }
936
937   myIsBusy = false;
938   onEditGroup();
939 }
940
941 //=================================================================================
942 // function : onRemoveElement()
943 // purpose  :
944 //=================================================================================
945 void SMESHGUI_EditMeshDlg::onRemoveElement()
946 {
947   if (myEditCurrentArgument != (QWidget*)ListCoincident)
948     return;
949   myIsBusy = true;
950
951   QList<QListWidgetItem*> selItems = ListEdit->selectedItems();
952   QListWidgetItem* anItem;
953
954   foreach(anItem, selItems)
955     delete anItem;
956   
957   myIsBusy = false;
958   onEditGroup();
959 }
960
961 //=================================================================================
962 // function : onSetFirst()
963 // purpose  :
964 //=================================================================================
965 void SMESHGUI_EditMeshDlg::onSetFirst()
966 {
967   if (myEditCurrentArgument != (QWidget*)ListCoincident)
968     return;
969   myIsBusy = true;
970   
971   QList<QListWidgetItem*> selItems = ListEdit->selectedItems();
972   QListWidgetItem* anItem;
973   
974   foreach(anItem, selItems) {
975     ListEdit->takeItem(ListEdit->row(anItem));
976     ListEdit->insertItem(0, anItem);
977   }
978
979   myIsBusy = false;
980   onEditGroup();
981 }
982
983 //=================================================================================
984 // function : SetEditCurrentArgument()
985 // purpose  :
986 //=================================================================================
987 void SMESHGUI_EditMeshDlg::SetEditCurrentArgument()
988 {
989   QPushButton* send = (QPushButton*)sender();
990
991   disconnect(mySelectionMgr, 0, this, 0);
992   mySelectionMgr->clearSelected();
993   mySelectionMgr->clearFilters();
994
995   if (send == SelectMeshButton) {
996     myEditCurrentArgument = (QWidget*)LineEditMesh;
997     SMESH::SetPointRepresentation(false);
998     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
999       aViewWindow->SetSelectionMode(ActorSelection);
1000     mySelectionMgr->installFilter(myMeshOrSubMeshOrGroupFilter);
1001   }
1002
1003   myEditCurrentArgument->setFocus();
1004   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
1005   SelectionIntoArgument();
1006 }
1007
1008 //=================================================================================
1009 // function : SelectionIntoArgument()
1010 // purpose  : Called when selection as changed or other case
1011 //=================================================================================
1012 void SMESHGUI_EditMeshDlg::SelectionIntoArgument()
1013 {
1014   if (myEditCurrentArgument == (QWidget*)LineEditMesh) {
1015     QString aString = "";
1016     LineEditMesh->setText(aString);
1017     
1018     ListCoincident->clear();
1019     ListEdit->clear();
1020     myActor = 0;
1021     
1022     int nbSel = SMESH::GetNameOfSelectedIObjects(mySelectionMgr, aString);
1023     if (nbSel != 1)
1024       return;
1025
1026     SALOME_ListIO aList;
1027     mySelectionMgr->selectedObjects(aList, SVTK_Viewer::Type());
1028     
1029     Handle(SALOME_InteractiveObject) IO = aList.First();
1030     myMesh = SMESH::GetMeshByIO(IO);
1031     
1032     if (myMesh->_is_nil())
1033       return;
1034     
1035     myActor = SMESH::FindActorByEntry(IO->getEntry());
1036     if (!myActor)
1037       myActor = SMESH::FindActorByObject(myMesh);
1038     if(!myActor)
1039       return;
1040     
1041     mySubMeshOrGroup = SMESH::SMESH_IDSource::_nil();
1042     
1043     if ((!SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(IO)->_is_nil() || //SUBMESH OR GROUP
1044          !SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(IO)->_is_nil()) &&
1045         !SMESH::IObjectToInterface<SMESH::SMESH_IDSource>(IO)->_is_nil())
1046       mySubMeshOrGroup = SMESH::IObjectToInterface<SMESH::SMESH_IDSource>(IO);
1047      
1048     LineEditMesh->setText(aString);
1049
1050     if (myAction == 0) {
1051       SMESH::SetPointRepresentation(true);
1052       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
1053         aViewWindow->SetSelectionMode(NodeSelection);
1054     }
1055     else
1056       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
1057         aViewWindow->SetSelectionMode(CellSelection);
1058   }
1059 }
1060
1061 //=================================================================================
1062 // function : DeactivateActiveDialog()
1063 // purpose  :
1064 //=================================================================================
1065 void SMESHGUI_EditMeshDlg::DeactivateActiveDialog()
1066 {
1067   if (GroupConstructors->isEnabled()) {
1068     GroupConstructors->setEnabled(false);
1069     GroupMesh->setEnabled(false);
1070     GroupCoincident->setEnabled(false);
1071     GroupEdit->setEnabled(false);
1072     GroupButtons->setEnabled(false);
1073     mySMESHGUI->ResetState();
1074     mySMESHGUI->SetActiveDialogBox(0);
1075   }
1076
1077   mySelectionMgr->clearSelected();
1078   disconnect(mySelectionMgr, 0, this, 0);
1079 }
1080
1081 //=================================================================================
1082 // function : ActivateThisDialog()
1083 // purpose  :
1084 //=================================================================================
1085 void SMESHGUI_EditMeshDlg::ActivateThisDialog()
1086 {
1087   /* Emit a signal to deactivate the active dialog */
1088   mySMESHGUI->EmitSignalDeactivateDialog();
1089   GroupConstructors->setEnabled(true);
1090   GroupMesh->setEnabled(true);
1091   GroupCoincident->setEnabled(true);
1092   GroupEdit->setEnabled(true);
1093   GroupButtons->setEnabled(true);
1094
1095   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
1096   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
1097   SelectionIntoArgument();
1098 }
1099
1100 //=================================================================================
1101 // function : enterEvent()
1102 // purpose  :
1103 //=================================================================================
1104 void SMESHGUI_EditMeshDlg::enterEvent(QEvent*)
1105 {
1106   if (!GroupConstructors->isEnabled())
1107     ActivateThisDialog();
1108 }
1109
1110 //=================================================================================
1111 // function : closeEvent()
1112 // purpose  :
1113 //=================================================================================
1114 void SMESHGUI_EditMeshDlg::closeEvent(QCloseEvent*)
1115 {
1116   /* same than click on cancel button */
1117   ClickOnCancel();
1118 }
1119
1120 //=======================================================================
1121 //function : hideEvent
1122 //purpose  : caused by ESC key
1123 //=======================================================================
1124 void SMESHGUI_EditMeshDlg::hideEvent (QHideEvent *)
1125 {
1126   if (!isMinimized())
1127     ClickOnCancel();
1128 }
1129
1130 //=================================================================================
1131 // function : keyPressEvent()
1132 // purpose  :
1133 //=================================================================================
1134 void SMESHGUI_EditMeshDlg::keyPressEvent( QKeyEvent* e)
1135 {
1136   QDialog::keyPressEvent( e );
1137   if ( e->isAccepted() )
1138     return;
1139
1140   if ( e->key() == Qt::Key_F1 ) {
1141     e->accept();
1142     ClickOnHelp();
1143   }
1144 }