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