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 // the control should accept the focus, so the boolen flag is corrected to be true
58 myIsObligatory = true;
59 //myOptionParam = theData->getProperty(PREVIOUS_FEATURE_PARAM);
60 QString aPageName = QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME));
61 myGroupBox = new QGroupBox(aPageName, theParent);
62 myGroupBox->setFlat(false);
64 QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
65 ModuleBase_Tools::adjustMargins(aGroupLay);
66 aGroupLay->setColumnStretch(1, 1);
68 QLabel* aLabel = new QLabel(myGroupBox);
69 aLabel->setText(tr("X"));
70 aLabel->setPixmap(QPixmap(":pictures/x_point.png"));
71 aGroupLay->addWidget(aLabel, 0, 0);
73 myXSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
74 myXSpin->setMinimum(-DBL_MAX);
75 myXSpin->setMaximum(DBL_MAX);
76 myXSpin->setToolTip(tr("X"));
77 aGroupLay->addWidget(myXSpin, 0, 1);
79 connect(myXSpin, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
82 QLabel* aLabel = new QLabel(myGroupBox);
83 aLabel->setText(tr("Y"));
84 aLabel->setPixmap(QPixmap(":pictures/y_point.png"));
85 aGroupLay->addWidget(aLabel, 1, 0);
87 myYSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
88 myYSpin->setMinimum(-DBL_MAX);
89 myYSpin->setMaximum(DBL_MAX);
90 myYSpin->setToolTip(tr("Y"));
91 aGroupLay->addWidget(myYSpin, 1, 1);
93 connect(myYSpin, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
95 QVBoxLayout* aLayout = new QVBoxLayout(this);
96 ModuleBase_Tools::zeroMargins(aLayout);
97 aLayout->addWidget(myGroupBox);
101 void PartSet_WidgetPoint2D::reset()
103 if (isComputedDefault()) {
105 if (myFeature->compute(myAttributeID))
110 double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
111 // it is important to block the spin box control in order to do not through out the
112 // locking of the validating state.
113 ModuleBase_Tools::setSpinValue(myXSpin, isOk ? aDefValue : 0.0);
114 ModuleBase_Tools::setSpinValue(myYSpin, isOk ? aDefValue : 0.0);
119 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
123 bool PartSet_WidgetPoint2D::setSelection(ModuleBase_ViewerPrs theValue)
125 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
127 TopoDS_Shape aShape = theValue.shape();
129 if (getPoint2d(aView, aShape, aX, aY)) {
130 isDone = setPoint(aX, aY);
135 bool PartSet_WidgetPoint2D::setPoint(double theX, double theY)
137 if (fabs(theX) >= MaxCoordinate)
139 if (fabs(theY) >= MaxCoordinate)
142 ModuleBase_Tools::setSpinValue(myXSpin, theX);
143 ModuleBase_Tools::setSpinValue(myYSpin, theY);
149 bool PartSet_WidgetPoint2D::storeValueCustom() const
151 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
152 if (!aData) // can be on abort of sketcher element
154 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
155 aData->attribute(attributeID()));
157 PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
158 bool isBlocked = that->blockSignals(true);
159 bool isImmutable = aPoint->setImmutable(true);
161 std::string _attr_name = myAttributeID;
162 double _X = myXSpin->value();
163 double _Y = myYSpin->value();
165 aPoint->setValue(myXSpin->value(), myYSpin->value());
166 // after movement the solver will call the update event: optimization
167 moveObject(myFeature);
168 aPoint->setImmutable(isImmutable);
169 that->blockSignals(isBlocked);
174 bool PartSet_WidgetPoint2D::restoreValue()
176 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
177 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
178 aData->attribute(attributeID()));
181 std::string _attr_name = myAttributeID;
182 double _X = aPoint->x();
183 double _Y = aPoint->y();
186 ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
187 ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
191 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
193 QList<QWidget*> aControls;
194 aControls.append(myXSpin);
195 aControls.append(myYSpin);
200 void PartSet_WidgetPoint2D::activateCustom()
202 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
203 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
204 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
205 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
206 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
209 aModes << TopAbs_VERTEX;
211 aModes << TopAbs_EDGE;
212 myWorkshop->moduleConnector()->activateSubShapesSelection(aModes);
215 void PartSet_WidgetPoint2D::deactivate()
217 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
218 disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
219 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
220 disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
221 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
222 myWorkshop->moduleConnector()->deactivateSubShapesSelection();
223 myWorkshop->operationMgr()->setLockValidating(false);
226 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView,
227 const TopoDS_Shape& theShape,
228 double& theX, double& theY) const
230 if (!theShape.IsNull()) {
231 if (theShape.ShapeType() == TopAbs_VERTEX) {
232 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
233 if (!aVertex.IsNull()) {
234 // A case when point is taken from existing vertex
235 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
236 PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
245 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
247 // the contex menu release by the right button should not be processed by this widget
248 if (theEvent->button() != Qt::LeftButton)
251 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
252 Handle(V3d_View) aView = theWnd->v3dView();
253 // TODO: This fragment doesn't work because bug in OCC Viewer. It can be used after fixing.
254 NCollection_List<TopoDS_Shape> aShapes;
255 std::list<ObjectPtr> aObjects;
256 aSelection->selectedShapes(aShapes, aObjects);
257 // if we have selection
258 if (aShapes.Extent() > 0) {
259 TopoDS_Shape aShape = aShapes.First();
260 ObjectPtr aObject = aObjects.front();
261 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aObject);
262 if (aSelectedFeature.get() != NULL) {
263 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
264 std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
265 if ((!aSPFeature) && (!aShape.IsNull()))
266 PartSet_Tools::createFixedObjectByExternal(aShape, aObject, mySketch);
269 if (getPoint2d(aView, aShape, aX, aY)) {
272 PartSet_Tools::setConstraints(mySketch, feature(), attributeID(),aX, aY);
273 emit vertexSelected();
274 emit focusOutWidget(this);
278 // End of Bug dependent fragment
280 // A case when point is taken from mouse event
281 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
283 PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
284 if (!setPoint(aX, anY))
287 /// Start alternative code
288 //std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
289 // GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
290 //QList<FeaturePtr> aIgnore;
291 //aIgnore.append(feature());
293 //double aTolerance = aView->Convert(7);
294 //std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt =
295 // PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
296 //if (aAttrPnt.get() != NULL) {
297 // aFeaturePoint->setValue(aAttrPnt->pnt());
298 // PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
299 // emit vertexSelected();
301 /// End alternative code
302 emit focusOutWidget(this);
306 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
310 myWorkshop->operationMgr()->setLockValidating(true);
311 // the Ok button should be disabled in the property panel by moving the mouse point in the viewer
312 // this leads that the user does not try to click Ok and it avoids an incorrect situation that the
313 // line is moved to the cursor to the Ok button
314 myWorkshop->operationMgr()->setApplyEnabled(false);
316 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
319 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
323 double PartSet_WidgetPoint2D::x() const
325 return myXSpin->value();
328 double PartSet_WidgetPoint2D::y() const
330 return myYSpin->value();
333 void PartSet_WidgetPoint2D::onValuesChanged()
335 myWorkshop->operationMgr()->setLockValidating(false);
336 emit valuesChanged();