Salome HOME
Whole column button with accept/abort actions is implemented
[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 }
38
39 PartSet_WidgetPoint2dDistance::~PartSet_WidgetPoint2dDistance()
40 {
41 }
42
43 void PartSet_WidgetPoint2dDistance::setPoint(FeaturePtr theFeature,
44                                              const std::shared_ptr<GeomAPI_Pnt2d>& thePnt)
45 {
46   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
47   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
48       aData->attribute(myFirstPntName));
49   if (!aPoint)
50     return;
51
52   double aRadius = thePnt->distance(aPoint->pnt());
53   AttributeDoublePtr aReal = aData->real(attributeID());
54   if (aReal && (aReal->value() != aRadius)) {
55     aReal->setValue(aRadius);
56     mySpinBox->blockSignals(true);
57     mySpinBox->setValue(aRadius);
58     mySpinBox->blockSignals(false);
59     emit valuesChanged();
60   }
61 }
62
63 void PartSet_WidgetPoint2dDistance::activateCustom()
64 {
65   XGUI_ViewerProxy* 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_IViewer* aViewer = myWorkshop->viewer();
75   disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
76              this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
77   disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
78              this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
79   myWorkshop->operationMgr()->setLockValidating(false);
80 }
81
82 void PartSet_WidgetPoint2dDistance::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
83 {
84   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
85
86   double aX, aY;
87   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
88
89   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
90   setPoint(feature(), aPnt);
91   emit focusOutWidget(this);
92 }
93
94 void PartSet_WidgetPoint2dDistance::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
95 {
96   myWorkshop->operationMgr()->setLockValidating(true);
97   myWorkshop->operationMgr()->setApplyEnabled(false);
98
99   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
100
101   double aX, aY;
102   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
103
104   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
105   setPoint(feature(), aPnt);
106 }
107
108 void PartSet_WidgetPoint2dDistance::onValuesChanged()
109 {
110   myWorkshop->operationMgr()->setLockValidating(false);
111   emit valuesChanged();
112 }
113