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