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