Salome HOME
The correction, to do not create a line by click in the viewer if the second click...
[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->setSpacing(2);
68   aGroupLay->setColumnStretch(1, 1);
69   {
70     QLabel* aLabel = new QLabel(myGroupBox);
71     aLabel->setText(tr("X "));
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     aGroupLay->addWidget(aLabel, 1, 0);
86
87     myYSpin = new ModuleBase_ParamSpinBox(myGroupBox);
88     myYSpin->setMinimum(-DBL_MAX);
89     myYSpin->setMaximum(DBL_MAX);
90     myYSpin->setToolTip(tr("Y"));
91     aGroupLay->addWidget(myYSpin, 1, 1);
92
93     connect(myYSpin, SIGNAL(valueChanged(const QString&)), this, SLOT(onValuesChanged()));
94   }
95   QVBoxLayout* aLayout = new QVBoxLayout(this);
96   ModuleBase_Tools::zeroMargins(aLayout);
97   aLayout->addWidget(myGroupBox);
98   setLayout(aLayout);
99 }
100
101 bool PartSet_WidgetPoint2D::reset()
102 {
103   bool aDone = false;
104   if (!isUseReset() || isComputedDefault() || myXSpin->hasVariable() || myYSpin->hasVariable()) {
105     aDone = false;
106   }
107   else {
108     bool isOk;
109     double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
110     // it is important to block the spin box control in order to do not through out the
111     // locking of the validating state.
112     ModuleBase_Tools::setSpinValue(myXSpin, isOk ? aDefValue : 0.0);
113     ModuleBase_Tools::setSpinValue(myYSpin, isOk ? aDefValue : 0.0);
114     storeValueCustom();
115     aDone = true;
116   }
117   return aDone;
118 }
119
120 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
121 {
122 }
123
124 bool PartSet_WidgetPoint2D::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
125                                          const bool theToValidate)
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 text is not empty then setValue will be ignored
169   // so we should set the text at first
170   aPoint->setText(myXSpin->hasVariable() ? myXSpin->text().toStdString() : "",
171                   myYSpin->hasVariable() ? myYSpin->text().toStdString() : "");
172   aPoint->setValue(!myXSpin->hasVariable() ? myXSpin->value() : aPoint->x(),
173                    !myYSpin->hasVariable() ? myYSpin->value() : aPoint->y());
174
175   // after movement the solver will call the update event: optimization
176   moveObject(myFeature);
177   aPoint->setImmutable(isImmutable);
178   that->blockSignals(isBlocked);
179
180   return true;
181 }
182
183 bool PartSet_WidgetPoint2D::restoreValueCustom()
184 {
185   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
186   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
187       aData->attribute(attributeID()));
188   QString aTextX = QString::fromStdString(aPoint->textX());
189   QString aTextY = QString::fromStdString(aPoint->textY());
190
191   bool isDouble = false;
192   double aVal = 0;
193   if (aTextX.isEmpty()) {
194     ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
195   } else {
196     aVal = aTextX.toDouble(&isDouble);
197     if (isDouble)
198       ModuleBase_Tools::setSpinValue(myXSpin, aVal);
199     else
200       ModuleBase_Tools::setSpinText(myXSpin, aTextX);
201   }
202   if (aTextY.isEmpty()) {
203     ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
204   } else {
205     aVal = aTextY.toDouble(&isDouble);
206     if (isDouble)
207       ModuleBase_Tools::setSpinValue(myYSpin, aVal);
208     else
209       ModuleBase_Tools::setSpinText(myYSpin, aTextY);
210   }
211   //if (aTextX.empty() || aTextY.empty()) {
212   //  ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
213   //  ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
214   //} else {
215   //  ModuleBase_Tools::setSpinText(myXSpin, QString::fromStdString(aTextX));
216   //  ModuleBase_Tools::setSpinText(myYSpin, QString::fromStdString(aTextY));
217   //}
218   return true;
219 }
220
221 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
222 {
223   QList<QWidget*> aControls;
224   aControls.append(myXSpin);
225   aControls.append(myYSpin);
226   return aControls;
227 }
228
229
230 void PartSet_WidgetPoint2D::activateCustom()
231 {
232   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
233   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
234           this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
235   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
236           this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
237
238   QIntList aModes;
239   aModes << TopAbs_VERTEX;
240   aModes << TopAbs_EDGE;
241   myWorkshop->activateSubShapesSelection(aModes);
242
243   myLockApplyMgr->activate();
244 }
245
246 void PartSet_WidgetPoint2D::deactivate()
247 {
248   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
249   disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
250              this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
251   disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
252              this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
253
254   myWorkshop->deactivateSubShapesSelection();
255
256   myLockApplyMgr->deactivate();
257 }
258
259 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView, 
260                                        const TopoDS_Shape& theShape, 
261                                        double& theX, double& theY) const
262 {
263   if (!theShape.IsNull()) {
264     if (theShape.ShapeType() == TopAbs_VERTEX) {
265       const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
266       if (!aVertex.IsNull()) {
267         // A case when point is taken from existing vertex
268         gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
269         PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
270         return true;
271       }
272     }
273   }
274   return false;
275 }
276
277 void PartSet_WidgetPoint2D::setConstraintWith(const ObjectPtr& theObject)
278 {
279   // Create point-edge coincedence
280   FeaturePtr aFeature = mySketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
281   std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
282
283   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
284       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
285   AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
286   std::shared_ptr<GeomDataAPI_Point2D> aThisPoint = 
287     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
288   aRef1->setAttr(aThisPoint);
289
290   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
291       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
292   aRef2->setObject(theObject);
293
294   aFeature->execute();
295 }
296
297 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
298 {
299   // the contex menu release by the right button should not be processed by this widget
300   if (theEvent->button() != Qt::LeftButton)
301     return;
302
303   ModuleBase_ISelection* aSelection = myWorkshop->selection();
304   Handle(V3d_View) aView = theWnd->v3dView();
305   // TODO: This fragment doesn't work because bug in OCC Viewer. It can be used after fixing.
306   NCollection_List<TopoDS_Shape> aShapes;
307   std::list<ObjectPtr> aObjects;
308   aSelection->selectedShapes(aShapes, aObjects);
309   // if we have selection
310   if (aShapes.Extent() > 0) {
311     TopoDS_Shape aShape = aShapes.First();
312     ObjectPtr aObject = aObjects.front();
313     FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aObject);
314     if (aSelectedFeature.get() != NULL) {
315       std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
316               std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
317       if ((!aSPFeature) && (!aShape.IsNull())) {
318         ResultPtr aFixedObject = PartSet_Tools::findFixedObjectByExternal(aShape, aObject, mySketch);
319         if (!aFixedObject.get())
320           aObject = PartSet_Tools::createFixedObjectByExternal(aShape, aObject, mySketch);
321         setConstraintWith(aObject);
322         emit vertexSelected();
323         emit focusOutWidget(this);
324         return;
325       }
326     }
327     double aX, aY;
328     bool isProcessed = false;
329     if (getPoint2d(aView, aShape, aX, aY)) {
330       PartSet_Tools::setConstraints(mySketch, feature(), attributeID(),aX, aY);
331       isProcessed = true;
332     } else if (aShape.ShapeType() == TopAbs_EDGE) {
333       setConstraintWith(aObject);
334       isProcessed = true;
335     }
336     if (isProcessed) {
337       // it is important to perform updateObject() in order to the current value is 
338       // processed by Sketch Solver. Test case: line is created from a previous point
339       // to some distance, but in the area of the highlighting of the point. Constraint
340       // coincidence is created, after the solver is performed, the distance between the
341       // points of the line becomes less than the tolerance. Validator of the line returns
342       // false, the line will be aborted, but sketch stays valid.
343       updateObject(feature());
344       emit vertexSelected();
345       emit focusOutWidget(this);
346       return;
347     }
348   }
349   // End of Bug dependent fragment
350
351   // A case when point is taken from mouse event
352   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
353   double aX, anY;
354   PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
355   if (!setPoint(aX, anY))
356     return;
357
358   /// Start alternative code
359   //std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
360   //    GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
361   //QList<FeaturePtr> aIgnore;
362   //aIgnore.append(feature());
363
364   //double aTolerance = aView->Convert(7);
365   //std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt = 
366   //  PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
367   //if (aAttrPnt.get() != NULL) {
368   //  aFeaturePoint->setValue(aAttrPnt->pnt());
369   //  PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
370   //  emit vertexSelected();
371   //}
372   /// End alternative code
373   emit focusOutWidget(this);
374 }
375
376
377 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
378 {
379   if (isEditingMode())
380     return;
381
382   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
383
384   double aX, anY;
385   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
386   setPoint(aX, anY);
387 }
388
389 double PartSet_WidgetPoint2D::x() const
390 {
391   return myXSpin->value();
392 }
393
394 double PartSet_WidgetPoint2D::y() const
395 {
396   return myYSpin->value();
397 }
398
399 void PartSet_WidgetPoint2D::onValuesChanged()
400 {
401   myLockApplyMgr->valuesChanged();
402   emit valuesChanged();
403 }