Salome HOME
NRI : Replace Geometry by GEOM.
[modules/geom.git] / src / GEOMGUI / GeometryGUI_TranslationDlg.cxx
1 using namespace std;
2 //  File      : GeometryGUI_TranslationDlg.cxx
3 //  Created   :
4 //  Author    : Lucien PIGNOLONI
5 //  Project   : SALOME
6 //  Module    : GeometryGUI
7 //  Copyright : OPEN CASCADE
8 //  $Header$
9
10 #include "GeometryGUI_TranslationDlg.h"
11
12 #include "GeometryGUI.h"
13 #include "QAD_Application.h"
14 #include "QAD_Config.h"
15 #include "QAD_Desktop.h"
16 #include "utilities.h"
17
18 #include <qbuttongroup.h>
19 #include <qgroupbox.h>
20 #include <qlabel.h>
21 #include <qlineedit.h>
22 #include <qpushbutton.h>
23 #include <qradiobutton.h>
24 #include <qlayout.h>
25 #include <qvariant.h>
26 #include <qtooltip.h>
27 #include <qwhatsthis.h>
28 #include <qimage.h>
29 #include <qvalidator.h>
30 #include <qpixmap.h>
31 #include <qevent.h>
32
33
34
35 //=================================================================================
36 // class    : GeometryGUI_TranslationDlg()
37 // purpose  : Constructs a GeometryGUI_TranslationDlg which is a child of 'parent', with the 
38 //            name 'name' and widget flags set to 'f'.
39 //            The dialog will by default be modeless, unless you set 'modal' to
40 //            TRUE to construct a modal dialog.
41 //=================================================================================
42 GeometryGUI_TranslationDlg::GeometryGUI_TranslationDlg( QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl )
43     : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
44 {
45     QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "GEOM",tr("ICON_DLG_TRANSLATION")));
46     QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap( "GEOM",tr("ICON_SELECT")));
47
48     if ( !name )
49         setName( "GeometryGUI_TranslationDlg" );
50     resize( 303, 219 ); 
51     setCaption( tr( "GEOM_TRANSLATION_TITLE"  ) );
52     setSizeGripEnabled( TRUE );
53     GeometryGUI_TranslationDlgLayout = new QGridLayout( this ); 
54     GeometryGUI_TranslationDlgLayout->setSpacing( 6 );
55     GeometryGUI_TranslationDlgLayout->setMargin( 11 );
56
57     /***************************************************************/
58     GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
59     GroupConstructors->setTitle( tr( "GEOM_TRANSLATION"  ) );
60     GroupConstructors->setExclusive( TRUE );
61     GroupConstructors->setColumnLayout(0, Qt::Vertical );
62     GroupConstructors->layout()->setSpacing( 0 );
63     GroupConstructors->layout()->setMargin( 0 );
64     GroupConstructorsLayout = new QGridLayout( GroupConstructors->layout() );
65     GroupConstructorsLayout->setAlignment( Qt::AlignTop );
66     GroupConstructorsLayout->setSpacing( 6 );
67     GroupConstructorsLayout->setMargin( 11 );
68     Constructor1 = new QRadioButton( GroupConstructors, "Constructor1" );
69     Constructor1->setText( tr( ""  ) );
70     Constructor1->setPixmap( image0 );
71     Constructor1->setChecked( TRUE );
72     Constructor1->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, Constructor1->sizePolicy().hasHeightForWidth() ) );
73     Constructor1->setMinimumSize( QSize( 50, 0 ) );
74     GroupConstructorsLayout->addWidget( Constructor1, 0, 0 );
75     QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
76     GroupConstructorsLayout->addItem( spacer, 0, 1 );
77     GeometryGUI_TranslationDlgLayout->addWidget( GroupConstructors, 0, 0 );
78
79     GroupC1 = new QGroupBox( this, "GroupC1" );
80     GroupC1->setTitle( tr( "GEOM_TRANSLATION"  ) ) ;
81     GroupC1->setMinimumSize( QSize( 0, 0 ) );
82     GroupC1->setFrameShape( QGroupBox::Box );
83     GroupC1->setFrameShadow( QGroupBox::Sunken );
84     GroupC1->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)7, (QSizePolicy::SizeType)5, GroupC1->sizePolicy().hasHeightForWidth() ) );
85     GroupC1->setColumnLayout(0, Qt::Vertical );
86     GroupC1->layout()->setSpacing( 0 );
87     GroupC1->layout()->setMargin( 0 );
88     GroupC1Layout = new QGridLayout( GroupC1->layout() );
89     GroupC1Layout->setAlignment( Qt::AlignTop );
90     GroupC1Layout->setSpacing( 6 );
91     GroupC1Layout->setMargin( 11 );
92
93     TextLabelC1A1 = new QLabel( GroupC1, "TextLabelC1A1" );
94     TextLabelC1A1->setText( tr( "GEOM_OBJECT"  ) );
95     TextLabelC1A1->setMinimumSize( QSize( 50, 0 ) );
96     TextLabelC1A1->setFrameShape( QLabel::NoFrame );
97     TextLabelC1A1->setFrameShadow( QLabel::Plain );
98     GroupC1Layout->addWidget( TextLabelC1A1, 0, 0 );
99
100     SelectButtonC1A1 = new QPushButton( GroupC1, "SelectButtonC1A1" );
101     SelectButtonC1A1->setText( tr( ""  ) );
102     SelectButtonC1A1->setPixmap( image1 );
103     SelectButtonC1A1->setToggleButton( FALSE );
104     SelectButtonC1A1->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)0, (QSizePolicy::SizeType)0, SelectButtonC1A1->sizePolicy().hasHeightForWidth() ) );
105     GroupC1Layout->addWidget( SelectButtonC1A1, 0, 1 );
106
107     LineEditC1A1 = new QLineEdit( GroupC1, "LineEditC1A1" );
108     GroupC1Layout->addWidget( LineEditC1A1, 0, 2 );
109
110     Layout1 = new QHBoxLayout; 
111     Layout1->setSpacing( 6 );
112     Layout1->setMargin( 0 );
113
114     TextLabel_DX = new QLabel( GroupC1, "TextLabel_DX" );
115     TextLabel_DX->setText( tr( "GEOM_DX" ) );
116     Layout1->addWidget( TextLabel_DX );
117
118     SpinBox_DX = new GeometryGUI_SpinBox( GroupC1, "SpinBox_DX" );
119     SpinBox_DX->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)7, (QSizePolicy::SizeType)0, SpinBox_DX->sizePolicy().hasHeightForWidth() ) );
120     Layout1->addWidget( SpinBox_DX );
121
122     TextLabel_DY = new QLabel( GroupC1, "TextLabel_DY" );
123     TextLabel_DY->setText( tr( "GEOM_DY" ) );
124     Layout1->addWidget( TextLabel_DY );
125
126     SpinBox_DY = new GeometryGUI_SpinBox( GroupC1, "SpinBox_DY" );
127     SpinBox_DY->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)7, (QSizePolicy::SizeType)0, SpinBox_DY->sizePolicy().hasHeightForWidth() ) );
128     Layout1->addWidget( SpinBox_DY );
129
130     TextLabel_DZ = new QLabel( GroupC1, "TextLabel_DZ" );
131     TextLabel_DZ->setText( tr( "GEOM_DZ" ) );
132     Layout1->addWidget( TextLabel_DZ );
133
134     SpinBox_DZ = new GeometryGUI_SpinBox( GroupC1, "SpinBox_DZ" );
135     SpinBox_DZ->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)7, (QSizePolicy::SizeType)0, SpinBox_DZ->sizePolicy().hasHeightForWidth() ) );
136     Layout1->addWidget( SpinBox_DZ );
137
138     GroupC1Layout->addMultiCellLayout( Layout1, 1, 1, 0, 2 );
139
140     GeometryGUI_TranslationDlgLayout->addWidget( GroupC1, 1, 0 );
141
142
143     /***************************************************************/
144     GroupButtons = new QGroupBox( this, "GroupButtons" );
145     GroupButtons->setGeometry( QRect( 10, 10, 281, 48 ) ); 
146     GroupButtons->setTitle( tr( ""  ) );
147     GroupButtons->setColumnLayout(0, Qt::Vertical );
148     GroupButtons->layout()->setSpacing( 0 );
149     GroupButtons->layout()->setMargin( 0 );
150     GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
151     GroupButtonsLayout->setAlignment( Qt::AlignTop );
152     GroupButtonsLayout->setSpacing( 6 );
153     GroupButtonsLayout->setMargin( 11 );
154     buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
155     buttonCancel->setText( tr( "GEOM_BUT_CLOSE"  ) );
156     buttonCancel->setAutoDefault( TRUE );
157     GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
158     buttonApply = new QPushButton( GroupButtons, "buttonApply" );
159     buttonApply->setText( tr( "GEOM_BUT_APPLY"  ) );
160     buttonApply->setAutoDefault( TRUE );
161     GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
162     QSpacerItem* spacer_9 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
163     GroupButtonsLayout->addItem( spacer_9, 0, 2 );
164     buttonOk = new QPushButton( GroupButtons, "buttonOk" );
165     buttonOk->setText( tr( "GEOM_BUT_OK"  ) );
166     buttonOk->setAutoDefault( TRUE );
167     buttonOk->setDefault( TRUE );
168     GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
169     GeometryGUI_TranslationDlgLayout->addWidget( GroupButtons, 2, 0 );
170     /***************************************************************/
171
172     Init(Sel) ; /* Initialisations */
173 }
174
175
176
177 //=================================================================================
178 // function : ~GeometryGUI_TranslationDlg()
179 // purpose  : Destroys the object and frees any allocated resources
180 //=================================================================================
181 GeometryGUI_TranslationDlg::~GeometryGUI_TranslationDlg()
182 {
183     // no need to delete child widgets, Qt does it all for us
184 }
185
186
187 //=================================================================================
188 // function : Init()
189 // purpose  :
190 //=================================================================================
191 void GeometryGUI_TranslationDlg::Init( SALOME_Selection* Sel )
192 {
193   double step ;
194   QString St = QAD_CONFIG->getSetting( "Geometry:SettingsGeomStep" ) ;
195   step = St.toDouble() ;
196
197   /* min, max, step and decimals for spin boxes */
198   SpinBox_DX->RangeStepAndValidator( -999.999, 999.999, step, 3 ) ;
199   SpinBox_DX->SetValue( 100.0 ) ;
200   SpinBox_DY->RangeStepAndValidator( -999.999, 999.999, step, 3 ) ;
201   SpinBox_DY->SetValue( 100.0 ) ;
202   SpinBox_DZ->RangeStepAndValidator( -999.999, 999.999, step, 3 ) ;
203   SpinBox_DZ->SetValue( 100.0 ) ; 
204
205   GroupC1->show();
206   myConstructorId = 0 ;
207   Constructor1->setChecked( TRUE );
208   myEditCurrentArgument = LineEditC1A1 ;        
209   mySelection = Sel;
210   myGeomGUI = GeometryGUI::GetGeometryGUI() ;
211   myOkBase = false ;
212
213   this->myVec.SetCoord( 100.0, 100.0, 100.0 ) ;
214   mySimulationTopoDs.Nullify() ;
215   myBase.Nullify() ;
216   myGeomGUI->SetActiveDialogBox( (QDialog*)this ) ;  
217
218   // TODO : previous selection into argument ?
219
220   /* Filter definitions */
221   Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "GEOM");
222   myGeom = GEOM::GEOM_Gen::_narrow(comp);
223   // myEdgeFilter   = new GEOM_ShapeTypeFilter( TopAbs_EDGE, myGeom );
224
225   /* signals and slots connections */
226   connect( buttonOk, SIGNAL( clicked() ),     this, SLOT( ClickOnOk() ) );
227   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
228   connect( buttonApply, SIGNAL( clicked() ), this, SLOT(ClickOnApply() ) );
229   connect( GroupConstructors, SIGNAL(clicked(int) ), SLOT( ConstructorsClicked(int) ) );  
230   connect( SelectButtonC1A1, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
231   
232   connect( SpinBox_DX, SIGNAL ( valueChanged( double) ), this, SLOT( ValueChangedInSpinBox( double) ) ) ;
233   connect( SpinBox_DY, SIGNAL ( valueChanged( double) ), this, SLOT( ValueChangedInSpinBox( double) ) ) ;
234   connect( SpinBox_DZ, SIGNAL ( valueChanged( double) ), this, SLOT( ValueChangedInSpinBox( double) ) ) ;
235
236   connect( LineEditC1A1, SIGNAL ( returnPressed() ), this, SLOT( LineEditReturnPressed() ) ) ;
237
238   connect( myGeomGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;  
239   connect( mySelection, SIGNAL( currentSelectionChanged() ),     this, SLOT( SelectionIntoArgument() ) );
240   /* to close dialog if study change */
241   connect( myGeomGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
242  
243   /* Move widget on the botton right corner of main widget */
244   int x, y ;
245   myGeomGUI->DefineDlgPosition( this, x, y ) ;
246   this->move( x, y ) ;
247   this->show() ; /* displays Dialog */
248
249   return ;
250 }
251
252
253 //=================================================================================
254 // function : ConstructorsClicked()
255 // purpose  : Radio button management
256 //=================================================================================
257 void GeometryGUI_TranslationDlg::ConstructorsClicked(int constructorId)
258 {
259   // myGeomGUI->EraseSimulationShape() ;
260   switch (constructorId)
261     {
262     case 0:
263       {
264         break;
265       }
266     case 1:
267       {
268         break;
269       }
270     }
271   return ;
272 }
273
274 //=================================================================================
275 // function : ClickOnOk()
276 // purpose  :
277 //=================================================================================
278 void GeometryGUI_TranslationDlg::ClickOnOk()
279 {
280   this->ClickOnApply() ;
281   this->ClickOnCancel() ;
282
283   return ;
284 }
285
286 //=================================================================================
287 // function : ClickOnApply()
288 // purpose  :
289 //=================================================================================
290 void GeometryGUI_TranslationDlg::ClickOnApply()
291 {
292   myGeomGUI->EraseSimulationShape() ;
293   myGeomGUI->GetDesktop()->putInfo( tr("") ) ; 
294   mySimulationTopoDs.Nullify() ;
295   switch(myConstructorId)
296     { 
297     case 0 :
298       { 
299         if(myOkBase) {
300           myGeomGUI->MakeTranslationAndDisplay( myGeomShape, myVec ) ;
301         }
302         break ;
303       }
304     case 1 :
305       {
306         break ;
307       }
308     }
309
310   // accept();
311   return ;
312 }
313
314 //=================================================================================
315 // function : ClickOnCancel()
316 // purpose  :
317 //=================================================================================
318 void GeometryGUI_TranslationDlg::ClickOnCancel()
319 {
320   mySelection->ClearFilters() ;
321   myGeomGUI->EraseSimulationShape() ;
322   mySimulationTopoDs.Nullify() ;
323   disconnect( mySelection, 0, this, 0 );
324   myGeomGUI->ResetState() ;
325   reject() ;
326   return ;
327 }
328
329
330 //=================================================================================
331 // function : SelectionIntoArgument()
332 // purpose  : Called when selection as changed or other case
333 //=================================================================================
334 void GeometryGUI_TranslationDlg::SelectionIntoArgument()
335 {
336   myEditCurrentArgument->setText("") ;
337   myGeomGUI->EraseSimulationShape() ; 
338   mySimulationTopoDs.Nullify() ;
339
340   /* Future name of selection */
341   QString aString = "";
342
343   int nbSel = myGeomGUI->GetNameOfSelectedIObjects(mySelection, aString) ;
344   if ( nbSel != 1 ) {
345     if ( myEditCurrentArgument == LineEditC1A1 ) {
346       LineEditC1A1->setText("") ;
347       myOkBase = false ;
348     }
349     return ;
350   }
351
352   /* nbSel == 1 ! */
353   TopoDS_Shape S; 
354   Standard_Boolean testResult ;
355   Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject() ;
356
357   if( !myGeomGUI->GetTopoFromSelection(mySelection, S) )
358     return ;
359
360   if ( myConstructorId == 0 && myEditCurrentArgument == LineEditC1A1 ) { 
361     myGeomShape = myGeomGUI->ConvertIOinGEOMShape(IO, testResult) ;
362     if( !testResult )
363       return ;
364     LineEditC1A1->setText(aString) ;
365     myBase = S ;
366     myOkBase = true ;
367   }
368   
369   if( myOkBase ) {
370     MakeTranslationSimulationAndDisplay() ;
371   }
372   return ;
373 }
374
375
376 //=================================================================================
377 // function : SetEditCurrentArgument()
378 // purpose  :
379 //=================================================================================
380 void GeometryGUI_TranslationDlg::SetEditCurrentArgument()
381 {
382   QPushButton* send = (QPushButton*)sender();
383   switch (myConstructorId)
384     {
385     case 0: /* default constructor */
386       { 
387         if(send == SelectButtonC1A1) {
388           LineEditC1A1->setFocus() ;
389           myEditCurrentArgument = LineEditC1A1 ;
390           SelectionIntoArgument() ;
391         }
392         break;
393       }
394     case 1:      
395       {         
396         break;
397       }
398     }
399   return ;
400 }
401
402 //=================================================================================
403 // function : LineEditReturnPressed()
404 // purpose  :
405 //=================================================================================
406 void GeometryGUI_TranslationDlg::LineEditReturnPressed()
407 {    
408   QLineEdit* send = (QLineEdit*)sender();  
409   if( send == LineEditC1A1 )
410     myEditCurrentArgument = LineEditC1A1 ;
411   else
412     return ;
413   
414   /* User name of object input management                          */
415   /* If successfull the selection is changed and signal emitted... */
416   /* so SelectionIntoArgument() is automatically called.           */
417   const QString objectUserName = myEditCurrentArgument->text() ;
418   QWidget* thisWidget = (QWidget*)this ;
419   if( myGeomGUI->SelectionByNameInDialogs( thisWidget, objectUserName, mySelection ) ) {
420     myEditCurrentArgument->setText( objectUserName ) ;
421   }
422   return ;
423 }
424
425
426
427 //=================================================================================
428 // function : ValueChangedInSpinBox()
429 // purpose  :
430 //=================================================================================
431 void GeometryGUI_TranslationDlg::ValueChangedInSpinBox( double newValue )
432 {  
433
434   QObject* send = (QObject*)sender() ; 
435   Standard_Real Dx, Dy, Dz ;
436
437   if( send == SpinBox_DX ) {
438     Dx = newValue ;
439     Dy = SpinBox_DY->GetValue() ;
440     Dz = SpinBox_DZ->GetValue() ;
441   } else if( send == SpinBox_DY ) {
442     Dx = SpinBox_DX->GetValue() ;
443     Dy = newValue ;
444     Dz = SpinBox_DZ->GetValue() ;
445   } else if( send == SpinBox_DZ ) {
446     Dx = SpinBox_DX->GetValue() ;
447     Dy = SpinBox_DY->GetValue() ;
448     Dz = newValue ;
449   }
450
451   this->myVec.SetCoord(Dx, Dy, Dz) ;
452   if ( myOkBase ) {
453     MakeTranslationSimulationAndDisplay() ;
454   }
455   else {
456     myGeomGUI->EraseSimulationShape() ; 
457     mySimulationTopoDs.Nullify() ;
458   }
459   return ;
460 }
461
462
463
464 //=================================================================================
465 // function : DeactivateActiveDialog()
466 // purpose  :
467 //=================================================================================
468 void GeometryGUI_TranslationDlg::DeactivateActiveDialog()
469 {
470   if ( GroupConstructors->isEnabled() ) {
471     GroupConstructors->setEnabled(false) ;
472     GroupC1->setEnabled(false) ;
473     GroupButtons->setEnabled(false) ;
474     disconnect( mySelection, 0, this, 0 );
475     myGeomGUI->EraseSimulationShape() ;
476     mySelection->ClearFilters() ;
477   }
478   return ;
479 }
480
481
482 //=================================================================================
483 // function : ActivateThisDialog()
484 // purpose  :
485 //=================================================================================
486 void GeometryGUI_TranslationDlg::ActivateThisDialog()
487 {
488   /* Emit a signal to deactivate the active dialog */
489   myGeomGUI->EmitSignalDeactivateDialog() ;  
490   GroupConstructors->setEnabled(true) ;
491   GroupC1->setEnabled(true) ;
492   GroupButtons->setEnabled(true) ;
493
494   connect ( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
495   if( !mySimulationTopoDs.IsNull() )
496     myGeomGUI->DisplaySimulationShape( mySimulationTopoDs ) ;
497
498   return ;
499 }
500
501
502 //=================================================================================
503 // function : enterEvent()
504 // purpose  :
505 //=================================================================================
506 void GeometryGUI_TranslationDlg::enterEvent(QEvent* e)
507 {
508   if ( GroupConstructors->isEnabled() )
509     return ;  
510   ActivateThisDialog() ;
511 }
512
513
514
515 //=================================================================================
516 // function : closeEvent()
517 // purpose  :
518 //=================================================================================
519 void GeometryGUI_TranslationDlg::closeEvent( QCloseEvent* e )
520 {
521   /* same than click on cancel button */
522   this->ClickOnCancel() ;
523   return ;
524 }
525
526
527 //=================================================================================
528 // function : MakeTranslationSimulationAndDisplay()
529 // purpose  :
530 //=================================================================================
531 void GeometryGUI_TranslationDlg::MakeTranslationSimulationAndDisplay()
532 {
533   myGeomGUI->EraseSimulationShape() ; 
534   mySimulationTopoDs.Nullify() ;
535
536   gp_Trsf theTransformation ;
537   theTransformation.SetTranslation(this->myVec) ;
538
539   BRepBuilderAPI_Transform myBRepTransformation(this->myBase, theTransformation, Standard_False) ;
540   mySimulationTopoDs = myBRepTransformation.Shape() ;
541   myGeomGUI->DisplaySimulationShape( mySimulationTopoDs ) ;
542   return ;
543 }