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::activateCustom()
96 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
97 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
98 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
99 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
100 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
103 void PartSet_WidgetPoint2dDistance::deactivate()
105 ModuleBase_ModelWidget::deactivate();
106 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
107 disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
108 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
109 disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
110 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
113 void PartSet_WidgetPoint2dDistance::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
115 // the contex menu release by the right button should not be processed by this widget
116 if (theEvent->button() != Qt::LeftButton)
119 if (mySpinBox->hasVariable())
122 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
125 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
127 std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
128 setPoint(feature(), aPnt);
130 // if the validator of the control returns false, focus should not be switched
131 if (getError().isEmpty())
132 emit focusOutWidget(this);
135 void PartSet_WidgetPoint2dDistance::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
140 if (mySpinBox->hasVariable())
143 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
146 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
148 std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
149 if (myState != ModifiedInViewer)
152 bool isBlocked = blockValueState(true);
153 setPoint(feature(), aPnt);
154 blockValueState(isBlocked);
155 setValueState(ModifiedInViewer);
158 void PartSet_WidgetPoint2dDistance::storeCurentValue()
160 // do not use cash if a variable is used
161 if (mySpinBox->hasVariable())
164 myValueIsCashed = true;
165 myIsFeatureVisibleInCash = XGUI_Displayer::isVisible(
166 XGUI_Tools::workshop(myWorkshop)->displayer(), myFeature);
167 myValueInCash = mySpinBox->value();
170 bool PartSet_WidgetPoint2dDistance::restoreCurentValue()
172 bool aRestoredAndHidden = true;
174 bool isVisible = myIsFeatureVisibleInCash;
175 // fill the control widgets by the cashed value
177 myValueIsCashed = false;
178 myIsFeatureVisibleInCash = true;
179 ModuleBase_Tools::setSpinValue(mySpinBox, myValueInCash);
181 // store value to the model
184 setValueState(Stored);
185 aRestoredAndHidden = false;
188 aRestoredAndHidden = true;
190 return aRestoredAndHidden;
193 bool PartSet_WidgetPoint2dDistance::processEnter()
195 bool isModified = getValueState() == ModifiedInPP;
197 emit valuesChanged();
198 mySpinBox->selectAll();