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