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