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 #define APPLY_BY_ENTER_OR_TAB
28 PartSet_WidgetPoint2dDistance::PartSet_WidgetPoint2dDistance(QWidget* theParent,
29 ModuleBase_IWorkshop* theWorkshop,
30 const Config_WidgetAPI* theData,
31 const std::string& theParentId)
32 : ModuleBase_WidgetDoubleValue(theParent, theData, theParentId), myWorkshop(theWorkshop)
34 myLockApplyMgr = new PartSet_LockApplyMgr(theParent, myWorkshop);
36 myFirstPntName = theData->getProperty("first_point");
38 // Reconnect to local slot
39 #ifdef APPLY_BY_ENTER_OR_TAB
40 // Apply widget value change by enter/tab event.
41 disconnect(mySpinBox, SIGNAL(editingFinished()), this, SIGNAL(valuesChanged()));
42 connect(mySpinBox, SIGNAL(editingFinished()), this, SLOT(onValuesChanged()));
43 connect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(valuesModified()));
45 disconnect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
46 connect(mySpinBox, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
50 PartSet_WidgetPoint2dDistance::~PartSet_WidgetPoint2dDistance()
54 void PartSet_WidgetPoint2dDistance::setPoint(FeaturePtr theFeature,
55 const std::shared_ptr<GeomAPI_Pnt2d>& thePnt)
57 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
58 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
59 aData->attribute(myFirstPntName));
63 double aValue = computeValue(aPoint->pnt(), thePnt);
64 AttributeDoublePtr aReal = aData->real(attributeID());
65 if (aReal && (aReal->value() != aValue)) {
66 aReal->setValue(aValue);
68 ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
73 double PartSet_WidgetPoint2dDistance::computeValue(const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPnt,
74 const std::shared_ptr<GeomAPI_Pnt2d>& theCurrentPnt)
76 return theCurrentPnt->distance(theFirstPnt);
79 void PartSet_WidgetPoint2dDistance::activateCustom()
81 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
82 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
83 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
84 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
85 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
87 myLockApplyMgr->activate();
90 void PartSet_WidgetPoint2dDistance::deactivate()
92 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
93 disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
94 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
95 disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
96 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
98 myLockApplyMgr->deactivate();
101 void PartSet_WidgetPoint2dDistance::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
103 // the contex menu release by the right button should not be processed by this widget
104 if (theEvent->button() != Qt::LeftButton)
107 if (mySpinBox->hasVariable())
110 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
113 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
115 std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
116 setPoint(feature(), aPnt);
117 emit focusOutWidget(this);
120 void PartSet_WidgetPoint2dDistance::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
125 if (mySpinBox->hasVariable())
128 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
131 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
133 std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
134 setPoint(feature(), aPnt);
137 void PartSet_WidgetPoint2dDistance::onValuesChanged()
139 myLockApplyMgr->valuesChanged();
140 emit valuesChanged();
143 bool PartSet_WidgetPoint2dDistance::isEventProcessed(QKeyEvent* theEvent)
145 return mySpinBox->isEventProcessed(theEvent);