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