Salome HOME
"2.11 Constraint with a point from the intersection between an outer edge and plane...
[modules/shaper.git] / src / PartSet / PartSet_WidgetPoint2dDistance.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_WidgetPoint2dDistance.cpp
4 // Created:     23 June 2014
5 // Author:      Vitaly Smetannikov
6
7 #include "PartSet_WidgetPoint2dDistance.h"
8 #include "PartSet_Tools.h"
9
10 #include <ModuleBase_ParamSpinBox.h>
11 #include <ModuleBase_IWorkshop.h>
12 #include <ModuleBase_IViewWindow.h>
13 #include <ModuleBase_IViewer.h>
14 #include <ModuleBase_Tools.h>
15
16 #include <GeomAPI_Pnt2d.h>
17 #include <Config_WidgetAPI.h>
18 #include <GeomDataAPI_Point2D.h>
19
20 #include <ModelAPI_Data.h>
21 #include <ModelAPI_AttributeDouble.h>
22
23 #include <QMouseEvent>
24
25 PartSet_WidgetPoint2dDistance::PartSet_WidgetPoint2dDistance(QWidget* theParent,
26                                                              ModuleBase_IWorkshop* theWorkshop,
27                                                              const Config_WidgetAPI* theData,
28                                                              const std::string& theParentId)
29  : ModuleBase_WidgetDoubleValue(theParent, theData, theParentId), myWorkshop(theWorkshop)
30 {
31   myFirstPntName = theData->getProperty("first_point");
32 }
33
34 PartSet_WidgetPoint2dDistance::~PartSet_WidgetPoint2dDistance()
35 {
36 }
37
38 void PartSet_WidgetPoint2dDistance::setPoint(FeaturePtr theFeature,
39                                              const std::shared_ptr<GeomAPI_Pnt2d>& thePnt)
40 {
41   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
42   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
43       aData->attribute(myFirstPntName));
44   if (!aPoint)
45     return;
46
47   double aValue = computeValue(aPoint->pnt(), thePnt);
48   AttributeDoublePtr aReal = aData->real(attributeID());
49   if (aReal && (aReal->value() != aValue)) {
50     aReal->setValue(aValue);
51     
52     ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
53     storeValue();
54   }
55 }
56
57 double PartSet_WidgetPoint2dDistance::computeValue(const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPnt,
58                                                    const std::shared_ptr<GeomAPI_Pnt2d>& theCurrentPnt)
59 {
60   return theCurrentPnt->distance(theFirstPnt);
61 }
62
63 void PartSet_WidgetPoint2dDistance::activateCustom()
64 {
65   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
66   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
67           this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
68   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
69           this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
70 }
71
72 void PartSet_WidgetPoint2dDistance::deactivate()
73 {
74   ModuleBase_ModelWidget::deactivate();
75   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
76   disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
77              this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
78   disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
79              this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
80 }
81
82 void PartSet_WidgetPoint2dDistance::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
83 {
84   // the contex menu release by the right button should not be processed by this widget
85   if (theEvent->button() != Qt::LeftButton)
86     return;
87
88   if (mySpinBox->hasVariable())
89     return;
90
91   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
92
93   double aX, aY;
94   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
95
96   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
97   setPoint(feature(), aPnt);
98
99   // if the validator of the control returns false, focus should not be switched
100   if (getError().isEmpty())
101     emit focusOutWidget(this);
102 }
103
104 void PartSet_WidgetPoint2dDistance::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
105 {
106   if (isEditingMode())
107     return;
108
109   if (mySpinBox->hasVariable())
110     return;
111
112   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
113
114   double aX, aY;
115   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
116
117   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
118   bool isBlocked = blockValueState(true);
119   setPoint(feature(), aPnt);
120   blockValueState(isBlocked);
121   setValueState(ModifiedInViewer);
122 }
123
124 bool PartSet_WidgetPoint2dDistance::processEnter()
125 {
126   bool isModified = getValueState() == ModifiedInPP;
127   if (isModified) {
128     emit valuesChanged();
129     mySpinBox->selectAll();
130   }
131   return isModified;
132 }