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);
149 else if (canBeActivatedByMove()) {
150 if (feature()->getKind() == SketchPlugin_Line::ID()) {
151 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aValue.object());
152 // Initialise new line with first point equal to end of previous
153 if (aFeature.get()) {
154 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
155 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
156 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
157 aData->attribute(SketchPlugin_Line::END_ID()));
159 setPoint(aPoint->x(), aPoint->y());
160 PartSet_Tools::setConstraints(mySketch, feature(), attributeID(), aPoint->x(),
170 void PartSet_WidgetPoint2D::selectContent()
172 myXSpin->selectAll();
175 bool PartSet_WidgetPoint2D::setPoint(double theX, double theY)
177 if (fabs(theX) >= MaxCoordinate)
179 if (fabs(theY) >= MaxCoordinate)
182 ModuleBase_Tools::setSpinValue(myXSpin, theX);
183 ModuleBase_Tools::setSpinValue(myYSpin, theY);
189 bool PartSet_WidgetPoint2D::storeValueCustom() const
191 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
192 if (!aData) // can be on abort of sketcher element
194 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
195 aData->attribute(attributeID()));
197 PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
198 bool isBlocked = that->blockSignals(true);
199 bool isImmutable = aPoint->setImmutable(true);
201 // if text is not empty then setValue will be ignored
202 // so we should set the text at first
203 aPoint->setText(myXSpin->hasVariable() ? myXSpin->text().toStdString() : "",
204 myYSpin->hasVariable() ? myYSpin->text().toStdString() : "");
205 aPoint->setValue(!myXSpin->hasVariable() ? myXSpin->value() : aPoint->x(),
206 !myYSpin->hasVariable() ? myYSpin->value() : aPoint->y());
208 // after movement the solver will call the update event: optimization
209 moveObject(myFeature);
210 aPoint->setImmutable(isImmutable);
211 that->blockSignals(isBlocked);
216 bool PartSet_WidgetPoint2D::restoreValueCustom()
218 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
219 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
220 aData->attribute(attributeID()));
221 QString aTextX = QString::fromStdString(aPoint->textX());
222 QString aTextY = QString::fromStdString(aPoint->textY());
224 bool isDouble = false;
226 if (aTextX.isEmpty()) {
227 ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
229 aVal = aTextX.toDouble(&isDouble);
231 ModuleBase_Tools::setSpinValue(myXSpin, aVal);
233 ModuleBase_Tools::setSpinText(myXSpin, aTextX);
235 if (aTextY.isEmpty()) {
236 ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
238 aVal = aTextY.toDouble(&isDouble);
240 ModuleBase_Tools::setSpinValue(myYSpin, aVal);
242 ModuleBase_Tools::setSpinText(myYSpin, aTextY);
244 //if (aTextX.empty() || aTextY.empty()) {
245 // ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
246 // ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
248 // ModuleBase_Tools::setSpinText(myXSpin, QString::fromStdString(aTextX));
249 // ModuleBase_Tools::setSpinText(myYSpin, QString::fromStdString(aTextY));
254 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
256 QList<QWidget*> aControls;
257 aControls.append(myXSpin);
258 aControls.append(myYSpin);
263 void PartSet_WidgetPoint2D::activateCustom()
265 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
266 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
267 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
268 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
269 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
272 aModes << TopAbs_VERTEX;
273 aModes << TopAbs_EDGE;
274 myWorkshop->activateSubShapesSelection(aModes);
277 bool PartSet_WidgetPoint2D::canBeActivatedByMove()
279 bool aCanBeActivated = false;
280 if (feature()->getKind() == SketchPlugin_Line::ID() &&
281 attributeID() == SketchPlugin_Line::START_ID())
282 aCanBeActivated = true;
284 return aCanBeActivated;
287 void PartSet_WidgetPoint2D::deactivate()
289 ModuleBase_ModelWidget::deactivate();
290 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
291 disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
292 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
293 disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
294 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
296 myWorkshop->deactivateSubShapesSelection();
299 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView,
300 const TopoDS_Shape& theShape,
301 double& theX, double& theY) const
303 if (!theShape.IsNull()) {
304 if (theShape.ShapeType() == TopAbs_VERTEX) {
305 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
306 if (!aVertex.IsNull()) {
307 // A case when point is taken from existing vertex
308 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
309 PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
317 void PartSet_WidgetPoint2D::setConstraintWith(const ObjectPtr& theObject)
319 // Create point-edge coincedence
320 FeaturePtr aFeature = mySketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
321 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
323 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
324 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
325 AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
326 std::shared_ptr<GeomDataAPI_Point2D> aThisPoint =
327 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
328 aRef1->setAttr(aThisPoint);
330 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
331 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
332 aRef2->setObject(theObject);
337 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
339 // the contex menu release by the right button should not be processed by this widget
340 if (theEvent->button() != Qt::LeftButton)
343 ModuleBase_ISelection* aSelection = myWorkshop->selection();
344 Handle(V3d_View) aView = theWnd->v3dView();
345 // TODO: This fragment doesn't work because bug in OCC Viewer. It can be used after fixing.
346 NCollection_List<TopoDS_Shape> aShapes;
347 std::list<ObjectPtr> aObjects;
348 aSelection->selectedShapes(aShapes, aObjects);
349 // if we have selection
350 if (aShapes.Extent() > 0) {
351 TopoDS_Shape aShape = aShapes.First();
352 ObjectPtr aObject = aObjects.front();
353 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aObject);
354 bool anExternal = false;
355 std::shared_ptr<SketchPlugin_Feature> aSPFeature;
356 if (aSelectedFeature.get() != NULL)
357 aSPFeature = std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
358 if ((!aSPFeature) && (!aShape.IsNull())) {
360 ResultPtr aFixedObject = PartSet_Tools::findFixedObjectByExternal(aShape, aObject, mySketch);
361 if (!aFixedObject.get())
362 aObject = PartSet_Tools::createFixedObjectByExternal(aShape, aObject, mySketch);
365 if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
366 // do not create a constraint to the point, which already used by the feature
367 // if the feature contains the point, focus is not switched
371 if (getPoint2d(aView, aShape, aX, aY))
373 bool anOrphanPoint = isOrphanPoint(aSelectedFeature, mySketch);
374 setConstraintWith(aObject);
376 emit vertexSelected();
377 emit focusOutWidget(this);
382 bool isProcessed = false;
383 if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
384 // when the point is selected, the coordinates of the point should be set into the attribute
385 // if the feature contains the point, focus is not switched
389 bool anOrphanPoint = isOrphanPoint(aSelectedFeature, mySketch);
390 // do not set a coincidence constraint in the attribute if the feature contains a point
391 // with the same coordinates. It is important for line creation in order to do not set
392 // the same constraints for the same points, oterwise the result line has zero length.
393 if (getPoint2d(aView, aShape, aX, aY)) {
395 PartSet_Tools::setConstraints(mySketch, feature(), attributeID(), aX, aY);
397 else if (aShape.ShapeType() == TopAbs_EDGE) {
398 if (MyFeaturesForCoincedence.contains(myFeature->getKind().c_str()))
399 setConstraintWith(aObject);
401 // it is important to perform updateObject() in order to the current value is
402 // processed by Sketch Solver. Test case: line is created from a previous point
403 // to some distance, but in the area of the highlighting of the point. Constraint
404 // coincidence is created, after the solver is performed, the distance between the
405 // points of the line becomes less than the tolerance. Validator of the line returns
406 // false, the line will be aborted, but sketch stays valid.
407 updateObject(feature());
409 emit vertexSelected();
410 emit focusOutWidget(this);
414 // End of Bug dependent fragment
416 // A case when point is taken from mouse event
417 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
419 PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
421 // if the feature contains the point, focus is not switched
422 if (!setPoint(aX, anY) || isFeatureContainsPoint(myFeature, aX, anY))
425 /// Start alternative code
426 //std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
427 // GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
428 //QList<FeaturePtr> aIgnore;
429 //aIgnore.append(feature());
431 //double aTolerance = aView->Convert(7);
432 //std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt =
433 // PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
434 //if (aAttrPnt.get() != NULL) {
435 // aFeaturePoint->setValue(aAttrPnt->pnt());
436 // PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
437 // emit vertexSelected();
439 /// End alternative code
440 emit focusOutWidget(this);
445 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
450 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
453 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
454 // we need to block the value state change
455 bool isBlocked = blockValueState(true);
457 blockValueState(isBlocked);
458 setValueState(ModifiedInViewer);
461 double PartSet_WidgetPoint2D::x() const
463 return myXSpin->value();
466 double PartSet_WidgetPoint2D::y() const
468 return myYSpin->value();
472 bool PartSet_WidgetPoint2D::isFeatureContainsPoint(const FeaturePtr& theFeature,
473 double theX, double theY)
475 bool aPointIsFound = false;
477 if (feature()->getKind() != SketchPlugin_Line::ID())
478 return aPointIsFound;
480 AttributePtr aWidgetAttribute = myFeature->attribute(attributeID());
482 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d =
483 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
484 std::list<AttributePtr> anAttributes =
485 myFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
486 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
487 for(; anIter != anAttributes.end() && !aPointIsFound; anIter++) {
488 AttributePoint2DPtr aPoint2DAttribute =
489 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
490 if (aPoint2DAttribute == aWidgetAttribute)
492 if (aPoint2DAttribute.get() && aPoint2DAttribute->isInitialized()) {
493 aPointIsFound = aPoint2DAttribute->pnt()->isEqual(aPnt2d);
496 return aPointIsFound;
499 /*void PartSet_WidgetPoint2D::onValuesChanged()
501 emit valuesChanged();
504 bool PartSet_WidgetPoint2D::processEnter()
506 //bool isModified = myXSpin->isModified() || myYSpin->isModified();
507 bool isModified = getValueState() == ModifiedInPP;
509 bool isXModified = myXSpin->hasFocus();//myXSpin->isModified();
510 emit valuesChanged();
512 //myXSpin->clearModified();
513 //myYSpin->clearModified();
515 myXSpin->selectAll();
517 myYSpin->selectAll();
522 bool PartSet_WidgetPoint2D::isOrphanPoint(const FeaturePtr& theFeature,
523 const CompositeFeaturePtr& theSketch)
525 bool anOrphanPoint = false;
526 if (theFeature.get()) {
527 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
528 std::string aFeatureKind = theFeature->getKind();
529 if (aFeatureKind == SketchPlugin_Point::ID())
530 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
531 theFeature->attribute(SketchPlugin_Point::COORD_ID()));
532 else if (aFeatureKind == SketchPlugin_Circle::ID())
533 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
534 theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
536 else if (aFeatureKind == SketchPlugin_Arc::ID())
537 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
538 theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
540 if (aPointAttr.get()) {
541 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
542 FeaturePtr aCoincidence = PartSet_Tools::findFirstCoincidence(theFeature, aPoint);
543 anOrphanPoint = true;
544 // if there is at least one concident line to the point, the point is not an orphant
545 if (aCoincidence.get()) {
546 QList<FeaturePtr> aCoinsideLines;
547 PartSet_Tools::findCoincidences(aCoincidence, aCoinsideLines,
548 SketchPlugin_ConstraintCoincidence::ENTITY_A());
549 PartSet_Tools::findCoincidences(aCoincidence, aCoinsideLines,
550 SketchPlugin_ConstraintCoincidence::ENTITY_B());
551 QList<FeaturePtr>::const_iterator anIt = aCoinsideLines.begin(),
552 aLast = aCoinsideLines.end();
553 for (; anIt != aLast && anOrphanPoint; anIt++) {
554 anOrphanPoint = (*anIt)->getKind() != SketchPlugin_Line::ID();
559 return anOrphanPoint;