Salome HOME
69801bb651cb5085d8b2e7fdd6233523c1078568
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_NodesDlg.cxx
1 //  SMESH SMESHGUI : GUI for SMESH component
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //
24 //  File   : SMESHGUI_NodesDlg.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SMESH
27 //  $Header$
28
29 #include "SMESHGUI_NodesDlg.h"
30
31 #include "SMESHGUI.h"
32 #include "SMESHGUI_SpinBox.h"
33 #include "SMESHGUI_Utils.h"
34 #include "SMESHGUI_VTKUtils.h"
35 #include "SMESHGUI_MeshUtils.h"
36
37 #include "SMESH_Actor.h"
38 #include "SMESH_ActorUtils.h"
39 #include "SMESH_ObjectDef.h"
40
41 #include "SMDS_Mesh.hxx"
42 #include "SMDS_MeshNode.hxx"
43
44 #include "SUIT_Session.h"
45 #include "SUIT_OverrideCursor.h"
46 #include "SUIT_ViewWindow.h"
47 #include "SUIT_ViewManager.h"
48 #include "SUIT_MessageBox.h"
49 #include "SUIT_Desktop.h"
50
51 #include "SalomeApp_Study.h"
52 #include "SalomeApp_SelectionMgr.h"
53
54 #include "SVTK_Selector.h"
55 #include "SVTK_ViewWindow.h"
56
57 #include "SALOME_Actor.h"
58 #include "SALOME_ListIO.hxx"
59
60 #include "utilities.h"
61
62 // VTK Includes
63 #include <vtkCell.h>
64 #include <vtkIdList.h>
65 #include <vtkIntArray.h>
66 #include <vtkCellArray.h>
67 #include <vtkUnsignedCharArray.h>
68 #include <vtkUnstructuredGrid.h>
69 #include <vtkDataSetMapper.h>
70 #include <vtkActorCollection.h>
71 #include <vtkRenderer.h>
72
73 // QT Includes
74 #include <qbuttongroup.h>
75 #include <qframe.h>
76 #include <qgroupbox.h>
77 #include <qlabel.h>
78 #include <qlineedit.h>
79 #include <qpushbutton.h>
80 #include <qradiobutton.h>
81 #include <qlayout.h>
82 #include <qvariant.h>
83 #include <qtooltip.h>
84 #include <qwhatsthis.h>
85 #include <qimage.h>
86 #include <qpixmap.h>
87 #include <qvalidator.h>
88 #include <qevent.h>
89
90 using namespace std;
91
92
93 namespace SMESH {
94
95   void AddNode (SMESH::SMESH_Mesh_ptr theMesh, float x, float y, float z)
96   {
97     SUIT_OverrideCursor wc;
98     try {
99       _PTR(SObject) aSobj = SMESH::FindSObject(theMesh);
100       SMESH::SMESH_MeshEditor_var aMeshEditor = theMesh->GetMeshEditor();
101       aMeshEditor->AddNode(x,y,z);
102       _PTR(Study) aStudy = GetActiveStudyDocument();
103       CORBA::Long anId = aStudy->StudyId();
104       if (TVisualObjPtr aVisualObj = SMESH::GetVisualObj(anId, aSobj->GetID().c_str())) {
105         aVisualObj->Update(true);
106       }
107     } catch (SALOME::SALOME_Exception& exc) {
108       INFOS("Follow exception was cought:\n\t" << exc.details.text);
109     } catch (const std::exception& exc) {
110       INFOS("Follow exception was cought:\n\t" << exc.what());
111     } catch (...) {
112       INFOS("Unknown exception was cought !!!");
113     }
114   }
115
116   class TNodeSimulation {
117     SVTK_ViewWindow* myViewWindow;
118
119     SALOME_Actor *myPreviewActor;
120     vtkDataSetMapper* myMapper;
121     vtkPoints* myPoints;
122
123   public:
124     TNodeSimulation(SVTK_ViewWindow* theViewWindow):
125       myViewWindow(theViewWindow)
126     {
127       vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
128
129       // Create points
130       myPoints = vtkPoints::New();
131       myPoints->SetNumberOfPoints(1);
132       myPoints->SetPoint(0,0.0,0.0,0.0);
133
134       // Create cells
135       vtkIdList *anIdList = vtkIdList::New();
136       anIdList->SetNumberOfIds(1);
137
138       vtkCellArray *aCells = vtkCellArray::New();
139       aCells->Allocate(2, 0);
140
141       vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
142       aCellTypesArray->SetNumberOfComponents(1);
143       aCellTypesArray->Allocate(1);
144
145       anIdList->SetId(0, 0);
146       aCells->InsertNextCell(anIdList);
147       aCellTypesArray->InsertNextValue(VTK_VERTEX);
148
149       vtkIntArray* aCellLocationsArray = vtkIntArray::New();
150       aCellLocationsArray->SetNumberOfComponents(1);
151       aCellLocationsArray->SetNumberOfTuples(1);
152
153       aCells->InitTraversal();
154       vtkIdType npts;
155       aCellLocationsArray->SetValue(0, aCells->GetTraversalLocation(npts));
156
157       aGrid->SetCells(aCellTypesArray, aCellLocationsArray, aCells);
158
159       aGrid->SetPoints(myPoints);
160       aGrid->SetCells(aCellTypesArray, aCellLocationsArray,aCells);
161       aCellLocationsArray->Delete();
162       aCellTypesArray->Delete();
163       aCells->Delete();
164       anIdList->Delete();
165
166       // Create and display actor
167       myMapper = vtkDataSetMapper::New();
168       myMapper->SetInput(aGrid);
169       aGrid->Delete();
170
171       myPreviewActor = SALOME_Actor::New();
172       myPreviewActor->SetInfinitive(true);
173       myPreviewActor->VisibilityOff();
174       myPreviewActor->PickableOff();
175       myPreviewActor->SetMapper(myMapper);
176
177       vtkProperty* aProp = vtkProperty::New();
178       aProp->SetRepresentationToPoints();
179
180       float anRGB[3];
181       anRGB[0] = GetFloat("SMESH:SettingsNodeColorRed",0)/255.;
182       anRGB[1] = GetFloat("SMESH:SettingsNodeColorGreen",255)/255.;
183       anRGB[2] = GetFloat("SMESH:SettingsNodeColorBlue",0)/255.;
184       aProp->SetColor(anRGB[0],anRGB[1],anRGB[2]);
185
186       float aPointSize = GetFloat("SMESH:SettingsNodesSize",3);
187       aProp->SetPointSize(aPointSize);
188
189       myPreviewActor->SetProperty(aProp);
190       aProp->Delete();
191
192       myViewWindow->AddActor(myPreviewActor);
193     }
194
195     void SetPosition (float x, float y, float z)
196     {
197       myPoints->SetPoint(0,x,y,z);
198       myPoints->Modified();
199       SetVisibility(true);
200     }
201
202     void SetVisibility (bool theVisibility)
203     {
204       myPreviewActor->SetVisibility(theVisibility);
205       RepaintCurrentView();
206     }
207
208     ~TNodeSimulation()
209     {
210       myViewWindow->RemoveActor(myPreviewActor);
211       myPreviewActor->Delete();
212
213       myMapper->RemoveAllInputs();
214       myMapper->Delete();
215
216       myPoints->Delete();
217     }
218   };
219 }
220
221 //=================================================================================
222 // class    : SMESHGUI_NodesDlg()
223 // purpose  :
224 //=================================================================================
225 SMESHGUI_NodesDlg::SMESHGUI_NodesDlg (SMESHGUI* theModule,
226                                       const char* name,
227                                       bool modal,
228                                       WFlags fl): 
229   QDialog(SMESH::GetDesktop(theModule), 
230           name, 
231           modal, 
232           WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | Qt::WDestructiveClose),
233   mySelector(SMESH::GetViewWindow(theModule)->GetSelector()),
234   mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
235   myViewWindow(SMESH::GetViewWindow(theModule)),
236   mySMESHGUI(theModule)
237 {
238   mySimulation = new SMESH::TNodeSimulation(myViewWindow);
239
240   QPixmap image0 (SMESHGUI::resourceMgr()->loadPixmap("SMESH", tr("ICON_DLG_NODE")));
241   if (!name)
242     setName("SMESHGUI_NodesDlg");
243   resize(303, 185);
244   setCaption(tr("MESH_NODE_TITLE"));
245   setSizeGripEnabled(TRUE);
246   SMESHGUI_NodesDlgLayout = new QGridLayout(this);
247   SMESHGUI_NodesDlgLayout->setSpacing(6);
248   SMESHGUI_NodesDlgLayout->setMargin(11);
249
250   /***************************************************************/
251   GroupButtons = new QGroupBox(this, "GroupButtons");
252   GroupButtons->setGeometry(QRect(10, 10, 281, 48));
253   GroupButtons->setTitle(tr("" ));
254   GroupButtons->setColumnLayout(0, Qt::Vertical);
255   GroupButtons->layout()->setSpacing(0);
256   GroupButtons->layout()->setMargin(0);
257   GroupButtonsLayout = new QGridLayout(GroupButtons->layout());
258   GroupButtonsLayout->setAlignment(Qt::AlignTop);
259   GroupButtonsLayout->setSpacing(6);
260   GroupButtonsLayout->setMargin(11);
261   buttonCancel = new QPushButton(GroupButtons, "buttonCancel");
262   buttonCancel->setText(tr("SMESH_BUT_CLOSE" ));
263   buttonCancel->setAutoDefault(TRUE);
264   GroupButtonsLayout->addWidget(buttonCancel, 0, 3);
265   buttonApply = new QPushButton(GroupButtons, "buttonApply");
266   buttonApply->setText(tr("SMESH_BUT_APPLY" ));
267   buttonApply->setAutoDefault(TRUE);
268   GroupButtonsLayout->addWidget(buttonApply, 0, 1);
269   QSpacerItem* spacer_9 = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
270   GroupButtonsLayout->addItem(spacer_9, 0, 2);
271   buttonOk = new QPushButton(GroupButtons, "buttonOk");
272   buttonOk->setText(tr("SMESH_BUT_OK" ));
273   buttonOk->setAutoDefault(TRUE);
274   buttonOk->setDefault(TRUE);
275   GroupButtonsLayout->addWidget(buttonOk, 0, 0);
276   SMESHGUI_NodesDlgLayout->addWidget(GroupButtons, 2, 0);
277
278   /***************************************************************/
279   GroupConstructors = new QButtonGroup(this, "GroupConstructors");
280   GroupConstructors->setTitle(tr("MESH_NODE" ));
281   GroupConstructors->setExclusive(TRUE);
282   GroupConstructors->setColumnLayout(0, Qt::Vertical);
283   GroupConstructors->layout()->setSpacing(0);
284   GroupConstructors->layout()->setMargin(0);
285   GroupConstructorsLayout = new QGridLayout(GroupConstructors->layout());
286   GroupConstructorsLayout->setAlignment(Qt::AlignTop);
287   GroupConstructorsLayout->setSpacing(6);
288   GroupConstructorsLayout->setMargin(11);
289   Constructor1 = new QRadioButton(GroupConstructors, "Constructor1");
290   Constructor1->setText(tr("" ));
291   Constructor1->setPixmap(image0);
292   Constructor1->setChecked(TRUE);
293   GroupConstructorsLayout->addWidget(Constructor1, 0, 0);
294   QSpacerItem* spacer_2 = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
295   GroupConstructorsLayout->addItem(spacer_2, 0, 1);
296   SMESHGUI_NodesDlgLayout->addWidget(GroupConstructors, 0, 0);
297
298   /***************************************************************/
299   GroupCoordinates = new QGroupBox(this, "GroupCoordinates");
300   GroupCoordinates->setTitle(tr("SMESH_COORDINATES" ));
301   GroupCoordinates->setColumnLayout(0, Qt::Vertical);
302   GroupCoordinates->layout()->setSpacing(0);
303   GroupCoordinates->layout()->setMargin(0);
304   GroupCoordinatesLayout = new QGridLayout(GroupCoordinates->layout());
305   GroupCoordinatesLayout->setAlignment(Qt::AlignTop);
306   GroupCoordinatesLayout->setSpacing(6);
307   GroupCoordinatesLayout->setMargin(11);
308   TextLabel_X = new QLabel(GroupCoordinates, "TextLabel_X");
309   TextLabel_X->setText(tr("SMESH_X" ));
310   GroupCoordinatesLayout->addWidget(TextLabel_X, 0, 0);
311   TextLabel_Y = new QLabel(GroupCoordinates, "TextLabel_Y");
312   TextLabel_Y->setText(tr("SMESH_Y" ));
313   GroupCoordinatesLayout->addWidget(TextLabel_Y, 0, 2);
314
315   TextLabel_Z = new QLabel(GroupCoordinates, "TextLabel_Z");
316   TextLabel_Z->setText(tr("SMESH_Z" ));
317   GroupCoordinatesLayout->addWidget(TextLabel_Z, 0, 4);
318
319   SpinBox_X = new SMESHGUI_SpinBox(GroupCoordinates, "SpinBox_X");
320   GroupCoordinatesLayout->addWidget(SpinBox_X, 0, 1);
321
322   SpinBox_Y = new SMESHGUI_SpinBox(GroupCoordinates, "SpinBox_Y");
323   GroupCoordinatesLayout->addWidget(SpinBox_Y, 0, 3);
324
325   SpinBox_Z = new SMESHGUI_SpinBox(GroupCoordinates, "SpinBox_Z");
326   GroupCoordinatesLayout->addWidget(SpinBox_Z, 0, 5);
327
328   SMESHGUI_NodesDlgLayout->addWidget(GroupCoordinates, 1, 0);
329
330   /* Initialisation and display */
331   Init();
332 }
333
334 //=======================================================================
335 // function : ~SMESHGUI_NodesDlg()
336 // purpose  : Destructor
337 //=======================================================================
338 SMESHGUI_NodesDlg::~SMESHGUI_NodesDlg()
339 {
340   delete mySimulation;
341 }
342
343 //=================================================================================
344 // function : Init()
345 // purpose  :
346 //=================================================================================
347 void SMESHGUI_NodesDlg::Init ()
348 {
349   /* Get setting of step value from file configuration */
350   double step;
351   // QString St = SUIT_CONFIG->getSetting("xxxxxxxxxxxxx");  TODO
352   // step = St.toDouble();                                    TODO
353   step = 25.0;
354
355   /* min, max, step and decimals for spin boxes */
356   SpinBox_X->RangeStepAndValidator(-999.999, +999.999, step, 3);
357   SpinBox_Y->RangeStepAndValidator(-999.999, +999.999, step, 3);
358   SpinBox_Z->RangeStepAndValidator(-999.999, +999.999, step, 3);
359   SpinBox_X->SetValue(0.0);
360   SpinBox_Y->SetValue(0.0);
361   SpinBox_Z->SetValue(0.0);
362
363   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
364
365   /* signals and slots connections */
366   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
367   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
368   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
369
370   connect(SpinBox_X, SIGNAL (valueChanged(double)), SLOT(ValueChangedInSpinBox(double)));
371   connect(SpinBox_Y, SIGNAL (valueChanged(double)), SLOT(ValueChangedInSpinBox(double)));
372   connect(SpinBox_Z, SIGNAL (valueChanged(double)), SLOT(ValueChangedInSpinBox(double)));
373
374   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(SelectionIntoArgument()));
375   connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()), SLOT(DeactivateActiveDialog()));
376   /* to close dialog if study frame change */
377   connect(mySMESHGUI, SIGNAL (SignalStudyFrameChanged()), SLOT(ClickOnCancel()));
378
379   /* Move widget on the botton right corner of main widget */
380   int x, y;
381   mySMESHGUI->DefineDlgPosition(this, x, y);
382   this->move(x, y);
383   this->show();
384
385   // set selection mode
386   SMESH::SetPointRepresentation(true);
387   myViewWindow->SetSelectionMode(NodeSelection);
388
389   SelectionIntoArgument();
390 }
391
392 //=================================================================================
393 // function : ValueChangedInSpinBox()
394 // purpose  :
395 //=================================================================================
396 void SMESHGUI_NodesDlg::ValueChangedInSpinBox (double newValue)
397 {
398   if (!myMesh->_is_nil()) {
399     double vx = SpinBox_X->GetValue();
400     double vy = SpinBox_Y->GetValue();
401     double vz = SpinBox_Z->GetValue();
402
403     mySimulation->SetPosition(vx,vy,vz);
404   }
405 }
406
407 //=================================================================================
408 // function : ClickOnOk()
409 // purpose  :
410 //=================================================================================
411 void SMESHGUI_NodesDlg::ClickOnOk()
412 {
413   if (ClickOnApply())
414     ClickOnCancel();
415 }
416
417 //=================================================================================
418 // function : ClickOnApply()
419 // purpose  :
420 //=================================================================================
421 bool SMESHGUI_NodesDlg::ClickOnApply()
422 {
423   if (mySMESHGUI->isActiveStudyLocked())
424     return false;
425
426   if (myMesh->_is_nil()) {
427     SUIT_MessageBox::warn1(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
428                            tr("MESH_IS_NOT_SELECTED"), tr("SMESH_BUT_OK"));
429     return false;
430   }
431
432   /* Recup args and call method */
433   double x = SpinBox_X->GetValue();
434   double y = SpinBox_Y->GetValue();
435   double z = SpinBox_Z->GetValue();
436   mySimulation->SetVisibility(false);
437   SMESH::AddNode(myMesh,x,y,z);
438   SMESH::SetPointRepresentation(true);
439
440   // select myMesh
441   SALOME_ListIO aList;
442   mySelectionMgr->selectedObjects(aList);
443   if (aList.Extent() != 1) {
444     if (SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView()) {
445       vtkActorCollection *aCollection = aViewWindow->getRenderer()->GetActors();
446       aCollection->InitTraversal();
447       while (vtkActor *anAct = aCollection->GetNextActor()) {
448         if (SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)) {
449           if (anActor->hasIO()) {
450             if (SMESH_MeshObj *aMeshObj = dynamic_cast<SMESH_MeshObj*>(anActor->GetObject().get())) {
451               if (myMesh->_is_equivalent(aMeshObj->GetMeshServer())) {
452                 aList.Clear();
453                 aList.Append(anActor->getIO());
454                 mySelectionMgr->setSelectedObjects(aList, false);
455                 break;
456               }
457             }
458           }
459         }
460       }
461     }
462   }
463   return true;
464 }
465
466 //=================================================================================
467 // function : ClickOnCancel()
468 // purpose  :
469 //=================================================================================
470 void SMESHGUI_NodesDlg::ClickOnCancel()
471 {
472   disconnect(mySelectionMgr, 0, this, 0);
473   myViewWindow->SetSelectionMode(ActorSelection);
474
475   mySimulation->SetVisibility(false);
476   SMESH::SetPointRepresentation(false);
477   mySMESHGUI->ResetState();
478
479   reject();
480 }
481
482 //=================================================================================
483 // function : SelectionIntoArgument()
484 // purpose  : Called when selection as changed or other case
485 //=================================================================================
486 void SMESHGUI_NodesDlg::SelectionIntoArgument()
487 {
488   if (!GroupConstructors->isEnabled())
489     return;
490
491   mySimulation->SetVisibility(false);
492   SMESH::SetPointRepresentation(true);
493
494   const SALOME_ListIO& aList = mySelector->StoredIObjects();
495   if (aList.Extent() == 1) {
496     Handle(SALOME_InteractiveObject) anIO = aList.First();
497     if (anIO->hasEntry()) {
498       myMesh = SMESH::GetMeshByIO(anIO);
499       if (myMesh->_is_nil()) return;
500       QString aText;
501       if (SMESH::GetNameOfSelectedNodes(mySelector,anIO,aText) == 1) {
502         if (SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh.in())) {
503           if (SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh()) {
504             if (const SMDS_MeshNode* aNode = aMesh->FindNode(aText.toInt())) {
505               SpinBox_X->SetValue(aNode->X());
506               SpinBox_Y->SetValue(aNode->Y());
507               SpinBox_Z->SetValue(aNode->Z());
508             }
509           }
510         }
511       }
512       mySimulation->SetPosition(SpinBox_X->GetValue(),
513                                 SpinBox_Y->GetValue(),
514                                 SpinBox_Z->GetValue());
515     }
516   }
517 }
518
519 //=================================================================================
520 // function : closeEvent()
521 // purpose  :
522 //=================================================================================
523 void SMESHGUI_NodesDlg::closeEvent (QCloseEvent*)
524 {
525   this->ClickOnCancel(); /* same than click on cancel button */
526 }
527
528 //=================================================================================
529 // function : hideEvent()
530 // purpose  : caused by ESC key
531 //=================================================================================
532 void SMESHGUI_NodesDlg::hideEvent (QHideEvent*)
533 {
534   if (!isMinimized())
535     ClickOnCancel();
536 }
537
538 //=================================================================================
539 // function : enterEvent()
540 // purpose  : to reactivate this dialog box when mouse enter onto the window
541 //=================================================================================
542 void SMESHGUI_NodesDlg::enterEvent(QEvent*)
543 {
544   if (!GroupConstructors->isEnabled())
545     ActivateThisDialog();
546 }
547
548 //=================================================================================
549 // function : DeactivateActiveDialog()
550 // purpose  : public slot to deactivate if active
551 //=================================================================================
552 void SMESHGUI_NodesDlg::DeactivateActiveDialog()
553 {
554   if (GroupConstructors->isEnabled()) {
555     GroupConstructors->setEnabled(false);
556     GroupCoordinates->setEnabled(false);
557     GroupButtons->setEnabled(false);
558     mySimulation->SetVisibility(false);
559     mySMESHGUI->ResetState();
560     mySMESHGUI->SetActiveDialogBox(0);
561   }
562 }
563
564 //=================================================================================
565 // function : ActivateThisDialog()
566 // purpose  :
567 //=================================================================================
568 void SMESHGUI_NodesDlg::ActivateThisDialog()
569 {
570   mySMESHGUI->EmitSignalDeactivateDialog();
571   GroupConstructors->setEnabled(true);
572   GroupCoordinates->setEnabled(true);
573   GroupButtons->setEnabled(true);
574
575   SMESH::SetPointRepresentation(true);
576   myViewWindow->SetSelectionMode(NodeSelection);
577
578   SelectionIntoArgument();
579 }