Salome HOME
sources v1.2
[modules/geom.git] / src / GEOMGUI / GeometryGUI_ArcDlg.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_ArcDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 using namespace std;
30 #include "GeometryGUI_ArcDlg.h"
31
32 #include "GeometryGUI.h"
33 #include "QAD_Application.h"
34 #include "QAD_Desktop.h"
35 #include "QAD_Tools.h"
36 #include "utilities.h"
37
38 #include <qbuttongroup.h>
39 #include <qgroupbox.h>
40 #include <qlabel.h>
41 #include <qlineedit.h>
42 #include <qpushbutton.h>
43 #include <qradiobutton.h>
44 #include <qlayout.h>
45 #include <qvariant.h>
46 #include <qtooltip.h>
47 #include <qwhatsthis.h>
48 #include <qimage.h>
49 #include <qpixmap.h>
50
51 //=================================================================================
52 // class    : GeometryGUI_ArcDlg()
53 // purpose  : Constructs a GeometryGUI_ArcDlg which is a child of 'parent', with the 
54 //            name 'name' and widget flags set to 'f'.
55 //            The dialog will by default be modeless, unless you set 'modal' to
56 //            TRUE to construct a modal dialog.
57 //=================================================================================
58 GeometryGUI_ArcDlg::GeometryGUI_ArcDlg( QWidget* parent,  const char* name, SALOME_Selection* Sel, bool modal, WFlags fl )
59     : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
60 {
61     QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "GeometryGUI",tr("ICON_DLG_ARC")));
62     QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap( "GeometryGUI",tr("ICON_SELECT")));
63
64     if ( !name )
65         setName( "GeometryGUI_ArcDlg" );
66     resize( 303, 251 ); 
67     setCaption( tr( "GEOM_ARC_TITLE"  ) );
68     setSizeGripEnabled( TRUE );
69     GeometryGUI_ArcDlgLayout = new QGridLayout( this ); 
70     GeometryGUI_ArcDlgLayout->setSpacing( 6 );
71     GeometryGUI_ArcDlgLayout->setMargin( 11 );
72
73     /***************************************************************/
74     GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
75     GroupConstructors->setTitle( tr( "GEOM_ARC"  ) );
76     GroupConstructors->setExclusive( TRUE );
77     GroupConstructors->setColumnLayout(0, Qt::Vertical );
78     GroupConstructors->layout()->setSpacing( 0 );
79     GroupConstructors->layout()->setMargin( 0 );
80     GroupConstructorsLayout = new QGridLayout( GroupConstructors->layout() );
81     GroupConstructorsLayout->setAlignment( Qt::AlignTop );
82     GroupConstructorsLayout->setSpacing( 6 );
83     GroupConstructorsLayout->setMargin( 11 );
84     Constructor1 = new QRadioButton( GroupConstructors, "Constructor1" );
85     Constructor1->setText( tr( ""  ) );
86     Constructor1->setPixmap( image0 );
87     Constructor1->setChecked( TRUE );
88     Constructor1->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, Constructor1->sizePolicy().hasHeightForWidth() ) );
89     Constructor1->setMinimumSize( QSize( 50, 0 ) );
90     GroupConstructorsLayout->addWidget( Constructor1, 0, 0 );
91     QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
92     GroupConstructorsLayout->addItem( spacer, 0, 1 );
93     GeometryGUI_ArcDlgLayout->addWidget( GroupConstructors, 0, 0 );
94
95     /***************************************************************/
96     GroupC1 = new QGroupBox( this, "GroupC1" );
97     GroupC1->setTitle( tr( "GEOM_POINTS"  ) );
98     GroupC1->setFrameShape( QGroupBox::Box );
99     GroupC1->setFrameShadow( QGroupBox::Sunken );
100     GroupC1->setColumnLayout(0, Qt::Vertical );
101     GroupC1->layout()->setSpacing( 0 );
102     GroupC1->layout()->setMargin( 0 );
103     GroupC1Layout = new QGridLayout( GroupC1->layout() );
104     GroupC1Layout->setAlignment( Qt::AlignTop );
105     GroupC1Layout->setSpacing( 6 );
106     GroupC1Layout->setMargin( 11 );
107     TextLabelC1A1 = new QLabel( GroupC1, "TextLabelC1A1" );
108     TextLabelC1A1->setText( tr( "GEOM_POINT_I"  ).arg( "1" ) );
109     TextLabelC1A1->setMinimumSize( QSize( 50, 0 ) );
110     TextLabelC1A1->setFrameShape( QLabel::NoFrame );
111     TextLabelC1A1->setFrameShadow( QLabel::Plain );
112     GroupC1Layout->addWidget( TextLabelC1A1, 0, 0 );
113     SelectButtonC1A1 = new QPushButton( GroupC1, "SelectButtonC1A1" );
114     SelectButtonC1A1->setText( tr( ""  ) );
115     SelectButtonC1A1->setPixmap( image1 );
116     SelectButtonC1A1->setToggleButton( FALSE );
117     GroupC1Layout->addWidget( SelectButtonC1A1, 0, 1 );
118     LineEditC1A1 = new QLineEdit( GroupC1, "LineEditC1A1" );
119     GroupC1Layout->addWidget( LineEditC1A1, 0, 2 );
120     TextLabelC1A2 = new QLabel( GroupC1, "TextLabelC1A2" );
121     TextLabelC1A2->setText( tr( "GEOM_POINT_I"  ).arg( "2" ) );
122     TextLabelC1A2->setMinimumSize( QSize( 50, 0 ) );
123     GroupC1Layout->addWidget( TextLabelC1A2, 1, 0 );
124     SelectButtonC1A2 = new QPushButton( GroupC1, "SelectButtonC1A2" );
125     SelectButtonC1A2->setText( tr( ""  ) );
126     SelectButtonC1A2->setPixmap( image1 );
127     GroupC1Layout->addWidget( SelectButtonC1A2, 1, 1 );
128     LineEditC1A2 = new QLineEdit( GroupC1, "LineEditC1A2" );
129     GroupC1Layout->addWidget( LineEditC1A2, 1, 2 );
130     TextLabelC1A3 = new QLabel( GroupC1, "TextLabelC1A3" );
131     TextLabelC1A3->setText( tr( "GEOM_POINT_I"  ).arg( "3" ) );
132     TextLabelC1A3->setMinimumSize( QSize( 50, 0 ) );
133     GroupC1Layout->addWidget( TextLabelC1A3, 2, 0 );
134     SelectButtonC1A3 = new QPushButton( GroupC1, "SelectButtonC1A3" );
135     SelectButtonC1A3->setText( tr( ""  ) );
136     SelectButtonC1A3->setPixmap( image1 );
137     GroupC1Layout->addWidget( SelectButtonC1A3, 2, 1 );
138     LineEditC1A3 = new QLineEdit( GroupC1, "LineEditC1A3" );
139     GroupC1Layout->addWidget( LineEditC1A3, 2, 2 );
140     GeometryGUI_ArcDlgLayout->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     buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
154     buttonCancel->setText( tr( "GEOM_BUT_CLOSE"  ) );
155     buttonCancel->setAutoDefault( TRUE );
156     GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
157     buttonApply = new QPushButton( GroupButtons, "buttonApply" );
158     buttonApply->setText( tr( "GEOM_BUT_APPLY"  ) );
159     buttonApply->setAutoDefault( TRUE );
160     GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
161     QSpacerItem* spacer_9 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
162     GroupButtonsLayout->addItem( spacer_9, 0, 2 );
163     buttonOk = new QPushButton( GroupButtons, "buttonOk" );
164     buttonOk->setText( tr( "GEOM_BUT_OK"  ) );
165     buttonOk->setAutoDefault( TRUE );
166     buttonOk->setDefault( TRUE );
167     GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
168     GeometryGUI_ArcDlgLayout->addWidget( GroupButtons, 2, 0 );
169     /***************************************************************/
170
171     Init(Sel) ; /* Initialisations */
172
173     /* Move widget on the botton right corner of main widget */
174     QAD_Tools::alignWidget(this, parent, AlignBottom | AlignRight);
175     /* Display  Dialog */
176     this->show() ; 
177 }
178
179
180 //=================================================================================
181 // function : ~GeometryGUI_ArcDlg()
182 // purpose  : Destroys the object and frees any allocated resources
183 //=================================================================================
184 GeometryGUI_ArcDlg::~GeometryGUI_ArcDlg()
185 {
186     // no need to delete child widgets, Qt does it all for us
187 }
188
189
190
191 //=================================================================================
192 // function : Init()
193 // purpose  :
194 //=================================================================================
195 void GeometryGUI_ArcDlg::Init( SALOME_Selection* Sel )
196 {
197
198   GroupC1->show();
199   myConstructorId = 0 ;
200   Constructor1->setChecked( TRUE );
201   myEditCurrentArgument = LineEditC1A1 ;        
202   mySelection = Sel;
203   myGeomGUI = GeometryGUI::GetGeometryGUI() ;
204   myOkPoint1 = myOkPoint2 = myOkPoint3 = false ;
205   mySimulationTopoDs.Nullify() ;
206   myGeomGUI->SetActiveDialogBox( (QDialog*)this ) ;
207
208   // TODO : previous selection into argument ?
209
210   /* Filter definitions */
211   Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "Geometry");
212   myGeom = GEOM::GEOM_Gen::_narrow(comp);
213   myVertexFilter = new GEOM_ShapeTypeFilter( TopAbs_VERTEX, myGeom );
214   mySelection->AddFilter(myVertexFilter) ;
215
216   /* signals and slots connections */
217   connect( buttonOk, SIGNAL( clicked() ),     this, SLOT( ClickOnOk() ) );
218   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
219   connect( buttonApply, SIGNAL( clicked() ),     this, SLOT( ClickOnApply() ) );
220   connect( GroupConstructors, SIGNAL(clicked(int) ), SLOT( ConstructorsClicked(int) ) );  
221   connect( SelectButtonC1A1, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
222   connect( SelectButtonC1A2, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
223   connect( SelectButtonC1A3, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
224   
225   connect( myGeomGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;  
226   connect( mySelection, SIGNAL( currentSelectionChanged() ),     this, SLOT( SelectionIntoArgument() ) );
227   /* to close dialog if study change */
228   connect( myGeomGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
229 }
230
231
232 //=================================================================================
233 // function : ConstructorsClicked()
234 // purpose  : Radio button management
235 //=================================================================================
236 void GeometryGUI_ArcDlg::ConstructorsClicked(int constructorId)
237 {
238   /* only a constructor now */
239   return ;
240 }
241
242 //=================================================================================
243 // function : ClickOnOk()
244 // purpose  :
245 //=================================================================================
246 void GeometryGUI_ArcDlg::ClickOnOk()
247 {
248   this->ClickOnApply() ;
249   this->ClickOnCancel() ;
250
251   return ;
252 }
253
254 //=================================================================================
255 // function : ClickOnApply()
256 // purpose  :
257 //=================================================================================
258 void GeometryGUI_ArcDlg::ClickOnApply()
259 {
260   myGeomGUI->EraseSimulationShape() ;
261   mySimulationTopoDs.Nullify() ;
262   myGeomGUI->GetDesktop()->putInfo( tr("") ) ; 
263   switch(myConstructorId)
264     { 
265     case 0 :
266       { 
267         if(myOkPoint1 && myOkPoint2 && myOkPoint3 ) {     
268           myGeomGUI->MakeArcAndDisplay( myPoint1, myPoint2, myPoint3 ) ; 
269         }
270         break ;
271       }
272     }
273   // accept();
274   return ;
275 }
276
277
278 //=================================================================================
279 // function : ClickOnCancel()
280 // purpose  :
281 //=================================================================================
282 void GeometryGUI_ArcDlg::ClickOnCancel()
283 {
284   mySelection->ClearFilters() ;
285   myGeomGUI->EraseSimulationShape() ;
286   mySimulationTopoDs.Nullify() ;
287   disconnect( mySelection, 0, this, 0 );
288   myGeomGUI->ResetState() ;
289   reject() ;
290   return ;
291 }
292
293
294 //=================================================================================
295 // function : SelectionIntoArgument()
296 // purpose  : Called when selection as changed or other case
297 //=================================================================================
298 void GeometryGUI_ArcDlg::SelectionIntoArgument()
299 {
300   myEditCurrentArgument->setText("") ;
301   myGeomGUI->EraseSimulationShape() ; 
302   mySimulationTopoDs.Nullify() ;
303  
304   QString aString = ""; /* name of future selection */
305  
306   int nbSel = myGeomGUI->GetNameOfSelectedIObjects(mySelection, aString) ;
307   if ( nbSel != 1 ) {
308     if ( myEditCurrentArgument == LineEditC1A1 ) {
309       myEditCurrentArgument->setText("") ;
310       myOkPoint1 = false ;
311     }
312     else if ( myEditCurrentArgument == LineEditC1A2 ) { 
313       myEditCurrentArgument->setText("") ;
314       myOkPoint2 = false ;
315     }
316     else if ( myEditCurrentArgument == LineEditC1A3 ) { 
317       myEditCurrentArgument->setText("") ;
318       myOkPoint3 = false ;
319     }
320     return ;
321   }
322   
323   // nbSel == 1 !
324   TopoDS_Shape S;  
325   if( !myGeomGUI->GetTopoFromSelection(mySelection, S) )
326     return ;
327   
328   if ( myEditCurrentArgument == LineEditC1A1 && myGeomGUI->VertexToPoint( S, this->myPoint1 ) ) {
329     myEditCurrentArgument->setText(aString) ;
330     myOkPoint1 = true ;
331    }
332   else if ( myEditCurrentArgument == LineEditC1A2 && myGeomGUI->VertexToPoint( S, this->myPoint2 ) ) {
333     myEditCurrentArgument->setText(aString) ;
334     myOkPoint2 = true ;
335   }  
336   else if ( myEditCurrentArgument == LineEditC1A3 && myGeomGUI->VertexToPoint( S, this->myPoint3 ) ) {
337     myEditCurrentArgument->setText(aString) ;
338     myOkPoint3 = true ;
339   }
340   
341   /* Simulation */
342   if( myOkPoint1 && myOkPoint2 && myOkPoint3 ) {
343     this->MakeArcSimulationAndDisplay() ;
344   }
345
346   return ;
347 }
348
349
350 //=================================================================================
351 // function : SetEditCurrentArgument()
352 // purpose  :
353 //=================================================================================
354 void GeometryGUI_ArcDlg::SetEditCurrentArgument()
355 {
356   QPushButton* send = (QPushButton*)sender();
357   switch (myConstructorId)
358     {
359     case 0: /* default constructor */
360       { 
361         mySelection->AddFilter(myVertexFilter) ;
362         if(send == SelectButtonC1A1) {
363           LineEditC1A1->setFocus() ;
364           myEditCurrentArgument = LineEditC1A1;
365         }
366         else if(send == SelectButtonC1A2) {
367           LineEditC1A2->setFocus() ;      
368           myEditCurrentArgument = LineEditC1A2;
369         }
370         else if(send == SelectButtonC1A3) {
371           LineEditC1A3->setFocus() ;      
372           myEditCurrentArgument = LineEditC1A3;
373         }
374         SelectionIntoArgument() ;
375         break;
376       }
377     }
378 }
379
380
381 //=================================================================================
382 // function : LineEditReturnPressed()
383 // purpose  :
384 //=================================================================================
385 void GeometryGUI_ArcDlg::LineEditReturnPressed()
386
387   QLineEdit* send = (QLineEdit*)sender();  
388   if( send == LineEditC1A1 )
389     myEditCurrentArgument = LineEditC1A1 ;
390   else if ( send == LineEditC1A2 )
391     myEditCurrentArgument = LineEditC1A2 ; 
392   else if ( send == LineEditC1A3 )
393     myEditCurrentArgument = LineEditC1A3 ; 
394   else
395     return ;
396   
397   /* User name of object input management                          */
398   /* If successfull the selection is changed and signal emitted... */
399   /* so SelectionIntoArgument() is automatically called.           */
400   const QString objectUserName = myEditCurrentArgument->text() ;
401   QWidget* thisWidget = (QWidget*)this ;
402   if( myGeomGUI->SelectionByNameInDialogs( thisWidget, objectUserName, mySelection ) ) {
403     myEditCurrentArgument->setText( objectUserName ) ;
404   }
405   return ;
406 }
407
408
409 //=================================================================================
410 // function : DeactivateActiveDialog()
411 // purpose  :
412 //=================================================================================
413 void GeometryGUI_ArcDlg::DeactivateActiveDialog()
414 {
415   if ( GroupConstructors->isEnabled() ) {
416     GroupConstructors->setEnabled(false) ;
417     GroupC1->setEnabled(false) ;
418     GroupButtons->setEnabled(false) ;
419     disconnect( mySelection, 0, this, 0 );
420     myGeomGUI->EraseSimulationShape() ;
421     mySelection->ClearFilters() ;
422   }
423   return ;
424 }
425
426
427 //=================================================================================
428 // function : ActivateThisDialog()
429 // purpose  :
430 //=================================================================================
431 void GeometryGUI_ArcDlg::ActivateThisDialog()
432 {
433   /* Emit a signal to deactivate the active dialog */
434   myGeomGUI->EmitSignalDeactivateDialog() ;   
435   GroupConstructors->setEnabled(true) ;
436   GroupC1->setEnabled(true) ;
437   GroupButtons->setEnabled(true) ;
438   connect ( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
439   if( !mySimulationTopoDs.IsNull() )
440     myGeomGUI->DisplaySimulationShape( mySimulationTopoDs ) ;
441   return ;
442 }
443
444
445 //=================================================================================
446 // function : enterEvent()
447 // purpose  :
448 //=================================================================================
449 void GeometryGUI_ArcDlg::enterEvent(QEvent* e)
450 {
451   if ( GroupConstructors->isEnabled() )
452     return ;  
453   ActivateThisDialog() ;
454   return ;
455 }
456
457
458
459 //=================================================================================
460 // function : closeEvent()
461 // purpose  :
462 //=================================================================================
463 void GeometryGUI_ArcDlg::closeEvent( QCloseEvent* e )
464 {
465   /* same than click on cancel button */
466   this->ClickOnCancel() ;
467   return ;
468 }
469
470
471 //=================================================================================
472 // function : MakeArcSimulationAndDisplay()
473 // purpose  :
474 //=================================================================================
475 void GeometryGUI_ArcDlg::MakeArcSimulationAndDisplay() 
476 {
477   myGeomGUI->EraseSimulationShape() ;
478   mySimulationTopoDs.Nullify() ;  
479   try {
480     if ( myPoint2.IsEqual( myPoint1, Precision::Confusion() ) ) {
481       myEditCurrentArgument->setText("") ;
482       return;
483     }
484     if ( myPoint2.IsEqual( myPoint3, Precision::Confusion() ) ) {
485       myEditCurrentArgument->setText("") ;
486       return;
487     }
488
489     gp_Vec v1( this->myPoint2, this->myPoint1 ) ;
490     gp_Vec v2( this->myPoint2, this->myPoint3 ) ;
491     if( v1.IsParallel(v2, Precision::Angular() ) ) {
492       myEditCurrentArgument->setText("") ;
493       return ;
494     }
495     
496     GC_MakeArcOfCircle Arc( this->myPoint1, this->myPoint2, this->myPoint3 );
497     if ( Arc.IsDone() ) {
498       Handle(Geom_TrimmedCurve) curve = Arc.Value() ;
499       mySimulationTopoDs = BRepBuilderAPI_MakeEdge(curve).Shape() ;
500       myGeomGUI->DisplaySimulationShape(mySimulationTopoDs) ;
501     }
502   }
503   catch(Standard_Failure) {
504     MESSAGE( "Exception catched in MakeArcSimulationAndDisplay" ) ;
505   }
506   return ;
507 }