]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_WidgetPoint2dDistance.cpp
Salome HOME
Apply widget value change by enter/tab event.
[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 #include "PartSet_LockApplyMgr.h"
10
11 #include <ModuleBase_ParamSpinBox.h>
12 #include <ModuleBase_IWorkshop.h>
13 #include <ModuleBase_IViewWindow.h>
14 #include <ModuleBase_IViewer.h>
15 #include <ModuleBase_Tools.h>
16
17 #include <GeomAPI_Pnt2d.h>
18 #include <Config_WidgetAPI.h>
19 #include <GeomDataAPI_Point2D.h>
20
21 #include <ModelAPI_Data.h>
22 #include <ModelAPI_AttributeDouble.h>
23
24 #include <QMouseEvent>
25
26 PartSet_WidgetPoint2dDistance::PartSet_WidgetPoint2dDistance(QWidget* theParent,
27                                                              ModuleBase_IWorkshop* theWorkshop,
28                                                              const Config_WidgetAPI* theData,
29                                                              const std::string& theParentId)
30  : ModuleBase_WidgetDoubleValue(theParent, theData, theParentId), myWorkshop(theWorkshop)
31 {
32   myLockApplyMgr = new PartSet_LockApplyMgr(theParent, myWorkshop);
33
34   myFirstPntName = theData->getProperty("first_point");
35
36   // Reconnect to local slot
37   // Apply widget value change by enter/tab event.
38   //disconnect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
39   //connect(mySpinBox, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
40   disconnect(mySpinBox, SIGNAL(editingFinished()), this, SIGNAL(valuesChanged()));
41   connect(mySpinBox, SIGNAL(editingFinished()), this, SLOT(onValuesChanged()));
42 }
43
44 PartSet_WidgetPoint2dDistance::~PartSet_WidgetPoint2dDistance()
45 {
46 }
47
48 void PartSet_WidgetPoint2dDistance::setPoint(FeaturePtr theFeature,
49                                              const std::shared_ptr<GeomAPI_Pnt2d>& thePnt)
50 {
51   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
52   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
53       aData->attribute(myFirstPntName));
54   if (!aPoint)
55     return;
56
57   double aValue = computeValue(aPoint->pnt(), thePnt);
58   AttributeDoublePtr aReal = aData->real(attributeID());
59   if (aReal && (aReal->value() != aValue)) {
60     aReal->setValue(aValue);
61     
62     ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
63     storeValue();
64   }
65 }
66
67 double PartSet_WidgetPoint2dDistance::computeValue(const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPnt,
68                                                    const std::shared_ptr<GeomAPI_Pnt2d>& theCurrentPnt)
69 {
70   return theCurrentPnt->distance(theFirstPnt);
71 }
72
73 void PartSet_WidgetPoint2dDistance::activateCustom()
74 {
75   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
76   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
77           this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
78   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
79           this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
80
81   myLockApplyMgr->activate();
82 }
83
84 void PartSet_WidgetPoint2dDistance::deactivate()
85 {
86   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
87   disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
88              this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
89   disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
90              this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
91
92   myLockApplyMgr->deactivate();
93 }
94
95 void PartSet_WidgetPoint2dDistance::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
96 {
97   // the contex menu release by the right button should not be processed by this widget
98   if (theEvent->button() != Qt::LeftButton)
99     return;
100
101   if (mySpinBox->hasVariable())
102     return;
103
104   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
105
106   double aX, aY;
107   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
108
109   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
110   setPoint(feature(), aPnt);
111   emit focusOutWidget(this);
112 }
113
114 void PartSet_WidgetPoint2dDistance::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
115 {
116   if (isEditingMode())
117     return;
118
119   if (mySpinBox->hasVariable())
120     return;
121
122   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
123
124   double aX, aY;
125   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
126
127   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
128   setPoint(feature(), aPnt);
129 }
130
131 void PartSet_WidgetPoint2dDistance::onValuesChanged()
132 {
133   myLockApplyMgr->valuesChanged();
134   emit valuesChanged();
135 }
136