]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_WidgetPoint2dDistance.cpp
Salome HOME
#1277 SKETCH : Bad restitution coordinates in the point creation panel
[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                                                              const std::string& theParentId)
33 : ModuleBase_WidgetDoubleValue(theParent, theData, theParentId), myWorkshop(theWorkshop),
34   myValueIsCashed(false), myIsFeatureVisibleInCash(true), myValueInCash(0)
35 {
36   myFirstPntName = theData->getProperty("first_point");
37 }
38
39 PartSet_WidgetPoint2dDistance::~PartSet_WidgetPoint2dDistance()
40 {
41 }
42
43 bool PartSet_WidgetPoint2dDistance::resetCustom()
44 {
45   bool aDone = false;
46   if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
47     aDone = false;
48   }
49   else {
50     if (myValueIsCashed) {
51       // if the restored value should be hidden, aDone = true to set
52       // reset state for the widget in the parent
53       aDone = restoreCurentValue();
54     }
55     else
56       aDone = ModuleBase_WidgetDoubleValue::resetCustom();
57   }
58   return aDone;
59 }
60
61 void PartSet_WidgetPoint2dDistance::setPoint(FeaturePtr theFeature,
62                                              const std::shared_ptr<GeomAPI_Pnt2d>& thePnt)
63 {
64   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
65   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
66       aData->attribute(myFirstPntName));
67   if (!aPoint)
68     return;
69
70   double aValue = computeValue(aPoint->pnt(), thePnt);
71   AttributeDoublePtr aReal = aData->real(attributeID());
72   if (aReal && (aReal->value() != aValue)) {
73     aReal->setValue(aValue);
74     
75     ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
76     storeValue();
77   }
78 }
79
80 double PartSet_WidgetPoint2dDistance::computeValue(const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPnt,
81                                                    const std::shared_ptr<GeomAPI_Pnt2d>& theCurrentPnt)
82 {
83   return theCurrentPnt->distance(theFirstPnt);
84 }
85
86 void PartSet_WidgetPoint2dDistance::activateCustom()
87 {
88   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
89   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
90           this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
91   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
92           this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
93 }
94
95 void PartSet_WidgetPoint2dDistance::deactivate()
96 {
97   ModuleBase_ModelWidget::deactivate();
98   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
99   disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
100              this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
101   disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
102              this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
103 }
104
105 void PartSet_WidgetPoint2dDistance::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
106 {
107   // the contex menu release by the right button should not be processed by this widget
108   if (theEvent->button() != Qt::LeftButton)
109     return;
110
111   if (mySpinBox->hasVariable())
112     return;
113
114   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
115
116   double aX, aY;
117   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
118
119   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
120   setPoint(feature(), aPnt);
121
122   // if the validator of the control returns false, focus should not be switched
123   if (getError().isEmpty())
124     emit focusOutWidget(this);
125 }
126
127 void PartSet_WidgetPoint2dDistance::onMouseMove(ModuleBase_IViewWindow* theWnd, 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 = mySpinBox->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   ModuleBase_Tools::setSpinValue(mySpinBox, 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   bool isModified = getValueState() == ModifiedInPP;
188   if (isModified) {
189     emit valuesChanged();
190     mySpinBox->selectAll();
191   }
192   return isModified;
193 }