1 // SMESH SMESHGUI : GUI for SMESH component
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SMESHGUI_NodesDlg.cxx
25 // Author : Nicolas REJNERI
29 #include "SMESHGUI_NodesDlg.h"
32 #include "SMESHGUI_SpinBox.h"
33 #include "SMESHGUI_Utils.h"
34 #include "SMESHGUI_VTKUtils.h"
35 #include "SMESHGUI_MeshUtils.h"
37 #include "SMESH_Actor.h"
38 #include "SMESH_ActorUtils.h"
39 #include "SMESH_ObjectDef.h"
41 #include "SMDS_Mesh.hxx"
42 #include "SMDS_MeshNode.hxx"
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"
50 #include "SalomeApp_Study.h"
51 #include "SalomeApp_Application.h"
53 #include "SVTK_ViewWindow.h"
55 #include "SALOME_Actor.h"
56 #include "SALOME_ListIO.hxx"
57 #include "SVTK_Selection.h"
59 #include "utilities.h"
63 #include <vtkIdList.h>
64 #include <vtkIntArray.h>
65 #include <vtkCellArray.h>
66 #include <vtkUnsignedCharArray.h>
67 #include <vtkUnstructuredGrid.h>
68 #include <vtkDataSetMapper.h>
69 #include <vtkActorCollection.h>
70 #include <vtkRenderer.h>
73 #include <qbuttongroup.h>
75 #include <qgroupbox.h>
77 #include <qlineedit.h>
78 #include <qpushbutton.h>
79 #include <qradiobutton.h>
83 #include <qwhatsthis.h>
86 #include <qvalidator.h>
94 void AddNode (SMESH::SMESH_Mesh_ptr theMesh, float x, float y, float z)
96 SUIT_OverrideCursor wc;
98 _PTR(SObject) aSobj = SMESH::FindSObject(theMesh);
99 SMESH::SMESH_MeshEditor_var aMeshEditor = theMesh->GetMeshEditor();
100 aMeshEditor->AddNode(x,y,z);
101 _PTR(Study) aStudy = GetActiveStudyDocument();
102 CORBA::Long anId = aStudy->StudyId();
103 if (TVisualObjPtr aVisualObj = SMESH::GetVisualObj(anId, aSobj->GetID().c_str())) {
104 aVisualObj->Update(true);
106 } catch (SALOME::SALOME_Exception& exc) {
107 INFOS("Follow exception was cought:\n\t" << exc.details.text);
108 } catch (const std::exception& exc) {
109 INFOS("Follow exception was cought:\n\t" << exc.what());
111 INFOS("Unknown exception was cought !!!");
115 class TNodeSimulation {
116 SalomeApp_Application* myApplication;
117 SUIT_ViewWindow* myViewWindow;
118 SVTK_ViewWindow* myVTKViewWindow;
120 SALOME_Actor *myPreviewActor;
121 vtkDataSetMapper* myMapper;
125 TNodeSimulation (SalomeApp_Application* theApplication)
127 myApplication = theApplication;
128 SUIT_ViewManager* mgr = theApplication->activeViewManager();
130 myViewWindow = mgr->getActiveView();
131 myVTKViewWindow = GetVtkViewWindow(myViewWindow);
133 vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
136 myPoints = vtkPoints::New();
137 myPoints->SetNumberOfPoints(1);
138 myPoints->SetPoint(0,0.0,0.0,0.0);
141 vtkIdList *anIdList = vtkIdList::New();
142 anIdList->SetNumberOfIds(1);
144 vtkCellArray *aCells = vtkCellArray::New();
145 aCells->Allocate(2, 0);
147 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
148 aCellTypesArray->SetNumberOfComponents(1);
149 aCellTypesArray->Allocate(1);
151 anIdList->SetId(0, 0);
152 aCells->InsertNextCell(anIdList);
153 aCellTypesArray->InsertNextValue(VTK_VERTEX);
155 vtkIntArray* aCellLocationsArray = vtkIntArray::New();
156 aCellLocationsArray->SetNumberOfComponents(1);
157 aCellLocationsArray->SetNumberOfTuples(1);
159 aCells->InitTraversal();
161 aCellLocationsArray->SetValue(0, aCells->GetTraversalLocation(npts));
163 aGrid->SetCells(aCellTypesArray, aCellLocationsArray, aCells);
165 aGrid->SetPoints(myPoints);
166 aGrid->SetCells(aCellTypesArray, aCellLocationsArray,aCells);
167 aCellLocationsArray->Delete();
168 aCellTypesArray->Delete();
172 // Create and display actor
173 myMapper = vtkDataSetMapper::New();
174 myMapper->SetInput(aGrid);
177 myPreviewActor = SALOME_Actor::New();
178 myPreviewActor->SetInfinitive(true);
179 myPreviewActor->VisibilityOff();
180 myPreviewActor->PickableOff();
181 myPreviewActor->SetMapper(myMapper);
183 vtkProperty* aProp = vtkProperty::New();
184 aProp->SetRepresentationToPoints();
187 anRGB[0] = GetFloat("SMESH:SettingsNodeColorRed",0)/255.;
188 anRGB[1] = GetFloat("SMESH:SettingsNodeColorGreen",255)/255.;
189 anRGB[2] = GetFloat("SMESH:SettingsNodeColorBlue",0)/255.;
190 aProp->SetColor(anRGB[0],anRGB[1],anRGB[2]);
192 float aPointSize = GetFloat("SMESH:SettingsNodesSize",3);
193 aProp->SetPointSize(aPointSize);
195 myPreviewActor->SetProperty(aProp);
198 myVTKViewWindow->AddActor(myPreviewActor);
201 void SetPosition (float x, float y, float z)
203 myPoints->SetPoint(0,x,y,z);
204 myPoints->Modified();
208 void SetVisibility (bool theVisibility)
210 myPreviewActor->SetVisibility(theVisibility);
211 RepaintCurrentView();
216 if (FindVtkViewWindow(myApplication->activeViewManager(), myViewWindow)) {
217 myVTKViewWindow->RemoveActor(myPreviewActor);
219 myPreviewActor->Delete();
221 myMapper->RemoveAllInputs();
229 //=================================================================================
230 // class : SMESHGUI_NodesDlg()
232 //=================================================================================
233 SMESHGUI_NodesDlg::SMESHGUI_NodesDlg (QWidget* parent,
235 SalomeApp_SelectionMgr* Sel,
238 : QDialog(parent, name, modal, WStyle_Customize | WStyle_NormalBorder |
239 WStyle_Title | WStyle_SysMenu | Qt::WDestructiveClose)
241 SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>
242 (SUIT_Session::session()->activeApplication());
243 mySimulation = new SMESH::TNodeSimulation (anApp);
245 QPixmap image0 (SMESHGUI::resourceMgr()->loadPixmap("SMESH", tr("ICON_DLG_NODE")));
247 setName("SMESHGUI_NodesDlg");
249 setCaption(tr("MESH_NODE_TITLE"));
250 setSizeGripEnabled(TRUE);
251 SMESHGUI_NodesDlgLayout = new QGridLayout(this);
252 SMESHGUI_NodesDlgLayout->setSpacing(6);
253 SMESHGUI_NodesDlgLayout->setMargin(11);
255 /***************************************************************/
256 GroupButtons = new QGroupBox(this, "GroupButtons");
257 GroupButtons->setGeometry(QRect(10, 10, 281, 48));
258 GroupButtons->setTitle(tr("" ));
259 GroupButtons->setColumnLayout(0, Qt::Vertical);
260 GroupButtons->layout()->setSpacing(0);
261 GroupButtons->layout()->setMargin(0);
262 GroupButtonsLayout = new QGridLayout(GroupButtons->layout());
263 GroupButtonsLayout->setAlignment(Qt::AlignTop);
264 GroupButtonsLayout->setSpacing(6);
265 GroupButtonsLayout->setMargin(11);
266 buttonCancel = new QPushButton(GroupButtons, "buttonCancel");
267 buttonCancel->setText(tr("SMESH_BUT_CLOSE" ));
268 buttonCancel->setAutoDefault(TRUE);
269 GroupButtonsLayout->addWidget(buttonCancel, 0, 3);
270 buttonApply = new QPushButton(GroupButtons, "buttonApply");
271 buttonApply->setText(tr("SMESH_BUT_APPLY" ));
272 buttonApply->setAutoDefault(TRUE);
273 GroupButtonsLayout->addWidget(buttonApply, 0, 1);
274 QSpacerItem* spacer_9 = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
275 GroupButtonsLayout->addItem(spacer_9, 0, 2);
276 buttonOk = new QPushButton(GroupButtons, "buttonOk");
277 buttonOk->setText(tr("SMESH_BUT_OK" ));
278 buttonOk->setAutoDefault(TRUE);
279 buttonOk->setDefault(TRUE);
280 GroupButtonsLayout->addWidget(buttonOk, 0, 0);
281 SMESHGUI_NodesDlgLayout->addWidget(GroupButtons, 2, 0);
283 /***************************************************************/
284 GroupConstructors = new QButtonGroup(this, "GroupConstructors");
285 GroupConstructors->setTitle(tr("MESH_NODE" ));
286 GroupConstructors->setExclusive(TRUE);
287 GroupConstructors->setColumnLayout(0, Qt::Vertical);
288 GroupConstructors->layout()->setSpacing(0);
289 GroupConstructors->layout()->setMargin(0);
290 GroupConstructorsLayout = new QGridLayout(GroupConstructors->layout());
291 GroupConstructorsLayout->setAlignment(Qt::AlignTop);
292 GroupConstructorsLayout->setSpacing(6);
293 GroupConstructorsLayout->setMargin(11);
294 Constructor1 = new QRadioButton(GroupConstructors, "Constructor1");
295 Constructor1->setText(tr("" ));
296 Constructor1->setPixmap(image0);
297 Constructor1->setChecked(TRUE);
298 GroupConstructorsLayout->addWidget(Constructor1, 0, 0);
299 QSpacerItem* spacer_2 = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
300 GroupConstructorsLayout->addItem(spacer_2, 0, 1);
301 SMESHGUI_NodesDlgLayout->addWidget(GroupConstructors, 0, 0);
303 /***************************************************************/
304 GroupCoordinates = new QGroupBox(this, "GroupCoordinates");
305 GroupCoordinates->setTitle(tr("SMESH_COORDINATES" ));
306 GroupCoordinates->setColumnLayout(0, Qt::Vertical);
307 GroupCoordinates->layout()->setSpacing(0);
308 GroupCoordinates->layout()->setMargin(0);
309 GroupCoordinatesLayout = new QGridLayout(GroupCoordinates->layout());
310 GroupCoordinatesLayout->setAlignment(Qt::AlignTop);
311 GroupCoordinatesLayout->setSpacing(6);
312 GroupCoordinatesLayout->setMargin(11);
313 TextLabel_X = new QLabel(GroupCoordinates, "TextLabel_X");
314 TextLabel_X->setText(tr("SMESH_X" ));
315 GroupCoordinatesLayout->addWidget(TextLabel_X, 0, 0);
316 TextLabel_Y = new QLabel(GroupCoordinates, "TextLabel_Y");
317 TextLabel_Y->setText(tr("SMESH_Y" ));
318 GroupCoordinatesLayout->addWidget(TextLabel_Y, 0, 2);
320 TextLabel_Z = new QLabel(GroupCoordinates, "TextLabel_Z");
321 TextLabel_Z->setText(tr("SMESH_Z" ));
322 GroupCoordinatesLayout->addWidget(TextLabel_Z, 0, 4);
324 SpinBox_X = new SMESHGUI_SpinBox(GroupCoordinates, "SpinBox_X");
325 GroupCoordinatesLayout->addWidget(SpinBox_X, 0, 1);
327 SpinBox_Y = new SMESHGUI_SpinBox(GroupCoordinates, "SpinBox_Y");
328 GroupCoordinatesLayout->addWidget(SpinBox_Y, 0, 3);
330 SpinBox_Z = new SMESHGUI_SpinBox(GroupCoordinates, "SpinBox_Z");
331 GroupCoordinatesLayout->addWidget(SpinBox_Z, 0, 5);
333 SMESHGUI_NodesDlgLayout->addWidget(GroupCoordinates, 1, 0);
335 /* Initialisation and display */
339 //=======================================================================
340 // function : ~SMESHGUI_NodesDlg()
341 // purpose : Destructor
342 //=======================================================================
343 SMESHGUI_NodesDlg::~SMESHGUI_NodesDlg()
348 //=================================================================================
351 //=================================================================================
352 void SMESHGUI_NodesDlg::Init (SalomeApp_SelectionMgr* Sel)
354 /* Get setting of step value from file configuration */
356 // QString St = SUIT_CONFIG->getSetting("xxxxxxxxxxxxx"); TODO
357 // step = St.toDouble(); TODO
360 /* min, max, step and decimals for spin boxes */
361 SpinBox_X->RangeStepAndValidator(-999.999, +999.999, step, 3);
362 SpinBox_Y->RangeStepAndValidator(-999.999, +999.999, step, 3);
363 SpinBox_Z->RangeStepAndValidator(-999.999, +999.999, step, 3);
364 SpinBox_X->SetValue(0.0);
365 SpinBox_Y->SetValue(0.0);
366 SpinBox_Z->SetValue(0.0);
368 mySelectionMgr = Sel;
369 myMeshGUI = SMESHGUI::GetSMESHGUI();
370 myMeshGUI->SetActiveDialogBox((QDialog*)this);
372 /* signals and slots connections */
373 connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
374 connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
375 connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
377 connect(SpinBox_X, SIGNAL (valueChanged(double)), SLOT(ValueChangedInSpinBox(double)));
378 connect(SpinBox_Y, SIGNAL (valueChanged(double)), SLOT(ValueChangedInSpinBox(double)));
379 connect(SpinBox_Z, SIGNAL (valueChanged(double)), SLOT(ValueChangedInSpinBox(double)));
381 connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(SelectionIntoArgument()));
382 connect(myMeshGUI, SIGNAL (SignalDeactivateActiveDialog()), SLOT(DeactivateActiveDialog()));
383 /* to close dialog if study frame change */
384 connect(myMeshGUI, SIGNAL (SignalStudyFrameChanged()), SLOT(ClickOnCancel()));
386 /* Move widget on the botton right corner of main widget */
388 myMeshGUI->DefineDlgPosition(this, x, y);
392 // set selection mode
393 SMESH::SetPointRepresentation(true);
395 mySelectionMgr->setSelectionModes(NodeSelection, true);
397 mySelectionMgr->setSelectionModes(NodeSelection);
400 SelectionIntoArgument();
403 //=================================================================================
404 // function : ValueChangedInSpinBox()
406 //=================================================================================
407 void SMESHGUI_NodesDlg::ValueChangedInSpinBox (double newValue)
409 if (!myMesh->_is_nil()) {
410 double vx = SpinBox_X->GetValue();
411 double vy = SpinBox_Y->GetValue();
412 double vz = SpinBox_Z->GetValue();
414 mySimulation->SetPosition(vx,vy,vz);
418 //=================================================================================
419 // function : ClickOnOk()
421 //=================================================================================
422 void SMESHGUI_NodesDlg::ClickOnOk()
428 //=================================================================================
429 // function : ClickOnApply()
431 //=================================================================================
432 bool SMESHGUI_NodesDlg::ClickOnApply()
434 if (myMeshGUI->isActiveStudyLocked())
437 if (myMesh->_is_nil()) {
438 SUIT_MessageBox::warn1(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
439 tr("MESH_IS_NOT_SELECTED"), tr("SMESH_BUT_OK"));
443 /* Recup args and call method */
444 double x = SpinBox_X->GetValue();
445 double y = SpinBox_Y->GetValue();
446 double z = SpinBox_Z->GetValue();
447 mySimulation->SetVisibility(false);
448 SMESH::AddNode(myMesh,x,y,z);
449 SMESH::SetPointRepresentation(true);
453 mySelectionMgr->selectedObjects(aList);
454 if (aList.Extent() != 1) {
455 if (SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView()) {
456 vtkActorCollection *aCollection = aViewWindow->getRenderer()->GetActors();
457 aCollection->InitTraversal();
458 while (vtkActor *anAct = aCollection->GetNextActor()) {
459 if (SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)) {
460 if (anActor->hasIO()) {
461 if (SMESH_MeshObj *aMeshObj = dynamic_cast<SMESH_MeshObj*>(anActor->GetObject().get())) {
462 if (myMesh->_is_equivalent(aMeshObj->GetMeshServer())) {
464 mySelectionMgr->clearSelected();
465 mySelectionMgr->AddIObject(anActor->getIO(), false);
468 aList.Append(anActor->getIO());
469 mySelectionMgr->setSelectedObjects(aList, false);
482 //=================================================================================
483 // function : ClickOnCancel()
485 //=================================================================================
486 void SMESHGUI_NodesDlg::ClickOnCancel()
488 MESSAGE("SMESHGUI_NodesDlg::ClickOnCancel() 1");
489 disconnect(mySelectionMgr, 0, this, 0);
490 mySelectionMgr->clearSelected();
491 mySelectionMgr->setSelectionModes(ActorSelection);
493 MESSAGE("SMESHGUI_NodesDlg::ClickOnCancel() 2");
494 mySimulation->SetVisibility(false);
495 SMESH::SetPointRepresentation(false);
496 myMeshGUI->ResetState();
499 MESSAGE("SMESHGUI_NodesDlg::ClickOnCancel() 3");
502 //=================================================================================
503 // function : SelectionIntoArgument()
504 // purpose : Called when selection as changed or other case
505 //=================================================================================
506 void SMESHGUI_NodesDlg::SelectionIntoArgument()
508 if (!GroupConstructors->isEnabled())
511 mySimulation->SetVisibility(false);
512 SMESH::SetPointRepresentation(true);
515 mySelectionMgr->selectedObjects(aList);
516 if (aList.Extent() == 1) {
517 Handle(SALOME_InteractiveObject) anIO = aList.First();
518 if (anIO->hasEntry()) {
519 myMesh = SMESH::GetMeshByIO(anIO);
520 if (myMesh->_is_nil()) return;
522 if (SMESH::GetNameOfSelectedNodes(mySelectionMgr, aText) == 1) {
523 if (SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh.in())) {
524 if (SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh()) {
525 if (const SMDS_MeshNode* aNode = aMesh->FindNode(aText.toInt())) {
526 SpinBox_X->SetValue(aNode->X());
527 SpinBox_Y->SetValue(aNode->Y());
528 SpinBox_Z->SetValue(aNode->Z());
533 mySimulation->SetPosition(SpinBox_X->GetValue(),
534 SpinBox_Y->GetValue(),
535 SpinBox_Z->GetValue());
540 //=================================================================================
541 // function : closeEvent()
543 //=================================================================================
544 void SMESHGUI_NodesDlg::closeEvent (QCloseEvent*)
546 this->ClickOnCancel(); /* same than click on cancel button */
549 //=================================================================================
550 // function : hideEvent()
551 // purpose : caused by ESC key
552 //=================================================================================
553 void SMESHGUI_NodesDlg::hideEvent (QHideEvent*)
559 //=================================================================================
560 // function : enterEvent()
561 // purpose : to reactivate this dialog box when mouse enter onto the window
562 //=================================================================================
563 void SMESHGUI_NodesDlg::enterEvent(QEvent*)
565 if (!GroupConstructors->isEnabled())
566 ActivateThisDialog();
569 //=================================================================================
570 // function : DeactivateActiveDialog()
571 // purpose : public slot to deactivate if active
572 //=================================================================================
573 void SMESHGUI_NodesDlg::DeactivateActiveDialog()
575 if (GroupConstructors->isEnabled()) {
576 GroupConstructors->setEnabled(false);
577 GroupCoordinates->setEnabled(false);
578 GroupButtons->setEnabled(false);
579 mySimulation->SetVisibility(false);
580 myMeshGUI->ResetState();
581 myMeshGUI->SetActiveDialogBox(0);
585 //=================================================================================
586 // function : ActivateThisDialog()
588 //=================================================================================
589 void SMESHGUI_NodesDlg::ActivateThisDialog()
591 myMeshGUI->EmitSignalDeactivateDialog();
592 GroupConstructors->setEnabled(true);
593 GroupCoordinates->setEnabled(true);
594 GroupButtons->setEnabled(true);
596 SMESH::SetPointRepresentation(true);
598 mySelectionMgr->setSelectionModes(NodeSelection, true);
600 mySelectionMgr->setSelectionModes(NodeSelection);
603 SelectionIntoArgument();