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_PropertyPanel.h>
16 #include <XGUI_OperationMgr.h>
18 #include <ModuleBase_DoubleSpinBox.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>
35 #include <QGridLayout>
38 #include <QMouseEvent>
39 #include <QApplication>
42 #include <TopoDS_Vertex.hxx>
43 #include <BRep_Tool.hxx>
48 PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent,
49 const Config_WidgetAPI* theData,
50 const std::string& theParentId)
51 : ModuleBase_ModelWidget(theParent, theData, theParentId)
53 //myOptionParam = theData->getProperty(PREVIOUS_FEATURE_PARAM);
54 QString aPageName = QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME));
55 myGroupBox = new QGroupBox(aPageName, theParent);
56 myGroupBox->setFlat(false);
58 QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
59 ModuleBase_Tools::adjustMargins(aGroupLay);
60 aGroupLay->setColumnStretch(1, 1);
62 QLabel* aLabel = new QLabel(myGroupBox);
64 aLabel->setPixmap(QPixmap(":pictures/x_point.png"));
65 aGroupLay->addWidget(aLabel, 0, 0);
67 myXSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
68 myXSpin->setMinimum(-DBL_MAX);
69 myXSpin->setMaximum(DBL_MAX);
70 myXSpin->setToolTip("X");
71 aGroupLay->addWidget(myXSpin, 0, 1);
73 connect(myXSpin, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
76 QLabel* aLabel = new QLabel(myGroupBox);
78 aLabel->setPixmap(QPixmap(":pictures/y_point.png"));
79 aGroupLay->addWidget(aLabel, 1, 0);
81 myYSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
82 myYSpin->setMinimum(-DBL_MAX);
83 myYSpin->setMaximum(DBL_MAX);
84 myYSpin->setToolTip("X");
85 aGroupLay->addWidget(myYSpin, 1, 1);
87 connect(myYSpin, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
91 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
95 bool PartSet_WidgetPoint2D::setSelection(ModuleBase_ViewerPrs theValue)
97 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
99 TopoDS_Shape aShape = theValue.shape();
101 if (getPoint2d(aView, aShape, aX, aY)) {
108 void PartSet_WidgetPoint2D::setPoint(double theX, double theY)
111 bool isBlocked = this->blockSignals(true);
112 myXSpin->blockSignals(true);
113 myXSpin->setValue(theX);
114 myXSpin->blockSignals(false);
116 myYSpin->blockSignals(true);
117 myYSpin->setValue(theY);
118 myYSpin->blockSignals(false);
119 this->blockSignals(isBlocked);
121 emit valuesChanged();
124 bool PartSet_WidgetPoint2D::storeValue() const
126 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
127 if (!aData) // can be on abort of sketcher element
129 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
130 aData->attribute(attributeID()));
132 PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
133 bool isBlocked = that->blockSignals(true);
134 bool isImmutable = aPoint->setImmutable(true);
136 std::string _attr_name = myAttributeID;
137 double _X = myXSpin->value();
138 double _Y = myYSpin->value();
140 aPoint->setValue(myXSpin->value(), myYSpin->value());
141 updateObject(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);
198 void PartSet_WidgetPoint2D::deactivate()
200 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
201 disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
202 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
203 disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
204 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
205 myWorkshop->moduleConnector()->deactivateSubShapesSelection();
206 myWorkshop->operationMgr()->setLockValidating(false);
209 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView,
210 const TopoDS_Shape& theShape,
211 double& theX, double& theY) const
213 if (!theShape.IsNull()) {
214 if (theShape.ShapeType() == TopAbs_VERTEX) {
215 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
216 if (!aVertex.IsNull()) {
217 // A case when point is taken from existing vertex
218 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
219 PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
228 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
230 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
231 NCollection_List<TopoDS_Shape> aShapes;
232 std::list<ObjectPtr> aObjects;
233 aSelection->selectedShapes(aShapes, aObjects);
234 if (aShapes.Extent() > 0) {
235 TopoDS_Shape aShape = aShapes.First();
237 if (getPoint2d(theWnd->v3dView(), aShape, aX, aY)) {
240 PartSet_Tools::setConstraints(mySketch, feature(), attributeID(),aX, aY);
241 emit vertexSelected(aObjects.front(), aShape);
242 emit focusOutWidget(this);
246 // A case when point is taken from mouse event
247 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
249 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
252 emit focusOutWidget(this);
256 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
258 myWorkshop->operationMgr()->setLockValidating(true);
259 myWorkshop->propertyPanel()->setOkEnabled(false);
261 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
264 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
268 double PartSet_WidgetPoint2D::x() const
270 return myXSpin->value();
273 double PartSet_WidgetPoint2D::y() const
275 return myYSpin->value();
278 void PartSet_WidgetPoint2D::onValuesChanged()
280 myWorkshop->operationMgr()->setLockValidating(false);
281 emit valuesChanged();