]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_WidgetPoint2d.cpp
Salome HOME
a8cd1a82388860ffaeb3fdc8d764e7d56962e6b7
[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_OperationMgr.h>
16
17 #include <ModuleBase_DoubleSpinBox.h>
18 #include <ModuleBase_Tools.h>
19 #include <ModuleBase_IViewWindow.h>
20
21 #include <Config_Keywords.h>
22 #include <Config_WidgetAPI.h>
23
24 #include <Events_Loop.h>
25 #include <ModelAPI_Events.h>
26
27 #include <ModelAPI_Feature.h>
28 #include <ModelAPI_Data.h>
29 #include <ModelAPI_Object.h>
30 #include <GeomDataAPI_Point2D.h>
31 #include <GeomAPI_Pnt2d.h>
32
33 #include <QGroupBox>
34 #include <QGridLayout>
35 #include <QLabel>
36 #include <QEvent>
37 #include <QMouseEvent>
38 #include <QApplication>
39
40 #include <TopoDS.hxx>
41 #include <TopoDS_Vertex.hxx>
42 #include <BRep_Tool.hxx>
43
44 #include <cfloat>
45 #include <climits>
46
47 const double MaxCoordinate = 1e12;
48
49
50 PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent, 
51                                               const Config_WidgetAPI* theData,
52                                               const std::string& theParentId)
53     : ModuleBase_ModelWidget(theParent, theData, theParentId)
54 {
55   //myOptionParam = theData->getProperty(PREVIOUS_FEATURE_PARAM);
56   QString aPageName = QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME));
57   myGroupBox = new QGroupBox(aPageName, theParent);
58   myGroupBox->setFlat(false);
59
60   QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
61   ModuleBase_Tools::adjustMargins(aGroupLay);
62   aGroupLay->setColumnStretch(1, 1);
63   {
64     QLabel* aLabel = new QLabel(myGroupBox);
65     aLabel->setText(tr("X"));
66     aLabel->setPixmap(QPixmap(":pictures/x_point.png"));
67     aGroupLay->addWidget(aLabel, 0, 0);
68
69     myXSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
70     myXSpin->setMinimum(-DBL_MAX);
71     myXSpin->setMaximum(DBL_MAX);
72     myXSpin->setToolTip(tr("X"));
73     aGroupLay->addWidget(myXSpin, 0, 1);
74
75     connect(myXSpin, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
76   }
77   {
78     QLabel* aLabel = new QLabel(myGroupBox);
79     aLabel->setText(tr("Y"));
80     aLabel->setPixmap(QPixmap(":pictures/y_point.png"));
81     aGroupLay->addWidget(aLabel, 1, 0);
82
83     myYSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
84     myYSpin->setMinimum(-DBL_MAX);
85     myYSpin->setMaximum(DBL_MAX);
86     myYSpin->setToolTip(tr("Y"));
87     aGroupLay->addWidget(myYSpin, 1, 1);
88
89     connect(myYSpin, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
90   }
91 }
92
93 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
94 {
95 }
96
97 bool PartSet_WidgetPoint2D::setSelection(ModuleBase_ViewerPrs theValue)
98 {
99   Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
100   bool isDone = false;
101   TopoDS_Shape aShape = theValue.shape();
102   double aX, aY;
103   if (getPoint2d(aView, aShape, aX, aY)) {
104     isDone = setPoint(aX, aY);
105   }
106   return isDone;
107 }
108
109 bool PartSet_WidgetPoint2D::setPoint(double theX, double theY)
110 {
111   if (fabs(theX) >= MaxCoordinate)
112     return false;
113   if (fabs(theY) >= MaxCoordinate)
114     return false;
115   bool isBlocked = this->blockSignals(true);
116   myXSpin->blockSignals(true);
117   myXSpin->setValue(theX);
118   myXSpin->blockSignals(false);
119
120   myYSpin->blockSignals(true);
121   myYSpin->setValue(theY);
122   myYSpin->blockSignals(false);
123   this->blockSignals(isBlocked);
124
125   emit valuesChanged();
126   return true;
127 }
128
129 bool PartSet_WidgetPoint2D::storeValue() const
130 {
131   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
132   if (!aData) // can be on abort of sketcher element
133     return false;
134   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
135       aData->attribute(attributeID()));
136   
137   PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
138   bool isBlocked = that->blockSignals(true);
139   bool isImmutable = aPoint->setImmutable(true);
140 #ifdef _DEBUG
141   std::string _attr_name = myAttributeID;
142   double _X = myXSpin->value();
143   double _Y = myYSpin->value();
144 #endif
145   aPoint->setValue(myXSpin->value(), myYSpin->value());
146   // after movement the solver will call the update event: optimization
147   moveObject(myFeature);
148   aPoint->setImmutable(isImmutable);
149   that->blockSignals(isBlocked);
150
151   return true;
152 }
153
154 bool PartSet_WidgetPoint2D::restoreValue()
155 {
156   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
157   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
158       aData->attribute(attributeID()));
159
160 #ifdef _DEBUG
161   std::string _attr_name = myAttributeID;
162   double _X = aPoint->x();
163   double _Y = aPoint->y();
164 #endif
165   bool isBlocked = this->blockSignals(true);
166   myXSpin->blockSignals(true);
167   myXSpin->setValue(aPoint->x());
168   myXSpin->blockSignals(false);
169
170   myYSpin->blockSignals(true);
171   myYSpin->setValue(aPoint->y());
172   myYSpin->blockSignals(false);
173   this->blockSignals(isBlocked);
174   return true;
175 }
176
177 QWidget* PartSet_WidgetPoint2D::getControl() const
178 {
179   return myGroupBox;
180 }
181
182 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
183 {
184   QList<QWidget*> aControls;
185   aControls.append(myXSpin);
186   aControls.append(myYSpin);
187   return aControls;
188 }
189
190
191 void PartSet_WidgetPoint2D::activate()
192 {
193   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
194   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
195           this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
196   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
197           this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
198
199   QIntList aModes;
200   aModes << TopAbs_VERTEX;
201   myWorkshop->moduleConnector()->activateSubShapesSelection(aModes);
202   if (!isEditingMode()) {
203     // the control value is stored to the mode by the focus in on the widget
204     // we need the value is initialized in order to enable the apply button in the property panel
205     // it should happens only in the creation mode because during edition all fields are filled
206     storeValue();
207   }
208 }
209
210 void PartSet_WidgetPoint2D::deactivate()
211 {
212   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
213   disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
214              this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
215   disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
216              this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
217   myWorkshop->moduleConnector()->deactivateSubShapesSelection();
218   myWorkshop->operationMgr()->setLockValidating(false);
219 }
220
221 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView, 
222                                        const TopoDS_Shape& theShape, 
223                                        double& theX, double& theY) const
224 {
225   if (!theShape.IsNull()) {
226     if (theShape.ShapeType() == TopAbs_VERTEX) {
227       const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
228       if (!aVertex.IsNull()) {
229         // A case when point is taken from existing vertex
230         gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
231         PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
232         return true;
233       }
234     }
235   }
236   return false;
237 }
238
239
240 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
241 {
242   XGUI_Selection* aSelection = myWorkshop->selector()->selection();
243   // TODO: This fragment doesn't work because bug in OCC Viewer. It can be used after fixing.
244   //NCollection_List<TopoDS_Shape> aShapes;
245   //std::list<ObjectPtr> aObjects;
246   //aSelection->selectedShapes(aShapes, aObjects);
247   //if (aShapes.Extent() > 0) {
248   //  TopoDS_Shape aShape = aShapes.First();
249   //  double aX, aY;
250   //  if (getPoint2d(theWnd->v3dView(), aShape, aX, aY)) {
251   //    setPoint(aX, aY);
252
253   //    PartSet_Tools::setConstraints(mySketch, feature(), attributeID(),aX, aY);
254   //    emit vertexSelected(aObjects.front(), aShape);
255   //    emit focusOutWidget(this);
256   //    return;
257   //  }
258   //}
259   // End of Bug dependent fragment
260
261   // A case when point is taken from mouse event
262   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
263   double aX, anY;
264   Handle(V3d_View) aView = theWnd->v3dView();
265   PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
266   if (!setPoint(aX, anY))
267     return;
268
269   std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
270       GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
271   QList<FeaturePtr> aIgnore;
272   aIgnore.append(feature());
273
274   double aTolerance = aView->Convert(7);
275   std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt = 
276     PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
277   if (aAttrPnt.get() != NULL) {
278     aFeaturePoint->setValue(aAttrPnt->pnt());
279     PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
280     emit vertexSelected();
281   }
282   emit focusOutWidget(this);
283 }
284
285
286 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
287 {
288   if (isEditingMode())
289     return;
290   myWorkshop->operationMgr()->setLockValidating(true);
291   // the Ok button should be disabled in the property panel by moving the mouse point in the viewer
292   // this leads that the user does not try to click Ok and it avoids an incorrect situation that the 
293   // line is moved to the cursor to the Ok button
294   myWorkshop->operationMgr()->setApplyEnabled(false);
295
296   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
297
298   double aX, anY;
299   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
300   setPoint(aX, anY);
301 }
302
303 double PartSet_WidgetPoint2D::x() const
304 {
305   return myXSpin->value();
306 }
307
308 double PartSet_WidgetPoint2D::y() const
309 {
310   return myYSpin->value();
311 }
312
313 void PartSet_WidgetPoint2D::onValuesChanged()
314 {
315   myWorkshop->operationMgr()->setLockValidating(false);
316   emit valuesChanged();
317 }