]> SALOME platform Git repositories - modules/geom.git/blob - src/BasicGUI/BasicGUI_PointDlg.cxx
Salome HOME
DCQ : New Archi
[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   QAD_Application::getDesktop()->putInfo(tr(""));
263   if(mySimulationTopoDs.IsNull())
264     return;
265   myGeomBase->EraseSimulationShape();
266   mySimulationTopoDs.Nullify();
267
268   /* Close local context */
269   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
270     myIC->CloseLocalContext(myLocalContextId);
271     myUseLocalContext = false;
272   }
273
274   switch(myConstructorId) 
275     {
276     case 0 :
277       {
278         /* Recup args and call method */
279         double x = GroupPoints->SpinBox_DX->GetValue();
280         double y = GroupPoints->SpinBox_DY->GetValue();
281         double z = GroupPoints->SpinBox_DZ->GetValue();
282       
283         myBasicGUI->MakePointAndDisplay(x,y,z);  /* WARNING : no display if a local context is opened */
284         if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
285           /* no display if a local context is opened */
286           myLocalContextId = myIC->OpenLocalContext();
287           //myGeomBase->SetDisplayedObjectList();
288           /* sub shapes selection */
289           myLocalContextMode = TopAbs_VERTEX;
290           myIC->ActivateStandardMode(myLocalContextMode);
291           myUseLocalContext = true;
292         }
293         break;
294       }
295     case 1 :
296       {
297         if(myOkEdge == true) {
298           /* this constructor method has no idl interface : we use same than constructor 0 */
299           myBasicGUI->MakePointAndDisplay(myPoint.X(), myPoint.Y(), myPoint.Z());
300         }
301         break;
302       }
303     }
304   return;
305 }
306
307
308 //=======================================================================
309 // function : ClickOnCancel()
310 // purpose  :
311 //=======================================================================
312 void BasicGUI_PointDlg::ClickOnCancel()
313 {
314   if(myConstructorId == 0 && QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
315     OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
316     myIC = v3d->getAISContext();
317     if(myIC->HasOpenedContext()) {
318       myIC->CloseLocalContext(myLocalContextId);
319       myUseLocalContext = false;
320     }
321   }
322
323   myGeomGUI->myState = -1;
324   GEOMBase_Skeleton::ClickOnCancel();
325   return;
326 }
327
328
329 //=================================================================================
330 // function : SelectionIntoArgument()
331 // purpose  : Called when selection as changed (for constructors not using local context)
332 //=================================================================================
333 void BasicGUI_PointDlg::SelectionIntoArgument()
334 {
335   myGeomBase->EraseSimulationShape();
336   mySimulationTopoDs.Nullify();
337   myEditCurrentArgument->setText("");
338   QString aString = ""; /* name of selection */
339
340   int nbSel = myGeomBase->GetNameOfSelectedIObjects(mySelection, aString);
341   if(nbSel != 1) {
342     myOkEdge = false;
343     return;
344   }
345
346   // nbSel == 1
347   TopoDS_Shape S; 
348   if(!myGeomBase->GetTopoFromSelection(mySelection, S))
349     return;
350
351   if(S.ShapeType() == TopAbs_EDGE) {
352     if(CalculateVertexOnCurve(TopoDS::Edge(S), myParameter, mySimulationTopoDs)) {
353       if(myGeomBase->VertexToPoint(mySimulationTopoDs, myPoint)) {
354         GroupDimensions->LineEdit1->setText(aString);
355         myOkEdge = true;
356         myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
357       }
358     }
359   }
360   return;
361 }
362
363
364 //=================================================================================
365 // function : LineEditReturnPressed()
366 // purpose  :
367 //=================================================================================
368 void BasicGUI_PointDlg::LineEditReturnPressed()
369 {
370   QLineEdit* send = (QLineEdit*)sender();
371   if(send == GroupDimensions->LineEdit1)
372     myEditCurrentArgument = GroupDimensions->LineEdit1;
373   else
374     return;
375
376   GEOMBase_Skeleton::LineEditReturnPressed();
377   return;
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 == GroupDimensions->PushButton1) {
390     GroupDimensions->LineEdit1->setFocus();
391     myEditCurrentArgument = GroupDimensions->LineEdit1;
392     mySelection->AddFilter(myEdgeFilter);
393     this->SelectionIntoArgument();
394   }
395   return;
396 }
397
398
399 //=================================================================================
400 // function : enterEvent()
401 // purpose  : to reactivate this dialog box when mouse enter onto the window
402 //=================================================================================
403 void BasicGUI_PointDlg::enterEvent(QEvent* e)
404 {
405   if (GroupConstructors->isEnabled())
406     return;
407   this->ActivateThisDialog();
408   return;
409 }
410
411
412 //=======================================================================
413 // function : closeEvent()
414 // purpose  :
415 //=======================================================================
416 void BasicGUI_PointDlg::closeEvent(QCloseEvent* e)
417 {
418   /* same than click on cancel button */
419   this->ClickOnCancel();
420 }
421
422
423 //=================================================================================
424 // function : ActivateThisDialog()
425 // purpose  :
426 //=================================================================================
427 void BasicGUI_PointDlg::ActivateThisDialog( )
428 {
429   GEOMBase_Skeleton::ActivateThisDialog();
430   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
431
432   myGeomGUI->myState = 0;
433   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
434     OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
435     myIC = v3d->getAISContext();
436     myLocalContextId = myIC->OpenLocalContext();
437     //myGeomBase->SetDisplayedObjectList();
438     /* sub shapes selection */
439     myLocalContextMode = TopAbs_VERTEX;
440     myIC->ActivateStandardMode(myLocalContextMode);
441     myUseLocalContext = true;
442   }
443
444   if(myConstructorId == 1)
445     mySelection->AddFilter(myEdgeFilter);
446
447   if(!mySimulationTopoDs.IsNull())
448     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
449   return;
450 }
451
452
453 //=================================================================================
454 // function : DeactivateActiveDialog()
455 // purpose  : public slot to deactivate if active
456 //=================================================================================
457 void BasicGUI_PointDlg::DeactivateActiveDialog()
458 {
459   if(myConstructorId == 0 && QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
460     myIC->CloseLocalContext(myLocalContextId);
461     myUseLocalContext = false;
462   }
463   myGeomGUI->myState = -1;
464   GEOMBase_Skeleton::DeactivateActiveDialog();
465   return;
466 }
467
468
469 //=================================================================================
470 // function : ValueChangedInSpinBox()
471 // purpose  :
472 //=================================================================================
473 void BasicGUI_PointDlg::ValueChangedInSpinBox(double newValue)
474 {
475   DlgRef_SpinBox* send = (DlgRef_SpinBox*)sender();
476   double vx, vy, vz;
477
478   if(send == GroupPoints->SpinBox_DX) {
479     vx = newValue;
480     vy = GroupPoints->SpinBox_DY->GetValue();
481     vz = GroupPoints->SpinBox_DZ->GetValue();
482   } else if(send == GroupPoints->SpinBox_DY) {
483     vx = GroupPoints->SpinBox_DX->GetValue();
484     vy = newValue;
485     vz = GroupPoints->SpinBox_DZ->GetValue();
486   } else if(send == GroupPoints->SpinBox_DZ) {
487     vx = GroupPoints->SpinBox_DX->GetValue();
488     vy = GroupPoints->SpinBox_DY->GetValue();
489     vz = newValue;
490   }  else if(send == GroupDimensions->SpinBox_DX) {
491     myParameter = newValue;
492   } else
493     return;
494
495   switch(myConstructorId) 
496     {
497     case 0: // default constructor 
498       {
499         myPoint.SetCoord(vx, vy, vz);
500         mySimulationTopoDs = BRepBuilderAPI_MakeVertex (myPoint).Shape();
501         myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
502         break;
503       }
504     case 1: 
505       {
506         this->SelectionIntoArgument();  
507         break;
508       }
509     }
510   return;
511 }
512
513
514 //=================================================================================
515 // function : CalculateVertexOnCurve()
516 // purpose  : Calculate a Vertex on the curve given by 'anEdge'.
517 //          : The position of resultVertex is given by aParameter.
518 //          : For a linear edge, aParameter=0.0 gives the first vertex of edge
519 //          :                    aParameter=1.0 gives the last vertex of edge
520 //          :                    aParameter=0.5 gives the vertex on the middle of edge
521 //          : It is possible to get vertices out of edge using values > 1.0 or < 0.0
522 //=================================================================================
523 bool BasicGUI_PointDlg::CalculateVertexOnCurve(const TopoDS_Edge& anEdge, const Standard_Real aParameter, TopoDS_Shape& resultVertex) 
524 {
525 #if OCC_VERSION_MAJOR >= 5
526   if(anEdge.IsNull() || !BRepAlgo::IsValid(anEdge))
527 #else
528   if(anEdge.IsNull() || !BRepAlgoAPI::IsValid(anEdge))
529 #endif
530     return false;
531
532   Standard_Real first, last;
533   Handle(Geom_Curve) curv = BRep_Tool::Curve(anEdge, first, last);
534   if(!curv->IsCN(0))
535     return false;
536   
537   Standard_Real param;
538   if(anEdge.Orientation() == TopAbs_FORWARD)
539     param = first + (last-first) * aParameter;
540   else
541     param = last + (first-last) * aParameter;
542
543   gp_Pnt paramPoint;
544   curv->D0(param, paramPoint);
545   resultVertex = BRepBuilderAPI_MakeVertex(paramPoint);
546   return true;
547 }
548
549
550 //=======================================================================
551 // funcion  : PointIntoCoordinates()
552 // purpose  : Sets user point coordinates into this dialog Spin boxes
553 //          : and displays it or not according to 'bool displayPoint'
554 //=======================================================================
555 void BasicGUI_PointDlg::PointIntoCoordinates(gp_Pnt P, bool displayPoint)
556 {
557   GroupPoints->SpinBox_DX->SetValue(P.X());
558   GroupPoints->SpinBox_DY->SetValue(P.Y());
559   GroupPoints->SpinBox_DZ->SetValue(P.Z());
560   myPoint.SetCoord(P.X(), P.Y(), P.Z());
561   if(displayPoint) {
562     mySimulationTopoDs = BRepBuilderAPI_MakeVertex(P).Shape();
563     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
564   }
565   return;
566 }