Salome HOME
Add support for tetra, pyramid and prism
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_MoveNodesDlg.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_MoveNodesDlg.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SMESH
27 //  $Header$
28
29 using namespace std;
30 #include "SMESHGUI_MoveNodesDlg.h"
31 #include "SMESHGUI.h"
32 #include "SMESHGUI_SpinBox.h"
33
34 #include "QAD_Application.h"
35 #include "QAD_Desktop.h"
36 #include "QAD_MessageBox.h"
37 #include "utilities.h"
38
39 // QT Includes
40 #include <qbuttongroup.h>
41 #include <qgroupbox.h>
42 #include <qlabel.h>
43 #include <qlineedit.h>
44 #include <qpushbutton.h>
45 #include <qradiobutton.h>
46 #include <qlayout.h>
47 #include <qvariant.h>
48 #include <qtooltip.h>
49 #include <qwhatsthis.h>
50 #include <qimage.h>
51 #include <qpixmap.h>
52
53 // Open CASCADE Include
54 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
55
56 // VTK Include
57 #include <vtkActor.h>
58
59 //=================================================================================
60 // class    : SMESHGUI_MoveNodesDlg()
61 // purpose  : 
62 //=================================================================================
63 SMESHGUI_MoveNodesDlg::SMESHGUI_MoveNodesDlg( QWidget* parent, const char* name, SALOME_Selection* Sel,
64                                               bool modal, WFlags fl )
65     : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
66 {
67     QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH",tr("ICON_DLG_MOVE_NODE")));
68     QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH",tr("ICON_SELECT")));
69
70     if ( !name )
71         setName( "SMESHGUI_MoveNodesDlg" );
72     resize( 303, 185 ); 
73     setCaption( tr( "SMESH_MOVE_NODES_TITLE"  ) );
74     setSizeGripEnabled( TRUE );
75     SMESHGUI_MoveNodesDlgLayout = new QGridLayout( this ); 
76     SMESHGUI_MoveNodesDlgLayout->setSpacing( 6 );
77     SMESHGUI_MoveNodesDlgLayout->setMargin( 11 );
78
79     /***************************************************************/
80     GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
81     GroupConstructors->setTitle( tr( "SMESH_NODES"  ) );
82     GroupConstructors->setExclusive( TRUE );
83     GroupConstructors->setColumnLayout(0, Qt::Vertical );
84     GroupConstructors->layout()->setSpacing( 0 );
85     GroupConstructors->layout()->setMargin( 0 );
86     GroupConstructorsLayout = new QGridLayout( GroupConstructors->layout() );
87     GroupConstructorsLayout->setAlignment( Qt::AlignTop );
88     GroupConstructorsLayout->setSpacing( 6 );
89     GroupConstructorsLayout->setMargin( 11 );
90     Constructor1 = new QRadioButton( GroupConstructors, "Constructor1" );
91     Constructor1->setText( tr( ""  ) );
92     Constructor1->setPixmap( image0 );
93     Constructor1->setChecked( TRUE );
94     Constructor1->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, Constructor1->sizePolicy().hasHeightForWidth() ) );
95     Constructor1->setMinimumSize( QSize( 50, 0 ) );
96     GroupConstructorsLayout->addWidget( Constructor1, 0, 0 );
97     QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
98     GroupConstructorsLayout->addItem( spacer, 0, 1 );
99     SMESHGUI_MoveNodesDlgLayout->addWidget( GroupConstructors, 0, 0 );
100     
101     /***************************************************************/
102     GroupButtons = new QGroupBox( this, "GroupButtons" );
103     GroupButtons->setGeometry( QRect( 10, 10, 281, 48 ) ); 
104     GroupButtons->setTitle( tr( ""  ) );
105     GroupButtons->setColumnLayout(0, Qt::Vertical );
106     GroupButtons->layout()->setSpacing( 0 );
107     GroupButtons->layout()->setMargin( 0 );
108     GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
109     GroupButtonsLayout->setAlignment( Qt::AlignTop );
110     GroupButtonsLayout->setSpacing( 6 );
111     GroupButtonsLayout->setMargin( 11 );
112     buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
113     buttonCancel->setText( tr( "SMESH_BUT_CLOSE"  ) );
114     buttonCancel->setAutoDefault( TRUE );
115     GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
116     buttonApply = new QPushButton( GroupButtons, "buttonApply" );
117     buttonApply->setText( tr( "SMESH_BUT_APPLY"  ) );
118     buttonApply->setAutoDefault( TRUE );
119     GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
120     QSpacerItem* spacer_9 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
121     GroupButtonsLayout->addItem( spacer_9, 0, 2 );
122     buttonOk = new QPushButton( GroupButtons, "buttonOk" );
123     buttonOk->setText( tr( "SMESH_BUT_OK"  ) );
124     buttonOk->setAutoDefault( TRUE );
125     buttonOk->setDefault( TRUE );
126     GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
127     SMESHGUI_MoveNodesDlgLayout->addWidget( GroupButtons, 3, 0 );
128
129     /***************************************************************/
130     GroupC1 = new QGroupBox( this, "GroupC1" );
131     GroupC1->setTitle( tr( "SMESH_MOVE"  ) );
132     GroupC1->setMinimumSize( QSize( 0, 0 ) );
133     GroupC1->setFrameShape( QGroupBox::Box );
134     GroupC1->setFrameShadow( QGroupBox::Sunken );
135     GroupC1->setColumnLayout(0, Qt::Vertical );
136     GroupC1->layout()->setSpacing( 0 );
137     GroupC1->layout()->setMargin( 0 );
138     GroupC1Layout = new QGridLayout( GroupC1->layout() );
139     GroupC1Layout->setAlignment( Qt::AlignTop );
140     GroupC1Layout->setSpacing( 6 );
141     GroupC1Layout->setMargin( 11 );
142     TextLabelC1A1 = new QLabel( GroupC1, "TextLabelC1A1" );
143     TextLabelC1A1->setText( tr( "SMESH_ID_NODES"  ) );
144     TextLabelC1A1->setMinimumSize( QSize( 50, 0 ) );
145     TextLabelC1A1->setFrameShape( QLabel::NoFrame );
146     TextLabelC1A1->setFrameShadow( QLabel::Plain );
147     GroupC1Layout->addWidget( TextLabelC1A1, 0, 0 );
148     SelectButtonC1A1 = new QPushButton( GroupC1, "SelectButtonC1A1" );
149     SelectButtonC1A1->setText( tr( ""  ) );
150     SelectButtonC1A1->setPixmap( image1 );
151     SelectButtonC1A1->setToggleButton( FALSE );
152     GroupC1Layout->addWidget( SelectButtonC1A1, 0, 1 );
153     LineEditC1A1 = new QLineEdit( GroupC1, "LineEditC1A1" );
154     GroupC1Layout->addWidget( LineEditC1A1, 0, 2 );
155
156     SMESHGUI_MoveNodesDlgLayout->addWidget( GroupC1, 1, 0 );
157
158     /***************************************************************/
159     GroupCoordinates = new QGroupBox( this, "GroupCoordinates" );
160     GroupCoordinates->setTitle( tr( "SMESH_COORDINATES"  ) );
161     GroupCoordinates->setColumnLayout(0, Qt::Vertical );
162     GroupCoordinates->layout()->setSpacing( 0 );
163     GroupCoordinates->layout()->setMargin( 0 );
164     GroupCoordinatesLayout = new QGridLayout( GroupCoordinates->layout() );
165     GroupCoordinatesLayout->setAlignment( Qt::AlignTop );
166     GroupCoordinatesLayout->setSpacing( 6 );
167     GroupCoordinatesLayout->setMargin( 11 );
168     TextLabel_X = new QLabel( GroupCoordinates, "TextLabel_X" );
169     TextLabel_X->setText( tr( "SMESH_X"  ) );
170     GroupCoordinatesLayout->addWidget( TextLabel_X, 0, 0 );
171     TextLabel_Y = new QLabel( GroupCoordinates, "TextLabel_Y" );
172     TextLabel_Y->setText( tr( "SMESH_Y"  ) );
173     GroupCoordinatesLayout->addWidget( TextLabel_Y, 0, 2 );
174   
175     SpinBox_X = new SMESHGUI_SpinBox( GroupCoordinates, "SpinBox_X" ) ;
176     GroupCoordinatesLayout->addWidget( SpinBox_X, 0, 1 );
177     
178     SpinBox_Y = new SMESHGUI_SpinBox( GroupCoordinates, "SpinBox_Y" ) ;
179     GroupCoordinatesLayout->addWidget( SpinBox_Y, 0, 3 );
180     
181     SpinBox_Z = new SMESHGUI_SpinBox( GroupCoordinates, "SpinBox_Z" ) ;
182     GroupCoordinatesLayout->addWidget( SpinBox_Z, 0, 5 );
183
184
185     TextLabel_Z = new QLabel( GroupCoordinates, "TextLabel_Z" );
186     TextLabel_Z->setText( tr( "SMESH_Z"  ) );
187     GroupCoordinatesLayout->addWidget( TextLabel_Z, 0, 4 );
188
189     SMESHGUI_MoveNodesDlgLayout->addWidget( GroupCoordinates, 2, 0 );
190
191     Init(Sel) ; /* Initialisations */
192 }
193
194 //=================================================================================
195 // function : ~SMESHGUI_MoveNodesDlg()
196 // purpose  : Destroys the object and frees any allocated resources
197 //=================================================================================
198 SMESHGUI_MoveNodesDlg::~SMESHGUI_MoveNodesDlg()
199 {
200     // no need to delete child widgets, Qt does it all for us
201 }
202
203
204 //=================================================================================
205 // function : Init()
206 // purpose  :
207 //=================================================================================
208 void SMESHGUI_MoveNodesDlg::Init( SALOME_Selection* Sel )
209 {
210
211   /* Get setting of step value from file configuration */
212   double step ;
213   // QString St = QAD_CONFIG->getSetting( "xxxxxxxxxxxxx" ) ;  TODO
214   // step = St.toDouble() ;                                    TODO
215   step = 25.0 ;
216   
217   GroupC1->show();
218   myConstructorId = 0 ;
219   Constructor1->setChecked( TRUE );
220   myEditCurrentArgument = LineEditC1A1 ;        
221   mySelection = Sel;  
222   this->myOkNodes = false ;
223   mySMESHGUI = SMESHGUI::GetSMESHGUI() ;
224   mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
225
226   /* min, max, step and decimals for spin boxes */
227   SpinBox_X->RangeStepAndValidator( -999.999, +999.999, step, 3 ) ;
228   SpinBox_Y->RangeStepAndValidator( -999.999, +999.999, step, 3 ) ;
229   SpinBox_Z->RangeStepAndValidator( -999.999, +999.999, step, 3 ) ;  
230   SpinBox_X->SetValue( 0.0 ) ;
231   SpinBox_Y->SetValue( 0.0 ) ;
232   SpinBox_Z->SetValue( 0.0 ) ;
233
234   /* signals and slots connections */
235   connect( buttonOk, SIGNAL( clicked() ),     this, SLOT( ClickOnOk() ) );
236   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
237   connect( buttonApply, SIGNAL( clicked() ), this, SLOT(ClickOnApply() ) );
238   connect( GroupConstructors, SIGNAL(clicked(int) ), SLOT( ConstructorsClicked(int) ) );
239
240   connect( SelectButtonC1A1, SIGNAL (clicked() ),   this, SLOT( SetEditCurrentArgument() ) ) ;
241   connect( mySMESHGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
242   connect( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
243   /* to close dialog if study change */
244   connect( mySMESHGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
245
246   connect( SpinBox_X, SIGNAL ( valueChanged( double) ), this, SLOT( ValueChangedInSpinBox( double) ) ) ;
247   connect( SpinBox_Y, SIGNAL ( valueChanged( double) ), this, SLOT( ValueChangedInSpinBox( double) ) ) ;
248   connect( SpinBox_Z, SIGNAL ( valueChanged( double) ), this, SLOT( ValueChangedInSpinBox( double) ) ) ;
249
250   /* Move widget on the botton right corner of main widget */
251   int x, y ;
252   mySMESHGUI->DefineDlgPosition( this, x, y ) ;
253   this->move( x, y ) ;
254   this->show() ; /* displays Dialog */
255
256   SelectionIntoArgument();
257   
258   return ;
259 }
260
261
262 //=================================================================================
263 // function : ConstructorsClicked()
264 // purpose  : Radio button management
265 //=================================================================================
266 void SMESHGUI_MoveNodesDlg::ConstructorsClicked(int constructorId)
267 {
268   return ;
269 }
270
271
272 //=================================================================================
273 // function : ClickOnApply()
274 // purpose  :
275 //=================================================================================
276 void SMESHGUI_MoveNodesDlg::ClickOnApply()
277 {
278   switch(myConstructorId)
279     { 
280     case 0 :
281       { 
282         if(myOkNodes) {   
283           mySMESHGUI->EraseSimulationActors();
284 //        mySMESHGUI->MoveNode( myMesh, myIdnode, LineEdit_X->text().toFloat(), LineEdit_Y->text().toFloat(), LineEdit_Z->text().toFloat() ) ;
285 //        mySelection->ClearIObjects();
286         }
287         break ;
288       }
289     }
290   return ;
291 }
292
293 //=================================================================================
294 // function : ClickOnOk()
295 // purpose  :
296 //=================================================================================
297 void SMESHGUI_MoveNodesDlg::ClickOnOk()
298 {
299   this->ClickOnApply() ;
300   this->ClickOnCancel() ;
301
302   return ;
303 }
304
305         
306 //=================================================================================
307 // function : ClickOnCancel()
308 // purpose  :
309 //=================================================================================
310 void SMESHGUI_MoveNodesDlg::ClickOnCancel()
311 {
312   QAD_Application::getDesktop()->SetSelectionMode( 4 );
313   disconnect( mySelection, 0, this, 0 );
314   mySMESHGUI->ResetState() ;
315   mySMESHGUI->EraseSimulationActors();
316   reject() ;
317   return ;
318 }
319
320
321 //=================================================================================
322 // function : SelectionIntoArgument()
323 // purpose  : Called when selection as changed or other case
324 //=================================================================================
325 void SMESHGUI_MoveNodesDlg::SelectionIntoArgument()
326 {
327
328   disconnect( SpinBox_X, SIGNAL ( valueChanged( double) ), this, SLOT( ValueChangedInSpinBox( double) ) ) ;
329   disconnect( SpinBox_Y, SIGNAL ( valueChanged( double) ), this, SLOT( ValueChangedInSpinBox( double) ) ) ;
330   disconnect( SpinBox_Z, SIGNAL ( valueChanged( double) ), this, SLOT( ValueChangedInSpinBox( double) ) ) ;
331
332   mySMESHGUI->EraseSimulationActors();
333
334   myEditCurrentArgument->setText("") ;
335   myOkNodes = false;
336   QString aString = "";
337
338   int nbSel = mySelection->IObjectCount();
339   if(nbSel != 1)
340     return ;
341
342   int nbNodes = mySMESHGUI->GetNameOfSelectedNodes(mySelection, aString) ;
343   if(nbNodes != 1) {
344     SpinBox_X->SetValue(0.0) ;
345     SpinBox_Y->SetValue(0.0) ;
346     SpinBox_Z->SetValue(0.0) ;
347     return ;
348   }
349   
350   if ( mySelection->SelectionMode() != 1 ){
351     QAD_MessageBox::warn1 ( QAD_Application::getDesktop(), tr ("SMESH_WRN_WARNING"),
352                             tr ("SMESH_WRN_SELECTIONMODE_NODES"), tr ("SMESH_BUT_YES") );
353     return;
354   }
355
356   myEditCurrentArgument->setText(aString) ;
357   Standard_Boolean res;
358   myMesh = mySMESHGUI->ConvertIOinMesh( mySelection->firstIObject(), res );
359   if (!res)
360     return ;
361
362   SMESH_Actor* ac = mySMESHGUI->FindActorByEntry( mySelection->firstIObject()->getEntry(), res, false );
363   if ( !res )
364     return ;
365
366   mySelection->GetIndex( mySelection->firstIObject(), myMapIndex);
367
368   TColStd_MapIteratorOfMapOfInteger ite( myMapIndex );
369   if ( ite.More() ) {
370     myIdnode = ite.Key();
371     mySimulationActor = mySMESHGUI->SimulationMoveNode( ac, myIdnode );
372   } else
373     return ;
374
375   MESSAGE ( " myIdnode " << myIdnode );
376
377   float *pt  = ac->GetMapper()->GetInput()->GetPoint(myIdnode);
378   MESSAGE ( " pt " << pt[0] << ";" << pt[1] << ";" << pt[2] )
379
380   SpinBox_X->SetValue( (double)pt[0] ) ;
381   SpinBox_Y->SetValue( (double)pt[1] ) ;
382   SpinBox_Z->SetValue( (double)pt[2] ) ;
383
384   connect( SpinBox_X, SIGNAL ( valueChanged( double) ), this, SLOT( ValueChangedInSpinBox( double) ) ) ;
385   connect( SpinBox_Y, SIGNAL ( valueChanged( double) ), this, SLOT( ValueChangedInSpinBox( double) ) ) ;
386   connect( SpinBox_Z, SIGNAL ( valueChanged( double) ), this, SLOT( ValueChangedInSpinBox( double) ) ) ;
387
388   myOkNodes = true ;
389   return ;
390 }
391
392
393 //=================================================================================
394 // function : SetEditCurrentArgument()
395 // purpose  :
396 //=================================================================================
397 void SMESHGUI_MoveNodesDlg::SetEditCurrentArgument()
398 {
399   QPushButton* send = (QPushButton*)sender();
400   switch (myConstructorId)
401     {
402     case 0: /* default constructor */
403       { 
404         if(send == SelectButtonC1A1) {
405           LineEditC1A1->setFocus() ;
406           myEditCurrentArgument = LineEditC1A1;
407         }
408         SelectionIntoArgument() ;
409         break;
410       }
411     }
412   return ;
413 }
414
415 //=================================================================================
416 // function : DeactivateActiveDialog()
417 // purpose  :
418 //=================================================================================
419 void SMESHGUI_MoveNodesDlg::DeactivateActiveDialog()
420 {
421   if ( GroupConstructors->isEnabled() ) {
422     GroupConstructors->setEnabled(false) ;
423     GroupC1->setEnabled(false) ;
424     GroupButtons->setEnabled(false) ;
425     mySMESHGUI->EraseSimulationActors() ;
426     mySMESHGUI->ResetState() ;    
427     mySMESHGUI->SetActiveDialogBox(0) ;
428   }
429   return ;
430 }
431
432
433 //=================================================================================
434 // function : ActivateThisDialog()
435 // purpose  :
436 //=================================================================================
437 void SMESHGUI_MoveNodesDlg::ActivateThisDialog()
438 {
439   /* Emit a signal to deactivate the active dialog */
440   mySMESHGUI->EmitSignalDeactivateDialog() ;   
441   GroupConstructors->setEnabled(true) ;
442   GroupC1->setEnabled(true) ;
443   GroupButtons->setEnabled(true) ;
444   
445   mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
446   return ;
447 }
448
449
450 //=================================================================================
451 // function : enterEvent()
452 // purpose  :
453 //=================================================================================
454 void SMESHGUI_MoveNodesDlg::enterEvent(QEvent* e)
455 {
456   if ( GroupConstructors->isEnabled() )
457     return ;  
458   ActivateThisDialog() ;
459   return ;
460 }
461
462
463 //=================================================================================
464 // function : closeEvent()
465 // purpose  :
466 //=================================================================================
467 void SMESHGUI_MoveNodesDlg::closeEvent( QCloseEvent* e )
468 {
469   /* same than click on cancel button */
470   this->ClickOnCancel() ;
471   return ;
472 }
473
474
475 //=================================================================================
476 // function : ValueChangedInSpinBox()
477 // purpose  :
478 //=================================================================================
479 void SMESHGUI_MoveNodesDlg::ValueChangedInSpinBox( double newValue )
480 {
481   double vx = SpinBox_X->GetValue() ;
482   double vy = SpinBox_Y->GetValue() ;
483   double vz = SpinBox_Z->GetValue() ;
484   
485   mySMESHGUI->DisplaySimulationMoveNode( mySimulationActor, myIdnode, vx, vy , vz );
486 }