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