Salome HOME
The default colors in preferences for sketch elements and features.
[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 #include <SketchPlugin_ConstraintCoincidence.h>
35
36 #include <QGroupBox>
37 #include <QGridLayout>
38 #include <QLabel>
39 #include <QEvent>
40 #include <QMouseEvent>
41 #include <QApplication>
42
43 #include <TopoDS.hxx>
44 #include <TopoDS_Vertex.hxx>
45 #include <BRep_Tool.hxx>
46
47 #include <cfloat>
48 #include <climits>
49
50 const double MaxCoordinate = 1e12;
51
52
53 PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent, 
54                                               const Config_WidgetAPI* theData,
55                                               const std::string& theParentId)
56     : ModuleBase_ModelWidget(theParent, theData, theParentId)
57 {
58   // the control should accept the focus, so the boolen flag is corrected to be true
59   myIsObligatory = true;
60   //myOptionParam = theData->getProperty(PREVIOUS_FEATURE_PARAM);
61   QString aPageName = QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME));
62   myGroupBox = new QGroupBox(aPageName, theParent);
63   myGroupBox->setFlat(false);
64
65   QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
66   ModuleBase_Tools::adjustMargins(aGroupLay);
67   aGroupLay->setColumnStretch(1, 1);
68   {
69     QLabel* aLabel = new QLabel(myGroupBox);
70     aLabel->setText(tr("X"));
71     aLabel->setPixmap(QPixmap(":pictures/x_point.png"));
72     aGroupLay->addWidget(aLabel, 0, 0);
73
74     myXSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
75     myXSpin->setMinimum(-DBL_MAX);
76     myXSpin->setMaximum(DBL_MAX);
77     myXSpin->setToolTip(tr("X"));
78     aGroupLay->addWidget(myXSpin, 0, 1);
79
80     connect(myXSpin, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
81   }
82   {
83     QLabel* aLabel = new QLabel(myGroupBox);
84     aLabel->setText(tr("Y"));
85     aLabel->setPixmap(QPixmap(":pictures/y_point.png"));
86     aGroupLay->addWidget(aLabel, 1, 0);
87
88     myYSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
89     myYSpin->setMinimum(-DBL_MAX);
90     myYSpin->setMaximum(DBL_MAX);
91     myYSpin->setToolTip(tr("Y"));
92     aGroupLay->addWidget(myYSpin, 1, 1);
93
94     connect(myYSpin, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
95   }
96   QVBoxLayout* aLayout = new QVBoxLayout(this);
97   ModuleBase_Tools::zeroMargins(aLayout);
98   aLayout->addWidget(myGroupBox);
99   setLayout(aLayout);
100 }
101
102 void PartSet_WidgetPoint2D::reset()
103 {
104   if (isComputedDefault()) {
105     //return;
106     if (myFeature->compute(myAttributeID))
107       restoreValue();
108   }
109   else {
110     bool isOk;
111     double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
112     // it is important to block the spin box control in order to do not through out the
113     // locking of the validating state.
114     ModuleBase_Tools::setSpinValue(myXSpin, isOk ? aDefValue : 0.0);
115     ModuleBase_Tools::setSpinValue(myYSpin, isOk ? aDefValue : 0.0);
116     storeValueCustom();
117   }
118 }
119
120 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
121 {
122 }
123
124 bool PartSet_WidgetPoint2D::setSelection(const QList<ModuleBase_ViewerPrs>& theValues, int& thePosition)
125 {
126   if (thePosition < 0 || thePosition >= theValues.size())
127     return false;
128   ModuleBase_ViewerPrs aValue = theValues[thePosition];
129   thePosition++;
130
131   Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
132   bool isDone = false;
133   TopoDS_Shape aShape = aValue.shape();
134   double aX, aY;
135   if (getPoint2d(aView, aShape, aX, aY)) {
136     isDone = setPoint(aX, aY);
137   }
138   return isDone;
139 }
140
141 bool PartSet_WidgetPoint2D::setPoint(double theX, double theY)
142 {
143   if (fabs(theX) >= MaxCoordinate)
144     return false;
145   if (fabs(theY) >= MaxCoordinate)
146     return false;
147
148   ModuleBase_Tools::setSpinValue(myXSpin, theX);
149   ModuleBase_Tools::setSpinValue(myYSpin, theY);
150
151   storeValue();
152   return true;
153 }
154
155 bool PartSet_WidgetPoint2D::storeValueCustom() const
156 {
157   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
158   if (!aData) // can be on abort of sketcher element
159     return false;
160   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
161       aData->attribute(attributeID()));
162   
163   PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
164   bool isBlocked = that->blockSignals(true);
165   bool isImmutable = aPoint->setImmutable(true);
166 #ifdef _DEBUG
167   std::string _attr_name = myAttributeID;
168   double _X = myXSpin->value();
169   double _Y = myYSpin->value();
170 #endif
171   aPoint->setValue(myXSpin->value(), myYSpin->value());
172   // after movement the solver will call the update event: optimization
173   moveObject(myFeature);
174   aPoint->setImmutable(isImmutable);
175   that->blockSignals(isBlocked);
176
177   return true;
178 }
179
180 bool PartSet_WidgetPoint2D::restoreValue()
181 {
182   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
183   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
184       aData->attribute(attributeID()));
185
186 #ifdef _DEBUG
187   std::string _attr_name = myAttributeID;
188   double _X = aPoint->x();
189   double _Y = aPoint->y();
190 #endif
191
192   ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
193   ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
194   return true;
195 }
196
197 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
198 {
199   QList<QWidget*> aControls;
200   aControls.append(myXSpin);
201   aControls.append(myYSpin);
202   return aControls;
203 }
204
205
206 void PartSet_WidgetPoint2D::activateCustom()
207 {
208   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
209   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
210           this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
211   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
212           this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
213
214   QIntList aModes;
215   aModes << TopAbs_VERTEX;
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     } else if (aShape.ShapeType() == TopAbs_EDGE) {
285       // Create point-edge coincedence
286       FeaturePtr aFeature = mySketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
287       std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
288
289       std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
290           ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
291       AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
292       std::shared_ptr<GeomDataAPI_Point2D> aThisPoint = 
293         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
294       aRef1->setAttr(aThisPoint);
295
296       std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
297           ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
298       aRef2->setObject(aObject);
299       aFeature->execute();
300       emit vertexSelected();
301       emit focusOutWidget(this);
302       return;
303     }
304   }
305   // End of Bug dependent fragment
306
307   // A case when point is taken from mouse event
308   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
309   double aX, anY;
310   PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
311   if (!setPoint(aX, anY))
312     return;
313
314   /// Start alternative code
315   //std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
316   //    GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
317   //QList<FeaturePtr> aIgnore;
318   //aIgnore.append(feature());
319
320   //double aTolerance = aView->Convert(7);
321   //std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt = 
322   //  PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
323   //if (aAttrPnt.get() != NULL) {
324   //  aFeaturePoint->setValue(aAttrPnt->pnt());
325   //  PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
326   //  emit vertexSelected();
327   //}
328   /// End alternative code
329   emit focusOutWidget(this);
330 }
331
332
333 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
334 {
335   if (isEditingMode())
336     return;
337   myWorkshop->operationMgr()->setLockValidating(true);
338   // the Ok button should be disabled in the property panel by moving the mouse point in the viewer
339   // this leads that the user does not try to click Ok and it avoids an incorrect situation that the 
340   // line is moved to the cursor to the Ok button
341   myWorkshop->operationMgr()->setApplyEnabled(false);
342
343   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
344
345   double aX, anY;
346   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
347   setPoint(aX, anY);
348 }
349
350 double PartSet_WidgetPoint2D::x() const
351 {
352   return myXSpin->value();
353 }
354
355 double PartSet_WidgetPoint2D::y() const
356 {
357   return myYSpin->value();
358 }
359
360 void PartSet_WidgetPoint2D::onValuesChanged()
361 {
362   myWorkshop->operationMgr()->setLockValidating(false);
363   emit valuesChanged();
364 }