Salome HOME
Issue #1860: fix end lines with spaces
[modules/shaper.git] / src / PartSet / PartSet_WidgetPoint2dDistance.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_WidgetPoint2dDistance.cpp
4 // Created:     23 June 2014
5 // Author:      Vitaly Smetannikov
6
7 #include "PartSet_WidgetPoint2dDistance.h"
8 #include "PartSet_Tools.h"
9
10 #include <XGUI_Tools.h>
11 #include <XGUI_Workshop.h>
12 #include <XGUI_Displayer.h>
13
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
21 #include <GeomAPI_Pnt2d.h>
22 #include <Config_WidgetAPI.h>
23 #include <GeomDataAPI_Point2D.h>
24
25 #include <ModelAPI_Data.h>
26 #include <ModelAPI_AttributeDouble.h>
27
28 #include <QMouseEvent>
29
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)
35 {
36   myFirstPntName = theData->getProperty("first_point");
37   myWidgetValidator = new ModuleBase_WidgetValidator(this, myWorkshop);
38 }
39
40 PartSet_WidgetPoint2dDistance::~PartSet_WidgetPoint2dDistance()
41 {
42 }
43
44 bool PartSet_WidgetPoint2dDistance::isValidSelectionCustom(
45                                       const std::shared_ptr<ModuleBase_ViewerPrs>& theValue)
46 {
47   return false;
48 }
49
50 bool PartSet_WidgetPoint2dDistance::resetCustom()
51 {
52   bool aDone = false;
53   if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
54     aDone = false;
55   }
56   else {
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();
61       emit objectUpdated();
62     }
63     else
64       aDone = ModuleBase_WidgetDoubleValue::resetCustom();
65   }
66   return aDone;
67 }
68
69 void PartSet_WidgetPoint2dDistance::setPoint(FeaturePtr theFeature,
70                                              const std::shared_ptr<GeomAPI_Pnt2d>& thePnt)
71 {
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));
75   if (!aPoint)
76     return;
77
78   double aValue = computeValue(aPoint->pnt(), thePnt);
79   AttributeDoublePtr aReal = aData->real(attributeID());
80   if (aReal && (aReal->value() != aValue)) {
81     aReal->setValue(aValue);
82
83     ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
84     storeValue();
85   }
86 }
87
88 double PartSet_WidgetPoint2dDistance::computeValue(
89                               const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPnt,
90                               const std::shared_ptr<GeomAPI_Pnt2d>& theCurrentPnt)
91 {
92   return theCurrentPnt->distance(theFirstPnt);
93 }
94
95 void PartSet_WidgetPoint2dDistance::mouseReleased(ModuleBase_IViewWindow* theWnd,
96                                                   QMouseEvent* theEvent)
97 {
98   // the contex menu release by the right button should not be processed by this widget
99   if (theEvent->button() != Qt::LeftButton)
100     return;
101
102   if (mySpinBox->hasVariable())
103     return;
104
105   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
106
107   double aX, aY;
108   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
109
110   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
111   setPoint(feature(), aPnt);
112
113   // if the validator of the control returns false, focus should not be switched
114   if (getError(false).isEmpty())
115     emit focusOutWidget(this);
116 }
117
118 void PartSet_WidgetPoint2dDistance::mouseMoved(ModuleBase_IViewWindow* theWnd,
119                                                QMouseEvent* theEvent)
120 {
121   if (isEditingMode())
122     return;
123
124   if (mySpinBox->hasVariable())
125     return;
126
127   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
128
129   double aX, aY;
130   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
131
132   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
133   if (myState != ModifiedInViewer)
134     storeCurentValue();
135
136   bool isBlocked = blockValueState(true);
137   setPoint(feature(), aPnt);
138   blockValueState(isBlocked);
139   setValueState(ModifiedInViewer);
140 }
141
142 void PartSet_WidgetPoint2dDistance::storeCurentValue()
143 {
144   // do not use cash if a variable is used
145   if (mySpinBox->hasVariable())
146     return;
147
148   myValueIsCashed = true;
149   myIsFeatureVisibleInCash = XGUI_Displayer::isVisible(
150                        XGUI_Tools::workshop(myWorkshop)->displayer(), myFeature);
151   myValueInCash = mySpinBox->value();
152 }
153
154 bool PartSet_WidgetPoint2dDistance::restoreCurentValue()
155 {
156   bool aRestoredAndHidden = true;
157
158   bool isVisible = myIsFeatureVisibleInCash;
159   // fill the control widgets by the cashed value
160
161   myValueIsCashed = false;
162   myIsFeatureVisibleInCash = true;
163   ModuleBase_Tools::setSpinValue(mySpinBox, myValueInCash);
164
165   // store value to the model
166   storeValueCustom();
167   if (isVisible) {
168     setValueState(Stored);
169     aRestoredAndHidden = false;
170   }
171   else
172     aRestoredAndHidden = true;
173
174   return aRestoredAndHidden;
175 }
176
177 bool PartSet_WidgetPoint2dDistance::processEnter()
178 {
179   bool isModified = getValueState() == ModifiedInPP;
180   if (isModified) {
181     emit valuesChanged();
182     mySpinBox->selectAll();
183   }
184   return isModified;
185 }