Salome HOME
A small bug is fixed.
[modules/geom.git] / src / BasicGUI / BasicGUI_PointDlg.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   : BasicGUI_PointDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 using namespace std;
30 #include "BasicGUI_PointDlg.h"
31
32 #include "SUIT_Session.h"
33 #include "SalomeApp_Application.h"
34 #include "SalomeApp_SelectionMgr.h"
35
36 #include "GEOMImpl_Types.hxx"
37
38 #include <qlabel.h>
39
40 #include "utilities.h"
41
42 #include <gp_Pnt.hxx>
43 #include <TopoDS_Shape.hxx>
44 #include <TopoDS_Vertex.hxx>
45 #include <TopAbs_ShapeEnum.hxx>
46 #include <TopoDS.hxx>
47 #include <BRep_Tool.hxx>
48
49 #include <qapplication.h>
50
51 //=================================================================================
52 // class    : BasicGUI_PointDlg()
53 // purpose  : Constructs a BasicGUI_PointDlg 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 BasicGUI_PointDlg::BasicGUI_PointDlg(GeometryGUI* theGeometryGUI, QWidget* parent, const char* name, bool modal, WFlags fl)
59   :GEOMBase_Skeleton(parent, name, modal, fl ), myGeometryGUI(theGeometryGUI)
60 {
61   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_POINT")));
62   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_POINT_EDGE")));
63   QPixmap image2(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
64   QPixmap image3(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_POINT_REF")));
65
66   setCaption(tr("GEOM_POINT_TITLE"));
67
68   QGroupBox* aFrame = new QGroupBox( 1, Qt::Horizontal, this );
69   aFrame->setInsideMargin( 0 );
70   aFrame->setFrameStyle( QFrame::NoFrame );
71
72   /***************************************************************/
73   GroupConstructors->setTitle(tr("GEOM_POINTS"));
74   RadioButton1->setPixmap(image0);
75   RadioButton2->setPixmap(image3);
76   RadioButton3->setPixmap(image1);
77
78   GroupXYZ = new DlgRef_3Spin( aFrame, "GroupXYZ" );
79   GroupXYZ->GroupBox1->setTitle(tr("GEOM_COORDINATES"));
80   GroupXYZ->TextLabel1->setText(tr("GEOM_X"));
81   GroupXYZ->TextLabel2->setText(tr("GEOM_Y"));
82   GroupXYZ->TextLabel3->setText(tr("GEOM_Z"));
83
84   GroupOnCurve = new DlgRef_1Sel1Spin( aFrame, "GroupOnCurve" );
85   GroupOnCurve->GroupBox1->setTitle(tr("GEOM_PARAM_POINT"));
86   GroupOnCurve->TextLabel1->setText(tr("GEOM_EDGE"));
87   GroupOnCurve->TextLabel2->setText(tr("GEOM_PARAMETER"));
88   GroupOnCurve->PushButton1->setPixmap(image2);
89
90   GroupRefPoint = new DlgRef_1Sel3Spin( aFrame, "GoupRefPoint" );
91   GroupRefPoint->GroupBox1->setTitle(tr("GEOM_REF_POINT"));
92   GroupRefPoint->TextLabel1->setText(tr("GEOM_POINT"));
93   GroupRefPoint->PushButton1->setPixmap(image2);
94   GroupRefPoint->TextLabel2->setText(tr("GEOM_DX"));
95   GroupRefPoint->TextLabel3->setText(tr("GEOM_DY"));
96   GroupRefPoint->TextLabel4->setText(tr("GEOM_DZ"));
97
98   Layout1->addWidget( aFrame, 2, 0 );
99   /***************************************************************/
100
101   myCoordGrp = new QGroupBox( 2, Qt::Horizontal, tr( "GEOM_COORDINATES" ), aFrame );
102   new QLabel( tr( "GEOM_X" ), myCoordGrp );
103   myX = new QLineEdit( myCoordGrp );
104   new QLabel( tr( "GEOM_Y" ), myCoordGrp );
105   myY = new QLineEdit( myCoordGrp );
106   new QLabel( tr( "GEOM_Z" ), myCoordGrp );
107   myZ = new QLineEdit( myCoordGrp );
108
109   myX->setReadOnly( true );
110   myY->setReadOnly( true );
111   myZ->setReadOnly( true );
112
113   myX->setEnabled( false );
114   myY->setEnabled( false );
115   myZ->setEnabled( false );
116
117   QPalette aPal = myX->palette();
118   aPal.setColor( QPalette::Disabled, QColorGroup::Text, QColor( 0, 0, 0 ) ) ;
119   myX->setPalette( aPal );
120   myY->setPalette( aPal );
121   myZ->setPalette( aPal );
122  
123   Init();
124 }
125
126
127 //=======================================================================
128 // function : ~BasicGUI_PointDlg()
129 // purpose  : Destructor
130 //=======================================================================
131 BasicGUI_PointDlg::~BasicGUI_PointDlg()
132 {
133 }
134
135
136 //=================================================================================
137 // function : Init()
138 // purpose  :
139 //=================================================================================
140 void BasicGUI_PointDlg::Init()
141 {
142   GroupOnCurve->LineEdit1->setReadOnly( true );
143   GroupRefPoint->LineEdit1->setReadOnly( true );
144
145   myEdge = GEOM::GEOM_Object::_nil();
146   myRefPoint = GEOM::GEOM_Object::_nil();
147
148   myEditCurrentArgument = 0;
149
150   // myGeometryGUI->SetState( 0 );
151
152   /* Get setting of step value from file configuration */
153   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
154   double step = resMgr->doubleValue( "Geometry", "SettingsGeomStep", 100);
155   
156   /* min, max, step and decimals for spin boxes */
157   GroupXYZ->SpinBox_DX->RangeStepAndValidator(-999.999, 999.999, step, 3);
158   GroupXYZ->SpinBox_DY->RangeStepAndValidator(-999.999, 999.999, step, 3);
159   GroupXYZ->SpinBox_DZ->RangeStepAndValidator(-999.999, 999.999, step, 3);
160   GroupXYZ->SpinBox_DX->SetValue(0.0);
161   GroupXYZ->SpinBox_DY->SetValue(0.0);
162   GroupXYZ->SpinBox_DZ->SetValue(0.0);
163
164   GroupRefPoint->SpinBox_DX->RangeStepAndValidator(-999.999, 999.999, step, 3);
165   GroupRefPoint->SpinBox_DY->RangeStepAndValidator(-999.999, 999.999, step, 3);
166   GroupRefPoint->SpinBox_DZ->RangeStepAndValidator(-999.999, 999.999, step, 3);
167   GroupRefPoint->SpinBox_DX->SetValue(0.0);
168   GroupRefPoint->SpinBox_DY->SetValue(0.0);
169   GroupRefPoint->SpinBox_DZ->SetValue(0.0);
170
171   step = 0.1;
172   GroupOnCurve->SpinBox_DX->RangeStepAndValidator(0., 1., step, 3);
173   GroupOnCurve->SpinBox_DX->SetValue( 0.5 );
174
175   /* signals and slots connections */
176   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
177   connect(myGeometryGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
178   connect(myGeometryGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
179
180   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
181   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
182   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
183
184   connect(GroupOnCurve->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
185   connect(GroupOnCurve->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
186
187   connect(GroupOnCurve->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
188   connect(GroupXYZ->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
189   connect(GroupXYZ->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
190   connect(GroupXYZ->SpinBox_DZ, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
191   connect(GroupRefPoint->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
192   connect(GroupRefPoint->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
193   connect(GroupRefPoint->SpinBox_DZ, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
194
195   connect(myGeometryGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupOnCurve->SpinBox_DX, SLOT(SetStep(double)));
196   connect(myGeometryGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupXYZ->SpinBox_DX, SLOT(SetStep(double)));
197   connect(myGeometryGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupXYZ->SpinBox_DY, SLOT(SetStep(double)));
198   connect(myGeometryGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupXYZ->SpinBox_DZ, SLOT(SetStep(double)));
199   connect(myGeometryGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupRefPoint->SpinBox_DX, SLOT(SetStep(double)));
200   connect(myGeometryGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupRefPoint->SpinBox_DY, SLOT(SetStep(double)));
201   connect(myGeometryGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupRefPoint->SpinBox_DZ, SLOT(SetStep(double)));
202
203   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
204           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));  
205   
206   initName( tr("GEOM_VERTEX") );
207   
208   ConstructorsClicked( 0 );
209 }
210
211
212 //=================================================================================
213 // function : ConstructorsClicked()
214 // purpose  : Radio button management
215 //=================================================================================
216 void BasicGUI_PointDlg::ConstructorsClicked(int constructorId)
217 {  
218   switch ( constructorId )
219   {
220   case 0:
221     {
222       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
223
224       GroupRefPoint->hide();
225       GroupOnCurve->hide();
226
227       GroupXYZ->show();
228
229       myCoordGrp->hide();
230       break;
231     }
232   case 1:
233     {
234       myEditCurrentArgument = GroupRefPoint->LineEdit1;
235       myEditCurrentArgument->setText("");
236       myRefPoint = GEOM::GEOM_Object::_nil();
237
238       globalSelection( GEOM_POINT );
239
240       GroupXYZ->hide();
241       GroupOnCurve->hide();
242
243       GroupRefPoint->show();
244       
245       myCoordGrp->show();
246       break;
247     }
248   case 2:
249     {
250       myEditCurrentArgument = GroupOnCurve->LineEdit1;
251       myEditCurrentArgument->setText("");
252       myEdge = GEOM::GEOM_Object::_nil();
253
254       globalSelection( GEOM_EDGE );
255
256       GroupXYZ->hide();
257       GroupRefPoint->hide();
258
259       GroupOnCurve->show();
260       
261       myCoordGrp->show();
262       break;
263     }
264   }
265
266   myX->setText( "" );
267   myY->setText( "" );
268   myZ->setText( "" );
269
270   qApp->processEvents();
271   updateGeometry();
272   resize( minimumSize() );
273
274   displayPreview();
275 }
276
277
278 //=================================================================================
279 // function : ClickOnOk()
280 // purpose  :
281 //=================================================================================
282 void BasicGUI_PointDlg::ClickOnOk()
283 {
284   if ( onAccept() )
285     ClickOnCancel();
286 }
287
288
289 //=================================================================================
290 // function : ClickOnApply()
291 // purpose  :
292 //=================================================================================
293 bool BasicGUI_PointDlg::ClickOnApply()
294 {
295   if ( !onAccept() )
296     return false;
297   
298   initName();
299   ConstructorsClicked( getConstructorId() );
300   return true;
301 }
302
303
304 //=======================================================================
305 // function : ClickOnCancel()
306 // purpose  :
307 //=======================================================================
308 void BasicGUI_PointDlg::ClickOnCancel()
309 {
310   GEOMBase_Skeleton::ClickOnCancel();
311 }
312
313
314 //=================================================================================
315 // function : SelectionIntoArgument()
316 // purpose  : Called when selection as changed (for constructors not using local context)
317 //=================================================================================
318 void BasicGUI_PointDlg::SelectionIntoArgument()
319 {
320   const int id = getConstructorId();
321
322   if ( ( id == 1 || id == 2 ) && myEditCurrentArgument != 0 )
323     {
324       myEditCurrentArgument->setText("");
325       myX->setText( "" );
326       myY->setText( "" );
327       myZ->setText( "" );
328       myRefPoint = myEdge = GEOM::GEOM_Object::_nil();
329     }
330         
331   if ( IObjectCount() == 1 )
332     {
333       Standard_Boolean aRes = Standard_False;
334       GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), aRes );
335       if ( !CORBA::is_nil( aSelectedObject ) && aRes )
336         {
337           if ( id == 0 )
338             {
339               // get CORBA reference to data object
340               TopoDS_Shape aShape = myGeometryGUI->GetShapeReader().GetShape( myGeometryGUI->GetGeomGen(), aSelectedObject );
341               if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
342                 {
343                   gp_Pnt aPnt = BRep_Tool::Pnt( TopoDS::Vertex( aShape ) );
344                   GroupXYZ->SpinBox_DX->SetValue( aPnt.X() );
345                   GroupXYZ->SpinBox_DY->SetValue( aPnt.Y() );
346                   GroupXYZ->SpinBox_DZ->SetValue( aPnt.Z() );
347                 }
348             }
349           else if ( id == 1 )
350             {
351               myRefPoint = aSelectedObject;
352               GroupRefPoint->LineEdit1->setText( GEOMBase::GetName( aSelectedObject ) );
353             }
354           else if ( id == 2 )
355             {
356               myEdge = aSelectedObject;
357               GroupOnCurve->LineEdit1->setText( GEOMBase::GetName( aSelectedObject ) );
358             }
359         }
360     }
361   
362   displayPreview();
363 }
364
365
366 //=================================================================================
367 // function : LineEditReturnPressed()
368 // purpose  :
369 //=================================================================================
370 void BasicGUI_PointDlg::LineEditReturnPressed()
371 {
372   QLineEdit* send = (QLineEdit*)sender();
373   if ( send == GroupRefPoint->LineEdit1 || send == GroupOnCurve->LineEdit1 )
374     {
375       myEditCurrentArgument = send;
376       GEOMBase_Skeleton::LineEditReturnPressed();
377     }
378 }
379
380
381 //=================================================================================
382 // function : SetEditCurrentArgument()
383 // purpose  :
384 //=================================================================================
385 void BasicGUI_PointDlg::SetEditCurrentArgument()
386 {
387   QPushButton* send = (QPushButton*)sender();
388
389   if ( send == GroupRefPoint->PushButton1 )
390   {
391     GroupRefPoint->LineEdit1->setFocus();
392     myEditCurrentArgument = GroupRefPoint->LineEdit1;
393     
394     globalSelection( GEOM_POINT );
395   }
396   else if ( send == GroupOnCurve->PushButton1 )
397   {
398     GroupOnCurve->LineEdit1->setFocus();
399     myEditCurrentArgument = GroupOnCurve->LineEdit1;
400     
401     globalSelection( GEOM_EDGE );
402   }
403 }
404
405
406 //=================================================================================
407 // function : enterEvent()
408 // purpose  : to reactivate this dialog box when mouse enter onto the window
409 //=================================================================================
410 void BasicGUI_PointDlg::enterEvent(QEvent* e)
411 {
412   if ( !GroupConstructors->isEnabled() )
413     ActivateThisDialog();
414 }
415
416
417 //=================================================================================
418 // function : ActivateThisDialog()
419 // purpose  :
420 //=================================================================================
421 void BasicGUI_PointDlg::ActivateThisDialog( )
422 {
423   GEOMBase_Skeleton::ActivateThisDialog();
424   // myGeometryGUI->SetState( 0 );
425   ConstructorsClicked( getConstructorId() );
426 }
427
428
429 //=================================================================================
430 // function : DeactivateActiveDialog()
431 // purpose  : public slot to deactivate if active
432 //=================================================================================
433 void BasicGUI_PointDlg::DeactivateActiveDialog()
434 {
435   // myGeometryGUI->SetState( -1 );
436   GEOMBase_Skeleton::DeactivateActiveDialog();
437 }
438
439
440 //=================================================================================
441 // function : ValueChangedInSpinBox()
442 // purpose  :
443 //=================================================================================
444 void BasicGUI_PointDlg::ValueChangedInSpinBox(double newValue)
445 {
446   displayPreview();
447 }
448
449 //=======================================================================
450 // funcion  : getParameter()
451 // purpose  :
452 //=======================================================================
453 double BasicGUI_PointDlg::getParameter() const
454 {
455   return GroupOnCurve->SpinBox_DX->GetValue();
456 }
457
458 //=================================================================================
459 // function : OnPointSelected
460 // purpose  :
461 //=================================================================================
462 void BasicGUI_PointDlg::OnPointSelected( const gp_Pnt& thePnt )
463 {
464   if ( getConstructorId() == 0 )
465   {
466     GroupXYZ->SpinBox_DX->SetValue( thePnt.X() );
467     GroupXYZ->SpinBox_DY->SetValue( thePnt.Y() );
468     GroupXYZ->SpinBox_DZ->SetValue( thePnt.Z() );
469
470     displayPreview();
471   }
472 }
473
474 //=================================================================================
475 // function : createOperation
476 // purpose  :
477 //=================================================================================
478 GEOM::GEOM_IOperations_ptr BasicGUI_PointDlg::createOperation()
479 {
480   return myGeometryGUI->GetGeomGen()->GetIBasicOperations( getStudyId() );
481 }
482
483 //=================================================================================
484 // function : isValid
485 // purpose  :
486 //=================================================================================
487 bool BasicGUI_PointDlg::isValid( QString& msg )
488 {
489   const int id = getConstructorId();
490   if ( id == 0 )
491     return true;
492   else if ( id == 1 )
493     return !myRefPoint->_is_nil();
494   else if ( id == 2 )
495     return !myEdge->_is_nil();
496   return false;
497 }
498
499 //=================================================================================
500 // function : execute
501 // purpose  :
502 //=================================================================================
503 bool BasicGUI_PointDlg::execute( ObjectList& objects )
504 {
505   bool res = false;
506
507   GEOM::GEOM_Object_var anObj;
508
509   switch ( getConstructorId() )
510   {
511   case 0 :
512   {
513     double x = GroupXYZ->SpinBox_DX->GetValue();
514     double y = GroupXYZ->SpinBox_DY->GetValue();
515     double z = GroupXYZ->SpinBox_DZ->GetValue();
516       
517     anObj = GEOM::GEOM_IBasicOperations::_narrow( getOperation() )->MakePointXYZ( x,y,z );
518     res = true;
519     break;
520   }
521   case 1 :
522   {
523     double dx = GroupRefPoint->SpinBox_DX->GetValue();
524     double dy = GroupRefPoint->SpinBox_DY->GetValue();
525     double dz = GroupRefPoint->SpinBox_DZ->GetValue();
526   
527     anObj = GEOM::GEOM_IBasicOperations::_narrow( getOperation() )->MakePointWithReference( myRefPoint, dx, dy, dz );
528     res = true;
529     break;
530   }
531   case 2 :
532     anObj = GEOM::GEOM_IBasicOperations::_narrow( getOperation() )->MakePointOnCurve( myEdge, getParameter() );
533     res = true;
534     break;
535   }
536
537   if ( getConstructorId() == 1 || getConstructorId() == 2 )
538   {
539     TopoDS_Shape aShape;
540     if ( GEOMBase::GetShape( anObj, aShape ) && !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
541     {
542       gp_Pnt aPnt = BRep_Tool::Pnt( TopoDS::Vertex( aShape ) );
543       myX->setText( QString( "%1" ).arg( aPnt.X() ) );
544       myY->setText( QString( "%1" ).arg( aPnt.Y() ) );
545       myZ->setText( QString( "%1" ).arg( aPnt.Z() ) );
546     }
547     else
548     {
549       myX->setText( "" );
550       myY->setText( "" );
551       myZ->setText( "" );
552     }
553   }
554   
555   if ( !anObj->_is_nil() )
556   {
557     //printf( "--> a valid point is created\n" );
558     objects.push_back( anObj._retn() );
559   }
560
561   return res;
562 }
563
564 //=================================================================================
565 // function : closeEvent
566 // purpose  :
567 //=================================================================================
568 void BasicGUI_PointDlg::closeEvent( QCloseEvent* e )
569 {
570   // myGeometryGUI->SetState( -1 );
571   GEOMBase_Skeleton::closeEvent( e );
572 }