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