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>
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>
17 #include <Config_Keywords.h>
18 #include <Config_WidgetAPI.h>
20 #include <Events_Loop.h>
21 #include <ModelAPI_Events.h>
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>
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>
37 #include <QGridLayout>
40 #include <QMouseEvent>
41 #include <QApplication>
44 #include <TopoDS_Vertex.hxx>
45 #include <BRep_Tool.hxx>
50 const double MaxCoordinate = 1e12;
52 static QStringList MyFeaturesForCoincedence;
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)
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();
67 // the control should accept the focus, so the boolean 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);
74 bool aAcceptVariables = theData->getBooleanAttribute(DOUBLE_WDG_ACCEPT_EXPRESSIONS, true);
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->setAcceptVariables(aAcceptVariables);
87 myXSpin->setMinimum(-DBL_MAX);
88 myXSpin->setMaximum(DBL_MAX);
89 myXSpin->setToolTip(tr("X"));
90 aGroupLay->addWidget(myXSpin, 0, 1);
92 connect(myXSpin, SIGNAL(valueChanged(const QString&)), this, SIGNAL(valuesModified()));
95 QLabel* aLabel = new QLabel(myGroupBox);
96 aLabel->setText(tr("Y "));
97 aGroupLay->addWidget(aLabel, 1, 0);
99 myYSpin = new ModuleBase_ParamSpinBox(myGroupBox);
100 myYSpin->setAcceptVariables(aAcceptVariables);
101 myYSpin->setMinimum(-DBL_MAX);
102 myYSpin->setMaximum(DBL_MAX);
103 myYSpin->setToolTip(tr("Y"));
104 aGroupLay->addWidget(myYSpin, 1, 1);
106 connect(myYSpin, SIGNAL(valueChanged(const QString&)), this, SIGNAL(valuesModified()));
108 QVBoxLayout* aLayout = new QVBoxLayout(this);
109 ModuleBase_Tools::zeroMargins(aLayout);
110 aLayout->addWidget(myGroupBox);
114 bool PartSet_WidgetPoint2D::resetCustom()
117 if (!isUseReset() || isComputedDefault() || myXSpin->hasVariable() || myYSpin->hasVariable()) {
122 double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
123 // it is important to block the spin box control in order to do not through out the
124 // locking of the validating state.
125 ModuleBase_Tools::setSpinValue(myXSpin, isOk ? aDefValue : 0.0);
126 ModuleBase_Tools::setSpinValue(myYSpin, isOk ? aDefValue : 0.0);
133 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
137 bool PartSet_WidgetPoint2D::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
138 const bool theToValidate)
141 if (theValues.empty())
144 ModuleBase_ViewerPrs aValue = theValues.takeFirst();
145 TopoDS_Shape aShape = aValue.shape();
146 if (!aShape.IsNull()) {
147 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
149 if (getPoint2d(aView, aShape, aX, aY)) {
150 isDone = setPoint(aX, aY);
151 PartSet_Tools::setConstraints(mySketch, feature(), attributeID(), aX, aY);
154 else if (canBeActivatedByMove()) {
155 if (feature()->getKind() == SketchPlugin_Line::ID()) {
156 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aValue.object());
157 // Initialize new line with first point equal to end of previous
158 if (aFeature.get()) {
159 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
160 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
161 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
162 aData->attribute(SketchPlugin_Line::END_ID()));
164 setPoint(aPoint->x(), aPoint->y());
165 PartSet_Tools::setConstraints(mySketch, feature(), attributeID(), aPoint->x(),
175 void PartSet_WidgetPoint2D::selectContent()
177 myXSpin->selectAll();
180 bool PartSet_WidgetPoint2D::setPoint(double theX, double theY)
182 if (fabs(theX) >= MaxCoordinate)
184 if (fabs(theY) >= MaxCoordinate)
187 ModuleBase_Tools::setSpinValue(myXSpin, theX);
188 ModuleBase_Tools::setSpinValue(myYSpin, theY);
194 bool PartSet_WidgetPoint2D::storeValueCustom() const
196 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
197 if (!aData) // can be on abort of sketcher element
199 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
200 aData->attribute(attributeID()));
202 PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
203 bool isBlocked = that->blockSignals(true);
204 bool isImmutable = aPoint->setImmutable(true);
206 // if text is not empty then setValue will be ignored
207 // so we should set the text at first
208 aPoint->setText(myXSpin->hasVariable() ? myXSpin->text().toStdString() : "",
209 myYSpin->hasVariable() ? myYSpin->text().toStdString() : "");
210 aPoint->setValue(!myXSpin->hasVariable() ? myXSpin->value() : aPoint->x(),
211 !myYSpin->hasVariable() ? myYSpin->value() : aPoint->y());
213 // after movement the solver will call the update event: optimization
214 moveObject(myFeature);
215 aPoint->setImmutable(isImmutable);
216 that->blockSignals(isBlocked);
221 bool PartSet_WidgetPoint2D::restoreValueCustom()
223 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
224 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
225 aData->attribute(attributeID()));
226 QString aTextX = QString::fromStdString(aPoint->textX());
227 QString aTextY = QString::fromStdString(aPoint->textY());
229 bool isDouble = false;
231 if (aTextX.isEmpty()) {
232 ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
234 aVal = aTextX.toDouble(&isDouble);
236 ModuleBase_Tools::setSpinValue(myXSpin, aVal);
238 ModuleBase_Tools::setSpinText(myXSpin, aTextX);
240 if (aTextY.isEmpty()) {
241 ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
243 aVal = aTextY.toDouble(&isDouble);
245 ModuleBase_Tools::setSpinValue(myYSpin, aVal);
247 ModuleBase_Tools::setSpinText(myYSpin, aTextY);
249 //if (aTextX.empty() || aTextY.empty()) {
250 // ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
251 // ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
253 // ModuleBase_Tools::setSpinText(myXSpin, QString::fromStdString(aTextX));
254 // ModuleBase_Tools::setSpinText(myYSpin, QString::fromStdString(aTextY));
259 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
261 QList<QWidget*> aControls;
262 aControls.append(myXSpin);
263 aControls.append(myYSpin);
268 void PartSet_WidgetPoint2D::activateCustom()
270 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
271 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
272 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
273 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
274 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
277 aModes << TopAbs_VERTEX;
278 aModes << TopAbs_EDGE;
279 myWorkshop->activateSubShapesSelection(aModes);
282 bool PartSet_WidgetPoint2D::canBeActivatedByMove()
284 bool aCanBeActivated = false;
285 if (feature()->getKind() == SketchPlugin_Line::ID() &&
286 attributeID() == SketchPlugin_Line::START_ID())
287 aCanBeActivated = true;
289 return aCanBeActivated;
292 void PartSet_WidgetPoint2D::deactivate()
294 // the value of the control should be stored to model if it was not
295 // initialized yet. It is important when we leave this control by Tab key.
296 // It should not be performed by the widget activation as the preview
297 // is visualized with default value. Line point is moved to origin.
298 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
299 if (anAttribute && !anAttribute->isInitialized())
302 ModuleBase_ModelWidget::deactivate();
303 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
304 disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
305 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
306 disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
307 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
309 myWorkshop->deactivateSubShapesSelection();
312 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView,
313 const TopoDS_Shape& theShape,
314 double& theX, double& theY) const
316 if (!theShape.IsNull()) {
317 if (theShape.ShapeType() == TopAbs_VERTEX) {
318 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
319 if (!aVertex.IsNull()) {
320 // A case when point is taken from existing vertex
321 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
322 PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
330 void PartSet_WidgetPoint2D::setConstraintWith(const ObjectPtr& theObject)
332 // Create point-edge coincedence
333 FeaturePtr aFeature = mySketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
334 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
336 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
337 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
338 AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
339 std::shared_ptr<GeomDataAPI_Point2D> aThisPoint =
340 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
341 aRef1->setAttr(aThisPoint);
343 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
344 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
345 aRef2->setObject(theObject);
350 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
352 // the contex menu release by the right button should not be processed by this widget
353 if (theEvent->button() != Qt::LeftButton)
356 ModuleBase_ISelection* aSelection = myWorkshop->selection();
357 Handle(V3d_View) aView = theWnd->v3dView();
358 // TODO: This fragment doesn't work because bug in OCC Viewer. It can be used after fixing.
359 NCollection_List<TopoDS_Shape> aShapes;
360 std::list<ObjectPtr> aObjects;
361 aSelection->selectedShapes(aShapes, aObjects);
362 // if we have selection and use it
363 if (aShapes.Extent() > 0 && useSelectedShapes()) {
364 TopoDS_Shape aShape = aShapes.First();
365 ObjectPtr aObject = aObjects.front();
366 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aObject);
367 bool anExternal = false;
368 std::shared_ptr<SketchPlugin_Feature> aSPFeature;
369 if (aSelectedFeature.get() != NULL)
370 aSPFeature = std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
371 if ((!aSPFeature && !aShape.IsNull()) ||
372 (aSPFeature.get() && aSPFeature->isExternal())) {
374 ResultPtr aFixedObject = PartSet_Tools::findFixedObjectByExternal(aShape, aObject, mySketch);
375 if (!aFixedObject.get())
376 aObject = PartSet_Tools::createFixedObjectByExternal(aShape, aObject, mySketch);
378 aObject = aFixedObject;
381 if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
382 // do not create a constraint to the point, which already used by the feature
383 // if the feature contains the point, focus is not switched
387 if (getPoint2d(aView, aShape, aX, aY))
390 setValueState(Stored); // in case of edge selection, Apply state should also be updated
391 bool anOrphanPoint = aShape.ShapeType() == TopAbs_VERTEX ||
392 isOrphanPoint(aSelectedFeature, mySketch, aX, aY);
393 setConstraintWith(aObject);
394 // fignal updated should be flushed in order to visualize possible created external objects
395 // e.g. selection of trihedron axis when input end arc point
396 updateObject(feature());
397 if (!anOrphanPoint && !anExternal)
398 emit vertexSelected();
400 emit focusOutWidget(this);
405 bool isProcessed = false;
406 if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
407 // when the point is selected, the coordinates of the point should be set into the attribute
408 // if the feature contains the point, focus is not switched
412 bool anOrphanPoint = isOrphanPoint(aSelectedFeature, mySketch, aX, aY);
413 // do not set a coincidence constraint in the attribute if the feature contains a point
414 // with the same coordinates. It is important for line creation in order to do not set
415 // the same constraints for the same points, oterwise the result line has zero length.
416 if (getPoint2d(aView, aShape, aX, aY)) {
418 PartSet_Tools::setConstraints(mySketch, feature(), attributeID(), aX, aY);
420 else if (aShape.ShapeType() == TopAbs_EDGE) {
421 if (MyFeaturesForCoincedence.contains(myFeature->getKind().c_str())) {
422 setConstraintWith(aObject);
423 setValueState(Stored); // in case of edge selection, Apply state should also be updated
426 // it is important to perform updateObject() in order to the current value is
427 // processed by Sketch Solver. Test case: line is created from a previous point
428 // to some distance, but in the area of the highlighting of the point. Constraint
429 // coincidence is created, after the solver is performed, the distance between the
430 // points of the line becomes less than the tolerance. Validator of the line returns
431 // false, the line will be aborted, but sketch stays valid.
432 updateObject(feature());
433 if (!anOrphanPoint && !anExternal)
434 emit vertexSelected();
435 emit focusOutWidget(this);
439 // End of Bug dependent fragment
441 // A case when point is taken from mouse event
442 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
444 PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
446 // if the feature contains the point, focus is not switched
447 if (!setPoint(aX, anY) || isFeatureContainsPoint(myFeature, aX, anY))
450 /// Start alternative code
451 //std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
452 // GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
453 //QList<FeaturePtr> aIgnore;
454 //aIgnore.append(feature());
456 //double aTolerance = aView->Convert(7);
457 //std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt =
458 // PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
459 //if (aAttrPnt.get() != NULL) {
460 // aFeaturePoint->setValue(aAttrPnt->pnt());
461 // PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
462 // emit vertexSelected();
464 /// End alternative code
465 emit focusOutWidget(this);
470 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
475 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
478 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
479 // we need to block the value state change
480 bool isBlocked = blockValueState(true);
482 blockValueState(isBlocked);
483 setValueState(ModifiedInViewer);
486 double PartSet_WidgetPoint2D::x() const
488 return myXSpin->value();
491 double PartSet_WidgetPoint2D::y() const
493 return myYSpin->value();
497 bool PartSet_WidgetPoint2D::isFeatureContainsPoint(const FeaturePtr& theFeature,
498 double theX, double theY)
500 bool aPointIsFound = false;
502 if (feature()->getKind() != SketchPlugin_Line::ID())
503 return aPointIsFound;
505 AttributePtr aWidgetAttribute = myFeature->attribute(attributeID());
507 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d =
508 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
509 std::list<AttributePtr> anAttributes =
510 myFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
511 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
512 for(; anIter != anAttributes.end() && !aPointIsFound; anIter++) {
513 AttributePoint2DPtr aPoint2DAttribute =
514 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
515 if (aPoint2DAttribute == aWidgetAttribute)
517 if (aPoint2DAttribute.get() && aPoint2DAttribute->isInitialized()) {
518 aPointIsFound = aPoint2DAttribute->pnt()->isEqual(aPnt2d);
521 return aPointIsFound;
524 void PartSet_WidgetPoint2D::initializeValueByActivate()
528 /*void PartSet_WidgetPoint2D::onValuesChanged()
530 emit valuesChanged();
533 bool PartSet_WidgetPoint2D::processEnter()
535 bool isModified = getValueState() == ModifiedInPP;
537 bool isXModified = myXSpin->hasFocus();
538 emit valuesChanged();
540 myXSpin->selectAll();
542 myYSpin->selectAll();
547 bool PartSet_WidgetPoint2D::useSelectedShapes() const
552 bool PartSet_WidgetPoint2D::isOrphanPoint(const FeaturePtr& theFeature,
553 const CompositeFeaturePtr& theSketch,
554 double theX, double theY)
556 bool anOrphanPoint = false;
557 if (theFeature.get()) {
558 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
559 std::string aFeatureKind = theFeature->getKind();
560 if (aFeatureKind == SketchPlugin_Point::ID())
561 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
562 theFeature->attribute(SketchPlugin_Point::COORD_ID()));
563 else if (aFeatureKind == SketchPlugin_Circle::ID())
564 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
565 theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
567 else if (aFeatureKind == SketchPlugin_Arc::ID())
568 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
569 theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
571 /// check that the geometry point with the given coordinates is the checked point
572 /// e.g. in arc the (x,y) point can not coicide to the center point and it automatically
573 /// means that this point is not an orphant one.
574 if (aPointAttr.get()) {
575 std::shared_ptr<GeomAPI_Pnt2d> aCheckedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
576 new GeomAPI_Pnt2d(theX, theY));
577 if (!aCheckedPoint->isEqual(aPointAttr->pnt()))
578 return anOrphanPoint;
581 if (aPointAttr.get()) {
582 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
583 FeaturePtr aCoincidence = PartSet_Tools::findFirstCoincidence(theFeature, aPoint);
584 anOrphanPoint = true;
585 // if there is at least one concident line to the point, the point is not an orphant
586 if (aCoincidence.get()) {
587 QList<FeaturePtr> aCoinsideLines;
588 PartSet_Tools::findCoincidences(aCoincidence, aCoinsideLines,
589 SketchPlugin_ConstraintCoincidence::ENTITY_A());
590 PartSet_Tools::findCoincidences(aCoincidence, aCoinsideLines,
591 SketchPlugin_ConstraintCoincidence::ENTITY_B());
592 QList<FeaturePtr>::const_iterator anIt = aCoinsideLines.begin(),
593 aLast = aCoinsideLines.end();
594 for (; anIt != aLast && anOrphanPoint; anIt++) {
595 anOrphanPoint = (*anIt)->getKind() != SketchPlugin_Line::ID();
600 return anOrphanPoint;