]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_WidgetPoint2d.cpp
Salome HOME
8f62484fba31d5c2c42aa86374b92d26878c45e3
[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 #define APPLY_BY_ENTER_OR_TAB
55
56 PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent, 
57                                              ModuleBase_IWorkshop* theWorkshop,
58                                              const Config_WidgetAPI* theData,
59                                              const std::string& theParentId)
60  : ModuleBase_ModelWidget(theParent, theData, theParentId), myWorkshop(theWorkshop)
61 {
62   if (MyFeaturesForCoincedence.isEmpty()) {
63     MyFeaturesForCoincedence << SketchPlugin_Line::ID().c_str()
64       << SketchPlugin_Arc::ID().c_str()
65       << SketchPlugin_Point::ID().c_str()
66       << SketchPlugin_Circle::ID().c_str();
67   }
68
69   // the control should accept the focus, so the boolen flag is corrected to be true
70   myIsObligatory = true;
71   //myOptionParam = theData->getProperty(PREVIOUS_FEATURE_PARAM);
72   QString aPageName = QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME));
73   myGroupBox = new QGroupBox(aPageName, theParent);
74   myGroupBox->setFlat(false);
75
76   QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
77   ModuleBase_Tools::adjustMargins(aGroupLay);
78   aGroupLay->setSpacing(2);
79   aGroupLay->setColumnStretch(1, 1);
80   {
81     QLabel* aLabel = new QLabel(myGroupBox);
82     aLabel->setText(tr("X "));
83     aGroupLay->addWidget(aLabel, 0, 0);
84
85     myXSpin = new ModuleBase_ParamSpinBox(myGroupBox);
86     myXSpin->setMinimum(-DBL_MAX);
87     myXSpin->setMaximum(DBL_MAX);
88     myXSpin->setToolTip(tr("X"));
89     aGroupLay->addWidget(myXSpin, 0, 1);
90
91 #ifdef APPLY_BY_ENTER_OR_TAB
92     // Apply widget value change by enter/tab event.
93     //connect(myXSpin, SIGNAL(editingFinished()), this, SLOT(onValuesChanged()));
94     //connect(myXSpin, SIGNAL(focusNextPrev()), this, SLOT(onValuesChanged()));
95     connect(myXSpin, SIGNAL(valueStored()), this, SLOT(onValuesChanged()));
96     connect(myXSpin, SIGNAL(valueChanged(const QString&)), this, SIGNAL(valuesModified()));
97     connect(myXSpin, SIGNAL(focusNextPrev()), this, SIGNAL(focusNextPrev()));
98 #else
99     connect(myXSpin, SIGNAL(valueChanged(const QString&)), this, SLOT(onValuesChanged()));
100 #endif
101   }
102   {
103     QLabel* aLabel = new QLabel(myGroupBox);
104     aLabel->setText(tr("Y "));
105     aGroupLay->addWidget(aLabel, 1, 0);
106
107     myYSpin = new ModuleBase_ParamSpinBox(myGroupBox);
108     myYSpin->setMinimum(-DBL_MAX);
109     myYSpin->setMaximum(DBL_MAX);
110     myYSpin->setToolTip(tr("Y"));
111     aGroupLay->addWidget(myYSpin, 1, 1);
112
113 #ifdef APPLY_BY_ENTER_OR_TAB
114     // Apply widget value change by enter/tab event.
115     //connect(myYSpin, SIGNAL(editingFinished()), this, SLOT(onValuesChanged()));
116     connect(myYSpin, SIGNAL(valueStored()), this, SLOT(onValuesChanged()));
117     connect(myYSpin, SIGNAL(valueChanged(const QString&)), this, SIGNAL(valuesModified()));
118     connect(myYSpin, SIGNAL(focusNextPrev()), this, SIGNAL(focusNextPrev()));
119 #else
120     connect(myYSpin, SIGNAL(valueChanged(const QString&)), this, SLOT(onValuesChanged()));
121 #endif
122   }
123   QVBoxLayout* aLayout = new QVBoxLayout(this);
124   ModuleBase_Tools::zeroMargins(aLayout);
125   aLayout->addWidget(myGroupBox);
126   setLayout(aLayout);
127 }
128
129 bool PartSet_WidgetPoint2D::resetCustom()
130 {
131   bool aDone = false;
132   if (!isUseReset() || isComputedDefault() || myXSpin->hasVariable() || myYSpin->hasVariable()) {
133     aDone = false;
134   }
135   else {
136     bool isOk;
137     double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
138     // it is important to block the spin box control in order to do not through out the
139     // locking of the validating state.
140     ModuleBase_Tools::setSpinValue(myXSpin, isOk ? aDefValue : 0.0);
141     ModuleBase_Tools::setSpinValue(myYSpin, isOk ? aDefValue : 0.0);
142     storeValueCustom();
143     aDone = true;
144   }
145   return aDone;
146 }
147
148 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
149 {
150 }
151
152 bool PartSet_WidgetPoint2D::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
153                                          const bool theToValidate)
154 {
155   if (theValues.empty())
156     return false;
157
158   ModuleBase_ViewerPrs aValue = theValues.takeFirst();
159
160   Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
161   bool isDone = false;
162   TopoDS_Shape aShape = aValue.shape();
163   double aX, aY;
164   if (getPoint2d(aView, aShape, aX, aY)) {
165     isDone = setPoint(aX, aY);
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 void PartSet_WidgetPoint2D::deactivate()
273 {
274   ModuleBase_ModelWidget::deactivate();
275   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
276   disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
277              this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
278   disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
279              this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
280
281   myWorkshop->deactivateSubShapesSelection();
282 }
283
284 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView, 
285                                        const TopoDS_Shape& theShape, 
286                                        double& theX, double& theY) const
287 {
288   if (!theShape.IsNull()) {
289     if (theShape.ShapeType() == TopAbs_VERTEX) {
290       const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
291       if (!aVertex.IsNull()) {
292         // A case when point is taken from existing vertex
293         gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
294         PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
295         return true;
296       }
297     }
298   }
299   return false;
300 }
301
302 void PartSet_WidgetPoint2D::setConstraintWith(const ObjectPtr& theObject)
303 {
304   // Create point-edge coincedence
305   FeaturePtr aFeature = mySketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
306   std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
307
308   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
309       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
310   AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
311   std::shared_ptr<GeomDataAPI_Point2D> aThisPoint = 
312     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
313   aRef1->setAttr(aThisPoint);
314
315   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
316       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
317   aRef2->setObject(theObject);
318
319   aFeature->execute();
320 }
321
322 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
323 {
324   // the contex menu release by the right button should not be processed by this widget
325   if (theEvent->button() != Qt::LeftButton)
326     return;
327
328   ModuleBase_ISelection* aSelection = myWorkshop->selection();
329   Handle(V3d_View) aView = theWnd->v3dView();
330   // TODO: This fragment doesn't work because bug in OCC Viewer. It can be used after fixing.
331   NCollection_List<TopoDS_Shape> aShapes;
332   std::list<ObjectPtr> aObjects;
333   aSelection->selectedShapes(aShapes, aObjects);
334   // if we have selection
335   if (aShapes.Extent() > 0) {
336     TopoDS_Shape aShape = aShapes.First();
337     ObjectPtr aObject = aObjects.front();
338     FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aObject);
339     bool anExternal = false;
340     if (aSelectedFeature.get() != NULL) {
341       std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
342               std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
343       if ((!aSPFeature) && (!aShape.IsNull())) {
344         anExternal = true;
345         ResultPtr aFixedObject = PartSet_Tools::findFixedObjectByExternal(aShape, aObject, mySketch);
346         if (!aFixedObject.get())
347           aObject = PartSet_Tools::createFixedObjectByExternal(aShape, aObject, mySketch);
348
349         double aX, aY;
350         if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
351           // do not create a constraint to the point, which already used by the feature
352           // if the feature contains the point, focus is not switched
353           setPoint(aX, aY);
354         }
355         else {
356           if (getPoint2d(aView, aShape, aX, aY))
357             setPoint(aX, aY);
358           setConstraintWith(aObject);
359           emit vertexSelected();
360           emit focusOutWidget(this);
361         }
362       }
363     }
364     if (!anExternal) {
365       double aX, aY;
366       bool isProcessed = false;
367       if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
368         // when the point is selected, the coordinates of the point should be set into the attribute
369         // if the feature contains the point, focus is not switched
370         setPoint(aX, aY);
371       }
372       else {
373         // do not set a coincidence constraint in the attribute if the feature contains a point
374         // with the same coordinates. It is important for line creation in order to do not set
375         // the same constraints for the same points, oterwise the result line has zero length.
376         if (getPoint2d(aView, aShape, aX, aY)) {
377           setPoint(aX, aY);
378           PartSet_Tools::setConstraints(mySketch, feature(), attributeID(), aX, aY);
379         }
380         else if (aShape.ShapeType() == TopAbs_EDGE) {
381           if (MyFeaturesForCoincedence.contains(myFeature->getKind().c_str()))
382             setConstraintWith(aObject);
383         }
384         // it is important to perform updateObject() in order to the current value is 
385         // processed by Sketch Solver. Test case: line is created from a previous point
386         // to some distance, but in the area of the highlighting of the point. Constraint
387         // coincidence is created, after the solver is performed, the distance between the
388         // points of the line becomes less than the tolerance. Validator of the line returns
389         // false, the line will be aborted, but sketch stays valid.
390         updateObject(feature());
391         emit vertexSelected();
392         emit focusOutWidget(this);
393       }
394     }
395   }
396   // End of Bug dependent fragment
397   else {
398     // A case when point is taken from mouse event
399     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
400     double aX, anY;
401     PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
402
403     // if the feature contains the point, focus is not switched
404     if (!setPoint(aX, anY) || isFeatureContainsPoint(myFeature, aX, anY))
405       return;
406
407     /// Start alternative code
408     //std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
409     //    GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
410     //QList<FeaturePtr> aIgnore;
411     //aIgnore.append(feature());
412
413     //double aTolerance = aView->Convert(7);
414     //std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt = 
415     //  PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
416     //if (aAttrPnt.get() != NULL) {
417     //  aFeaturePoint->setValue(aAttrPnt->pnt());
418     //  PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
419     //  emit vertexSelected();
420     //}
421     /// End alternative code
422     emit focusOutWidget(this);
423   }
424 }
425
426
427 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
428 {
429   if (isEditingMode())
430     return;
431
432   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
433
434   double aX, anY;
435   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
436   // we need to block the value state change 
437   bool isBlocked = blockValueState(true);
438   setPoint(aX, anY);
439   blockValueState(isBlocked);
440   setValueState(ModifiedInViewer);
441 }
442
443 double PartSet_WidgetPoint2D::x() const
444 {
445   return myXSpin->value();
446 }
447
448 double PartSet_WidgetPoint2D::y() const
449 {
450   return myYSpin->value();
451 }
452
453
454 bool PartSet_WidgetPoint2D::isFeatureContainsPoint(const FeaturePtr& theFeature,
455                                                    double theX, double theY)
456 {
457   bool aPointIsFound = false;
458   AttributePtr aWidgetAttribute = myFeature->attribute(attributeID());
459
460   std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = 
461                                     std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
462   std::list<AttributePtr> anAttributes =
463                                 myFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
464   std::list<AttributePtr>::iterator anIter = anAttributes.begin();
465   for(; anIter != anAttributes.end() && !aPointIsFound; anIter++) {
466     AttributePoint2DPtr aPoint2DAttribute =
467       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
468     if (aPoint2DAttribute == aWidgetAttribute)
469       continue;
470     if (aPoint2DAttribute.get() && aPoint2DAttribute->isInitialized()) {
471       aPointIsFound = aPoint2DAttribute->pnt()->isEqual(aPnt2d);
472     }
473   }
474   return aPointIsFound;
475 }
476
477 void PartSet_WidgetPoint2D::onValuesChanged()
478 {
479   emit valuesChanged();
480 }
481
482 bool PartSet_WidgetPoint2D::processEnter()
483 {
484   bool isModified = myXSpin->isModified() || myYSpin->isModified();
485   if (isModified) {
486     bool isXModified = myXSpin->isModified();
487     onValuesChanged();
488     myXSpin->clearModified();
489     myYSpin->clearModified();
490     if (isXModified)
491       myXSpin->selectAll();
492     else
493       myYSpin->selectAll();
494   }
495   return isModified;
496 }