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>
36 #include <QGridLayout>
39 #include <QMouseEvent>
40 #include <QApplication>
43 #include <TopoDS_Vertex.hxx>
44 #include <BRep_Tool.hxx>
49 const double MaxCoordinate = 1e12;
52 PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent,
53 const Config_WidgetAPI* theData,
54 const std::string& theParentId)
55 : ModuleBase_ModelWidget(theParent, theData, theParentId)
57 //myOptionParam = theData->getProperty(PREVIOUS_FEATURE_PARAM);
58 QString aPageName = QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME));
59 myGroupBox = new QGroupBox(aPageName, theParent);
60 myGroupBox->setFlat(false);
62 QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
63 ModuleBase_Tools::adjustMargins(aGroupLay);
64 aGroupLay->setColumnStretch(1, 1);
66 QLabel* aLabel = new QLabel(myGroupBox);
67 aLabel->setText(tr("X"));
68 aLabel->setPixmap(QPixmap(":pictures/x_point.png"));
69 aGroupLay->addWidget(aLabel, 0, 0);
71 myXSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
72 myXSpin->setMinimum(-DBL_MAX);
73 myXSpin->setMaximum(DBL_MAX);
74 myXSpin->setToolTip(tr("X"));
75 aGroupLay->addWidget(myXSpin, 0, 1);
77 connect(myXSpin, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
80 QLabel* aLabel = new QLabel(myGroupBox);
81 aLabel->setText(tr("Y"));
82 aLabel->setPixmap(QPixmap(":pictures/y_point.png"));
83 aGroupLay->addWidget(aLabel, 1, 0);
85 myYSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
86 myYSpin->setMinimum(-DBL_MAX);
87 myYSpin->setMaximum(DBL_MAX);
88 myYSpin->setToolTip(tr("Y"));
89 aGroupLay->addWidget(myYSpin, 1, 1);
91 connect(myYSpin, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
95 void PartSet_WidgetPoint2D::reset()
97 if (isComputedDefault()) {
99 if (myFeature->compute(myAttributeID))
104 double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
105 // it is important to block the spin box control in order to do not through out the
106 // locking of the validating state.
107 ModuleBase_Tools::setSpinValue(myXSpin, isOk ? aDefValue : 0.0);
108 ModuleBase_Tools::setSpinValue(myYSpin, isOk ? aDefValue : 0.0);
112 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
116 bool PartSet_WidgetPoint2D::setSelection(ModuleBase_ViewerPrs theValue)
118 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
120 TopoDS_Shape aShape = theValue.shape();
122 if (getPoint2d(aView, aShape, aX, aY)) {
123 isDone = setPoint(aX, aY);
128 bool PartSet_WidgetPoint2D::setPoint(double theX, double theY)
130 if (fabs(theX) >= MaxCoordinate)
132 if (fabs(theY) >= MaxCoordinate)
135 ModuleBase_Tools::setSpinValue(myXSpin, theX);
136 ModuleBase_Tools::setSpinValue(myYSpin, theY);
142 bool PartSet_WidgetPoint2D::storeValueCustom() const
144 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
145 if (!aData) // can be on abort of sketcher element
147 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
148 aData->attribute(attributeID()));
150 PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
151 bool isBlocked = that->blockSignals(true);
152 bool isImmutable = aPoint->setImmutable(true);
154 std::string _attr_name = myAttributeID;
155 double _X = myXSpin->value();
156 double _Y = myYSpin->value();
158 aPoint->setValue(myXSpin->value(), myYSpin->value());
159 // after movement the solver will call the update event: optimization
160 moveObject(myFeature);
161 aPoint->setImmutable(isImmutable);
162 that->blockSignals(isBlocked);
167 bool PartSet_WidgetPoint2D::restoreValue()
169 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
170 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
171 aData->attribute(attributeID()));
174 std::string _attr_name = myAttributeID;
175 double _X = aPoint->x();
176 double _Y = aPoint->y();
179 ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
180 ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
184 QWidget* PartSet_WidgetPoint2D::getControl() const
189 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
191 QList<QWidget*> aControls;
192 aControls.append(myXSpin);
193 aControls.append(myYSpin);
198 void PartSet_WidgetPoint2D::activateCustom()
200 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
201 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
202 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
203 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
204 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
207 aModes << TopAbs_VERTEX;
209 aModes << TopAbs_EDGE;
210 myWorkshop->moduleConnector()->activateSubShapesSelection(aModes);
213 void PartSet_WidgetPoint2D::deactivate()
215 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
216 disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
217 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
218 disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
219 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
220 myWorkshop->moduleConnector()->deactivateSubShapesSelection();
221 myWorkshop->operationMgr()->setLockValidating(false);
224 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView,
225 const TopoDS_Shape& theShape,
226 double& theX, double& theY) const
228 if (!theShape.IsNull()) {
229 if (theShape.ShapeType() == TopAbs_VERTEX) {
230 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
231 if (!aVertex.IsNull()) {
232 // A case when point is taken from existing vertex
233 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
234 PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
243 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
245 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
246 Handle(V3d_View) aView = theWnd->v3dView();
247 // TODO: This fragment doesn't work because bug in OCC Viewer. It can be used after fixing.
248 NCollection_List<TopoDS_Shape> aShapes;
249 std::list<ObjectPtr> aObjects;
250 aSelection->selectedShapes(aShapes, aObjects);
251 // if we have selection
252 if (aShapes.Extent() > 0) {
253 TopoDS_Shape aShape = aShapes.First();
254 ObjectPtr aObject = aObjects.front();
255 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aObject);
256 if (aSelectedFeature.get() != NULL) {
257 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
258 std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
259 if ((!aSPFeature) && (!aShape.IsNull()))
260 PartSet_Tools::createFixedObjectByExternal(aShape, aObject, mySketch);
263 if (getPoint2d(aView, aShape, aX, aY)) {
266 PartSet_Tools::setConstraints(mySketch, feature(), attributeID(),aX, aY);
267 emit vertexSelected();
268 emit focusOutWidget(this);
272 // End of Bug dependent fragment
274 // A case when point is taken from mouse event
275 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
277 PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
278 if (!setPoint(aX, anY))
281 /// Start alternative code
282 //std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
283 // GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
284 //QList<FeaturePtr> aIgnore;
285 //aIgnore.append(feature());
287 //double aTolerance = aView->Convert(7);
288 //std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt =
289 // PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
290 //if (aAttrPnt.get() != NULL) {
291 // aFeaturePoint->setValue(aAttrPnt->pnt());
292 // PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
293 // emit vertexSelected();
295 /// End alternative code
296 emit focusOutWidget(this);
300 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
304 myWorkshop->operationMgr()->setLockValidating(true);
305 // the Ok button should be disabled in the property panel by moving the mouse point in the viewer
306 // this leads that the user does not try to click Ok and it avoids an incorrect situation that the
307 // line is moved to the cursor to the Ok button
308 myWorkshop->operationMgr()->setApplyEnabled(false);
310 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
313 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
317 double PartSet_WidgetPoint2D::x() const
319 return myXSpin->value();
322 double PartSet_WidgetPoint2D::y() const
324 return myYSpin->value();
327 void PartSet_WidgetPoint2D::onValuesChanged()
329 myWorkshop->operationMgr()->setLockValidating(false);
330 emit valuesChanged();