]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_WidgetPoint2d.cpp
Salome HOME
Issue #394 Undo-ing a Sketch element
[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 void PartSet_WidgetPoint2D::reset()
96 {
97   bool isOk;
98   double aDefValue = QString::fromStdString(myDefaultValue).toDouble(&isOk);
99
100   myXSpin->setValue(isOk ? aDefValue : 0.0);
101   myYSpin->setValue(isOk ? aDefValue : 0.0);
102 }
103
104 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
105 {
106 }
107
108 bool PartSet_WidgetPoint2D::setSelection(ModuleBase_ViewerPrs theValue)
109 {
110   Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
111   bool isDone = false;
112   TopoDS_Shape aShape = theValue.shape();
113   double aX, aY;
114   if (getPoint2d(aView, aShape, aX, aY)) {
115     isDone = setPoint(aX, aY);
116   }
117   return isDone;
118 }
119
120 bool PartSet_WidgetPoint2D::setPoint(double theX, double theY)
121 {
122   if (fabs(theX) >= MaxCoordinate)
123     return false;
124   if (fabs(theY) >= MaxCoordinate)
125     return false;
126   bool isBlocked = this->blockSignals(true);
127   myXSpin->blockSignals(true);
128   myXSpin->setValue(theX);
129   myXSpin->blockSignals(false);
130
131   myYSpin->blockSignals(true);
132   myYSpin->setValue(theY);
133   myYSpin->blockSignals(false);
134   this->blockSignals(isBlocked);
135
136   storeValue();
137   return true;
138 }
139
140 bool PartSet_WidgetPoint2D::storeValue() const
141 {
142   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
143   if (!aData) // can be on abort of sketcher element
144     return false;
145   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
146       aData->attribute(attributeID()));
147   
148   PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
149   bool isBlocked = that->blockSignals(true);
150   bool isImmutable = aPoint->setImmutable(true);
151 #ifdef _DEBUG
152   std::string _attr_name = myAttributeID;
153   double _X = myXSpin->value();
154   double _Y = myYSpin->value();
155 #endif
156   aPoint->setValue(myXSpin->value(), myYSpin->value());
157   // after movement the solver will call the update event: optimization
158   moveObject(myFeature);
159   aPoint->setImmutable(isImmutable);
160   that->blockSignals(isBlocked);
161
162   return true;
163 }
164
165 bool PartSet_WidgetPoint2D::restoreValue()
166 {
167   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
168   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
169       aData->attribute(attributeID()));
170
171 #ifdef _DEBUG
172   std::string _attr_name = myAttributeID;
173   double _X = aPoint->x();
174   double _Y = aPoint->y();
175 #endif
176   bool isBlocked = this->blockSignals(true);
177   myXSpin->blockSignals(true);
178   myXSpin->setValue(aPoint->x());
179   myXSpin->blockSignals(false);
180
181   myYSpin->blockSignals(true);
182   myYSpin->setValue(aPoint->y());
183   myYSpin->blockSignals(false);
184   this->blockSignals(isBlocked);
185   return true;
186 }
187
188 QWidget* PartSet_WidgetPoint2D::getControl() const
189 {
190   return myGroupBox;
191 }
192
193 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
194 {
195   QList<QWidget*> aControls;
196   aControls.append(myXSpin);
197   aControls.append(myYSpin);
198   return aControls;
199 }
200
201
202 void PartSet_WidgetPoint2D::activateCustom()
203 {
204   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
205   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
206           this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
207   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
208           this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
209
210   QIntList aModes;
211   aModes << TopAbs_VERTEX;
212   myWorkshop->moduleConnector()->activateSubShapesSelection(aModes);
213 }
214
215 void PartSet_WidgetPoint2D::deactivate()
216 {
217   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
218   disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
219              this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
220   disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
221              this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
222   myWorkshop->moduleConnector()->deactivateSubShapesSelection();
223   myWorkshop->operationMgr()->setLockValidating(false);
224 }
225
226 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView, 
227                                        const TopoDS_Shape& theShape, 
228                                        double& theX, double& theY) const
229 {
230   if (!theShape.IsNull()) {
231     if (theShape.ShapeType() == TopAbs_VERTEX) {
232       const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
233       if (!aVertex.IsNull()) {
234         // A case when point is taken from existing vertex
235         gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
236         PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
237         return true;
238       }
239     }
240   }
241   return false;
242 }
243
244
245 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
246 {
247   XGUI_Selection* aSelection = myWorkshop->selector()->selection();
248   Handle(V3d_View) aView = theWnd->v3dView();
249   // TODO: This fragment doesn't work because bug in OCC Viewer. It can be used after fixing.
250   NCollection_List<TopoDS_Shape> aShapes;
251   std::list<ObjectPtr> aObjects;
252   aSelection->selectedShapes(aShapes, aObjects);
253   // if we have selection
254   if (aShapes.Extent() > 0) {
255     TopoDS_Shape aShape = aShapes.First();
256     ObjectPtr aObject = aObjects.front();
257     FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aObject);
258     if (aSelectedFeature.get() != NULL) {
259       std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
260               std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
261       if ((!aSPFeature) && (!aShape.IsNull()))
262         PartSet_Tools::createFixedObjectByExternal(aShape, aObject, mySketch);
263     }
264     double aX, aY;
265     if (getPoint2d(aView, aShape, aX, aY)) {
266       setPoint(aX, aY);
267
268       PartSet_Tools::setConstraints(mySketch, feature(), attributeID(),aX, aY);
269       emit vertexSelected();
270       emit focusOutWidget(this);
271       return;
272     }
273   }
274   // End of Bug dependent fragment
275
276   // A case when point is taken from mouse event
277   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
278   double aX, anY;
279   PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
280   if (!setPoint(aX, anY))
281     return;
282
283   /// Start alternative code
284   //std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
285   //    GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
286   //QList<FeaturePtr> aIgnore;
287   //aIgnore.append(feature());
288
289   //double aTolerance = aView->Convert(7);
290   //std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt = 
291   //  PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
292   //if (aAttrPnt.get() != NULL) {
293   //  aFeaturePoint->setValue(aAttrPnt->pnt());
294   //  PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
295   //  emit vertexSelected();
296   //}
297   /// End alternative code
298   emit focusOutWidget(this);
299 }
300
301
302 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
303 {
304   if (isEditingMode())
305     return;
306   myWorkshop->operationMgr()->setLockValidating(true);
307   // the Ok button should be disabled in the property panel by moving the mouse point in the viewer
308   // this leads that the user does not try to click Ok and it avoids an incorrect situation that the 
309   // line is moved to the cursor to the Ok button
310   myWorkshop->operationMgr()->setApplyEnabled(false);
311
312   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
313
314   double aX, anY;
315   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
316   setPoint(aX, anY);
317 }
318
319 double PartSet_WidgetPoint2D::x() const
320 {
321   return myXSpin->value();
322 }
323
324 double PartSet_WidgetPoint2D::y() const
325 {
326   return myYSpin->value();
327 }
328
329 void PartSet_WidgetPoint2D::onValuesChanged()
330 {
331   myWorkshop->operationMgr()->setLockValidating(false);
332   emit valuesChanged();
333 }