1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PartSet_WidgetPoint2dDistance.cpp
4 // Created: 23 June 2014
5 // Author: Vitaly Smetannikov
7 #include "PartSet_WidgetPoint2dDistance.h"
8 #include "PartSet_Tools.h"
9 #include "PartSet_LockApplyMgr.h"
11 #include <ModuleBase_ParamSpinBox.h>
12 #include <ModuleBase_IWorkshop.h>
13 #include <ModuleBase_IViewWindow.h>
14 #include <ModuleBase_IViewer.h>
15 #include <ModuleBase_Tools.h>
17 #include <GeomAPI_Pnt2d.h>
18 #include <Config_WidgetAPI.h>
19 #include <GeomDataAPI_Point2D.h>
21 #include <ModelAPI_Data.h>
22 #include <ModelAPI_AttributeDouble.h>
24 #include <QMouseEvent>
26 PartSet_WidgetPoint2dDistance::PartSet_WidgetPoint2dDistance(QWidget* theParent,
27 ModuleBase_IWorkshop* theWorkshop,
28 const Config_WidgetAPI* theData,
29 const std::string& theParentId)
30 : ModuleBase_WidgetDoubleValue(theParent, theData, theParentId), myWorkshop(theWorkshop)
32 myLockApplyMgr = new PartSet_LockApplyMgr(theParent, myWorkshop);
34 myFirstPntName = theData->getProperty("first_point");
36 // Reconnect to local slot
37 disconnect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
38 connect(mySpinBox, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
41 PartSet_WidgetPoint2dDistance::~PartSet_WidgetPoint2dDistance()
45 void PartSet_WidgetPoint2dDistance::setPoint(FeaturePtr theFeature,
46 const std::shared_ptr<GeomAPI_Pnt2d>& thePnt)
48 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
49 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
50 aData->attribute(myFirstPntName));
54 double aValue = computeValue(aPoint->pnt(), thePnt);
55 AttributeDoublePtr aReal = aData->real(attributeID());
56 if (aReal && (aReal->value() != aValue)) {
57 aReal->setValue(aValue);
59 ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
64 double PartSet_WidgetPoint2dDistance::computeValue(const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPnt,
65 const std::shared_ptr<GeomAPI_Pnt2d>& theCurrentPnt)
67 return theCurrentPnt->distance(theFirstPnt);
70 void PartSet_WidgetPoint2dDistance::activateCustom()
72 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
73 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
74 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
75 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
76 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
78 myLockApplyMgr->activate();
81 void PartSet_WidgetPoint2dDistance::deactivate()
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*)));
89 myLockApplyMgr->deactivate();
92 void PartSet_WidgetPoint2dDistance::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
94 // the contex menu release by the right button should not be processed by this widget
95 if (theEvent->button() != Qt::LeftButton)
98 if (mySpinBox->hasVariable())
101 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
104 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
106 std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
107 setPoint(feature(), aPnt);
108 emit focusOutWidget(this);
111 void PartSet_WidgetPoint2dDistance::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
116 if (mySpinBox->hasVariable())
119 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
122 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
124 std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
125 setPoint(feature(), aPnt);
128 void PartSet_WidgetPoint2dDistance::onValuesChanged()
130 myLockApplyMgr->valuesChanged();
131 emit valuesChanged();