Salome HOME
Merge branch 'vsr/make_test' into Dev_1.5.0
[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 //#define APPLY_BY_ENTER_OR_TAB
27
28 PartSet_WidgetPoint2dDistance::PartSet_WidgetPoint2dDistance(QWidget* theParent,
29                                                              ModuleBase_IWorkshop* theWorkshop,
30                                                              const Config_WidgetAPI* theData,
31                                                              const std::string& theParentId)
32  : ModuleBase_WidgetDoubleValue(theParent, theData, theParentId), myWorkshop(theWorkshop)
33 {
34   myLockApplyMgr = new PartSet_LockApplyMgr(theParent, myWorkshop);
35
36   myFirstPntName = theData->getProperty("first_point");
37
38   // Reconnect to local slot
39 #ifdef APPLY_BY_ENTER_OR_TAB
40   // Apply widget value change by enter/tab event.
41   disconnect(mySpinBox, SIGNAL(editingFinished()), this, SIGNAL(valuesChanged()));
42   connect(mySpinBox, SIGNAL(editingFinished()), this, SLOT(onValuesChanged()));
43 #else
44   disconnect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
45   connect(mySpinBox, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
46 #endif
47 }
48
49 PartSet_WidgetPoint2dDistance::~PartSet_WidgetPoint2dDistance()
50 {
51 }
52
53 void PartSet_WidgetPoint2dDistance::setPoint(FeaturePtr theFeature,
54                                              const std::shared_ptr<GeomAPI_Pnt2d>& thePnt)
55 {
56   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
57   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
58       aData->attribute(myFirstPntName));
59   if (!aPoint)
60     return;
61
62   double aValue = computeValue(aPoint->pnt(), thePnt);
63   AttributeDoublePtr aReal = aData->real(attributeID());
64   if (aReal && (aReal->value() != aValue)) {
65     aReal->setValue(aValue);
66     
67     ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
68     storeValue();
69   }
70 }
71
72 double PartSet_WidgetPoint2dDistance::computeValue(const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPnt,
73                                                    const std::shared_ptr<GeomAPI_Pnt2d>& theCurrentPnt)
74 {
75   return theCurrentPnt->distance(theFirstPnt);
76 }
77
78 void PartSet_WidgetPoint2dDistance::activateCustom()
79 {
80   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
81   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
82           this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
83   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
84           this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
85
86   myLockApplyMgr->activate();
87 }
88
89 void PartSet_WidgetPoint2dDistance::deactivate()
90 {
91   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
92   disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
93              this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
94   disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
95              this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
96
97   myLockApplyMgr->deactivate();
98 }
99
100 void PartSet_WidgetPoint2dDistance::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
101 {
102   // the contex menu release by the right button should not be processed by this widget
103   if (theEvent->button() != Qt::LeftButton)
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   setPoint(feature(), aPnt);
116   emit focusOutWidget(this);
117 }
118
119 void PartSet_WidgetPoint2dDistance::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
120 {
121   if (isEditingMode())
122     return;
123
124   if (mySpinBox->hasVariable())
125     return;
126
127   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
128
129   double aX, aY;
130   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
131
132   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
133   setPoint(feature(), aPnt);
134 }
135
136 void PartSet_WidgetPoint2dDistance::onValuesChanged()
137 {
138   myLockApplyMgr->valuesChanged();
139   emit valuesChanged();
140 }
141