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