Salome HOME
#1404 Random crash with Shaper: AIS presentations: operation prs, result sketch prs...
[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::activateCustom()
95 {
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*)));
101 }
102
103 void PartSet_WidgetPoint2dDistance::deactivate()
104 {
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*)));
111 }
112
113 void PartSet_WidgetPoint2dDistance::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
114 {
115   // the contex menu release by the right button should not be processed by this widget
116   if (theEvent->button() != Qt::LeftButton)
117     return;
118
119   if (mySpinBox->hasVariable())
120     return;
121
122   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
123
124   double aX, aY;
125   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
126
127   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
128   setPoint(feature(), aPnt);
129
130   // if the validator of the control returns false, focus should not be switched
131   if (getError().isEmpty())
132     emit focusOutWidget(this);
133 }
134
135 void PartSet_WidgetPoint2dDistance::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
136 {
137   if (isEditingMode())
138     return;
139
140   if (mySpinBox->hasVariable())
141     return;
142
143   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
144
145   double aX, aY;
146   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
147
148   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
149   if (myState != ModifiedInViewer)
150     storeCurentValue();
151
152   bool isBlocked = blockValueState(true);
153   setPoint(feature(), aPnt);
154   blockValueState(isBlocked);
155   setValueState(ModifiedInViewer);
156 }
157
158 void PartSet_WidgetPoint2dDistance::storeCurentValue()
159 {
160   // do not use cash if a variable is used
161   if (mySpinBox->hasVariable())
162     return;
163
164   myValueIsCashed = true;
165   myIsFeatureVisibleInCash = XGUI_Displayer::isVisible(
166                        XGUI_Tools::workshop(myWorkshop)->displayer(), myFeature);
167   myValueInCash = mySpinBox->value();
168 }
169
170 bool PartSet_WidgetPoint2dDistance::restoreCurentValue()
171 {
172   bool aRestoredAndHidden = true;
173
174   bool isVisible = myIsFeatureVisibleInCash;
175   // fill the control widgets by the cashed value
176
177   myValueIsCashed = false;
178   myIsFeatureVisibleInCash = true;
179   ModuleBase_Tools::setSpinValue(mySpinBox, myValueInCash);
180
181   // store value to the model
182   storeValueCustom();
183   if (isVisible) {
184     setValueState(Stored);
185     aRestoredAndHidden = false;
186   }
187   else
188     aRestoredAndHidden = true;
189
190   return aRestoredAndHidden;
191 }
192
193 bool PartSet_WidgetPoint2dDistance::processEnter()
194 {
195   bool isModified = getValueState() == ModifiedInPP;
196   if (isModified) {
197     emit valuesChanged();
198     mySpinBox->selectAll();
199   }
200   return isModified;
201 }