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>
10 #include <XGUI_Workshop.h>
11 #include <XGUI_ViewerProxy.h>
12 #include <XGUI_ModuleConnector.h>
13 #include <XGUI_SelectionMgr.h>
14 #include <XGUI_Selection.h>
15 #include <XGUI_OperationMgr.h>
17 #include <ModuleBase_DoubleSpinBox.h>
18 #include <ModuleBase_Tools.h>
19 #include <ModuleBase_IViewWindow.h>
21 #include <Config_Keywords.h>
22 #include <Config_WidgetAPI.h>
24 #include <Events_Loop.h>
25 #include <ModelAPI_Events.h>
27 #include <ModelAPI_Feature.h>
28 #include <ModelAPI_Data.h>
29 #include <ModelAPI_Object.h>
30 #include <GeomDataAPI_Point2D.h>
31 #include <GeomAPI_Pnt2d.h>
33 #include <SketchPlugin_Feature.h>
34 #include <SketchPlugin_ConstraintCoincidence.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;
53 PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent,
54 const Config_WidgetAPI* theData,
55 const std::string& theParentId)
56 : ModuleBase_ModelWidget(theParent, theData, theParentId)
58 // the control should accept the focus, so the boolen flag is corrected to be true
59 myIsObligatory = true;
60 //myOptionParam = theData->getProperty(PREVIOUS_FEATURE_PARAM);
61 QString aPageName = QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME));
62 myGroupBox = new QGroupBox(aPageName, theParent);
63 myGroupBox->setFlat(false);
65 QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
66 ModuleBase_Tools::adjustMargins(aGroupLay);
67 aGroupLay->setColumnStretch(1, 1);
69 QLabel* aLabel = new QLabel(myGroupBox);
70 aLabel->setText(tr("X"));
71 aLabel->setPixmap(QPixmap(":pictures/x_point.png"));
72 aGroupLay->addWidget(aLabel, 0, 0);
74 myXSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
75 myXSpin->setMinimum(-DBL_MAX);
76 myXSpin->setMaximum(DBL_MAX);
77 myXSpin->setToolTip(tr("X"));
78 aGroupLay->addWidget(myXSpin, 0, 1);
80 connect(myXSpin, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
83 QLabel* aLabel = new QLabel(myGroupBox);
84 aLabel->setText(tr("Y"));
85 aLabel->setPixmap(QPixmap(":pictures/y_point.png"));
86 aGroupLay->addWidget(aLabel, 1, 0);
88 myYSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
89 myYSpin->setMinimum(-DBL_MAX);
90 myYSpin->setMaximum(DBL_MAX);
91 myYSpin->setToolTip(tr("Y"));
92 aGroupLay->addWidget(myYSpin, 1, 1);
94 connect(myYSpin, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
96 QVBoxLayout* aLayout = new QVBoxLayout(this);
97 ModuleBase_Tools::zeroMargins(aLayout);
98 aLayout->addWidget(myGroupBox);
102 void PartSet_WidgetPoint2D::reset()
107 if (isComputedDefault()) {
109 if (myFeature->compute(myAttributeID))
114 double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
115 // it is important to block the spin box control in order to do not through out the
116 // locking of the validating state.
117 ModuleBase_Tools::setSpinValue(myXSpin, isOk ? aDefValue : 0.0);
118 ModuleBase_Tools::setSpinValue(myYSpin, isOk ? aDefValue : 0.0);
123 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
127 bool PartSet_WidgetPoint2D::setSelection(const QList<ModuleBase_ViewerPrs>& theValues, int& thePosition)
129 if (thePosition < 0 || thePosition >= theValues.size())
131 ModuleBase_ViewerPrs aValue = theValues[thePosition];
134 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
136 TopoDS_Shape aShape = aValue.shape();
138 if (getPoint2d(aView, aShape, aX, aY)) {
139 isDone = setPoint(aX, aY);
144 bool PartSet_WidgetPoint2D::setPoint(double theX, double theY)
146 if (fabs(theX) >= MaxCoordinate)
148 if (fabs(theY) >= MaxCoordinate)
151 ModuleBase_Tools::setSpinValue(myXSpin, theX);
152 ModuleBase_Tools::setSpinValue(myYSpin, theY);
158 bool PartSet_WidgetPoint2D::storeValueCustom() const
160 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
161 if (!aData) // can be on abort of sketcher element
163 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
164 aData->attribute(attributeID()));
166 PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
167 bool isBlocked = that->blockSignals(true);
168 bool isImmutable = aPoint->setImmutable(true);
170 std::string _attr_name = myAttributeID;
171 double _X = myXSpin->value();
172 double _Y = myYSpin->value();
174 aPoint->setValue(myXSpin->value(), myYSpin->value());
175 // after movement the solver will call the update event: optimization
176 moveObject(myFeature);
177 aPoint->setImmutable(isImmutable);
178 that->blockSignals(isBlocked);
183 bool PartSet_WidgetPoint2D::restoreValue()
185 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
186 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
187 aData->attribute(attributeID()));
190 std::string _attr_name = myAttributeID;
191 double _X = aPoint->x();
192 double _Y = aPoint->y();
195 ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
196 ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
200 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
202 QList<QWidget*> aControls;
203 aControls.append(myXSpin);
204 aControls.append(myYSpin);
209 void PartSet_WidgetPoint2D::activateCustom()
211 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
212 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
213 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
214 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
215 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
216 connect(aViewer, SIGNAL(leaveViewPort()), this, SLOT(onLeaveViewPort()));
219 aModes << TopAbs_VERTEX;
220 aModes << TopAbs_EDGE;
221 myWorkshop->moduleConnector()->activateSubShapesSelection(aModes);
224 void PartSet_WidgetPoint2D::deactivate()
226 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
227 disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
228 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
229 disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
230 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
231 disconnect(aViewer, SIGNAL(leaveViewPort()), this, SLOT(onLeaveViewPort()));
233 myWorkshop->moduleConnector()->deactivateSubShapesSelection();
234 myWorkshop->operationMgr()->setLockValidating(false);
237 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView,
238 const TopoDS_Shape& theShape,
239 double& theX, double& theY) const
241 if (!theShape.IsNull()) {
242 if (theShape.ShapeType() == TopAbs_VERTEX) {
243 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
244 if (!aVertex.IsNull()) {
245 // A case when point is taken from existing vertex
246 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
247 PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
256 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
258 // the contex menu release by the right button should not be processed by this widget
259 if (theEvent->button() != Qt::LeftButton)
262 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
263 Handle(V3d_View) aView = theWnd->v3dView();
264 // TODO: This fragment doesn't work because bug in OCC Viewer. It can be used after fixing.
265 NCollection_List<TopoDS_Shape> aShapes;
266 std::list<ObjectPtr> aObjects;
267 aSelection->selectedShapes(aShapes, aObjects);
268 // if we have selection
269 if (aShapes.Extent() > 0) {
270 TopoDS_Shape aShape = aShapes.First();
271 ObjectPtr aObject = aObjects.front();
272 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aObject);
273 if (aSelectedFeature.get() != NULL) {
274 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
275 std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
276 if ((!aSPFeature) && (!aShape.IsNull())) {
277 ResultPtr aFixedObject = PartSet_Tools::findFixedObjectByExternal(aShape, aObject, mySketch);
278 if (!aFixedObject.get())
279 aFixedObject = PartSet_Tools::createFixedObjectByExternal(aShape, aObject, mySketch);
283 if (getPoint2d(aView, aShape, aX, aY)) {
286 PartSet_Tools::setConstraints(mySketch, feature(), attributeID(),aX, aY);
287 emit vertexSelected();
288 emit focusOutWidget(this);
290 } else if (aShape.ShapeType() == TopAbs_EDGE) {
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(aObject);
306 emit vertexSelected();
307 emit focusOutWidget(this);
311 // End of Bug dependent fragment
313 // A case when point is taken from mouse event
314 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
316 PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
317 if (!setPoint(aX, anY))
320 /// Start alternative code
321 //std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
322 // GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
323 //QList<FeaturePtr> aIgnore;
324 //aIgnore.append(feature());
326 //double aTolerance = aView->Convert(7);
327 //std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt =
328 // PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
329 //if (aAttrPnt.get() != NULL) {
330 // aFeaturePoint->setValue(aAttrPnt->pnt());
331 // PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
332 // emit vertexSelected();
334 /// End alternative code
335 emit focusOutWidget(this);
339 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
343 myWorkshop->operationMgr()->setLockValidating(true);
344 // the Ok button should be disabled in the property panel by moving the mouse point in the viewer
345 // this leads that the user does not try to click Ok and it avoids an incorrect situation that the
346 // line is moved to the cursor to the Ok button
347 myWorkshop->operationMgr()->setApplyEnabled(false);
349 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
352 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
356 void PartSet_WidgetPoint2D::onLeaveViewPort()
358 // it is important to restore the validity state in the property panel after leaving the
359 // view port. Unlock the validating.
360 XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
361 if (anOperationMgr->isValidationLocked()) {
362 anOperationMgr->setLockValidating(false);
363 anOperationMgr->onValidateOperation();
367 double PartSet_WidgetPoint2D::x() const
369 return myXSpin->value();
372 double PartSet_WidgetPoint2D::y() const
374 return myYSpin->value();
377 void PartSet_WidgetPoint2D::onValuesChanged()
379 myWorkshop->operationMgr()->setLockValidating(false);
380 emit valuesChanged();