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"
49 #include "SUIT_Desktop.h"
51 #include "SalomeApp_Study.h"
52 #include "LightApp_Application.h"
53 #include "LightApp_SelectionMgr.h"
55 #include "SVTK_Selector.h"
56 #include "SVTK_ViewWindow.h"
57 #include "VTKViewer_CellLocationsArray.h"
59 #include "SALOME_Actor.h"
60 #include "SALOME_ListIO.hxx"
62 #include "utilities.h"
66 #include <vtkIdList.h>
67 #include <vtkCellArray.h>
68 #include <vtkUnsignedCharArray.h>
69 #include <vtkUnstructuredGrid.h>
70 #include <vtkDataSetMapper.h>
71 #include <vtkActorCollection.h>
72 #include <vtkRenderer.h>
73 #include <vtkProperty.h>
76 #include <qbuttongroup.h>
78 #include <qgroupbox.h>
80 #include <qlineedit.h>
81 #include <qpushbutton.h>
82 #include <qradiobutton.h>
86 #include <qwhatsthis.h>
89 #include <qvalidator.h>
97 void AddNode (SMESH::SMESH_Mesh_ptr theMesh, float x, float y, float z)
99 SUIT_OverrideCursor wc;
101 _PTR(SObject) aSobj = SMESH::FindSObject(theMesh);
102 SMESH::SMESH_MeshEditor_var aMeshEditor = theMesh->GetMeshEditor();
103 aMeshEditor->AddNode(x,y,z);
104 _PTR(Study) aStudy = GetActiveStudyDocument();
105 CORBA::Long anId = aStudy->StudyId();
106 if (TVisualObjPtr aVisualObj = SMESH::GetVisualObj(anId, aSobj->GetID().c_str())) {
107 aVisualObj->Update(true);
109 } catch (SALOME::SALOME_Exception& exc) {
110 INFOS("Follow exception was cought:\n\t" << exc.details.text);
111 } catch (const std::exception& exc) {
112 INFOS("Follow exception was cought:\n\t" << exc.what());
114 INFOS("Unknown exception was cought !!!");
118 class TNodeSimulation {
119 SVTK_ViewWindow* myViewWindow;
121 SALOME_Actor *myPreviewActor;
122 vtkDataSetMapper* myMapper;
126 TNodeSimulation(SVTK_ViewWindow* theViewWindow):
127 myViewWindow(theViewWindow)
129 vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
132 myPoints = vtkPoints::New();
133 myPoints->SetNumberOfPoints(1);
134 myPoints->SetPoint(0,0.0,0.0,0.0);
137 vtkIdList *anIdList = vtkIdList::New();
138 anIdList->SetNumberOfIds(1);
140 vtkCellArray *aCells = vtkCellArray::New();
141 aCells->Allocate(2, 0);
143 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
144 aCellTypesArray->SetNumberOfComponents(1);
145 aCellTypesArray->Allocate(1);
147 anIdList->SetId(0, 0);
148 aCells->InsertNextCell(anIdList);
149 aCellTypesArray->InsertNextValue(VTK_VERTEX);
151 VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
152 aCellLocationsArray->SetNumberOfComponents(1);
153 aCellLocationsArray->SetNumberOfTuples(1);
155 aCells->InitTraversal();
157 aCellLocationsArray->SetValue(0, aCells->GetTraversalLocation(npts));
159 aGrid->SetCells(aCellTypesArray, aCellLocationsArray, aCells);
161 aGrid->SetPoints(myPoints);
162 aGrid->SetCells(aCellTypesArray, aCellLocationsArray,aCells);
163 aCellLocationsArray->Delete();
164 aCellTypesArray->Delete();
168 // Create and display actor
169 myMapper = vtkDataSetMapper::New();
170 myMapper->SetInput(aGrid);
173 myPreviewActor = SALOME_Actor::New();
174 myPreviewActor->SetInfinitive(true);
175 myPreviewActor->VisibilityOff();
176 myPreviewActor->PickableOff();
177 myPreviewActor->SetMapper(myMapper);
179 vtkProperty* aProp = vtkProperty::New();
180 aProp->SetRepresentationToPoints();
182 vtkFloatingPointType anRGB[3];
183 GetColor( "SMESH", "node_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 255, 0 ) );
184 aProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
186 vtkFloatingPointType aPointSize = GetFloat( "SMESH:node_size", 3 );
187 aProp->SetPointSize( aPointSize );
189 myPreviewActor->SetProperty( aProp );
192 myViewWindow->AddActor(myPreviewActor);
195 void SetPosition (float x, float y, float z)
197 myPoints->SetPoint(0,x,y,z);
198 myPoints->Modified();
202 void SetVisibility (bool theVisibility)
204 myPreviewActor->SetVisibility(theVisibility);
205 RepaintCurrentView();
210 myViewWindow->RemoveActor(myPreviewActor);
211 myPreviewActor->Delete();
213 myMapper->RemoveAllInputs();
221 //=================================================================================
222 // class : SMESHGUI_NodesDlg()
224 //=================================================================================
225 SMESHGUI_NodesDlg::SMESHGUI_NodesDlg (SMESHGUI* theModule,
229 QDialog(SMESH::GetDesktop(theModule),
232 WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | Qt::WDestructiveClose),
233 mySelector(SMESH::GetViewWindow(theModule)->GetSelector()),
234 mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
235 mySMESHGUI(theModule)
237 mySimulation = new SMESH::TNodeSimulation(SMESH::GetViewWindow( mySMESHGUI ));
239 QPixmap image0 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_DLG_NODE")));
241 setName("SMESHGUI_NodesDlg");
243 setCaption(tr("MESH_NODE_TITLE"));
244 setSizeGripEnabled(TRUE);
245 SMESHGUI_NodesDlgLayout = new QGridLayout(this);
246 SMESHGUI_NodesDlgLayout->setSpacing(6);
247 SMESHGUI_NodesDlgLayout->setMargin(11);
249 /***************************************************************/
250 GroupButtons = new QGroupBox(this, "GroupButtons");
251 GroupButtons->setGeometry(QRect(10, 10, 281, 48));
252 GroupButtons->setTitle(tr("" ));
253 GroupButtons->setColumnLayout(0, Qt::Vertical);
254 GroupButtons->layout()->setSpacing(0);
255 GroupButtons->layout()->setMargin(0);
256 GroupButtonsLayout = new QGridLayout(GroupButtons->layout());
257 GroupButtonsLayout->setAlignment(Qt::AlignTop);
258 GroupButtonsLayout->setSpacing(6);
259 GroupButtonsLayout->setMargin(11);
260 buttonHelp = new QPushButton(GroupButtons, "buttonHelp");
261 buttonHelp->setText(tr("SMESH_BUT_HELP" ));
262 buttonHelp->setAutoDefault(TRUE);
263 GroupButtonsLayout->addWidget(buttonHelp, 0, 4);
264 buttonCancel = new QPushButton(GroupButtons, "buttonCancel");
265 buttonCancel->setText(tr("SMESH_BUT_CLOSE" ));
266 buttonCancel->setAutoDefault(TRUE);
267 GroupButtonsLayout->addWidget(buttonCancel, 0, 3);
268 buttonApply = new QPushButton(GroupButtons, "buttonApply");
269 buttonApply->setText(tr("SMESH_BUT_APPLY" ));
270 buttonApply->setAutoDefault(TRUE);
271 GroupButtonsLayout->addWidget(buttonApply, 0, 1);
272 QSpacerItem* spacer_9 = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
273 GroupButtonsLayout->addItem(spacer_9, 0, 2);
274 buttonOk = new QPushButton(GroupButtons, "buttonOk");
275 buttonOk->setText(tr("SMESH_BUT_OK" ));
276 buttonOk->setAutoDefault(TRUE);
277 buttonOk->setDefault(TRUE);
278 GroupButtonsLayout->addWidget(buttonOk, 0, 0);
279 SMESHGUI_NodesDlgLayout->addWidget(GroupButtons, 2, 0);
281 /***************************************************************/
282 GroupConstructors = new QButtonGroup(this, "GroupConstructors");
283 GroupConstructors->setTitle(tr("MESH_NODE" ));
284 GroupConstructors->setExclusive(TRUE);
285 GroupConstructors->setColumnLayout(0, Qt::Vertical);
286 GroupConstructors->layout()->setSpacing(0);
287 GroupConstructors->layout()->setMargin(0);
288 GroupConstructorsLayout = new QGridLayout(GroupConstructors->layout());
289 GroupConstructorsLayout->setAlignment(Qt::AlignTop);
290 GroupConstructorsLayout->setSpacing(6);
291 GroupConstructorsLayout->setMargin(11);
292 Constructor1 = new QRadioButton(GroupConstructors, "Constructor1");
293 Constructor1->setText(tr("" ));
294 Constructor1->setPixmap(image0);
295 Constructor1->setChecked(TRUE);
296 GroupConstructorsLayout->addWidget(Constructor1, 0, 0);
297 QSpacerItem* spacer_2 = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
298 GroupConstructorsLayout->addItem(spacer_2, 0, 1);
299 SMESHGUI_NodesDlgLayout->addWidget(GroupConstructors, 0, 0);
301 /***************************************************************/
302 GroupCoordinates = new QGroupBox(this, "GroupCoordinates");
303 GroupCoordinates->setTitle(tr("SMESH_COORDINATES" ));
304 GroupCoordinates->setColumnLayout(0, Qt::Vertical);
305 GroupCoordinates->layout()->setSpacing(0);
306 GroupCoordinates->layout()->setMargin(0);
307 GroupCoordinatesLayout = new QGridLayout(GroupCoordinates->layout());
308 GroupCoordinatesLayout->setAlignment(Qt::AlignTop);
309 GroupCoordinatesLayout->setSpacing(6);
310 GroupCoordinatesLayout->setMargin(11);
311 TextLabel_X = new QLabel(GroupCoordinates, "TextLabel_X");
312 TextLabel_X->setText(tr("SMESH_X" ));
313 GroupCoordinatesLayout->addWidget(TextLabel_X, 0, 0);
314 TextLabel_Y = new QLabel(GroupCoordinates, "TextLabel_Y");
315 TextLabel_Y->setText(tr("SMESH_Y" ));
316 GroupCoordinatesLayout->addWidget(TextLabel_Y, 0, 2);
318 TextLabel_Z = new QLabel(GroupCoordinates, "TextLabel_Z");
319 TextLabel_Z->setText(tr("SMESH_Z" ));
320 GroupCoordinatesLayout->addWidget(TextLabel_Z, 0, 4);
322 SpinBox_X = new SMESHGUI_SpinBox(GroupCoordinates, "SpinBox_X");
323 GroupCoordinatesLayout->addWidget(SpinBox_X, 0, 1);
325 SpinBox_Y = new SMESHGUI_SpinBox(GroupCoordinates, "SpinBox_Y");
326 GroupCoordinatesLayout->addWidget(SpinBox_Y, 0, 3);
328 SpinBox_Z = new SMESHGUI_SpinBox(GroupCoordinates, "SpinBox_Z");
329 GroupCoordinatesLayout->addWidget(SpinBox_Z, 0, 5);
331 SMESHGUI_NodesDlgLayout->addWidget(GroupCoordinates, 1, 0);
333 myHelpFileName = "/files/adding_nodes_and_elements.htm#Adding_nodes";
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 ()
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 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
370 /* signals and slots connections */
371 connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
372 connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
373 connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
374 connect(buttonHelp, SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
376 connect(SpinBox_X, SIGNAL (valueChanged(double)), SLOT(ValueChangedInSpinBox(double)));
377 connect(SpinBox_Y, SIGNAL (valueChanged(double)), SLOT(ValueChangedInSpinBox(double)));
378 connect(SpinBox_Z, SIGNAL (valueChanged(double)), SLOT(ValueChangedInSpinBox(double)));
380 connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(SelectionIntoArgument()));
381 connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()), SLOT(DeactivateActiveDialog()));
382 /* to close dialog if study frame change */
383 connect(mySMESHGUI, SIGNAL (SignalStudyFrameChanged()), SLOT(ClickOnCancel()));
387 // set selection mode
388 SMESH::SetPointRepresentation(true);
389 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
390 aViewWindow->SetSelectionMode(NodeSelection);
392 SelectionIntoArgument();
395 //=================================================================================
396 // function : ValueChangedInSpinBox()
398 //=================================================================================
399 void SMESHGUI_NodesDlg::ValueChangedInSpinBox (double newValue)
401 if (!myMesh->_is_nil()) {
402 double vx = SpinBox_X->GetValue();
403 double vy = SpinBox_Y->GetValue();
404 double vz = SpinBox_Z->GetValue();
406 mySimulation->SetPosition(vx,vy,vz);
410 //=================================================================================
411 // function : ClickOnOk()
413 //=================================================================================
414 void SMESHGUI_NodesDlg::ClickOnOk()
420 //=================================================================================
421 // function : ClickOnApply()
423 //=================================================================================
424 bool SMESHGUI_NodesDlg::ClickOnApply()
426 if (mySMESHGUI->isActiveStudyLocked())
429 if (myMesh->_is_nil()) {
430 SUIT_MessageBox::warn1(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
431 tr("MESH_IS_NOT_SELECTED"), tr("SMESH_BUT_OK"));
435 /* Recup args and call method */
436 double x = SpinBox_X->GetValue();
437 double y = SpinBox_Y->GetValue();
438 double z = SpinBox_Z->GetValue();
439 mySimulation->SetVisibility(false);
440 SMESH::AddNode(myMesh,x,y,z);
441 SMESH::SetPointRepresentation(true);
445 mySelectionMgr->selectedObjects(aList);
446 if (aList.Extent() != 1) {
447 if (SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView()) {
448 vtkActorCollection *aCollection = aViewWindow->getRenderer()->GetActors();
449 aCollection->InitTraversal();
450 while (vtkActor *anAct = aCollection->GetNextActor()) {
451 if (SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)) {
452 if (anActor->hasIO()) {
453 if (SMESH_MeshObj *aMeshObj = dynamic_cast<SMESH_MeshObj*>(anActor->GetObject().get())) {
454 if (myMesh->_is_equivalent(aMeshObj->GetMeshServer())) {
456 aList.Append(anActor->getIO());
457 mySelectionMgr->setSelectedObjects(aList, false);
469 //=================================================================================
470 // function : ClickOnCancel()
472 //=================================================================================
473 void SMESHGUI_NodesDlg::ClickOnCancel()
475 disconnect(mySelectionMgr, 0, this, 0);
476 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
477 aViewWindow->SetSelectionMode(ActorSelection);
479 mySimulation->SetVisibility(false);
480 SMESH::SetPointRepresentation(false);
481 mySMESHGUI->ResetState();
486 //=================================================================================
487 // function : ClickOnHelp()
489 //=================================================================================
490 void SMESHGUI_NodesDlg::ClickOnHelp()
492 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
494 app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
496 SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
497 QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
498 arg(app->resourceMgr()->stringValue("ExternalBrowser", "application")).arg(myHelpFileName),
499 QObject::tr("BUT_OK"));
503 //=================================================================================
504 // function : SelectionIntoArgument()
505 // purpose : Called when selection as changed or other case
506 //=================================================================================
507 void SMESHGUI_NodesDlg::SelectionIntoArgument()
509 if (!GroupConstructors->isEnabled())
512 mySimulation->SetVisibility(false);
513 SMESH::SetPointRepresentation(true);
515 const SALOME_ListIO& aList = mySelector->StoredIObjects();
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(mySelector,anIO,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 mySMESHGUI->ResetState();
581 mySMESHGUI->SetActiveDialogBox(0);
585 //=================================================================================
586 // function : ActivateThisDialog()
588 //=================================================================================
589 void SMESHGUI_NodesDlg::ActivateThisDialog()
591 mySMESHGUI->EmitSignalDeactivateDialog();
592 GroupConstructors->setEnabled(true);
593 GroupCoordinates->setEnabled(true);
594 GroupButtons->setEnabled(true);
596 SMESH::SetPointRepresentation(true);
597 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
598 aViewWindow->SetSelectionMode(NodeSelection);
600 SelectionIntoArgument();