1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PartSet_WidgetPoint2D.cpp
4 // Created: 25 Apr 2014
5 // Author: Natalia ERMOLAEVA
7 #include "PartSet_WidgetPoint2d.h"
8 #include <PartSet_Tools.h>
9 #include <PartSet_Module.h>
10 #include <PartSet_LockApplyMgr.h>
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>
18 #include <Config_Keywords.h>
19 #include <Config_WidgetAPI.h>
21 #include <Events_Loop.h>
22 #include <ModelAPI_Events.h>
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>
30 #include <SketchPlugin_Feature.h>
31 #include <SketchPlugin_ConstraintCoincidence.h>
32 #include <SketchPlugin_Line.h>
33 #include <SketchPlugin_Arc.h>
34 #include <SketchPlugin_Circle.h>
35 #include <SketchPlugin_Point.h>
38 #include <QGridLayout>
41 #include <QMouseEvent>
42 #include <QApplication>
45 #include <TopoDS_Vertex.hxx>
46 #include <BRep_Tool.hxx>
51 const double MaxCoordinate = 1e12;
53 static QStringList MyFeaturesForCoincedence;
55 PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent,
56 ModuleBase_IWorkshop* theWorkshop,
57 const Config_WidgetAPI* theData,
58 const std::string& theParentId)
59 : ModuleBase_ModelWidget(theParent, theData, theParentId), myWorkshop(theWorkshop)
61 if (MyFeaturesForCoincedence.isEmpty()) {
62 MyFeaturesForCoincedence << SketchPlugin_Line::ID().c_str()
63 << SketchPlugin_Arc::ID().c_str()
64 << SketchPlugin_Point::ID().c_str()
65 << SketchPlugin_Circle::ID().c_str();
67 myLockApplyMgr = new PartSet_LockApplyMgr(theParent, myWorkshop);
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);
76 QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
77 ModuleBase_Tools::adjustMargins(aGroupLay);
78 aGroupLay->setSpacing(2);
79 aGroupLay->setColumnStretch(1, 1);
81 QLabel* aLabel = new QLabel(myGroupBox);
82 aLabel->setText(tr("X "));
83 aGroupLay->addWidget(aLabel, 0, 0);
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);
91 connect(myXSpin, SIGNAL(valueChanged(const QString&)), this, SLOT(onValuesChanged()));
94 QLabel* aLabel = new QLabel(myGroupBox);
95 aLabel->setText(tr("Y "));
96 aGroupLay->addWidget(aLabel, 1, 0);
98 myYSpin = new ModuleBase_ParamSpinBox(myGroupBox);
99 myYSpin->setMinimum(-DBL_MAX);
100 myYSpin->setMaximum(DBL_MAX);
101 myYSpin->setToolTip(tr("Y"));
102 aGroupLay->addWidget(myYSpin, 1, 1);
104 connect(myYSpin, SIGNAL(valueChanged(const QString&)), this, SLOT(onValuesChanged()));
106 QVBoxLayout* aLayout = new QVBoxLayout(this);
107 ModuleBase_Tools::zeroMargins(aLayout);
108 aLayout->addWidget(myGroupBox);
112 bool PartSet_WidgetPoint2D::reset()
115 if (!isUseReset() || isComputedDefault() || myXSpin->hasVariable() || myYSpin->hasVariable()) {
120 double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
121 // it is important to block the spin box control in order to do not through out the
122 // locking of the validating state.
123 ModuleBase_Tools::setSpinValue(myXSpin, isOk ? aDefValue : 0.0);
124 ModuleBase_Tools::setSpinValue(myYSpin, isOk ? aDefValue : 0.0);
131 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
135 bool PartSet_WidgetPoint2D::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
136 const bool theToValidate)
138 if (theValues.empty())
141 ModuleBase_ViewerPrs aValue = theValues.takeFirst();
143 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
145 TopoDS_Shape aShape = aValue.shape();
147 if (getPoint2d(aView, aShape, aX, aY)) {
148 isDone = setPoint(aX, aY);
153 bool PartSet_WidgetPoint2D::setPoint(double theX, double theY)
155 if (fabs(theX) >= MaxCoordinate)
157 if (fabs(theY) >= MaxCoordinate)
160 ModuleBase_Tools::setSpinValue(myXSpin, theX);
161 ModuleBase_Tools::setSpinValue(myYSpin, theY);
167 bool PartSet_WidgetPoint2D::storeValueCustom() const
169 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
170 if (!aData) // can be on abort of sketcher element
172 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
173 aData->attribute(attributeID()));
175 PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
176 bool isBlocked = that->blockSignals(true);
177 bool isImmutable = aPoint->setImmutable(true);
179 // if text is not empty then setValue will be ignored
180 // so we should set the text at first
181 aPoint->setText(myXSpin->hasVariable() ? myXSpin->text().toStdString() : "",
182 myYSpin->hasVariable() ? myYSpin->text().toStdString() : "");
183 aPoint->setValue(!myXSpin->hasVariable() ? myXSpin->value() : aPoint->x(),
184 !myYSpin->hasVariable() ? myYSpin->value() : aPoint->y());
186 // after movement the solver will call the update event: optimization
187 moveObject(myFeature);
188 aPoint->setImmutable(isImmutable);
189 that->blockSignals(isBlocked);
194 bool PartSet_WidgetPoint2D::restoreValueCustom()
196 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
197 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
198 aData->attribute(attributeID()));
199 QString aTextX = QString::fromStdString(aPoint->textX());
200 QString aTextY = QString::fromStdString(aPoint->textY());
202 bool isDouble = false;
204 if (aTextX.isEmpty()) {
205 ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
207 aVal = aTextX.toDouble(&isDouble);
209 ModuleBase_Tools::setSpinValue(myXSpin, aVal);
211 ModuleBase_Tools::setSpinText(myXSpin, aTextX);
213 if (aTextY.isEmpty()) {
214 ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
216 aVal = aTextY.toDouble(&isDouble);
218 ModuleBase_Tools::setSpinValue(myYSpin, aVal);
220 ModuleBase_Tools::setSpinText(myYSpin, aTextY);
222 //if (aTextX.empty() || aTextY.empty()) {
223 // ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
224 // ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
226 // ModuleBase_Tools::setSpinText(myXSpin, QString::fromStdString(aTextX));
227 // ModuleBase_Tools::setSpinText(myYSpin, QString::fromStdString(aTextY));
232 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
234 QList<QWidget*> aControls;
235 aControls.append(myXSpin);
236 aControls.append(myYSpin);
241 void PartSet_WidgetPoint2D::activateCustom()
243 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
244 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
245 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
246 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
247 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
250 aModes << TopAbs_VERTEX;
251 aModes << TopAbs_EDGE;
252 myWorkshop->activateSubShapesSelection(aModes);
254 myLockApplyMgr->activate();
257 void PartSet_WidgetPoint2D::deactivate()
259 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
260 disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
261 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
262 disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
263 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
265 myWorkshop->deactivateSubShapesSelection();
267 myLockApplyMgr->deactivate();
270 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView,
271 const TopoDS_Shape& theShape,
272 double& theX, double& theY) const
274 if (!theShape.IsNull()) {
275 if (theShape.ShapeType() == TopAbs_VERTEX) {
276 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
277 if (!aVertex.IsNull()) {
278 // A case when point is taken from existing vertex
279 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
280 PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
288 void PartSet_WidgetPoint2D::setConstraintWith(const ObjectPtr& theObject)
290 // Create point-edge coincedence
291 FeaturePtr aFeature = mySketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
292 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
294 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
295 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
296 AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
297 std::shared_ptr<GeomDataAPI_Point2D> aThisPoint =
298 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
299 aRef1->setAttr(aThisPoint);
301 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
302 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
303 aRef2->setObject(theObject);
308 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
310 // the contex menu release by the right button should not be processed by this widget
311 if (theEvent->button() != Qt::LeftButton)
314 ModuleBase_ISelection* aSelection = myWorkshop->selection();
315 Handle(V3d_View) aView = theWnd->v3dView();
316 // TODO: This fragment doesn't work because bug in OCC Viewer. It can be used after fixing.
317 NCollection_List<TopoDS_Shape> aShapes;
318 std::list<ObjectPtr> aObjects;
319 aSelection->selectedShapes(aShapes, aObjects);
320 // if we have selection
321 if (aShapes.Extent() > 0) {
322 TopoDS_Shape aShape = aShapes.First();
323 ObjectPtr aObject = aObjects.front();
324 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aObject);
325 bool anExternal = false;
326 if (aSelectedFeature.get() != NULL) {
327 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
328 std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
329 if ((!aSPFeature) && (!aShape.IsNull())) {
331 ResultPtr aFixedObject = PartSet_Tools::findFixedObjectByExternal(aShape, aObject, mySketch);
332 if (!aFixedObject.get())
333 aObject = PartSet_Tools::createFixedObjectByExternal(aShape, aObject, mySketch);
336 if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
337 // do not create a constraint to the point, which already used by the feature
338 // if the feature contains the point, focus is not switched
342 setConstraintWith(aObject);
343 emit vertexSelected();
344 emit focusOutWidget(this);
350 bool isProcessed = false;
351 if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
352 // when the point is selected, the coordinates of the point should be set into the attribute
353 // if the feature contains the point, focus is not switched
357 // do not set a coincidence constraint in the attribute if the feature contains a point
358 // with the same coordinates. It is important for line creation in order to do not set
359 // the same constraints for the same points, oterwise the result line has zero length.
360 if (getPoint2d(aView, aShape, aX, aY))
361 PartSet_Tools::setConstraints(mySketch, feature(), attributeID(), aX, aY);
362 else if (aShape.ShapeType() == TopAbs_EDGE) {
363 if (MyFeaturesForCoincedence.contains(myFeature->getKind().c_str()))
364 setConstraintWith(aObject);
366 // it is important to perform updateObject() in order to the current value is
367 // processed by Sketch Solver. Test case: line is created from a previous point
368 // to some distance, but in the area of the highlighting of the point. Constraint
369 // coincidence is created, after the solver is performed, the distance between the
370 // points of the line becomes less than the tolerance. Validator of the line returns
371 // false, the line will be aborted, but sketch stays valid.
372 updateObject(feature());
373 emit vertexSelected();
374 emit focusOutWidget(this);
378 // End of Bug dependent fragment
380 // A case when point is taken from mouse event
381 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
383 PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
385 // if the feature contains the point, focus is not switched
386 if (!setPoint(aX, anY) || isFeatureContainsPoint(myFeature, aX, anY))
389 /// Start alternative code
390 //std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
391 // GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
392 //QList<FeaturePtr> aIgnore;
393 //aIgnore.append(feature());
395 //double aTolerance = aView->Convert(7);
396 //std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt =
397 // PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
398 //if (aAttrPnt.get() != NULL) {
399 // aFeaturePoint->setValue(aAttrPnt->pnt());
400 // PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
401 // emit vertexSelected();
403 /// End alternative code
404 emit focusOutWidget(this);
409 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
414 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
417 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
421 double PartSet_WidgetPoint2D::x() const
423 return myXSpin->value();
426 double PartSet_WidgetPoint2D::y() const
428 return myYSpin->value();
432 bool PartSet_WidgetPoint2D::isFeatureContainsPoint(const FeaturePtr& theFeature,
433 double theX, double theY)
435 bool aPointIsFound = false;
436 AttributePtr aWidgetAttribute = myFeature->attribute(attributeID());
438 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d =
439 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
440 std::list<AttributePtr> anAttributes =
441 myFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
442 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
443 for(; anIter != anAttributes.end(); anIter++) {
444 AttributePoint2DPtr aPoint2DAttribute =
445 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
446 if (aPoint2DAttribute == aWidgetAttribute)
448 if (aPoint2DAttribute.get()) {
449 aPointIsFound = aPoint2DAttribute->pnt()->isEqual(aPnt2d);
452 return aPointIsFound;
455 void PartSet_WidgetPoint2D::onValuesChanged()
457 myLockApplyMgr->valuesChanged();
458 emit valuesChanged();