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