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.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : SMESHGUI_NodesDlg.cxx
23 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
27 #include "SMESHGUI_NodesDlg.h"
30 #include "SMESHGUI_SpinBox.h"
31 #include "SMESHGUI_Utils.h"
32 #include "SMESHGUI_VTKUtils.h"
33 #include "SMESHGUI_MeshUtils.h"
35 #include <SMESH_Actor.h>
36 #include <SMESH_ActorUtils.h>
37 #include <SMESH_ObjectDef.h>
39 #include <SMDS_Mesh.hxx>
40 #include <SMDS_MeshNode.hxx>
42 // SALOME GUI includes
43 #include <SUIT_Session.h>
44 #include <SUIT_OverrideCursor.h>
45 #include <SUIT_MessageBox.h>
46 #include <SUIT_Desktop.h>
47 #include <SUIT_ResourceMgr.h>
49 #include <LightApp_Application.h>
50 #include <LightApp_SelectionMgr.h>
52 #include <SVTK_ViewWindow.h>
53 #include <VTKViewer_CellLocationsArray.h>
55 // SALOME KERNEL includes
56 #include <SALOMEDS_Study.hxx>
57 #include <SALOMEDS_SObject.hxx>
59 #include <utilities.h>
62 #include <vtkIdList.h>
63 #include <vtkCellArray.h>
64 #include <vtkUnsignedCharArray.h>
65 #include <vtkUnstructuredGrid.h>
66 #include <vtkDataSetMapper.h>
67 #include <vtkRenderer.h>
68 #include <vtkProperty.h>
69 #include <vtkPoints.h>
74 #include <QPushButton>
75 #include <QRadioButton>
76 #include <QHBoxLayout>
77 #include <QVBoxLayout>
81 #include <SALOMEconfig.h>
82 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
89 void AddNode( SMESH::SMESH_Mesh_ptr theMesh, float x, float y, float z )
91 SUIT_OverrideCursor wc;
93 _PTR(SObject) aSobj = SMESH::FindSObject( theMesh );
94 SMESH::SMESH_MeshEditor_var aMeshEditor = theMesh->GetMeshEditor();
95 aMeshEditor->AddNode( x, y, z );
96 _PTR(Study) aStudy = GetActiveStudyDocument();
97 CORBA::Long anId = aStudy->StudyId();
98 if (TVisualObjPtr aVisualObj = SMESH::GetVisualObj( anId, aSobj->GetID().c_str() ) ) {
99 aVisualObj->Update( true );
102 catch ( SALOME::SALOME_Exception& exc ) {
103 INFOS( "Follow exception was cought:\n\t" << exc.details.text );
105 catch ( const std::exception& exc ) {
106 INFOS( "Follow exception was cought:\n\t" << exc.what() );
109 INFOS( "Unknown exception was cought !!!" );
113 class TNodeSimulation
115 SVTK_ViewWindow* myViewWindow;
117 SALOME_Actor* myPreviewActor;
118 vtkDataSetMapper* myMapper;
122 TNodeSimulation( SVTK_ViewWindow* theViewWindow ):
123 myViewWindow( theViewWindow )
125 vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
128 myPoints = vtkPoints::New();
129 myPoints->SetNumberOfPoints( 1 );
130 myPoints->SetPoint( 0, 0.0, 0.0, 0.0 );
133 vtkIdList *anIdList = vtkIdList::New();
134 anIdList->SetNumberOfIds( 1 );
136 vtkCellArray *aCells = vtkCellArray::New();
137 aCells->Allocate( 2, 0 );
139 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
140 aCellTypesArray->SetNumberOfComponents( 1 );
141 aCellTypesArray->Allocate( 1 );
143 anIdList->SetId( 0, 0 );
144 aCells->InsertNextCell( anIdList );
145 aCellTypesArray->InsertNextValue( VTK_VERTEX );
147 VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
148 aCellLocationsArray->SetNumberOfComponents( 1 );
149 aCellLocationsArray->SetNumberOfTuples( 1 );
151 aCells->InitTraversal();
153 aCellLocationsArray->SetValue( 0, aCells->GetTraversalLocation( npts ) );
155 aGrid->SetCells( aCellTypesArray, aCellLocationsArray, aCells );
157 aGrid->SetPoints( myPoints );
158 aGrid->SetCells( aCellTypesArray, aCellLocationsArray, aCells );
159 aCellLocationsArray->Delete();
160 aCellTypesArray->Delete();
164 // Create and display actor
165 myMapper = vtkDataSetMapper::New();
166 myMapper->SetInput( aGrid );
169 myPreviewActor = SALOME_Actor::New();
170 myPreviewActor->SetInfinitive( true );
171 myPreviewActor->VisibilityOff();
172 myPreviewActor->PickableOff();
173 myPreviewActor->SetMapper( myMapper );
175 vtkProperty* aProp = vtkProperty::New();
176 aProp->SetRepresentationToPoints();
178 vtkFloatingPointType anRGB[3];
179 GetColor( "SMESH", "node_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 255, 0 ) );
180 aProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
182 vtkFloatingPointType aPointSize = GetFloat( "SMESH:node_size", 3 );
183 aProp->SetPointSize( aPointSize );
185 myPreviewActor->SetProperty( aProp );
188 myViewWindow->AddActor( myPreviewActor );
191 void SetPosition( float x, float y, float z )
193 myPoints->SetPoint( 0, x, y, z );
194 myPoints->Modified();
195 SetVisibility( true );
198 void SetVisibility( bool theVisibility )
200 myPreviewActor->SetVisibility( theVisibility );
201 RepaintCurrentView();
206 myViewWindow->RemoveActor( myPreviewActor );
207 myPreviewActor->Delete();
209 myMapper->RemoveAllInputs();
217 //=================================================================================
218 // class : SMESHGUI_NodesDlg()
220 //=================================================================================
221 SMESHGUI_NodesDlg::SMESHGUI_NodesDlg( SMESHGUI* theModule ):
222 QDialog( SMESH::GetDesktop( theModule ) ),
223 mySelector( SMESH::GetViewWindow( theModule )->GetSelector() ),
224 mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
225 mySMESHGUI( theModule )
228 setAttribute( Qt::WA_DeleteOnClose, true );
229 setWindowTitle( "MESH_NODE_TITLE" );
230 setSizeGripEnabled( true );
232 mySimulation = new SMESH::TNodeSimulation( SMESH::GetViewWindow( mySMESHGUI ) );
234 QPixmap image0( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH",
235 tr( "ICON_DLG_NODE" ) ) );
237 QVBoxLayout* SMESHGUI_NodesDlgLayout = new QVBoxLayout( this );
238 SMESHGUI_NodesDlgLayout->setSpacing( SPACING );
239 SMESHGUI_NodesDlgLayout->setMargin( MARGIN );
241 /***************************************************************/
242 GroupConstructors = new QGroupBox( tr( "MESH_NODE" ), this );
243 QHBoxLayout* GroupConstructorsLayout = new QHBoxLayout( GroupConstructors );
244 GroupConstructorsLayout->setSpacing( SPACING );
245 GroupConstructorsLayout->setMargin( MARGIN );
247 Constructor1 = new QRadioButton( GroupConstructors );
248 Constructor1->setIcon( image0 );
249 Constructor1->setChecked( true );
251 GroupConstructorsLayout->addWidget( Constructor1 );
252 GroupConstructorsLayout->addStretch();
254 /***************************************************************/
255 GroupCoordinates = new QGroupBox( tr( "SMESH_COORDINATES" ), this );
256 QHBoxLayout* GroupCoordinatesLayout = new QHBoxLayout(GroupCoordinates);
257 GroupCoordinatesLayout->setSpacing(SPACING);
258 GroupCoordinatesLayout->setMargin(MARGIN);
260 TextLabel_X = new QLabel( tr( "SMESH_X" ), GroupCoordinates );
261 SpinBox_X = new SMESHGUI_SpinBox( GroupCoordinates );
263 TextLabel_Y = new QLabel( tr( "SMESH_Y" ), GroupCoordinates );
264 SpinBox_Y = new SMESHGUI_SpinBox( GroupCoordinates );
266 TextLabel_Z = new QLabel( tr( "SMESH_Z" ), GroupCoordinates );
267 SpinBox_Z = new SMESHGUI_SpinBox( GroupCoordinates );
269 GroupCoordinatesLayout->addWidget( TextLabel_X );
270 GroupCoordinatesLayout->addWidget( SpinBox_X );
271 GroupCoordinatesLayout->addWidget( TextLabel_Y);
272 GroupCoordinatesLayout->addWidget( SpinBox_Y );
273 GroupCoordinatesLayout->addWidget( TextLabel_Z );
274 GroupCoordinatesLayout->addWidget( SpinBox_Z );
276 /***************************************************************/
277 GroupButtons = new QGroupBox( this );
278 QHBoxLayout* GroupButtonsLayout = new QHBoxLayout( GroupButtons );
279 GroupButtonsLayout->setSpacing( SPACING );
280 GroupButtonsLayout->setMargin( MARGIN );
281 buttonOk = new QPushButton( tr( "SMESH_BUT_OK" ), GroupButtons );
282 buttonOk->setAutoDefault( true );
283 buttonOk->setDefault( true );
284 buttonApply = new QPushButton( tr( "SMESH_BUT_APPLY" ), GroupButtons );
285 buttonApply->setAutoDefault( true );
286 buttonCancel = new QPushButton( tr( "SMESH_BUT_CLOSE" ), GroupButtons );
287 buttonCancel->setAutoDefault( true );
288 buttonHelp = new QPushButton( tr( "SMESH_BUT_HELP" ), GroupButtons );
289 buttonHelp->setAutoDefault( true );
291 GroupButtonsLayout->addWidget( buttonOk );
292 GroupButtonsLayout->addSpacing( 10 );
293 GroupButtonsLayout->addWidget( buttonApply );
294 GroupButtonsLayout->addSpacing( 10 );
295 GroupButtonsLayout->addStretch();
296 GroupButtonsLayout->addWidget( buttonCancel );
297 GroupButtonsLayout->addWidget( buttonHelp );
299 /***************************************************************/
300 SMESHGUI_NodesDlgLayout->addWidget( GroupConstructors );
301 SMESHGUI_NodesDlgLayout->addWidget( GroupCoordinates );
302 SMESHGUI_NodesDlgLayout->addWidget( GroupButtons );
304 myHelpFileName = "adding_nodes_and_elements_page.html#adding_nodes_anchor";
306 /* Initialisation and display */
310 //=======================================================================
311 // function : ~SMESHGUI_NodesDlg()
312 // purpose : Destructor
313 //=======================================================================
314 SMESHGUI_NodesDlg::~SMESHGUI_NodesDlg()
319 //=================================================================================
322 //=================================================================================
323 void SMESHGUI_NodesDlg::Init()
325 /* Get setting of step value from file configuration */
328 /* min, max, step and decimals for spin boxes */
329 SpinBox_X->RangeStepAndValidator( COORD_MIN, COORD_MAX, step, DBL_DIGITS_DISPLAY );
330 SpinBox_Y->RangeStepAndValidator( COORD_MIN, COORD_MAX, step, DBL_DIGITS_DISPLAY );
331 SpinBox_Z->RangeStepAndValidator( COORD_MIN, COORD_MAX, step, DBL_DIGITS_DISPLAY );
332 SpinBox_X->SetValue( 0.0 );
333 SpinBox_Y->SetValue( 0.0 );
334 SpinBox_Z->SetValue( 0.0 );
336 mySMESHGUI->SetActiveDialogBox( this );
338 /* signals and slots connections */
339 connect( buttonOk, SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
340 connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) );
341 connect( buttonApply, SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
342 connect( buttonHelp, SIGNAL( clicked() ), this, SLOT( ClickOnHelp() ) );
344 connect( SpinBox_X, SIGNAL( valueChanged( double ) ), SLOT( ValueChangedInSpinBox( double ) ) );
345 connect( SpinBox_Y, SIGNAL( valueChanged( double ) ), SLOT( ValueChangedInSpinBox( double ) ) );
346 connect( SpinBox_Z, SIGNAL( valueChanged( double ) ), SLOT( ValueChangedInSpinBox( double ) ) );
348 connect( mySelectionMgr, SIGNAL( currentSelectionChanged() ), SLOT( SelectionIntoArgument() ) );
349 connect( mySMESHGUI, SIGNAL( SignalDeactivateActiveDialog() ), SLOT( DeactivateActiveDialog() ) );
350 /* to close dialog if study frame change */
351 connect( mySMESHGUI, SIGNAL( SignalStudyFrameChanged() ), SLOT( ClickOnCancel() ) );
353 // set selection mode
354 SMESH::SetPointRepresentation( true );
355 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ) )
356 aViewWindow->SetSelectionMode( NodeSelection );
358 SelectionIntoArgument();
361 //=================================================================================
362 // function : ValueChangedInSpinBox()
364 //=================================================================================
365 void SMESHGUI_NodesDlg::ValueChangedInSpinBox( double newValue )
367 if ( !myMesh->_is_nil() ) {
368 double vx = SpinBox_X->GetValue();
369 double vy = SpinBox_Y->GetValue();
370 double vz = SpinBox_Z->GetValue();
372 mySimulation->SetPosition( vx, vy, vz );
376 //=================================================================================
377 // function : ClickOnOk()
379 //=================================================================================
380 void SMESHGUI_NodesDlg::ClickOnOk()
382 if ( ClickOnApply() )
386 //=================================================================================
387 // function : ClickOnApply()
389 //=================================================================================
390 bool SMESHGUI_NodesDlg::ClickOnApply()
392 if ( mySMESHGUI->isActiveStudyLocked() )
395 if ( myMesh->_is_nil() ) {
396 SUIT_MessageBox::warning( this, tr( "SMESH_WRN_WARNING" ),
397 tr( "MESH_IS_NOT_SELECTED" ) );
401 /* Recup args and call method */
402 double x = SpinBox_X->GetValue();
403 double y = SpinBox_Y->GetValue();
404 double z = SpinBox_Z->GetValue();
405 mySimulation->SetVisibility( false );
406 SMESH::AddNode( myMesh, x, y, z );
407 SMESH::SetPointRepresentation( true );
411 mySelectionMgr->selectedObjects( aList );
412 if ( aList.Extent() != 1 ) {
413 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView() ) {
414 vtkActorCollection *aCollection = aViewWindow->getRenderer()->GetActors();
415 aCollection->InitTraversal();
416 while ( vtkActor *anAct = aCollection->GetNextActor() ) {
417 if ( SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>( anAct ) ) {
418 if ( anActor->hasIO() ) {
419 if ( SMESH_MeshObj *aMeshObj = dynamic_cast<SMESH_MeshObj*>( anActor->GetObject().get() ) ) {
420 if ( myMesh->_is_equivalent( aMeshObj->GetMeshServer() ) ) {
422 aList.Append( anActor->getIO() );
423 mySelectionMgr->setSelectedObjects( aList, false );
435 //=================================================================================
436 // function : ClickOnCancel()
438 //=================================================================================
439 void SMESHGUI_NodesDlg::ClickOnCancel()
441 disconnect( mySelectionMgr, 0, this, 0 );
442 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ) )
443 aViewWindow->SetSelectionMode( ActorSelection );
445 mySimulation->SetVisibility( false );
446 SMESH::SetPointRepresentation( false );
447 mySMESHGUI->ResetState();
452 //=================================================================================
453 // function : ClickOnHelp()
455 //=================================================================================
456 void SMESHGUI_NodesDlg::ClickOnHelp()
458 LightApp_Application* app = (LightApp_Application*)( SUIT_Session::session()->activeApplication() );
460 app->onHelpContextModule( mySMESHGUI ? app->moduleName( mySMESHGUI->moduleName() ) :
461 QString( "" ), myHelpFileName );
465 platform = "winapplication";
467 platform = "application";
469 SUIT_MessageBox::warning( this, tr("WRN_WARNING"),
470 tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
471 arg( app->resourceMgr()->stringValue( "ExternalBrowser",
473 arg( myHelpFileName ) );
477 //=================================================================================
478 // function : SelectionIntoArgument()
479 // purpose : Called when selection as changed or other case
480 //=================================================================================
481 void SMESHGUI_NodesDlg::SelectionIntoArgument()
483 if ( !GroupConstructors->isEnabled() )
486 mySimulation->SetVisibility( false );
487 SMESH::SetPointRepresentation( true );
489 const SALOME_ListIO& aList = mySelector->StoredIObjects();
490 if ( aList.Extent() == 1 ) {
491 Handle(SALOME_InteractiveObject) anIO = aList.First();
492 if ( anIO->hasEntry() ) {
493 myMesh = SMESH::GetMeshByIO( anIO );
494 if ( myMesh->_is_nil() ) return;
496 if ( SMESH::GetNameOfSelectedNodes( mySelector, anIO, aText ) == 1 ) {
497 if ( SMESH_Actor* anActor = SMESH::FindActorByObject( myMesh.in() ) ) {
498 if ( SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh() ) {
499 if ( const SMDS_MeshNode* aNode = aMesh->FindNode( aText.toInt() ) ) {
500 SpinBox_X->SetValue( aNode->X() );
501 SpinBox_Y->SetValue( aNode->Y() );
502 SpinBox_Z->SetValue( aNode->Z() );
507 mySimulation->SetPosition( SpinBox_X->GetValue(),
508 SpinBox_Y->GetValue(),
509 SpinBox_Z->GetValue() );
514 //=================================================================================
515 // function : closeEvent()
517 //=================================================================================
518 void SMESHGUI_NodesDlg::closeEvent( QCloseEvent* )
520 this->ClickOnCancel(); /* same than click on cancel button */
523 //=================================================================================
524 // function : hideEvent()
525 // purpose : caused by ESC key
526 //=================================================================================
527 void SMESHGUI_NodesDlg::hideEvent( QHideEvent* )
529 if ( !isMinimized() )
533 //=================================================================================
534 // function : enterEvent()
535 // purpose : to reactivate this dialog box when mouse enter onto the window
536 //=================================================================================
537 void SMESHGUI_NodesDlg::enterEvent( QEvent* )
539 if ( !GroupConstructors->isEnabled() )
540 ActivateThisDialog();
543 //=================================================================================
544 // function : DeactivateActiveDialog()
545 // purpose : public slot to deactivate if active
546 //=================================================================================
547 void SMESHGUI_NodesDlg::DeactivateActiveDialog()
549 if ( GroupConstructors->isEnabled() ) {
550 GroupConstructors->setEnabled( false );
551 GroupCoordinates->setEnabled( false );
552 GroupButtons->setEnabled( false );
553 mySimulation->SetVisibility( false );
554 mySMESHGUI->ResetState();
555 mySMESHGUI->SetActiveDialogBox( 0 );
559 //=================================================================================
560 // function : ActivateThisDialog()
562 //=================================================================================
563 void SMESHGUI_NodesDlg::ActivateThisDialog()
565 mySMESHGUI->EmitSignalDeactivateDialog();
566 GroupConstructors->setEnabled( true );
567 GroupCoordinates->setEnabled( true );
568 GroupButtons->setEnabled( true );
570 SMESH::SetPointRepresentation( true );
571 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ) )
572 aViewWindow->SetSelectionMode( NodeSelection );
574 SelectionIntoArgument();
577 //=================================================================================
578 // function : keyPressEvent()
580 //=================================================================================
581 void SMESHGUI_NodesDlg::keyPressEvent( QKeyEvent* e )
583 QDialog::keyPressEvent( e );
584 if ( e->isAccepted() )
587 if ( e->key() == Qt::Key_F1 ) {