1 // File: PartSet_WidgetPoint2D.cpp
2 // Created: 25 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include "PartSet_WidgetPoint2d.h"
6 #include <PartSet_Tools.h>
8 #include <XGUI_Workshop.h>
9 #include <XGUI_ViewerProxy.h>
10 #include <XGUI_ModuleConnector.h>
11 #include <XGUI_SelectionMgr.h>
12 #include <XGUI_Selection.h>
14 #include <ModuleBase_DoubleSpinBox.h>
15 #include <ModuleBase_Tools.h>
16 #include <ModuleBase_IViewWindow.h>
18 #include <Config_Keywords.h>
19 #include <Config_WidgetAPI.h>
21 #include <Events_Loop.h>
22 #include <ModelAPI_Events.h>
24 #include <ModelAPI_Feature.h>
25 #include <ModelAPI_Data.h>
26 #include <ModelAPI_Object.h>
27 #include <GeomDataAPI_Point2D.h>
28 #include <GeomAPI_Pnt2d.h>
31 #include <QGridLayout>
34 #include <QMouseEvent>
35 #include <QApplication>
38 #include <TopoDS_Vertex.hxx>
39 #include <BRep_Tool.hxx>
44 PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent,
45 const Config_WidgetAPI* theData,
46 const std::string& theParentId)
47 : ModuleBase_ModelWidget(theParent, theData, theParentId)
49 //myOptionParam = theData->getProperty(PREVIOUS_FEATURE_PARAM);
50 QString aPageName = QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME));
51 myGroupBox = new QGroupBox(aPageName, theParent);
52 myGroupBox->setFlat(false);
54 QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
55 ModuleBase_Tools::adjustMargins(aGroupLay);
56 aGroupLay->setColumnStretch(1, 1);
58 QLabel* aLabel = new QLabel(myGroupBox);
60 aLabel->setPixmap(QPixmap(":pictures/x_point.png"));
61 aGroupLay->addWidget(aLabel, 0, 0);
63 myXSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
64 myXSpin->setMinimum(-DBL_MAX);
65 myXSpin->setMaximum(DBL_MAX);
66 myXSpin->setToolTip("X");
67 aGroupLay->addWidget(myXSpin, 0, 1);
69 connect(myXSpin, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
72 QLabel* aLabel = new QLabel(myGroupBox);
74 aLabel->setPixmap(QPixmap(":pictures/y_point.png"));
75 aGroupLay->addWidget(aLabel, 1, 0);
77 myYSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
78 myYSpin->setMinimum(-DBL_MAX);
79 myYSpin->setMaximum(DBL_MAX);
80 myYSpin->setToolTip("X");
81 aGroupLay->addWidget(myYSpin, 1, 1);
83 connect(myYSpin, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
87 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
91 bool PartSet_WidgetPoint2D::setSelection(ModuleBase_ViewerPrs theValue)
93 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
95 TopoDS_Shape aShape = theValue.shape();
97 if (getPoint2d(aView, aShape, aX, aY)) {
104 void PartSet_WidgetPoint2D::setPoint(double theX, double theY)
107 bool isBlocked = this->blockSignals(true);
108 myXSpin->setValue(theX);
109 myYSpin->setValue(theY);
110 this->blockSignals(isBlocked);
112 emit valuesChanged();
115 bool PartSet_WidgetPoint2D::storeValue() const
117 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
118 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
119 aData->attribute(attributeID()));
121 PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
122 bool isBlocked = that->blockSignals(true);
123 bool isImmutable = aPoint->setImmutable(true);
125 std::string _attr_name = myAttributeID;
126 double _X = myXSpin->value();
127 double _Y = myYSpin->value();
129 aPoint->setValue(myXSpin->value(), myYSpin->value());
130 updateObject(myFeature);
131 aPoint->setImmutable(isImmutable);
132 that->blockSignals(isBlocked);
137 bool PartSet_WidgetPoint2D::restoreValue()
139 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
140 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
141 aData->attribute(attributeID()));
144 std::string _attr_name = myAttributeID;
145 double _X = aPoint->x();
146 double _Y = aPoint->y();
148 bool isBlocked = this->blockSignals(true);
149 myXSpin->setValue(aPoint->x());
150 myYSpin->setValue(aPoint->y());
151 this->blockSignals(isBlocked);
155 QWidget* PartSet_WidgetPoint2D::getControl() const
160 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
162 QList<QWidget*> aControls;
163 aControls.append(myXSpin);
164 aControls.append(myYSpin);
168 //bool PartSet_WidgetPoint2D::initFromPrevious(ObjectPtr theObject)
170 // if (myOptionParam.length() == 0)
172 // std::shared_ptr<ModelAPI_Data> aData = theObject->data();
173 // std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
174 // aData->attribute(myOptionParam));
176 // bool isBlocked = this->blockSignals(true);
177 // myXSpin->setValue(aPoint->x());
178 // myYSpin->setValue(aPoint->y());
179 // this->blockSignals(isBlocked);
181 // emit valuesChanged();
182 // emit storedPoint2D(theObject, myOptionParam);
188 void PartSet_WidgetPoint2D::activate()
190 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
191 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
192 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
193 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
194 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
197 aModes << TopAbs_VERTEX;
198 myWorkshop->moduleConnector()->activateSubShapesSelection(aModes);
201 void PartSet_WidgetPoint2D::deactivate()
203 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
204 disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
205 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
206 disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
207 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
208 myWorkshop->moduleConnector()->deactivateSubShapesSelection();
211 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView,
212 const TopoDS_Shape& theShape,
213 double& theX, double& theY) const
215 if (!theShape.IsNull()) {
216 if (theShape.ShapeType() == TopAbs_VERTEX) {
217 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
218 if (!aVertex.IsNull()) {
219 // A case when point is taken from existing vertex
220 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
221 PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
230 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
232 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
233 NCollection_List<TopoDS_Shape> aShapes;
234 std::list<ObjectPtr> aObjects;
235 aSelection->selectedShapes(aShapes, aObjects);
236 if (aShapes.Extent() > 0) {
237 TopoDS_Shape aShape = aShapes.First();
239 if (getPoint2d(theWnd->v3dView(), aShape, aX, aY)) {
242 PartSet_Tools::setConstraints(mySketch, feature(), attributeID(),aX, aY);
243 emit vertexSelected(aObjects.front(), aShape);
244 emit focusOutWidget(this);
248 // A case when point is taken from mouse event
249 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
251 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
254 emit focusOutWidget(this);
258 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
260 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
263 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
267 double PartSet_WidgetPoint2D::x() const
269 return myXSpin->value();
272 double PartSet_WidgetPoint2D::y() const
274 return myYSpin->value();