Salome HOME
Issue #1787 : code's adjusting
[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 #include <ModuleBase_LabelValue.h>
21
22 #include <GeomAPI_Pnt2d.h>
23 #include <Config_WidgetAPI.h>
24 #include <GeomDataAPI_Point2D.h>
25
26 #include <ModelAPI_Data.h>
27 #include <ModelAPI_AttributeDouble.h>
28
29 #include <QMouseEvent>
30
31 PartSet_WidgetPoint2dDistance::PartSet_WidgetPoint2dDistance(QWidget* theParent,
32                                                              ModuleBase_IWorkshop* theWorkshop,
33                                                              const Config_WidgetAPI* theData)
34 : ModuleBase_WidgetLabelValue(theParent, theData), myWorkshop(theWorkshop),
35   myValueIsCashed(false), myIsFeatureVisibleInCash(true), myValueInCash(0)
36 {
37   myFirstPntName = theData->getProperty("first_point");
38   myWidgetValidator = new ModuleBase_WidgetValidator(this, myWorkshop);
39 }
40
41 PartSet_WidgetPoint2dDistance::~PartSet_WidgetPoint2dDistance()
42 {
43 }
44
45 bool PartSet_WidgetPoint2dDistance::isValidSelectionCustom(
46                                       const std::shared_ptr<ModuleBase_ViewerPrs>& theValue)
47 {
48   return false;
49 }
50
51 bool PartSet_WidgetPoint2dDistance::resetCustom()
52 {
53   bool aDone = false;
54   if (!isUseReset() || isComputedDefault() /*|| mySpinBox->hasVariable()*/) {
55     aDone = false;
56   }
57   else {
58     if (myValueIsCashed) {
59       // if the restored value should be hidden, aDone = true to set
60       // reset state for the widget in the parent
61       aDone = restoreCurentValue();
62       emit objectUpdated();
63     }
64     else {
65       bool isOk;
66       double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
67       // it is important to block the spin box control in order to do not through out the
68       // locking of the validating state.
69       myLabel->setValue(isOk ? aDefValue : 0.0);
70       storeValue();
71       aDone = true;
72     }
73   }
74   return aDone;
75 }
76
77 void PartSet_WidgetPoint2dDistance::setPoint(FeaturePtr theFeature,
78                                              const std::shared_ptr<GeomAPI_Pnt2d>& thePnt)
79 {
80   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
81   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
82       aData->attribute(myFirstPntName));
83   if (!aPoint)
84     return;
85
86   double aValue = computeValue(aPoint->pnt(), thePnt);
87   AttributeDoublePtr aReal = aData->real(attributeID());
88   if (aReal && (aReal->value() != aValue)) {
89     aReal->setValue(aValue);
90
91     myLabel->setValue(aValue);
92     storeValue();
93   }
94 }
95
96 double PartSet_WidgetPoint2dDistance::computeValue(
97                               const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPnt,
98                               const std::shared_ptr<GeomAPI_Pnt2d>& theCurrentPnt)
99 {
100   return theCurrentPnt->distance(theFirstPnt);
101 }
102
103 void PartSet_WidgetPoint2dDistance::mouseReleased(ModuleBase_IViewWindow* theWnd,
104                                                   QMouseEvent* theEvent)
105 {
106   // the contex menu release by the right button should not be processed by this widget
107   if (theEvent->button() != Qt::LeftButton)
108     return;
109
110   //if (mySpinBox->hasVariable())
111   //  return;
112
113   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
114
115   double aX, aY;
116   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
117
118   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
119   setPoint(feature(), aPnt);
120
121   // if the validator of the control returns false, focus should not be switched
122   if (getError(false).isEmpty())
123     emit focusOutWidget(this);
124 }
125
126 void PartSet_WidgetPoint2dDistance::mouseMoved(ModuleBase_IViewWindow* theWnd,
127                                                QMouseEvent* theEvent)
128 {
129   if (isEditingMode())
130     return;
131
132   //if (mySpinBox->hasVariable())
133   //  return;
134
135   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
136
137   double aX, aY;
138   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
139
140   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
141   if (myState != ModifiedInViewer)
142     storeCurentValue();
143
144   bool isBlocked = blockValueState(true);
145   setPoint(feature(), aPnt);
146   blockValueState(isBlocked);
147   setValueState(ModifiedInViewer);
148 }
149
150 void PartSet_WidgetPoint2dDistance::storeCurentValue()
151 {
152   // do not use cash if a variable is used
153   //if (mySpinBox->hasVariable())
154   //  return;
155
156   myValueIsCashed = true;
157   myIsFeatureVisibleInCash = XGUI_Displayer::isVisible(
158                        XGUI_Tools::workshop(myWorkshop)->displayer(), myFeature);
159   myValueInCash = myLabel->value();
160 }
161
162 bool PartSet_WidgetPoint2dDistance::restoreCurentValue()
163 {
164   bool aRestoredAndHidden = true;
165
166   bool isVisible = myIsFeatureVisibleInCash;
167   // fill the control widgets by the cashed value
168
169   myValueIsCashed = false;
170   myIsFeatureVisibleInCash = true;
171   myLabel->setValue(myValueInCash);
172
173   // store value to the model
174   storeValueCustom();
175   if (isVisible) {
176     setValueState(Stored);
177     aRestoredAndHidden = false;
178   }
179   else
180     aRestoredAndHidden = true;
181
182   return aRestoredAndHidden;
183 }
184
185 bool PartSet_WidgetPoint2dDistance::processEnter()
186 {
187   return false;
188   /*
189   bool isModified = getValueState() == ModifiedInPP;
190   if (isModified) {
191     emit valuesChanged();
192     //mySpinBox->selectAll();
193   }
194   return isModified;*/
195 }
196
197 bool PartSet_WidgetPoint2dDistance::storeValueCustom()
198 {
199   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
200   if (!aData) // can be on abort of sketcher element
201     return false;
202   AttributeDoublePtr anAttribute = myFeature->data()->real(attributeID());
203   anAttribute->setValue(myLabel->value());
204
205   // after movement the solver will call the update event: optimization
206   updateObject(myFeature);
207
208   return true;
209 }