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_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>
79 #include <QButtonGroup>
82 #include <SALOMEconfig.h>
83 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
90 void AddNode( SMESH::SMESH_Mesh_ptr theMesh, float x, float y, float z, const QStringList& theParameters )
92 SUIT_OverrideCursor wc;
94 _PTR(SObject) aSobj = SMESH::FindSObject( theMesh );
95 SMESH::SMESH_MeshEditor_var aMeshEditor = theMesh->GetMeshEditor();
96 aMeshEditor->AddNode( x, y, z );
97 theMesh->SetParameters( SMESHGUI::JoinObjectParameters(theParameters) );
98 _PTR(Study) aStudy = GetActiveStudyDocument();
99 CORBA::Long anId = aStudy->StudyId();
100 if (TVisualObjPtr aVisualObj = SMESH::GetVisualObj( anId, aSobj->GetID().c_str() ) ) {
101 aVisualObj->Update( true );
104 catch ( SALOME::SALOME_Exception& exc ) {
105 INFOS( "Follow exception was cought:\n\t" << exc.details.text );
107 catch ( const std::exception& exc ) {
108 INFOS( "Follow exception was cought:\n\t" << exc.what() );
111 INFOS( "Unknown exception was cought !!!" );
115 class TNodeSimulation
117 SVTK_ViewWindow* myViewWindow;
119 SALOME_Actor* myPreviewActor;
120 vtkDataSetMapper* myMapper;
124 TNodeSimulation( SVTK_ViewWindow* theViewWindow ):
125 myViewWindow( theViewWindow )
127 vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
130 myPoints = vtkPoints::New();
131 myPoints->SetNumberOfPoints( 1 );
132 myPoints->SetPoint( 0, 0.0, 0.0, 0.0 );
135 vtkIdList *anIdList = vtkIdList::New();
136 anIdList->SetNumberOfIds( 1 );
138 vtkCellArray *aCells = vtkCellArray::New();
139 aCells->Allocate( 2, 0 );
141 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
142 aCellTypesArray->SetNumberOfComponents( 1 );
143 aCellTypesArray->Allocate( 1 );
145 anIdList->SetId( 0, 0 );
146 aCells->InsertNextCell( anIdList );
147 aCellTypesArray->InsertNextValue( VTK_VERTEX );
149 VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
150 aCellLocationsArray->SetNumberOfComponents( 1 );
151 aCellLocationsArray->SetNumberOfTuples( 1 );
153 aCells->InitTraversal();
155 aCellLocationsArray->SetValue( 0, aCells->GetTraversalLocation( npts ) );
157 aGrid->SetCells( aCellTypesArray, aCellLocationsArray, aCells );
159 aGrid->SetPoints( myPoints );
160 aGrid->SetCells( aCellTypesArray, aCellLocationsArray, aCells );
161 aCellLocationsArray->Delete();
162 aCellTypesArray->Delete();
166 // Create and display actor
167 myMapper = vtkDataSetMapper::New();
168 myMapper->SetInput( aGrid );
171 myPreviewActor = SALOME_Actor::New();
172 myPreviewActor->SetInfinitive( true );
173 myPreviewActor->VisibilityOff();
174 myPreviewActor->PickableOff();
175 myPreviewActor->SetMapper( myMapper );
177 vtkProperty* aProp = vtkProperty::New();
178 aProp->SetRepresentationToPoints();
180 vtkFloatingPointType anRGB[3];
181 GetColor( "SMESH", "node_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 255, 0 ) );
182 aProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
184 vtkFloatingPointType aPointSize = GetFloat( "SMESH:node_size", 3 );
185 aProp->SetPointSize( aPointSize );
187 myPreviewActor->SetProperty( aProp );
190 myViewWindow->AddActor( myPreviewActor );
193 void SetPosition( float x, float y, float z )
195 myPoints->SetPoint( 0, x, y, z );
196 myPoints->Modified();
197 SetVisibility( true );
200 void SetVisibility( bool theVisibility )
202 myPreviewActor->SetVisibility( theVisibility );
203 RepaintCurrentView();
208 myViewWindow->RemoveActor( myPreviewActor );
209 myPreviewActor->Delete();
211 myMapper->RemoveAllInputs();
219 //=================================================================================
220 // class : SMESHGUI_NodesDlg()
222 //=================================================================================
223 SMESHGUI_NodesDlg::SMESHGUI_NodesDlg( SMESHGUI* theModule ):
224 QDialog( SMESH::GetDesktop( theModule ) ),
225 mySelector( SMESH::GetViewWindow( theModule )->GetSelector() ),
226 mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
227 mySMESHGUI( theModule )
230 setAttribute( Qt::WA_DeleteOnClose, true );
231 setWindowTitle( tr("MESH_NODE_TITLE") );
232 setSizeGripEnabled( true );
234 mySimulation = new SMESH::TNodeSimulation( SMESH::GetViewWindow( mySMESHGUI ) );
236 QPixmap image0( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH",
237 tr( "ICON_DLG_NODE" ) ) );
239 QVBoxLayout* SMESHGUI_NodesDlgLayout = new QVBoxLayout( this );
240 SMESHGUI_NodesDlgLayout->setSpacing( SPACING );
241 SMESHGUI_NodesDlgLayout->setMargin( MARGIN );
243 /***************************************************************/
244 GroupConstructors = new QGroupBox( tr( "MESH_NODE" ), this );
245 QButtonGroup* ButtonGroup = new QButtonGroup(this);
246 QHBoxLayout* GroupConstructorsLayout = new QHBoxLayout( GroupConstructors );
247 GroupConstructorsLayout->setSpacing( SPACING );
248 GroupConstructorsLayout->setMargin( MARGIN );
250 Constructor1 = new QRadioButton( GroupConstructors );
251 Constructor1->setIcon( image0 );
252 Constructor1->setChecked( true );
254 GroupConstructorsLayout->addWidget( Constructor1 );
255 ButtonGroup->addButton( Constructor1, 0 );
257 /***************************************************************/
258 GroupCoordinates = new QGroupBox( tr( "SMESH_COORDINATES" ), this );
259 QHBoxLayout* GroupCoordinatesLayout = new QHBoxLayout(GroupCoordinates);
260 GroupCoordinatesLayout->setSpacing(SPACING);
261 GroupCoordinatesLayout->setMargin(MARGIN);
263 TextLabel_X = new QLabel( tr( "SMESH_X" ), GroupCoordinates );
264 SpinBox_X = new SMESHGUI_SpinBox( GroupCoordinates );
266 TextLabel_Y = new QLabel( tr( "SMESH_Y" ), GroupCoordinates );
267 SpinBox_Y = new SMESHGUI_SpinBox( GroupCoordinates );
269 TextLabel_Z = new QLabel( tr( "SMESH_Z" ), GroupCoordinates );
270 SpinBox_Z = new SMESHGUI_SpinBox( GroupCoordinates );
272 GroupCoordinatesLayout->addWidget( TextLabel_X );
273 GroupCoordinatesLayout->addWidget( SpinBox_X );
274 GroupCoordinatesLayout->addWidget( TextLabel_Y);
275 GroupCoordinatesLayout->addWidget( SpinBox_Y );
276 GroupCoordinatesLayout->addWidget( TextLabel_Z );
277 GroupCoordinatesLayout->addWidget( SpinBox_Z );
279 /***************************************************************/
280 GroupButtons = new QGroupBox( this );
281 QHBoxLayout* GroupButtonsLayout = new QHBoxLayout( GroupButtons );
282 GroupButtonsLayout->setSpacing( SPACING );
283 GroupButtonsLayout->setMargin( MARGIN );
284 buttonOk = new QPushButton( tr( "SMESH_BUT_APPLY_AND_CLOSE" ), GroupButtons );
285 buttonOk->setAutoDefault( true );
286 buttonOk->setDefault( true );
287 buttonApply = new QPushButton( tr( "SMESH_BUT_APPLY" ), GroupButtons );
288 buttonApply->setAutoDefault( true );
289 buttonCancel = new QPushButton( tr( "SMESH_BUT_CLOSE" ), GroupButtons );
290 buttonCancel->setAutoDefault( true );
291 buttonHelp = new QPushButton( tr( "SMESH_BUT_HELP" ), GroupButtons );
292 buttonHelp->setAutoDefault( true );
294 GroupButtonsLayout->addWidget( buttonOk );
295 GroupButtonsLayout->addSpacing( 10 );
296 GroupButtonsLayout->addWidget( buttonApply );
297 GroupButtonsLayout->addSpacing( 10 );
298 GroupButtonsLayout->addStretch();
299 GroupButtonsLayout->addWidget( buttonCancel );
300 GroupButtonsLayout->addWidget( buttonHelp );
302 /***************************************************************/
303 SMESHGUI_NodesDlgLayout->addWidget( GroupConstructors );
304 SMESHGUI_NodesDlgLayout->addWidget( GroupCoordinates );
305 SMESHGUI_NodesDlgLayout->addWidget( GroupButtons );
307 myHelpFileName = "adding_nodes_and_elements_page.html#adding_nodes_anchor";
309 /* Initialisation and display */
313 //=======================================================================
314 // function : ~SMESHGUI_NodesDlg()
315 // purpose : Destructor
316 //=======================================================================
317 SMESHGUI_NodesDlg::~SMESHGUI_NodesDlg()
322 //=================================================================================
325 //=================================================================================
326 void SMESHGUI_NodesDlg::Init()
328 /* Get setting of step value from file configuration */
331 /* min, max, step and decimals for spin boxes */
332 SpinBox_X->RangeStepAndValidator( COORD_MIN, COORD_MAX, step, DBL_DIGITS_DISPLAY );
333 SpinBox_Y->RangeStepAndValidator( COORD_MIN, COORD_MAX, step, DBL_DIGITS_DISPLAY );
334 SpinBox_Z->RangeStepAndValidator( COORD_MIN, COORD_MAX, step, DBL_DIGITS_DISPLAY );
335 SpinBox_X->SetValue( 0.0 );
336 SpinBox_Y->SetValue( 0.0 );
337 SpinBox_Z->SetValue( 0.0 );
339 mySMESHGUI->SetActiveDialogBox( this );
341 /* signals and slots connections */
342 connect( buttonOk, SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
343 connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) );
344 connect( buttonApply, SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
345 connect( buttonHelp, SIGNAL( clicked() ), this, SLOT( ClickOnHelp() ) );
347 connect( SpinBox_X, SIGNAL( valueChanged( double ) ), SLOT( ValueChangedInSpinBox( double ) ) );
348 connect( SpinBox_Y, SIGNAL( valueChanged( double ) ), SLOT( ValueChangedInSpinBox( double ) ) );
349 connect( SpinBox_Z, SIGNAL( valueChanged( double ) ), SLOT( ValueChangedInSpinBox( double ) ) );
351 connect( mySelectionMgr, SIGNAL( currentSelectionChanged() ), SLOT( SelectionIntoArgument() ) );
352 connect( mySMESHGUI, SIGNAL( SignalDeactivateActiveDialog() ), SLOT( DeactivateActiveDialog() ) );
353 /* to close dialog if study frame change */
354 connect( mySMESHGUI, SIGNAL( SignalStudyFrameChanged() ), SLOT( ClickOnCancel() ) );
356 // set selection mode
357 SMESH::SetPointRepresentation( true );
358 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ) )
359 aViewWindow->SetSelectionMode( NodeSelection );
361 SelectionIntoArgument();
364 //=================================================================================
365 // function : ValueChangedInSpinBox()
367 //=================================================================================
368 void SMESHGUI_NodesDlg::ValueChangedInSpinBox( double newValue )
370 if ( !myMesh->_is_nil() ) {
371 double vx = SpinBox_X->GetValue();
372 double vy = SpinBox_Y->GetValue();
373 double vz = SpinBox_Z->GetValue();
375 mySimulation->SetPosition( vx, vy, vz );
379 //=================================================================================
380 // function : ClickOnOk()
382 //=================================================================================
383 void SMESHGUI_NodesDlg::ClickOnOk()
385 if ( ClickOnApply() )
389 //=================================================================================
390 // function : ClickOnApply()
392 //=================================================================================
393 bool SMESHGUI_NodesDlg::ClickOnApply()
395 if ( mySMESHGUI->isActiveStudyLocked() )
398 if ( myMesh->_is_nil() ) {
399 SUIT_MessageBox::warning( this, tr( "SMESH_WRN_WARNING" ),
400 tr( "MESH_IS_NOT_SELECTED" ) );
407 /* Recup args and call method */
408 double x = SpinBox_X->GetValue();
409 double y = SpinBox_Y->GetValue();
410 double z = SpinBox_Z->GetValue();
412 QStringList aParameters;
413 aParameters << SpinBox_X->text();
414 aParameters << SpinBox_Y->text();
415 aParameters << SpinBox_Z->text();
417 mySimulation->SetVisibility( false );
418 SMESH::AddNode( myMesh, x, y, z, aParameters );
419 SMESH::SetPointRepresentation( true );
423 mySelectionMgr->selectedObjects( aList );
424 if ( aList.Extent() != 1 ) {
425 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView() ) {
426 vtkActorCollection *aCollection = aViewWindow->getRenderer()->GetActors();
427 aCollection->InitTraversal();
428 while ( vtkActor *anAct = aCollection->GetNextActor() ) {
429 if ( SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>( anAct ) ) {
430 if ( anActor->hasIO() ) {
431 if ( SMESH_MeshObj *aMeshObj = dynamic_cast<SMESH_MeshObj*>( anActor->GetObject().get() ) ) {
432 if ( myMesh->_is_equivalent( aMeshObj->GetMeshServer() ) ) {
434 aList.Append( anActor->getIO() );
435 mySelectionMgr->setSelectedObjects( aList, false );
447 //=================================================================================
448 // function : ClickOnCancel()
450 //=================================================================================
451 void SMESHGUI_NodesDlg::ClickOnCancel()
453 disconnect( mySelectionMgr, 0, this, 0 );
454 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ) )
455 aViewWindow->SetSelectionMode( ActorSelection );
457 mySimulation->SetVisibility( false );
458 SMESH::SetPointRepresentation( false );
459 mySMESHGUI->ResetState();
464 //=================================================================================
465 // function : ClickOnHelp()
467 //=================================================================================
468 void SMESHGUI_NodesDlg::ClickOnHelp()
470 LightApp_Application* app = (LightApp_Application*)( SUIT_Session::session()->activeApplication() );
472 app->onHelpContextModule( mySMESHGUI ? app->moduleName( mySMESHGUI->moduleName() ) :
473 QString( "" ), myHelpFileName );
477 platform = "winapplication";
479 platform = "application";
481 SUIT_MessageBox::warning( this, tr("WRN_WARNING"),
482 tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
483 arg( app->resourceMgr()->stringValue( "ExternalBrowser",
485 arg( myHelpFileName ) );
489 //=================================================================================
490 // function : SelectionIntoArgument()
491 // purpose : Called when selection as changed or other case
492 //=================================================================================
493 void SMESHGUI_NodesDlg::SelectionIntoArgument()
495 if ( !GroupConstructors->isEnabled() )
498 mySimulation->SetVisibility( false );
499 SMESH::SetPointRepresentation( true );
501 const SALOME_ListIO& aList = mySelector->StoredIObjects();
502 if ( aList.Extent() == 1 ) {
503 Handle(SALOME_InteractiveObject) anIO = aList.First();
504 if ( anIO->hasEntry() ) {
505 myMesh = SMESH::GetMeshByIO( anIO );
506 if ( myMesh->_is_nil() ) return;
508 if ( SMESH::GetNameOfSelectedNodes( mySelector, anIO, aText ) == 1 ) {
509 if ( SMESH_Actor* anActor = SMESH::FindActorByObject( myMesh.in() ) ) {
510 if ( SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh() ) {
511 if ( const SMDS_MeshNode* aNode = aMesh->FindNode( aText.toInt() ) ) {
512 SpinBox_X->SetValue( aNode->X() );
513 SpinBox_Y->SetValue( aNode->Y() );
514 SpinBox_Z->SetValue( aNode->Z() );
519 mySimulation->SetPosition( SpinBox_X->GetValue(),
520 SpinBox_Y->GetValue(),
521 SpinBox_Z->GetValue() );
526 //=================================================================================
527 // function : closeEvent()
529 //=================================================================================
530 void SMESHGUI_NodesDlg::closeEvent( QCloseEvent* )
532 this->ClickOnCancel(); /* same than click on cancel button */
535 //=================================================================================
536 // function : hideEvent()
537 // purpose : caused by ESC key
538 //=================================================================================
539 void SMESHGUI_NodesDlg::hideEvent( QHideEvent* )
541 if ( !isMinimized() )
545 //=================================================================================
546 // function : enterEvent()
547 // purpose : to reactivate this dialog box when mouse enter onto the window
548 //=================================================================================
549 void SMESHGUI_NodesDlg::enterEvent( QEvent* )
551 if ( !GroupConstructors->isEnabled() )
552 ActivateThisDialog();
555 //=================================================================================
556 // function : DeactivateActiveDialog()
557 // purpose : public slot to deactivate if active
558 //=================================================================================
559 void SMESHGUI_NodesDlg::DeactivateActiveDialog()
561 if ( GroupConstructors->isEnabled() ) {
562 GroupConstructors->setEnabled( false );
563 GroupCoordinates->setEnabled( false );
564 GroupButtons->setEnabled( false );
565 mySimulation->SetVisibility( false );
566 mySMESHGUI->ResetState();
567 mySMESHGUI->SetActiveDialogBox( 0 );
571 //=================================================================================
572 // function : ActivateThisDialog()
574 //=================================================================================
575 void SMESHGUI_NodesDlg::ActivateThisDialog()
577 mySMESHGUI->EmitSignalDeactivateDialog();
578 GroupConstructors->setEnabled( true );
579 GroupCoordinates->setEnabled( true );
580 GroupButtons->setEnabled( true );
582 SMESH::SetPointRepresentation( true );
583 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ) )
584 aViewWindow->SetSelectionMode( NodeSelection );
586 SelectionIntoArgument();
589 //=================================================================================
590 // function : keyPressEvent()
592 //=================================================================================
593 void SMESHGUI_NodesDlg::keyPressEvent( QKeyEvent* e )
595 QDialog::keyPressEvent( e );
596 if ( e->isAccepted() )
599 if ( e->key() == Qt::Key_F1 ) {
605 //=================================================================================
606 // function : isValid
608 //=================================================================================
609 bool SMESHGUI_NodesDlg::isValid()
613 ok = SpinBox_X->isValid( msg, true ) && ok;
614 ok = SpinBox_Y->isValid( msg, true ) && ok;
615 ok = SpinBox_Z->isValid( msg, true ) && ok;
618 QString str( tr( "SMESH_INCORRECT_INPUT" ) );
619 if ( !msg.isEmpty() )
621 SUIT_MessageBox::critical( this, tr( "SMESH_ERROR" ), str );