Salome HOME
b32ba144362f34571c5c8f14cbc66c950ecb06aa
[modules/shaper.git] / src / PartSet / PartSet_WidgetPoint2dDistance.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_WidgetPoint2dDistance.h
4 // Created:     23 June 2014
5 // Author:      Vitaly Smetannikov
6
7 #include "PartSet_WidgetPoint2dDistance.h"
8 #include "PartSet_Tools.h"
9
10 #include <ModuleBase_DoubleSpinBox.h>
11 #include <ModuleBase_IViewWindow.h>
12
13 #include <XGUI_ViewerProxy.h>
14 #include <XGUI_Workshop.h>
15 #include <XGUI_PropertyPanel.h>
16 #include <XGUI_OperationMgr.h>
17
18 #include <GeomAPI_Pnt2d.h>
19 #include <Config_WidgetAPI.h>
20 #include <GeomDataAPI_Point2D.h>
21
22 #include <ModelAPI_Data.h>
23 #include <ModelAPI_AttributeDouble.h>
24
25 #include <QMouseEvent>
26
27 PartSet_WidgetPoint2dDistance::PartSet_WidgetPoint2dDistance(QWidget* theParent,
28                                                                    const Config_WidgetAPI* theData,
29                                                                    const std::string& theParentId)
30     : ModuleBase_WidgetDoubleValue(theParent, theData, theParentId)
31 {
32   myFirstPntName = theData->getProperty("first_point");
33
34   // Reconnect to local slot
35   disconnect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
36   connect(mySpinBox, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
37   connect(mySpinBox, SIGNAL(valueChanged(double)), this, SLOT(controlValuesChanged()));
38 }
39
40 PartSet_WidgetPoint2dDistance::~PartSet_WidgetPoint2dDistance()
41 {
42 }
43
44 void PartSet_WidgetPoint2dDistance::setPoint(FeaturePtr theFeature,
45                                              const std::shared_ptr<GeomAPI_Pnt2d>& thePnt)
46 {
47   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
48   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
49       aData->attribute(myFirstPntName));
50   if (!aPoint)
51     return;
52
53   double aRadius = thePnt->distance(aPoint->pnt());
54   AttributeDoublePtr aReal = aData->real(attributeID());
55   if (aReal && (aReal->value() != aRadius)) {
56     aReal->setValue(aRadius);
57     mySpinBox->blockSignals(true);
58     mySpinBox->setValue(aRadius);
59     mySpinBox->blockSignals(false);
60     emit valuesChanged();
61   }
62 }
63
64 void PartSet_WidgetPoint2dDistance::activateCustom()
65 {
66   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
67   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
68           this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
69   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
70           this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
71 }
72
73 void PartSet_WidgetPoint2dDistance::deactivate()
74 {
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   myWorkshop->operationMgr()->setLockValidating(false);
81 }
82
83 void PartSet_WidgetPoint2dDistance::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
84 {
85   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
86
87   double aX, aY;
88   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
89
90   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
91   setPoint(feature(), aPnt);
92   emit focusOutWidget(this);
93 }
94
95 void PartSet_WidgetPoint2dDistance::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
96 {
97   myWorkshop->operationMgr()->setLockValidating(true);
98   myWorkshop->operationMgr()->setApplyEnabled(false);
99
100   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
101
102   double aX, aY;
103   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
104
105   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
106   setPoint(feature(), aPnt);
107 }
108
109 void PartSet_WidgetPoint2dDistance::onValuesChanged()
110 {
111   myWorkshop->operationMgr()->setLockValidating(false);
112   emit valuesChanged();
113 }
114