Salome HOME
Bug #619: Sketch plane rotation.
[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 {
127   if (theValues.empty())
128     return false;
129
130   ModuleBase_ViewerPrs aValue = theValues.takeFirst();
131
132   Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
133   bool isDone = false;
134   TopoDS_Shape aShape = aValue.shape();
135   double aX, aY;
136   if (getPoint2d(aView, aShape, aX, aY)) {
137     isDone = setPoint(aX, aY);
138   }
139   return isDone;
140 }
141
142 bool PartSet_WidgetPoint2D::setPoint(double theX, double theY)
143 {
144   if (fabs(theX) >= MaxCoordinate)
145     return false;
146   if (fabs(theY) >= MaxCoordinate)
147     return false;
148
149   ModuleBase_Tools::setSpinValue(myXSpin, theX);
150   ModuleBase_Tools::setSpinValue(myYSpin, theY);
151
152   storeValue();
153   return true;
154 }
155
156 bool PartSet_WidgetPoint2D::storeValueCustom() const
157 {
158   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
159   if (!aData) // can be on abort of sketcher element
160     return false;
161   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
162       aData->attribute(attributeID()));
163   
164   PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
165   bool isBlocked = that->blockSignals(true);
166   bool isImmutable = aPoint->setImmutable(true);
167   
168   if (myXSpin->hasVariable() || myYSpin->hasVariable()) {
169     aPoint->setText(myXSpin->text().toStdString(), myYSpin->text().toStdString());
170   } else {
171     aPoint->setValue(myXSpin->value(), myYSpin->value());
172     aPoint->setText("", "");
173   }
174   // after movement the solver will call the update event: optimization
175   moveObject(myFeature);
176   aPoint->setImmutable(isImmutable);
177   that->blockSignals(isBlocked);
178
179   return true;
180 }
181
182 bool PartSet_WidgetPoint2D::restoreValue()
183 {
184   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
185   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
186       aData->attribute(attributeID()));
187   std::string aTextX = aPoint->textX();
188   std::string aTextY = aPoint->textY();
189   if (aTextX.empty() || aTextY.empty()) {
190     double aX = aPoint->x();
191     ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
192     ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
193   } else {
194     ModuleBase_Tools::setSpinText(myXSpin, QString::fromStdString(aTextX));
195     ModuleBase_Tools::setSpinText(myYSpin, QString::fromStdString(aTextY));
196   }
197   return true;
198 }
199
200 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
201 {
202   QList<QWidget*> aControls;
203   aControls.append(myXSpin);
204   aControls.append(myYSpin);
205   return aControls;
206 }
207
208
209 void PartSet_WidgetPoint2D::activateCustom()
210 {
211   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
212   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
213           this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
214   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
215           this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
216
217   QIntList aModes;
218   aModes << TopAbs_VERTEX;
219   aModes << TopAbs_EDGE;
220   myWorkshop->activateSubShapesSelection(aModes);
221
222   myLockApplyMgr->activate();
223 }
224
225 void PartSet_WidgetPoint2D::deactivate()
226 {
227   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
228   disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
229              this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
230   disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
231              this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
232
233   myWorkshop->deactivateSubShapesSelection();
234
235   myLockApplyMgr->deactivate();
236 }
237
238 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView, 
239                                        const TopoDS_Shape& theShape, 
240                                        double& theX, double& theY) const
241 {
242   if (!theShape.IsNull()) {
243     if (theShape.ShapeType() == TopAbs_VERTEX) {
244       const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
245       if (!aVertex.IsNull()) {
246         // A case when point is taken from existing vertex
247         gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
248         PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
249         return true;
250       }
251     }
252   }
253   return false;
254 }
255
256 void PartSet_WidgetPoint2D::setConstraintWith(const ObjectPtr& theObject)
257 {
258   // Create point-edge coincedence
259   FeaturePtr aFeature = mySketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
260   std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
261
262   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
263       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
264   AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
265   std::shared_ptr<GeomDataAPI_Point2D> aThisPoint = 
266     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
267   aRef1->setAttr(aThisPoint);
268
269   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
270       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
271   aRef2->setObject(theObject);
272
273   aFeature->execute();
274 }
275
276 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
277 {
278   // the contex menu release by the right button should not be processed by this widget
279   if (theEvent->button() != Qt::LeftButton)
280     return;
281
282   ModuleBase_ISelection* aSelection = myWorkshop->selection();
283   Handle(V3d_View) aView = theWnd->v3dView();
284   // TODO: This fragment doesn't work because bug in OCC Viewer. It can be used after fixing.
285   NCollection_List<TopoDS_Shape> aShapes;
286   std::list<ObjectPtr> aObjects;
287   aSelection->selectedShapes(aShapes, aObjects);
288   // if we have selection
289   if (aShapes.Extent() > 0) {
290     TopoDS_Shape aShape = aShapes.First();
291     ObjectPtr aObject = aObjects.front();
292     FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aObject);
293     if (aSelectedFeature.get() != NULL) {
294       std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
295               std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
296       if ((!aSPFeature) && (!aShape.IsNull())) {
297         ResultPtr aFixedObject = PartSet_Tools::findFixedObjectByExternal(aShape, aObject, mySketch);
298         if (!aFixedObject.get())
299           aObject = PartSet_Tools::createFixedObjectByExternal(aShape, aObject, mySketch);
300         setConstraintWith(aObject);
301         emit vertexSelected();
302         emit focusOutWidget(this);
303         return;
304       }
305     }
306     double aX, aY;
307     if (getPoint2d(aView, aShape, aX, aY)) {
308       PartSet_Tools::setConstraints(mySketch, feature(), attributeID(),aX, aY);
309       emit vertexSelected();
310       emit focusOutWidget(this);
311       return;
312     } else if (aShape.ShapeType() == TopAbs_EDGE) {
313       setConstraintWith(aObject);
314       emit vertexSelected();
315       emit focusOutWidget(this);
316       return;
317     }
318   }
319   // End of Bug dependent fragment
320
321   // A case when point is taken from mouse event
322   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
323   double aX, anY;
324   PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
325   if (!setPoint(aX, anY))
326     return;
327
328   /// Start alternative code
329   //std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
330   //    GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
331   //QList<FeaturePtr> aIgnore;
332   //aIgnore.append(feature());
333
334   //double aTolerance = aView->Convert(7);
335   //std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt = 
336   //  PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
337   //if (aAttrPnt.get() != NULL) {
338   //  aFeaturePoint->setValue(aAttrPnt->pnt());
339   //  PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
340   //  emit vertexSelected();
341   //}
342   /// End alternative code
343   emit focusOutWidget(this);
344 }
345
346
347 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
348 {
349   if (isEditingMode())
350     return;
351
352   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
353
354   double aX, anY;
355   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
356   setPoint(aX, anY);
357 }
358
359 double PartSet_WidgetPoint2D::x() const
360 {
361   return myXSpin->value();
362 }
363
364 double PartSet_WidgetPoint2D::y() const
365 {
366   return myYSpin->value();
367 }
368
369 void PartSet_WidgetPoint2D::onValuesChanged()
370 {
371   myLockApplyMgr->valuesChanged();
372   emit valuesChanged();
373 }