]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_WidgetPoint2dDistance.cpp
Salome HOME
Merge branch 'Dev_0.6.1' of newgeom:newgeom into Dev_0.6.1
[modules/shaper.git] / src / PartSet / PartSet_WidgetPoint2dDistance.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_WidgetPoint2dDistance.h
4 // Created:     23 June 2014
5 // Author:      Vitaly Smetannikov
6
7 #include "PartSet_WidgetPoint2dDistance.h"
8 #include "PartSet_Tools.h"
9
10 #include <ModuleBase_DoubleSpinBox.h>
11 #include <ModuleBase_IViewWindow.h>
12
13 #include <XGUI_ViewerProxy.h>
14 #include <XGUI_Workshop.h>
15
16 #include <GeomAPI_Pnt2d.h>
17 #include <Config_WidgetAPI.h>
18 #include <GeomDataAPI_Point2D.h>
19
20 #include <ModelAPI_Data.h>
21 #include <ModelAPI_AttributeDouble.h>
22
23 #include <QMouseEvent>
24
25 PartSet_WidgetPoint2dDistance::PartSet_WidgetPoint2dDistance(QWidget* theParent,
26                                                                    const Config_WidgetAPI* theData,
27                                                                    const std::string& theParentId)
28     : ModuleBase_WidgetDoubleValue(theParent, theData, theParentId)
29 {
30   myFirstPntName = theData->getProperty("first_point");
31 }
32
33 PartSet_WidgetPoint2dDistance::~PartSet_WidgetPoint2dDistance()
34 {
35 }
36
37 //bool PartSet_WidgetPoint2dDistance::setValue(ModuleBase_WidgetValue* theValue)
38 //{
39 //  bool isDone = false;
40 //
41 //  if (theValue) {
42 //    ModuleBase_WidgetValueFeature* aFeatureValue =
43 //        dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
44 //    if (aFeatureValue) {
45 //      std::shared_ptr<GeomAPI_Pnt2d> aPnt = aFeatureValue->point();
46 //      ObjectPtr aObject = aFeatureValue->object();
47 //      FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
48 //      if (aFeature && aPnt) {
49 //        setPoint(aFeature, aPnt);
50 //        isDone = true;
51 //      }
52 //    }
53 //  }
54 //  return isDone;
55 //}
56
57 void PartSet_WidgetPoint2dDistance::setPoint(FeaturePtr theFeature,
58                                              const std::shared_ptr<GeomAPI_Pnt2d>& thePnt)
59 {
60   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
61   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
62       aData->attribute(myFirstPntName));
63   if (!aPoint)
64     return;
65
66   double aRadius = thePnt->distance(aPoint->pnt());
67   AttributeDoublePtr aReal = aData->real(attributeID());
68   if (aReal && (aReal->value() != aRadius)) {
69     aReal->setValue(aRadius);
70     mySpinBox->setValue(aRadius);
71   }
72 }
73
74 void PartSet_WidgetPoint2dDistance::activate()
75 {
76   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
77   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
78           this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
79   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
80           this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
81 }
82
83 void PartSet_WidgetPoint2dDistance::deactivate()
84 {
85   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
86   disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
87              this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
88   disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
89              this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
90 }
91
92 void PartSet_WidgetPoint2dDistance::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
93 {
94   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
95
96   double aX, aY;
97   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
98
99   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
100   setPoint(feature(), aPnt);
101   emit focusOutWidget(this);
102 }
103
104 void PartSet_WidgetPoint2dDistance::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
105 {
106   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
107
108   double aX, aY;
109   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
110
111   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
112   setPoint(feature(), aPnt);
113 }
114
115