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