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