Salome HOME
b49330a6b905764efd807fd9a6abda7320be0325
[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     storeValueCustom();
116   }
117 }
118
119 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
120 {
121 }
122
123 bool PartSet_WidgetPoint2D::setSelection(const QList<ModuleBase_ViewerPrs>& theValues, int& thePosition)
124 {
125   if (thePosition < 0 || thePosition >= theValues.size())
126     return false;
127   ModuleBase_ViewerPrs aValue = theValues[thePosition];
128   thePosition++;
129
130   Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
131   bool isDone = false;
132   TopoDS_Shape aShape = aValue.shape();
133   double aX, aY;
134   if (getPoint2d(aView, aShape, aX, aY)) {
135     isDone = setPoint(aX, aY);
136   }
137   return isDone;
138 }
139
140 bool PartSet_WidgetPoint2D::setPoint(double theX, double theY)
141 {
142   if (fabs(theX) >= MaxCoordinate)
143     return false;
144   if (fabs(theY) >= MaxCoordinate)
145     return false;
146
147   ModuleBase_Tools::setSpinValue(myXSpin, theX);
148   ModuleBase_Tools::setSpinValue(myYSpin, theY);
149
150   storeValue();
151   return true;
152 }
153
154 bool PartSet_WidgetPoint2D::storeValueCustom() const
155 {
156   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
157   if (!aData) // can be on abort of sketcher element
158     return false;
159   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
160       aData->attribute(attributeID()));
161   
162   PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
163   bool isBlocked = that->blockSignals(true);
164   bool isImmutable = aPoint->setImmutable(true);
165 #ifdef _DEBUG
166   std::string _attr_name = myAttributeID;
167   double _X = myXSpin->value();
168   double _Y = myYSpin->value();
169 #endif
170   aPoint->setValue(myXSpin->value(), myYSpin->value());
171   // after movement the solver will call the update event: optimization
172   moveObject(myFeature);
173   aPoint->setImmutable(isImmutable);
174   that->blockSignals(isBlocked);
175
176   return true;
177 }
178
179 bool PartSet_WidgetPoint2D::restoreValue()
180 {
181   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
182   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
183       aData->attribute(attributeID()));
184
185 #ifdef _DEBUG
186   std::string _attr_name = myAttributeID;
187   double _X = aPoint->x();
188   double _Y = aPoint->y();
189 #endif
190
191   ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
192   ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
193   return true;
194 }
195
196 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
197 {
198   QList<QWidget*> aControls;
199   aControls.append(myXSpin);
200   aControls.append(myYSpin);
201   return aControls;
202 }
203
204
205 void PartSet_WidgetPoint2D::activateCustom()
206 {
207   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
208   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
209           this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
210   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
211           this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
212
213   QIntList aModes;
214   aModes << TopAbs_VERTEX;
215   if (isEditingMode())
216     aModes << TopAbs_EDGE;
217   myWorkshop->moduleConnector()->activateSubShapesSelection(aModes);
218 }
219
220 void PartSet_WidgetPoint2D::deactivate()
221 {
222   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
223   disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
224              this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
225   disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
226              this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
227   myWorkshop->moduleConnector()->deactivateSubShapesSelection();
228   myWorkshop->operationMgr()->setLockValidating(false);
229 }
230
231 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView, 
232                                        const TopoDS_Shape& theShape, 
233                                        double& theX, double& theY) const
234 {
235   if (!theShape.IsNull()) {
236     if (theShape.ShapeType() == TopAbs_VERTEX) {
237       const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
238       if (!aVertex.IsNull()) {
239         // A case when point is taken from existing vertex
240         gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
241         PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
242         return true;
243       }
244     }
245   }
246   return false;
247 }
248
249
250 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
251 {
252   // the contex menu release by the right button should not be processed by this widget
253   if (theEvent->button() != Qt::LeftButton)
254     return;
255
256   XGUI_Selection* aSelection = myWorkshop->selector()->selection();
257   Handle(V3d_View) aView = theWnd->v3dView();
258   // TODO: This fragment doesn't work because bug in OCC Viewer. It can be used after fixing.
259   NCollection_List<TopoDS_Shape> aShapes;
260   std::list<ObjectPtr> aObjects;
261   aSelection->selectedShapes(aShapes, aObjects);
262   // if we have selection
263   if (aShapes.Extent() > 0) {
264     TopoDS_Shape aShape = aShapes.First();
265     ObjectPtr aObject = aObjects.front();
266     FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aObject);
267     if (aSelectedFeature.get() != NULL) {
268       std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
269               std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
270       if ((!aSPFeature) && (!aShape.IsNull())) {
271         ResultPtr aFixedObject = PartSet_Tools::findFixedObjectByExternal(aShape, aObject, mySketch);
272         if (!aFixedObject.get())
273           aFixedObject = PartSet_Tools::createFixedObjectByExternal(aShape, aObject, mySketch);
274       }
275     }
276     double aX, aY;
277     if (getPoint2d(aView, aShape, aX, aY)) {
278       setPoint(aX, aY);
279
280       PartSet_Tools::setConstraints(mySketch, feature(), attributeID(),aX, aY);
281       emit vertexSelected();
282       emit focusOutWidget(this);
283       return;
284     }
285   }
286   // End of Bug dependent fragment
287
288   // A case when point is taken from mouse event
289   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
290   double aX, anY;
291   PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
292   if (!setPoint(aX, anY))
293     return;
294
295   /// Start alternative code
296   //std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
297   //    GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
298   //QList<FeaturePtr> aIgnore;
299   //aIgnore.append(feature());
300
301   //double aTolerance = aView->Convert(7);
302   //std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt = 
303   //  PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
304   //if (aAttrPnt.get() != NULL) {
305   //  aFeaturePoint->setValue(aAttrPnt->pnt());
306   //  PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
307   //  emit vertexSelected();
308   //}
309   /// End alternative code
310   emit focusOutWidget(this);
311 }
312
313
314 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
315 {
316   if (isEditingMode())
317     return;
318   myWorkshop->operationMgr()->setLockValidating(true);
319   // the Ok button should be disabled in the property panel by moving the mouse point in the viewer
320   // this leads that the user does not try to click Ok and it avoids an incorrect situation that the 
321   // line is moved to the cursor to the Ok button
322   myWorkshop->operationMgr()->setApplyEnabled(false);
323
324   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
325
326   double aX, anY;
327   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
328   setPoint(aX, anY);
329 }
330
331 double PartSet_WidgetPoint2D::x() const
332 {
333   return myXSpin->value();
334 }
335
336 double PartSet_WidgetPoint2D::y() const
337 {
338   return myYSpin->value();
339 }
340
341 void PartSet_WidgetPoint2D::onValuesChanged()
342 {
343   myWorkshop->operationMgr()->setLockValidating(false);
344   emit valuesChanged();
345 }