Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[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_ParamSpinBox.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 // It is not clear a necesity of this method also it contradicts to scenario defined in parent class
45 //void PartSet_WidgetPoint2dDistance::reset()
46 //{
47 //  bool isOk;
48 //  double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
49 //
50 //  ModuleBase_Tools::setSpinValue(mySpinBox, isOk ? aDefValue : 0.0);
51 //  storeValueCustom();
52 //}
53
54 void PartSet_WidgetPoint2dDistance::setPoint(FeaturePtr theFeature,
55                                              const std::shared_ptr<GeomAPI_Pnt2d>& thePnt)
56 {
57   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
58   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
59       aData->attribute(myFirstPntName));
60   if (!aPoint)
61     return;
62
63   double aRadius = thePnt->distance(aPoint->pnt());
64   AttributeDoublePtr aReal = aData->real(attributeID());
65   if (aReal && (aReal->value() != aRadius)) {
66     aReal->setValue(aRadius);
67     
68     ModuleBase_Tools::setSpinValue(mySpinBox, aRadius);
69     storeValue();
70   }
71 }
72
73 void PartSet_WidgetPoint2dDistance::activateCustom()
74 {
75   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
76   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
77           this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
78   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
79           this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
80 }
81
82 void PartSet_WidgetPoint2dDistance::deactivate()
83 {
84   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
85   disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
86              this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
87   disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
88              this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
89   myWorkshop->operationMgr()->setLockValidating(false);
90 }
91
92 void PartSet_WidgetPoint2dDistance::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
93 {
94   // the contex menu release by the right button should not be processed by this widget
95   if (theEvent->button() != Qt::LeftButton)
96     return;
97
98   if (mySpinBox->hasVariable())
99     return;
100
101   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
102
103   double aX, aY;
104   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
105
106   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
107   setPoint(feature(), aPnt);
108   emit focusOutWidget(this);
109 }
110
111 void PartSet_WidgetPoint2dDistance::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
112 {
113   if (mySpinBox->hasVariable())
114     return;
115
116   myWorkshop->operationMgr()->setLockValidating(true);
117   myWorkshop->operationMgr()->setApplyEnabled(false);
118
119   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
120
121   double aX, aY;
122   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
123
124   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
125   setPoint(feature(), aPnt);
126 }
127
128 void PartSet_WidgetPoint2dDistance::onValuesChanged()
129 {
130   myWorkshop->operationMgr()->setLockValidating(false);
131   emit valuesChanged();
132 }
133