Salome HOME
NRI : Merge from V1_2.
[modules/geom.git] / src / GEOMGUI / GeometryGUI_LineDlg.cxx
1 using namespace std;
2 //  File      : GeometryGUI_LineDlg.cxx
3 //  Created   :
4 //  Author    : Lucien PIGNOLONI
5 //  Project   : SALOME
6 //  Module    : GeometryGUI
7 //  Copyright : OPEN CASCADE
8 //  $Header$
9
10 #include "GeometryGUI_LineDlg.h"
11
12 #include "GeometryGUI.h"
13
14 #include <Precision.hxx>
15
16 #include "QAD_Application.h"
17 #include "QAD_Desktop.h"
18 #include "utilities.h"
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 <qpixmap.h>
32
33
34 //=================================================================================
35 // class    : GeometryGUI_LineDlg()
36 // purpose  : Constructs a GeometryGUI_LineDlg 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_LineDlg::GeometryGUI_LineDlg( 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
45     QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "GEOM",tr("ICON_DLG_LINE_2P")));
46     QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap( "GEOM",tr("ICON_SELECT")));
47     QPixmap image2(QAD_Desktop::getResourceManager()->loadPixmap( "GEOM",tr("ICON_DLG_LINE_PV")));
48     QPixmap image3(QAD_Desktop::getResourceManager()->loadPixmap( "GEOM",tr("ICON_DLG_LINE_EDGE")));
49
50     if ( !name )
51         setName( "GeometryGUI_LineDlg" );
52     resize( 303, 225 ); 
53     setCaption( tr( "GEOM_LINE_TITLE"  ) );
54     setSizeGripEnabled( TRUE );
55     GeometryGUI_LineDlgLayout = new QGridLayout( this ); 
56     GeometryGUI_LineDlgLayout->setSpacing( 6 );
57     GeometryGUI_LineDlgLayout->setMargin( 11 );
58
59     /***************************************************************/
60     GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
61     GroupConstructors->setTitle( tr( "GEOM_LINE"  ) );
62     GroupConstructors->setExclusive( TRUE );
63     GroupConstructors->setColumnLayout(0, Qt::Vertical );
64     GroupConstructors->layout()->setSpacing( 0 );
65     GroupConstructors->layout()->setMargin( 0 );
66     GroupConstructorsLayout = new QGridLayout( GroupConstructors->layout() );
67     GroupConstructorsLayout->setAlignment( Qt::AlignTop );
68     GroupConstructorsLayout->setSpacing( 6 );
69     GroupConstructorsLayout->setMargin( 11 );
70     Constructor1 = new QRadioButton( GroupConstructors, "Constructor1" );
71     Constructor1->setText( tr( ""  ) );
72     Constructor1->setPixmap( image0 );
73     Constructor1->setChecked( TRUE );
74     Constructor1->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, Constructor1->sizePolicy().hasHeightForWidth() ) );
75     Constructor1->setMinimumSize( QSize( 50, 0 ) );
76     GroupConstructorsLayout->addWidget( Constructor1, 0, 0 );
77     QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
78
79     QSpacerItem* spacer_3 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
80     GroupConstructorsLayout->addItem( spacer_3, 0, 5 );
81     GeometryGUI_LineDlgLayout->addWidget( GroupConstructors, 0, 0 );
82
83     /***************************************************************/
84     GroupC1 = new QGroupBox( this, "GroupC1" );
85     GroupC1->setTitle( tr( "GEOM_POINTS"  ) );
86     GroupC1->setMinimumSize( QSize( 0, 0 ) );
87     GroupC1->setFrameShape( QGroupBox::Box );
88     GroupC1->setFrameShadow( QGroupBox::Sunken );
89     GroupC1->setColumnLayout(0, Qt::Vertical );
90     GroupC1->layout()->setSpacing( 0 );
91     GroupC1->layout()->setMargin( 0 );
92     GroupC1Layout = new QGridLayout( GroupC1->layout() );
93     GroupC1Layout->setAlignment( Qt::AlignTop );
94     GroupC1Layout->setSpacing( 6 );
95     GroupC1Layout->setMargin( 11 );
96     SelectButtonC1A2 = new QPushButton( GroupC1, "SelectButtonC1A2" );
97     SelectButtonC1A2->setText( tr( ""  ) );
98     SelectButtonC1A2->setPixmap( image1 );
99     GroupC1Layout->addWidget( SelectButtonC1A2, 1, 1 );
100     LineEditC1A1 = new QLineEdit( GroupC1, "LineEditC1A1" );
101     GroupC1Layout->addWidget( LineEditC1A1, 0, 2 );
102     LineEditC1A2 = new QLineEdit( GroupC1, "LineEditC1A2" );
103     GroupC1Layout->addWidget( LineEditC1A2, 1, 2 );
104     SelectButtonC1A1 = new QPushButton( GroupC1, "SelectButtonC1A1" );
105     SelectButtonC1A1->setText( tr( ""  ) );
106     SelectButtonC1A1->setPixmap( image1 );
107     SelectButtonC1A1->setToggleButton( FALSE );
108     GroupC1Layout->addWidget( SelectButtonC1A1, 0, 1 );
109     TextLabelC1A1 = new QLabel( GroupC1, "TextLabelC1A1" );
110     TextLabelC1A1->setText( tr( "GEOM_POINT_I"  ).arg("1") );
111     TextLabelC1A1->setMinimumSize( QSize( 50, 0 ) );
112     TextLabelC1A1->setFrameShape( QLabel::NoFrame );
113     TextLabelC1A1->setFrameShadow( QLabel::Plain );
114     GroupC1Layout->addWidget( TextLabelC1A1, 0, 0 );
115     TextLabelC1A2 = new QLabel( GroupC1, "TextLabelC1A2" );
116     TextLabelC1A2->setText( tr( "GEOM_POINT_I"  ).arg("2") );
117     TextLabelC1A2->setMinimumSize( QSize( 50, 0 ) );
118     GroupC1Layout->addWidget( TextLabelC1A2, 1, 0 );
119     GeometryGUI_LineDlgLayout->addWidget( GroupC1, 1, 0 );
120
121     /***************************************************************/
122     GroupButtons = new QGroupBox( this, "GroupButtons" );
123     GroupButtons->setGeometry( QRect( 10, 10, 281, 48 ) ); 
124     GroupButtons->setTitle( tr( ""  ) );
125     GroupButtons->setColumnLayout(0, Qt::Vertical );
126     GroupButtons->layout()->setSpacing( 0 );
127     GroupButtons->layout()->setMargin( 0 );
128     GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
129     GroupButtonsLayout->setAlignment( Qt::AlignTop );
130     GroupButtonsLayout->setSpacing( 6 );
131     GroupButtonsLayout->setMargin( 11 );
132     buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
133     buttonCancel->setText( tr( "GEOM_BUT_CLOSE"  ) );
134     buttonCancel->setAutoDefault( TRUE );
135     GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
136     buttonApply = new QPushButton( GroupButtons, "buttonApply" );
137     buttonApply->setText( tr( "GEOM_BUT_APPLY"  ) );
138     buttonApply->setAutoDefault( TRUE );
139     GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
140     QSpacerItem* spacer_9 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
141     GroupButtonsLayout->addItem( spacer_9, 0, 2 );
142     buttonOk = new QPushButton( GroupButtons, "buttonOk" );
143     buttonOk->setText( tr( "GEOM_BUT_OK"  ) );
144     buttonOk->setAutoDefault( TRUE );
145     buttonOk->setDefault( TRUE );
146     GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
147     GeometryGUI_LineDlgLayout->addWidget( GroupButtons, 2, 0 );
148
149     Init(Sel) ; /* Initialisations */
150 }
151
152
153 //=================================================================================
154 // function : ~GeometryGUI_LineDlg()
155 // purpose  : Destroys the object and frees any allocated resources
156 //=================================================================================
157 GeometryGUI_LineDlg::~GeometryGUI_LineDlg()
158 {  
159   /* no need to delete child widgets, Qt does it all for us */
160 }
161
162
163 //=================================================================================
164 // function : Init()
165 // purpose  :
166 //=================================================================================
167 void GeometryGUI_LineDlg::Init( SALOME_Selection* Sel )
168 {
169
170   GroupC1->show();
171   // GroupC2->hide();
172   // GroupC3->hide();
173   myConstructorId = 0 ;
174   Constructor1->setChecked( TRUE );
175   myEditCurrentArgument = LineEditC1A1 ;        
176   mySelection = Sel;
177   myGeomGUI = GeometryGUI::GetGeometryGUI() ;
178   myPoint1.SetCoord( 0.0, 0.0, 0.0 );
179   myPoint2.SetCoord( 0.0, 0.0, 0.0 );
180   myOkPoint1 = myOkPoint2 = false ;
181   mySimulationTopoDs.Nullify() ;
182   myGeomGUI->SetActiveDialogBox( (QDialog*)this ) ;  
183
184   // TODO : previous selection into argument ?
185
186   /* Filters definition */
187   Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "GEOM");
188   myGeom = GEOM::GEOM_Gen::_narrow(comp);
189   myVertexFilter = new GEOM_ShapeTypeFilter( TopAbs_VERTEX, myGeom );
190   myEdgeFilter   = new GEOM_ShapeTypeFilter( TopAbs_EDGE, myGeom );
191   mySelection->AddFilter(myVertexFilter) ; /* first filter used */
192
193   /* signals and slots connections */
194   connect( buttonOk, SIGNAL( clicked() ),     this, SLOT( ClickOnOk() ) );
195   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
196   connect( buttonApply, SIGNAL( clicked() ), this, SLOT(ClickOnApply() ) );
197   connect( GroupConstructors, SIGNAL(clicked(int) ), SLOT( ConstructorsClicked(int) ) );  
198   connect( SelectButtonC1A1, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
199   connect( SelectButtonC1A2, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
200
201   connect( LineEditC1A1, SIGNAL ( returnPressed() ), this, SLOT( LineEditReturnPressed() ) ) ;
202   connect( LineEditC1A2, SIGNAL ( returnPressed() ), this, SLOT( LineEditReturnPressed() ) ) ;
203    
204   connect( myGeomGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;  
205   connect( mySelection, SIGNAL( currentSelectionChanged() ),     this, SLOT( SelectionIntoArgument() ) );
206   /* to close dialog if study change */
207   connect( myGeomGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
208  
209   /* Move widget on the botton right corner of main widget */
210   int x, y ;
211   myGeomGUI->DefineDlgPosition( this, x, y ) ;
212   this->move( x, y ) ;
213   this->show() ; /* displays Dialog */
214
215   return ;
216 }
217
218
219 //=================================================================================
220 // function : ConstructorsClicked()
221 // purpose  : Radio button management
222 //=================================================================================
223 void GeometryGUI_LineDlg::ConstructorsClicked(int constructorId)
224 {
225   myGeomGUI->EraseSimulationShape() ;
226
227   switch (constructorId)
228     {
229     case 0:
230       {
231         GroupC1->show();
232         //
233         //
234         myConstructorId = constructorId ;
235         myEditCurrentArgument = LineEditC1A1 ;  
236         Constructor1->setChecked( TRUE );
237         LineEditC1A1->setText(tr("")) ;
238         LineEditC1A2->setText(tr("")) ;
239         myOkPoint1 = myOkPoint2 = false ;
240         /* filter for next selections */
241         mySelection->ClearFilters() ;
242         mySelection->AddFilter( myVertexFilter );
243         connect ( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
244         break;
245       }
246     case 1:
247       {
248         break;
249       }
250     case 2:
251       {
252         break;
253       }
254     }
255  return ;
256 }
257
258 //=================================================================================
259 // function : ClickOnOk()
260 // purpose  :
261 //=================================================================================
262 void GeometryGUI_LineDlg::ClickOnOk()
263 {
264   this->ClickOnApply() ;
265   this->ClickOnCancel() ;
266
267   return ;
268 }
269
270 //=================================================================================
271 // function : ClickOnApply()
272 // purpose  :
273 //=================================================================================
274 void GeometryGUI_LineDlg::ClickOnApply()
275 {
276   myGeomGUI->EraseSimulationShape() ;
277   mySimulationTopoDs.Nullify() ;
278   switch(myConstructorId)
279     { 
280     case 0 :
281       { 
282         if(myOkPoint1 && myOkPoint2) 
283           myGeomGUI->MakeLineAndDisplay( myPoint1, myPoint2 ) ;
284         break ;
285       }
286     case 1 :
287       {
288         break ;
289       }
290     case 2 :
291       {
292         break ;
293       }  
294     }
295   // accept();
296   return ;
297 }
298
299
300 //=================================================================================
301 // function : ClickOnCancel()
302 // purpose  :
303 //=================================================================================
304 void GeometryGUI_LineDlg::ClickOnCancel()
305 {
306   mySelection->ClearFilters() ;
307   myGeomGUI->EraseSimulationShape() ;
308   mySimulationTopoDs.Nullify() ;
309   disconnect( mySelection, 0, this, 0 );
310   myGeomGUI->ResetState() ;
311   reject() ;
312   return ;
313 }
314
315 //=================================================================================
316 // function : LineEditReturnPressed()
317 // purpose  :
318 //=================================================================================
319 void GeometryGUI_LineDlg::LineEditReturnPressed()
320 {
321   QLineEdit* send = (QLineEdit*)sender() ;  
322   if( send == LineEditC1A1 )
323     myEditCurrentArgument = LineEditC1A1 ;
324   else if ( send == LineEditC1A2 )
325     myEditCurrentArgument = LineEditC1A2 ; 
326   else
327     return ;
328   
329   /* User name of object input management                          */
330   /* If successfull the selection is changed and signal emitted... */
331   /* so SelectionIntoArgument() is automatically called.           */
332   const QString objectUserName = myEditCurrentArgument->text() ;
333   QWidget* thisWidget = (QWidget*)this ;
334   if( myGeomGUI->SelectionByNameInDialogs( thisWidget, objectUserName, mySelection ) ) {
335     myEditCurrentArgument->setText( objectUserName ) ;
336   }
337   return ;
338 }
339
340
341 //=================================================================================
342 // function : SelectionIntoArgument()
343 // purpose  : Called when selection as changed or other case
344 //=================================================================================
345 void GeometryGUI_LineDlg::SelectionIntoArgument()
346 {
347
348   myGeomGUI->EraseSimulationShape() ; 
349   mySimulationTopoDs.Nullify() ;
350   QString aString = ""; /* name of selection */
351
352   int nbSel = myGeomGUI->GetNameOfSelectedIObjects(mySelection, aString) ;
353   if ( nbSel != 1 ) {
354     if ( myEditCurrentArgument == LineEditC1A1 ) {
355       myEditCurrentArgument->setText("") ;
356       myOkPoint1 = false ;
357     }
358     else if ( myEditCurrentArgument == LineEditC1A2 ) { 
359       myEditCurrentArgument->setText("") ;
360       myOkPoint2 = false ;
361     }
362     return ;
363   }
364
365   // nbSel == 1 !
366   TopoDS_Shape S; 
367   if( !myGeomGUI->GetTopoFromSelection(mySelection, S) )
368     return ;
369   
370   /* Constructor 1 treatment */
371   if ( myConstructorId == 0 && myEditCurrentArgument == LineEditC1A1 && myGeomGUI->VertexToPoint(S, myPoint1) ) {
372     myEditCurrentArgument->setText(aString) ;
373     myOkPoint1 = true ;
374   }
375   else if ( myConstructorId == 0 && myEditCurrentArgument == LineEditC1A2 && myGeomGUI->VertexToPoint(S, myPoint2)  ) {
376     myEditCurrentArgument->setText(aString) ;
377     myOkPoint2 = true ;
378   }
379   
380   if( myOkPoint1 && myOkPoint2 && myPoint1.Distance(myPoint2) > Precision::Confusion() ) {
381     mySimulationTopoDs = BRepBuilderAPI_MakeEdge( myPoint1, myPoint2 ).Shape();
382     /* Try to add an arrow at simulation shape */
383     bool notNeedToTest = this->AddArrowToSimulation(mySimulationTopoDs) ;
384     myGeomGUI->DisplaySimulationShape( mySimulationTopoDs ) ; 
385   }  
386   return ;
387 }
388
389 //=================================================================================
390 // function : SetEditCurrentArgument()
391 // purpose  :
392 //=================================================================================
393 void GeometryGUI_LineDlg::SetEditCurrentArgument()
394 {
395   QPushButton* send = (QPushButton*)sender();
396   mySelection->ClearFilters() ;
397   switch (myConstructorId)
398     {
399     case 0: /* default constructor */
400       { 
401         if(send == SelectButtonC1A1) {
402           LineEditC1A1->setFocus() ;
403           myEditCurrentArgument = LineEditC1A1;   
404         }
405         else if(send == SelectButtonC1A2) {
406           LineEditC1A2->setFocus() ;      
407           myEditCurrentArgument = LineEditC1A2;
408         }       
409         mySelection->AddFilter(myVertexFilter) ;
410         SelectionIntoArgument() ;
411         break;
412       }
413     }
414   return ;
415 }
416
417
418 //=================================================================================
419 // function : DeactivateActiveDialog()
420 // purpose  :
421 //=================================================================================
422 void GeometryGUI_LineDlg::DeactivateActiveDialog()
423 {
424   if ( GroupConstructors->isEnabled() ) {
425     GroupConstructors->setEnabled(false) ;
426     GroupC1->setEnabled(false) ;
427     GroupButtons->setEnabled(false) ;
428     disconnect( mySelection, 0, this, 0 );
429     myGeomGUI->EraseSimulationShape() ;
430     mySelection->ClearFilters() ;
431   }
432   return ;
433 }
434
435
436 //=================================================================================
437 // function : ActivateThisDialog()
438 // purpose  :
439 //=================================================================================
440 void GeometryGUI_LineDlg::ActivateThisDialog()
441 {
442   /* Emit a signal to deactivate the active dialog */
443   myGeomGUI->EmitSignalDeactivateDialog() ; 
444   GroupConstructors->setEnabled(true) ;
445   GroupC1->setEnabled(true) ;
446   GroupButtons->setEnabled(true) ;
447
448   connect ( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
449   if( !mySimulationTopoDs.IsNull() )
450     myGeomGUI->DisplaySimulationShape( mySimulationTopoDs ) ;
451
452   return ;
453 }
454
455
456 //=================================================================================
457 // function : enterEvent()
458 // purpose  :
459 //=================================================================================
460 void GeometryGUI_LineDlg::enterEvent(QEvent* e)
461 {
462   if ( GroupConstructors->isEnabled() )
463     return ;  
464   ActivateThisDialog() ;
465 }
466
467
468 //=================================================================================
469 // function : closeEvent()
470 // purpose  :
471 //=================================================================================
472 void GeometryGUI_LineDlg::closeEvent( QCloseEvent* e )
473 {
474   this->ClickOnCancel() ; /* same than click on cancel button */
475 }
476
477 //=================================================================================
478 // function : AddArrowToSimulation()
479 // purpose  : An arrow (cone topology) is added to 'modifiedShape'
480 //          : to simulate a vector or an 'oriented line' display. The result is in 'modifiedShape'.
481 //          : If an arrow can't be added returns false and 'modifiedShape' isn't modified !
482 //=================================================================================
483 bool GeometryGUI_LineDlg::AddArrowToSimulation( TopoDS_Shape& modifiedShape )
484 {
485   TopoDS_Shape arrow ;
486   /* Try to add a cone simulation shape to show direction of a linear edge */
487   if( myGeomGUI->CreateArrowForLinearEdge( modifiedShape, arrow ) ) {
488     TopoDS_Compound Comp ;
489     BRep_Builder B;
490     B.MakeCompound (Comp);
491     B.Add( Comp, modifiedShape ) ;
492     B.Add( Comp, arrow ) ;
493     modifiedShape = Comp ;
494     return true ;
495   }
496   return false ;
497 }
498