]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_WidgetPoint2dDistance.cpp
Salome HOME
Preparations for Split operation. It includes:
[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(const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPnt,
89                                                    const std::shared_ptr<GeomAPI_Pnt2d>& theCurrentPnt)
90 {
91   return theCurrentPnt->distance(theFirstPnt);
92 }
93
94 void PartSet_WidgetPoint2dDistance::mouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
95 {
96   // the contex menu release by the right button should not be processed by this widget
97   if (theEvent->button() != Qt::LeftButton)
98     return;
99
100   if (mySpinBox->hasVariable())
101     return;
102
103   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
104
105   double aX, aY;
106   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
107
108   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
109   setPoint(feature(), aPnt);
110
111   // if the validator of the control returns false, focus should not be switched
112   if (getError(false).isEmpty())
113     emit focusOutWidget(this);
114 }
115
116 void PartSet_WidgetPoint2dDistance::mouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
117 {
118   if (isEditingMode())
119     return;
120
121   if (mySpinBox->hasVariable())
122     return;
123
124   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
125
126   double aX, aY;
127   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
128
129   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
130   if (myState != ModifiedInViewer)
131     storeCurentValue();
132
133   bool isBlocked = blockValueState(true);
134   setPoint(feature(), aPnt);
135   blockValueState(isBlocked);
136   setValueState(ModifiedInViewer);
137 }
138
139 void PartSet_WidgetPoint2dDistance::storeCurentValue()
140 {
141   // do not use cash if a variable is used
142   if (mySpinBox->hasVariable())
143     return;
144
145   myValueIsCashed = true;
146   myIsFeatureVisibleInCash = XGUI_Displayer::isVisible(
147                        XGUI_Tools::workshop(myWorkshop)->displayer(), myFeature);
148   myValueInCash = mySpinBox->value();
149 }
150
151 bool PartSet_WidgetPoint2dDistance::restoreCurentValue()
152 {
153   bool aRestoredAndHidden = true;
154
155   bool isVisible = myIsFeatureVisibleInCash;
156   // fill the control widgets by the cashed value
157
158   myValueIsCashed = false;
159   myIsFeatureVisibleInCash = true;
160   ModuleBase_Tools::setSpinValue(mySpinBox, myValueInCash);
161
162   // store value to the model
163   storeValueCustom();
164   if (isVisible) {
165     setValueState(Stored);
166     aRestoredAndHidden = false;
167   }
168   else
169     aRestoredAndHidden = true;
170
171   return aRestoredAndHidden;
172 }
173
174 bool PartSet_WidgetPoint2dDistance::processEnter()
175 {
176   bool isModified = getValueState() == ModifiedInPP;
177   if (isModified) {
178     emit valuesChanged();
179     mySpinBox->selectAll();
180   }
181   return isModified;
182 }