Salome HOME
NRI : Merge BRANCH_V1_2c
[modules/geom.git] / src / GEOMGUI / GeometryGUI_EllipseDlg.cxx
1 //  GEOM GEOMGUI : GUI for Geometry component
2 //
3 //  Copyright (C) 2003  OPEN CASCADE 
4 // 
5 //  This library is free software; you can redistribute it and/or 
6 //  modify it under the terms of the GNU Lesser General Public 
7 //  License as published by the Free Software Foundation; either 
8 //  version 2.1 of the License. 
9 // 
10 //  This library is distributed in the hope that it will be useful, 
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
13 //  Lesser General Public License for more details. 
14 // 
15 //  You should have received a copy of the GNU Lesser General Public 
16 //  License along with this library; if not, write to the Free Software 
17 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
18 // 
19 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
20 //
21 //
22 //
23 //  File   : GeometryGUI_EllipseDlg.cxx
24 //  Author : Nicolas REJNERI
25 //  Module : GEOM
26 //  $Header$
27
28 #include "GeometryGUI_EllipseDlg.h"
29
30 #include "GeometryGUI.h"
31 #include "gp_Elips.hxx"
32 #include "QAD_Application.h"
33 #include "QAD_Desktop.h"
34 #include "QAD_Config.h"
35 #include "utilities.h"
36
37 #include <BRepAdaptor_Curve.hxx>
38
39 #include <qbuttongroup.h>
40 #include <qgroupbox.h>
41 #include <qlabel.h>
42 #include <qlineedit.h>
43 #include <qpushbutton.h>
44 #include <qradiobutton.h>
45 #include <qspinbox.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 //=================================================================================
54 // class    : GeometryGUI_EllipseDlg()
55 // purpose  : Constructs a GeometryGUI_EllipseDlg which is a child of 'parent', with the 
56 //            name 'name' and widget flags set to 'f'.
57 //            The dialog will by default be modeless, unless you set 'modal' to
58 //            TRUE to construct a modal dialog.
59 //=================================================================================
60 GeometryGUI_EllipseDlg::GeometryGUI_EllipseDlg( QWidget* parent,  const char* name, SALOME_Selection* Sel, bool modal, WFlags fl )
61     : QDialog( parent, name, modal, fl )
62 {
63     QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "GEOM",tr("ICON_DLG_ELLIPSE_PV")));
64     QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap( "GEOM",tr("ICON_SELECT")));
65
66     if ( !name )
67         setName( "GeometryGUI_EllipseDlg" );
68     resize( 303, 285 ); 
69     setCaption( tr( "GEOM_ELLIPSE_TITLE"  ) );
70   
71     GeometryGUI_EllipseDlgLayout = new QGridLayout( this ); 
72     GeometryGUI_EllipseDlgLayout->setSpacing( 6 );
73     GeometryGUI_EllipseDlgLayout->setMargin( 11 );
74     
75     /***************************************************************/
76     GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
77     GroupConstructors->setTitle( tr( "GEOM_ELLIPSE"  ) );
78     GroupConstructors->setExclusive( TRUE );
79     GroupConstructors->setColumnLayout(0, Qt::Vertical );
80     GroupConstructors->layout()->setSpacing( 0 );
81     GroupConstructors->layout()->setMargin( 0 );
82     GroupConstructorsLayout = new QGridLayout( GroupConstructors->layout() );
83     GroupConstructorsLayout->setAlignment( Qt::AlignTop );
84     GroupConstructorsLayout->setSpacing( 6 );
85     GroupConstructorsLayout->setMargin( 11 );
86     Constructor1 = new QRadioButton( GroupConstructors, "Constructor1" );
87     Constructor1->setText( tr( ""  ) );
88     Constructor1->setPixmap( image0 );
89     Constructor1->setChecked( TRUE );
90     Constructor1->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, Constructor1->sizePolicy().hasHeightForWidth() ) );
91     Constructor1->setMinimumSize( QSize( 50, 0 ) );
92     GroupConstructorsLayout->addWidget( Constructor1, 0, 0 );
93     QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
94     GroupConstructorsLayout->addItem( spacer, 0, 1 );
95     GeometryGUI_EllipseDlgLayout->addWidget( GroupConstructors, 0, 0 );
96     
97     /***************************************************************/
98     GroupButtons = new QGroupBox( this, "GroupButtons" );
99     GroupButtons->setTitle( tr( ""  ) );
100     GroupButtons->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)5, (QSizePolicy::SizeType)5, GroupButtons->sizePolicy().hasHeightForWidth() ) );
101     GroupButtons->setColumnLayout(0, Qt::Vertical );
102     GroupButtons->layout()->setSpacing( 0 );
103     GroupButtons->layout()->setMargin( 0 );
104     GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
105     GroupButtonsLayout->setAlignment( Qt::AlignTop );
106     GroupButtonsLayout->setSpacing( 6 );
107     GroupButtonsLayout->setMargin( 11 );
108     buttonApply = new QPushButton( GroupButtons, "buttonApply" );
109     buttonApply->setText( tr( "GEOM_BUT_APPLY"  ) );
110     buttonApply->setAutoDefault( TRUE );
111     buttonApply->setDefault( TRUE );
112     GroupButtonsLayout->addWidget( buttonApply, 0, 2 );
113     buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
114     buttonCancel->setText( tr( "GEOM_BUT_CLOSE"  ) );
115     buttonCancel->setAutoDefault( TRUE );
116     GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
117     QSpacerItem* spacer_2 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
118     GroupButtonsLayout->addItem( spacer_2, 0, 1 );
119     buttonOk = new QPushButton( GroupButtons, "buttonOk" );
120     buttonOk->setText( tr( "GEOM_BUT_OK"  ) );
121     buttonOk->setAutoDefault( TRUE );
122     GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
123     GeometryGUI_EllipseDlgLayout->addWidget( GroupButtons, 2, 0 );
124
125     /***************************************************************/
126     GroupC1 = new QGroupBox( this, "GroupC1" );
127     GroupC1->setTitle( tr( "GEOM_ARGUMENTS"  ) );
128     GroupC1->setMinimumSize( QSize( 0, 150 ) );
129     GroupC1->setFrameShape( QGroupBox::Box );
130     GroupC1->setFrameShadow( QGroupBox::Sunken );
131     GroupC1->setColumnLayout(0, Qt::Vertical );
132     GroupC1->layout()->setSpacing( 0 );
133     GroupC1->layout()->setMargin( 0 );
134     GroupC1Layout = new QGridLayout( GroupC1->layout() );
135     GroupC1Layout->setAlignment( Qt::AlignTop );
136     GroupC1Layout->setSpacing( 6 );
137     GroupC1Layout->setMargin( 11 );
138     LineEditC1A1 = new QLineEdit( GroupC1, "LineEditC1A1" );
139     GroupC1Layout->addWidget( LineEditC1A1, 0, 2 );
140     LineEditC1A2 = new QLineEdit( GroupC1, "LineEditC1A2" );
141     GroupC1Layout->addWidget( LineEditC1A2, 1, 2 );
142     TextLabelC1A1 = new QLabel( GroupC1, "TextLabelC1A1" );
143     TextLabelC1A1->setText( tr( "GEOM_CENTER"  ) );
144     TextLabelC1A1->setMinimumSize( QSize( 50, 0 ) );
145     TextLabelC1A1->setFrameShape( QLabel::NoFrame );
146     TextLabelC1A1->setFrameShadow( QLabel::Plain );
147
148     GroupC1Layout->addWidget( TextLabelC1A1, 0, 0 );
149     TextLabelC1A4 = new QLabel( GroupC1, "TextLabelC1A4" );
150     TextLabelC1A4->setText( tr( "GEOM_RADIUS_MINOR"  ) );
151     TextLabelC1A4->setMinimumSize( QSize( 50, 0 ) );
152     GroupC1Layout->addMultiCellWidget( TextLabelC1A4, 3, 3, 0, 1 );
153
154     TextLabelC1A3 = new QLabel( GroupC1, "TextLabelC1A3" );
155     TextLabelC1A3->setText( tr( "GEOM_RADIUS_MAJOR"  ) );
156     TextLabelC1A3->setMinimumSize( QSize( 50, 0 ) );
157     GroupC1Layout->addWidget( TextLabelC1A3, 2, 0 );
158
159     TextLabelC1A2 = new QLabel( GroupC1, "TextLabelC1A2" );
160     TextLabelC1A2->setText( tr( "GEOM_VECTOR"  ) );
161     TextLabelC1A2->setMinimumSize( QSize( 50, 0 ) );
162
163     GroupC1Layout->addWidget( TextLabelC1A2, 1, 0 );
164     SelectButtonC1A1 = new QPushButton( GroupC1, "SelectButtonC1A1" );
165     SelectButtonC1A1->setText( tr( ""  ) );
166     SelectButtonC1A1->setPixmap( image1 );
167     SelectButtonC1A1->setToggleButton( FALSE );
168     GroupC1Layout->addWidget( SelectButtonC1A1, 0, 1 );
169     SelectButtonC1A2 = new QPushButton( GroupC1, "SelectButtonC1A2" );
170     SelectButtonC1A2->setText( tr( ""  ) );
171     SelectButtonC1A2->setPixmap( image1 );
172     GroupC1Layout->addWidget( SelectButtonC1A2, 1, 1 );
173
174     SpinBox_C1A3 = new GeometryGUI_SpinBox( GroupC1, "GeomSpinBox_C1A3" );
175     GroupC1Layout->addWidget( SpinBox_C1A3, 2, 2 ) ;
176     SpinBox_C1A4 = new GeometryGUI_SpinBox( GroupC1, "GeomSpinBox_C1A4" );
177     GroupC1Layout->addWidget( SpinBox_C1A4, 3, 2 );
178
179     GeometryGUI_EllipseDlgLayout->addWidget( GroupC1, 1, 0 );
180     /***************************************************************/
181
182     Init(Sel) ; /* Initialisations */
183 }
184
185
186 //=================================================================================
187 // function : ~GeometryGUI_EllipseDlg()
188 // purpose  : Destroys the object and frees any allocated resources
189 //=================================================================================
190 GeometryGUI_EllipseDlg::~GeometryGUI_EllipseDlg()
191 {
192   // no need to delete child widgets, Qt does it all for us
193 }
194
195
196
197 //=================================================================================
198 // function : Init()
199 // purpose  :
200 //=================================================================================
201 void GeometryGUI_EllipseDlg::Init( SALOME_Selection* Sel )
202 {
203   double step ;
204   QString St = QAD_CONFIG->getSetting( "Geometry:SettingsGeomStep" ) ;
205   step = St.toDouble() ;
206
207   /* min, max, step and decimals for spin boxes */
208   SpinBox_C1A3->RangeStepAndValidator( 0.00001, 999999.99999, step, 5 ) ;
209   SpinBox_C1A3->SetValue( 200.0 ) ;   /* = myMajorRadius */
210   myMajorRadius = 200.0 ;
211
212   /* min, max, step and decimals for spin boxes */
213   SpinBox_C1A4->RangeStepAndValidator( 0.00001, 999999.99999, step, 5 ) ;
214   SpinBox_C1A4->SetValue( 100.0 ) ;   /* = myMinorRadius */
215   myMinorRadius = 100.0 ;
216
217   GroupC1->show();
218   myConstructorId = 0 ;
219   Constructor1->setChecked( TRUE );
220   myEditCurrentArgument = LineEditC1A1 ;        
221   mySelection = Sel;
222   myGeomGUI = GeometryGUI::GetGeometryGUI() ;
223   myOkPoint = myOkDir = false ;
224   mySimulationTopoDs.Nullify() ;
225   myGeomGUI->SetActiveDialogBox( (QDialog*)this ) ;
226
227   // TODO : previous selection into argument ?
228
229   /* Filter definitions */
230   Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "GEOM");
231   myGeom = GEOM::GEOM_Gen::_narrow(comp);  
232   myEdgeFilter   = new GEOM_EdgeFilter( StdSelect_Line, myGeom );
233   myVertexFilter = new GEOM_ShapeTypeFilter( TopAbs_VERTEX, myGeom );
234
235   mySelection->AddFilter(myVertexFilter) ; /* first filter used */
236
237   /* signals and slots connections */
238   connect( buttonOk, SIGNAL( pressed() ),     this, SLOT( ClickOnOk() ) );
239   connect( buttonCancel, SIGNAL( pressed() ), this, SLOT( ClickOnCancel() ) ) ;
240   connect( buttonApply, SIGNAL( pressed() ),     this, SLOT( ClickOnApply() ) );
241   connect( GroupConstructors, SIGNAL(clicked(int) ), SLOT( ConstructorsClicked(int) ) );  
242   connect( SelectButtonC1A1, SIGNAL (pressed() ), this, SLOT( SetEditCurrentArgument() ) ) ;
243   connect( SelectButtonC1A2, SIGNAL (pressed() ), this, SLOT( SetEditCurrentArgument() ) ) ;
244
245   connect( LineEditC1A1, SIGNAL ( returnPressed() ), this, SLOT( LineEditReturnPressed() ) ) ;
246   connect( LineEditC1A2, SIGNAL ( returnPressed() ), this, SLOT( LineEditReturnPressed() ) ) ;
247   
248   connect( SpinBox_C1A3, SIGNAL ( valueChanged( double) ), this, SLOT( ValueChangedInSpinBox( double) ) ) ;
249   connect( SpinBox_C1A4, SIGNAL ( valueChanged( double) ), this, SLOT( ValueChangedInSpinBox( double) ) ) ;
250
251   connect( myGeomGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;  
252   connect( mySelection, SIGNAL( currentSelectionChanged() ),     this, SLOT( SelectionIntoArgument() ) );
253   /* to close dialog if study change */
254   connect( myGeomGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
255
256   /* Move widget on the botton right corner of main widget */
257   int x, y ;
258   myGeomGUI->DefineDlgPosition( this, x, y ) ;
259   this->move( x, y ) ;
260   this->show() ; /* displays Dialog */
261   
262   return ;
263 }
264
265
266 //=================================================================================
267 // function : ConstructorsClicked()
268 // purpose  : Radio button management
269 //=================================================================================
270 void GeometryGUI_EllipseDlg::ConstructorsClicked(int constructorId)
271 {
272   /* only a constructor now */
273   return ;
274 }
275
276 //=================================================================================
277 // function : ClickOnOk()
278 // purpose  :
279 //=================================================================================
280 void GeometryGUI_EllipseDlg::ClickOnOk()
281 {
282   this->ClickOnApply() ;
283   this->ClickOnCancel() ;
284
285   return ;
286 }
287
288 //=================================================================================
289 // function : ClickOnApply()
290 // purpose  :
291 //=================================================================================
292 void GeometryGUI_EllipseDlg::ClickOnApply()
293 {
294   myGeomGUI->EraseSimulationShape() ;
295   mySimulationTopoDs.Nullify() ;
296   switch(myConstructorId)
297     { 
298     case 0 :
299       { 
300         if(myOkPoint && myOkDir) {        
301           myGeomGUI->MakeEllipseAndDisplay( myPoint, myDir, myMajorRadius, myMinorRadius ) ; 
302         }
303         break ;
304       }
305     }
306   return ;
307 }
308
309 //=================================================================================
310 // function : ClickOnCancel()
311 // purpose  :
312 //=================================================================================
313 void GeometryGUI_EllipseDlg::ClickOnCancel()
314 {
315   mySelection->ClearFilters() ;
316   myGeomGUI->EraseSimulationShape() ;
317   mySimulationTopoDs.Nullify() ;
318   disconnect( mySelection, 0, this, 0 );
319   myGeomGUI->ResetState() ;
320   reject() ;
321   return ;
322 }
323
324 //=================================================================================
325 // function : SelectionIntoArgument()
326 // purpose  : Called when selection as changed or other case
327 //=================================================================================
328 void GeometryGUI_EllipseDlg::SelectionIntoArgument()
329 {
330
331   myGeomGUI->EraseSimulationShape() ; 
332   mySimulationTopoDs.Nullify() ;
333
334   /* Future name of selection */
335   QString aString = "";
336   
337   int nbSel = myGeomGUI->GetNameOfSelectedIObjects(mySelection, aString) ;
338   if ( nbSel != 1 ) {
339     if ( myEditCurrentArgument == LineEditC1A1 ) {
340       LineEditC1A1->setText("") ;
341       myOkPoint = false ;
342     }
343     else if ( myEditCurrentArgument == LineEditC1A2 ) { 
344       LineEditC1A2->setText("") ;
345       myOkDir = false ;
346     }
347     return ;
348   }
349
350   /* nbSel == 1 */
351   TopoDS_Shape S; 
352   if( !myGeomGUI->GetTopoFromSelection(mySelection, S) )
353     return ;
354  
355   /*  gp_Pnt : not used */
356   if ( myEditCurrentArgument == LineEditC1A1 && myGeomGUI->VertexToPoint(S, myPoint) ) {
357     LineEditC1A1->setText(aString) ;
358     myOkPoint = true ;
359   }    
360   else if ( myEditCurrentArgument == LineEditC1A2 /*&& myGeomGUI->LinearLocationAndDirection(S, notUsed, myDir) */) {
361     BRepAdaptor_Curve curv(TopoDS::Edge(S));
362     myDir = curv.Line().Direction();    
363     LineEditC1A2->setText(aString) ;
364     myOkDir = true ;
365   }
366
367   if( myOkPoint && myOkDir ) {
368     this->MakeEllipseSimulationAndDisplay() ;
369   }
370   return ;
371 }
372
373 //=================================================================================
374 // function : SetEditCurrentArgument()
375 // purpose  :
376 //=================================================================================
377 void GeometryGUI_EllipseDlg::SetEditCurrentArgument()
378 {
379   mySelection->ClearFilters() ;
380   QPushButton* send = (QPushButton*)sender();
381   switch (myConstructorId)
382     {
383     case 0: /* default constructor */
384       { 
385         if(send == SelectButtonC1A1) {
386           LineEditC1A1->setFocus() ;
387           myEditCurrentArgument = LineEditC1A1;
388           mySelection->AddFilter(myVertexFilter) ;
389         }
390         else if(send == SelectButtonC1A2) {
391           LineEditC1A2->setFocus() ;      
392           myEditCurrentArgument = LineEditC1A2;
393           mySelection->AddFilter(myEdgeFilter) ;
394         }       
395         SelectionIntoArgument() ;
396         break;
397       }
398     }
399   return ;
400 }
401
402 //=================================================================================
403 // function : LineEditReturnPressed()
404 // purpose  :
405 //=================================================================================
406 void GeometryGUI_EllipseDlg::LineEditReturnPressed()
407 {
408   QLineEdit* send = (QLineEdit*)sender();  
409   if( send == LineEditC1A1 )
410     myEditCurrentArgument = LineEditC1A1 ;
411   else if ( send == LineEditC1A2 )
412     myEditCurrentArgument = LineEditC1A2 ; 
413   else
414     return ;
415   
416   /* User name of object input management                          */
417   /* If successfull the selection is changed and signal emitted... */
418   /* so SelectionIntoArgument() is automatically called.           */
419   const QString objectUserName = myEditCurrentArgument->text() ;
420   QWidget* thisWidget = (QWidget*)this ;
421   if( myGeomGUI->SelectionByNameInDialogs( thisWidget, objectUserName, mySelection ) ) {
422     myEditCurrentArgument->setText( objectUserName ) ;
423   }
424   return ;
425 }
426
427
428 //=================================================================================
429 // function : ValueChangedInSpinBox()
430 // purpose  :
431 //=================================================================================
432 void GeometryGUI_EllipseDlg::ValueChangedInSpinBox( double newValue )
433 {  
434   myGeomGUI->EraseSimulationShape() ; 
435   mySimulationTopoDs.Nullify() ;
436
437   GeometryGUI_SpinBox* send = (GeometryGUI_SpinBox*)sender();
438   if(send == SpinBox_C1A3)
439     myMajorRadius = newValue ;
440   else if(send == SpinBox_C1A4)
441     myMinorRadius = newValue ;
442   else
443     return ;
444
445   if (myOkPoint && myOkDir) {
446     MakeEllipseSimulationAndDisplay() ;
447   }
448
449   return ;
450 }
451
452
453 //=================================================================================
454 // function : DeactivateActiveDialog()
455 // purpose  :
456 //=================================================================================
457 void GeometryGUI_EllipseDlg::DeactivateActiveDialog()
458 {
459   if ( GroupConstructors->isEnabled() ) {
460     GroupConstructors->setEnabled(false) ;
461     GroupC1->setEnabled(false) ;
462     GroupButtons->setEnabled(false) ;
463     disconnect( mySelection, 0, this, 0 );
464     myGeomGUI->EraseSimulationShape() ;
465     mySelection->ClearFilters() ;
466   }
467   return ;
468 }
469
470 //=================================================================================
471 // function : ActivateThisDialog()
472 // purpose  :
473 //=================================================================================
474 void GeometryGUI_EllipseDlg::ActivateThisDialog()
475 {
476   /* Emit a signal to deactivate the active dialog */
477   myGeomGUI->EmitSignalDeactivateDialog() ;   
478   GroupConstructors->setEnabled(true) ;
479   GroupC1->setEnabled(true) ;
480   GroupButtons->setEnabled(true) ;
481   connect ( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
482   if( !mySimulationTopoDs.IsNull() )
483     myGeomGUI->DisplaySimulationShape( mySimulationTopoDs ) ;
484   return ;
485 }
486
487
488 //=================================================================================
489 // function : enterEvent()
490 // purpose  :
491 //=================================================================================
492 void GeometryGUI_EllipseDlg::enterEvent(QEvent* e)
493 {
494   if ( GroupConstructors->isEnabled() )
495     return ;  
496   ActivateThisDialog() ;
497 }
498
499
500 //=================================================================================
501 // function : closeEvent()
502 // purpose  :
503 //=================================================================================
504 void GeometryGUI_EllipseDlg::closeEvent( QCloseEvent* e )
505 {
506   this->ClickOnCancel() ; /* same than click on cancel button */
507 }
508
509
510 //=================================================================================
511 // function : MakeEllipseSimulationAndDisplay()
512 // purpose  :
513 //=================================================================================
514 void GeometryGUI_EllipseDlg::MakeEllipseSimulationAndDisplay() 
515 {
516   myGeomGUI->EraseSimulationShape() ;
517   
518   if( myMajorRadius <  myMinorRadius )
519     return ;
520
521   try {
522     const gp_Ax2 axis( this->myPoint, this->myDir ) ;
523     gp_Elips ellipse( axis, this->myMajorRadius, this->myMinorRadius );
524
525     BRepBuilderAPI_MakeEdge MakeEdge( ellipse );
526     mySimulationTopoDs = MakeEdge.Shape();
527     myGeomGUI->DisplaySimulationShape( mySimulationTopoDs ) ;
528   }
529   catch(Standard_Failure) {
530     MESSAGE( "Exception catched in MakeEllipseSimulationAndDisplay" ) ;
531   }
532   return ;
533 }