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_WidgetValueFeature.h>
15 #include <ModuleBase_DoubleSpinBox.h>
16 #include <ModuleBase_Tools.h>
17 #include <ModuleBase_IViewWindow.h>
19 #include <Config_Keywords.h>
20 #include <Config_WidgetAPI.h>
22 #include <Events_Loop.h>
23 #include <ModelAPI_Events.h>
25 #include <ModelAPI_Feature.h>
26 #include <ModelAPI_Data.h>
27 #include <ModelAPI_Object.h>
28 #include <GeomDataAPI_Point2D.h>
29 #include <GeomAPI_Pnt2d.h>
32 #include <QGridLayout>
35 #include <QMouseEvent>
36 #include <QApplication>
39 #include <TopoDS_Vertex.hxx>
40 #include <BRep_Tool.hxx>
45 PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent,
46 const Config_WidgetAPI* theData,
47 const std::string& theParentId)
48 : ModuleBase_ModelWidget(theParent, theData, theParentId)
50 //myOptionParam = theData->getProperty(PREVIOUS_FEATURE_PARAM);
51 QString aPageName = QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME));
52 myGroupBox = new QGroupBox(aPageName, theParent);
53 myGroupBox->setFlat(false);
55 QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
56 ModuleBase_Tools::adjustMargins(aGroupLay);
57 aGroupLay->setColumnStretch(1, 1);
59 QLabel* aLabel = new QLabel(myGroupBox);
61 aLabel->setPixmap(QPixmap(":pictures/x_point.png"));
62 aGroupLay->addWidget(aLabel, 0, 0);
64 myXSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
65 myXSpin->setMinimum(-DBL_MAX);
66 myXSpin->setMaximum(DBL_MAX);
67 myXSpin->setToolTip("X");
68 aGroupLay->addWidget(myXSpin, 0, 1);
70 connect(myXSpin, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
73 QLabel* aLabel = new QLabel(myGroupBox);
75 aLabel->setPixmap(QPixmap(":pictures/y_point.png"));
76 aGroupLay->addWidget(aLabel, 1, 0);
78 myYSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
79 myYSpin->setMinimum(-DBL_MAX);
80 myYSpin->setMaximum(DBL_MAX);
81 myYSpin->setToolTip("X");
82 aGroupLay->addWidget(myYSpin, 1, 1);
84 connect(myYSpin, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
88 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
92 bool PartSet_WidgetPoint2D::setValue(ModuleBase_WidgetValue* theValue)
96 ModuleBase_WidgetValueFeature* aFeatureValue =
97 dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
99 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aFeatureValue->point();
101 setPoint(aPoint->x(), aPoint->y());
109 void PartSet_WidgetPoint2D::setPoint(double theX, double theY)
112 bool isBlocked = this->blockSignals(true);
113 myXSpin->setValue(theX);
114 myYSpin->setValue(theY);
115 this->blockSignals(isBlocked);
117 emit valuesChanged();
120 bool PartSet_WidgetPoint2D::storeValue() const
122 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
123 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
124 aData->attribute(attributeID()));
126 PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
127 bool isBlocked = that->blockSignals(true);
128 bool isImmutable = aPoint->setImmutable(true);
130 std::string _attr_name = myAttributeID;
131 double _X = myXSpin->value();
132 double _Y = myYSpin->value();
134 aPoint->setValue(myXSpin->value(), myYSpin->value());
135 updateObject(myFeature);
136 aPoint->setImmutable(isImmutable);
137 that->blockSignals(isBlocked);
142 bool PartSet_WidgetPoint2D::restoreValue()
144 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
145 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
146 aData->attribute(attributeID()));
149 std::string _attr_name = myAttributeID;
150 double _X = aPoint->x();
151 double _Y = aPoint->y();
153 bool isBlocked = this->blockSignals(true);
154 myXSpin->setValue(aPoint->x());
155 myYSpin->setValue(aPoint->y());
156 this->blockSignals(isBlocked);
160 QWidget* PartSet_WidgetPoint2D::getControl() const
165 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
167 QList<QWidget*> aControls;
168 aControls.append(myXSpin);
169 aControls.append(myYSpin);
173 //bool PartSet_WidgetPoint2D::initFromPrevious(ObjectPtr theObject)
175 // if (myOptionParam.length() == 0)
177 // std::shared_ptr<ModelAPI_Data> aData = theObject->data();
178 // std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
179 // aData->attribute(myOptionParam));
181 // bool isBlocked = this->blockSignals(true);
182 // myXSpin->setValue(aPoint->x());
183 // myYSpin->setValue(aPoint->y());
184 // this->blockSignals(isBlocked);
186 // emit valuesChanged();
187 // emit storedPoint2D(theObject, myOptionParam);
193 void PartSet_WidgetPoint2D::activate()
195 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
196 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
197 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
198 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
199 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
202 aModes << TopAbs_VERTEX;
203 myWorkshop->moduleConnector()->activateSubShapesSelection(aModes);
206 void PartSet_WidgetPoint2D::deactivate()
208 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
209 disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
210 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
211 disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
212 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
213 myWorkshop->moduleConnector()->deactivateSubShapesSelection();
217 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
219 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
220 NCollection_List<TopoDS_Shape> aShapes;
221 std::list<ObjectPtr> aObjects;
222 aSelection->selectedShapes(aShapes, aObjects);
223 if (aShapes.Extent() > 0) {
224 TopoDS_Shape aShape = aShapes.First();
225 if (!aShape.IsNull()) {
226 if (aShape.ShapeType() == TopAbs_VERTEX) {
227 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
228 if (!aVertex.IsNull()) {
229 // A case when point is taken from existing vertex
230 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
232 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
235 PartSet_Tools::setConstraints(mySketch, feature(), attributeID(),aX, aY);
236 emit vertexSelected(aObjects.front(), aShape);
237 QApplication::processEvents();
238 emit focusOutWidget(this);
244 // A case when point is taken from mouse event
245 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
247 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
250 emit focusOutWidget(this);
254 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
256 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
259 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
263 double PartSet_WidgetPoint2D::x() const
265 return myXSpin->value();
268 double PartSet_WidgetPoint2D::y() const
270 return myYSpin->value();