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"
10 #include <ModuleBase_ParamSpinBox.h>
11 #include <ModuleBase_IWorkshop.h>
12 #include <ModuleBase_IViewWindow.h>
13 #include <ModuleBase_IViewer.h>
14 #include <ModuleBase_Tools.h>
16 #include <GeomAPI_Pnt2d.h>
17 #include <Config_WidgetAPI.h>
18 #include <GeomDataAPI_Point2D.h>
20 #include <ModelAPI_Data.h>
21 #include <ModelAPI_AttributeDouble.h>
23 #include <QMouseEvent>
25 PartSet_WidgetPoint2dDistance::PartSet_WidgetPoint2dDistance(QWidget* theParent,
26 ModuleBase_IWorkshop* theWorkshop,
27 const Config_WidgetAPI* theData,
28 const std::string& theParentId)
29 : ModuleBase_WidgetDoubleValue(theParent, theData, theParentId), myWorkshop(theWorkshop)
31 myFirstPntName = theData->getProperty("first_point");
34 PartSet_WidgetPoint2dDistance::~PartSet_WidgetPoint2dDistance()
38 void PartSet_WidgetPoint2dDistance::setPoint(FeaturePtr theFeature,
39 const std::shared_ptr<GeomAPI_Pnt2d>& thePnt)
41 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
42 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
43 aData->attribute(myFirstPntName));
47 double aValue = computeValue(aPoint->pnt(), thePnt);
48 AttributeDoublePtr aReal = aData->real(attributeID());
49 if (aReal && (aReal->value() != aValue)) {
50 aReal->setValue(aValue);
52 ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
57 double PartSet_WidgetPoint2dDistance::computeValue(const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPnt,
58 const std::shared_ptr<GeomAPI_Pnt2d>& theCurrentPnt)
60 return theCurrentPnt->distance(theFirstPnt);
63 void PartSet_WidgetPoint2dDistance::activateCustom()
65 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
66 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
67 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
68 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
69 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
72 void PartSet_WidgetPoint2dDistance::deactivate()
74 ModuleBase_ModelWidget::deactivate();
75 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
76 disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
77 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
78 disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
79 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
82 void PartSet_WidgetPoint2dDistance::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
84 // the contex menu release by the right button should not be processed by this widget
85 if (theEvent->button() != Qt::LeftButton)
88 if (mySpinBox->hasVariable())
91 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
94 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
96 std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
97 setPoint(feature(), aPnt);
98 emit focusOutWidget(this);
101 void PartSet_WidgetPoint2dDistance::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
106 if (mySpinBox->hasVariable())
109 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
112 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
114 std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
115 bool isBlocked = blockValueState(true);
116 setPoint(feature(), aPnt);
117 blockValueState(isBlocked);
118 setValueState(ModifiedInViewer);
121 bool PartSet_WidgetPoint2dDistance::processEnter()
123 bool isModified = mySpinBox->isModified();
125 emit valuesChanged();
126 mySpinBox->clearModified();
127 mySpinBox->selectAll();