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 #define APPLY_BY_ENTER_OR_TAB
27 PartSet_WidgetPoint2dDistance::PartSet_WidgetPoint2dDistance(QWidget* theParent,
28 ModuleBase_IWorkshop* theWorkshop,
29 const Config_WidgetAPI* theData,
30 const std::string& theParentId)
31 : ModuleBase_WidgetDoubleValue(theParent, theData, theParentId), myWorkshop(theWorkshop)
33 myFirstPntName = theData->getProperty("first_point");
35 // Reconnect to local slot
36 #ifdef APPLY_BY_ENTER_OR_TAB
37 // Apply widget value change by enter/tab event.
38 //disconnect(mySpinBox, SIGNAL(editingFinished()), this, SIGNAL(valuesChanged()));
39 disconnect(mySpinBox, SIGNAL(valueStored()), this, SIGNAL(valuesChanged()));
40 connect(mySpinBox, SIGNAL(editingFinished()), this, SLOT(onValuesChanged()));
41 connect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(valuesModified()));
43 disconnect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
44 connect(mySpinBox, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
48 PartSet_WidgetPoint2dDistance::~PartSet_WidgetPoint2dDistance()
52 void PartSet_WidgetPoint2dDistance::setPoint(FeaturePtr theFeature,
53 const std::shared_ptr<GeomAPI_Pnt2d>& thePnt)
55 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
56 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
57 aData->attribute(myFirstPntName));
61 double aValue = computeValue(aPoint->pnt(), thePnt);
62 AttributeDoublePtr aReal = aData->real(attributeID());
63 if (aReal && (aReal->value() != aValue)) {
64 aReal->setValue(aValue);
66 ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
71 double PartSet_WidgetPoint2dDistance::computeValue(const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPnt,
72 const std::shared_ptr<GeomAPI_Pnt2d>& theCurrentPnt)
74 return theCurrentPnt->distance(theFirstPnt);
77 void PartSet_WidgetPoint2dDistance::activateCustom()
79 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
80 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
81 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
82 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
83 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
86 void PartSet_WidgetPoint2dDistance::deactivate()
88 ModuleBase_ModelWidget::deactivate();
89 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
90 disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
91 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
92 disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
93 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
96 void PartSet_WidgetPoint2dDistance::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
98 // the contex menu release by the right button should not be processed by this widget
99 if (theEvent->button() != Qt::LeftButton)
102 if (mySpinBox->hasVariable())
105 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
108 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
110 std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
111 setPoint(feature(), aPnt);
112 emit focusOutWidget(this);
115 void PartSet_WidgetPoint2dDistance::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
120 if (mySpinBox->hasVariable())
123 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
126 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
128 std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
129 bool isBlocked = blockValueState(true);
130 setPoint(feature(), aPnt);
131 blockValueState(isBlocked);
132 setValueState(ModifiedInViewer);
135 void PartSet_WidgetPoint2dDistance::onValuesChanged()
137 emit valuesChanged();
140 bool PartSet_WidgetPoint2dDistance::processEnter()
142 bool isModified = mySpinBox->isModified();
144 emit valuesChanged();
145 mySpinBox->clearModified();
146 mySpinBox->selectAll();