Salome HOME
086133dedeb2dd46cc0f551861c0b03ab317eaa6
[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::reset()
44 {
45   bool isOk;
46   double aDefValue = QString::fromStdString(myDefaultValue).toDouble(&isOk);
47   mySpinBox->setValue(isOk ? aDefValue : 0.0);
48 }
49
50 void PartSet_WidgetPoint2dDistance::setPoint(FeaturePtr theFeature,
51                                              const std::shared_ptr<GeomAPI_Pnt2d>& thePnt)
52 {
53   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
54   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
55       aData->attribute(myFirstPntName));
56   if (!aPoint)
57     return;
58
59   double aRadius = thePnt->distance(aPoint->pnt());
60   AttributeDoublePtr aReal = aData->real(attributeID());
61   if (aReal && (aReal->value() != aRadius)) {
62     aReal->setValue(aRadius);
63     mySpinBox->blockSignals(true);
64     mySpinBox->setValue(aRadius);
65     mySpinBox->blockSignals(false);
66     storeValue();
67   }
68 }
69
70 void PartSet_WidgetPoint2dDistance::activateCustom()
71 {
72   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
73   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
74           this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
75   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
76           this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
77 }
78
79 void PartSet_WidgetPoint2dDistance::deactivate()
80 {
81   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
82   disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
83              this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
84   disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
85              this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
86   myWorkshop->operationMgr()->setLockValidating(false);
87 }
88
89 void PartSet_WidgetPoint2dDistance::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
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   myWorkshop->operationMgr()->setLockValidating(true);
104   myWorkshop->operationMgr()->setApplyEnabled(false);
105
106   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
107
108   double aX, aY;
109   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
110
111   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
112   setPoint(feature(), aPnt);
113 }
114
115 void PartSet_WidgetPoint2dDistance::onValuesChanged()
116 {
117   myWorkshop->operationMgr()->setLockValidating(false);
118   emit valuesChanged();
119 }
120