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 //asl: theMesh->SetParameters( theParameters.join(":").toLatin1().constData() );
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 SMESHGUI_Helper( theModule ),
227 mySelector( SMESH::GetViewWindow( theModule )->GetSelector() ),
228 mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
229 mySMESHGUI( theModule )
232 setAttribute( Qt::WA_DeleteOnClose, true );
233 setWindowTitle( tr("MESH_NODE_TITLE") );
234 setSizeGripEnabled( true );
236 mySimulation = new SMESH::TNodeSimulation( SMESH::GetViewWindow( mySMESHGUI ) );
238 QPixmap image0( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH",
239 tr( "ICON_DLG_NODE" ) ) );
241 QVBoxLayout* SMESHGUI_NodesDlgLayout = new QVBoxLayout( this );
242 SMESHGUI_NodesDlgLayout->setSpacing( SPACING );
243 SMESHGUI_NodesDlgLayout->setMargin( MARGIN );
245 /***************************************************************/
246 GroupConstructors = new QGroupBox( tr( "MESH_NODE" ), this );
247 QButtonGroup* ButtonGroup = new QButtonGroup(this);
248 QHBoxLayout* GroupConstructorsLayout = new QHBoxLayout( GroupConstructors );
249 GroupConstructorsLayout->setSpacing( SPACING );
250 GroupConstructorsLayout->setMargin( MARGIN );
252 Constructor1 = new QRadioButton( GroupConstructors );
253 Constructor1->setIcon( image0 );
254 Constructor1->setChecked( true );
256 GroupConstructorsLayout->addWidget( Constructor1 );
257 ButtonGroup->addButton( Constructor1, 0 );
259 /***************************************************************/
260 GroupCoordinates = new QGroupBox( tr( "SMESH_COORDINATES" ), this );
261 QHBoxLayout* GroupCoordinatesLayout = new QHBoxLayout(GroupCoordinates);
262 GroupCoordinatesLayout->setSpacing(SPACING);
263 GroupCoordinatesLayout->setMargin(MARGIN);
265 TextLabel_X = new QLabel( tr( "SMESH_X" ), GroupCoordinates );
266 SpinBox_X = new SMESHGUI_SpinBox( GroupCoordinates );
268 TextLabel_Y = new QLabel( tr( "SMESH_Y" ), GroupCoordinates );
269 SpinBox_Y = new SMESHGUI_SpinBox( GroupCoordinates );
271 TextLabel_Z = new QLabel( tr( "SMESH_Z" ), GroupCoordinates );
272 SpinBox_Z = new SMESHGUI_SpinBox( GroupCoordinates );
274 GroupCoordinatesLayout->addWidget( TextLabel_X );
275 GroupCoordinatesLayout->addWidget( SpinBox_X );
276 GroupCoordinatesLayout->addWidget( TextLabel_Y);
277 GroupCoordinatesLayout->addWidget( SpinBox_Y );
278 GroupCoordinatesLayout->addWidget( TextLabel_Z );
279 GroupCoordinatesLayout->addWidget( SpinBox_Z );
281 /***************************************************************/
282 GroupButtons = new QGroupBox( this );
283 QHBoxLayout* GroupButtonsLayout = new QHBoxLayout( GroupButtons );
284 GroupButtonsLayout->setSpacing( SPACING );
285 GroupButtonsLayout->setMargin( MARGIN );
286 buttonOk = new QPushButton( tr( "SMESH_BUT_APPLY_AND_CLOSE" ), GroupButtons );
287 buttonOk->setAutoDefault( true );
288 buttonOk->setDefault( true );
289 buttonApply = new QPushButton( tr( "SMESH_BUT_APPLY" ), GroupButtons );
290 buttonApply->setAutoDefault( true );
291 buttonCancel = new QPushButton( tr( "SMESH_BUT_CLOSE" ), GroupButtons );
292 buttonCancel->setAutoDefault( true );
293 buttonHelp = new QPushButton( tr( "SMESH_BUT_HELP" ), GroupButtons );
294 buttonHelp->setAutoDefault( true );
296 GroupButtonsLayout->addWidget( buttonOk );
297 GroupButtonsLayout->addSpacing( 10 );
298 GroupButtonsLayout->addWidget( buttonApply );
299 GroupButtonsLayout->addSpacing( 10 );
300 GroupButtonsLayout->addStretch();
301 GroupButtonsLayout->addWidget( buttonCancel );
302 GroupButtonsLayout->addWidget( buttonHelp );
304 /***************************************************************/
305 SMESHGUI_NodesDlgLayout->addWidget( GroupConstructors );
306 SMESHGUI_NodesDlgLayout->addWidget( GroupCoordinates );
307 SMESHGUI_NodesDlgLayout->addWidget( GroupButtons );
309 myHelpFileName = "adding_nodes_and_elements_page.html#adding_nodes_anchor";
311 /* Initialisation and display */
315 //=======================================================================
316 // function : ~SMESHGUI_NodesDlg()
317 // purpose : Destructor
318 //=======================================================================
319 SMESHGUI_NodesDlg::~SMESHGUI_NodesDlg()
324 //=================================================================================
327 //=================================================================================
328 void SMESHGUI_NodesDlg::Init()
330 /* Get setting of step value from file configuration */
333 /* min, max, step and decimals for spin boxes */
334 SpinBox_X->RangeStepAndValidator( COORD_MIN, COORD_MAX, step, DBL_DIGITS_DISPLAY );
335 SpinBox_Y->RangeStepAndValidator( COORD_MIN, COORD_MAX, step, DBL_DIGITS_DISPLAY );
336 SpinBox_Z->RangeStepAndValidator( COORD_MIN, COORD_MAX, step, DBL_DIGITS_DISPLAY );
337 SpinBox_X->SetValue( 0.0 );
338 SpinBox_Y->SetValue( 0.0 );
339 SpinBox_Z->SetValue( 0.0 );
341 mySMESHGUI->SetActiveDialogBox( this );
343 /* signals and slots connections */
344 connect( buttonOk, SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
345 connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) );
346 connect( buttonApply, SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
347 connect( buttonHelp, SIGNAL( clicked() ), this, SLOT( ClickOnHelp() ) );
349 connect( SpinBox_X, SIGNAL( valueChanged( double ) ), SLOT( ValueChangedInSpinBox( double ) ) );
350 connect( SpinBox_Y, SIGNAL( valueChanged( double ) ), SLOT( ValueChangedInSpinBox( double ) ) );
351 connect( SpinBox_Z, SIGNAL( valueChanged( double ) ), SLOT( ValueChangedInSpinBox( double ) ) );
353 connect( mySelectionMgr, SIGNAL( currentSelectionChanged() ), SLOT( SelectionIntoArgument() ) );
354 connect( mySMESHGUI, SIGNAL( SignalDeactivateActiveDialog() ), SLOT( DeactivateActiveDialog() ) );
355 /* to close dialog if study frame change */
356 connect( mySMESHGUI, SIGNAL( SignalStudyFrameChanged() ), SLOT( ClickOnCancel() ) );
358 // set selection mode
359 SMESH::SetPointRepresentation( true );
360 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ) )
361 aViewWindow->SetSelectionMode( NodeSelection );
363 SelectionIntoArgument();
366 //=================================================================================
367 // function : ValueChangedInSpinBox()
369 //=================================================================================
370 void SMESHGUI_NodesDlg::ValueChangedInSpinBox( double newValue )
372 if ( !myMesh->_is_nil() ) {
373 double vx = SpinBox_X->GetValue();
374 double vy = SpinBox_Y->GetValue();
375 double vz = SpinBox_Z->GetValue();
377 mySimulation->SetPosition( vx, vy, vz );
381 //=================================================================================
382 // function : ClickOnOk()
384 //=================================================================================
385 void SMESHGUI_NodesDlg::ClickOnOk()
387 if ( ClickOnApply() )
391 //=================================================================================
392 // function : ClickOnApply()
394 //=================================================================================
395 bool SMESHGUI_NodesDlg::ClickOnApply()
397 if ( mySMESHGUI->isActiveStudyLocked() )
400 if ( myMesh->_is_nil() ) {
401 SUIT_MessageBox::warning( this, tr( "SMESH_WRN_WARNING" ),
402 tr( "MESH_IS_NOT_SELECTED" ) );
409 /* Recup args and call method */
410 double x = SpinBox_X->GetValue();
411 double y = SpinBox_Y->GetValue();
412 double z = SpinBox_Z->GetValue();
414 QStringList aParameters;
415 aParameters << SpinBox_X->text();
416 aParameters << SpinBox_Y->text();
417 aParameters << SpinBox_Z->text();
419 mySimulation->SetVisibility( false );
420 SMESH::AddNode( myMesh, x, y, z, aParameters );
421 SMESH::SetPointRepresentation( true );
425 mySelectionMgr->selectedObjects( aList );
426 if ( aList.Extent() != 1 ) {
427 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView() ) {
428 VTK::ActorCollectionCopy aCopy(aViewWindow->getRenderer()->GetActors());
429 vtkActorCollection *aCollection = aCopy.GetActors();
430 aCollection->InitTraversal();
431 while ( vtkActor *anAct = aCollection->GetNextActor() ) {
432 if ( SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>( anAct ) ) {
433 if ( anActor->hasIO() ) {
434 if ( SMESH_MeshObj *aMeshObj = dynamic_cast<SMESH_MeshObj*>( anActor->GetObject().get() ) ) {
435 if ( myMesh->_is_equivalent( aMeshObj->GetMeshServer() ) ) {
437 aList.Append( anActor->getIO() );
438 mySelectionMgr->setSelectedObjects( aList, false );
450 //=================================================================================
451 // function : ClickOnCancel()
453 //=================================================================================
454 void SMESHGUI_NodesDlg::ClickOnCancel()
456 disconnect( mySelectionMgr, 0, this, 0 );
457 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ) )
458 aViewWindow->SetSelectionMode( ActorSelection );
460 mySimulation->SetVisibility( false );
461 SMESH::SetPointRepresentation( false );
462 mySMESHGUI->ResetState();
467 //=================================================================================
468 // function : ClickOnHelp()
470 //=================================================================================
471 void SMESHGUI_NodesDlg::ClickOnHelp()
473 LightApp_Application* app = (LightApp_Application*)( SUIT_Session::session()->activeApplication() );
475 app->onHelpContextModule( mySMESHGUI ? app->moduleName( mySMESHGUI->moduleName() ) :
476 QString( "" ), myHelpFileName );
480 platform = "winapplication";
482 platform = "application";
484 SUIT_MessageBox::warning( this, tr("WRN_WARNING"),
485 tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
486 arg( app->resourceMgr()->stringValue( "ExternalBrowser",
488 arg( myHelpFileName ) );
492 //=================================================================================
493 // function : SelectionIntoArgument()
494 // purpose : Called when selection as changed or other case
495 //=================================================================================
496 void SMESHGUI_NodesDlg::SelectionIntoArgument()
498 if ( !GroupConstructors->isEnabled() )
501 mySimulation->SetVisibility( false );
502 SMESH::SetPointRepresentation( true );
504 const SALOME_ListIO& aList = mySelector->StoredIObjects();
505 if ( aList.Extent() == 1 ) {
506 Handle(SALOME_InteractiveObject) anIO = aList.First();
507 if ( anIO->hasEntry() ) {
508 myMesh = SMESH::GetMeshByIO( anIO );
509 if ( myMesh->_is_nil() ) return;
511 if ( SMESH::GetNameOfSelectedNodes( mySelector, anIO, aText ) == 1 ) {
512 if ( SMESH_Actor* anActor = SMESH::FindActorByObject( myMesh.in() ) ) {
513 if ( SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh() ) {
514 if ( const SMDS_MeshNode* aNode = aMesh->FindNode( aText.toInt() ) ) {
515 SpinBox_X->SetValue( aNode->X() );
516 SpinBox_Y->SetValue( aNode->Y() );
517 SpinBox_Z->SetValue( aNode->Z() );
522 mySimulation->SetPosition( SpinBox_X->GetValue(),
523 SpinBox_Y->GetValue(),
524 SpinBox_Z->GetValue() );
529 //=================================================================================
530 // function : closeEvent()
532 //=================================================================================
533 void SMESHGUI_NodesDlg::closeEvent( QCloseEvent* )
535 this->ClickOnCancel(); /* same than click on cancel button */
538 //=================================================================================
539 // function : hideEvent()
540 // purpose : caused by ESC key
541 //=================================================================================
542 void SMESHGUI_NodesDlg::hideEvent( QHideEvent* )
544 if ( !isMinimized() )
548 //=================================================================================
549 // function : enterEvent()
550 // purpose : to reactivate this dialog box when mouse enter onto the window
551 //=================================================================================
552 void SMESHGUI_NodesDlg::enterEvent( QEvent* )
554 if ( !GroupConstructors->isEnabled() )
555 ActivateThisDialog();
558 //=================================================================================
559 // function : DeactivateActiveDialog()
560 // purpose : public slot to deactivate if active
561 //=================================================================================
562 void SMESHGUI_NodesDlg::DeactivateActiveDialog()
564 if ( GroupConstructors->isEnabled() ) {
565 GroupConstructors->setEnabled( false );
566 GroupCoordinates->setEnabled( false );
567 GroupButtons->setEnabled( false );
568 mySimulation->SetVisibility( false );
569 mySMESHGUI->ResetState();
570 mySMESHGUI->SetActiveDialogBox( 0 );
574 //=================================================================================
575 // function : ActivateThisDialog()
577 //=================================================================================
578 void SMESHGUI_NodesDlg::ActivateThisDialog()
580 mySMESHGUI->EmitSignalDeactivateDialog();
581 GroupConstructors->setEnabled( true );
582 GroupCoordinates->setEnabled( true );
583 GroupButtons->setEnabled( true );
585 SMESH::SetPointRepresentation( true );
586 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ) )
587 aViewWindow->SetSelectionMode( NodeSelection );
589 SelectionIntoArgument();
592 //=================================================================================
593 // function : keyPressEvent()
595 //=================================================================================
596 void SMESHGUI_NodesDlg::keyPressEvent( QKeyEvent* e )
598 QDialog::keyPressEvent( e );
599 if ( e->isAccepted() )
602 if ( e->key() == Qt::Key_F1 ) {
608 //=================================================================================
609 // function : isValid
611 //=================================================================================
612 bool SMESHGUI_NodesDlg::isValid()
614 return checkParameters( true, 3, SpinBox_X, SpinBox_Y, SpinBox_Z );