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