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>
16 #include <ModuleBase_DoubleSpinBox.h>
17 #include <ModuleBase_Tools.h>
18 #include <ModuleBase_IViewWindow.h>
20 #include <Config_Keywords.h>
21 #include <Config_WidgetAPI.h>
23 #include <Events_Loop.h>
24 #include <ModelAPI_Events.h>
26 #include <ModelAPI_Feature.h>
27 #include <ModelAPI_Data.h>
28 #include <ModelAPI_Object.h>
29 #include <GeomDataAPI_Point2D.h>
30 #include <GeomAPI_Pnt2d.h>
33 #include <QGridLayout>
36 #include <QMouseEvent>
37 #include <QApplication>
40 #include <TopoDS_Vertex.hxx>
41 #include <BRep_Tool.hxx>
46 PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent,
47 const Config_WidgetAPI* theData,
48 const std::string& theParentId)
49 : ModuleBase_ModelWidget(theParent, theData, theParentId)
51 //myOptionParam = theData->getProperty(PREVIOUS_FEATURE_PARAM);
52 QString aPageName = QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME));
53 myGroupBox = new QGroupBox(aPageName, theParent);
54 myGroupBox->setFlat(false);
56 QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
57 ModuleBase_Tools::adjustMargins(aGroupLay);
58 aGroupLay->setColumnStretch(1, 1);
60 QLabel* aLabel = new QLabel(myGroupBox);
62 aLabel->setPixmap(QPixmap(":pictures/x_point.png"));
63 aGroupLay->addWidget(aLabel, 0, 0);
65 myXSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
66 myXSpin->setMinimum(-DBL_MAX);
67 myXSpin->setMaximum(DBL_MAX);
68 myXSpin->setToolTip("X");
69 aGroupLay->addWidget(myXSpin, 0, 1);
71 connect(myXSpin, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
74 QLabel* aLabel = new QLabel(myGroupBox);
76 aLabel->setPixmap(QPixmap(":pictures/y_point.png"));
77 aGroupLay->addWidget(aLabel, 1, 0);
79 myYSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
80 myYSpin->setMinimum(-DBL_MAX);
81 myYSpin->setMaximum(DBL_MAX);
82 myYSpin->setToolTip("X");
83 aGroupLay->addWidget(myYSpin, 1, 1);
85 connect(myYSpin, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
89 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
93 bool PartSet_WidgetPoint2D::setSelection(ModuleBase_ViewerPrs theValue)
95 Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
97 TopoDS_Shape aShape = theValue.shape();
99 if (getPoint2d(aView, aShape, aX, aY)) {
106 void PartSet_WidgetPoint2D::setPoint(double theX, double theY)
109 bool isBlocked = this->blockSignals(true);
110 myXSpin->setValue(theX);
111 myYSpin->setValue(theY);
112 this->blockSignals(isBlocked);
114 emit valuesChanged();
117 bool PartSet_WidgetPoint2D::storeValue() const
119 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
120 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
121 aData->attribute(attributeID()));
123 PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
124 bool isBlocked = that->blockSignals(true);
125 bool isImmutable = aPoint->setImmutable(true);
127 std::string _attr_name = myAttributeID;
128 double _X = myXSpin->value();
129 double _Y = myYSpin->value();
131 aPoint->setValue(myXSpin->value(), myYSpin->value());
132 updateObject(myFeature);
133 aPoint->setImmutable(isImmutable);
134 that->blockSignals(isBlocked);
139 bool PartSet_WidgetPoint2D::restoreValue()
141 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
142 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
143 aData->attribute(attributeID()));
146 std::string _attr_name = myAttributeID;
147 double _X = aPoint->x();
148 double _Y = aPoint->y();
150 bool isBlocked = this->blockSignals(true);
151 myXSpin->setValue(aPoint->x());
152 myYSpin->setValue(aPoint->y());
153 this->blockSignals(isBlocked);
157 QWidget* PartSet_WidgetPoint2D::getControl() const
162 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
164 QList<QWidget*> aControls;
165 aControls.append(myXSpin);
166 aControls.append(myYSpin);
170 //bool PartSet_WidgetPoint2D::initFromPrevious(ObjectPtr theObject)
172 // if (myOptionParam.length() == 0)
174 // std::shared_ptr<ModelAPI_Data> aData = theObject->data();
175 // std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
176 // aData->attribute(myOptionParam));
178 // bool isBlocked = this->blockSignals(true);
179 // myXSpin->setValue(aPoint->x());
180 // myYSpin->setValue(aPoint->y());
181 // this->blockSignals(isBlocked);
183 // emit valuesChanged();
184 // emit storedPoint2D(theObject, myOptionParam);
190 void PartSet_WidgetPoint2D::activate()
192 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
193 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
194 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
195 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
196 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
199 aModes << TopAbs_VERTEX;
200 myWorkshop->moduleConnector()->activateSubShapesSelection(aModes);
203 void PartSet_WidgetPoint2D::deactivate()
205 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
206 disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
207 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
208 disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
209 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
210 myWorkshop->moduleConnector()->deactivateSubShapesSelection();
213 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView,
214 const TopoDS_Shape& theShape,
215 double& theX, double& theY) const
217 if (!theShape.IsNull()) {
218 if (theShape.ShapeType() == TopAbs_VERTEX) {
219 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
220 if (!aVertex.IsNull()) {
221 // A case when point is taken from existing vertex
222 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
223 PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
232 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
234 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
235 NCollection_List<TopoDS_Shape> aShapes;
236 std::list<ObjectPtr> aObjects;
237 aSelection->selectedShapes(aShapes, aObjects);
238 if (aShapes.Extent() > 0) {
239 TopoDS_Shape aShape = aShapes.First();
241 if (getPoint2d(theWnd->v3dView(), aShape, aX, aY)) {
244 PartSet_Tools::setConstraints(mySketch, feature(), attributeID(),aX, aY);
245 emit vertexSelected(aObjects.front(), aShape);
246 emit focusOutWidget(this);
250 // A case when point is taken from mouse event
251 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
253 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
256 emit focusOutWidget(this);
260 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
262 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
265 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
269 double PartSet_WidgetPoint2D::x() const
271 return myXSpin->value();
274 double PartSet_WidgetPoint2D::y() const
276 return myYSpin->value();