Salome HOME
Porting SMESH module to Qt 4
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_NodesDlg.cxx
1 // SMESH SMESHGUI : GUI for SMESH component
2 //
3 // Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File   : SMESHGUI_NodesDlg.cxx
23 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
24 //
25
26 // SMESH includes
27 #include "SMESHGUI_NodesDlg.h"
28
29 #include "SMESHGUI.h"
30 #include "SMESHGUI_SpinBox.h"
31 #include "SMESHGUI_Utils.h"
32 #include "SMESHGUI_VTKUtils.h"
33 #include "SMESHGUI_MeshUtils.h"
34
35 #include <SMESH_Actor.h>
36 #include <SMESH_ActorUtils.h>
37 #include <SMESH_ObjectDef.h>
38
39 #include <SMDS_Mesh.hxx>
40 #include <SMDS_MeshNode.hxx>
41
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>
48
49 #include <LightApp_Application.h>
50 #include <LightApp_SelectionMgr.h>
51
52 #include <SVTK_ViewWindow.h>
53 #include <VTKViewer_CellLocationsArray.h>
54
55 // SALOME KERNEL includes
56 #include <SALOMEDS_Study.hxx>
57 #include <SALOMEDS_SObject.hxx>
58
59 #include <utilities.h>
60
61 // VTK includes
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>
70
71 // Qt includes
72 #include <QGroupBox>
73 #include <QLabel>
74 #include <QPushButton>
75 #include <QRadioButton>
76 #include <QHBoxLayout>
77 #include <QVBoxLayout>
78 #include <QKeyEvent>
79
80 // IDL includes
81 #include <SALOMEconfig.h>
82 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
83
84 #define SPACING 6
85 #define MARGIN  11
86
87 namespace SMESH
88 {
89   void AddNode( SMESH::SMESH_Mesh_ptr theMesh, float x, float y, float z )
90   {
91     SUIT_OverrideCursor wc;
92     try {
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 );
100       }
101     } 
102     catch ( SALOME::SALOME_Exception& exc ) {
103       INFOS( "Follow exception was cought:\n\t" << exc.details.text );
104     }
105     catch ( const std::exception& exc ) {
106       INFOS( "Follow exception was cought:\n\t" << exc.what() );
107     } 
108     catch ( ... ) {
109       INFOS( "Unknown exception was cought !!!" );
110     }
111   }
112
113   class TNodeSimulation 
114   {
115     SVTK_ViewWindow*  myViewWindow;
116
117     SALOME_Actor*     myPreviewActor;
118     vtkDataSetMapper* myMapper;
119     vtkPoints*        myPoints;
120
121   public:
122     TNodeSimulation( SVTK_ViewWindow* theViewWindow ):
123       myViewWindow( theViewWindow )
124     {
125       vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
126
127       // Create points
128       myPoints = vtkPoints::New();
129       myPoints->SetNumberOfPoints( 1 );
130       myPoints->SetPoint( 0, 0.0, 0.0, 0.0 );
131
132       // Create cells
133       vtkIdList *anIdList = vtkIdList::New();
134       anIdList->SetNumberOfIds( 1 );
135
136       vtkCellArray *aCells = vtkCellArray::New();
137       aCells->Allocate( 2, 0 );
138
139       vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
140       aCellTypesArray->SetNumberOfComponents( 1 );
141       aCellTypesArray->Allocate( 1 );
142
143       anIdList->SetId( 0, 0 );
144       aCells->InsertNextCell( anIdList );
145       aCellTypesArray->InsertNextValue( VTK_VERTEX );
146
147       VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
148       aCellLocationsArray->SetNumberOfComponents( 1 );
149       aCellLocationsArray->SetNumberOfTuples( 1 );
150
151       aCells->InitTraversal();
152       vtkIdType npts = 0;
153       aCellLocationsArray->SetValue( 0, aCells->GetTraversalLocation( npts ) );
154
155       aGrid->SetCells( aCellTypesArray, aCellLocationsArray, aCells );
156
157       aGrid->SetPoints( myPoints );
158       aGrid->SetCells( aCellTypesArray, aCellLocationsArray, aCells );
159       aCellLocationsArray->Delete();
160       aCellTypesArray->Delete();
161       aCells->Delete();
162       anIdList->Delete();
163
164       // Create and display actor
165       myMapper = vtkDataSetMapper::New();
166       myMapper->SetInput( aGrid );
167       aGrid->Delete();
168
169       myPreviewActor = SALOME_Actor::New();
170       myPreviewActor->SetInfinitive( true );
171       myPreviewActor->VisibilityOff();
172       myPreviewActor->PickableOff();
173       myPreviewActor->SetMapper( myMapper );
174
175       vtkProperty* aProp = vtkProperty::New();
176       aProp->SetRepresentationToPoints();
177
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] );
181
182       vtkFloatingPointType aPointSize = GetFloat( "SMESH:node_size", 3 );
183       aProp->SetPointSize( aPointSize );
184
185       myPreviewActor->SetProperty( aProp );
186       aProp->Delete();
187
188       myViewWindow->AddActor( myPreviewActor );
189     }
190
191     void SetPosition( float x, float y, float z )
192     {
193       myPoints->SetPoint( 0, x, y, z );
194       myPoints->Modified();
195       SetVisibility( true );
196     }
197
198     void SetVisibility( bool theVisibility )
199     {
200       myPreviewActor->SetVisibility( theVisibility );
201       RepaintCurrentView();
202     }
203
204     ~TNodeSimulation()
205     {
206       myViewWindow->RemoveActor( myPreviewActor );
207       myPreviewActor->Delete();
208
209       myMapper->RemoveAllInputs();
210       myMapper->Delete();
211
212       myPoints->Delete();
213     }
214   };
215 }
216
217 //=================================================================================
218 // class    : SMESHGUI_NodesDlg()
219 // purpose  :
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 )
226 {
227   setModal( false );
228   setAttribute( Qt::WA_DeleteOnClose, true );
229   setWindowTitle( "MESH_NODE_TITLE" );
230   setSizeGripEnabled( true );
231   
232   mySimulation = new SMESH::TNodeSimulation( SMESH::GetViewWindow( mySMESHGUI ) );
233   
234   QPixmap image0( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH", 
235                                                                    tr( "ICON_DLG_NODE" ) ) );
236   
237   QVBoxLayout* SMESHGUI_NodesDlgLayout = new QVBoxLayout( this );
238   SMESHGUI_NodesDlgLayout->setSpacing( SPACING );
239   SMESHGUI_NodesDlgLayout->setMargin( MARGIN );
240
241   /***************************************************************/
242   GroupConstructors = new QGroupBox( tr( "MESH_NODE" ), this );
243   QHBoxLayout* GroupConstructorsLayout = new QHBoxLayout( GroupConstructors );
244   GroupConstructorsLayout->setSpacing( SPACING );
245   GroupConstructorsLayout->setMargin( MARGIN );
246
247   Constructor1 = new QRadioButton( GroupConstructors );
248   Constructor1->setIcon( image0 );
249   Constructor1->setChecked( true );
250
251   GroupConstructorsLayout->addWidget( Constructor1 );
252   GroupConstructorsLayout->addStretch();
253
254   /***************************************************************/
255   GroupCoordinates = new QGroupBox( tr( "SMESH_COORDINATES" ), this );
256   QHBoxLayout* GroupCoordinatesLayout = new QHBoxLayout(GroupCoordinates);
257   GroupCoordinatesLayout->setSpacing(SPACING);
258   GroupCoordinatesLayout->setMargin(MARGIN);
259
260   TextLabel_X = new QLabel( tr( "SMESH_X" ), GroupCoordinates );
261   SpinBox_X = new SMESHGUI_SpinBox( GroupCoordinates );
262
263   TextLabel_Y = new QLabel( tr( "SMESH_Y" ), GroupCoordinates );
264   SpinBox_Y = new SMESHGUI_SpinBox( GroupCoordinates );
265
266   TextLabel_Z = new QLabel( tr( "SMESH_Z" ), GroupCoordinates );
267   SpinBox_Z = new SMESHGUI_SpinBox( GroupCoordinates );
268
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 );
275
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 );
290
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 );
298
299   /***************************************************************/
300   SMESHGUI_NodesDlgLayout->addWidget( GroupConstructors );
301   SMESHGUI_NodesDlgLayout->addWidget( GroupCoordinates );
302   SMESHGUI_NodesDlgLayout->addWidget( GroupButtons );
303
304   myHelpFileName = "adding_nodes_and_elements_page.html#adding_nodes_anchor";
305
306   /* Initialisation and display */
307   Init();
308 }
309
310 //=======================================================================
311 // function : ~SMESHGUI_NodesDlg()
312 // purpose  : Destructor
313 //=======================================================================
314 SMESHGUI_NodesDlg::~SMESHGUI_NodesDlg()
315 {
316   delete mySimulation;
317 }
318
319 //=================================================================================
320 // function : Init()
321 // purpose  :
322 //=================================================================================
323 void SMESHGUI_NodesDlg::Init()
324 {
325   /* Get setting of step value from file configuration */
326   double step = 25.0;
327
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 );
335
336   mySMESHGUI->SetActiveDialogBox( this );
337
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() ) );
343
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 ) ) );
347
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() ) );
352
353   // set selection mode
354   SMESH::SetPointRepresentation( true );
355   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ) )
356     aViewWindow->SetSelectionMode( NodeSelection );
357
358   SelectionIntoArgument();
359 }
360
361 //=================================================================================
362 // function : ValueChangedInSpinBox()
363 // purpose  :
364 //=================================================================================
365 void SMESHGUI_NodesDlg::ValueChangedInSpinBox( double newValue )
366 {
367   if ( !myMesh->_is_nil() ) {
368     double vx = SpinBox_X->GetValue();
369     double vy = SpinBox_Y->GetValue();
370     double vz = SpinBox_Z->GetValue();
371
372     mySimulation->SetPosition( vx, vy, vz );
373   }
374 }
375
376 //=================================================================================
377 // function : ClickOnOk()
378 // purpose  :
379 //=================================================================================
380 void SMESHGUI_NodesDlg::ClickOnOk()
381 {
382   if ( ClickOnApply() )
383     ClickOnCancel();
384 }
385
386 //=================================================================================
387 // function : ClickOnApply()
388 // purpose  :
389 //=================================================================================
390 bool SMESHGUI_NodesDlg::ClickOnApply()
391 {
392   if ( mySMESHGUI->isActiveStudyLocked() )
393     return false;
394
395   if ( myMesh->_is_nil() ) {
396     SUIT_MessageBox::warning( this, tr( "SMESH_WRN_WARNING" ),
397                               tr( "MESH_IS_NOT_SELECTED" ) );
398     return false;
399   }
400
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 );
408
409   // select myMesh
410   SALOME_ListIO aList;
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() ) ) {
421                 aList.Clear();
422                 aList.Append( anActor->getIO() );
423                 mySelectionMgr->setSelectedObjects( aList, false );
424                 break;
425               }
426             }
427           }
428         }
429       }
430     }
431   }
432   return true;
433 }
434
435 //=================================================================================
436 // function : ClickOnCancel()
437 // purpose  :
438 //=================================================================================
439 void SMESHGUI_NodesDlg::ClickOnCancel()
440 {
441   disconnect( mySelectionMgr, 0, this, 0 );
442   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ) )
443     aViewWindow->SetSelectionMode( ActorSelection );
444
445   mySimulation->SetVisibility( false );
446   SMESH::SetPointRepresentation( false );
447   mySMESHGUI->ResetState();
448
449   reject();
450 }
451
452 //=================================================================================
453 // function : ClickOnHelp()
454 // purpose  :
455 //=================================================================================
456 void SMESHGUI_NodesDlg::ClickOnHelp()
457 {
458   LightApp_Application* app = (LightApp_Application*)( SUIT_Session::session()->activeApplication() );
459   if ( app ) 
460     app->onHelpContextModule( mySMESHGUI ? app->moduleName( mySMESHGUI->moduleName() ) : 
461                               QString( "" ), myHelpFileName );
462   else {
463     QString platform;
464 #ifdef WIN32
465     platform = "winapplication";
466 #else
467     platform = "application";
468 #endif
469     SUIT_MessageBox::warning( this, tr("WRN_WARNING"),
470                               tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
471                               arg( app->resourceMgr()->stringValue( "ExternalBrowser", 
472                                                                     platform ) ).
473                               arg( myHelpFileName ) );
474   }
475 }
476
477 //=================================================================================
478 // function : SelectionIntoArgument()
479 // purpose  : Called when selection as changed or other case
480 //=================================================================================
481 void SMESHGUI_NodesDlg::SelectionIntoArgument()
482 {
483   if ( !GroupConstructors->isEnabled() )
484     return;
485
486   mySimulation->SetVisibility( false );
487   SMESH::SetPointRepresentation( true );
488
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;
495       QString aText;
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() );
503             }
504           }
505         }
506       }
507       mySimulation->SetPosition( SpinBox_X->GetValue(),
508                                  SpinBox_Y->GetValue(),
509                                  SpinBox_Z->GetValue() );
510     }
511   }
512 }
513
514 //=================================================================================
515 // function : closeEvent()
516 // purpose  :
517 //=================================================================================
518 void SMESHGUI_NodesDlg::closeEvent( QCloseEvent* )
519 {
520   this->ClickOnCancel(); /* same than click on cancel button */
521 }
522
523 //=================================================================================
524 // function : hideEvent()
525 // purpose  : caused by ESC key
526 //=================================================================================
527 void SMESHGUI_NodesDlg::hideEvent( QHideEvent* )
528 {
529   if ( !isMinimized() )
530     ClickOnCancel();
531 }
532
533 //=================================================================================
534 // function : enterEvent()
535 // purpose  : to reactivate this dialog box when mouse enter onto the window
536 //=================================================================================
537 void SMESHGUI_NodesDlg::enterEvent( QEvent* )
538 {
539   if ( !GroupConstructors->isEnabled() )
540     ActivateThisDialog();
541 }
542
543 //=================================================================================
544 // function : DeactivateActiveDialog()
545 // purpose  : public slot to deactivate if active
546 //=================================================================================
547 void SMESHGUI_NodesDlg::DeactivateActiveDialog()
548 {
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 );
556   }
557 }
558
559 //=================================================================================
560 // function : ActivateThisDialog()
561 // purpose  :
562 //=================================================================================
563 void SMESHGUI_NodesDlg::ActivateThisDialog()
564 {
565   mySMESHGUI->EmitSignalDeactivateDialog();
566   GroupConstructors->setEnabled( true );
567   GroupCoordinates->setEnabled( true );
568   GroupButtons->setEnabled( true );
569
570   SMESH::SetPointRepresentation( true );
571   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ) )
572     aViewWindow->SetSelectionMode( NodeSelection );
573
574   SelectionIntoArgument();
575 }
576
577 //=================================================================================
578 // function : keyPressEvent()
579 // purpose  :
580 //=================================================================================
581 void SMESHGUI_NodesDlg::keyPressEvent( QKeyEvent* e )
582 {
583   QDialog::keyPressEvent( e );
584   if ( e->isAccepted() )
585     return;
586
587   if ( e->key() == Qt::Key_F1 ) {
588     e->accept();
589     ClickOnHelp();
590   }
591 }