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 <XGUI_Workshop.h>
12 #include <XGUI_ViewerProxy.h>
13 #include <XGUI_ModuleConnector.h>
14 #include <XGUI_SelectionMgr.h>
15 #include <XGUI_Selection.h>
16 #include <XGUI_OperationMgr.h>
18 #include <ModuleBase_ParamSpinBox.h>
19 #include <ModuleBase_Tools.h>
20 #include <ModuleBase_IViewWindow.h>
22 #include <Config_Keywords.h>
23 #include <Config_WidgetAPI.h>
25 #include <Events_Loop.h>
26 #include <ModelAPI_Events.h>
28 #include <ModelAPI_Feature.h>
29 #include <ModelAPI_Data.h>
30 #include <ModelAPI_Object.h>
31 #include <GeomDataAPI_Point2D.h>
32 #include <GeomAPI_Pnt2d.h>
34 #include <SketchPlugin_Feature.h>
35 #include <SketchPlugin_ConstraintCoincidence.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;
54 PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent,
55 const Config_WidgetAPI* theData,
56 const std::string& theParentId)
57 : ModuleBase_ModelWidget(theParent, theData, theParentId)
59 // the control should accept the focus, so the boolen flag is corrected to be true
60 myIsObligatory = true;
61 //myOptionParam = theData->getProperty(PREVIOUS_FEATURE_PARAM);
62 QString aPageName = QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME));
63 myGroupBox = new QGroupBox(aPageName, theParent);
64 myGroupBox->setFlat(false);
66 QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
67 ModuleBase_Tools::adjustMargins(aGroupLay);
68 aGroupLay->setColumnStretch(1, 1);
70 QLabel* aLabel = new QLabel(myGroupBox);
71 aLabel->setText(tr("X"));
72 aLabel->setPixmap(QPixmap(":pictures/x_point.png"));
73 aGroupLay->addWidget(aLabel, 0, 0);
75 myXSpin = new ModuleBase_ParamSpinBox(myGroupBox);
76 myXSpin->setMinimum(-DBL_MAX);
77 myXSpin->setMaximum(DBL_MAX);
78 myXSpin->setToolTip(tr("X"));
79 aGroupLay->addWidget(myXSpin, 0, 1);
81 connect(myXSpin, SIGNAL(valueChanged(const QString&)), this, SLOT(onValuesChanged()));
84 QLabel* aLabel = new QLabel(myGroupBox);
85 aLabel->setText(tr("Y"));
86 aLabel->setPixmap(QPixmap(":pictures/y_point.png"));
87 aGroupLay->addWidget(aLabel, 1, 0);
89 myYSpin = new ModuleBase_ParamSpinBox(myGroupBox);
90 myYSpin->setMinimum(-DBL_MAX);
91 myYSpin->setMaximum(DBL_MAX);
92 myYSpin->setToolTip(tr("Y"));
93 aGroupLay->addWidget(myYSpin, 1, 1);
95 connect(myYSpin, SIGNAL(valueChanged(const QString&)), this, SLOT(onValuesChanged()));
97 QVBoxLayout* aLayout = new QVBoxLayout(this);
98 ModuleBase_Tools::zeroMargins(aLayout);
99 aLayout->addWidget(myGroupBox);
103 void PartSet_WidgetPoint2D::reset()
108 if (isComputedDefault() || myXSpin->hasVariable() || myYSpin->hasVariable()) {
110 //if (myFeature->compute(myAttributeID))
115 double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
116 // it is important to block the spin box control in order to do not through out the
117 // locking of the validating state.
118 ModuleBase_Tools::setSpinValue(myXSpin, isOk ? aDefValue : 0.0);
119 ModuleBase_Tools::setSpinValue(myYSpin, isOk ? aDefValue : 0.0);
124 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
128 bool PartSet_WidgetPoint2D::setSelection(const QList<ModuleBase_ViewerPrs>& theValues, int& thePosition)
130 if (thePosition < 0 || thePosition >= theValues.size())
132 ModuleBase_ViewerPrs aValue = theValues[thePosition];
135 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
137 TopoDS_Shape aShape = aValue.shape();
139 if (getPoint2d(aView, aShape, aX, aY)) {
140 isDone = setPoint(aX, aY);
145 bool PartSet_WidgetPoint2D::setPoint(double theX, double theY)
147 if (fabs(theX) >= MaxCoordinate)
149 if (fabs(theY) >= MaxCoordinate)
152 ModuleBase_Tools::setSpinValue(myXSpin, theX);
153 ModuleBase_Tools::setSpinValue(myYSpin, theY);
159 bool PartSet_WidgetPoint2D::storeValueCustom() const
161 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
162 if (!aData) // can be on abort of sketcher element
164 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
165 aData->attribute(attributeID()));
167 PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
168 bool isBlocked = that->blockSignals(true);
169 bool isImmutable = aPoint->setImmutable(true);
171 if (myXSpin->hasVariable() || myYSpin->hasVariable()) {
172 aPoint->setText(myXSpin->text().toStdString(), myYSpin->text().toStdString());
174 aPoint->setValue(myXSpin->value(), myYSpin->value());
175 aPoint->setText("", "");
177 // after movement the solver will call the update event: optimization
178 moveObject(myFeature);
179 aPoint->setImmutable(isImmutable);
180 that->blockSignals(isBlocked);
185 bool PartSet_WidgetPoint2D::restoreValue()
187 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
188 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
189 aData->attribute(attributeID()));
190 std::string aTextX = aPoint->textX();
191 std::string aTextY = aPoint->textY();
192 if (aTextX.empty() || aTextY.empty()) {
193 double aX = aPoint->x();
194 ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
195 ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
197 ModuleBase_Tools::setSpinText(myXSpin, QString::fromStdString(aTextX));
198 ModuleBase_Tools::setSpinText(myYSpin, QString::fromStdString(aTextY));
203 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
205 QList<QWidget*> aControls;
206 aControls.append(myXSpin);
207 aControls.append(myYSpin);
212 void PartSet_WidgetPoint2D::activateCustom()
214 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
215 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
216 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
217 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
218 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
219 connect(aViewer, SIGNAL(enterViewPort()), this, SLOT(onLockValidating()));
220 connect(aViewer, SIGNAL(leaveViewPort()), this, SLOT(onUnlockValidating()));
223 aModes << TopAbs_VERTEX;
224 aModes << TopAbs_EDGE;
225 myWorkshop->moduleConnector()->activateSubShapesSelection(aModes);
227 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
228 if (aModule->isMouseOverWindow())
232 void PartSet_WidgetPoint2D::deactivate()
234 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
235 disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
236 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
237 disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
238 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
239 disconnect(aViewer, SIGNAL(enterViewPort()), this, SLOT(onLockValidating()));
240 disconnect(aViewer, SIGNAL(leaveViewPort()), this, SLOT(onUnlockValidating()));
242 myWorkshop->moduleConnector()->deactivateSubShapesSelection();
243 onUnlockValidating();
246 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView,
247 const TopoDS_Shape& theShape,
248 double& theX, double& theY) const
250 if (!theShape.IsNull()) {
251 if (theShape.ShapeType() == TopAbs_VERTEX) {
252 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
253 if (!aVertex.IsNull()) {
254 // A case when point is taken from existing vertex
255 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
256 PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
265 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
267 // the contex menu release by the right button should not be processed by this widget
268 if (theEvent->button() != Qt::LeftButton)
271 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
272 Handle(V3d_View) aView = theWnd->v3dView();
273 // TODO: This fragment doesn't work because bug in OCC Viewer. It can be used after fixing.
274 NCollection_List<TopoDS_Shape> aShapes;
275 std::list<ObjectPtr> aObjects;
276 aSelection->selectedShapes(aShapes, aObjects);
277 // if we have selection
278 if (aShapes.Extent() > 0) {
279 TopoDS_Shape aShape = aShapes.First();
280 ObjectPtr aObject = aObjects.front();
281 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aObject);
282 if (aSelectedFeature.get() != NULL) {
283 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
284 std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
285 if ((!aSPFeature) && (!aShape.IsNull())) {
286 ResultPtr aFixedObject = PartSet_Tools::findFixedObjectByExternal(aShape, aObject, mySketch);
287 if (!aFixedObject.get())
288 aFixedObject = PartSet_Tools::createFixedObjectByExternal(aShape, aObject, mySketch);
292 if (getPoint2d(aView, aShape, aX, aY)) {
295 PartSet_Tools::setConstraints(mySketch, feature(), attributeID(),aX, aY);
296 emit vertexSelected();
297 emit focusOutWidget(this);
299 } else if (aShape.ShapeType() == TopAbs_EDGE) {
300 // Create point-edge coincedence
301 FeaturePtr aFeature = mySketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
302 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
304 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
305 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
306 AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
307 std::shared_ptr<GeomDataAPI_Point2D> aThisPoint =
308 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
309 aRef1->setAttr(aThisPoint);
311 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
312 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
313 aRef2->setObject(aObject);
315 emit vertexSelected();
316 emit focusOutWidget(this);
320 // End of Bug dependent fragment
322 // A case when point is taken from mouse event
323 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
325 PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
326 if (!setPoint(aX, anY))
329 /// Start alternative code
330 //std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
331 // GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
332 //QList<FeaturePtr> aIgnore;
333 //aIgnore.append(feature());
335 //double aTolerance = aView->Convert(7);
336 //std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt =
337 // PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
338 //if (aAttrPnt.get() != NULL) {
339 // aFeaturePoint->setValue(aAttrPnt->pnt());
340 // PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
341 // emit vertexSelected();
343 /// End alternative code
344 emit focusOutWidget(this);
348 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
353 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
356 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
360 void PartSet_WidgetPoint2D::onLockValidating()
362 XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
363 anOperationMgr->setLockValidating(true);
364 // the Ok button should be disabled in the property panel by moving the mouse point in the viewer
365 // this leads that the user does not try to click Ok and it avoids an incorrect situation that the
366 // line is moved to the cursor to the Ok button
367 anOperationMgr->setApplyEnabled(false);
370 void PartSet_WidgetPoint2D::onUnlockValidating()
372 // it is important to restore the validity state in the property panel after leaving the
373 // view port. Unlock the validating.
374 XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
375 if (anOperationMgr->isValidationLocked()) {
376 anOperationMgr->setLockValidating(false);
377 anOperationMgr->onValidateOperation();
381 double PartSet_WidgetPoint2D::x() const
383 return myXSpin->value();
386 double PartSet_WidgetPoint2D::y() const
388 return myYSpin->value();
391 void PartSet_WidgetPoint2D::onValuesChanged()
393 myWorkshop->operationMgr()->setLockValidating(false);
394 emit valuesChanged();