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