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