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>
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::setValue(ModuleBase_WidgetValue* theValue)
95 ModuleBase_WidgetValueFeature* aFeatureValue =
96 dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
98 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aFeatureValue->point();
100 setPoint(aPoint->x(), aPoint->y());
108 void PartSet_WidgetPoint2D::setPoint(double theX, double theY)
111 bool isBlocked = this->blockSignals(true);
112 myXSpin->setValue(theX);
113 myYSpin->setValue(theY);
114 this->blockSignals(isBlocked);
116 emit valuesChanged();
119 bool PartSet_WidgetPoint2D::storeValue() const
121 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
122 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
123 aData->attribute(attributeID()));
125 PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
126 bool isBlocked = that->blockSignals(true);
127 bool isImmutable = aPoint->setImmutable(true);
129 std::string _attr_name = myAttributeID;
130 double _X = myXSpin->value();
131 double _Y = myYSpin->value();
133 aPoint->setValue(myXSpin->value(), myYSpin->value());
134 updateObject(myFeature);
135 aPoint->setImmutable(isImmutable);
136 that->blockSignals(isBlocked);
141 bool PartSet_WidgetPoint2D::restoreValue()
143 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
144 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
145 aData->attribute(attributeID()));
148 std::string _attr_name = myAttributeID;
149 double _X = aPoint->x();
150 double _Y = aPoint->y();
152 bool isBlocked = this->blockSignals(true);
153 myXSpin->setValue(aPoint->x());
154 myYSpin->setValue(aPoint->y());
155 this->blockSignals(isBlocked);
159 QWidget* PartSet_WidgetPoint2D::getControl() const
164 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
166 QList<QWidget*> aControls;
167 aControls.append(myXSpin);
168 aControls.append(myYSpin);
172 //bool PartSet_WidgetPoint2D::initFromPrevious(ObjectPtr theObject)
174 // if (myOptionParam.length() == 0)
176 // std::shared_ptr<ModelAPI_Data> aData = theObject->data();
177 // std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
178 // aData->attribute(myOptionParam));
180 // bool isBlocked = this->blockSignals(true);
181 // myXSpin->setValue(aPoint->x());
182 // myYSpin->setValue(aPoint->y());
183 // this->blockSignals(isBlocked);
185 // emit valuesChanged();
186 // emit storedPoint2D(theObject, myOptionParam);
192 void PartSet_WidgetPoint2D::activate()
194 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
195 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
196 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
197 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
198 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
201 aModes << TopAbs_VERTEX;
202 myWorkshop->moduleConnector()->activateSubShapesSelection(aModes);
205 void PartSet_WidgetPoint2D::deactivate()
207 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
208 disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
209 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
210 disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
211 this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
212 myWorkshop->moduleConnector()->deactivateSubShapesSelection();
216 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
218 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
219 NCollection_List<TopoDS_Shape> aShapes;
220 std::list<ObjectPtr> aObjects;
221 aSelection->selectedShapes(aShapes, aObjects);
222 if (aShapes.Extent() > 0) {
223 TopoDS_Shape aShape = aShapes.First();
224 if (!aShape.IsNull()) {
225 if (aShape.ShapeType() == TopAbs_VERTEX) {
226 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
227 if (!aVertex.IsNull()) {
228 // A case when point is taken from existing vertex
229 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
231 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
233 emit vertexSelected(aObjects.front(), aShape);
234 emit focusOutWidget(this);
240 // A case when point is taken from mouse event
241 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
243 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
246 emit focusOutWidget(this);
250 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
252 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
255 PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
259 double PartSet_WidgetPoint2D::x() const
261 return myXSpin->value();
264 double PartSet_WidgetPoint2D::y() const
266 return myYSpin->value();