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