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 : ModuleBase_WidgetDoubleValue(theParent, theData), myWorkshop(theWorkshop),
33 myValueIsCashed(false), myIsFeatureVisibleInCash(true), myValueInCash(0)
35 myFirstPntName = theData->getProperty("first_point");
38 PartSet_WidgetPoint2dDistance::~PartSet_WidgetPoint2dDistance()
42 bool PartSet_WidgetPoint2dDistance::resetCustom()
45 if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
49 if (myValueIsCashed) {
50 // if the restored value should be hidden, aDone = true to set
51 // reset state for the widget in the parent
52 aDone = restoreCurentValue();
55 aDone = ModuleBase_WidgetDoubleValue::resetCustom();
60 void PartSet_WidgetPoint2dDistance::setPoint(FeaturePtr theFeature,
61 const std::shared_ptr<GeomAPI_Pnt2d>& thePnt)
63 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
64 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
65 aData->attribute(myFirstPntName));
69 double aValue = computeValue(aPoint->pnt(), thePnt);
70 AttributeDoublePtr aReal = aData->real(attributeID());
71 if (aReal && (aReal->value() != aValue)) {
72 aReal->setValue(aValue);
74 ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
79 double PartSet_WidgetPoint2dDistance::computeValue(const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPnt,
80 const std::shared_ptr<GeomAPI_Pnt2d>& theCurrentPnt)
82 return theCurrentPnt->distance(theFirstPnt);
85 void PartSet_WidgetPoint2dDistance::activateCustom()
87 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
88 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
89 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
90 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
91 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
94 void PartSet_WidgetPoint2dDistance::deactivate()
96 ModuleBase_ModelWidget::deactivate();
97 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
98 disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
99 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
100 disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
101 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
104 void PartSet_WidgetPoint2dDistance::onMouseRelease(ModuleBase_IViewWindow* theWnd, 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().isEmpty())
123 emit focusOutWidget(this);
126 void PartSet_WidgetPoint2dDistance::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
131 if (mySpinBox->hasVariable())
134 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
137 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
139 std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
140 if (myState != ModifiedInViewer)
143 bool isBlocked = blockValueState(true);
144 setPoint(feature(), aPnt);
145 blockValueState(isBlocked);
146 setValueState(ModifiedInViewer);
149 void PartSet_WidgetPoint2dDistance::storeCurentValue()
151 // do not use cash if a variable is used
152 if (mySpinBox->hasVariable())
155 myValueIsCashed = true;
156 myIsFeatureVisibleInCash = XGUI_Displayer::isVisible(
157 XGUI_Tools::workshop(myWorkshop)->displayer(), myFeature);
158 myValueInCash = mySpinBox->value();
161 bool PartSet_WidgetPoint2dDistance::restoreCurentValue()
163 bool aRestoredAndHidden = true;
165 bool isVisible = myIsFeatureVisibleInCash;
166 // fill the control widgets by the cashed value
168 myValueIsCashed = false;
169 myIsFeatureVisibleInCash = true;
170 ModuleBase_Tools::setSpinValue(mySpinBox, myValueInCash);
172 // store value to the model
175 setValueState(Stored);
176 aRestoredAndHidden = false;
179 aRestoredAndHidden = true;
181 return aRestoredAndHidden;
184 bool PartSet_WidgetPoint2dDistance::processEnter()
186 bool isModified = getValueState() == ModifiedInPP;
188 emit valuesChanged();
189 mySpinBox->selectAll();