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