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>
34 #include <QGridLayout>
37 #include <QMouseEvent>
38 #include <QApplication>
41 #include <TopoDS_Vertex.hxx>
42 #include <BRep_Tool.hxx>
47 const double MaxCoordinate = 1e12;
50 PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent,
51 const Config_WidgetAPI* theData,
52 const std::string& theParentId)
53 : ModuleBase_ModelWidget(theParent, theData, theParentId)
55 //myOptionParam = theData->getProperty(PREVIOUS_FEATURE_PARAM);
56 QString aPageName = QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME));
57 myGroupBox = new QGroupBox(aPageName, theParent);
58 myGroupBox->setFlat(false);
60 QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
61 ModuleBase_Tools::adjustMargins(aGroupLay);
62 aGroupLay->setColumnStretch(1, 1);
64 QLabel* aLabel = new QLabel(myGroupBox);
65 aLabel->setText(tr("X"));
66 aLabel->setPixmap(QPixmap(":pictures/x_point.png"));
67 aGroupLay->addWidget(aLabel, 0, 0);
69 myXSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
70 myXSpin->setMinimum(-DBL_MAX);
71 myXSpin->setMaximum(DBL_MAX);
72 myXSpin->setToolTip(tr("X"));
73 aGroupLay->addWidget(myXSpin, 0, 1);
75 connect(myXSpin, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
78 QLabel* aLabel = new QLabel(myGroupBox);
79 aLabel->setText(tr("Y"));
80 aLabel->setPixmap(QPixmap(":pictures/y_point.png"));
81 aGroupLay->addWidget(aLabel, 1, 0);
83 myYSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
84 myYSpin->setMinimum(-DBL_MAX);
85 myYSpin->setMaximum(DBL_MAX);
86 myYSpin->setToolTip(tr("Y"));
87 aGroupLay->addWidget(myYSpin, 1, 1);
89 connect(myYSpin, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
93 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
97 bool PartSet_WidgetPoint2D::setSelection(ModuleBase_ViewerPrs theValue)
99 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
101 TopoDS_Shape aShape = theValue.shape();
103 if (getPoint2d(aView, aShape, aX, aY)) {
104 isDone = setPoint(aX, aY);
109 bool PartSet_WidgetPoint2D::setPoint(double theX, double theY)
111 if (fabs(theX) >= MaxCoordinate)
113 if (fabs(theY) >= MaxCoordinate)
115 bool isBlocked = this->blockSignals(true);
116 myXSpin->blockSignals(true);
117 myXSpin->setValue(theX);
118 myXSpin->blockSignals(false);
120 myYSpin->blockSignals(true);
121 myYSpin->setValue(theY);
122 myYSpin->blockSignals(false);
123 this->blockSignals(isBlocked);
125 emit valuesChanged();
129 bool PartSet_WidgetPoint2D::storeValue() const
131 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
132 if (!aData) // can be on abort of sketcher element
134 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
135 aData->attribute(attributeID()));
137 PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
138 bool isBlocked = that->blockSignals(true);
139 bool isImmutable = aPoint->setImmutable(true);
141 std::string _attr_name = myAttributeID;
142 double _X = myXSpin->value();
143 double _Y = myYSpin->value();
145 aPoint->setValue(myXSpin->value(), myYSpin->value());
146 // after movement the solver will call the update event: optimization
147 moveObject(myFeature);
148 aPoint->setImmutable(isImmutable);
149 that->blockSignals(isBlocked);
154 bool PartSet_WidgetPoint2D::restoreValue()
156 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
157 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
158 aData->attribute(attributeID()));
161 std::string _attr_name = myAttributeID;
162 double _X = aPoint->x();
163 double _Y = aPoint->y();
165 bool isBlocked = this->blockSignals(true);
166 myXSpin->blockSignals(true);
167 myXSpin->setValue(aPoint->x());
168 myXSpin->blockSignals(false);
170 myYSpin->blockSignals(true);
171 myYSpin->setValue(aPoint->y());
172 myYSpin->blockSignals(false);
173 this->blockSignals(isBlocked);
177 QWidget* PartSet_WidgetPoint2D::getControl() const
182 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
184 QList<QWidget*> aControls;
185 aControls.append(myXSpin);
186 aControls.append(myYSpin);
191 void PartSet_WidgetPoint2D::activateCustom()
193 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
194 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
195 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
196 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
197 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
200 aModes << TopAbs_VERTEX;
201 myWorkshop->moduleConnector()->activateSubShapesSelection(aModes);
204 void PartSet_WidgetPoint2D::deactivate()
206 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
207 disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
208 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
209 disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
210 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
211 myWorkshop->moduleConnector()->deactivateSubShapesSelection();
212 myWorkshop->operationMgr()->setLockValidating(false);
215 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView,
216 const TopoDS_Shape& theShape,
217 double& theX, double& theY) const
219 if (!theShape.IsNull()) {
220 if (theShape.ShapeType() == TopAbs_VERTEX) {
221 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
222 if (!aVertex.IsNull()) {
223 // A case when point is taken from existing vertex
224 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
225 PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
234 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
236 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
237 // TODO: This fragment doesn't work because bug in OCC Viewer. It can be used after fixing.
238 //NCollection_List<TopoDS_Shape> aShapes;
239 //std::list<ObjectPtr> aObjects;
240 //aSelection->selectedShapes(aShapes, aObjects);
241 //if (aShapes.Extent() > 0) {
242 // TopoDS_Shape aShape = aShapes.First();
244 // if (getPoint2d(theWnd->v3dView(), aShape, aX, aY)) {
247 // PartSet_Tools::setConstraints(mySketch, feature(), attributeID(),aX, aY);
248 // emit vertexSelected(aObjects.front(), aShape);
249 // emit focusOutWidget(this);
253 // End of Bug dependent fragment
255 // A case when point is taken from mouse event
256 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
258 Handle(V3d_View) aView = theWnd->v3dView();
259 PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
260 if (!setPoint(aX, anY))
263 std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
264 GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
265 QList<FeaturePtr> aIgnore;
266 aIgnore.append(feature());
268 double aTolerance = aView->Convert(7);
269 std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt =
270 PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
271 if (aAttrPnt.get() != NULL) {
272 aFeaturePoint->setValue(aAttrPnt->pnt());
273 PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
274 emit vertexSelected();
276 emit focusOutWidget(this);
280 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
284 myWorkshop->operationMgr()->setLockValidating(true);
285 // the Ok button should be disabled in the property panel by moving the mouse point in the viewer
286 // this leads that the user does not try to click Ok and it avoids an incorrect situation that the
287 // line is moved to the cursor to the Ok button
288 myWorkshop->operationMgr()->setApplyEnabled(false);
290 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
293 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
297 double PartSet_WidgetPoint2D::x() const
299 return myXSpin->value();
302 double PartSet_WidgetPoint2D::y() const
304 return myYSpin->value();
307 void PartSet_WidgetPoint2D::onValuesChanged()
309 myWorkshop->operationMgr()->setLockValidating(false);
310 emit valuesChanged();