Salome HOME
Change Handle(SMESHDS_Document) to SMESHDS_Document*
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SMESHGUI_NodesDlg.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SMESH
27 //  $Header$
28
29 using namespace std;
30 #include "SMESHGUI_NodesDlg.h"
31 #include "SMESHGUI.h"
32 #include "SMESHGUI_SpinBox.h"
33
34 #include "QAD_Application.h"
35 #include "QAD_Desktop.h"
36 #include "utilities.h"
37 #include "QAD_MessageBox.h"
38
39 #include "SMESH_Actor.h"
40 #include <TColStd_MapOfInteger.hxx>
41 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
42
43 // VTK Includes
44 #include <vtkPoints.h>
45 #include <vtkPolyData.h>
46 #include <vtkPolyDataMapper.h>
47 #include <vtkDataSetMapper.h>
48 #include <vtkIdList.h>
49 #include <vtkVertex.h>
50
51 // QT Includes
52 #include <qbuttongroup.h>
53 #include <qframe.h>
54 #include <qgroupbox.h>
55 #include <qlabel.h>
56 #include <qlineedit.h>
57 #include <qpushbutton.h>
58 #include <qradiobutton.h>
59 #include <qlayout.h>
60 #include <qvariant.h>
61 #include <qtooltip.h>
62 #include <qwhatsthis.h>
63 #include <qimage.h>
64 #include <qpixmap.h>
65 #include <qvalidator.h>
66 #include <qevent.h>
67
68
69 //=================================================================================
70 // class    : SMESHGUI_NodesDlg()
71 // purpose  : 
72 //=================================================================================
73 SMESHGUI_NodesDlg::SMESHGUI_NodesDlg( QWidget* parent,
74                                       const char* name,
75                                       SALOME_Selection* Sel,
76                                       bool modal,
77                                       WFlags fl )
78   : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
79 {
80   
81   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH",tr("ICON_DLG_NODE")));
82   if ( !name )
83     setName( "SMESHGUI_NodesDlg" );
84   resize( 303, 185 ); 
85   setCaption( tr( "MESH_NODE_TITLE"  ) );
86   setSizeGripEnabled( TRUE );
87   SMESHGUI_NodesDlgLayout = new QGridLayout( this ); 
88   SMESHGUI_NodesDlgLayout->setSpacing( 6 );
89   SMESHGUI_NodesDlgLayout->setMargin( 11 );
90
91   /***************************************************************/
92   GroupButtons = new QGroupBox( this, "GroupButtons" );
93   GroupButtons->setGeometry( QRect( 10, 10, 281, 48 ) ); 
94   GroupButtons->setTitle( tr( ""  ) );
95   GroupButtons->setColumnLayout(0, Qt::Vertical );
96   GroupButtons->layout()->setSpacing( 0 );
97   GroupButtons->layout()->setMargin( 0 );
98   GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
99   GroupButtonsLayout->setAlignment( Qt::AlignTop );
100   GroupButtonsLayout->setSpacing( 6 );
101   GroupButtonsLayout->setMargin( 11 );
102   buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
103   buttonCancel->setText( tr( "SMESH_BUT_CLOSE"  ) );
104   buttonCancel->setAutoDefault( TRUE );
105   GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
106   buttonApply = new QPushButton( GroupButtons, "buttonApply" );
107   buttonApply->setText( tr( "SMESH_BUT_APPLY"  ) );
108   buttonApply->setAutoDefault( TRUE );
109   GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
110   QSpacerItem* spacer_9 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
111   GroupButtonsLayout->addItem( spacer_9, 0, 2 );
112   buttonOk = new QPushButton( GroupButtons, "buttonOk" );
113   buttonOk->setText( tr( "SMESH_BUT_OK"  ) );
114   buttonOk->setAutoDefault( TRUE );
115   buttonOk->setDefault( TRUE );
116   GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
117   SMESHGUI_NodesDlgLayout->addWidget( GroupButtons, 2, 0 );
118
119   /***************************************************************/
120   GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
121   GroupConstructors->setTitle( tr( "MESH_NODE"  ) );
122   GroupConstructors->setExclusive( TRUE );
123   GroupConstructors->setColumnLayout(0, Qt::Vertical );
124   GroupConstructors->layout()->setSpacing( 0 );
125   GroupConstructors->layout()->setMargin( 0 );
126   GroupConstructorsLayout = new QGridLayout( GroupConstructors->layout() );
127   GroupConstructorsLayout->setAlignment( Qt::AlignTop );
128   GroupConstructorsLayout->setSpacing( 6 );
129   GroupConstructorsLayout->setMargin( 11 );
130   Constructor1 = new QRadioButton( GroupConstructors, "Constructor1" );
131   Constructor1->setText( tr( ""  ) );
132   Constructor1->setPixmap( image0 );
133   Constructor1->setChecked( TRUE );
134   GroupConstructorsLayout->addWidget( Constructor1, 0, 0 );
135   QSpacerItem* spacer_2 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
136   GroupConstructorsLayout->addItem( spacer_2, 0, 1 );
137   SMESHGUI_NodesDlgLayout->addWidget( GroupConstructors, 0, 0 );
138
139   /***************************************************************/
140   GroupCoordinates = new QGroupBox( this, "GroupCoordinates" );
141   GroupCoordinates->setTitle( tr( "SMESH_COORDINATES"  ) );
142   GroupCoordinates->setColumnLayout(0, Qt::Vertical );
143   GroupCoordinates->layout()->setSpacing( 0 );
144   GroupCoordinates->layout()->setMargin( 0 );
145   GroupCoordinatesLayout = new QGridLayout( GroupCoordinates->layout() );
146   GroupCoordinatesLayout->setAlignment( Qt::AlignTop );
147   GroupCoordinatesLayout->setSpacing( 6 );
148   GroupCoordinatesLayout->setMargin( 11 );
149   TextLabel_X = new QLabel( GroupCoordinates, "TextLabel_X" );
150   TextLabel_X->setText( tr( "SMESH_X"  ) );
151   GroupCoordinatesLayout->addWidget( TextLabel_X, 0, 0 );
152   TextLabel_Y = new QLabel( GroupCoordinates, "TextLabel_Y" );
153   TextLabel_Y->setText( tr( "SMESH_Y"  ) );
154   GroupCoordinatesLayout->addWidget( TextLabel_Y, 0, 2 );
155   
156   TextLabel_Z = new QLabel( GroupCoordinates, "TextLabel_Z" );
157   TextLabel_Z->setText( tr( "SMESH_Z"  ) );
158   GroupCoordinatesLayout->addWidget( TextLabel_Z, 0, 4 );
159   
160   SpinBox_X = new SMESHGUI_SpinBox( GroupCoordinates, "SpinBox_X" ) ;
161   GroupCoordinatesLayout->addWidget( SpinBox_X, 0, 1 );
162
163   SpinBox_Y = new SMESHGUI_SpinBox( GroupCoordinates, "SpinBox_Y" ) ;
164   GroupCoordinatesLayout->addWidget( SpinBox_Y, 0, 3 );
165  
166   SpinBox_Z = new SMESHGUI_SpinBox( GroupCoordinates, "SpinBox_Z" ) ;
167   GroupCoordinatesLayout->addWidget( SpinBox_Z, 0, 5 );
168
169   SMESHGUI_NodesDlgLayout->addWidget( GroupCoordinates, 1, 0 );
170  
171   /* Initialisation and display */
172   Init(Sel) ;
173 }
174
175
176 //=======================================================================
177 // function : ~SMESHGUI_NodesDlg()
178 // purpose  : Destructor
179 //=======================================================================
180 SMESHGUI_NodesDlg::~SMESHGUI_NodesDlg()
181 {}
182
183
184 //=================================================================================
185 // function : Init()
186 // purpose  :
187 //=================================================================================
188 void SMESHGUI_NodesDlg::Init(SALOME_Selection* Sel)
189 {
190   /* Get setting of step value from file configuration */
191   double step ;
192   // QString St = QAD_CONFIG->getSetting( "xxxxxxxxxxxxx" ) ;  TODO
193   // step = St.toDouble() ;                                    TODO
194   step = 25.0 ;
195   
196   /* min, max, step and decimals for spin boxes */
197   SpinBox_X->RangeStepAndValidator( -999.999, +999.999, step, 3 ) ;
198   SpinBox_Y->RangeStepAndValidator( -999.999, +999.999, step, 3 ) ;
199   SpinBox_Z->RangeStepAndValidator( -999.999, +999.999, step, 3 ) ;  
200   SpinBox_X->SetValue( 0.0 ) ;
201   SpinBox_Y->SetValue( 0.0 ) ;
202   SpinBox_Z->SetValue( 0.0 ) ;
203   
204   mySelection = Sel ;
205   myMeshGUI = SMESHGUI::GetSMESHGUI() ;   
206   myMeshGUI->SetActiveDialogBox( (QDialog*)this ) ;
207   
208   /* signals and slots connections */
209   connect( buttonOk, SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
210   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) );
211   connect( buttonApply, SIGNAL( clicked() ), this, SLOT(ClickOnApply() ) );
212   
213   connect( SpinBox_X, SIGNAL ( valueChanged( double) ), this, SLOT( ValueChangedInSpinBox( double) ) ) ;
214   connect( SpinBox_Y, SIGNAL ( valueChanged( double) ), this, SLOT( ValueChangedInSpinBox( double) ) ) ;
215   connect( SpinBox_Z, SIGNAL ( valueChanged( double) ), this, SLOT( ValueChangedInSpinBox( double) ) ) ;
216
217   connect( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
218   connect( myMeshGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
219   /* to close dialog if study change */
220   connect( myMeshGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
221  
222   /* Move widget on the botton right corner of main widget */
223   int x, y ;
224   myMeshGUI->DefineDlgPosition( this, x, y ) ;
225   this->move( x, y ) ;
226   this->show() ;
227
228   SelectionIntoArgument();
229 }
230
231 //=================================================================================
232 // function : ValueChangedInSpinBox()
233 // purpose  :
234 //=================================================================================
235 void SMESHGUI_NodesDlg::ValueChangedInSpinBox( double newValue )
236 {
237   double vx = SpinBox_X->GetValue() ;
238   double vy = SpinBox_Y->GetValue() ;
239   double vz = SpinBox_Z->GetValue() ;
240   myMeshGUI->DisplaySimulationNode( myMesh, vx, vy, vz );
241   //myMeshGUI->ViewNodes();
242
243   return ;
244 }
245
246 //=================================================================================
247 // function : ClickOnOk()
248 // purpose  :
249 //=================================================================================
250 void SMESHGUI_NodesDlg::ClickOnOk()
251 {
252   this->ClickOnApply() ;
253   this->ClickOnCancel() ;
254
255   return ;
256 }
257
258 //=======================================================================
259 // function : ClickOnApply()
260 // purpose  :
261 //=======================================================================
262 void SMESHGUI_NodesDlg::ClickOnApply()
263 {
264   if ( myMeshGUI->GetActiveStudy()->getActiveStudyFrame()->getTypeView() != VIEW_VTK ) {
265     return;
266   }
267   
268   if ( Constructor1->isChecked() ) {
269     /* Recup args and call method */
270     double x = SpinBox_X->GetValue() ;
271     double y = SpinBox_Y->GetValue() ;
272     double z = SpinBox_Z->GetValue() ;
273     myMeshGUI->EraseSimulationActors() ;
274     myMeshGUI->AddNode( myMesh, x, y, z ) ; 
275     myMeshGUI->ViewNodes();
276     mySelection->ClearIObjects();
277   }
278 }
279
280
281 //=======================================================================
282 // function : ClickOnCancel()
283 // purpose  :
284 //=======================================================================
285 void SMESHGUI_NodesDlg::ClickOnCancel()
286 {
287   QAD_Application::getDesktop()->SetSelectionMode( 4 );
288   disconnect( mySelection, 0, this, 0 );
289   myMeshGUI->ResetState() ;
290   myMeshGUI->EraseSimulationActors() ;
291   reject() ;
292   return ;
293 }
294
295
296 //=================================================================================
297 // function : SelectionIntoArgument()
298 // purpose  : Called when selection as changed or other case
299 //=================================================================================
300 void SMESHGUI_NodesDlg::SelectionIntoArgument()
301 {
302   int nbSel = mySelection->IObjectCount();
303   if(nbSel != 1) {
304     SpinBox_X->SetValue(0.0) ;
305     SpinBox_Y->SetValue(0.0) ;
306     SpinBox_Z->SetValue(0.0) ;
307     return;
308   }
309
310   Standard_Boolean res;
311   myMesh = myMeshGUI->ConvertIOinMesh( mySelection->firstIObject(), res );
312   if (!res) {
313     SpinBox_X->SetValue(0.0) ;
314     SpinBox_Y->SetValue(0.0) ;
315     SpinBox_Z->SetValue(0.0) ;
316     return ;
317   }
318   
319   if ( mySelection->SelectionMode() != 1 ) {
320     SpinBox_X->SetValue(0.0) ;
321     SpinBox_Y->SetValue(0.0) ;
322     SpinBox_Z->SetValue(0.0) ;
323     QAD_MessageBox::warn1 ( QAD_Application::getDesktop(), tr ("SMESH_WRN_WARNING"),
324                             tr ("SMESH_WRN_SELECTIONMODE_NODES"), tr ("SMESH_BUT_YES") );
325     return;
326   }
327
328   QString aString = "";
329   int nbNodes = myMeshGUI->GetNameOfSelectedNodes(mySelection, aString) ;
330   if(nbNodes != 1) {
331     SpinBox_X->SetValue(0.0) ;
332     SpinBox_Y->SetValue(0.0) ;
333     SpinBox_Z->SetValue(0.0) ;
334     return ;
335   } 
336
337   if ( nbNodes == 1 ) {
338     TColStd_MapOfInteger myMapIndex;
339     mySelection->GetIndex( mySelection->firstIObject(), myMapIndex);
340     TColStd_MapIteratorOfMapOfInteger ite( myMapIndex );
341     int idNodes[1];
342     for ( ; ite.More(); ite.Next() ) {
343       idNodes[0] = ite.Key();
344     }
345
346     Standard_Boolean result;
347     SMESH_Actor* ac = myMeshGUI->FindActor( myMesh, result, true );
348     vtkUnstructuredGrid* ugrid = vtkUnstructuredGrid::SafeDownCast( ac->DataSource );
349     float *p0 = ugrid->GetPoint(idNodes[0]);
350
351     SpinBox_X->SetValue( p0[0] ) ;
352     SpinBox_Y->SetValue( p0[1] ) ;
353     SpinBox_Z->SetValue( p0[2] ) ;
354        
355     myMeshGUI->DisplaySimulationNode( myMesh, p0[0], p0[1], p0[2] );
356     //  myMeshGUI->ViewNodes();
357   } 
358
359   return ;
360 }
361
362
363 //=======================================================================
364 // function : closeEvent()
365 // purpose  :
366 //=======================================================================
367 void SMESHGUI_NodesDlg::closeEvent(QCloseEvent* e)
368 {
369   this->ClickOnCancel() ; /* same than click on cancel button */
370   return ;
371 }
372
373
374 //=================================================================================
375 // function : enterEvent()
376 // purpose  : to reactivate this dialog box when mouse enter onto the window
377 //=================================================================================
378 void SMESHGUI_NodesDlg::enterEvent( QEvent* e)
379 {
380   if ( GroupConstructors->isEnabled() )
381     return ;  
382   ActivateThisDialog() ;
383   return ;  
384 }
385
386
387 //=================================================================================
388 // function : DeactivateActiveDialog()
389 // purpose  : public slot to deactivate if active
390 //=================================================================================
391 void SMESHGUI_NodesDlg::DeactivateActiveDialog()
392 {
393   if ( GroupConstructors->isEnabled() ) {
394     GroupConstructors->setEnabled(false) ;
395     GroupCoordinates->setEnabled(false) ;
396     GroupButtons->setEnabled(false) ;
397     myMeshGUI->EraseSimulationActors() ;
398     myMeshGUI->ResetState() ;    
399     myMeshGUI->SetActiveDialogBox(0) ;
400   }
401   return ;
402 }
403
404
405 //=================================================================================
406 // function : ActivateThisDialog()
407 // purpose  :
408 //=================================================================================
409 void SMESHGUI_NodesDlg::ActivateThisDialog( )
410 {
411   myMeshGUI->EmitSignalDeactivateDialog() ;
412   GroupConstructors->setEnabled(true) ;
413   GroupCoordinates->setEnabled(true) ;
414   GroupButtons->setEnabled(true) ;
415   return ;
416 }