Salome HOME
Pipe validator fix
[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
20 #include <GeomAPI_Pnt2d.h>
21 #include <Config_WidgetAPI.h>
22 #include <GeomDataAPI_Point2D.h>
23
24 #include <ModelAPI_Data.h>
25 #include <ModelAPI_AttributeDouble.h>
26
27 #include <QMouseEvent>
28
29 PartSet_WidgetPoint2dDistance::PartSet_WidgetPoint2dDistance(QWidget* theParent,
30                                                              ModuleBase_IWorkshop* theWorkshop,
31                                                              const Config_WidgetAPI* theData)
32 : ModuleBase_WidgetDoubleValue(theParent, theData), myWorkshop(theWorkshop),
33   myValueIsCashed(false), myIsFeatureVisibleInCash(true), myValueInCash(0)
34 {
35   myFirstPntName = theData->getProperty("first_point");
36 }
37
38 PartSet_WidgetPoint2dDistance::~PartSet_WidgetPoint2dDistance()
39 {
40 }
41
42 bool PartSet_WidgetPoint2dDistance::resetCustom()
43 {
44   bool aDone = false;
45   if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
46     aDone = false;
47   }
48   else {
49     if (myValueIsCashed) {
50       // if the restored value should be hidden, aDone = true to set
51       // reset state for the widget in the parent
52       aDone = restoreCurentValue();
53     }
54     else
55       aDone = ModuleBase_WidgetDoubleValue::resetCustom();
56   }
57   return aDone;
58 }
59
60 void PartSet_WidgetPoint2dDistance::setPoint(FeaturePtr theFeature,
61                                              const std::shared_ptr<GeomAPI_Pnt2d>& thePnt)
62 {
63   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
64   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
65       aData->attribute(myFirstPntName));
66   if (!aPoint)
67     return;
68
69   double aValue = computeValue(aPoint->pnt(), thePnt);
70   AttributeDoublePtr aReal = aData->real(attributeID());
71   if (aReal && (aReal->value() != aValue)) {
72     aReal->setValue(aValue);
73     
74     ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
75     storeValue();
76   }
77 }
78
79 double PartSet_WidgetPoint2dDistance::computeValue(const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPnt,
80                                                    const std::shared_ptr<GeomAPI_Pnt2d>& theCurrentPnt)
81 {
82   return theCurrentPnt->distance(theFirstPnt);
83 }
84
85 void PartSet_WidgetPoint2dDistance::activateCustom()
86 {
87   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
88   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
89           this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
90   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
91           this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
92 }
93
94 void PartSet_WidgetPoint2dDistance::deactivate()
95 {
96   ModuleBase_ModelWidget::deactivate();
97   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
98   disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
99              this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
100   disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
101              this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
102 }
103
104 void PartSet_WidgetPoint2dDistance::onMouseRelease(ModuleBase_IViewWindow* theWnd, 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().isEmpty())
123     emit focusOutWidget(this);
124 }
125
126 void PartSet_WidgetPoint2dDistance::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
127 {
128   if (isEditingMode())
129     return;
130
131   if (mySpinBox->hasVariable())
132     return;
133
134   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
135
136   double aX, aY;
137   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
138
139   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
140   if (myState != ModifiedInViewer)
141     storeCurentValue();
142
143   bool isBlocked = blockValueState(true);
144   setPoint(feature(), aPnt);
145   blockValueState(isBlocked);
146   setValueState(ModifiedInViewer);
147 }
148
149 void PartSet_WidgetPoint2dDistance::storeCurentValue()
150 {
151   // do not use cash if a variable is used
152   if (mySpinBox->hasVariable())
153     return;
154
155   myValueIsCashed = true;
156   myIsFeatureVisibleInCash = XGUI_Displayer::isVisible(
157                        XGUI_Tools::workshop(myWorkshop)->displayer(), myFeature);
158   myValueInCash = mySpinBox->value();
159 }
160
161 bool PartSet_WidgetPoint2dDistance::restoreCurentValue()
162 {
163   bool aRestoredAndHidden = true;
164
165   bool isVisible = myIsFeatureVisibleInCash;
166   // fill the control widgets by the cashed value
167
168   myValueIsCashed = false;
169   myIsFeatureVisibleInCash = true;
170   ModuleBase_Tools::setSpinValue(mySpinBox, myValueInCash);
171
172   // store value to the model
173   storeValueCustom();
174   if (isVisible) {
175     setValueState(Stored);
176     aRestoredAndHidden = false;
177   }
178   else
179     aRestoredAndHidden = true;
180
181   return aRestoredAndHidden;
182 }
183
184 bool PartSet_WidgetPoint2dDistance::processEnter()
185 {
186   bool isModified = getValueState() == ModifiedInPP;
187   if (isModified) {
188     emit valuesChanged();
189     mySpinBox->selectAll();
190   }
191   return isModified;
192 }