]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_WidgetPoint2d.cpp
Salome HOME
A correction to check modified value in extrusion cut/sketch/etc operations.
[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
11 #include <ModuleBase_ParamSpinBox.h>
12 #include <ModuleBase_Tools.h>
13 #include <ModuleBase_IViewer.h>
14 #include <ModuleBase_IViewWindow.h>
15 #include <ModuleBase_ISelection.h>
16
17 #include <Config_Keywords.h>
18 #include <Config_WidgetAPI.h>
19
20 #include <Events_Loop.h>
21 #include <ModelAPI_Events.h>
22
23 #include <ModelAPI_Feature.h>
24 #include <ModelAPI_Data.h>
25 #include <ModelAPI_Object.h>
26 #include <GeomDataAPI_Point2D.h>
27 #include <GeomAPI_Pnt2d.h>
28
29 #include <SketchPlugin_Feature.h>
30 #include <SketchPlugin_ConstraintCoincidence.h>
31 #include <SketchPlugin_Line.h>
32 #include <SketchPlugin_Arc.h>
33 #include <SketchPlugin_Circle.h>
34 #include <SketchPlugin_Point.h>
35
36 #include <QGroupBox>
37 #include <QGridLayout>
38 #include <QLabel>
39 #include <QEvent>
40 #include <QMouseEvent>
41 #include <QApplication>
42
43 #include <TopoDS.hxx>
44 #include <TopoDS_Vertex.hxx>
45 #include <BRep_Tool.hxx>
46
47 #include <cfloat>
48 #include <climits>
49
50 const double MaxCoordinate = 1e12;
51
52 static QStringList MyFeaturesForCoincedence;
53
54 PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent, 
55                                              ModuleBase_IWorkshop* theWorkshop,
56                                              const Config_WidgetAPI* theData,
57                                              const std::string& theParentId)
58  : ModuleBase_ModelWidget(theParent, theData, theParentId), myWorkshop(theWorkshop)
59 {
60   if (MyFeaturesForCoincedence.isEmpty()) {
61     MyFeaturesForCoincedence << SketchPlugin_Line::ID().c_str()
62       << SketchPlugin_Arc::ID().c_str()
63       << SketchPlugin_Point::ID().c_str()
64       << SketchPlugin_Circle::ID().c_str();
65   }
66
67   // the control should accept the focus, so the boolen flag is corrected to be true
68   myIsObligatory = true;
69   //myOptionParam = theData->getProperty(PREVIOUS_FEATURE_PARAM);
70   QString aPageName = QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME));
71   myGroupBox = new QGroupBox(aPageName, theParent);
72   myGroupBox->setFlat(false);
73
74   QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
75   ModuleBase_Tools::adjustMargins(aGroupLay);
76   aGroupLay->setSpacing(2);
77   aGroupLay->setColumnStretch(1, 1);
78   {
79     QLabel* aLabel = new QLabel(myGroupBox);
80     aLabel->setText(tr("X "));
81     aGroupLay->addWidget(aLabel, 0, 0);
82
83     myXSpin = new ModuleBase_ParamSpinBox(myGroupBox);
84     myXSpin->setMinimum(-DBL_MAX);
85     myXSpin->setMaximum(DBL_MAX);
86     myXSpin->setToolTip(tr("X"));
87     aGroupLay->addWidget(myXSpin, 0, 1);
88
89     connect(myXSpin, SIGNAL(valueChanged(const QString&)), this, SIGNAL(valuesModified()));
90   }
91   {
92     QLabel* aLabel = new QLabel(myGroupBox);
93     aLabel->setText(tr("Y "));
94     aGroupLay->addWidget(aLabel, 1, 0);
95
96     myYSpin = new ModuleBase_ParamSpinBox(myGroupBox);
97     myYSpin->setMinimum(-DBL_MAX);
98     myYSpin->setMaximum(DBL_MAX);
99     myYSpin->setToolTip(tr("Y"));
100     aGroupLay->addWidget(myYSpin, 1, 1);
101
102     connect(myYSpin, SIGNAL(valueChanged(const QString&)), this, SIGNAL(valuesModified()));
103   }
104   QVBoxLayout* aLayout = new QVBoxLayout(this);
105   ModuleBase_Tools::zeroMargins(aLayout);
106   aLayout->addWidget(myGroupBox);
107   setLayout(aLayout);
108 }
109
110 bool PartSet_WidgetPoint2D::resetCustom()
111 {
112   bool aDone = false;
113   if (!isUseReset() || isComputedDefault() || myXSpin->hasVariable() || myYSpin->hasVariable()) {
114     aDone = false;
115   }
116   else {
117     bool isOk;
118     double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
119     // it is important to block the spin box control in order to do not through out the
120     // locking of the validating state.
121     ModuleBase_Tools::setSpinValue(myXSpin, isOk ? aDefValue : 0.0);
122     ModuleBase_Tools::setSpinValue(myYSpin, isOk ? aDefValue : 0.0);
123     storeValueCustom();
124     aDone = true;
125   }
126   return aDone;
127 }
128
129 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
130 {
131 }
132
133 bool PartSet_WidgetPoint2D::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
134                                          const bool theToValidate)
135 {
136   bool isDone = false;
137   if (theValues.empty())
138     return isDone;
139
140   ModuleBase_ViewerPrs aValue = theValues.takeFirst();
141   TopoDS_Shape aShape = aValue.shape();
142   if (!aShape.IsNull()) {
143     Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
144     double aX, aY;
145     if (getPoint2d(aView, aShape, aX, aY)) {
146       isDone = setPoint(aX, aY);
147     }
148   }
149   else if (canBeActivatedByMove()) {
150     if (feature()->getKind() == SketchPlugin_Line::ID()) {
151       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aValue.object());
152       // Initialise new line with first point equal to end of previous
153       if (aFeature.get()) {
154         std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
155         std::shared_ptr<GeomDataAPI_Point2D> aPoint = 
156           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
157                                        aData->attribute(SketchPlugin_Line::END_ID()));
158         if (aPoint) {
159           setPoint(aPoint->x(), aPoint->y());
160           PartSet_Tools::setConstraints(mySketch, feature(), attributeID(), aPoint->x(),
161                                         aPoint->y());
162           isDone = true;
163         }
164       }
165     }
166   }
167   return isDone;
168 }
169
170 bool PartSet_WidgetPoint2D::setPoint(double theX, double theY)
171 {
172   if (fabs(theX) >= MaxCoordinate)
173     return false;
174   if (fabs(theY) >= MaxCoordinate)
175     return false;
176
177   ModuleBase_Tools::setSpinValue(myXSpin, theX);
178   ModuleBase_Tools::setSpinValue(myYSpin, theY);
179
180   storeValue();
181   return true;
182 }
183
184 bool PartSet_WidgetPoint2D::storeValueCustom() const
185 {
186   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
187   if (!aData) // can be on abort of sketcher element
188     return false;
189   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
190       aData->attribute(attributeID()));
191
192   PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
193   bool isBlocked = that->blockSignals(true);
194   bool isImmutable = aPoint->setImmutable(true);
195
196   // if text is not empty then setValue will be ignored
197   // so we should set the text at first
198   aPoint->setText(myXSpin->hasVariable() ? myXSpin->text().toStdString() : "",
199                   myYSpin->hasVariable() ? myYSpin->text().toStdString() : "");
200   aPoint->setValue(!myXSpin->hasVariable() ? myXSpin->value() : aPoint->x(),
201                    !myYSpin->hasVariable() ? myYSpin->value() : aPoint->y());
202
203   // after movement the solver will call the update event: optimization
204   moveObject(myFeature);
205   aPoint->setImmutable(isImmutable);
206   that->blockSignals(isBlocked);
207
208   return true;
209 }
210
211 bool PartSet_WidgetPoint2D::restoreValueCustom()
212 {
213   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
214   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
215       aData->attribute(attributeID()));
216   QString aTextX = QString::fromStdString(aPoint->textX());
217   QString aTextY = QString::fromStdString(aPoint->textY());
218
219   bool isDouble = false;
220   double aVal = 0;
221   if (aTextX.isEmpty()) {
222     ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
223   } else {
224     aVal = aTextX.toDouble(&isDouble);
225     if (isDouble)
226       ModuleBase_Tools::setSpinValue(myXSpin, aVal);
227     else
228       ModuleBase_Tools::setSpinText(myXSpin, aTextX);
229   }
230   if (aTextY.isEmpty()) {
231     ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
232   } else {
233     aVal = aTextY.toDouble(&isDouble);
234     if (isDouble)
235       ModuleBase_Tools::setSpinValue(myYSpin, aVal);
236     else
237       ModuleBase_Tools::setSpinText(myYSpin, aTextY);
238   }
239   //if (aTextX.empty() || aTextY.empty()) {
240   //  ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
241   //  ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
242   //} else {
243   //  ModuleBase_Tools::setSpinText(myXSpin, QString::fromStdString(aTextX));
244   //  ModuleBase_Tools::setSpinText(myYSpin, QString::fromStdString(aTextY));
245   //}
246   return true;
247 }
248
249 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
250 {
251   QList<QWidget*> aControls;
252   aControls.append(myXSpin);
253   aControls.append(myYSpin);
254   return aControls;
255 }
256
257
258 void PartSet_WidgetPoint2D::activateCustom()
259 {
260   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
261   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
262           this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
263   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
264           this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
265
266   QIntList aModes;
267   aModes << TopAbs_VERTEX;
268   aModes << TopAbs_EDGE;
269   myWorkshop->activateSubShapesSelection(aModes);
270 }
271
272 bool PartSet_WidgetPoint2D::canBeActivatedByMove()
273 {
274   bool aCanBeActivated = false;
275   if (feature()->getKind() == SketchPlugin_Line::ID() &&
276       attributeID() == SketchPlugin_Line::START_ID())
277     aCanBeActivated = true;
278
279   return aCanBeActivated;
280 }
281
282 void PartSet_WidgetPoint2D::deactivate()
283 {
284   ModuleBase_ModelWidget::deactivate();
285   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
286   disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
287              this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
288   disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
289              this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
290
291   myWorkshop->deactivateSubShapesSelection();
292 }
293
294 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView, 
295                                        const TopoDS_Shape& theShape, 
296                                        double& theX, double& theY) const
297 {
298   if (!theShape.IsNull()) {
299     if (theShape.ShapeType() == TopAbs_VERTEX) {
300       const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
301       if (!aVertex.IsNull()) {
302         // A case when point is taken from existing vertex
303         gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
304         PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
305         return true;
306       }
307     }
308   }
309   return false;
310 }
311
312 void PartSet_WidgetPoint2D::setConstraintWith(const ObjectPtr& theObject)
313 {
314   // Create point-edge coincedence
315   FeaturePtr aFeature = mySketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
316   std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
317
318   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
319       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
320   AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
321   std::shared_ptr<GeomDataAPI_Point2D> aThisPoint = 
322     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
323   aRef1->setAttr(aThisPoint);
324
325   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
326       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
327   aRef2->setObject(theObject);
328
329   aFeature->execute();
330 }
331
332 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
333 {
334   // the contex menu release by the right button should not be processed by this widget
335   if (theEvent->button() != Qt::LeftButton)
336     return;
337
338   ModuleBase_ISelection* aSelection = myWorkshop->selection();
339   Handle(V3d_View) aView = theWnd->v3dView();
340   // TODO: This fragment doesn't work because bug in OCC Viewer. It can be used after fixing.
341   NCollection_List<TopoDS_Shape> aShapes;
342   std::list<ObjectPtr> aObjects;
343   aSelection->selectedShapes(aShapes, aObjects);
344   // if we have selection
345   if (aShapes.Extent() > 0) {
346     TopoDS_Shape aShape = aShapes.First();
347     ObjectPtr aObject = aObjects.front();
348     FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aObject);
349     bool anExternal = false;
350     if (aSelectedFeature.get() != NULL) {
351       std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
352               std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
353       if ((!aSPFeature) && (!aShape.IsNull())) {
354         anExternal = true;
355         ResultPtr aFixedObject = PartSet_Tools::findFixedObjectByExternal(aShape, aObject, mySketch);
356         if (!aFixedObject.get())
357           aObject = PartSet_Tools::createFixedObjectByExternal(aShape, aObject, mySketch);
358
359         double aX, aY;
360         if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
361           // do not create a constraint to the point, which already used by the feature
362           // if the feature contains the point, focus is not switched
363           setPoint(aX, aY);
364         }
365         else {
366           if (getPoint2d(aView, aShape, aX, aY))
367             setPoint(aX, aY);
368           setConstraintWith(aObject);
369           emit vertexSelected();
370           emit focusOutWidget(this);
371         }
372       }
373     }
374     if (!anExternal) {
375       double aX, aY;
376       bool isProcessed = false;
377       if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
378         // when the point is selected, the coordinates of the point should be set into the attribute
379         // if the feature contains the point, focus is not switched
380         setPoint(aX, aY);
381       }
382       else {
383         // do not set a coincidence constraint in the attribute if the feature contains a point
384         // with the same coordinates. It is important for line creation in order to do not set
385         // the same constraints for the same points, oterwise the result line has zero length.
386         if (getPoint2d(aView, aShape, aX, aY)) {
387           setPoint(aX, aY);
388           PartSet_Tools::setConstraints(mySketch, feature(), attributeID(), aX, aY);
389         }
390         else if (aShape.ShapeType() == TopAbs_EDGE) {
391           if (MyFeaturesForCoincedence.contains(myFeature->getKind().c_str()))
392             setConstraintWith(aObject);
393         }
394         // it is important to perform updateObject() in order to the current value is 
395         // processed by Sketch Solver. Test case: line is created from a previous point
396         // to some distance, but in the area of the highlighting of the point. Constraint
397         // coincidence is created, after the solver is performed, the distance between the
398         // points of the line becomes less than the tolerance. Validator of the line returns
399         // false, the line will be aborted, but sketch stays valid.
400         updateObject(feature());
401         emit vertexSelected();
402         emit focusOutWidget(this);
403       }
404     }
405   }
406   // End of Bug dependent fragment
407   else {
408     // A case when point is taken from mouse event
409     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
410     double aX, anY;
411     PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
412
413     // if the feature contains the point, focus is not switched
414     if (!setPoint(aX, anY) || isFeatureContainsPoint(myFeature, aX, anY))
415       return;
416
417     /// Start alternative code
418     //std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
419     //    GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
420     //QList<FeaturePtr> aIgnore;
421     //aIgnore.append(feature());
422
423     //double aTolerance = aView->Convert(7);
424     //std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt = 
425     //  PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
426     //if (aAttrPnt.get() != NULL) {
427     //  aFeaturePoint->setValue(aAttrPnt->pnt());
428     //  PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
429     //  emit vertexSelected();
430     //}
431     /// End alternative code
432     emit focusOutWidget(this);
433   }
434 }
435
436
437 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
438 {
439   if (isEditingMode())
440     return;
441
442   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
443
444   double aX, anY;
445   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
446   // we need to block the value state change 
447   bool isBlocked = blockValueState(true);
448   setPoint(aX, anY);
449   blockValueState(isBlocked);
450   setValueState(ModifiedInViewer);
451 }
452
453 double PartSet_WidgetPoint2D::x() const
454 {
455   return myXSpin->value();
456 }
457
458 double PartSet_WidgetPoint2D::y() const
459 {
460   return myYSpin->value();
461 }
462
463
464 bool PartSet_WidgetPoint2D::isFeatureContainsPoint(const FeaturePtr& theFeature,
465                                                    double theX, double theY)
466 {
467   bool aPointIsFound = false;
468   AttributePtr aWidgetAttribute = myFeature->attribute(attributeID());
469
470   std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = 
471                                     std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
472   std::list<AttributePtr> anAttributes =
473                                 myFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
474   std::list<AttributePtr>::iterator anIter = anAttributes.begin();
475   for(; anIter != anAttributes.end() && !aPointIsFound; anIter++) {
476     AttributePoint2DPtr aPoint2DAttribute =
477       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
478     if (aPoint2DAttribute == aWidgetAttribute)
479       continue;
480     if (aPoint2DAttribute.get() && aPoint2DAttribute->isInitialized()) {
481       aPointIsFound = aPoint2DAttribute->pnt()->isEqual(aPnt2d);
482     }
483   }
484   return aPointIsFound;
485 }
486
487 /*void PartSet_WidgetPoint2D::onValuesChanged()
488 {
489   emit valuesChanged();
490 }*/
491
492 bool PartSet_WidgetPoint2D::processEnter()
493 {
494   bool isModified = myXSpin->isModified() || myYSpin->isModified();
495   if (isModified) {
496     bool isXModified = myXSpin->isModified();
497     emit valuesChanged();
498     //onValuesChanged();
499     myXSpin->clearModified();
500     myYSpin->clearModified();
501     if (isXModified)
502       myXSpin->selectAll();
503     else
504       myYSpin->selectAll();
505   }
506   return isModified;
507 }