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 //#define APPLY_BY_ENTER_OR_TAB
57 PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent,
58 ModuleBase_IWorkshop* theWorkshop,
59 const Config_WidgetAPI* theData,
60 const std::string& theParentId)
61 : ModuleBase_ModelWidget(theParent, theData, theParentId), myWorkshop(theWorkshop)
63 if (MyFeaturesForCoincedence.isEmpty()) {
64 MyFeaturesForCoincedence << SketchPlugin_Line::ID().c_str()
65 << SketchPlugin_Arc::ID().c_str()
66 << SketchPlugin_Point::ID().c_str()
67 << SketchPlugin_Circle::ID().c_str();
69 myLockApplyMgr = new PartSet_LockApplyMgr(theParent, myWorkshop);
71 // the control should accept the focus, so the boolen flag is corrected to be true
72 myIsObligatory = true;
73 //myOptionParam = theData->getProperty(PREVIOUS_FEATURE_PARAM);
74 QString aPageName = QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME));
75 myGroupBox = new QGroupBox(aPageName, theParent);
76 myGroupBox->setFlat(false);
78 QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
79 ModuleBase_Tools::adjustMargins(aGroupLay);
80 aGroupLay->setSpacing(2);
81 aGroupLay->setColumnStretch(1, 1);
83 QLabel* aLabel = new QLabel(myGroupBox);
84 aLabel->setText(tr("X "));
85 aGroupLay->addWidget(aLabel, 0, 0);
87 myXSpin = new ModuleBase_ParamSpinBox(myGroupBox);
88 myXSpin->setMinimum(-DBL_MAX);
89 myXSpin->setMaximum(DBL_MAX);
90 myXSpin->setToolTip(tr("X"));
91 aGroupLay->addWidget(myXSpin, 0, 1);
93 #ifdef APPLY_BY_ENTER_OR_TAB
94 // Apply widget value change by enter/tab event.
95 connect(myXSpin, SIGNAL(editingFinished()), this, SLOT(onValuesChanged()));
97 connect(myXSpin, SIGNAL(valueChanged(const QString&)), this, SLOT(onValuesChanged()));
101 QLabel* aLabel = new QLabel(myGroupBox);
102 aLabel->setText(tr("Y "));
103 aGroupLay->addWidget(aLabel, 1, 0);
105 myYSpin = new ModuleBase_ParamSpinBox(myGroupBox);
106 myYSpin->setMinimum(-DBL_MAX);
107 myYSpin->setMaximum(DBL_MAX);
108 myYSpin->setToolTip(tr("Y"));
109 aGroupLay->addWidget(myYSpin, 1, 1);
111 #ifdef APPLY_BY_ENTER_OR_TAB
112 // Apply widget value change by enter/tab event.
113 connect(myYSpin, SIGNAL(editingFinished()), this, SLOT(onValuesChanged()));
115 connect(myYSpin, SIGNAL(valueChanged(const QString&)), this, SLOT(onValuesChanged()));
118 QVBoxLayout* aLayout = new QVBoxLayout(this);
119 ModuleBase_Tools::zeroMargins(aLayout);
120 aLayout->addWidget(myGroupBox);
124 bool PartSet_WidgetPoint2D::reset()
127 if (!isUseReset() || isComputedDefault() || myXSpin->hasVariable() || myYSpin->hasVariable()) {
132 double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
133 // it is important to block the spin box control in order to do not through out the
134 // locking of the validating state.
135 ModuleBase_Tools::setSpinValue(myXSpin, isOk ? aDefValue : 0.0);
136 ModuleBase_Tools::setSpinValue(myYSpin, isOk ? aDefValue : 0.0);
143 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
147 bool PartSet_WidgetPoint2D::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
148 const bool theToValidate)
150 if (theValues.empty())
153 ModuleBase_ViewerPrs aValue = theValues.takeFirst();
155 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
157 TopoDS_Shape aShape = aValue.shape();
159 if (getPoint2d(aView, aShape, aX, aY)) {
160 isDone = setPoint(aX, aY);
165 bool PartSet_WidgetPoint2D::setPoint(double theX, double theY)
167 if (fabs(theX) >= MaxCoordinate)
169 if (fabs(theY) >= MaxCoordinate)
172 ModuleBase_Tools::setSpinValue(myXSpin, theX);
173 ModuleBase_Tools::setSpinValue(myYSpin, theY);
179 bool PartSet_WidgetPoint2D::storeValueCustom() const
181 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
182 if (!aData) // can be on abort of sketcher element
184 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
185 aData->attribute(attributeID()));
187 PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
188 bool isBlocked = that->blockSignals(true);
189 bool isImmutable = aPoint->setImmutable(true);
191 // if text is not empty then setValue will be ignored
192 // so we should set the text at first
193 aPoint->setText(myXSpin->hasVariable() ? myXSpin->text().toStdString() : "",
194 myYSpin->hasVariable() ? myYSpin->text().toStdString() : "");
195 aPoint->setValue(!myXSpin->hasVariable() ? myXSpin->value() : aPoint->x(),
196 !myYSpin->hasVariable() ? myYSpin->value() : aPoint->y());
198 // after movement the solver will call the update event: optimization
199 moveObject(myFeature);
200 aPoint->setImmutable(isImmutable);
201 that->blockSignals(isBlocked);
206 bool PartSet_WidgetPoint2D::restoreValueCustom()
208 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
209 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
210 aData->attribute(attributeID()));
211 QString aTextX = QString::fromStdString(aPoint->textX());
212 QString aTextY = QString::fromStdString(aPoint->textY());
214 bool isDouble = false;
216 if (aTextX.isEmpty()) {
217 ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
219 aVal = aTextX.toDouble(&isDouble);
221 ModuleBase_Tools::setSpinValue(myXSpin, aVal);
223 ModuleBase_Tools::setSpinText(myXSpin, aTextX);
225 if (aTextY.isEmpty()) {
226 ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
228 aVal = aTextY.toDouble(&isDouble);
230 ModuleBase_Tools::setSpinValue(myYSpin, aVal);
232 ModuleBase_Tools::setSpinText(myYSpin, aTextY);
234 //if (aTextX.empty() || aTextY.empty()) {
235 // ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
236 // ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
238 // ModuleBase_Tools::setSpinText(myXSpin, QString::fromStdString(aTextX));
239 // ModuleBase_Tools::setSpinText(myYSpin, QString::fromStdString(aTextY));
244 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
246 QList<QWidget*> aControls;
247 aControls.append(myXSpin);
248 aControls.append(myYSpin);
253 void PartSet_WidgetPoint2D::activateCustom()
255 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
256 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
257 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
258 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
259 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
262 aModes << TopAbs_VERTEX;
263 aModes << TopAbs_EDGE;
264 myWorkshop->activateSubShapesSelection(aModes);
266 myLockApplyMgr->activate();
269 void PartSet_WidgetPoint2D::deactivate()
271 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
272 disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
273 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
274 disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
275 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
277 myWorkshop->deactivateSubShapesSelection();
279 myLockApplyMgr->deactivate();
282 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView,
283 const TopoDS_Shape& theShape,
284 double& theX, double& theY) const
286 if (!theShape.IsNull()) {
287 if (theShape.ShapeType() == TopAbs_VERTEX) {
288 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
289 if (!aVertex.IsNull()) {
290 // A case when point is taken from existing vertex
291 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
292 PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
300 void PartSet_WidgetPoint2D::setConstraintWith(const ObjectPtr& theObject)
302 // Create point-edge coincedence
303 FeaturePtr aFeature = mySketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
304 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
306 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
307 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
308 AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
309 std::shared_ptr<GeomDataAPI_Point2D> aThisPoint =
310 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
311 aRef1->setAttr(aThisPoint);
313 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
314 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
315 aRef2->setObject(theObject);
320 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
322 // the contex menu release by the right button should not be processed by this widget
323 if (theEvent->button() != Qt::LeftButton)
326 ModuleBase_ISelection* aSelection = myWorkshop->selection();
327 Handle(V3d_View) aView = theWnd->v3dView();
328 // TODO: This fragment doesn't work because bug in OCC Viewer. It can be used after fixing.
329 NCollection_List<TopoDS_Shape> aShapes;
330 std::list<ObjectPtr> aObjects;
331 aSelection->selectedShapes(aShapes, aObjects);
332 // if we have selection
333 if (aShapes.Extent() > 0) {
334 TopoDS_Shape aShape = aShapes.First();
335 ObjectPtr aObject = aObjects.front();
336 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aObject);
337 bool anExternal = false;
338 if (aSelectedFeature.get() != NULL) {
339 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
340 std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
341 if ((!aSPFeature) && (!aShape.IsNull())) {
343 ResultPtr aFixedObject = PartSet_Tools::findFixedObjectByExternal(aShape, aObject, mySketch);
344 if (!aFixedObject.get())
345 aObject = PartSet_Tools::createFixedObjectByExternal(aShape, aObject, mySketch);
348 if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
349 // do not create a constraint to the point, which already used by the feature
350 // if the feature contains the point, focus is not switched
354 if (getPoint2d(aView, aShape, aX, aY))
356 setConstraintWith(aObject);
357 emit vertexSelected();
358 emit focusOutWidget(this);
364 bool isProcessed = false;
365 if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
366 // when the point is selected, the coordinates of the point should be set into the attribute
367 // if the feature contains the point, focus is not switched
371 // do not set a coincidence constraint in the attribute if the feature contains a point
372 // with the same coordinates. It is important for line creation in order to do not set
373 // the same constraints for the same points, oterwise the result line has zero length.
374 if (getPoint2d(aView, aShape, aX, aY)) {
376 PartSet_Tools::setConstraints(mySketch, feature(), attributeID(), aX, aY);
378 else if (aShape.ShapeType() == TopAbs_EDGE) {
379 if (MyFeaturesForCoincedence.contains(myFeature->getKind().c_str()))
380 setConstraintWith(aObject);
382 // it is important to perform updateObject() in order to the current value is
383 // processed by Sketch Solver. Test case: line is created from a previous point
384 // to some distance, but in the area of the highlighting of the point. Constraint
385 // coincidence is created, after the solver is performed, the distance between the
386 // points of the line becomes less than the tolerance. Validator of the line returns
387 // false, the line will be aborted, but sketch stays valid.
388 updateObject(feature());
389 emit vertexSelected();
390 emit focusOutWidget(this);
394 // End of Bug dependent fragment
396 // A case when point is taken from mouse event
397 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
399 PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
401 // if the feature contains the point, focus is not switched
402 if (!setPoint(aX, anY) || isFeatureContainsPoint(myFeature, aX, anY))
405 /// Start alternative code
406 //std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
407 // GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
408 //QList<FeaturePtr> aIgnore;
409 //aIgnore.append(feature());
411 //double aTolerance = aView->Convert(7);
412 //std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt =
413 // PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
414 //if (aAttrPnt.get() != NULL) {
415 // aFeaturePoint->setValue(aAttrPnt->pnt());
416 // PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
417 // emit vertexSelected();
419 /// End alternative code
420 emit focusOutWidget(this);
425 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
430 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
433 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
437 double PartSet_WidgetPoint2D::x() const
439 return myXSpin->value();
442 double PartSet_WidgetPoint2D::y() const
444 return myYSpin->value();
448 bool PartSet_WidgetPoint2D::isFeatureContainsPoint(const FeaturePtr& theFeature,
449 double theX, double theY)
451 bool aPointIsFound = false;
452 AttributePtr aWidgetAttribute = myFeature->attribute(attributeID());
454 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d =
455 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
456 std::list<AttributePtr> anAttributes =
457 myFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
458 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
459 for(; anIter != anAttributes.end() && !aPointIsFound; anIter++) {
460 AttributePoint2DPtr aPoint2DAttribute =
461 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
462 if (aPoint2DAttribute == aWidgetAttribute)
464 if (aPoint2DAttribute.get() && aPoint2DAttribute->isInitialized()) {
465 aPointIsFound = aPoint2DAttribute->pnt()->isEqual(aPnt2d);
468 return aPointIsFound;
471 void PartSet_WidgetPoint2D::onValuesChanged()
473 myLockApplyMgr->valuesChanged();
474 emit valuesChanged();