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();
63 aDone = ModuleBase_WidgetDoubleValue::resetCustom();
68 void PartSet_WidgetPoint2dDistance::setPoint(FeaturePtr theFeature,
69 const std::shared_ptr<GeomAPI_Pnt2d>& thePnt)
71 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
72 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
73 aData->attribute(myFirstPntName));
77 double aValue = computeValue(aPoint->pnt(), thePnt);
78 AttributeDoublePtr aReal = aData->real(attributeID());
79 if (aReal && (aReal->value() != aValue)) {
80 aReal->setValue(aValue);
82 ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
87 double PartSet_WidgetPoint2dDistance::computeValue(const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPnt,
88 const std::shared_ptr<GeomAPI_Pnt2d>& theCurrentPnt)
90 return theCurrentPnt->distance(theFirstPnt);
93 void PartSet_WidgetPoint2dDistance::activateCustom()
95 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
96 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
97 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
98 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
99 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
102 void PartSet_WidgetPoint2dDistance::deactivate()
104 ModuleBase_ModelWidget::deactivate();
105 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
106 disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
107 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
108 disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
109 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
112 void PartSet_WidgetPoint2dDistance::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
114 // the contex menu release by the right button should not be processed by this widget
115 if (theEvent->button() != Qt::LeftButton)
118 if (mySpinBox->hasVariable())
121 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
124 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
126 std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
127 setPoint(feature(), aPnt);
129 // if the validator of the control returns false, focus should not be switched
130 if (getError().isEmpty())
131 emit focusOutWidget(this);
134 void PartSet_WidgetPoint2dDistance::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
139 if (mySpinBox->hasVariable())
142 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
145 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
147 std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
148 if (myState != ModifiedInViewer)
151 bool isBlocked = blockValueState(true);
152 setPoint(feature(), aPnt);
153 blockValueState(isBlocked);
154 setValueState(ModifiedInViewer);
157 void PartSet_WidgetPoint2dDistance::storeCurentValue()
159 // do not use cash if a variable is used
160 if (mySpinBox->hasVariable())
163 myValueIsCashed = true;
164 myIsFeatureVisibleInCash = XGUI_Displayer::isVisible(
165 XGUI_Tools::workshop(myWorkshop)->displayer(), myFeature);
166 myValueInCash = mySpinBox->value();
169 bool PartSet_WidgetPoint2dDistance::restoreCurentValue()
171 bool aRestoredAndHidden = true;
173 bool isVisible = myIsFeatureVisibleInCash;
174 // fill the control widgets by the cashed value
176 myValueIsCashed = false;
177 myIsFeatureVisibleInCash = true;
178 ModuleBase_Tools::setSpinValue(mySpinBox, myValueInCash);
180 // store value to the model
183 setValueState(Stored);
184 aRestoredAndHidden = false;
187 aRestoredAndHidden = true;
189 return aRestoredAndHidden;
192 bool PartSet_WidgetPoint2dDistance::processEnter()
194 bool isModified = getValueState() == ModifiedInPP;
196 emit valuesChanged();
197 mySpinBox->selectAll();