Salome HOME
Issue #332: Find point by coordinates comparison instead selection
[modules/shaper.git] / src / PartSet / PartSet_WidgetPoint2d.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_WidgetPoint2D.cpp
4 // Created:     25 Apr 2014
5 // Author:      Natalia ERMOLAEVA
6
7 #include "PartSet_WidgetPoint2d.h"
8 #include <PartSet_Tools.h>
9
10 #include <XGUI_Workshop.h>
11 #include <XGUI_ViewerProxy.h>
12 #include <XGUI_ModuleConnector.h>
13 #include <XGUI_SelectionMgr.h>
14 #include <XGUI_Selection.h>
15 #include <XGUI_PropertyPanel.h>
16 #include <XGUI_OperationMgr.h>
17
18 #include <ModuleBase_DoubleSpinBox.h>
19 #include <ModuleBase_Tools.h>
20 #include <ModuleBase_IViewWindow.h>
21
22 #include <Config_Keywords.h>
23 #include <Config_WidgetAPI.h>
24
25 #include <Events_Loop.h>
26 #include <ModelAPI_Events.h>
27
28 #include <ModelAPI_Feature.h>
29 #include <ModelAPI_Data.h>
30 #include <ModelAPI_Object.h>
31 #include <GeomDataAPI_Point2D.h>
32 #include <GeomAPI_Pnt2d.h>
33
34 #include <QGroupBox>
35 #include <QGridLayout>
36 #include <QLabel>
37 #include <QEvent>
38 #include <QMouseEvent>
39 #include <QApplication>
40
41 #include <TopoDS.hxx>
42 #include <TopoDS_Vertex.hxx>
43 #include <BRep_Tool.hxx>
44
45 #include <cfloat>
46 #include <climits>
47
48 PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent, 
49                                               const Config_WidgetAPI* theData,
50                                               const std::string& theParentId)
51     : ModuleBase_ModelWidget(theParent, theData, theParentId)
52 {
53   //myOptionParam = theData->getProperty(PREVIOUS_FEATURE_PARAM);
54   QString aPageName = QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME));
55   myGroupBox = new QGroupBox(aPageName, theParent);
56   myGroupBox->setFlat(false);
57
58   QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
59   ModuleBase_Tools::adjustMargins(aGroupLay);
60   aGroupLay->setColumnStretch(1, 1);
61   {
62     QLabel* aLabel = new QLabel(myGroupBox);
63     aLabel->setText(tr("X"));
64     aLabel->setPixmap(QPixmap(":pictures/x_point.png"));
65     aGroupLay->addWidget(aLabel, 0, 0);
66
67     myXSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
68     myXSpin->setMinimum(-DBL_MAX);
69     myXSpin->setMaximum(DBL_MAX);
70     myXSpin->setToolTip(tr("X"));
71     aGroupLay->addWidget(myXSpin, 0, 1);
72
73     connect(myXSpin, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
74   }
75   {
76     QLabel* aLabel = new QLabel(myGroupBox);
77     aLabel->setText(tr("Y"));
78     aLabel->setPixmap(QPixmap(":pictures/y_point.png"));
79     aGroupLay->addWidget(aLabel, 1, 0);
80
81     myYSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
82     myYSpin->setMinimum(-DBL_MAX);
83     myYSpin->setMaximum(DBL_MAX);
84     myYSpin->setToolTip(tr("Y"));
85     aGroupLay->addWidget(myYSpin, 1, 1);
86
87     connect(myYSpin, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
88   }
89 }
90
91 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
92 {
93 }
94
95 bool PartSet_WidgetPoint2D::setSelection(ModuleBase_ViewerPrs theValue)
96 {
97   Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
98   bool isDone = false;
99   TopoDS_Shape aShape = theValue.shape();
100   double aX, aY;
101   if (getPoint2d(aView, aShape, aX, aY)) {
102     setPoint(aX, aY);
103     isDone = true;
104   }
105   return isDone;
106 }
107
108 void PartSet_WidgetPoint2D::setPoint(double theX, double theY)
109 {
110
111   bool isBlocked = this->blockSignals(true);
112   myXSpin->blockSignals(true);
113   myXSpin->setValue(theX);
114   myXSpin->blockSignals(false);
115
116   myYSpin->blockSignals(true);
117   myYSpin->setValue(theY);
118   myYSpin->blockSignals(false);
119   this->blockSignals(isBlocked);
120
121   emit valuesChanged();
122 }
123
124 bool PartSet_WidgetPoint2D::storeValue() const
125 {
126   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
127   if (!aData) // can be on abort of sketcher element
128     return false;
129   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
130       aData->attribute(attributeID()));
131   
132   PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
133   bool isBlocked = that->blockSignals(true);
134   bool isImmutable = aPoint->setImmutable(true);
135 #ifdef _DEBUG
136   std::string _attr_name = myAttributeID;
137   double _X = myXSpin->value();
138   double _Y = myYSpin->value();
139 #endif
140   aPoint->setValue(myXSpin->value(), myYSpin->value());
141   // after movement the solver will call the update event: optimization
142   moveObject(myFeature);
143   aPoint->setImmutable(isImmutable);
144   that->blockSignals(isBlocked);
145
146   return true;
147 }
148
149 bool PartSet_WidgetPoint2D::restoreValue()
150 {
151   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
152   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
153       aData->attribute(attributeID()));
154
155 #ifdef _DEBUG
156   std::string _attr_name = myAttributeID;
157   double _X = aPoint->x();
158   double _Y = aPoint->y();
159 #endif
160   bool isBlocked = this->blockSignals(true);
161   myXSpin->blockSignals(true);
162   myXSpin->setValue(aPoint->x());
163   myXSpin->blockSignals(false);
164
165   myYSpin->blockSignals(true);
166   myYSpin->setValue(aPoint->y());
167   myYSpin->blockSignals(false);
168   this->blockSignals(isBlocked);
169   return true;
170 }
171
172 QWidget* PartSet_WidgetPoint2D::getControl() const
173 {
174   return myGroupBox;
175 }
176
177 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
178 {
179   QList<QWidget*> aControls;
180   aControls.append(myXSpin);
181   aControls.append(myYSpin);
182   return aControls;
183 }
184
185
186 void PartSet_WidgetPoint2D::activate()
187 {
188   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
189   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
190           this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
191   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
192           this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
193
194   QIntList aModes;
195   aModes << TopAbs_VERTEX;
196   myWorkshop->moduleConnector()->activateSubShapesSelection(aModes);
197 }
198
199 void PartSet_WidgetPoint2D::deactivate()
200 {
201   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
202   disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
203              this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
204   disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
205              this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
206   myWorkshop->moduleConnector()->deactivateSubShapesSelection();
207   myWorkshop->operationMgr()->setLockValidating(false);
208 }
209
210 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView, 
211                                        const TopoDS_Shape& theShape, 
212                                        double& theX, double& theY) const
213 {
214   if (!theShape.IsNull()) {
215     if (theShape.ShapeType() == TopAbs_VERTEX) {
216       const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
217       if (!aVertex.IsNull()) {
218         // A case when point is taken from existing vertex
219         gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
220         PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
221         return true;
222       }
223     }
224   }
225   return false;
226 }
227
228
229 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
230 {
231   XGUI_Selection* aSelection = myWorkshop->selector()->selection();
232   // TODO: This fragment doesn't work because bug in OCC Viewer. It can be used after fixing.
233   //NCollection_List<TopoDS_Shape> aShapes;
234   //std::list<ObjectPtr> aObjects;
235   //aSelection->selectedShapes(aShapes, aObjects);
236   //if (aShapes.Extent() > 0) {
237   //  TopoDS_Shape aShape = aShapes.First();
238   //  double aX, aY;
239   //  if (getPoint2d(theWnd->v3dView(), aShape, aX, aY)) {
240   //    setPoint(aX, aY);
241
242   //    PartSet_Tools::setConstraints(mySketch, feature(), attributeID(),aX, aY);
243   //    emit vertexSelected(aObjects.front(), aShape);
244   //    emit focusOutWidget(this);
245   //    return;
246   //  }
247   //}
248   // End of Bug dependent fragment
249
250   // A case when point is taken from mouse event
251   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
252   double aX, anY;
253   Handle(V3d_View) aView = theWnd->v3dView();
254   PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
255   setPoint(aX, anY);
256
257   std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
258       GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
259   QList<FeaturePtr> aIgnore;
260   aIgnore.append(feature());
261
262   double aTolerance = aView->Convert(4);
263   std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt = 
264     PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
265   if (aAttrPnt.get() != NULL) {
266
267     PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
268     emit vertexSelected();
269   }
270   emit focusOutWidget(this);
271 }
272
273
274 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
275 {
276   if (isEditingMode())
277     return;
278   myWorkshop->operationMgr()->setLockValidating(true);
279   myWorkshop->propertyPanel()->setOkEnabled(false);
280
281   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
282
283   double aX, anY;
284   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
285   setPoint(aX, anY);
286 }
287
288 double PartSet_WidgetPoint2D::x() const
289 {
290   return myXSpin->value();
291 }
292
293 double PartSet_WidgetPoint2D::y() const
294 {
295   return myYSpin->value();
296 }
297
298 void PartSet_WidgetPoint2D::onValuesChanged()
299 {
300   myWorkshop->operationMgr()->setLockValidating(false);
301   emit valuesChanged();
302 }