1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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 // SMESH SMESHGUI : GUI for SMESH component
23 // File : SMESHGUI_NodesDlg.cxx
24 // 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_Algorithm.h>
54 #include <VTKViewer_CellLocationsArray.h>
56 // SALOME KERNEL includes
57 #include <SALOMEDS_Study.hxx>
58 #include <SALOMEDS_SObject.hxx>
60 #include <utilities.h>
63 #include <vtkIdList.h>
64 #include <vtkCellArray.h>
65 #include <vtkUnsignedCharArray.h>
66 #include <vtkUnstructuredGrid.h>
67 #include <vtkDataSetMapper.h>
68 #include <vtkRenderer.h>
69 #include <vtkProperty.h>
70 #include <vtkPoints.h>
75 #include <QPushButton>
76 #include <QRadioButton>
77 #include <QHBoxLayout>
78 #include <QVBoxLayout>
80 #include <QButtonGroup>
83 #include <SALOMEconfig.h>
84 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
91 void AddNode( SMESH::SMESH_Mesh_ptr theMesh, float x, float y, float z, const QStringList& theParameters )
93 SUIT_OverrideCursor wc;
95 _PTR(SObject) aSobj = SMESH::FindSObject( theMesh );
96 SMESH::SMESH_MeshEditor_var aMeshEditor = theMesh->GetMeshEditor();
97 aMeshEditor->AddNode( x, y, z );
98 theMesh->SetParameters( SMESHGUI::JoinObjectParameters(theParameters) );
99 _PTR(Study) aStudy = GetActiveStudyDocument();
100 CORBA::Long anId = aStudy->StudyId();
101 if (TVisualObjPtr aVisualObj = SMESH::GetVisualObj( anId, aSobj->GetID().c_str() ) ) {
102 aVisualObj->Update( true );
105 catch ( SALOME::SALOME_Exception& exc ) {
106 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() );
112 INFOS( "Unknown exception was cought !!!" );
116 class TNodeSimulation
118 SVTK_ViewWindow* myViewWindow;
120 SALOME_Actor* myPreviewActor;
121 vtkDataSetMapper* myMapper;
125 TNodeSimulation( SVTK_ViewWindow* theViewWindow ):
126 myViewWindow( theViewWindow )
128 vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
131 myPoints = vtkPoints::New();
132 myPoints->SetNumberOfPoints( 1 );
133 myPoints->SetPoint( 0, 0.0, 0.0, 0.0 );
136 vtkIdList *anIdList = vtkIdList::New();
137 anIdList->SetNumberOfIds( 1 );
139 vtkCellArray *aCells = vtkCellArray::New();
140 aCells->Allocate( 2, 0 );
142 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
143 aCellTypesArray->SetNumberOfComponents( 1 );
144 aCellTypesArray->Allocate( 1 );
146 anIdList->SetId( 0, 0 );
147 aCells->InsertNextCell( anIdList );
148 aCellTypesArray->InsertNextValue( VTK_VERTEX );
150 VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
151 aCellLocationsArray->SetNumberOfComponents( 1 );
152 aCellLocationsArray->SetNumberOfTuples( 1 );
154 aCells->InitTraversal();
156 aCellLocationsArray->SetValue( 0, aCells->GetTraversalLocation( npts ) );
158 aGrid->SetCells( aCellTypesArray, aCellLocationsArray, aCells );
160 aGrid->SetPoints( myPoints );
161 aGrid->SetCells( aCellTypesArray, aCellLocationsArray, aCells );
162 aCellLocationsArray->Delete();
163 aCellTypesArray->Delete();
167 // Create and display actor
168 myMapper = vtkDataSetMapper::New();
169 myMapper->SetInput( aGrid );
172 myPreviewActor = SALOME_Actor::New();
173 myPreviewActor->SetInfinitive( true );
174 myPreviewActor->VisibilityOff();
175 myPreviewActor->PickableOff();
176 myPreviewActor->SetMapper( myMapper );
178 vtkProperty* aProp = vtkProperty::New();
179 aProp->SetRepresentationToPoints();
181 vtkFloatingPointType anRGB[3];
182 GetColor( "SMESH", "node_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 255, 0 ) );
183 aProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
185 vtkFloatingPointType aPointSize = GetFloat( "SMESH:node_size", 3 );
186 aProp->SetPointSize( aPointSize );
188 myPreviewActor->SetProperty( aProp );
191 myViewWindow->AddActor( myPreviewActor );
194 void SetPosition( float x, float y, float z )
196 myPoints->SetPoint( 0, x, y, z );
197 myPoints->Modified();
198 SetVisibility( true );
201 void SetVisibility( bool theVisibility )
203 myPreviewActor->SetVisibility( theVisibility );
204 RepaintCurrentView();
209 myViewWindow->RemoveActor( myPreviewActor );
210 myPreviewActor->Delete();
212 myMapper->RemoveAllInputs();
220 //=================================================================================
221 // class : SMESHGUI_NodesDlg()
223 //=================================================================================
224 SMESHGUI_NodesDlg::SMESHGUI_NodesDlg( SMESHGUI* theModule ):
225 QDialog( SMESH::GetDesktop( theModule ) ),
226 mySelector( SMESH::GetViewWindow( theModule )->GetSelector() ),
227 mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
228 mySMESHGUI( theModule )
231 setAttribute( Qt::WA_DeleteOnClose, true );
232 setWindowTitle( tr("MESH_NODE_TITLE") );
233 setSizeGripEnabled( true );
235 mySimulation = new SMESH::TNodeSimulation( SMESH::GetViewWindow( mySMESHGUI ) );
237 QPixmap image0( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH",
238 tr( "ICON_DLG_NODE" ) ) );
240 QVBoxLayout* SMESHGUI_NodesDlgLayout = new QVBoxLayout( this );
241 SMESHGUI_NodesDlgLayout->setSpacing( SPACING );
242 SMESHGUI_NodesDlgLayout->setMargin( MARGIN );
244 /***************************************************************/
245 GroupConstructors = new QGroupBox( tr( "MESH_NODE" ), this );
246 QButtonGroup* ButtonGroup = new QButtonGroup(this);
247 QHBoxLayout* GroupConstructorsLayout = new QHBoxLayout( GroupConstructors );
248 GroupConstructorsLayout->setSpacing( SPACING );
249 GroupConstructorsLayout->setMargin( MARGIN );
251 Constructor1 = new QRadioButton( GroupConstructors );
252 Constructor1->setIcon( image0 );
253 Constructor1->setChecked( true );
255 GroupConstructorsLayout->addWidget( Constructor1 );
256 ButtonGroup->addButton( Constructor1, 0 );
258 /***************************************************************/
259 GroupCoordinates = new QGroupBox( tr( "SMESH_COORDINATES" ), this );
260 QHBoxLayout* GroupCoordinatesLayout = new QHBoxLayout(GroupCoordinates);
261 GroupCoordinatesLayout->setSpacing(SPACING);
262 GroupCoordinatesLayout->setMargin(MARGIN);
264 TextLabel_X = new QLabel( tr( "SMESH_X" ), GroupCoordinates );
265 SpinBox_X = new SMESHGUI_SpinBox( GroupCoordinates );
267 TextLabel_Y = new QLabel( tr( "SMESH_Y" ), GroupCoordinates );
268 SpinBox_Y = new SMESHGUI_SpinBox( GroupCoordinates );
270 TextLabel_Z = new QLabel( tr( "SMESH_Z" ), GroupCoordinates );
271 SpinBox_Z = new SMESHGUI_SpinBox( GroupCoordinates );
273 GroupCoordinatesLayout->addWidget( TextLabel_X );
274 GroupCoordinatesLayout->addWidget( SpinBox_X );
275 GroupCoordinatesLayout->addWidget( TextLabel_Y);
276 GroupCoordinatesLayout->addWidget( SpinBox_Y );
277 GroupCoordinatesLayout->addWidget( TextLabel_Z );
278 GroupCoordinatesLayout->addWidget( SpinBox_Z );
280 /***************************************************************/
281 GroupButtons = new QGroupBox( this );
282 QHBoxLayout* GroupButtonsLayout = new QHBoxLayout( GroupButtons );
283 GroupButtonsLayout->setSpacing( SPACING );
284 GroupButtonsLayout->setMargin( MARGIN );
285 buttonOk = new QPushButton( tr( "SMESH_BUT_APPLY_AND_CLOSE" ), GroupButtons );
286 buttonOk->setAutoDefault( true );
287 buttonOk->setDefault( true );
288 buttonApply = new QPushButton( tr( "SMESH_BUT_APPLY" ), GroupButtons );
289 buttonApply->setAutoDefault( true );
290 buttonCancel = new QPushButton( tr( "SMESH_BUT_CLOSE" ), GroupButtons );
291 buttonCancel->setAutoDefault( true );
292 buttonHelp = new QPushButton( tr( "SMESH_BUT_HELP" ), GroupButtons );
293 buttonHelp->setAutoDefault( true );
295 GroupButtonsLayout->addWidget( buttonOk );
296 GroupButtonsLayout->addSpacing( 10 );
297 GroupButtonsLayout->addWidget( buttonApply );
298 GroupButtonsLayout->addSpacing( 10 );
299 GroupButtonsLayout->addStretch();
300 GroupButtonsLayout->addWidget( buttonCancel );
301 GroupButtonsLayout->addWidget( buttonHelp );
303 /***************************************************************/
304 SMESHGUI_NodesDlgLayout->addWidget( GroupConstructors );
305 SMESHGUI_NodesDlgLayout->addWidget( GroupCoordinates );
306 SMESHGUI_NodesDlgLayout->addWidget( GroupButtons );
308 myHelpFileName = "adding_nodes_and_elements_page.html#adding_nodes_anchor";
310 /* Initialisation and display */
314 //=======================================================================
315 // function : ~SMESHGUI_NodesDlg()
316 // purpose : Destructor
317 //=======================================================================
318 SMESHGUI_NodesDlg::~SMESHGUI_NodesDlg()
323 //=================================================================================
326 //=================================================================================
327 void SMESHGUI_NodesDlg::Init()
329 /* Get setting of step value from file configuration */
332 /* min, max, step and decimals for spin boxes */
333 SpinBox_X->RangeStepAndValidator( COORD_MIN, COORD_MAX, step, DBL_DIGITS_DISPLAY );
334 SpinBox_Y->RangeStepAndValidator( COORD_MIN, COORD_MAX, step, DBL_DIGITS_DISPLAY );
335 SpinBox_Z->RangeStepAndValidator( COORD_MIN, COORD_MAX, step, DBL_DIGITS_DISPLAY );
336 SpinBox_X->SetValue( 0.0 );
337 SpinBox_Y->SetValue( 0.0 );
338 SpinBox_Z->SetValue( 0.0 );
340 mySMESHGUI->SetActiveDialogBox( this );
342 /* signals and slots connections */
343 connect( buttonOk, SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
344 connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) );
345 connect( buttonApply, SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
346 connect( buttonHelp, SIGNAL( clicked() ), this, SLOT( ClickOnHelp() ) );
348 connect( SpinBox_X, SIGNAL( valueChanged( double ) ), SLOT( ValueChangedInSpinBox( double ) ) );
349 connect( SpinBox_Y, SIGNAL( valueChanged( double ) ), SLOT( ValueChangedInSpinBox( double ) ) );
350 connect( SpinBox_Z, SIGNAL( valueChanged( double ) ), SLOT( ValueChangedInSpinBox( double ) ) );
352 connect( mySelectionMgr, SIGNAL( currentSelectionChanged() ), SLOT( SelectionIntoArgument() ) );
353 connect( mySMESHGUI, SIGNAL( SignalDeactivateActiveDialog() ), SLOT( DeactivateActiveDialog() ) );
354 /* to close dialog if study frame change */
355 connect( mySMESHGUI, SIGNAL( SignalStudyFrameChanged() ), SLOT( ClickOnCancel() ) );
357 // set selection mode
358 SMESH::SetPointRepresentation( true );
359 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ) )
360 aViewWindow->SetSelectionMode( NodeSelection );
362 SelectionIntoArgument();
365 //=================================================================================
366 // function : ValueChangedInSpinBox()
368 //=================================================================================
369 void SMESHGUI_NodesDlg::ValueChangedInSpinBox( double newValue )
371 if ( !myMesh->_is_nil() ) {
372 double vx = SpinBox_X->GetValue();
373 double vy = SpinBox_Y->GetValue();
374 double vz = SpinBox_Z->GetValue();
376 mySimulation->SetPosition( vx, vy, vz );
380 //=================================================================================
381 // function : ClickOnOk()
383 //=================================================================================
384 void SMESHGUI_NodesDlg::ClickOnOk()
386 if ( ClickOnApply() )
390 //=================================================================================
391 // function : ClickOnApply()
393 //=================================================================================
394 bool SMESHGUI_NodesDlg::ClickOnApply()
396 if ( mySMESHGUI->isActiveStudyLocked() )
399 if ( myMesh->_is_nil() ) {
400 SUIT_MessageBox::warning( this, tr( "SMESH_WRN_WARNING" ),
401 tr( "MESH_IS_NOT_SELECTED" ) );
408 /* Recup args and call method */
409 double x = SpinBox_X->GetValue();
410 double y = SpinBox_Y->GetValue();
411 double z = SpinBox_Z->GetValue();
413 QStringList aParameters;
414 aParameters << SpinBox_X->text();
415 aParameters << SpinBox_Y->text();
416 aParameters << SpinBox_Z->text();
418 mySimulation->SetVisibility( false );
419 SMESH::AddNode( myMesh, x, y, z, aParameters );
420 SMESH::SetPointRepresentation( true );
424 mySelectionMgr->selectedObjects( aList );
425 if ( aList.Extent() != 1 ) {
426 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView() ) {
427 VTK::ActorCollectionCopy aCopy(aViewWindow->getRenderer()->GetActors());
428 vtkActorCollection *aCollection = aCopy.GetActors();
429 aCollection->InitTraversal();
430 while ( vtkActor *anAct = aCollection->GetNextActor() ) {
431 if ( SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>( anAct ) ) {
432 if ( anActor->hasIO() ) {
433 if ( SMESH_MeshObj *aMeshObj = dynamic_cast<SMESH_MeshObj*>( anActor->GetObject().get() ) ) {
434 if ( myMesh->_is_equivalent( aMeshObj->GetMeshServer() ) ) {
436 aList.Append( anActor->getIO() );
437 mySelectionMgr->setSelectedObjects( aList, false );
449 //=================================================================================
450 // function : ClickOnCancel()
452 //=================================================================================
453 void SMESHGUI_NodesDlg::ClickOnCancel()
455 disconnect( mySelectionMgr, 0, this, 0 );
456 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ) )
457 aViewWindow->SetSelectionMode( ActorSelection );
459 mySimulation->SetVisibility( false );
460 SMESH::SetPointRepresentation( false );
461 mySMESHGUI->ResetState();
466 //=================================================================================
467 // function : ClickOnHelp()
469 //=================================================================================
470 void SMESHGUI_NodesDlg::ClickOnHelp()
472 LightApp_Application* app = (LightApp_Application*)( SUIT_Session::session()->activeApplication() );
474 app->onHelpContextModule( mySMESHGUI ? app->moduleName( mySMESHGUI->moduleName() ) :
475 QString( "" ), myHelpFileName );
479 platform = "winapplication";
481 platform = "application";
483 SUIT_MessageBox::warning( this, tr("WRN_WARNING"),
484 tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
485 arg( app->resourceMgr()->stringValue( "ExternalBrowser",
487 arg( myHelpFileName ) );
491 //=================================================================================
492 // function : SelectionIntoArgument()
493 // purpose : Called when selection as changed or other case
494 //=================================================================================
495 void SMESHGUI_NodesDlg::SelectionIntoArgument()
497 if ( !GroupConstructors->isEnabled() )
500 mySimulation->SetVisibility( false );
501 SMESH::SetPointRepresentation( true );
503 const SALOME_ListIO& aList = mySelector->StoredIObjects();
504 if ( aList.Extent() == 1 ) {
505 Handle(SALOME_InteractiveObject) anIO = aList.First();
506 if ( anIO->hasEntry() ) {
507 myMesh = SMESH::GetMeshByIO( anIO );
508 if ( myMesh->_is_nil() ) return;
510 if ( SMESH::GetNameOfSelectedNodes( mySelector, anIO, aText ) == 1 ) {
511 if ( SMESH_Actor* anActor = SMESH::FindActorByObject( myMesh.in() ) ) {
512 if ( SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh() ) {
513 if ( const SMDS_MeshNode* aNode = aMesh->FindNode( aText.toInt() ) ) {
514 SpinBox_X->SetValue( aNode->X() );
515 SpinBox_Y->SetValue( aNode->Y() );
516 SpinBox_Z->SetValue( aNode->Z() );
521 mySimulation->SetPosition( SpinBox_X->GetValue(),
522 SpinBox_Y->GetValue(),
523 SpinBox_Z->GetValue() );
528 //=================================================================================
529 // function : closeEvent()
531 //=================================================================================
532 void SMESHGUI_NodesDlg::closeEvent( QCloseEvent* )
534 this->ClickOnCancel(); /* same than click on cancel button */
537 //=================================================================================
538 // function : hideEvent()
539 // purpose : caused by ESC key
540 //=================================================================================
541 void SMESHGUI_NodesDlg::hideEvent( QHideEvent* )
543 if ( !isMinimized() )
547 //=================================================================================
548 // function : enterEvent()
549 // purpose : to reactivate this dialog box when mouse enter onto the window
550 //=================================================================================
551 void SMESHGUI_NodesDlg::enterEvent( QEvent* )
553 if ( !GroupConstructors->isEnabled() )
554 ActivateThisDialog();
557 //=================================================================================
558 // function : DeactivateActiveDialog()
559 // purpose : public slot to deactivate if active
560 //=================================================================================
561 void SMESHGUI_NodesDlg::DeactivateActiveDialog()
563 if ( GroupConstructors->isEnabled() ) {
564 GroupConstructors->setEnabled( false );
565 GroupCoordinates->setEnabled( false );
566 GroupButtons->setEnabled( false );
567 mySimulation->SetVisibility( false );
568 mySMESHGUI->ResetState();
569 mySMESHGUI->SetActiveDialogBox( 0 );
573 //=================================================================================
574 // function : ActivateThisDialog()
576 //=================================================================================
577 void SMESHGUI_NodesDlg::ActivateThisDialog()
579 mySMESHGUI->EmitSignalDeactivateDialog();
580 GroupConstructors->setEnabled( true );
581 GroupCoordinates->setEnabled( true );
582 GroupButtons->setEnabled( true );
584 SMESH::SetPointRepresentation( true );
585 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ) )
586 aViewWindow->SetSelectionMode( NodeSelection );
588 SelectionIntoArgument();
591 //=================================================================================
592 // function : keyPressEvent()
594 //=================================================================================
595 void SMESHGUI_NodesDlg::keyPressEvent( QKeyEvent* e )
597 QDialog::keyPressEvent( e );
598 if ( e->isAccepted() )
601 if ( e->key() == Qt::Key_F1 ) {
607 //=================================================================================
608 // function : isValid
610 //=================================================================================
611 bool SMESHGUI_NodesDlg::isValid()
615 ok = SpinBox_X->isValid( msg, true ) && ok;
616 ok = SpinBox_Y->isValid( msg, true ) && ok;
617 ok = SpinBox_Z->isValid( msg, true ) && ok;
620 QString str( tr( "SMESH_INCORRECT_INPUT" ) );
621 if ( !msg.isEmpty() )
623 SUIT_MessageBox::critical( this, tr( "SMESH_ERROR" ), str );