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 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);
74 QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
75 ModuleBase_Tools::adjustMargins(aGroupLay);
76 aGroupLay->setSpacing(2);
77 aGroupLay->setColumnStretch(1, 1);
79 QLabel* aLabel = new QLabel(myGroupBox);
80 aLabel->setText(tr("X "));
81 aGroupLay->addWidget(aLabel, 0, 0);
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);
89 connect(myXSpin, SIGNAL(valueChanged(const QString&)), this, SIGNAL(valuesModified()));
92 QLabel* aLabel = new QLabel(myGroupBox);
93 aLabel->setText(tr("Y "));
94 aGroupLay->addWidget(aLabel, 1, 0);
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);
102 connect(myYSpin, SIGNAL(valueChanged(const QString&)), this, SIGNAL(valuesModified()));
104 QVBoxLayout* aLayout = new QVBoxLayout(this);
105 ModuleBase_Tools::zeroMargins(aLayout);
106 aLayout->addWidget(myGroupBox);
110 bool PartSet_WidgetPoint2D::resetCustom()
113 if (!isUseReset() || isComputedDefault() || myXSpin->hasVariable() || myYSpin->hasVariable()) {
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);
129 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
133 bool PartSet_WidgetPoint2D::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
134 const bool theToValidate)
137 if (theValues.empty())
140 ModuleBase_ViewerPrs aValue = theValues.takeFirst();
141 TopoDS_Shape aShape = aValue.shape();
142 if (!aShape.IsNull()) {
143 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
145 if (getPoint2d(aView, aShape, aX, aY)) {
146 isDone = setPoint(aX, aY);
147 PartSet_Tools::setConstraints(mySketch, feature(), attributeID(), aX, aY);
150 else if (canBeActivatedByMove()) {
151 if (feature()->getKind() == SketchPlugin_Line::ID()) {
152 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aValue.object());
153 // Initialise new line with first point equal to end of previous
154 if (aFeature.get()) {
155 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
156 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
157 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
158 aData->attribute(SketchPlugin_Line::END_ID()));
160 setPoint(aPoint->x(), aPoint->y());
161 PartSet_Tools::setConstraints(mySketch, feature(), attributeID(), aPoint->x(),
171 void PartSet_WidgetPoint2D::selectContent()
173 myXSpin->selectAll();
176 bool PartSet_WidgetPoint2D::setPoint(double theX, double theY)
178 if (fabs(theX) >= MaxCoordinate)
180 if (fabs(theY) >= MaxCoordinate)
183 ModuleBase_Tools::setSpinValue(myXSpin, theX);
184 ModuleBase_Tools::setSpinValue(myYSpin, theY);
190 bool PartSet_WidgetPoint2D::storeValueCustom() const
192 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
193 if (!aData) // can be on abort of sketcher element
195 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
196 aData->attribute(attributeID()));
198 PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
199 bool isBlocked = that->blockSignals(true);
200 bool isImmutable = aPoint->setImmutable(true);
202 // if text is not empty then setValue will be ignored
203 // so we should set the text at first
204 aPoint->setText(myXSpin->hasVariable() ? myXSpin->text().toStdString() : "",
205 myYSpin->hasVariable() ? myYSpin->text().toStdString() : "");
206 aPoint->setValue(!myXSpin->hasVariable() ? myXSpin->value() : aPoint->x(),
207 !myYSpin->hasVariable() ? myYSpin->value() : aPoint->y());
209 // after movement the solver will call the update event: optimization
210 moveObject(myFeature);
211 aPoint->setImmutable(isImmutable);
212 that->blockSignals(isBlocked);
217 bool PartSet_WidgetPoint2D::restoreValueCustom()
219 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
220 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
221 aData->attribute(attributeID()));
222 QString aTextX = QString::fromStdString(aPoint->textX());
223 QString aTextY = QString::fromStdString(aPoint->textY());
225 bool isDouble = false;
227 if (aTextX.isEmpty()) {
228 ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
230 aVal = aTextX.toDouble(&isDouble);
232 ModuleBase_Tools::setSpinValue(myXSpin, aVal);
234 ModuleBase_Tools::setSpinText(myXSpin, aTextX);
236 if (aTextY.isEmpty()) {
237 ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
239 aVal = aTextY.toDouble(&isDouble);
241 ModuleBase_Tools::setSpinValue(myYSpin, aVal);
243 ModuleBase_Tools::setSpinText(myYSpin, aTextY);
245 //if (aTextX.empty() || aTextY.empty()) {
246 // ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
247 // ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
249 // ModuleBase_Tools::setSpinText(myXSpin, QString::fromStdString(aTextX));
250 // ModuleBase_Tools::setSpinText(myYSpin, QString::fromStdString(aTextY));
255 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
257 QList<QWidget*> aControls;
258 aControls.append(myXSpin);
259 aControls.append(myYSpin);
264 void PartSet_WidgetPoint2D::activateCustom()
266 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
267 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
268 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
269 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
270 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
273 aModes << TopAbs_VERTEX;
274 aModes << TopAbs_EDGE;
275 myWorkshop->activateSubShapesSelection(aModes);
278 bool PartSet_WidgetPoint2D::canBeActivatedByMove()
280 bool aCanBeActivated = false;
281 if (feature()->getKind() == SketchPlugin_Line::ID() &&
282 attributeID() == SketchPlugin_Line::START_ID())
283 aCanBeActivated = true;
285 return aCanBeActivated;
288 void PartSet_WidgetPoint2D::deactivate()
290 // the value of the control should be stored to model if it was not
291 // initialized yet. It is important when we leave this control by Tab key.
292 // It should not be performed by the widget activation as the preview
293 // is visualized with default value. Line point is moved to origin.
294 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
295 if (anAttribute && !anAttribute->isInitialized())
298 ModuleBase_ModelWidget::deactivate();
299 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
300 disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
301 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
302 disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
303 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
305 myWorkshop->deactivateSubShapesSelection();
308 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView,
309 const TopoDS_Shape& theShape,
310 double& theX, double& theY) const
312 if (!theShape.IsNull()) {
313 if (theShape.ShapeType() == TopAbs_VERTEX) {
314 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
315 if (!aVertex.IsNull()) {
316 // A case when point is taken from existing vertex
317 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
318 PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
326 void PartSet_WidgetPoint2D::setConstraintWith(const ObjectPtr& theObject)
328 // Create point-edge coincedence
329 FeaturePtr aFeature = mySketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
330 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
332 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
333 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
334 AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
335 std::shared_ptr<GeomDataAPI_Point2D> aThisPoint =
336 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
337 aRef1->setAttr(aThisPoint);
339 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
340 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
341 aRef2->setObject(theObject);
346 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
348 // the contex menu release by the right button should not be processed by this widget
349 if (theEvent->button() != Qt::LeftButton)
352 ModuleBase_ISelection* aSelection = myWorkshop->selection();
353 Handle(V3d_View) aView = theWnd->v3dView();
354 // TODO: This fragment doesn't work because bug in OCC Viewer. It can be used after fixing.
355 NCollection_List<TopoDS_Shape> aShapes;
356 std::list<ObjectPtr> aObjects;
357 aSelection->selectedShapes(aShapes, aObjects);
358 // if we have selection
359 if (aShapes.Extent() > 0) {
360 TopoDS_Shape aShape = aShapes.First();
361 ObjectPtr aObject = aObjects.front();
362 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aObject);
363 bool anExternal = false;
364 std::shared_ptr<SketchPlugin_Feature> aSPFeature;
365 if (aSelectedFeature.get() != NULL)
366 aSPFeature = std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
367 if ((!aSPFeature) && (!aShape.IsNull())) {
369 ResultPtr aFixedObject = PartSet_Tools::findFixedObjectByExternal(aShape, aObject, mySketch);
370 if (!aFixedObject.get())
371 aObject = PartSet_Tools::createFixedObjectByExternal(aShape, aObject, mySketch);
374 if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
375 // do not create a constraint to the point, which already used by the feature
376 // if the feature contains the point, focus is not switched
380 if (getPoint2d(aView, aShape, aX, aY))
382 bool anOrphanPoint = aShape.ShapeType() == TopAbs_VERTEX ||
383 isOrphanPoint(aSelectedFeature, mySketch, aX, aY);
384 setConstraintWith(aObject);
386 emit vertexSelected();
387 emit focusOutWidget(this);
392 bool isProcessed = false;
393 if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
394 // when the point is selected, the coordinates of the point should be set into the attribute
395 // if the feature contains the point, focus is not switched
399 bool anOrphanPoint = isOrphanPoint(aSelectedFeature, mySketch, aX, aY);
400 // do not set a coincidence constraint in the attribute if the feature contains a point
401 // with the same coordinates. It is important for line creation in order to do not set
402 // the same constraints for the same points, oterwise the result line has zero length.
403 if (getPoint2d(aView, aShape, aX, aY)) {
405 PartSet_Tools::setConstraints(mySketch, feature(), attributeID(), aX, aY);
407 else if (aShape.ShapeType() == TopAbs_EDGE) {
408 if (MyFeaturesForCoincedence.contains(myFeature->getKind().c_str()))
409 setConstraintWith(aObject);
411 // it is important to perform updateObject() in order to the current value is
412 // processed by Sketch Solver. Test case: line is created from a previous point
413 // to some distance, but in the area of the highlighting of the point. Constraint
414 // coincidence is created, after the solver is performed, the distance between the
415 // points of the line becomes less than the tolerance. Validator of the line returns
416 // false, the line will be aborted, but sketch stays valid.
417 updateObject(feature());
419 emit vertexSelected();
420 emit focusOutWidget(this);
424 // End of Bug dependent fragment
426 // A case when point is taken from mouse event
427 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
429 PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
431 // if the feature contains the point, focus is not switched
432 if (!setPoint(aX, anY) || isFeatureContainsPoint(myFeature, aX, anY))
435 /// Start alternative code
436 //std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
437 // GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
438 //QList<FeaturePtr> aIgnore;
439 //aIgnore.append(feature());
441 //double aTolerance = aView->Convert(7);
442 //std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt =
443 // PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
444 //if (aAttrPnt.get() != NULL) {
445 // aFeaturePoint->setValue(aAttrPnt->pnt());
446 // PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
447 // emit vertexSelected();
449 /// End alternative code
450 emit focusOutWidget(this);
455 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
460 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
463 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
464 // we need to block the value state change
465 bool isBlocked = blockValueState(true);
467 blockValueState(isBlocked);
468 setValueState(ModifiedInViewer);
471 double PartSet_WidgetPoint2D::x() const
473 return myXSpin->value();
476 double PartSet_WidgetPoint2D::y() const
478 return myYSpin->value();
482 bool PartSet_WidgetPoint2D::isFeatureContainsPoint(const FeaturePtr& theFeature,
483 double theX, double theY)
485 bool aPointIsFound = false;
487 if (feature()->getKind() != SketchPlugin_Line::ID())
488 return aPointIsFound;
490 AttributePtr aWidgetAttribute = myFeature->attribute(attributeID());
492 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d =
493 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
494 std::list<AttributePtr> anAttributes =
495 myFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
496 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
497 for(; anIter != anAttributes.end() && !aPointIsFound; anIter++) {
498 AttributePoint2DPtr aPoint2DAttribute =
499 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
500 if (aPoint2DAttribute == aWidgetAttribute)
502 if (aPoint2DAttribute.get() && aPoint2DAttribute->isInitialized()) {
503 aPointIsFound = aPoint2DAttribute->pnt()->isEqual(aPnt2d);
506 return aPointIsFound;
509 void PartSet_WidgetPoint2D::initializeValueByActivate()
513 /*void PartSet_WidgetPoint2D::onValuesChanged()
515 emit valuesChanged();
518 bool PartSet_WidgetPoint2D::processEnter()
520 bool isModified = getValueState() == ModifiedInPP;
522 bool isXModified = myXSpin->hasFocus();
523 emit valuesChanged();
525 myXSpin->selectAll();
527 myYSpin->selectAll();
532 bool PartSet_WidgetPoint2D::isOrphanPoint(const FeaturePtr& theFeature,
533 const CompositeFeaturePtr& theSketch,
534 double theX, double theY)
536 bool anOrphanPoint = false;
537 if (theFeature.get()) {
538 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
539 std::string aFeatureKind = theFeature->getKind();
540 if (aFeatureKind == SketchPlugin_Point::ID())
541 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
542 theFeature->attribute(SketchPlugin_Point::COORD_ID()));
543 else if (aFeatureKind == SketchPlugin_Circle::ID())
544 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
545 theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
547 else if (aFeatureKind == SketchPlugin_Arc::ID())
548 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
549 theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
551 /// check that the geometry point with the given coordinates is the checked point
552 /// e.g. in arc the (x,y) point can not coicide to the center point and it automatically
553 /// means that this point is not an orphant one.
554 if (aPointAttr.get()) {
555 std::shared_ptr<GeomAPI_Pnt2d> aCheckedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
556 new GeomAPI_Pnt2d(theX, theY));
557 if (!aCheckedPoint->isEqual(aPointAttr->pnt()))
558 return anOrphanPoint;
561 if (aPointAttr.get()) {
562 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
563 FeaturePtr aCoincidence = PartSet_Tools::findFirstCoincidence(theFeature, aPoint);
564 anOrphanPoint = true;
565 // if there is at least one concident line to the point, the point is not an orphant
566 if (aCoincidence.get()) {
567 QList<FeaturePtr> aCoinsideLines;
568 PartSet_Tools::findCoincidences(aCoincidence, aCoinsideLines,
569 SketchPlugin_ConstraintCoincidence::ENTITY_A());
570 PartSet_Tools::findCoincidences(aCoincidence, aCoinsideLines,
571 SketchPlugin_ConstraintCoincidence::ENTITY_B());
572 QList<FeaturePtr>::const_iterator anIt = aCoinsideLines.begin(),
573 aLast = aCoinsideLines.end();
574 for (; anIt != aLast && anOrphanPoint; anIt++) {
575 anOrphanPoint = (*anIt)->getKind() != SketchPlugin_Line::ID();
580 return anOrphanPoint;