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)
136 if (theValues.empty())
139 ModuleBase_ViewerPrs aValue = theValues.takeFirst();
141 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
143 TopoDS_Shape aShape = aValue.shape();
145 if (getPoint2d(aView, aShape, aX, aY)) {
146 isDone = setPoint(aX, aY);
151 bool PartSet_WidgetPoint2D::setPoint(double theX, double theY)
153 if (fabs(theX) >= MaxCoordinate)
155 if (fabs(theY) >= MaxCoordinate)
158 ModuleBase_Tools::setSpinValue(myXSpin, theX);
159 ModuleBase_Tools::setSpinValue(myYSpin, theY);
165 bool PartSet_WidgetPoint2D::storeValueCustom() const
167 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
168 if (!aData) // can be on abort of sketcher element
170 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
171 aData->attribute(attributeID()));
173 PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
174 bool isBlocked = that->blockSignals(true);
175 bool isImmutable = aPoint->setImmutable(true);
177 // if text is not empty then setValue will be ignored
178 // so we should set the text at first
179 aPoint->setText(myXSpin->hasVariable() ? myXSpin->text().toStdString() : "",
180 myYSpin->hasVariable() ? myYSpin->text().toStdString() : "");
181 aPoint->setValue(!myXSpin->hasVariable() ? myXSpin->value() : aPoint->x(),
182 !myYSpin->hasVariable() ? myYSpin->value() : aPoint->y());
184 // after movement the solver will call the update event: optimization
185 moveObject(myFeature);
186 aPoint->setImmutable(isImmutable);
187 that->blockSignals(isBlocked);
192 bool PartSet_WidgetPoint2D::restoreValueCustom()
194 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
195 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
196 aData->attribute(attributeID()));
197 QString aTextX = QString::fromStdString(aPoint->textX());
198 QString aTextY = QString::fromStdString(aPoint->textY());
200 bool isDouble = false;
202 if (aTextX.isEmpty()) {
203 ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
205 aVal = aTextX.toDouble(&isDouble);
207 ModuleBase_Tools::setSpinValue(myXSpin, aVal);
209 ModuleBase_Tools::setSpinText(myXSpin, aTextX);
211 if (aTextY.isEmpty()) {
212 ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
214 aVal = aTextY.toDouble(&isDouble);
216 ModuleBase_Tools::setSpinValue(myYSpin, aVal);
218 ModuleBase_Tools::setSpinText(myYSpin, aTextY);
220 //if (aTextX.empty() || aTextY.empty()) {
221 // ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
222 // ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
224 // ModuleBase_Tools::setSpinText(myXSpin, QString::fromStdString(aTextX));
225 // ModuleBase_Tools::setSpinText(myYSpin, QString::fromStdString(aTextY));
230 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
232 QList<QWidget*> aControls;
233 aControls.append(myXSpin);
234 aControls.append(myYSpin);
239 void PartSet_WidgetPoint2D::activateCustom()
241 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
242 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
243 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
244 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
245 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
248 aModes << TopAbs_VERTEX;
249 aModes << TopAbs_EDGE;
250 myWorkshop->activateSubShapesSelection(aModes);
253 void PartSet_WidgetPoint2D::deactivate()
255 ModuleBase_ModelWidget::deactivate();
256 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
257 disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
258 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
259 disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
260 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
262 myWorkshop->deactivateSubShapesSelection();
265 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView,
266 const TopoDS_Shape& theShape,
267 double& theX, double& theY) const
269 if (!theShape.IsNull()) {
270 if (theShape.ShapeType() == TopAbs_VERTEX) {
271 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
272 if (!aVertex.IsNull()) {
273 // A case when point is taken from existing vertex
274 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
275 PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
283 void PartSet_WidgetPoint2D::setConstraintWith(const ObjectPtr& theObject)
285 // Create point-edge coincedence
286 FeaturePtr aFeature = mySketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
287 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
289 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
290 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
291 AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
292 std::shared_ptr<GeomDataAPI_Point2D> aThisPoint =
293 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
294 aRef1->setAttr(aThisPoint);
296 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
297 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
298 aRef2->setObject(theObject);
303 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
305 // the contex menu release by the right button should not be processed by this widget
306 if (theEvent->button() != Qt::LeftButton)
309 ModuleBase_ISelection* aSelection = myWorkshop->selection();
310 Handle(V3d_View) aView = theWnd->v3dView();
311 // TODO: This fragment doesn't work because bug in OCC Viewer. It can be used after fixing.
312 NCollection_List<TopoDS_Shape> aShapes;
313 std::list<ObjectPtr> aObjects;
314 aSelection->selectedShapes(aShapes, aObjects);
315 // if we have selection
316 if (aShapes.Extent() > 0) {
317 TopoDS_Shape aShape = aShapes.First();
318 ObjectPtr aObject = aObjects.front();
319 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aObject);
320 bool anExternal = false;
321 if (aSelectedFeature.get() != NULL) {
322 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
323 std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
324 if ((!aSPFeature) && (!aShape.IsNull())) {
326 ResultPtr aFixedObject = PartSet_Tools::findFixedObjectByExternal(aShape, aObject, mySketch);
327 if (!aFixedObject.get())
328 aObject = PartSet_Tools::createFixedObjectByExternal(aShape, aObject, mySketch);
331 if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
332 // do not create a constraint to the point, which already used by the feature
333 // if the feature contains the point, focus is not switched
337 if (getPoint2d(aView, aShape, aX, aY))
339 bool anOrphanPoint = isOrphanPoint(aSelectedFeature, mySketch);
340 setConstraintWith(aObject);
342 emit vertexSelected();
343 emit focusOutWidget(this);
349 bool isProcessed = false;
350 if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
351 // when the point is selected, the coordinates of the point should be set into the attribute
352 // if the feature contains the point, focus is not switched
356 bool anOrphanPoint = isOrphanPoint(aSelectedFeature, mySketch);
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)) {
362 PartSet_Tools::setConstraints(mySketch, feature(), attributeID(), aX, aY);
364 else if (aShape.ShapeType() == TopAbs_EDGE) {
365 if (MyFeaturesForCoincedence.contains(myFeature->getKind().c_str()))
366 setConstraintWith(aObject);
368 // it is important to perform updateObject() in order to the current value is
369 // processed by Sketch Solver. Test case: line is created from a previous point
370 // to some distance, but in the area of the highlighting of the point. Constraint
371 // coincidence is created, after the solver is performed, the distance between the
372 // points of the line becomes less than the tolerance. Validator of the line returns
373 // false, the line will be aborted, but sketch stays valid.
374 updateObject(feature());
376 emit vertexSelected();
377 emit focusOutWidget(this);
381 // End of Bug dependent fragment
383 // A case when point is taken from mouse event
384 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
386 PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
388 // if the feature contains the point, focus is not switched
389 if (!setPoint(aX, anY) || isFeatureContainsPoint(myFeature, aX, anY))
392 /// Start alternative code
393 //std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
394 // GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
395 //QList<FeaturePtr> aIgnore;
396 //aIgnore.append(feature());
398 //double aTolerance = aView->Convert(7);
399 //std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt =
400 // PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
401 //if (aAttrPnt.get() != NULL) {
402 // aFeaturePoint->setValue(aAttrPnt->pnt());
403 // PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
404 // emit vertexSelected();
406 /// End alternative code
407 emit focusOutWidget(this);
412 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
417 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
420 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
421 // we need to block the value state change
422 bool isBlocked = blockValueState(true);
424 blockValueState(isBlocked);
425 setValueState(ModifiedInViewer);
428 double PartSet_WidgetPoint2D::x() const
430 return myXSpin->value();
433 double PartSet_WidgetPoint2D::y() const
435 return myYSpin->value();
439 bool PartSet_WidgetPoint2D::isFeatureContainsPoint(const FeaturePtr& theFeature,
440 double theX, double theY)
442 bool aPointIsFound = false;
443 AttributePtr aWidgetAttribute = myFeature->attribute(attributeID());
445 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d =
446 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
447 std::list<AttributePtr> anAttributes =
448 myFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
449 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
450 for(; anIter != anAttributes.end() && !aPointIsFound; anIter++) {
451 AttributePoint2DPtr aPoint2DAttribute =
452 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
453 if (aPoint2DAttribute == aWidgetAttribute)
455 if (aPoint2DAttribute.get() && aPoint2DAttribute->isInitialized()) {
456 aPointIsFound = aPoint2DAttribute->pnt()->isEqual(aPnt2d);
459 return aPointIsFound;
462 /*void PartSet_WidgetPoint2D::onValuesChanged()
464 emit valuesChanged();
467 bool PartSet_WidgetPoint2D::processEnter()
469 bool isModified = myXSpin->isModified() || myYSpin->isModified();
471 bool isXModified = myXSpin->isModified();
472 emit valuesChanged();
474 myXSpin->clearModified();
475 myYSpin->clearModified();
477 myXSpin->selectAll();
479 myYSpin->selectAll();
484 bool PartSet_WidgetPoint2D::isOrphanPoint(const FeaturePtr& theFeature,
485 const CompositeFeaturePtr& theSketch)
487 bool anOrphanPoint = false;
488 if (theFeature.get()) {
489 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
490 std::string aFeatureKind = theFeature->getKind();
491 if (aFeatureKind == SketchPlugin_Point::ID())
492 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
493 theFeature->attribute(SketchPlugin_Point::COORD_ID()));
494 else if (aFeatureKind == SketchPlugin_Circle::ID())
495 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
496 theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
498 else if (aFeatureKind == SketchPlugin_Arc::ID())
499 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
500 theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
502 if (aPointAttr.get()) {
503 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
504 FeaturePtr aCoincidence = PartSet_Tools::findFirstCoincidence(theFeature, aPoint);
505 anOrphanPoint = true;
506 // if there is at least one concident line to the point, the point is not an orphant
507 if (aCoincidence.get()) {
508 QList<FeaturePtr> aCoinsideLines;
509 PartSet_Tools::findCoincidences(aCoincidence, aCoinsideLines,
510 SketchPlugin_ConstraintCoincidence::ENTITY_A());
511 PartSet_Tools::findCoincidences(aCoincidence, aCoinsideLines,
512 SketchPlugin_ConstraintCoincidence::ENTITY_B());
513 QList<FeaturePtr>::const_iterator anIt = aCoinsideLines.begin(),
514 aLast = aCoinsideLines.end();
515 for (; anIt != aLast && anOrphanPoint; anIt++) {
516 anOrphanPoint = (*anIt)->getKind() != SketchPlugin_Line::ID();
521 return anOrphanPoint;