Salome HOME
Issue #718 - Translation with parameters - wrong coordinates
[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 #include <PartSet_Module.h>
10 #include <PartSet_LockApplyMgr.h>
11
12 #include <ModuleBase_ParamSpinBox.h>
13 #include <ModuleBase_Tools.h>
14 #include <ModuleBase_IViewer.h>
15 #include <ModuleBase_IViewWindow.h>
16 #include <ModuleBase_ISelection.h>
17
18 #include <Config_Keywords.h>
19 #include <Config_WidgetAPI.h>
20
21 #include <Events_Loop.h>
22 #include <ModelAPI_Events.h>
23
24 #include <ModelAPI_Feature.h>
25 #include <ModelAPI_Data.h>
26 #include <ModelAPI_Object.h>
27 #include <GeomDataAPI_Point2D.h>
28 #include <GeomAPI_Pnt2d.h>
29
30 #include <SketchPlugin_Feature.h>
31 #include <SketchPlugin_ConstraintCoincidence.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                                              ModuleBase_IWorkshop* theWorkshop,
52                                              const Config_WidgetAPI* theData,
53                                              const std::string& theParentId)
54  : ModuleBase_ModelWidget(theParent, theData, theParentId), myWorkshop(theWorkshop)
55 {
56   myLockApplyMgr = new PartSet_LockApplyMgr(theParent, myWorkshop);
57
58   // the control should accept the focus, so the boolen flag is corrected to be true
59   myIsObligatory = true;
60   //myOptionParam = theData->getProperty(PREVIOUS_FEATURE_PARAM);
61   QString aPageName = QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME));
62   myGroupBox = new QGroupBox(aPageName, theParent);
63   myGroupBox->setFlat(false);
64
65   QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
66   ModuleBase_Tools::adjustMargins(aGroupLay);
67   aGroupLay->setColumnStretch(1, 1);
68   {
69     QLabel* aLabel = new QLabel(myGroupBox);
70     aLabel->setText(tr("X"));
71     aLabel->setPixmap(QPixmap(":pictures/x_point.png"));
72     aGroupLay->addWidget(aLabel, 0, 0);
73
74     myXSpin = new ModuleBase_ParamSpinBox(myGroupBox);
75     myXSpin->setMinimum(-DBL_MAX);
76     myXSpin->setMaximum(DBL_MAX);
77     myXSpin->setToolTip(tr("X"));
78     aGroupLay->addWidget(myXSpin, 0, 1);
79
80     connect(myXSpin, SIGNAL(valueChanged(const QString&)), this, SLOT(onValuesChanged()));
81   }
82   {
83     QLabel* aLabel = new QLabel(myGroupBox);
84     aLabel->setText(tr("Y"));
85     aLabel->setPixmap(QPixmap(":pictures/y_point.png"));
86     aGroupLay->addWidget(aLabel, 1, 0);
87
88     myYSpin = new ModuleBase_ParamSpinBox(myGroupBox);
89     myYSpin->setMinimum(-DBL_MAX);
90     myYSpin->setMaximum(DBL_MAX);
91     myYSpin->setToolTip(tr("Y"));
92     aGroupLay->addWidget(myYSpin, 1, 1);
93
94     connect(myYSpin, SIGNAL(valueChanged(const QString&)), this, SLOT(onValuesChanged()));
95   }
96   QVBoxLayout* aLayout = new QVBoxLayout(this);
97   ModuleBase_Tools::zeroMargins(aLayout);
98   aLayout->addWidget(myGroupBox);
99   setLayout(aLayout);
100 }
101
102 bool PartSet_WidgetPoint2D::reset()
103 {
104   bool aDone = false;
105   if (!isUseReset() || isComputedDefault() || myXSpin->hasVariable() || myYSpin->hasVariable()) {
106     aDone = false;
107   }
108   else {
109     bool isOk;
110     double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
111     // it is important to block the spin box control in order to do not through out the
112     // locking of the validating state.
113     ModuleBase_Tools::setSpinValue(myXSpin, isOk ? aDefValue : 0.0);
114     ModuleBase_Tools::setSpinValue(myYSpin, isOk ? aDefValue : 0.0);
115     storeValueCustom();
116     aDone = true;
117   }
118   return aDone;
119 }
120
121 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
122 {
123 }
124
125 bool PartSet_WidgetPoint2D::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
126                                          const bool theToValidate)
127 {
128   if (theValues.empty())
129     return false;
130
131   ModuleBase_ViewerPrs aValue = theValues.takeFirst();
132
133   Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
134   bool isDone = false;
135   TopoDS_Shape aShape = aValue.shape();
136   double aX, aY;
137   if (getPoint2d(aView, aShape, aX, aY)) {
138     isDone = setPoint(aX, aY);
139   }
140   return isDone;
141 }
142
143 bool PartSet_WidgetPoint2D::setPoint(double theX, double theY)
144 {
145   if (fabs(theX) >= MaxCoordinate)
146     return false;
147   if (fabs(theY) >= MaxCoordinate)
148     return false;
149
150   ModuleBase_Tools::setSpinValue(myXSpin, theX);
151   ModuleBase_Tools::setSpinValue(myYSpin, theY);
152
153   storeValue();
154   return true;
155 }
156
157 bool PartSet_WidgetPoint2D::storeValueCustom() const
158 {
159   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
160   if (!aData) // can be on abort of sketcher element
161     return false;
162   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
163       aData->attribute(attributeID()));
164   
165   PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
166   bool isBlocked = that->blockSignals(true);
167   bool isImmutable = aPoint->setImmutable(true);
168
169   // if text is not empty then setValue will be ignored
170   // so we should set the text at first
171   aPoint->setText(myXSpin->hasVariable() ? myXSpin->text().toStdString() : "",
172                   myYSpin->hasVariable() ? myYSpin->text().toStdString() : "");
173   aPoint->setValue(!myXSpin->hasVariable() ? myXSpin->value() : aPoint->x(),
174                    !myYSpin->hasVariable() ? myYSpin->value() : aPoint->y());
175
176   // after movement the solver will call the update event: optimization
177   moveObject(myFeature);
178   aPoint->setImmutable(isImmutable);
179   that->blockSignals(isBlocked);
180
181   return true;
182 }
183
184 bool PartSet_WidgetPoint2D::restoreValue()
185 {
186   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
187   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
188       aData->attribute(attributeID()));
189   QString aTextX = QString::fromStdString(aPoint->textX());
190   QString aTextY = QString::fromStdString(aPoint->textY());
191
192   bool isDouble = false;
193   double aVal = 0;
194   if (aTextX.isEmpty()) {
195     ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
196   } else {
197     aVal = aTextX.toDouble(&isDouble);
198     if (isDouble)
199       ModuleBase_Tools::setSpinValue(myXSpin, aVal);
200     else
201       ModuleBase_Tools::setSpinText(myXSpin, aTextX);
202   }
203   if (aTextY.isEmpty()) {
204     ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
205   } else {
206     aVal = aTextY.toDouble(&isDouble);
207     if (isDouble)
208       ModuleBase_Tools::setSpinValue(myYSpin, aVal);
209     else
210       ModuleBase_Tools::setSpinText(myYSpin, aTextY);
211   }
212   //if (aTextX.empty() || aTextY.empty()) {
213   //  ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
214   //  ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
215   //} else {
216   //  ModuleBase_Tools::setSpinText(myXSpin, QString::fromStdString(aTextX));
217   //  ModuleBase_Tools::setSpinText(myYSpin, QString::fromStdString(aTextY));
218   //}
219   return true;
220 }
221
222 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
223 {
224   QList<QWidget*> aControls;
225   aControls.append(myXSpin);
226   aControls.append(myYSpin);
227   return aControls;
228 }
229
230
231 void PartSet_WidgetPoint2D::activateCustom()
232 {
233   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
234   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
235           this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
236   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
237           this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
238
239   QIntList aModes;
240   aModes << TopAbs_VERTEX;
241   aModes << TopAbs_EDGE;
242   myWorkshop->activateSubShapesSelection(aModes);
243
244   myLockApplyMgr->activate();
245 }
246
247 void PartSet_WidgetPoint2D::deactivate()
248 {
249   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
250   disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
251              this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
252   disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
253              this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
254
255   myWorkshop->deactivateSubShapesSelection();
256
257   myLockApplyMgr->deactivate();
258 }
259
260 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView, 
261                                        const TopoDS_Shape& theShape, 
262                                        double& theX, double& theY) const
263 {
264   if (!theShape.IsNull()) {
265     if (theShape.ShapeType() == TopAbs_VERTEX) {
266       const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
267       if (!aVertex.IsNull()) {
268         // A case when point is taken from existing vertex
269         gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
270         PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
271         return true;
272       }
273     }
274   }
275   return false;
276 }
277
278 void PartSet_WidgetPoint2D::setConstraintWith(const ObjectPtr& theObject)
279 {
280   // Create point-edge coincedence
281   FeaturePtr aFeature = mySketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
282   std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
283
284   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
285       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
286   AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
287   std::shared_ptr<GeomDataAPI_Point2D> aThisPoint = 
288     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
289   aRef1->setAttr(aThisPoint);
290
291   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
292       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
293   aRef2->setObject(theObject);
294
295   aFeature->execute();
296 }
297
298 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
299 {
300   // the contex menu release by the right button should not be processed by this widget
301   if (theEvent->button() != Qt::LeftButton)
302     return;
303
304   ModuleBase_ISelection* aSelection = myWorkshop->selection();
305   Handle(V3d_View) aView = theWnd->v3dView();
306   // TODO: This fragment doesn't work because bug in OCC Viewer. It can be used after fixing.
307   NCollection_List<TopoDS_Shape> aShapes;
308   std::list<ObjectPtr> aObjects;
309   aSelection->selectedShapes(aShapes, aObjects);
310   // if we have selection
311   if (aShapes.Extent() > 0) {
312     TopoDS_Shape aShape = aShapes.First();
313     ObjectPtr aObject = aObjects.front();
314     FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aObject);
315     if (aSelectedFeature.get() != NULL) {
316       std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
317               std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
318       if ((!aSPFeature) && (!aShape.IsNull())) {
319         ResultPtr aFixedObject = PartSet_Tools::findFixedObjectByExternal(aShape, aObject, mySketch);
320         if (!aFixedObject.get())
321           aObject = PartSet_Tools::createFixedObjectByExternal(aShape, aObject, mySketch);
322         setConstraintWith(aObject);
323         emit vertexSelected();
324         emit focusOutWidget(this);
325         return;
326       }
327     }
328     double aX, aY;
329     if (getPoint2d(aView, aShape, aX, aY)) {
330       PartSet_Tools::setConstraints(mySketch, feature(), attributeID(),aX, aY);
331       emit vertexSelected();
332       emit focusOutWidget(this);
333       return;
334     } else if (aShape.ShapeType() == TopAbs_EDGE) {
335       setConstraintWith(aObject);
336       emit vertexSelected();
337       emit focusOutWidget(this);
338       return;
339     }
340   }
341   // End of Bug dependent fragment
342
343   // A case when point is taken from mouse event
344   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
345   double aX, anY;
346   PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
347   if (!setPoint(aX, anY))
348     return;
349
350   /// Start alternative code
351   //std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
352   //    GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
353   //QList<FeaturePtr> aIgnore;
354   //aIgnore.append(feature());
355
356   //double aTolerance = aView->Convert(7);
357   //std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt = 
358   //  PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
359   //if (aAttrPnt.get() != NULL) {
360   //  aFeaturePoint->setValue(aAttrPnt->pnt());
361   //  PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
362   //  emit vertexSelected();
363   //}
364   /// End alternative code
365   emit focusOutWidget(this);
366 }
367
368
369 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
370 {
371   if (isEditingMode())
372     return;
373
374   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
375
376   double aX, anY;
377   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
378   setPoint(aX, anY);
379 }
380
381 double PartSet_WidgetPoint2D::x() const
382 {
383   return myXSpin->value();
384 }
385
386 double PartSet_WidgetPoint2D::y() const
387 {
388   return myYSpin->value();
389 }
390
391 void PartSet_WidgetPoint2D::onValuesChanged()
392 {
393   myLockApplyMgr->valuesChanged();
394   emit valuesChanged();
395 }