]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_WidgetPoint2dDistance.cpp
Salome HOME
A correction to check modified value in extrusion cut/sketch/etc operations.
[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   emit focusOutWidget(this);
99 }
100
101 void PartSet_WidgetPoint2dDistance::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
102 {
103   if (isEditingMode())
104     return;
105
106   if (mySpinBox->hasVariable())
107     return;
108
109   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
110
111   double aX, aY;
112   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
113
114   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
115   bool isBlocked = blockValueState(true);
116   setPoint(feature(), aPnt);
117   blockValueState(isBlocked);
118   setValueState(ModifiedInViewer);
119 }
120
121 bool PartSet_WidgetPoint2dDistance::processEnter()
122 {
123   bool isModified = mySpinBox->isModified();
124   if (isModified) {
125     emit valuesChanged();
126     mySpinBox->clearModified();
127     mySpinBox->selectAll();
128   }
129   return isModified;
130 }