Salome HOME
51c3289a57fbfb919cb614688f6a05ad799eb2ff
[modules/shaper.git] / src / PartSet / PartSet_WidgetPoint2d.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_WidgetPoint2D.cpp
4 // Created:     25 Apr 2014
5 // Author:      Natalia ERMOLAEVA
6
7 #include "PartSet_WidgetPoint2d.h"
8 #include <PartSet_Tools.h>
9
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>
17
18 #include <ModuleBase_DoubleSpinBox.h>
19 #include <ModuleBase_Tools.h>
20 #include <ModuleBase_IViewWindow.h>
21
22 #include <Config_Keywords.h>
23 #include <Config_WidgetAPI.h>
24
25 #include <Events_Loop.h>
26 #include <ModelAPI_Events.h>
27
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>
33
34 #include <QGroupBox>
35 #include <QGridLayout>
36 #include <QLabel>
37 #include <QEvent>
38 #include <QMouseEvent>
39 #include <QApplication>
40
41 #include <TopoDS.hxx>
42 #include <TopoDS_Vertex.hxx>
43 #include <BRep_Tool.hxx>
44
45 #include <cfloat>
46 #include <climits>
47
48 PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent, 
49                                               const Config_WidgetAPI* theData,
50                                               const std::string& theParentId)
51     : ModuleBase_ModelWidget(theParent, theData, theParentId)
52 {
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);
57
58   QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
59   ModuleBase_Tools::adjustMargins(aGroupLay);
60   aGroupLay->setColumnStretch(1, 1);
61   {
62     QLabel* aLabel = new QLabel(myGroupBox);
63     aLabel->setText("X");
64     aLabel->setPixmap(QPixmap(":pictures/x_point.png"));
65     aGroupLay->addWidget(aLabel, 0, 0);
66
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);
72
73     connect(myXSpin, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
74   }
75   {
76     QLabel* aLabel = new QLabel(myGroupBox);
77     aLabel->setText("Y");
78     aLabel->setPixmap(QPixmap(":pictures/y_point.png"));
79     aGroupLay->addWidget(aLabel, 1, 0);
80
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);
86
87     connect(myYSpin, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
88   }
89 }
90
91 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
92 {
93 }
94
95 bool PartSet_WidgetPoint2D::setSelection(ModuleBase_ViewerPrs theValue)
96 {
97   Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
98   bool isDone = false;
99   TopoDS_Shape aShape = theValue.shape();
100   double aX, aY;
101   if (getPoint2d(aView, aShape, aX, aY)) {
102     setPoint(aX, aY);
103     isDone = true;
104   }
105   return isDone;
106 }
107
108 void PartSet_WidgetPoint2D::setPoint(double theX, double theY)
109 {
110
111   bool isBlocked = this->blockSignals(true);
112   myXSpin->blockSignals(true);
113   myXSpin->setValue(theX);
114   myXSpin->blockSignals(false);
115
116   myYSpin->blockSignals(true);
117   myYSpin->setValue(theY);
118   myYSpin->blockSignals(false);
119   this->blockSignals(isBlocked);
120
121   emit valuesChanged();
122 }
123
124 bool PartSet_WidgetPoint2D::storeValue() const
125 {
126   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
127   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
128       aData->attribute(attributeID()));
129   
130   PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
131   bool isBlocked = that->blockSignals(true);
132   bool isImmutable = aPoint->setImmutable(true);
133 #ifdef _DEBUG
134   std::string _attr_name = myAttributeID;
135   double _X = myXSpin->value();
136   double _Y = myYSpin->value();
137 #endif
138   aPoint->setValue(myXSpin->value(), myYSpin->value());
139   updateObject(myFeature);
140   aPoint->setImmutable(isImmutable);
141   that->blockSignals(isBlocked);
142
143   return true;
144 }
145
146 bool PartSet_WidgetPoint2D::restoreValue()
147 {
148   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
149   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
150       aData->attribute(attributeID()));
151
152 #ifdef _DEBUG
153   std::string _attr_name = myAttributeID;
154   double _X = aPoint->x();
155   double _Y = aPoint->y();
156 #endif
157   bool isBlocked = this->blockSignals(true);
158   myXSpin->blockSignals(true);
159   myXSpin->setValue(aPoint->x());
160   myXSpin->blockSignals(false);
161
162   myYSpin->blockSignals(true);
163   myYSpin->setValue(aPoint->y());
164   myYSpin->blockSignals(false);
165   this->blockSignals(isBlocked);
166   return true;
167 }
168
169 QWidget* PartSet_WidgetPoint2D::getControl() const
170 {
171   return myGroupBox;
172 }
173
174 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
175 {
176   QList<QWidget*> aControls;
177   aControls.append(myXSpin);
178   aControls.append(myYSpin);
179   return aControls;
180 }
181
182
183 void PartSet_WidgetPoint2D::activate()
184 {
185   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
186   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
187           this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
188   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
189           this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
190
191   QIntList aModes;
192   aModes << TopAbs_VERTEX;
193   myWorkshop->moduleConnector()->activateSubShapesSelection(aModes);
194 }
195
196 void PartSet_WidgetPoint2D::deactivate()
197 {
198   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
199   disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
200              this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
201   disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
202              this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
203   myWorkshop->moduleConnector()->deactivateSubShapesSelection();
204   myWorkshop->operationMgr()->setLockValidating(false);
205 }
206
207 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView, 
208                                        const TopoDS_Shape& theShape, 
209                                        double& theX, double& theY) const
210 {
211   if (!theShape.IsNull()) {
212     if (theShape.ShapeType() == TopAbs_VERTEX) {
213       const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
214       if (!aVertex.IsNull()) {
215         // A case when point is taken from existing vertex
216         gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
217         PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
218         return true;
219       }
220     }
221   }
222   return false;
223 }
224
225
226 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
227 {
228   XGUI_Selection* aSelection = myWorkshop->selector()->selection();
229   NCollection_List<TopoDS_Shape> aShapes;
230   std::list<ObjectPtr> aObjects;
231   aSelection->selectedShapes(aShapes, aObjects);
232   if (aShapes.Extent() > 0) {
233     TopoDS_Shape aShape = aShapes.First();
234     double aX, aY;
235     if (getPoint2d(theWnd->v3dView(), aShape, aX, aY)) {
236       setPoint(aX, aY);
237
238       PartSet_Tools::setConstraints(mySketch, feature(), attributeID(),aX, aY);
239       emit vertexSelected(aObjects.front(), aShape);
240       emit focusOutWidget(this);
241       return;
242     }
243   }
244   // A case when point is taken from mouse event
245   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
246   double aX, anY;
247   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
248   setPoint(aX, anY);
249
250   emit focusOutWidget(this);
251 }
252
253
254 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
255 {
256   myWorkshop->operationMgr()->setLockValidating(true);
257   myWorkshop->propertyPanel()->setOkEnabled(false);
258
259   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
260
261   double aX, anY;
262   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
263   setPoint(aX, anY);
264 }
265
266 double PartSet_WidgetPoint2D::x() const
267 {
268   return myXSpin->value();
269 }
270
271 double PartSet_WidgetPoint2D::y() const
272 {
273   return myYSpin->value();
274 }
275
276 void PartSet_WidgetPoint2D::onValuesChanged()
277 {
278   myWorkshop->operationMgr()->setLockValidating(false);
279   emit valuesChanged();
280 }