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