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 <XGUI_Tools.h>
11 #include <XGUI_Workshop.h>
12 #include <XGUI_Displayer.h>
14 #include <ModuleBase_ParamSpinBox.h>
15 #include <ModuleBase_IWorkshop.h>
16 #include <ModuleBase_IViewWindow.h>
17 #include <ModuleBase_IViewer.h>
18 #include <ModuleBase_Tools.h>
19 #include <ModuleBase_WidgetValidator.h>
21 #include <GeomAPI_Pnt2d.h>
22 #include <Config_WidgetAPI.h>
23 #include <GeomDataAPI_Point2D.h>
25 #include <ModelAPI_Data.h>
26 #include <ModelAPI_AttributeDouble.h>
28 #include <QMouseEvent>
30 PartSet_WidgetPoint2dDistance::PartSet_WidgetPoint2dDistance(QWidget* theParent,
31 ModuleBase_IWorkshop* theWorkshop,
32 const Config_WidgetAPI* theData)
33 : ModuleBase_WidgetDoubleValue(theParent, theData), myWorkshop(theWorkshop),
34 myValueIsCashed(false), myIsFeatureVisibleInCash(true), myValueInCash(0)
36 myFirstPntName = theData->getProperty("first_point");
37 myWidgetValidator = new ModuleBase_WidgetValidator(this, myWorkshop);
40 PartSet_WidgetPoint2dDistance::~PartSet_WidgetPoint2dDistance()
44 bool PartSet_WidgetPoint2dDistance::isValidSelectionCustom(
45 const std::shared_ptr<ModuleBase_ViewerPrs>& theValue)
50 bool PartSet_WidgetPoint2dDistance::resetCustom()
53 if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
57 if (myValueIsCashed) {
58 // if the restored value should be hidden, aDone = true to set
59 // reset state for the widget in the parent
60 aDone = restoreCurentValue();
64 aDone = ModuleBase_WidgetDoubleValue::resetCustom();
69 void PartSet_WidgetPoint2dDistance::setPoint(FeaturePtr theFeature,
70 const std::shared_ptr<GeomAPI_Pnt2d>& thePnt)
72 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
73 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
74 aData->attribute(myFirstPntName));
78 double aValue = computeValue(aPoint->pnt(), thePnt);
79 AttributeDoublePtr aReal = aData->real(attributeID());
80 if (aReal && (aReal->value() != aValue)) {
81 aReal->setValue(aValue);
83 ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
88 double PartSet_WidgetPoint2dDistance::computeValue(
89 const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPnt,
90 const std::shared_ptr<GeomAPI_Pnt2d>& theCurrentPnt)
92 return theCurrentPnt->distance(theFirstPnt);
95 void PartSet_WidgetPoint2dDistance::mouseReleased(ModuleBase_IViewWindow* theWnd,
96 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);
113 // if the validator of the control returns false, focus should not be switched
114 if (getError(false).isEmpty())
115 emit focusOutWidget(this);
118 void PartSet_WidgetPoint2dDistance::mouseMoved(ModuleBase_IViewWindow* theWnd,
119 QMouseEvent* theEvent)
124 if (mySpinBox->hasVariable())
127 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
130 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
132 std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
133 if (myState != ModifiedInViewer)
136 bool isBlocked = blockValueState(true);
137 setPoint(feature(), aPnt);
138 blockValueState(isBlocked);
139 setValueState(ModifiedInViewer);
142 void PartSet_WidgetPoint2dDistance::storeCurentValue()
144 // do not use cash if a variable is used
145 if (mySpinBox->hasVariable())
148 myValueIsCashed = true;
149 myIsFeatureVisibleInCash = XGUI_Displayer::isVisible(
150 XGUI_Tools::workshop(myWorkshop)->displayer(), myFeature);
151 myValueInCash = mySpinBox->value();
154 bool PartSet_WidgetPoint2dDistance::restoreCurentValue()
156 bool aRestoredAndHidden = true;
158 bool isVisible = myIsFeatureVisibleInCash;
159 // fill the control widgets by the cashed value
161 myValueIsCashed = false;
162 myIsFeatureVisibleInCash = true;
163 ModuleBase_Tools::setSpinValue(mySpinBox, myValueInCash);
165 // store value to the model
168 setValueState(Stored);
169 aRestoredAndHidden = false;
172 aRestoredAndHidden = true;
174 return aRestoredAndHidden;
177 bool PartSet_WidgetPoint2dDistance::processEnter()
179 bool isModified = getValueState() == ModifiedInPP;
181 emit valuesChanged();
182 mySpinBox->selectAll();