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