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