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>
20 #include <GeomAPI_Pnt2d.h>
21 #include <Config_WidgetAPI.h>
22 #include <GeomDataAPI_Point2D.h>
24 #include <ModelAPI_Data.h>
25 #include <ModelAPI_AttributeDouble.h>
27 #include <QMouseEvent>
29 PartSet_WidgetPoint2dDistance::PartSet_WidgetPoint2dDistance(QWidget* theParent,
30 ModuleBase_IWorkshop* theWorkshop,
31 const Config_WidgetAPI* theData,
32 const std::string& theParentId)
33 : ModuleBase_WidgetDoubleValue(theParent, theData, theParentId), myWorkshop(theWorkshop),
34 myValueIsCashed(false), myIsFeatureVisibleInCash(true), myValueInCash(0)
36 myFirstPntName = theData->getProperty("first_point");
39 PartSet_WidgetPoint2dDistance::~PartSet_WidgetPoint2dDistance()
43 bool PartSet_WidgetPoint2dDistance::resetCustom()
46 if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
50 if (myValueIsCashed) {
51 // if the restored value should be hidden, aDone = true to set
52 // reset state for the widget in the parent
53 aDone = restoreCurentValue();
56 aDone = ModuleBase_WidgetDoubleValue::resetCustom();
61 void PartSet_WidgetPoint2dDistance::setPoint(FeaturePtr theFeature,
62 const std::shared_ptr<GeomAPI_Pnt2d>& thePnt)
64 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
65 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
66 aData->attribute(myFirstPntName));
70 double aValue = computeValue(aPoint->pnt(), thePnt);
71 AttributeDoublePtr aReal = aData->real(attributeID());
72 if (aReal && (aReal->value() != aValue)) {
73 aReal->setValue(aValue);
75 ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
80 double PartSet_WidgetPoint2dDistance::computeValue(const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPnt,
81 const std::shared_ptr<GeomAPI_Pnt2d>& theCurrentPnt)
83 return theCurrentPnt->distance(theFirstPnt);
86 void PartSet_WidgetPoint2dDistance::activateCustom()
88 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
89 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
90 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
91 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
92 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
95 void PartSet_WidgetPoint2dDistance::deactivate()
97 ModuleBase_ModelWidget::deactivate();
98 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
99 disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
100 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
101 disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
102 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
105 void PartSet_WidgetPoint2dDistance::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
107 // the contex menu release by the right button should not be processed by this widget
108 if (theEvent->button() != Qt::LeftButton)
111 if (mySpinBox->hasVariable())
114 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
117 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
119 std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
120 setPoint(feature(), aPnt);
122 // if the validator of the control returns false, focus should not be switched
123 if (getError().isEmpty())
124 emit focusOutWidget(this);
127 void PartSet_WidgetPoint2dDistance::onMouseMove(ModuleBase_IViewWindow* theWnd, 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 = mySpinBox->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 ModuleBase_Tools::setSpinValue(mySpinBox, myValueInCash);
173 // store value to the model
176 setValueState(Stored);
177 aRestoredAndHidden = false;
180 aRestoredAndHidden = true;
182 return aRestoredAndHidden;
185 bool PartSet_WidgetPoint2dDistance::processEnter()
187 bool isModified = getValueState() == ModifiedInPP;
189 emit valuesChanged();
190 mySpinBox->selectAll();