Salome HOME
Small correction
[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   SMESH::SetPointRepresentation(false);
642   disconnect(mySelectionMgr, 0, this, 0);
643   disconnect(mySMESHGUI, 0, this, 0);
644   mySMESHGUI->ResetState();
645
646   mySelectionMgr->clearFilters();
647   //mySelectionMgr->clearSelected();
648
649   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
650     aViewWindow->SetSelectionMode(ActorSelection);
651
652   reject();
653 }
654
655 //=================================================================================
656 // function : ClickOnHelp()
657 // purpose  :
658 //=================================================================================
659 void SMESHGUI_EditMeshDlg::ClickOnHelp()
660 {
661   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
662   if (app) 
663     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
664   else {
665     QString platform;
666 #ifdef WIN32
667     platform = "winapplication";
668 #else
669     platform = "application";
670 #endif
671     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
672                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
673                              arg(app->resourceMgr()->stringValue("ExternalBrowser", 
674                                                                  platform)).
675                              arg(myHelpFileName));
676   }
677 }
678
679 //=================================================================================
680 // function : onEditGroup()
681 // purpose  :
682 //=================================================================================
683 void SMESHGUI_EditMeshDlg::onEditGroup()
684 {
685   QList<QListWidgetItem*> selItems = ListCoincident->selectedItems();
686   if ( selItems.count() != 1 ) {
687     ListEdit->clear();
688     return;
689   }
690
691   QStringList aNewIds;
692
693   for (int i = 0; i < ListEdit->count(); i++ )
694     aNewIds.append(ListEdit->item(i)->text());
695
696   ListCoincident->clearSelection();
697   selItems.first()->setText(aNewIds.join(" "));
698   selItems.first()->setSelected(true);
699 }
700
701 //=================================================================================
702 // function : updateControls()
703 // purpose  :
704 //=================================================================================
705 void SMESHGUI_EditMeshDlg::updateControls()
706 {
707   if (ListEdit->count() == 0)
708     SetFirstButton->setEnabled(false);
709   bool enable = !(myMesh->_is_nil()) && ListCoincident->count();
710   buttonOk->setEnabled(enable);
711   buttonApply->setEnabled(enable);
712 }
713
714 //=================================================================================
715 // function : onDetect()
716 // purpose  :
717 //=================================================================================
718 void SMESHGUI_EditMeshDlg::onDetect()
719 {
720   if ( myMesh->_is_nil() || LineEditMesh->text().isEmpty() )
721     return;
722
723   try {
724     SUIT_OverrideCursor aWaitCursor;
725     SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
726
727     ListCoincident->clear();
728     ListEdit->clear();
729
730     SMESH::array_of_long_array_var aGroupsArray;
731
732     switch (myAction) {
733     case 0 :
734       if(!mySubMeshOrGroup->_is_nil())
735         aMeshEditor->FindCoincidentNodesOnPart(mySubMeshOrGroup, SpinBoxTolerance->GetValue(), aGroupsArray);
736       else
737         aMeshEditor->FindCoincidentNodes(SpinBoxTolerance->GetValue(), aGroupsArray);
738       break;
739     case 1 :
740       if(!mySubMeshOrGroup->_is_nil())
741         aMeshEditor->FindEqualElements(mySubMeshOrGroup, aGroupsArray);
742       else
743         aMeshEditor->FindEqualElements(myMesh, aGroupsArray);
744       break;
745     }
746     
747     for (int i = 0; i < aGroupsArray->length(); i++) {
748       SMESH::long_array& aGroup = aGroupsArray[i];
749
750       QStringList anIDs;
751       for (int j = 0; j < aGroup.length(); j++)
752         anIDs.append(QString::number(aGroup[j]));
753
754       ListCoincident->addItem(anIDs.join(" "));
755     }
756    } catch(...) {
757   }
758
759   ListCoincident->selectAll();
760   updateControls();
761 }
762
763 //=================================================================================
764 // function : onSelectGroup()
765 // purpose  :
766 //=================================================================================
767 void SMESHGUI_EditMeshDlg::onSelectGroup()
768 {
769   if (myIsBusy || !myActor)
770     return;
771   myEditCurrentArgument = (QWidget*)ListCoincident;
772
773   ListEdit->clear();
774   
775   TColStd_MapOfInteger anIndices;
776   QList<QListWidgetItem*> selItems = ListCoincident->selectedItems();
777   QListWidgetItem* anItem;
778   QStringList aListIds;
779
780   ListEdit->clear();
781
782   foreach(anItem, selItems) {
783     aListIds = anItem->text().split(" ", QString::SkipEmptyParts);
784     for (int i = 0; i < aListIds.count(); i++)
785       anIndices.Add(aListIds[i].toInt());
786   }
787   
788   if (selItems.count() == 1) {
789     ListEdit->addItems(aListIds);
790     ListEdit->selectAll();
791   }
792
793   mySelector->AddOrRemoveIndex(myActor->getIO(), anIndices, false);
794   SALOME_ListIO aList;
795   aList.Append(myActor->getIO());
796   mySelectionMgr->setSelectedObjects(aList,false);
797   
798   if (myAction == 0) {
799     myIdPreview->SetPointsData(myActor->GetObject()->GetMesh(), anIndices);
800     myIdPreview->SetPointsLabeled(!anIndices.IsEmpty(), myActor->GetVisibility());
801   }
802   else {
803     std::list< gp_XYZ > aGrCentersXYZ;
804     FindGravityCenter(anIndices, aGrCentersXYZ);
805     myIdPreview->SetElemsData( anIndices, aGrCentersXYZ);
806     myIdPreview->SetPointsLabeled(!anIndices.IsEmpty(), myActor->GetVisibility());
807   }
808
809   updateControls();
810 }
811
812 //=================================================================================
813 // function : onSelectAll()
814 // purpose  :
815 //=================================================================================
816 void SMESHGUI_EditMeshDlg::onSelectAll (bool isToggled)
817 {
818   if ( isToggled )
819     ListCoincident->selectAll();
820   else
821     ListCoincident->clearSelection();
822 }
823
824 //=================================================================================
825 // function : onSelectElementFromGroup()
826 // purpose  :
827 //=================================================================================
828 void SMESHGUI_EditMeshDlg::onSelectElementFromGroup()
829 {
830   if (myIsBusy || !myActor)
831     return;
832
833   TColStd_MapOfInteger anIndices;
834   QList<QListWidgetItem*> selItems = ListEdit->selectedItems();
835   QListWidgetItem* anItem;
836
837   foreach(anItem, selItems)
838     anIndices.Add(anItem->text().toInt());
839
840   SetFirstButton->setEnabled(selItems.count() == 1);
841
842   mySelector->AddOrRemoveIndex(myActor->getIO(), anIndices, false);
843   SALOME_ListIO aList;
844   aList.Append(myActor->getIO());
845   mySelectionMgr->setSelectedObjects(aList);
846
847   if (myAction == 0) {
848     myIdPreview->SetPointsData(myActor->GetObject()->GetMesh(), anIndices);
849     myIdPreview->SetPointsLabeled(!anIndices.IsEmpty(), myActor->GetVisibility());
850   }
851   else {
852     std::list< gp_XYZ > aGrCentersXYZ;
853     FindGravityCenter(anIndices, aGrCentersXYZ);
854     myIdPreview->SetElemsData(anIndices, aGrCentersXYZ);
855     myIdPreview->SetPointsLabeled(!anIndices.IsEmpty(), myActor->GetVisibility());
856   }
857 }
858
859 //=================================================================================
860 // function : onAddGroup()
861 // purpose  :
862 //=================================================================================
863 void SMESHGUI_EditMeshDlg::onAddGroup()
864 {
865   if ( myMesh->_is_nil() || LineEditMesh->text().isEmpty() )
866     return;
867
868   QString anIDs = "";
869   int aNbElements = 0;
870   aNbElements = SMESH::GetNameOfSelectedNodes(mySelector, myActor->getIO(), anIDs);
871
872   if (aNbElements < 1)
873     return;
874   
875   ListCoincident->clearSelection();
876   ListCoincident->addItem(anIDs);
877   int nbGroups = ListCoincident->count();
878   if (nbGroups) {
879     ListCoincident->setCurrentRow(nbGroups-1);
880     ListCoincident->item(nbGroups-1)->setSelected(true);
881   }
882   else {
883     // VSR ? this code seems to be never executed!!!
884     ListCoincident->setCurrentRow(0);
885     //ListCoincident->setSelected(0, true); // VSR: no items - no selection
886   }
887
888   updateControls();
889 }
890
891 //=================================================================================
892 // function : onRemoveGroup()
893 // purpose  :
894 //=================================================================================
895 void SMESHGUI_EditMeshDlg::onRemoveGroup()
896 {
897   if (myEditCurrentArgument != (QWidget*)ListCoincident)
898     return;
899   myIsBusy = true;
900
901   QList<QListWidgetItem*> selItems = ListCoincident->selectedItems();
902   QListWidgetItem* anItem;
903
904   foreach(anItem, selItems)
905     delete anItem;
906
907   ListEdit->clear();
908   updateControls();
909
910   myIsBusy = false;
911 }
912
913 //=================================================================================
914 // function : onAddElement()
915 // purpose  :
916 //=================================================================================
917 void SMESHGUI_EditMeshDlg::onAddElement()
918 {
919   if (!myActor)
920     return;
921   myIsBusy = true;
922
923   QString aListStr = "";
924   int aNbNnodes = 0;
925
926   aNbNnodes = SMESH::GetNameOfSelectedNodes(mySelector, myActor->getIO(), aListStr);
927   if (aNbNnodes < 1)
928     return;
929
930   QStringList aNodes = aListStr.split(" ", QString::SkipEmptyParts);
931
932   for (QStringList::iterator it = aNodes.begin(); it != aNodes.end(); ++it) {
933     QList<QListWidgetItem*> found = ListEdit->findItems(*it, Qt::MatchExactly);
934     if ( found.count() == 0 ) {
935       QListWidgetItem* anItem = new QListWidgetItem(*it);
936       ListEdit->addItem(anItem);
937       anItem->setSelected(true);
938     }
939     else {
940       QListWidgetItem* anItem;
941       foreach(anItem, found) anItem->setSelected(true);
942     }
943   }
944
945   myIsBusy = false;
946   onEditGroup();
947 }
948
949 //=================================================================================
950 // function : onRemoveElement()
951 // purpose  :
952 //=================================================================================
953 void SMESHGUI_EditMeshDlg::onRemoveElement()
954 {
955   if (myEditCurrentArgument != (QWidget*)ListCoincident)
956     return;
957   myIsBusy = true;
958
959   QList<QListWidgetItem*> selItems = ListEdit->selectedItems();
960   QListWidgetItem* anItem;
961
962   foreach(anItem, selItems)
963     delete anItem;
964   
965   myIsBusy = false;
966   onEditGroup();
967 }
968
969 //=================================================================================
970 // function : onSetFirst()
971 // purpose  :
972 //=================================================================================
973 void SMESHGUI_EditMeshDlg::onSetFirst()
974 {
975   if (myEditCurrentArgument != (QWidget*)ListCoincident)
976     return;
977   myIsBusy = true;
978   
979   QList<QListWidgetItem*> selItems = ListEdit->selectedItems();
980   QListWidgetItem* anItem;
981   
982   foreach(anItem, selItems) {
983     ListEdit->takeItem(ListEdit->row(anItem));
984     ListEdit->insertItem(0, anItem);
985   }
986
987   myIsBusy = false;
988   onEditGroup();
989 }
990
991 //=================================================================================
992 // function : SetEditCurrentArgument()
993 // purpose  :
994 //=================================================================================
995 void SMESHGUI_EditMeshDlg::SetEditCurrentArgument()
996 {
997   QPushButton* send = (QPushButton*)sender();
998
999   disconnect(mySelectionMgr, 0, this, 0);
1000   mySelectionMgr->clearSelected();
1001   mySelectionMgr->clearFilters();
1002
1003   if (send == SelectMeshButton) {
1004     myEditCurrentArgument = (QWidget*)LineEditMesh;
1005     SMESH::SetPointRepresentation(false);
1006     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
1007       aViewWindow->SetSelectionMode(ActorSelection);
1008     mySelectionMgr->installFilter(myMeshOrSubMeshOrGroupFilter);
1009   }
1010
1011   myEditCurrentArgument->setFocus();
1012   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
1013   SelectionIntoArgument();
1014 }
1015
1016 //=================================================================================
1017 // function : SelectionIntoArgument()
1018 // purpose  : Called when selection as changed or other case
1019 //=================================================================================
1020 void SMESHGUI_EditMeshDlg::SelectionIntoArgument()
1021 {
1022   if (myEditCurrentArgument == (QWidget*)LineEditMesh) {
1023     QString aString = "";
1024     LineEditMesh->setText(aString);
1025     
1026     ListCoincident->clear();
1027     ListEdit->clear();
1028     myActor = 0;
1029     
1030     int nbSel = SMESH::GetNameOfSelectedIObjects(mySelectionMgr, aString);
1031     if (nbSel != 1)
1032       return;
1033
1034     SALOME_ListIO aList;
1035     mySelectionMgr->selectedObjects(aList, SVTK_Viewer::Type());
1036     
1037     Handle(SALOME_InteractiveObject) IO = aList.First();
1038     myMesh = SMESH::GetMeshByIO(IO);
1039     
1040     if (myMesh->_is_nil())
1041       return;
1042     
1043     myActor = SMESH::FindActorByEntry(IO->getEntry());
1044     if (!myActor)
1045       myActor = SMESH::FindActorByObject(myMesh);
1046     if(!myActor)
1047       return;
1048     
1049     mySubMeshOrGroup = SMESH::SMESH_IDSource::_nil();
1050     
1051     if ((!SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(IO)->_is_nil() || //SUBMESH OR GROUP
1052          !SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(IO)->_is_nil()) &&
1053         !SMESH::IObjectToInterface<SMESH::SMESH_IDSource>(IO)->_is_nil())
1054       mySubMeshOrGroup = SMESH::IObjectToInterface<SMESH::SMESH_IDSource>(IO);
1055      
1056     LineEditMesh->setText(aString);
1057
1058     if (myAction == 0) {
1059       SMESH::SetPointRepresentation(true);
1060       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
1061         aViewWindow->SetSelectionMode(NodeSelection);
1062     }
1063     else
1064       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
1065         aViewWindow->SetSelectionMode(CellSelection);
1066   }
1067 }
1068
1069 //=================================================================================
1070 // function : DeactivateActiveDialog()
1071 // purpose  :
1072 //=================================================================================
1073 void SMESHGUI_EditMeshDlg::DeactivateActiveDialog()
1074 {
1075   if (GroupConstructors->isEnabled()) {
1076     GroupConstructors->setEnabled(false);
1077     GroupMesh->setEnabled(false);
1078     GroupCoincident->setEnabled(false);
1079     GroupEdit->setEnabled(false);
1080     GroupButtons->setEnabled(false);
1081     mySMESHGUI->ResetState();
1082     mySMESHGUI->SetActiveDialogBox(0);
1083   }
1084
1085   mySelectionMgr->clearSelected();
1086   disconnect(mySelectionMgr, 0, this, 0);
1087 }
1088
1089 //=================================================================================
1090 // function : ActivateThisDialog()
1091 // purpose  :
1092 //=================================================================================
1093 void SMESHGUI_EditMeshDlg::ActivateThisDialog()
1094 {
1095   /* Emit a signal to deactivate the active dialog */
1096   mySMESHGUI->EmitSignalDeactivateDialog();
1097   GroupConstructors->setEnabled(true);
1098   GroupMesh->setEnabled(true);
1099   GroupCoincident->setEnabled(true);
1100   GroupEdit->setEnabled(true);
1101   GroupButtons->setEnabled(true);
1102
1103   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
1104   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
1105   SelectionIntoArgument();
1106 }
1107
1108 //=================================================================================
1109 // function : enterEvent()
1110 // purpose  :
1111 //=================================================================================
1112 void SMESHGUI_EditMeshDlg::enterEvent(QEvent*)
1113 {
1114   if (!GroupConstructors->isEnabled())
1115     ActivateThisDialog();
1116 }
1117
1118 //=================================================================================
1119 // function : closeEvent()
1120 // purpose  :
1121 //=================================================================================
1122 void SMESHGUI_EditMeshDlg::closeEvent(QCloseEvent*)
1123 {
1124   /* same than click on cancel button */
1125   ClickOnCancel();
1126 }
1127
1128 //=======================================================================
1129 //function : hideEvent
1130 //purpose  : caused by ESC key
1131 //=======================================================================
1132 void SMESHGUI_EditMeshDlg::hideEvent (QHideEvent *)
1133 {
1134   if (!isMinimized())
1135     ClickOnCancel();
1136 }
1137
1138 //=================================================================================
1139 // function : keyPressEvent()
1140 // purpose  :
1141 //=================================================================================
1142 void SMESHGUI_EditMeshDlg::keyPressEvent( QKeyEvent* e)
1143 {
1144   QDialog::keyPressEvent( e );
1145   if ( e->isAccepted() )
1146     return;
1147
1148   if ( e->key() == Qt::Key_F1 ) {
1149     e->accept();
1150     ClickOnHelp();
1151   }
1152 }