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