Salome HOME
3dfefe34962ae3378ab53ef8ed4d66ecc06c70ed
[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 #include "PartSet_LockApplyMgr.h"
10
11 #include <ModuleBase_ParamSpinBox.h>
12 #include <ModuleBase_IWorkshop.h>
13 #include <ModuleBase_IViewWindow.h>
14 #include <ModuleBase_IViewer.h>
15 #include <ModuleBase_Tools.h>
16
17 #include <GeomAPI_Pnt2d.h>
18 #include <Config_WidgetAPI.h>
19 #include <GeomDataAPI_Point2D.h>
20
21 #include <ModelAPI_Data.h>
22 #include <ModelAPI_AttributeDouble.h>
23
24 #include <QMouseEvent>
25
26 PartSet_WidgetPoint2dDistance::PartSet_WidgetPoint2dDistance(QWidget* theParent,
27                                                              ModuleBase_IWorkshop* theWorkshop,
28                                                              const Config_WidgetAPI* theData,
29                                                              const std::string& theParentId)
30  : ModuleBase_WidgetDoubleValue(theParent, theData, theParentId), myWorkshop(theWorkshop)
31 {
32   myLockApplyMgr = new PartSet_LockApplyMgr(theParent, myWorkshop);
33
34   myFirstPntName = theData->getProperty("first_point");
35
36   // Reconnect to local slot
37   disconnect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
38   connect(mySpinBox, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
39 }
40
41 PartSet_WidgetPoint2dDistance::~PartSet_WidgetPoint2dDistance()
42 {
43 }
44
45 // It is not clear a necesity of this method also it contradicts to scenario defined in parent class
46 //void PartSet_WidgetPoint2dDistance::reset()
47 //{
48 //  bool isOk;
49 //  double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
50 //
51 //  ModuleBase_Tools::setSpinValue(mySpinBox, isOk ? aDefValue : 0.0);
52 //  storeValueCustom();
53 //}
54
55 void PartSet_WidgetPoint2dDistance::setPoint(FeaturePtr theFeature,
56                                              const std::shared_ptr<GeomAPI_Pnt2d>& thePnt)
57 {
58   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
59   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
60       aData->attribute(myFirstPntName));
61   if (!aPoint)
62     return;
63
64   double aValue = computeValue(aPoint->pnt(), thePnt);
65   AttributeDoublePtr aReal = aData->real(attributeID());
66   if (aReal && (aReal->value() != aValue)) {
67     aReal->setValue(aValue);
68     
69     ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
70     storeValue();
71   }
72 }
73
74 double PartSet_WidgetPoint2dDistance::computeValue(const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPnt,
75                                                    const std::shared_ptr<GeomAPI_Pnt2d>& theCurrentPnt)
76 {
77   return theCurrentPnt->distance(theFirstPnt);
78 }
79
80 void PartSet_WidgetPoint2dDistance::activateCustom()
81 {
82   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
83   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
84           this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
85   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
86           this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
87
88   myLockApplyMgr->activate();
89 }
90
91 void PartSet_WidgetPoint2dDistance::deactivate()
92 {
93   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
94   disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
95              this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
96   disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
97              this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
98
99   myLockApplyMgr->deactivate();
100 }
101
102 void PartSet_WidgetPoint2dDistance::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
103 {
104   // the contex menu release by the right button should not be processed by this widget
105   if (theEvent->button() != Qt::LeftButton)
106     return;
107
108   if (mySpinBox->hasVariable())
109     return;
110
111   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
112
113   double aX, aY;
114   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
115
116   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
117   setPoint(feature(), aPnt);
118   emit focusOutWidget(this);
119 }
120
121 void PartSet_WidgetPoint2dDistance::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
122 {
123   if (isEditingMode())
124     return;
125
126   if (mySpinBox->hasVariable())
127     return;
128
129   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
130
131   double aX, aY;
132   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
133
134   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
135   setPoint(feature(), aPnt);
136 }
137
138 void PartSet_WidgetPoint2dDistance::onValuesChanged()
139 {
140   myLockApplyMgr->valuesChanged();
141   emit valuesChanged();
142 }
143