Salome HOME
756e9a8a91fb38bc694a42bfcd828b455df38830
[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 #include <ModuleBase_Tools.h>
13
14 #include <XGUI_ViewerProxy.h>
15 #include <XGUI_Workshop.h>
16 #include <XGUI_PropertyPanel.h>
17 #include <XGUI_OperationMgr.h>
18
19 #include <GeomAPI_Pnt2d.h>
20 #include <Config_WidgetAPI.h>
21 #include <GeomDataAPI_Point2D.h>
22
23 #include <ModelAPI_Data.h>
24 #include <ModelAPI_AttributeDouble.h>
25
26 #include <QMouseEvent>
27
28 PartSet_WidgetPoint2dDistance::PartSet_WidgetPoint2dDistance(QWidget* theParent,
29                                                                    const Config_WidgetAPI* theData,
30                                                                    const std::string& theParentId)
31     : ModuleBase_WidgetDoubleValue(theParent, theData, theParentId)
32 {
33   myFirstPntName = theData->getProperty("first_point");
34
35   // Reconnect to local slot
36   disconnect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
37   connect(mySpinBox, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
38 }
39
40 PartSet_WidgetPoint2dDistance::~PartSet_WidgetPoint2dDistance()
41 {
42 }
43
44 void PartSet_WidgetPoint2dDistance::reset()
45 {
46   bool isOk;
47   double aDefValue = QString::fromStdString(myDefaultValue).toDouble(&isOk);
48
49   ModuleBase_Tools::setSpinValue(mySpinBox, isOk ? aDefValue : 0.0);
50 }
51
52 void PartSet_WidgetPoint2dDistance::setPoint(FeaturePtr theFeature,
53                                              const std::shared_ptr<GeomAPI_Pnt2d>& thePnt)
54 {
55   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
56   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
57       aData->attribute(myFirstPntName));
58   if (!aPoint)
59     return;
60
61   double aRadius = thePnt->distance(aPoint->pnt());
62   AttributeDoublePtr aReal = aData->real(attributeID());
63   if (aReal && (aReal->value() != aRadius)) {
64     aReal->setValue(aRadius);
65     
66     ModuleBase_Tools::setSpinValue(mySpinBox, aRadius);
67     storeValue();
68   }
69 }
70
71 void PartSet_WidgetPoint2dDistance::activateCustom()
72 {
73   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
74   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
75           this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
76   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
77           this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
78 }
79
80 void PartSet_WidgetPoint2dDistance::deactivate()
81 {
82   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
83   disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
84              this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
85   disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
86              this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
87   myWorkshop->operationMgr()->setLockValidating(false);
88 }
89
90 void PartSet_WidgetPoint2dDistance::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
91 {
92   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
93
94   double aX, aY;
95   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
96
97   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
98   setPoint(feature(), aPnt);
99   emit focusOutWidget(this);
100 }
101
102 void PartSet_WidgetPoint2dDistance::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
103 {
104   myWorkshop->operationMgr()->setLockValidating(true);
105   myWorkshop->operationMgr()->setApplyEnabled(false);
106
107   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
108
109   double aX, aY;
110   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
111
112   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
113   setPoint(feature(), aPnt);
114 }
115
116 void PartSet_WidgetPoint2dDistance::onValuesChanged()
117 {
118   myWorkshop->operationMgr()->setLockValidating(false);
119   emit valuesChanged();
120 }
121