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