]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_WidgetPoint2d.cpp
Salome HOME
Preselection using in operations: setSelection of widget returns a modified list...
[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 #include <PartSet_Module.h>
10
11 #include <XGUI_Workshop.h>
12 #include <XGUI_ViewerProxy.h>
13 #include <XGUI_ModuleConnector.h>
14 #include <XGUI_SelectionMgr.h>
15 #include <XGUI_Selection.h>
16 #include <XGUI_OperationMgr.h>
17
18 #include <ModuleBase_ParamSpinBox.h>
19 #include <ModuleBase_Tools.h>
20 #include <ModuleBase_IViewWindow.h>
21
22 #include <Config_Keywords.h>
23 #include <Config_WidgetAPI.h>
24
25 #include <Events_Loop.h>
26 #include <ModelAPI_Events.h>
27
28 #include <ModelAPI_Feature.h>
29 #include <ModelAPI_Data.h>
30 #include <ModelAPI_Object.h>
31 #include <GeomDataAPI_Point2D.h>
32 #include <GeomAPI_Pnt2d.h>
33
34 #include <SketchPlugin_Feature.h>
35 #include <SketchPlugin_ConstraintCoincidence.h>
36
37 #include <QGroupBox>
38 #include <QGridLayout>
39 #include <QLabel>
40 #include <QEvent>
41 #include <QMouseEvent>
42 #include <QApplication>
43
44 #include <TopoDS.hxx>
45 #include <TopoDS_Vertex.hxx>
46 #include <BRep_Tool.hxx>
47
48 #include <cfloat>
49 #include <climits>
50
51 const double MaxCoordinate = 1e12;
52
53
54 PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent, 
55                                               const Config_WidgetAPI* theData,
56                                               const std::string& theParentId)
57     : ModuleBase_ModelWidget(theParent, theData, theParentId)
58 {
59   // the control should accept the focus, so the boolen flag is corrected to be true
60   myIsObligatory = true;
61   //myOptionParam = theData->getProperty(PREVIOUS_FEATURE_PARAM);
62   QString aPageName = QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME));
63   myGroupBox = new QGroupBox(aPageName, theParent);
64   myGroupBox->setFlat(false);
65
66   QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
67   ModuleBase_Tools::adjustMargins(aGroupLay);
68   aGroupLay->setColumnStretch(1, 1);
69   {
70     QLabel* aLabel = new QLabel(myGroupBox);
71     aLabel->setText(tr("X"));
72     aLabel->setPixmap(QPixmap(":pictures/x_point.png"));
73     aGroupLay->addWidget(aLabel, 0, 0);
74
75     myXSpin = new ModuleBase_ParamSpinBox(myGroupBox);
76     myXSpin->setMinimum(-DBL_MAX);
77     myXSpin->setMaximum(DBL_MAX);
78     myXSpin->setToolTip(tr("X"));
79     aGroupLay->addWidget(myXSpin, 0, 1);
80
81     connect(myXSpin, SIGNAL(valueChanged(const QString&)), this, SLOT(onValuesChanged()));
82   }
83   {
84     QLabel* aLabel = new QLabel(myGroupBox);
85     aLabel->setText(tr("Y"));
86     aLabel->setPixmap(QPixmap(":pictures/y_point.png"));
87     aGroupLay->addWidget(aLabel, 1, 0);
88
89     myYSpin = new ModuleBase_ParamSpinBox(myGroupBox);
90     myYSpin->setMinimum(-DBL_MAX);
91     myYSpin->setMaximum(DBL_MAX);
92     myYSpin->setToolTip(tr("Y"));
93     aGroupLay->addWidget(myYSpin, 1, 1);
94
95     connect(myYSpin, SIGNAL(valueChanged(const QString&)), this, SLOT(onValuesChanged()));
96   }
97   QVBoxLayout* aLayout = new QVBoxLayout(this);
98   ModuleBase_Tools::zeroMargins(aLayout);
99   aLayout->addWidget(myGroupBox);
100   setLayout(aLayout);
101 }
102
103 void PartSet_WidgetPoint2D::reset()
104 {
105   if (!isUseReset())
106     return;
107
108   if (isComputedDefault() || myXSpin->hasVariable() || myYSpin->hasVariable()) {
109     return;
110     //if (myFeature->compute(myAttributeID))
111     //  restoreValue();
112   }
113   else {
114     bool isOk;
115     double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
116     // it is important to block the spin box control in order to do not through out the
117     // locking of the validating state.
118     ModuleBase_Tools::setSpinValue(myXSpin, isOk ? aDefValue : 0.0);
119     ModuleBase_Tools::setSpinValue(myYSpin, isOk ? aDefValue : 0.0);
120     storeValueCustom();
121   }
122 }
123
124 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
125 {
126 }
127
128 bool PartSet_WidgetPoint2D::setSelection(QList<ModuleBase_ViewerPrs>& theValues)
129 {
130   if (theValues.empty())
131     return false;
132
133   ModuleBase_ViewerPrs aValue = theValues.takeFirst();
134
135   Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
136   bool isDone = false;
137   TopoDS_Shape aShape = aValue.shape();
138   double aX, aY;
139   if (getPoint2d(aView, aShape, aX, aY)) {
140     isDone = setPoint(aX, aY);
141   }
142   return isDone;
143 }
144
145 bool PartSet_WidgetPoint2D::setPoint(double theX, double theY)
146 {
147   if (fabs(theX) >= MaxCoordinate)
148     return false;
149   if (fabs(theY) >= MaxCoordinate)
150     return false;
151
152   ModuleBase_Tools::setSpinValue(myXSpin, theX);
153   ModuleBase_Tools::setSpinValue(myYSpin, theY);
154
155   storeValue();
156   return true;
157 }
158
159 bool PartSet_WidgetPoint2D::storeValueCustom() const
160 {
161   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
162   if (!aData) // can be on abort of sketcher element
163     return false;
164   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
165       aData->attribute(attributeID()));
166   
167   PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
168   bool isBlocked = that->blockSignals(true);
169   bool isImmutable = aPoint->setImmutable(true);
170   
171   if (myXSpin->hasVariable() || myYSpin->hasVariable()) {
172     aPoint->setText(myXSpin->text().toStdString(), myYSpin->text().toStdString());
173   } else {
174     aPoint->setValue(myXSpin->value(), myYSpin->value());
175     aPoint->setText("", "");
176   }
177   // after movement the solver will call the update event: optimization
178   moveObject(myFeature);
179   aPoint->setImmutable(isImmutable);
180   that->blockSignals(isBlocked);
181
182   return true;
183 }
184
185 bool PartSet_WidgetPoint2D::restoreValue()
186 {
187   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
188   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
189       aData->attribute(attributeID()));
190   std::string aTextX = aPoint->textX();
191   std::string aTextY = aPoint->textY();
192   if (aTextX.empty() || aTextY.empty()) {
193     double aX = aPoint->x();
194     ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
195     ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
196   } else {
197     ModuleBase_Tools::setSpinText(myXSpin, QString::fromStdString(aTextX));
198     ModuleBase_Tools::setSpinText(myYSpin, QString::fromStdString(aTextY));
199   }
200   return true;
201 }
202
203 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
204 {
205   QList<QWidget*> aControls;
206   aControls.append(myXSpin);
207   aControls.append(myYSpin);
208   return aControls;
209 }
210
211
212 void PartSet_WidgetPoint2D::activateCustom()
213 {
214   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
215   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
216           this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
217   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
218           this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
219   connect(aViewer, SIGNAL(enterViewPort()), this, SLOT(onLockValidating()));
220   connect(aViewer, SIGNAL(leaveViewPort()), this, SLOT(onUnlockValidating()));
221
222   QIntList aModes;
223   aModes << TopAbs_VERTEX;
224   aModes << TopAbs_EDGE;
225   myWorkshop->moduleConnector()->activateSubShapesSelection(aModes);
226
227   PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
228   if (aModule->isMouseOverWindow())
229     onLockValidating();
230 }
231
232 void PartSet_WidgetPoint2D::deactivate()
233 {
234   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
235   disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
236              this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
237   disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
238              this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
239   disconnect(aViewer, SIGNAL(enterViewPort()), this, SLOT(onLockValidating()));
240   disconnect(aViewer, SIGNAL(leaveViewPort()), this, SLOT(onUnlockValidating()));
241
242   myWorkshop->moduleConnector()->deactivateSubShapesSelection();
243   onUnlockValidating();
244 }
245
246 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView, 
247                                        const TopoDS_Shape& theShape, 
248                                        double& theX, double& theY) const
249 {
250   if (!theShape.IsNull()) {
251     if (theShape.ShapeType() == TopAbs_VERTEX) {
252       const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
253       if (!aVertex.IsNull()) {
254         // A case when point is taken from existing vertex
255         gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
256         PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
257         return true;
258       }
259     }
260   }
261   return false;
262 }
263
264
265 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
266 {
267   // the contex menu release by the right button should not be processed by this widget
268   if (theEvent->button() != Qt::LeftButton)
269     return;
270
271   XGUI_Selection* aSelection = myWorkshop->selector()->selection();
272   Handle(V3d_View) aView = theWnd->v3dView();
273   // TODO: This fragment doesn't work because bug in OCC Viewer. It can be used after fixing.
274   NCollection_List<TopoDS_Shape> aShapes;
275   std::list<ObjectPtr> aObjects;
276   aSelection->selectedShapes(aShapes, aObjects);
277   // if we have selection
278   if (aShapes.Extent() > 0) {
279     TopoDS_Shape aShape = aShapes.First();
280     ObjectPtr aObject = aObjects.front();
281     FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aObject);
282     if (aSelectedFeature.get() != NULL) {
283       std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
284               std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
285       if ((!aSPFeature) && (!aShape.IsNull())) {
286         ResultPtr aFixedObject = PartSet_Tools::findFixedObjectByExternal(aShape, aObject, mySketch);
287         if (!aFixedObject.get())
288           aFixedObject = PartSet_Tools::createFixedObjectByExternal(aShape, aObject, mySketch);
289       }
290     }
291     double aX, aY;
292     if (getPoint2d(aView, aShape, aX, aY)) {
293       setPoint(aX, aY);
294
295       PartSet_Tools::setConstraints(mySketch, feature(), attributeID(),aX, aY);
296       emit vertexSelected();
297       emit focusOutWidget(this);
298       return;
299     } else if (aShape.ShapeType() == TopAbs_EDGE) {
300       // Create point-edge coincedence
301       FeaturePtr aFeature = mySketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
302       std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
303
304       std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
305           ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
306       AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
307       std::shared_ptr<GeomDataAPI_Point2D> aThisPoint = 
308         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
309       aRef1->setAttr(aThisPoint);
310
311       std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
312           ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
313       aRef2->setObject(aObject);
314       aFeature->execute();
315       emit vertexSelected();
316       emit focusOutWidget(this);
317       return;
318     }
319   }
320   // End of Bug dependent fragment
321
322   // A case when point is taken from mouse event
323   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
324   double aX, anY;
325   PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
326   if (!setPoint(aX, anY))
327     return;
328
329   /// Start alternative code
330   //std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
331   //    GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
332   //QList<FeaturePtr> aIgnore;
333   //aIgnore.append(feature());
334
335   //double aTolerance = aView->Convert(7);
336   //std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt = 
337   //  PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
338   //if (aAttrPnt.get() != NULL) {
339   //  aFeaturePoint->setValue(aAttrPnt->pnt());
340   //  PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
341   //  emit vertexSelected();
342   //}
343   /// End alternative code
344   emit focusOutWidget(this);
345 }
346
347
348 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
349 {
350   if (isEditingMode())
351     return;
352
353   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
354
355   double aX, anY;
356   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
357   setPoint(aX, anY);
358 }
359
360 void PartSet_WidgetPoint2D::onLockValidating()
361 {
362   XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
363   anOperationMgr->setLockValidating(true);
364   // the Ok button should be disabled in the property panel by moving the mouse point in the viewer
365   // this leads that the user does not try to click Ok and it avoids an incorrect situation that the 
366   // line is moved to the cursor to the Ok button
367   anOperationMgr->setApplyEnabled(false);
368 }
369
370 void PartSet_WidgetPoint2D::onUnlockValidating()
371 {
372   // it is important to restore the validity state in the property panel after leaving the
373   // view port. Unlock the validating.
374   XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
375   if (anOperationMgr->isValidationLocked()) {
376     anOperationMgr->setLockValidating(false);
377     anOperationMgr->onValidateOperation();
378   }
379 }
380
381 double PartSet_WidgetPoint2D::x() const
382 {
383   return myXSpin->value();
384 }
385
386 double PartSet_WidgetPoint2D::y() const
387 {
388   return myYSpin->value();
389 }
390
391 void PartSet_WidgetPoint2D::onValuesChanged()
392 {
393   myWorkshop->operationMgr()->setLockValidating(false);
394   emit valuesChanged();
395 }