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