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 PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent,
48 const Config_WidgetAPI* theData,
49 const std::string& theParentId)
50 : ModuleBase_ModelWidget(theParent, theData, theParentId)
52 //myOptionParam = theData->getProperty(PREVIOUS_FEATURE_PARAM);
53 QString aPageName = QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME));
54 myGroupBox = new QGroupBox(aPageName, theParent);
55 myGroupBox->setFlat(false);
57 QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
58 ModuleBase_Tools::adjustMargins(aGroupLay);
59 aGroupLay->setColumnStretch(1, 1);
61 QLabel* aLabel = new QLabel(myGroupBox);
62 aLabel->setText(tr("X"));
63 aLabel->setPixmap(QPixmap(":pictures/x_point.png"));
64 aGroupLay->addWidget(aLabel, 0, 0);
66 myXSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
67 myXSpin->setMinimum(-DBL_MAX);
68 myXSpin->setMaximum(DBL_MAX);
69 myXSpin->setToolTip(tr("X"));
70 aGroupLay->addWidget(myXSpin, 0, 1);
72 connect(myXSpin, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
75 QLabel* aLabel = new QLabel(myGroupBox);
76 aLabel->setText(tr("Y"));
77 aLabel->setPixmap(QPixmap(":pictures/y_point.png"));
78 aGroupLay->addWidget(aLabel, 1, 0);
80 myYSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
81 myYSpin->setMinimum(-DBL_MAX);
82 myYSpin->setMaximum(DBL_MAX);
83 myYSpin->setToolTip(tr("Y"));
84 aGroupLay->addWidget(myYSpin, 1, 1);
86 connect(myYSpin, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
90 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
94 bool PartSet_WidgetPoint2D::setSelection(ModuleBase_ViewerPrs theValue)
96 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
98 TopoDS_Shape aShape = theValue.shape();
100 if (getPoint2d(aView, aShape, aX, aY)) {
107 void PartSet_WidgetPoint2D::setPoint(double theX, double theY)
110 bool isBlocked = this->blockSignals(true);
111 myXSpin->blockSignals(true);
112 myXSpin->setValue(theX);
113 myXSpin->blockSignals(false);
115 myYSpin->blockSignals(true);
116 myYSpin->setValue(theY);
117 myYSpin->blockSignals(false);
118 this->blockSignals(isBlocked);
120 emit valuesChanged();
123 bool PartSet_WidgetPoint2D::storeValue() const
125 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
126 if (!aData) // can be on abort of sketcher element
128 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
129 aData->attribute(attributeID()));
131 PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
132 bool isBlocked = that->blockSignals(true);
133 bool isImmutable = aPoint->setImmutable(true);
135 std::string _attr_name = myAttributeID;
136 double _X = myXSpin->value();
137 double _Y = myYSpin->value();
139 aPoint->setValue(myXSpin->value(), myYSpin->value());
140 // after movement the solver will call the update event: optimization
141 moveObject(myFeature);
142 aPoint->setImmutable(isImmutable);
143 that->blockSignals(isBlocked);
148 bool PartSet_WidgetPoint2D::restoreValue()
150 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
151 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
152 aData->attribute(attributeID()));
155 std::string _attr_name = myAttributeID;
156 double _X = aPoint->x();
157 double _Y = aPoint->y();
159 bool isBlocked = this->blockSignals(true);
160 myXSpin->blockSignals(true);
161 myXSpin->setValue(aPoint->x());
162 myXSpin->blockSignals(false);
164 myYSpin->blockSignals(true);
165 myYSpin->setValue(aPoint->y());
166 myYSpin->blockSignals(false);
167 this->blockSignals(isBlocked);
171 QWidget* PartSet_WidgetPoint2D::getControl() const
176 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
178 QList<QWidget*> aControls;
179 aControls.append(myXSpin);
180 aControls.append(myYSpin);
185 void PartSet_WidgetPoint2D::activate()
187 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
188 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
189 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
190 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
191 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
194 aModes << TopAbs_VERTEX;
195 myWorkshop->moduleConnector()->activateSubShapesSelection(aModes);
196 if (!isEditingMode()) {
197 // the control value is stored to the mode by the focus in on the widget
198 // we need the value is initialized in order to enable the apply button in the property panel
199 // it should happens only in the creation mode because during edition all fields are filled
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);
262 std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
263 GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
264 QList<FeaturePtr> aIgnore;
265 aIgnore.append(feature());
267 double aTolerance = aView->Convert(7);
268 std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt =
269 PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
270 if (aAttrPnt.get() != NULL) {
271 aFeaturePoint->setValue(aAttrPnt->pnt());
272 PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
273 emit vertexSelected();
275 emit focusOutWidget(this);
279 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
283 myWorkshop->operationMgr()->setLockValidating(true);
284 // the Ok button should be disabled in the property panel by moving the mouse point in the viewer
285 // this leads that the user does not try to click Ok and it avoids an incorrect situation that the
286 // line is moved to the cursor to the Ok button
287 myWorkshop->operationMgr()->setApplyEnabled(false);
289 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
292 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
296 double PartSet_WidgetPoint2D::x() const
298 return myXSpin->value();
301 double PartSet_WidgetPoint2D::y() const
303 return myYSpin->value();
306 void PartSet_WidgetPoint2D::onValuesChanged()
308 myWorkshop->operationMgr()->setLockValidating(false);
309 emit valuesChanged();