Salome HOME
d2f1a08aaef00d9598a035cdfed552b6f627159a
[modules/shaper.git] / src / PartSet / PartSet_WidgetPoint2dDistance.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_WidgetPoint2dDistance.cpp
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_IWorkshop.h>
12 #include <ModuleBase_IViewWindow.h>
13 #include <ModuleBase_IViewer.h>
14 #include <ModuleBase_Tools.h>
15
16 #include <GeomAPI_Pnt2d.h>
17 #include <Config_WidgetAPI.h>
18 #include <GeomDataAPI_Point2D.h>
19
20 #include <ModelAPI_Data.h>
21 #include <ModelAPI_AttributeDouble.h>
22
23 #include <QMouseEvent>
24
25 #define APPLY_BY_ENTER_OR_TAB
26
27 PartSet_WidgetPoint2dDistance::PartSet_WidgetPoint2dDistance(QWidget* theParent,
28                                                              ModuleBase_IWorkshop* theWorkshop,
29                                                              const Config_WidgetAPI* theData,
30                                                              const std::string& theParentId)
31  : ModuleBase_WidgetDoubleValue(theParent, theData, theParentId), myWorkshop(theWorkshop)
32 {
33   myFirstPntName = theData->getProperty("first_point");
34
35   // Reconnect to local slot
36 #ifdef APPLY_BY_ENTER_OR_TAB
37   // Apply widget value change by enter/tab event.
38   //disconnect(mySpinBox, SIGNAL(editingFinished()), this, SIGNAL(valuesChanged()));
39   disconnect(mySpinBox, SIGNAL(valueStored()), this, SIGNAL(valuesChanged()));
40   connect(mySpinBox, SIGNAL(editingFinished()), this, SLOT(onValuesChanged()));
41   connect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(valuesModified()));
42 #else
43   disconnect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
44   connect(mySpinBox, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
45 #endif
46 }
47
48 PartSet_WidgetPoint2dDistance::~PartSet_WidgetPoint2dDistance()
49 {
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 aValue = computeValue(aPoint->pnt(), thePnt);
62   AttributeDoublePtr aReal = aData->real(attributeID());
63   if (aReal && (aReal->value() != aValue)) {
64     aReal->setValue(aValue);
65     
66     ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
67     storeValue();
68   }
69 }
70
71 double PartSet_WidgetPoint2dDistance::computeValue(const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPnt,
72                                                    const std::shared_ptr<GeomAPI_Pnt2d>& theCurrentPnt)
73 {
74   return theCurrentPnt->distance(theFirstPnt);
75 }
76
77 void PartSet_WidgetPoint2dDistance::activateCustom()
78 {
79   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
80   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
81           this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
82   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
83           this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
84 }
85
86 void PartSet_WidgetPoint2dDistance::deactivate()
87 {
88   ModuleBase_ModelWidget::deactivate();
89   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
90   disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
91              this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
92   disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
93              this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
94 }
95
96 void PartSet_WidgetPoint2dDistance::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
97 {
98   // the contex menu release by the right button should not be processed by this widget
99   if (theEvent->button() != Qt::LeftButton)
100     return;
101
102   if (mySpinBox->hasVariable())
103     return;
104
105   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
106
107   double aX, aY;
108   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
109
110   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
111   setPoint(feature(), aPnt);
112   emit focusOutWidget(this);
113 }
114
115 void PartSet_WidgetPoint2dDistance::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
116 {
117   if (isEditingMode())
118     return;
119
120   if (mySpinBox->hasVariable())
121     return;
122
123   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
124
125   double aX, aY;
126   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
127
128   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
129   bool isBlocked = blockValueState(true);
130   setPoint(feature(), aPnt);
131   blockValueState(isBlocked);
132   setValueState(ModifiedInViewer);
133 }
134
135 void PartSet_WidgetPoint2dDistance::onValuesChanged()
136 {
137   emit valuesChanged();
138 }
139
140 bool PartSet_WidgetPoint2dDistance::processEnter()
141 {
142   bool isModified = mySpinBox->isModified();
143   if (isModified) {
144     emit valuesChanged();
145     mySpinBox->clearModified();
146     mySpinBox->selectAll();
147   }
148   return isModified;
149 }