Salome HOME
DCQ : Merge with Ecole_Ete_a6.
[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 "OCCViewer_Viewer3d.h"
34
35 #include <BRepBuilderAPI_MakeVertex.hxx>
36 #include <BRep_Tool.hxx>
37 #include <Geom_Curve.hxx>
38 #if OCC_VERSION_MAJOR >= 5
39 #include <BRepAlgo.hxx>
40 #else
41 #include <BRepAlgoAPI.hxx>
42 #endif
43
44 //=================================================================================
45 // class    : BasicGUI_PointDlg()
46 // purpose  : Constructs a BasicGUI_PointDlg which is a child of 'parent', with the 
47 //            name 'name' and widget flags set to 'f'.
48 //            The dialog will by default be modeless, unless you set 'modal' to
49 //            TRUE to construct a modal dialog.
50 //=================================================================================
51 BasicGUI_PointDlg::BasicGUI_PointDlg(QWidget* parent, const char* name, BasicGUI* theBasicGUI, SALOME_Selection* Sel, const Handle (AIS_InteractiveContext)& ic, bool modal, WFlags fl)
52   :GEOMBase_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
53 {
54   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_POINT")));
55   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_POINT_EDGE")));
56   QPixmap image2(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
57
58   setCaption(tr("GEOM_POINT_TITLE"));
59
60   /***************************************************************/
61   GroupConstructors->setTitle(tr("GEOM_POINTS"));
62   RadioButton1->setPixmap(image0);
63   RadioButton2->setPixmap(image1);
64   RadioButton3->close(TRUE);
65
66   GroupPoints = new DlgRef_3Spin(this, "GroupPoints");
67   GroupPoints->GroupBox1->setTitle(tr("GEOM_COORDINATES"));
68   GroupPoints->TextLabel1->setText(tr("GEOM_X"));
69   GroupPoints->TextLabel2->setText(tr("GEOM_Y"));
70   GroupPoints->TextLabel3->setText(tr("GEOM_Z"));
71
72   GroupDimensions = new DlgRef_1Sel1Spin(this, "GroupDimensions");
73   GroupDimensions->GroupBox1->setTitle(tr("GEOM_PARAM_POINT"));
74   GroupDimensions->TextLabel1->setText(tr("GEOM_EDGE"));
75   GroupDimensions->TextLabel2->setText(tr("GEOM_PARAMETER"));
76   GroupDimensions->PushButton1->setPixmap(image2);
77
78   Layout1->addWidget(GroupPoints, 1, 0);
79   Layout1->addWidget(GroupDimensions, 1, 0);
80   /***************************************************************/
81
82   /* Initialisations */
83   myBasicGUI = theBasicGUI;
84   Init(ic);
85 }
86
87
88 //=======================================================================
89 // function : ~BasicGUI_PointDlg()
90 // purpose  : Destructor
91 //=======================================================================
92 BasicGUI_PointDlg::~BasicGUI_PointDlg()
93 {
94   /* no need to delete child widgets, Qt does it all for us */
95 }
96
97
98 //=================================================================================
99 // function : Init()
100 // purpose  :
101 //=================================================================================
102 void BasicGUI_PointDlg::Init(const Handle(AIS_InteractiveContext)& ic)
103 {
104   /* init variables */
105   myConstructorId = 0;
106   myEditCurrentArgument = GroupDimensions->LineEdit1;
107
108   myIC = ic;
109   myParameter = 0.50;
110   myPoint.SetCoord(0.0, 0.0, 0.0);
111   myOkEdge = false;
112
113   myGeomGUI->myState = 0;
114
115   /*  Vertices Filter for all arguments */
116   myEdgeFilter = new GEOM_ShapeTypeFilter(TopAbs_EDGE, myGeom);
117
118   /* Get setting of step value from file configuration */
119   QString St = QAD_CONFIG->getSetting("Geometry:SettingsGeomStep");
120   step = St.toDouble();
121
122   /* min, max, step and decimals for spin boxes */
123   double specificStep = 0.1;
124   GroupDimensions->SpinBox_DX->RangeStepAndValidator(-999999.99999, 999999.99999, specificStep, 5);
125   GroupPoints->SpinBox_DX->RangeStepAndValidator(-999.999, 999.999, step, 3);
126   GroupPoints->SpinBox_DY->RangeStepAndValidator(-999.999, 999.999, step, 3);
127   GroupPoints->SpinBox_DZ->RangeStepAndValidator(-999.999, 999.999, step, 3);
128   GroupDimensions->SpinBox_DX->SetValue(myParameter);
129   GroupPoints->SpinBox_DX->SetValue(0.0);
130   GroupPoints->SpinBox_DY->SetValue(0.0);
131   GroupPoints->SpinBox_DZ->SetValue(0.0);
132
133   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
134     myLocalContextId = myIC->OpenLocalContext();
135     //myGeomBase->SetDisplayedObjectList();
136     /* sub shapes selection */
137     myLocalContextMode = TopAbs_VERTEX;
138     myIC->ActivateStandardMode(myLocalContextMode);
139     myUseLocalContext = true;
140   } 
141   else
142     myUseLocalContext = false;
143
144   TopoDS_Shape S;
145   bool displayPoint = true;
146   if(myGeomBase->GetTopoFromSelection(mySelection, S)) {
147     /* Filter a possibly previous selection and try to put it into coordinates */
148     if(myGeomBase->VertexToPoint(S, myPoint))
149       displayPoint = false;
150   }
151   this->PointIntoCoordinates(myPoint, displayPoint);
152     
153   /* signals and slots connections */
154   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
155   connect(myGeomGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
156   connect(myGeomGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
157
158   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
159   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
160   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
161
162   connect(GroupDimensions->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
163   connect(GroupDimensions->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
164
165   connect(GroupDimensions->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
166   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
167   connect(GroupPoints->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
168   connect(GroupPoints->SpinBox_DZ, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
169
170   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DX, SLOT(SetStep(double)));
171   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DX, SLOT(SetStep(double)));
172   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DY, SLOT(SetStep(double)));
173   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DZ, SLOT(SetStep(double)));
174
175   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
176
177   /* displays Dialog */
178   GroupDimensions->hide();
179   GroupPoints->show();
180   this->show();
181
182   return;
183 }
184
185
186 //=================================================================================
187 // function : ConstructorsClicked()
188 // purpose  : Radio button management
189 //=================================================================================
190 void BasicGUI_PointDlg::ConstructorsClicked(int constructorId)
191 {
192   myConstructorId = constructorId;
193   mySelection->ClearFilters();
194   myGeomBase->EraseSimulationShape();
195   mySimulationTopoDs.Nullify();
196   disconnect(mySelection, 0, this, 0);
197
198   switch (constructorId)
199     {
200     case 0:
201       {
202         if(myUseLocalContext == false && QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
203           myLocalContextId = myIC->OpenLocalContext();
204           //myGeomBase->SetDisplayedObjectList();
205           /* sub shapes selection */
206           myLocalContextMode = TopAbs_VERTEX;
207           myIC->ActivateStandardMode(myLocalContextMode);
208           myUseLocalContext = true;
209         }
210
211         GroupDimensions->hide();
212         resize(0, 0);
213         GroupPoints->show();
214
215         /* Display point simulation */
216         PointIntoCoordinates(myPoint, true);
217         break;
218       }
219     case 1:
220       {
221         if(myUseLocalContext == true && QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
222           myIC->CloseLocalContext(myLocalContextId);
223           myUseLocalContext = false;
224         }
225
226         GroupPoints->hide();
227         resize(0, 0);
228         GroupDimensions->show();
229
230         myEditCurrentArgument = GroupDimensions->LineEdit1;
231         GroupDimensions->LineEdit1->setText("");
232         myOkEdge = false;
233
234         /* filter for next selections */
235         mySelection->AddFilter(myEdgeFilter);
236         connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
237         break;
238       }
239     }
240  return;
241 }
242
243
244 //=================================================================================
245 // function : ClickOnOk()
246 // purpose  :
247 //=================================================================================
248 void BasicGUI_PointDlg::ClickOnOk()
249 {
250   this->ClickOnApply();
251   this->ClickOnCancel();
252   return;
253 }
254
255
256 //=================================================================================
257 // function : ClickOnApply()
258 // purpose  :
259 //=================================================================================
260 void BasicGUI_PointDlg::ClickOnApply()
261 {
262   buttonApply->setFocus();
263   QAD_Application::getDesktop()->putInfo(tr(""));
264   if(mySimulationTopoDs.IsNull())
265     return;
266   myGeomBase->EraseSimulationShape();
267   mySimulationTopoDs.Nullify();
268
269   /* Close local context */
270   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
271     myIC->CloseLocalContext(myLocalContextId);
272     myUseLocalContext = false;
273   }
274
275   switch(myConstructorId) 
276     {
277     case 0 :
278       {
279         /* Recup args and call method */
280         double x = GroupPoints->SpinBox_DX->GetValue();
281         double y = GroupPoints->SpinBox_DY->GetValue();
282         double z = GroupPoints->SpinBox_DZ->GetValue();
283       
284         myBasicGUI->MakePointAndDisplay(x,y,z);  /* WARNING : no display if a local context is opened */
285         if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
286           /* no display if a local context is opened */
287           myLocalContextId = myIC->OpenLocalContext();
288           //myGeomBase->SetDisplayedObjectList();
289           /* sub shapes selection */
290           myLocalContextMode = TopAbs_VERTEX;
291           myIC->ActivateStandardMode(myLocalContextMode);
292           myUseLocalContext = true;
293         }
294         break;
295       }
296     case 1 :
297       {
298         if(myOkEdge == true) {
299           /* this constructor method has no idl interface : we use same than constructor 0 */
300           myBasicGUI->MakePointAndDisplay(myPoint.X(), myPoint.Y(), myPoint.Z());
301         }
302         break;
303       }
304     }
305   return;
306 }
307
308
309 //=======================================================================
310 // function : ClickOnCancel()
311 // purpose  :
312 //=======================================================================
313 void BasicGUI_PointDlg::ClickOnCancel()
314 {
315   if(myConstructorId == 0 && QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
316     OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
317     myIC = v3d->getAISContext();
318     if(myIC->HasOpenedContext()) {
319       myIC->CloseLocalContext(myLocalContextId);
320       myUseLocalContext = false;
321     }
322   }
323
324   myGeomGUI->myState = -1;
325   GEOMBase_Skeleton::ClickOnCancel();
326   return;
327 }
328
329
330 //=================================================================================
331 // function : SelectionIntoArgument()
332 // purpose  : Called when selection as changed (for constructors not using local context)
333 //=================================================================================
334 void BasicGUI_PointDlg::SelectionIntoArgument()
335 {
336   myGeomBase->EraseSimulationShape();
337   mySimulationTopoDs.Nullify();
338   myEditCurrentArgument->setText("");
339   QString aString = ""; /* name of selection */
340
341   int nbSel = myGeomBase->GetNameOfSelectedIObjects(mySelection, aString);
342   if(nbSel != 1) {
343     myOkEdge = false;
344     return;
345   }
346
347   // nbSel == 1
348   TopoDS_Shape S; 
349   if(!myGeomBase->GetTopoFromSelection(mySelection, S))
350     return;
351
352   if(S.ShapeType() == TopAbs_EDGE) {
353     if(CalculateVertexOnCurve(TopoDS::Edge(S), myParameter, mySimulationTopoDs)) {
354       if(myGeomBase->VertexToPoint(mySimulationTopoDs, myPoint)) {
355         GroupDimensions->LineEdit1->setText(aString);
356         myOkEdge = true;
357         myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
358       }
359     }
360   }
361   return;
362 }
363
364
365 //=================================================================================
366 // function : LineEditReturnPressed()
367 // purpose  :
368 //=================================================================================
369 void BasicGUI_PointDlg::LineEditReturnPressed()
370 {
371   QLineEdit* send = (QLineEdit*)sender();
372   if(send == GroupDimensions->LineEdit1)
373     myEditCurrentArgument = GroupDimensions->LineEdit1;
374   else
375     return;
376
377   GEOMBase_Skeleton::LineEditReturnPressed();
378   return;
379 }
380
381
382 //=================================================================================
383 // function : SetEditCurrentArgument()
384 // purpose  :
385 //=================================================================================
386 void BasicGUI_PointDlg::SetEditCurrentArgument()
387 {
388   QPushButton* send = (QPushButton*)sender();
389
390   if(send == GroupDimensions->PushButton1) {
391     GroupDimensions->LineEdit1->setFocus();
392     myEditCurrentArgument = GroupDimensions->LineEdit1;
393     mySelection->AddFilter(myEdgeFilter);
394     this->SelectionIntoArgument();
395   }
396   return;
397 }
398
399
400 //=================================================================================
401 // function : enterEvent()
402 // purpose  : to reactivate this dialog box when mouse enter onto the window
403 //=================================================================================
404 void BasicGUI_PointDlg::enterEvent(QEvent* e)
405 {
406   if (GroupConstructors->isEnabled())
407     return;
408   this->ActivateThisDialog();
409   return;
410 }
411
412
413 //=======================================================================
414 // function : closeEvent()
415 // purpose  :
416 //=======================================================================
417 void BasicGUI_PointDlg::closeEvent(QCloseEvent* e)
418 {
419   /* same than click on cancel button */
420   this->ClickOnCancel();
421 }
422
423
424 //=================================================================================
425 // function : ActivateThisDialog()
426 // purpose  :
427 //=================================================================================
428 void BasicGUI_PointDlg::ActivateThisDialog( )
429 {
430   GEOMBase_Skeleton::ActivateThisDialog();
431   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
432
433   myGeomGUI->myState = 0;
434   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
435     OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
436     myIC = v3d->getAISContext();
437     myLocalContextId = myIC->OpenLocalContext();
438     //myGeomBase->SetDisplayedObjectList();
439     /* sub shapes selection */
440     myLocalContextMode = TopAbs_VERTEX;
441     myIC->ActivateStandardMode(myLocalContextMode);
442     myUseLocalContext = true;
443   }
444
445   if(myConstructorId == 1)
446     mySelection->AddFilter(myEdgeFilter);
447
448   if(!mySimulationTopoDs.IsNull())
449     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
450   return;
451 }
452
453
454 //=================================================================================
455 // function : DeactivateActiveDialog()
456 // purpose  : public slot to deactivate if active
457 //=================================================================================
458 void BasicGUI_PointDlg::DeactivateActiveDialog()
459 {
460   if(myConstructorId == 0 && QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
461     myIC->CloseLocalContext(myLocalContextId);
462     myUseLocalContext = false;
463   }
464   myGeomGUI->myState = -1;
465   GEOMBase_Skeleton::DeactivateActiveDialog();
466   return;
467 }
468
469
470 //=================================================================================
471 // function : ValueChangedInSpinBox()
472 // purpose  :
473 //=================================================================================
474 void BasicGUI_PointDlg::ValueChangedInSpinBox(double newValue)
475 {
476   DlgRef_SpinBox* send = (DlgRef_SpinBox*)sender();
477   double vx, vy, vz;
478
479   if(send == GroupPoints->SpinBox_DX) {
480     vx = newValue;
481     vy = GroupPoints->SpinBox_DY->GetValue();
482     vz = GroupPoints->SpinBox_DZ->GetValue();
483   } else if(send == GroupPoints->SpinBox_DY) {
484     vx = GroupPoints->SpinBox_DX->GetValue();
485     vy = newValue;
486     vz = GroupPoints->SpinBox_DZ->GetValue();
487   } else if(send == GroupPoints->SpinBox_DZ) {
488     vx = GroupPoints->SpinBox_DX->GetValue();
489     vy = GroupPoints->SpinBox_DY->GetValue();
490     vz = newValue;
491   }  else if(send == GroupDimensions->SpinBox_DX) {
492     myParameter = newValue;
493   } else
494     return;
495
496   switch(myConstructorId) 
497     {
498     case 0: // default constructor 
499       {
500         myPoint.SetCoord(vx, vy, vz);
501         mySimulationTopoDs = BRepBuilderAPI_MakeVertex (myPoint).Shape();
502         myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
503         break;
504       }
505     case 1: 
506       {
507         this->SelectionIntoArgument();  
508         break;
509       }
510     }
511   return;
512 }
513
514
515 //=================================================================================
516 // function : CalculateVertexOnCurve()
517 // purpose  : Calculate a Vertex on the curve given by 'anEdge'.
518 //          : The position of resultVertex is given by aParameter.
519 //          : For a linear edge, aParameter=0.0 gives the first vertex of edge
520 //          :                    aParameter=1.0 gives the last vertex of edge
521 //          :                    aParameter=0.5 gives the vertex on the middle of edge
522 //          : It is possible to get vertices out of edge using values > 1.0 or < 0.0
523 //=================================================================================
524 bool BasicGUI_PointDlg::CalculateVertexOnCurve(const TopoDS_Edge& anEdge, const Standard_Real aParameter, TopoDS_Shape& resultVertex) 
525 {
526 #if OCC_VERSION_MAJOR >= 5
527   if(anEdge.IsNull() || !BRepAlgo::IsValid(anEdge))
528 #else
529   if(anEdge.IsNull() || !BRepAlgoAPI::IsValid(anEdge))
530 #endif
531     return false;
532
533   Standard_Real first, last;
534   Handle(Geom_Curve) curv = BRep_Tool::Curve(anEdge, first, last);
535   if(!curv->IsCN(0))
536     return false;
537   
538   Standard_Real param;
539   if(anEdge.Orientation() == TopAbs_FORWARD)
540     param = first + (last-first) * aParameter;
541   else
542     param = last + (first-last) * aParameter;
543
544   gp_Pnt paramPoint;
545   curv->D0(param, paramPoint);
546   resultVertex = BRepBuilderAPI_MakeVertex(paramPoint);
547   return true;
548 }
549
550
551 //=======================================================================
552 // funcion  : PointIntoCoordinates()
553 // purpose  : Sets user point coordinates into this dialog Spin boxes
554 //          : and displays it or not according to 'bool displayPoint'
555 //=======================================================================
556 void BasicGUI_PointDlg::PointIntoCoordinates(gp_Pnt P, bool displayPoint)
557 {
558   GroupPoints->SpinBox_DX->SetValue(P.X());
559   GroupPoints->SpinBox_DY->SetValue(P.Y());
560   GroupPoints->SpinBox_DZ->SetValue(P.Z());
561   myPoint.SetCoord(P.X(), P.Y(), P.Z());
562   if(displayPoint) {
563     mySimulationTopoDs = BRepBuilderAPI_MakeVertex(P).Shape();
564     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
565   }
566   return;
567 }