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