Salome HOME
Fix for bug IPAL19438 : Qt4 porting. MESH. For many fields number of digits after...
[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->setSelectionMode(QListWidget::ExtendedSelection);
416
417   AddElemButton = new QPushButton(GroupEdit);
418   AddElemButton->setIcon(IconAdd);
419   RemoveElemButton = new QPushButton(GroupEdit);
420   RemoveElemButton->setIcon(IconRemove);
421   SetFirstButton = new QPushButton(GroupEdit);
422   SetFirstButton->setIcon(QPixmap(IconFirst));
423
424   GroupEditLayout->addWidget(ListEdit,         0, 0, 2, 1);
425   GroupEditLayout->addWidget(AddElemButton,    0, 1);
426   GroupEditLayout->addWidget(RemoveElemButton, 0, 2);
427   GroupEditLayout->addWidget(SetFirstButton,   1, 1, 1, 2);
428
429   /***************************************************************/
430   GroupButtons = new QGroupBox(this);
431   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
432   GroupButtonsLayout->setSpacing(SPACING);
433   GroupButtonsLayout->setMargin(MARGIN);
434
435   buttonOk = new QPushButton(tr("SMESH_BUT_OK"), GroupButtons);
436   buttonOk->setAutoDefault(true);
437   buttonOk->setDefault(true);
438   buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
439   buttonApply->setAutoDefault(true);
440   buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
441   buttonCancel->setAutoDefault(true);
442   buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
443   buttonHelp->setAutoDefault(true);
444
445   GroupButtonsLayout->addWidget(buttonOk);
446   GroupButtonsLayout->addSpacing(10);
447   GroupButtonsLayout->addWidget(buttonApply);
448   GroupButtonsLayout->addSpacing(10);
449   GroupButtonsLayout->addStretch();
450   GroupButtonsLayout->addWidget(buttonCancel);
451   GroupButtonsLayout->addWidget(buttonHelp);
452
453   /***************************************************************/
454   DlgLayout->addWidget(GroupConstructors);
455   DlgLayout->addWidget(GroupMesh);
456   DlgLayout->addWidget(GroupCoincident);
457   DlgLayout->addWidget(GroupEdit);
458   DlgLayout->addWidget(GroupButtons);
459
460   Init(); // Initialisations
461 }
462
463 //=================================================================================
464 // function : ~SMESHGUI_EditMeshDlg()
465 // purpose  : Destroys the object and frees any allocated resources
466 //=================================================================================
467 SMESHGUI_EditMeshDlg::~SMESHGUI_EditMeshDlg()
468 {
469   delete myIdPreview;
470 }
471
472 //=================================================================================
473 // function : Init()
474 // purpose  :
475 //=================================================================================
476 void SMESHGUI_EditMeshDlg::Init()
477 {
478   if (myAction == 0) {
479     SpinBoxTolerance->RangeStepAndValidator(0.0, COORD_MAX, 0.1, 5);
480     SpinBoxTolerance->SetValue(1e-05);
481   }
482
483   RadioButton->setChecked(true);
484
485   myEditCurrentArgument = (QWidget*)LineEditMesh; 
486
487   myActor = 0;
488   mySubMeshOrGroup = SMESH::SMESH_subMesh::_nil();
489
490   mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
491
492   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
493   myIsBusy = false;
494   
495   // Costruction of the logical filter
496   SMESH_TypeFilter* aMeshOrSubMeshFilter = new SMESH_TypeFilter (MESHorSUBMESH);
497   SMESH_TypeFilter* aSmeshGroupFilter    = new SMESH_TypeFilter (GROUP);
498   
499   QList<SUIT_SelectionFilter*> aListOfFilters;
500   if (aMeshOrSubMeshFilter) aListOfFilters.append(aMeshOrSubMeshFilter);
501   if (aSmeshGroupFilter)    aListOfFilters.append(aSmeshGroupFilter);
502
503   myMeshOrSubMeshOrGroupFilter =
504     new SMESH_LogicalFilter (aListOfFilters, SMESH_LogicalFilter::LO_OR);
505   
506   /* signals and slots connections */
507   connect(buttonOk,     SIGNAL(clicked()), this, SLOT(ClickOnOk()));
508   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
509   connect(buttonApply,  SIGNAL(clicked()), this, SLOT(ClickOnApply()));
510   connect(buttonHelp,   SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
511
512   connect(SelectMeshButton, SIGNAL (clicked()), this, SLOT(SetEditCurrentArgument()));
513   connect(DetectButton, SIGNAL (clicked()), this, SLOT(onDetect()));
514   connect(ListCoincident, SIGNAL (itemSelectionChanged()), this, SLOT(onSelectGroup()));
515   connect(AddGroupButton, SIGNAL (clicked()), this, SLOT(onAddGroup()));
516   connect(RemoveGroupButton, SIGNAL (clicked()), this, SLOT(onRemoveGroup()));
517   connect(SelectAllCB, SIGNAL(toggled(bool)), this, SLOT(onSelectAll(bool)));
518   connect(ListEdit, SIGNAL (itemSelectionChanged()), this, SLOT(onSelectElementFromGroup()));
519   connect(AddElemButton, SIGNAL (clicked()), this, SLOT(onAddElement()));
520   connect(RemoveElemButton, SIGNAL (clicked()), this, SLOT(onRemoveElement()));
521   connect(SetFirstButton, SIGNAL( clicked() ), this, SLOT( onSetFirst() ) );
522
523   connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
524   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
525   /* to close dialog if study change */
526   connect(mySMESHGUI, SIGNAL (SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
527
528   SetFirstButton->setEnabled(false);
529   buttonOk->setEnabled(false);
530   buttonApply->setEnabled(false);
531
532   // Init Mesh field from selection
533   SelectionIntoArgument();
534
535   myHelpFileName = "merging_elements_page.html";
536 }
537
538 //=================================================================================
539 // function : FindGravityCenter()
540 // purpose  :
541 //=================================================================================
542 void SMESHGUI_EditMeshDlg::FindGravityCenter(TColStd_MapOfInteger & theElemsIdMap, 
543                                              std::list< gp_XYZ > & theGrCentersXYZ)
544 {
545   if (!myActor)
546     return;
547
548   SMDS_Mesh* aMesh = 0;
549   aMesh = myActor->GetObject()->GetMesh();
550   if (!aMesh)
551     return;
552
553   int nbNodes;
554
555   TColStd_MapIteratorOfMapOfInteger idIter( theElemsIdMap );
556   for( ; idIter.More(); idIter.Next() ) {
557     const SMDS_MeshElement* anElem = aMesh->FindElement(idIter.Key());
558     if ( !anElem )
559       continue;
560
561     gp_XYZ anXYZ(0., 0., 0.);
562     SMDS_ElemIteratorPtr nodeIt = anElem->nodesIterator();
563     for ( nbNodes = 0; nodeIt->more(); nbNodes++ ) {
564       const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
565       anXYZ.Add( gp_XYZ( node->X(), node->Y(), node->Z() ) );
566     }
567     anXYZ.Divide( nbNodes );
568     
569     theGrCentersXYZ.push_back( anXYZ );
570   }
571 }
572
573 //=================================================================================
574 // function : ClickOnApply()
575 // purpose  :
576 //=================================================================================
577 bool SMESHGUI_EditMeshDlg::ClickOnApply()
578 {
579   if (mySMESHGUI->isActiveStudyLocked() || myMesh->_is_nil())
580     return false;
581
582   try {
583     SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
584
585     QApplication::setOverrideCursor(Qt::WaitCursor);
586
587     SMESH::long_array_var anIds = new SMESH::long_array;
588     SMESH::array_of_long_array_var aGroupsOfElements = new SMESH::array_of_long_array;
589
590     aGroupsOfElements->length(ListCoincident->count());
591
592     int anArrayNum = 0;
593     for (int i = 0; i < ListCoincident->count(); i++) {
594       QStringList aListIds = ListCoincident->item(i)->text().split(" ", QString::SkipEmptyParts);
595
596       anIds->length(aListIds.count());
597       for (int i = 0; i < aListIds.count(); i++)
598         anIds[i] = aListIds[i].toInt();
599
600       aGroupsOfElements[anArrayNum++] = anIds.inout();
601     }
602
603     if( myAction == 0 )
604       aMeshEditor->MergeNodes (aGroupsOfElements.inout());
605     else
606       aMeshEditor->MergeElements (aGroupsOfElements.inout());
607
608     QApplication::restoreOverrideCursor();
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->currentItem()->setText(aNewIds.join(" "));
687   ListCoincident->currentItem()->setSelected(true);
688 }
689
690 //=================================================================================
691 // function : updateControls()
692 // purpose  :
693 //=================================================================================
694 void SMESHGUI_EditMeshDlg::updateControls()
695 {
696   if (ListEdit->count() == 0)
697     SetFirstButton->setEnabled(false);
698   bool enable = !(myMesh->_is_nil()) && ListCoincident->count();
699   buttonOk->setEnabled(enable);
700   buttonApply->setEnabled(enable);
701 }
702
703 //=================================================================================
704 // function : onDetect()
705 // purpose  :
706 //=================================================================================
707 void SMESHGUI_EditMeshDlg::onDetect()
708 {
709   if ( myMesh->_is_nil() || LineEditMesh->text().isEmpty() )
710     return;
711
712   try {
713     SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
714
715     QApplication::setOverrideCursor(Qt::WaitCursor);
716     ListCoincident->clear();
717     ListEdit->clear();
718
719     SMESH::array_of_long_array_var aGroupsArray;
720
721     switch (myAction) {
722     case 0 :
723       if(!mySubMeshOrGroup->_is_nil())
724         aMeshEditor->FindCoincidentNodesOnPart(mySubMeshOrGroup, SpinBoxTolerance->GetValue(), aGroupsArray);
725       else
726         aMeshEditor->FindCoincidentNodes(SpinBoxTolerance->GetValue(), aGroupsArray);
727       break;
728     case 1 :
729       if(!mySubMeshOrGroup->_is_nil())
730         aMeshEditor->FindEqualElements(mySubMeshOrGroup, aGroupsArray);
731       else
732         aMeshEditor->FindEqualElements(myMesh, aGroupsArray);
733       break;
734     }
735     
736     for (int i = 0; i < aGroupsArray->length(); i++) {
737       SMESH::long_array& aGroup = aGroupsArray[i];
738
739       QStringList anIDs;
740       for (int j = 0; j < aGroup.length(); j++)
741         anIDs.append(QString::number(aGroup[j]));
742
743       ListCoincident->addItem(anIDs.join(" "));
744     }
745     QApplication::restoreOverrideCursor();
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
1072 //=================================================================================
1073 // function : ActivateThisDialog()
1074 // purpose  :
1075 //=================================================================================
1076 void SMESHGUI_EditMeshDlg::ActivateThisDialog()
1077 {
1078   /* Emit a signal to deactivate the active dialog */
1079   mySMESHGUI->EmitSignalDeactivateDialog();
1080   GroupConstructors->setEnabled(true);
1081   GroupMesh->setEnabled(true);
1082   GroupCoincident->setEnabled(true);
1083   GroupEdit->setEnabled(true);
1084   GroupButtons->setEnabled(true);
1085
1086   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
1087   SelectionIntoArgument();
1088 }
1089
1090 //=================================================================================
1091 // function : enterEvent()
1092 // purpose  :
1093 //=================================================================================
1094 void SMESHGUI_EditMeshDlg::enterEvent(QEvent*)
1095 {
1096   if (!GroupConstructors->isEnabled())
1097     ActivateThisDialog();
1098 }
1099
1100 //=================================================================================
1101 // function : closeEvent()
1102 // purpose  :
1103 //=================================================================================
1104 void SMESHGUI_EditMeshDlg::closeEvent(QCloseEvent*)
1105 {
1106   /* same than click on cancel button */
1107   ClickOnCancel();
1108 }
1109
1110 //=======================================================================
1111 //function : hideEvent
1112 //purpose  : caused by ESC key
1113 //=======================================================================
1114 void SMESHGUI_EditMeshDlg::hideEvent (QHideEvent *)
1115 {
1116   if (!isMinimized())
1117     ClickOnCancel();
1118 }
1119
1120 //=================================================================================
1121 // function : keyPressEvent()
1122 // purpose  :
1123 //=================================================================================
1124 void SMESHGUI_EditMeshDlg::keyPressEvent( QKeyEvent* e)
1125 {
1126   QDialog::keyPressEvent( e );
1127   if ( e->isAccepted() )
1128     return;
1129
1130   if ( e->key() == Qt::Key_F1 ) {
1131     e->accept();
1132     ClickOnHelp();
1133   }
1134 }