Salome HOME
f3991587b375a6180473b803b6d5efe74d197e1b
[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     }
62     else
63       aDone = ModuleBase_WidgetDoubleValue::resetCustom();
64   }
65   return aDone;
66 }
67
68 void PartSet_WidgetPoint2dDistance::setPoint(FeaturePtr theFeature,
69                                              const std::shared_ptr<GeomAPI_Pnt2d>& thePnt)
70 {
71   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
72   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
73       aData->attribute(myFirstPntName));
74   if (!aPoint)
75     return;
76
77   double aValue = computeValue(aPoint->pnt(), thePnt);
78   AttributeDoublePtr aReal = aData->real(attributeID());
79   if (aReal && (aReal->value() != aValue)) {
80     aReal->setValue(aValue);
81     
82     ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
83     storeValue();
84   }
85 }
86
87 double PartSet_WidgetPoint2dDistance::computeValue(const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPnt,
88                                                    const std::shared_ptr<GeomAPI_Pnt2d>& theCurrentPnt)
89 {
90   return theCurrentPnt->distance(theFirstPnt);
91 }
92
93 void PartSet_WidgetPoint2dDistance::activateCustom()
94 {
95   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
96   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
97           this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
98   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
99           this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
100 }
101
102 void PartSet_WidgetPoint2dDistance::deactivate()
103 {
104   ModuleBase_ModelWidget::deactivate();
105   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
106   disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
107              this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
108   disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
109              this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
110 }
111
112 void PartSet_WidgetPoint2dDistance::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
113 {
114   // the contex menu release by the right button should not be processed by this widget
115   if (theEvent->button() != Qt::LeftButton)
116     return;
117
118   if (mySpinBox->hasVariable())
119     return;
120
121   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
122
123   double aX, aY;
124   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
125
126   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
127   setPoint(feature(), aPnt);
128
129   // if the validator of the control returns false, focus should not be switched
130   if (getError().isEmpty())
131     emit focusOutWidget(this);
132 }
133
134 void PartSet_WidgetPoint2dDistance::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
135 {
136   if (isEditingMode())
137     return;
138
139   if (mySpinBox->hasVariable())
140     return;
141
142   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
143
144   double aX, aY;
145   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
146
147   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
148   if (myState != ModifiedInViewer)
149     storeCurentValue();
150
151   bool isBlocked = blockValueState(true);
152   setPoint(feature(), aPnt);
153   blockValueState(isBlocked);
154   setValueState(ModifiedInViewer);
155 }
156
157 void PartSet_WidgetPoint2dDistance::storeCurentValue()
158 {
159   // do not use cash if a variable is used
160   if (mySpinBox->hasVariable())
161     return;
162
163   myValueIsCashed = true;
164   myIsFeatureVisibleInCash = XGUI_Displayer::isVisible(
165                        XGUI_Tools::workshop(myWorkshop)->displayer(), myFeature);
166   myValueInCash = mySpinBox->value();
167 }
168
169 bool PartSet_WidgetPoint2dDistance::restoreCurentValue()
170 {
171   bool aRestoredAndHidden = true;
172
173   bool isVisible = myIsFeatureVisibleInCash;
174   // fill the control widgets by the cashed value
175
176   myValueIsCashed = false;
177   myIsFeatureVisibleInCash = true;
178   ModuleBase_Tools::setSpinValue(mySpinBox, myValueInCash);
179
180   // store value to the model
181   storeValueCustom();
182   if (isVisible) {
183     setValueState(Stored);
184     aRestoredAndHidden = false;
185   }
186   else
187     aRestoredAndHidden = true;
188
189   return aRestoredAndHidden;
190 }
191
192 bool PartSet_WidgetPoint2dDistance::processEnter()
193 {
194   bool isModified = getValueState() == ModifiedInPP;
195   if (isModified) {
196     emit valuesChanged();
197     mySpinBox->selectAll();
198   }
199   return isModified;
200 }