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 // Apply widget value change by enter/tab event.
90 connect(myXSpin, SIGNAL(valueStored()), this, SLOT(onValuesChanged()));
91 connect(myXSpin, SIGNAL(valueChanged(const QString&)), this, SIGNAL(valuesModified()));
92 connect(myXSpin, SIGNAL(focusNextPrev()), this, SIGNAL(focusNextPrev()));
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->setMinimum(-DBL_MAX);
101 myYSpin->setMaximum(DBL_MAX);
102 myYSpin->setToolTip(tr("Y"));
103 aGroupLay->addWidget(myYSpin, 1, 1);
105 // Apply widget value change by enter/tab event.
106 connect(myYSpin, SIGNAL(valueStored()), this, SLOT(onValuesChanged()));
107 connect(myYSpin, SIGNAL(valueChanged(const QString&)), this, SIGNAL(valuesModified()));
108 connect(myYSpin, SIGNAL(focusNextPrev()), this, SIGNAL(focusNextPrev()));
110 QVBoxLayout* aLayout = new QVBoxLayout(this);
111 ModuleBase_Tools::zeroMargins(aLayout);
112 aLayout->addWidget(myGroupBox);
116 bool PartSet_WidgetPoint2D::resetCustom()
119 if (!isUseReset() || isComputedDefault() || myXSpin->hasVariable() || myYSpin->hasVariable()) {
124 double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
125 // it is important to block the spin box control in order to do not through out the
126 // locking of the validating state.
127 ModuleBase_Tools::setSpinValue(myXSpin, isOk ? aDefValue : 0.0);
128 ModuleBase_Tools::setSpinValue(myYSpin, isOk ? aDefValue : 0.0);
135 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
139 bool PartSet_WidgetPoint2D::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
140 const bool theToValidate)
142 if (theValues.empty())
145 ModuleBase_ViewerPrs aValue = theValues.takeFirst();
147 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
149 TopoDS_Shape aShape = aValue.shape();
151 if (getPoint2d(aView, aShape, aX, aY)) {
152 isDone = setPoint(aX, aY);
157 bool PartSet_WidgetPoint2D::setPoint(double theX, double theY)
159 if (fabs(theX) >= MaxCoordinate)
161 if (fabs(theY) >= MaxCoordinate)
164 ModuleBase_Tools::setSpinValue(myXSpin, theX);
165 ModuleBase_Tools::setSpinValue(myYSpin, theY);
171 bool PartSet_WidgetPoint2D::storeValueCustom() const
173 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
174 if (!aData) // can be on abort of sketcher element
176 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
177 aData->attribute(attributeID()));
179 PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
180 bool isBlocked = that->blockSignals(true);
181 bool isImmutable = aPoint->setImmutable(true);
183 // if text is not empty then setValue will be ignored
184 // so we should set the text at first
185 aPoint->setText(myXSpin->hasVariable() ? myXSpin->text().toStdString() : "",
186 myYSpin->hasVariable() ? myYSpin->text().toStdString() : "");
187 aPoint->setValue(!myXSpin->hasVariable() ? myXSpin->value() : aPoint->x(),
188 !myYSpin->hasVariable() ? myYSpin->value() : aPoint->y());
190 // after movement the solver will call the update event: optimization
191 moveObject(myFeature);
192 aPoint->setImmutable(isImmutable);
193 that->blockSignals(isBlocked);
198 bool PartSet_WidgetPoint2D::restoreValueCustom()
200 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
201 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
202 aData->attribute(attributeID()));
203 QString aTextX = QString::fromStdString(aPoint->textX());
204 QString aTextY = QString::fromStdString(aPoint->textY());
206 bool isDouble = false;
208 if (aTextX.isEmpty()) {
209 ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
211 aVal = aTextX.toDouble(&isDouble);
213 ModuleBase_Tools::setSpinValue(myXSpin, aVal);
215 ModuleBase_Tools::setSpinText(myXSpin, aTextX);
217 if (aTextY.isEmpty()) {
218 ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
220 aVal = aTextY.toDouble(&isDouble);
222 ModuleBase_Tools::setSpinValue(myYSpin, aVal);
224 ModuleBase_Tools::setSpinText(myYSpin, aTextY);
226 //if (aTextX.empty() || aTextY.empty()) {
227 // ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
228 // ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
230 // ModuleBase_Tools::setSpinText(myXSpin, QString::fromStdString(aTextX));
231 // ModuleBase_Tools::setSpinText(myYSpin, QString::fromStdString(aTextY));
236 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
238 QList<QWidget*> aControls;
239 aControls.append(myXSpin);
240 aControls.append(myYSpin);
245 void PartSet_WidgetPoint2D::activateCustom()
247 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
248 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
249 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
250 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
251 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
254 aModes << TopAbs_VERTEX;
255 aModes << TopAbs_EDGE;
256 myWorkshop->activateSubShapesSelection(aModes);
259 void PartSet_WidgetPoint2D::deactivate()
261 ModuleBase_ModelWidget::deactivate();
262 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
263 disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
264 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
265 disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
266 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
268 myWorkshop->deactivateSubShapesSelection();
271 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView,
272 const TopoDS_Shape& theShape,
273 double& theX, double& theY) const
275 if (!theShape.IsNull()) {
276 if (theShape.ShapeType() == TopAbs_VERTEX) {
277 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
278 if (!aVertex.IsNull()) {
279 // A case when point is taken from existing vertex
280 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
281 PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
289 void PartSet_WidgetPoint2D::setConstraintWith(const ObjectPtr& theObject)
291 // Create point-edge coincedence
292 FeaturePtr aFeature = mySketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
293 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
295 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
296 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
297 AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
298 std::shared_ptr<GeomDataAPI_Point2D> aThisPoint =
299 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
300 aRef1->setAttr(aThisPoint);
302 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
303 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
304 aRef2->setObject(theObject);
309 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
311 // the contex menu release by the right button should not be processed by this widget
312 if (theEvent->button() != Qt::LeftButton)
315 ModuleBase_ISelection* aSelection = myWorkshop->selection();
316 Handle(V3d_View) aView = theWnd->v3dView();
317 // TODO: This fragment doesn't work because bug in OCC Viewer. It can be used after fixing.
318 NCollection_List<TopoDS_Shape> aShapes;
319 std::list<ObjectPtr> aObjects;
320 aSelection->selectedShapes(aShapes, aObjects);
321 // if we have selection
322 if (aShapes.Extent() > 0) {
323 TopoDS_Shape aShape = aShapes.First();
324 ObjectPtr aObject = aObjects.front();
325 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aObject);
326 bool anExternal = false;
327 if (aSelectedFeature.get() != NULL) {
328 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
329 std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
330 if ((!aSPFeature) && (!aShape.IsNull())) {
332 ResultPtr aFixedObject = PartSet_Tools::findFixedObjectByExternal(aShape, aObject, mySketch);
333 if (!aFixedObject.get())
334 aObject = PartSet_Tools::createFixedObjectByExternal(aShape, aObject, mySketch);
337 if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
338 // do not create a constraint to the point, which already used by the feature
339 // if the feature contains the point, focus is not switched
343 if (getPoint2d(aView, aShape, aX, aY))
345 setConstraintWith(aObject);
346 emit vertexSelected();
347 emit focusOutWidget(this);
353 bool isProcessed = false;
354 if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
355 // when the point is selected, the coordinates of the point should be set into the attribute
356 // if the feature contains the point, focus is not switched
360 // do not set a coincidence constraint in the attribute if the feature contains a point
361 // with the same coordinates. It is important for line creation in order to do not set
362 // the same constraints for the same points, oterwise the result line has zero length.
363 if (getPoint2d(aView, aShape, aX, aY)) {
365 PartSet_Tools::setConstraints(mySketch, feature(), attributeID(), aX, aY);
367 else if (aShape.ShapeType() == TopAbs_EDGE) {
368 if (MyFeaturesForCoincedence.contains(myFeature->getKind().c_str()))
369 setConstraintWith(aObject);
371 // it is important to perform updateObject() in order to the current value is
372 // processed by Sketch Solver. Test case: line is created from a previous point
373 // to some distance, but in the area of the highlighting of the point. Constraint
374 // coincidence is created, after the solver is performed, the distance between the
375 // points of the line becomes less than the tolerance. Validator of the line returns
376 // false, the line will be aborted, but sketch stays valid.
377 updateObject(feature());
378 emit vertexSelected();
379 emit focusOutWidget(this);
383 // End of Bug dependent fragment
385 // A case when point is taken from mouse event
386 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
388 PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
390 // if the feature contains the point, focus is not switched
391 if (!setPoint(aX, anY) || isFeatureContainsPoint(myFeature, aX, anY))
394 /// Start alternative code
395 //std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
396 // GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
397 //QList<FeaturePtr> aIgnore;
398 //aIgnore.append(feature());
400 //double aTolerance = aView->Convert(7);
401 //std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt =
402 // PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
403 //if (aAttrPnt.get() != NULL) {
404 // aFeaturePoint->setValue(aAttrPnt->pnt());
405 // PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
406 // emit vertexSelected();
408 /// End alternative code
409 emit focusOutWidget(this);
414 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
419 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
422 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
423 // we need to block the value state change
424 bool isBlocked = blockValueState(true);
426 blockValueState(isBlocked);
427 setValueState(ModifiedInViewer);
430 double PartSet_WidgetPoint2D::x() const
432 return myXSpin->value();
435 double PartSet_WidgetPoint2D::y() const
437 return myYSpin->value();
441 bool PartSet_WidgetPoint2D::isFeatureContainsPoint(const FeaturePtr& theFeature,
442 double theX, double theY)
444 bool aPointIsFound = false;
445 AttributePtr aWidgetAttribute = myFeature->attribute(attributeID());
447 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d =
448 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
449 std::list<AttributePtr> anAttributes =
450 myFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
451 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
452 for(; anIter != anAttributes.end() && !aPointIsFound; anIter++) {
453 AttributePoint2DPtr aPoint2DAttribute =
454 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
455 if (aPoint2DAttribute == aWidgetAttribute)
457 if (aPoint2DAttribute.get() && aPoint2DAttribute->isInitialized()) {
458 aPointIsFound = aPoint2DAttribute->pnt()->isEqual(aPnt2d);
461 return aPointIsFound;
464 void PartSet_WidgetPoint2D::onValuesChanged()
466 emit valuesChanged();
469 bool PartSet_WidgetPoint2D::processEnter()
471 bool isModified = myXSpin->isModified() || myYSpin->isModified();
473 bool isXModified = myXSpin->isModified();
475 myXSpin->clearModified();
476 myYSpin->clearModified();
478 myXSpin->selectAll();
480 myYSpin->selectAll();