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>
20 #include <ModuleBase_LabelValue.h>
22 #include <GeomAPI_Pnt2d.h>
23 #include <Config_WidgetAPI.h>
24 #include <GeomDataAPI_Point2D.h>
26 #include <ModelAPI_Data.h>
27 #include <ModelAPI_AttributeDouble.h>
29 #include <QMouseEvent>
31 PartSet_WidgetPoint2dDistance::PartSet_WidgetPoint2dDistance(QWidget* theParent,
32 ModuleBase_IWorkshop* theWorkshop,
33 const Config_WidgetAPI* theData)
34 : ModuleBase_WidgetLabelValue(theParent, theData), myWorkshop(theWorkshop),
35 myValueIsCashed(false), myIsFeatureVisibleInCash(true), myValueInCash(0)
37 myFirstPntName = theData->getProperty("first_point");
38 myWidgetValidator = new ModuleBase_WidgetValidator(this, myWorkshop);
41 PartSet_WidgetPoint2dDistance::~PartSet_WidgetPoint2dDistance()
45 bool PartSet_WidgetPoint2dDistance::isValidSelectionCustom(
46 const std::shared_ptr<ModuleBase_ViewerPrs>& theValue)
51 bool PartSet_WidgetPoint2dDistance::resetCustom()
54 if (!isUseReset() || isComputedDefault() /*|| mySpinBox->hasVariable()*/) {
58 if (myValueIsCashed) {
59 // if the restored value should be hidden, aDone = true to set
60 // reset state for the widget in the parent
61 aDone = restoreCurentValue();
66 double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
67 // it is important to block the spin box control in order to do not through out the
68 // locking of the validating state.
69 myLabel->setValue(isOk ? aDefValue : 0.0);
77 void PartSet_WidgetPoint2dDistance::setPoint(FeaturePtr theFeature,
78 const std::shared_ptr<GeomAPI_Pnt2d>& thePnt)
80 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
81 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
82 aData->attribute(myFirstPntName));
86 double aValue = computeValue(aPoint->pnt(), thePnt);
87 AttributeDoublePtr aReal = aData->real(attributeID());
88 if (aReal && (aReal->value() != aValue)) {
89 aReal->setValue(aValue);
91 myLabel->setValue(aValue);
96 double PartSet_WidgetPoint2dDistance::computeValue(
97 const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPnt,
98 const std::shared_ptr<GeomAPI_Pnt2d>& theCurrentPnt)
100 return theCurrentPnt->distance(theFirstPnt);
103 void PartSet_WidgetPoint2dDistance::mouseReleased(ModuleBase_IViewWindow* theWnd,
104 QMouseEvent* theEvent)
106 // the contex menu release by the right button should not be processed by this widget
107 if (theEvent->button() != Qt::LeftButton)
110 //if (mySpinBox->hasVariable())
113 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
116 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
118 std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
119 setPoint(feature(), aPnt);
121 // if the validator of the control returns false, focus should not be switched
122 if (getError(false).isEmpty())
123 emit focusOutWidget(this);
126 void PartSet_WidgetPoint2dDistance::mouseMoved(ModuleBase_IViewWindow* theWnd,
127 QMouseEvent* theEvent)
132 //if (mySpinBox->hasVariable())
135 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
138 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
140 std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
141 if (myState != ModifiedInViewer)
144 bool isBlocked = blockValueState(true);
145 setPoint(feature(), aPnt);
146 blockValueState(isBlocked);
147 setValueState(ModifiedInViewer);
150 void PartSet_WidgetPoint2dDistance::storeCurentValue()
152 // do not use cash if a variable is used
153 //if (mySpinBox->hasVariable())
156 myValueIsCashed = true;
157 myIsFeatureVisibleInCash = XGUI_Displayer::isVisible(
158 XGUI_Tools::workshop(myWorkshop)->displayer(), myFeature);
159 myValueInCash = myLabel->value();
162 bool PartSet_WidgetPoint2dDistance::restoreCurentValue()
164 bool aRestoredAndHidden = true;
166 bool isVisible = myIsFeatureVisibleInCash;
167 // fill the control widgets by the cashed value
169 myValueIsCashed = false;
170 myIsFeatureVisibleInCash = true;
171 myLabel->setValue(myValueInCash);
173 // store value to the model
176 setValueState(Stored);
177 aRestoredAndHidden = false;
180 aRestoredAndHidden = true;
182 return aRestoredAndHidden;
185 bool PartSet_WidgetPoint2dDistance::processEnter()
189 bool isModified = getValueState() == ModifiedInPP;
191 emit valuesChanged();
192 //mySpinBox->selectAll();
197 bool PartSet_WidgetPoint2dDistance::storeValueCustom()
199 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
200 if (!aData) // can be on abort of sketcher element
202 AttributeDoublePtr anAttribute = myFeature->data()->real(attributeID());
203 anAttribute->setValue(myLabel->value());
205 // after movement the solver will call the update event: optimization
206 updateObject(myFeature);