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