Salome HOME
d3f73af250ef9d7a9f9fd420a80df89e1e0876ef
[modules/shaper.git] / src / PartSet / PartSet_WidgetPoint2d.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_WidgetPoint2D.cpp
4 // Created:     25 Apr 2014
5 // Author:      Natalia ERMOLAEVA
6
7 #include "PartSet_WidgetPoint2d.h"
8 #include <PartSet_Tools.h>
9
10 #include <XGUI_Workshop.h>
11 #include <XGUI_ViewerProxy.h>
12 #include <XGUI_ModuleConnector.h>
13 #include <XGUI_SelectionMgr.h>
14 #include <XGUI_Selection.h>
15
16 #include <ModuleBase_DoubleSpinBox.h>
17 #include <ModuleBase_Tools.h>
18 #include <ModuleBase_IViewWindow.h>
19
20 #include <Config_Keywords.h>
21 #include <Config_WidgetAPI.h>
22
23 #include <Events_Loop.h>
24 #include <ModelAPI_Events.h>
25
26 #include <ModelAPI_Feature.h>
27 #include <ModelAPI_Data.h>
28 #include <ModelAPI_Object.h>
29 #include <GeomDataAPI_Point2D.h>
30 #include <GeomAPI_Pnt2d.h>
31
32 #include <QGroupBox>
33 #include <QGridLayout>
34 #include <QLabel>
35 #include <QEvent>
36 #include <QMouseEvent>
37 #include <QApplication>
38
39 #include <TopoDS.hxx>
40 #include <TopoDS_Vertex.hxx>
41 #include <BRep_Tool.hxx>
42
43 #include <cfloat>
44 #include <climits>
45
46 PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent, 
47                                               const Config_WidgetAPI* theData,
48                                               const std::string& theParentId)
49     : ModuleBase_ModelWidget(theParent, theData, theParentId)
50 {
51   //myOptionParam = theData->getProperty(PREVIOUS_FEATURE_PARAM);
52   QString aPageName = QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME));
53   myGroupBox = new QGroupBox(aPageName, theParent);
54   myGroupBox->setFlat(false);
55
56   QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
57   ModuleBase_Tools::adjustMargins(aGroupLay);
58   aGroupLay->setColumnStretch(1, 1);
59   {
60     QLabel* aLabel = new QLabel(myGroupBox);
61     aLabel->setText("X");
62     aLabel->setPixmap(QPixmap(":pictures/x_point.png"));
63     aGroupLay->addWidget(aLabel, 0, 0);
64
65     myXSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
66     myXSpin->setMinimum(-DBL_MAX);
67     myXSpin->setMaximum(DBL_MAX);
68     myXSpin->setToolTip("X");
69     aGroupLay->addWidget(myXSpin, 0, 1);
70
71     connect(myXSpin, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
72   }
73   {
74     QLabel* aLabel = new QLabel(myGroupBox);
75     aLabel->setText("Y");
76     aLabel->setPixmap(QPixmap(":pictures/y_point.png"));
77     aGroupLay->addWidget(aLabel, 1, 0);
78
79     myYSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
80     myYSpin->setMinimum(-DBL_MAX);
81     myYSpin->setMaximum(DBL_MAX);
82     myYSpin->setToolTip("X");
83     aGroupLay->addWidget(myYSpin, 1, 1);
84
85     connect(myYSpin, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
86   }
87 }
88
89 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
90 {
91 }
92
93 bool PartSet_WidgetPoint2D::setSelection(ModuleBase_ViewerPrs theValue)
94 {
95   Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
96   bool isDone = false;
97   TopoDS_Shape aShape = theValue.shape();
98   double aX, aY;
99   if (getPoint2d(aView, aShape, aX, aY)) {
100     setPoint(aX, aY);
101     isDone = true;
102   }
103   return isDone;
104 }
105
106 void PartSet_WidgetPoint2D::setPoint(double theX, double theY)
107 {
108
109   bool isBlocked = this->blockSignals(true);
110   myXSpin->setValue(theX);
111   myYSpin->setValue(theY);
112   this->blockSignals(isBlocked);
113
114   emit valuesChanged();
115 }
116
117 bool PartSet_WidgetPoint2D::storeValue() const
118 {
119   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
120   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
121       aData->attribute(attributeID()));
122   
123   PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
124   bool isBlocked = that->blockSignals(true);
125   bool isImmutable = aPoint->setImmutable(true);
126 #ifdef _DEBUG
127   std::string _attr_name = myAttributeID;
128   double _X = myXSpin->value();
129   double _Y = myYSpin->value();
130 #endif
131   aPoint->setValue(myXSpin->value(), myYSpin->value());
132   updateObject(myFeature);
133   aPoint->setImmutable(isImmutable);
134   that->blockSignals(isBlocked);
135
136   return true;
137 }
138
139 bool PartSet_WidgetPoint2D::restoreValue()
140 {
141   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
142   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
143       aData->attribute(attributeID()));
144
145 #ifdef _DEBUG
146   std::string _attr_name = myAttributeID;
147   double _X = aPoint->x();
148   double _Y = aPoint->y();
149 #endif
150   bool isBlocked = this->blockSignals(true);
151   myXSpin->setValue(aPoint->x());
152   myYSpin->setValue(aPoint->y());
153   this->blockSignals(isBlocked);
154   return true;
155 }
156
157 QWidget* PartSet_WidgetPoint2D::getControl() const
158 {
159   return myGroupBox;
160 }
161
162 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
163 {
164   QList<QWidget*> aControls;
165   aControls.append(myXSpin);
166   aControls.append(myYSpin);
167   return aControls;
168 }
169
170 //bool PartSet_WidgetPoint2D::initFromPrevious(ObjectPtr theObject)
171 //{
172 //  if (myOptionParam.length() == 0)
173 //    return false;
174 //  std::shared_ptr<ModelAPI_Data> aData = theObject->data();
175 //  std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
176 //      aData->attribute(myOptionParam));
177 //  if (aPoint) {
178 //    bool isBlocked = this->blockSignals(true);
179 //    myXSpin->setValue(aPoint->x());
180 //    myYSpin->setValue(aPoint->y());
181 //    this->blockSignals(isBlocked);
182 //
183 //    emit valuesChanged();
184 //    emit storedPoint2D(theObject, myOptionParam);
185 //    return true;
186 //  }
187 //  return false;
188 //}
189
190 void PartSet_WidgetPoint2D::activate()
191 {
192   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
193   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
194           this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
195   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
196           this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
197
198   QIntList aModes;
199   aModes << TopAbs_VERTEX;
200   myWorkshop->moduleConnector()->activateSubShapesSelection(aModes);
201 }
202
203 void PartSet_WidgetPoint2D::deactivate()
204 {
205   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
206   disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
207              this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
208   disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
209              this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
210   myWorkshop->moduleConnector()->deactivateSubShapesSelection();
211 }
212
213 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView, 
214                                        const TopoDS_Shape& theShape, 
215                                        double& theX, double& theY) const
216 {
217   if (!theShape.IsNull()) {
218     if (theShape.ShapeType() == TopAbs_VERTEX) {
219       const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
220       if (!aVertex.IsNull()) {
221         // A case when point is taken from existing vertex
222         gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
223         PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
224         return true;
225       }
226     }
227   }
228   return false;
229 }
230
231
232 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
233 {
234   XGUI_Selection* aSelection = myWorkshop->selector()->selection();
235   NCollection_List<TopoDS_Shape> aShapes;
236   std::list<ObjectPtr> aObjects;
237   aSelection->selectedShapes(aShapes, aObjects);
238   if (aShapes.Extent() > 0) {
239     TopoDS_Shape aShape = aShapes.First();
240     double aX, aY;
241     if (getPoint2d(theWnd->v3dView(), aShape, aX, aY)) {
242       setPoint(aX, aY);
243
244       PartSet_Tools::setConstraints(mySketch, feature(), attributeID(),aX, aY);
245       emit vertexSelected(aObjects.front(), aShape);
246       emit focusOutWidget(this);
247       return;
248     }
249   }
250   // A case when point is taken from mouse event
251   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
252   double aX, anY;
253   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
254   setPoint(aX, anY);
255
256   emit focusOutWidget(this);
257 }
258
259
260 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
261 {
262   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
263
264   double aX, anY;
265   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
266   setPoint(aX, anY);
267 }
268
269 double PartSet_WidgetPoint2D::x() const
270 {
271   return myXSpin->value();
272 }
273
274 double PartSet_WidgetPoint2D::y() const
275 {
276   return myYSpin->value();
277 }
278