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(const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPnt,
89 const std::shared_ptr<GeomAPI_Pnt2d>& theCurrentPnt)
91 return theCurrentPnt->distance(theFirstPnt);
94 void PartSet_WidgetPoint2dDistance::mouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
96 // the contex menu release by the right button should not be processed by this widget
97 if (theEvent->button() != Qt::LeftButton)
100 if (mySpinBox->hasVariable())
103 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
106 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
108 std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
109 setPoint(feature(), aPnt);
111 // if the validator of the control returns false, focus should not be switched
112 if (getError(false).isEmpty())
113 emit focusOutWidget(this);
116 void PartSet_WidgetPoint2dDistance::mouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
121 if (mySpinBox->hasVariable())
124 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
127 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
129 std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
130 if (myState != ModifiedInViewer)
133 bool isBlocked = blockValueState(true);
134 setPoint(feature(), aPnt);
135 blockValueState(isBlocked);
136 setValueState(ModifiedInViewer);
139 void PartSet_WidgetPoint2dDistance::storeCurentValue()
141 // do not use cash if a variable is used
142 if (mySpinBox->hasVariable())
145 myValueIsCashed = true;
146 myIsFeatureVisibleInCash = XGUI_Displayer::isVisible(
147 XGUI_Tools::workshop(myWorkshop)->displayer(), myFeature);
148 myValueInCash = mySpinBox->value();
151 bool PartSet_WidgetPoint2dDistance::restoreCurentValue()
153 bool aRestoredAndHidden = true;
155 bool isVisible = myIsFeatureVisibleInCash;
156 // fill the control widgets by the cashed value
158 myValueIsCashed = false;
159 myIsFeatureVisibleInCash = true;
160 ModuleBase_Tools::setSpinValue(mySpinBox, myValueInCash);
162 // store value to the model
165 setValueState(Stored);
166 aRestoredAndHidden = false;
169 aRestoredAndHidden = true;
171 return aRestoredAndHidden;
174 bool PartSet_WidgetPoint2dDistance::processEnter()
176 bool isModified = getValueState() == ModifiedInPP;
178 emit valuesChanged();
179 mySpinBox->selectAll();