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 <SalomeApp_Notebook.h>
54 #include <SVTK_ViewWindow.h>
55 #include <VTKViewer_Algorithm.h>
56 #include <VTKViewer_CellLocationsArray.h>
58 // SALOME KERNEL includes
59 #include <SALOMEDS_Study.hxx>
60 #include <SALOMEDS_SObject.hxx>
62 #include <utilities.h>
65 #include <vtkIdList.h>
66 #include <vtkCellArray.h>
67 #include <vtkUnsignedCharArray.h>
68 #include <vtkUnstructuredGrid.h>
69 #include <vtkDataSetMapper.h>
70 #include <vtkRenderer.h>
71 #include <vtkProperty.h>
72 #include <vtkPoints.h>
77 #include <QPushButton>
78 #include <QRadioButton>
79 #include <QHBoxLayout>
80 #include <QVBoxLayout>
82 #include <QButtonGroup>
85 #include <SALOMEconfig.h>
86 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
93 void AddNode( SMESH::SMESH_Mesh_ptr theMesh, float x, float y, float z,
94 SalomeApp_Notebook* theNotebook, const QStringList& theParameters )
96 SUIT_OverrideCursor wc;
98 _PTR(SObject) aSobj = SMESH::FindSObject( theMesh );
99 SMESH::SMESH_MeshEditor_var aMeshEditor = theMesh->GetMeshEditor();
100 aMeshEditor->AddNode( x, y, z );
101 theNotebook->setParameters( theMesh, theParameters );
102 _PTR(Study) aStudy = GetActiveStudyDocument();
103 CORBA::Long anId = aStudy->StudyId();
104 if (TVisualObjPtr aVisualObj = SMESH::GetVisualObj( anId, aSobj->GetID().c_str() ) ) {
105 aVisualObj->Update( true );
108 catch ( SALOME::SALOME_Exception& exc ) {
109 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() );
115 INFOS( "Unknown exception was cought !!!" );
119 class TNodeSimulation
121 SVTK_ViewWindow* myViewWindow;
123 SALOME_Actor* myPreviewActor;
124 vtkDataSetMapper* myMapper;
128 TNodeSimulation( SVTK_ViewWindow* theViewWindow ):
129 myViewWindow( theViewWindow )
131 vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
134 myPoints = vtkPoints::New();
135 myPoints->SetNumberOfPoints( 1 );
136 myPoints->SetPoint( 0, 0.0, 0.0, 0.0 );
139 vtkIdList *anIdList = vtkIdList::New();
140 anIdList->SetNumberOfIds( 1 );
142 vtkCellArray *aCells = vtkCellArray::New();
143 aCells->Allocate( 2, 0 );
145 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
146 aCellTypesArray->SetNumberOfComponents( 1 );
147 aCellTypesArray->Allocate( 1 );
149 anIdList->SetId( 0, 0 );
150 aCells->InsertNextCell( anIdList );
151 aCellTypesArray->InsertNextValue( VTK_VERTEX );
153 VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
154 aCellLocationsArray->SetNumberOfComponents( 1 );
155 aCellLocationsArray->SetNumberOfTuples( 1 );
157 aCells->InitTraversal();
159 aCellLocationsArray->SetValue( 0, aCells->GetTraversalLocation( npts ) );
161 aGrid->SetCells( aCellTypesArray, aCellLocationsArray, aCells );
163 aGrid->SetPoints( myPoints );
164 aGrid->SetCells( aCellTypesArray, aCellLocationsArray, aCells );
165 aCellLocationsArray->Delete();
166 aCellTypesArray->Delete();
170 // Create and display actor
171 myMapper = vtkDataSetMapper::New();
172 myMapper->SetInput( aGrid );
175 myPreviewActor = SALOME_Actor::New();
176 myPreviewActor->SetInfinitive( true );
177 myPreviewActor->VisibilityOff();
178 myPreviewActor->PickableOff();
179 myPreviewActor->SetMapper( myMapper );
181 vtkProperty* aProp = vtkProperty::New();
182 aProp->SetRepresentationToPoints();
184 vtkFloatingPointType anRGB[3];
185 GetColor( "SMESH", "node_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 255, 0 ) );
186 aProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
188 vtkFloatingPointType aPointSize = GetFloat( "SMESH:node_size", 3 );
189 aProp->SetPointSize( aPointSize );
191 myPreviewActor->SetProperty( aProp );
194 myViewWindow->AddActor( myPreviewActor );
197 void SetPosition( float x, float y, float z )
199 myPoints->SetPoint( 0, x, y, z );
200 myPoints->Modified();
201 SetVisibility( true );
204 void SetVisibility( bool theVisibility )
206 myPreviewActor->SetVisibility( theVisibility );
207 RepaintCurrentView();
212 myViewWindow->RemoveActor( myPreviewActor );
213 myPreviewActor->Delete();
215 myMapper->RemoveAllInputs();
223 //=================================================================================
224 // class : SMESHGUI_NodesDlg()
226 //=================================================================================
227 SMESHGUI_NodesDlg::SMESHGUI_NodesDlg( SMESHGUI* theModule ):
228 QDialog( SMESH::GetDesktop( theModule ) ),
229 SMESHGUI_Helper( theModule ),
230 mySelector( SMESH::GetViewWindow( theModule )->GetSelector() ),
231 mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
232 mySMESHGUI( theModule )
235 setAttribute( Qt::WA_DeleteOnClose, true );
236 setWindowTitle( tr("MESH_NODE_TITLE") );
237 setSizeGripEnabled( true );
239 mySimulation = new SMESH::TNodeSimulation( SMESH::GetViewWindow( mySMESHGUI ) );
241 QPixmap image0( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH",
242 tr( "ICON_DLG_NODE" ) ) );
244 QVBoxLayout* SMESHGUI_NodesDlgLayout = new QVBoxLayout( this );
245 SMESHGUI_NodesDlgLayout->setSpacing( SPACING );
246 SMESHGUI_NodesDlgLayout->setMargin( MARGIN );
248 /***************************************************************/
249 GroupConstructors = new QGroupBox( tr( "MESH_NODE" ), this );
250 QButtonGroup* ButtonGroup = new QButtonGroup(this);
251 QHBoxLayout* GroupConstructorsLayout = new QHBoxLayout( GroupConstructors );
252 GroupConstructorsLayout->setSpacing( SPACING );
253 GroupConstructorsLayout->setMargin( MARGIN );
255 Constructor1 = new QRadioButton( GroupConstructors );
256 Constructor1->setIcon( image0 );
257 Constructor1->setChecked( true );
259 GroupConstructorsLayout->addWidget( Constructor1 );
260 ButtonGroup->addButton( Constructor1, 0 );
262 /***************************************************************/
263 GroupCoordinates = new QGroupBox( tr( "SMESH_COORDINATES" ), this );
264 QHBoxLayout* GroupCoordinatesLayout = new QHBoxLayout(GroupCoordinates);
265 GroupCoordinatesLayout->setSpacing(SPACING);
266 GroupCoordinatesLayout->setMargin(MARGIN);
268 TextLabel_X = new QLabel( tr( "SMESH_X" ), GroupCoordinates );
269 SpinBox_X = new SMESHGUI_SpinBox( GroupCoordinates );
271 TextLabel_Y = new QLabel( tr( "SMESH_Y" ), GroupCoordinates );
272 SpinBox_Y = new SMESHGUI_SpinBox( GroupCoordinates );
274 TextLabel_Z = new QLabel( tr( "SMESH_Z" ), GroupCoordinates );
275 SpinBox_Z = new SMESHGUI_SpinBox( GroupCoordinates );
277 GroupCoordinatesLayout->addWidget( TextLabel_X );
278 GroupCoordinatesLayout->addWidget( SpinBox_X );
279 GroupCoordinatesLayout->addWidget( TextLabel_Y);
280 GroupCoordinatesLayout->addWidget( SpinBox_Y );
281 GroupCoordinatesLayout->addWidget( TextLabel_Z );
282 GroupCoordinatesLayout->addWidget( SpinBox_Z );
284 /***************************************************************/
285 GroupButtons = new QGroupBox( this );
286 QHBoxLayout* GroupButtonsLayout = new QHBoxLayout( GroupButtons );
287 GroupButtonsLayout->setSpacing( SPACING );
288 GroupButtonsLayout->setMargin( MARGIN );
289 buttonOk = new QPushButton( tr( "SMESH_BUT_APPLY_AND_CLOSE" ), GroupButtons );
290 buttonOk->setAutoDefault( true );
291 buttonOk->setDefault( true );
292 buttonApply = new QPushButton( tr( "SMESH_BUT_APPLY" ), GroupButtons );
293 buttonApply->setAutoDefault( true );
294 buttonCancel = new QPushButton( tr( "SMESH_BUT_CLOSE" ), GroupButtons );
295 buttonCancel->setAutoDefault( true );
296 buttonHelp = new QPushButton( tr( "SMESH_BUT_HELP" ), GroupButtons );
297 buttonHelp->setAutoDefault( true );
299 GroupButtonsLayout->addWidget( buttonOk );
300 GroupButtonsLayout->addSpacing( 10 );
301 GroupButtonsLayout->addWidget( buttonApply );
302 GroupButtonsLayout->addSpacing( 10 );
303 GroupButtonsLayout->addStretch();
304 GroupButtonsLayout->addWidget( buttonCancel );
305 GroupButtonsLayout->addWidget( buttonHelp );
307 /***************************************************************/
308 SMESHGUI_NodesDlgLayout->addWidget( GroupConstructors );
309 SMESHGUI_NodesDlgLayout->addWidget( GroupCoordinates );
310 SMESHGUI_NodesDlgLayout->addWidget( GroupButtons );
312 myHelpFileName = "adding_nodes_and_elements_page.html#adding_nodes_anchor";
314 /* Initialisation and display */
318 //=======================================================================
319 // function : ~SMESHGUI_NodesDlg()
320 // purpose : Destructor
321 //=======================================================================
322 SMESHGUI_NodesDlg::~SMESHGUI_NodesDlg()
327 //=================================================================================
330 //=================================================================================
331 void SMESHGUI_NodesDlg::Init()
333 /* Get setting of step value from file configuration */
336 /* min, max, step and decimals for spin boxes */
337 SpinBox_X->RangeStepAndValidator( COORD_MIN, COORD_MAX, step, DBL_DIGITS_DISPLAY );
338 SpinBox_Y->RangeStepAndValidator( COORD_MIN, COORD_MAX, step, DBL_DIGITS_DISPLAY );
339 SpinBox_Z->RangeStepAndValidator( COORD_MIN, COORD_MAX, step, DBL_DIGITS_DISPLAY );
340 SpinBox_X->SetValue( 0.0 );
341 SpinBox_Y->SetValue( 0.0 );
342 SpinBox_Z->SetValue( 0.0 );
344 mySMESHGUI->SetActiveDialogBox( this );
346 /* signals and slots connections */
347 connect( buttonOk, SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
348 connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) );
349 connect( buttonApply, SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
350 connect( buttonHelp, SIGNAL( clicked() ), this, SLOT( ClickOnHelp() ) );
352 connect( SpinBox_X, SIGNAL( valueChanged( double ) ), SLOT( ValueChangedInSpinBox( double ) ) );
353 connect( SpinBox_Y, SIGNAL( valueChanged( double ) ), SLOT( ValueChangedInSpinBox( double ) ) );
354 connect( SpinBox_Z, SIGNAL( valueChanged( double ) ), SLOT( ValueChangedInSpinBox( double ) ) );
356 connect( mySelectionMgr, SIGNAL( currentSelectionChanged() ), SLOT( SelectionIntoArgument() ) );
357 connect( mySMESHGUI, SIGNAL( SignalDeactivateActiveDialog() ), SLOT( DeactivateActiveDialog() ) );
358 /* to close dialog if study frame change */
359 connect( mySMESHGUI, SIGNAL( SignalStudyFrameChanged() ), SLOT( ClickOnCancel() ) );
361 // set selection mode
362 SMESH::SetPointRepresentation( true );
363 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ) )
364 aViewWindow->SetSelectionMode( NodeSelection );
366 SelectionIntoArgument();
369 //=================================================================================
370 // function : ValueChangedInSpinBox()
372 //=================================================================================
373 void SMESHGUI_NodesDlg::ValueChangedInSpinBox( double newValue )
375 if ( !myMesh->_is_nil() ) {
376 double vx = SpinBox_X->GetValue();
377 double vy = SpinBox_Y->GetValue();
378 double vz = SpinBox_Z->GetValue();
380 mySimulation->SetPosition( vx, vy, vz );
384 //=================================================================================
385 // function : ClickOnOk()
387 //=================================================================================
388 void SMESHGUI_NodesDlg::ClickOnOk()
390 if ( ClickOnApply() )
394 //=================================================================================
395 // function : ClickOnApply()
397 //=================================================================================
398 bool SMESHGUI_NodesDlg::ClickOnApply()
400 if ( mySMESHGUI->isActiveStudyLocked() )
403 if ( myMesh->_is_nil() ) {
404 SUIT_MessageBox::warning( this, tr( "SMESH_WRN_WARNING" ),
405 tr( "MESH_IS_NOT_SELECTED" ) );
412 /* Recup args and call method */
413 double x = SpinBox_X->GetValue();
414 double y = SpinBox_Y->GetValue();
415 double z = SpinBox_Z->GetValue();
417 QStringList aParameters;
418 aParameters << SpinBox_X->text();
419 aParameters << SpinBox_Y->text();
420 aParameters << SpinBox_Z->text();
422 mySimulation->SetVisibility( false );
423 SMESH::AddNode( myMesh, x, y, z, getNotebook(), aParameters );
424 SMESH::SetPointRepresentation( true );
428 mySelectionMgr->selectedObjects( aList );
429 if ( aList.Extent() != 1 ) {
430 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView() ) {
431 VTK::ActorCollectionCopy aCopy(aViewWindow->getRenderer()->GetActors());
432 vtkActorCollection *aCollection = aCopy.GetActors();
433 aCollection->InitTraversal();
434 while ( vtkActor *anAct = aCollection->GetNextActor() ) {
435 if ( SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>( anAct ) ) {
436 if ( anActor->hasIO() ) {
437 if ( SMESH_MeshObj *aMeshObj = dynamic_cast<SMESH_MeshObj*>( anActor->GetObject().get() ) ) {
438 if ( myMesh->_is_equivalent( aMeshObj->GetMeshServer() ) ) {
440 aList.Append( anActor->getIO() );
441 mySelectionMgr->setSelectedObjects( aList, false );
453 //=================================================================================
454 // function : ClickOnCancel()
456 //=================================================================================
457 void SMESHGUI_NodesDlg::ClickOnCancel()
459 disconnect( mySelectionMgr, 0, this, 0 );
460 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ) )
461 aViewWindow->SetSelectionMode( ActorSelection );
463 mySimulation->SetVisibility( false );
464 SMESH::SetPointRepresentation( false );
465 mySMESHGUI->ResetState();
470 //=================================================================================
471 // function : ClickOnHelp()
473 //=================================================================================
474 void SMESHGUI_NodesDlg::ClickOnHelp()
476 LightApp_Application* app = (LightApp_Application*)( SUIT_Session::session()->activeApplication() );
478 app->onHelpContextModule( mySMESHGUI ? app->moduleName( mySMESHGUI->moduleName() ) :
479 QString( "" ), myHelpFileName );
483 platform = "winapplication";
485 platform = "application";
487 SUIT_MessageBox::warning( this, tr("WRN_WARNING"),
488 tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
489 arg( app->resourceMgr()->stringValue( "ExternalBrowser",
491 arg( myHelpFileName ) );
495 //=================================================================================
496 // function : SelectionIntoArgument()
497 // purpose : Called when selection as changed or other case
498 //=================================================================================
499 void SMESHGUI_NodesDlg::SelectionIntoArgument()
501 if ( !GroupConstructors->isEnabled() )
504 mySimulation->SetVisibility( false );
505 SMESH::SetPointRepresentation( true );
507 const SALOME_ListIO& aList = mySelector->StoredIObjects();
508 if ( aList.Extent() == 1 ) {
509 Handle(SALOME_InteractiveObject) anIO = aList.First();
510 if ( anIO->hasEntry() ) {
511 myMesh = SMESH::GetMeshByIO( anIO );
512 if ( myMesh->_is_nil() ) return;
514 if ( SMESH::GetNameOfSelectedNodes( mySelector, anIO, aText ) == 1 ) {
515 if ( SMESH_Actor* anActor = SMESH::FindActorByObject( myMesh.in() ) ) {
516 if ( SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh() ) {
517 if ( const SMDS_MeshNode* aNode = aMesh->FindNode( aText.toInt() ) ) {
518 SpinBox_X->SetValue( aNode->X() );
519 SpinBox_Y->SetValue( aNode->Y() );
520 SpinBox_Z->SetValue( aNode->Z() );
525 mySimulation->SetPosition( SpinBox_X->GetValue(),
526 SpinBox_Y->GetValue(),
527 SpinBox_Z->GetValue() );
532 //=================================================================================
533 // function : closeEvent()
535 //=================================================================================
536 void SMESHGUI_NodesDlg::closeEvent( QCloseEvent* )
538 this->ClickOnCancel(); /* same than click on cancel button */
541 //=================================================================================
542 // function : hideEvent()
543 // purpose : caused by ESC key
544 //=================================================================================
545 void SMESHGUI_NodesDlg::hideEvent( QHideEvent* )
547 if ( !isMinimized() )
551 //=================================================================================
552 // function : enterEvent()
553 // purpose : to reactivate this dialog box when mouse enter onto the window
554 //=================================================================================
555 void SMESHGUI_NodesDlg::enterEvent( QEvent* )
557 if ( !GroupConstructors->isEnabled() )
558 ActivateThisDialog();
561 //=================================================================================
562 // function : DeactivateActiveDialog()
563 // purpose : public slot to deactivate if active
564 //=================================================================================
565 void SMESHGUI_NodesDlg::DeactivateActiveDialog()
567 if ( GroupConstructors->isEnabled() ) {
568 GroupConstructors->setEnabled( false );
569 GroupCoordinates->setEnabled( false );
570 GroupButtons->setEnabled( false );
571 mySimulation->SetVisibility( false );
572 mySMESHGUI->ResetState();
573 mySMESHGUI->SetActiveDialogBox( 0 );
577 //=================================================================================
578 // function : ActivateThisDialog()
580 //=================================================================================
581 void SMESHGUI_NodesDlg::ActivateThisDialog()
583 mySMESHGUI->EmitSignalDeactivateDialog();
584 GroupConstructors->setEnabled( true );
585 GroupCoordinates->setEnabled( true );
586 GroupButtons->setEnabled( true );
588 SMESH::SetPointRepresentation( true );
589 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ) )
590 aViewWindow->SetSelectionMode( NodeSelection );
592 SelectionIntoArgument();
595 //=================================================================================
596 // function : keyPressEvent()
598 //=================================================================================
599 void SMESHGUI_NodesDlg::keyPressEvent( QKeyEvent* e )
601 QDialog::keyPressEvent( e );
602 if ( e->isAccepted() )
605 if ( e->key() == Qt::Key_F1 ) {
611 //=================================================================================
612 // function : isValid
614 //=================================================================================
615 bool SMESHGUI_NodesDlg::isValid()
617 return checkParameters( true, 3, SpinBox_X, SpinBox_Y, SpinBox_Z );