]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_WidgetPoint2d.cpp
Salome HOME
Merge branch 'Dev_0.6' of newgeom:newgeom into Dev_0.6
[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_WidgetValueFeature.h>
15 #include <ModuleBase_DoubleSpinBox.h>
16 #include <ModuleBase_Tools.h>
17 #include <ModuleBase_IViewWindow.h>
18
19 #include <Config_Keywords.h>
20 #include <Config_WidgetAPI.h>
21
22 #include <Events_Loop.h>
23 #include <ModelAPI_Events.h>
24
25 #include <ModelAPI_Feature.h>
26 #include <ModelAPI_Data.h>
27 #include <ModelAPI_Object.h>
28 #include <GeomDataAPI_Point2D.h>
29 #include <GeomAPI_Pnt2d.h>
30
31 #include <QGroupBox>
32 #include <QGridLayout>
33 #include <QLabel>
34 #include <QEvent>
35 #include <QMouseEvent>
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::setValue(ModuleBase_WidgetValue* theValue)
92 {
93   bool isDone = false;
94   if (theValue) {
95     ModuleBase_WidgetValueFeature* aFeatureValue =
96         dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
97     if (aFeatureValue) {
98       std::shared_ptr<GeomAPI_Pnt2d> aPoint = aFeatureValue->point();
99       if (aPoint) {
100         setPoint(aPoint->x(), aPoint->y());
101         isDone = true;
102       }
103     }
104   }
105   return isDone;
106 }
107
108 void PartSet_WidgetPoint2D::setPoint(double theX, double theY)
109 {
110
111   bool isBlocked = this->blockSignals(true);
112   myXSpin->setValue(theX);
113   myYSpin->setValue(theY);
114   this->blockSignals(isBlocked);
115
116   emit valuesChanged();
117 }
118
119 bool PartSet_WidgetPoint2D::storeValue() const
120 {
121   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
122   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
123       aData->attribute(attributeID()));
124   
125   PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
126   bool isBlocked = that->blockSignals(true);
127   bool isImmutable = aPoint->setImmutable(true);
128 #ifdef _DEBUG
129   std::string _attr_name = myAttributeID;
130   double _X = myXSpin->value();
131   double _Y = myYSpin->value();
132 #endif
133   aPoint->setValue(myXSpin->value(), myYSpin->value());
134   updateObject(myFeature);
135   aPoint->setImmutable(isImmutable);
136   that->blockSignals(isBlocked);
137
138   return true;
139 }
140
141 bool PartSet_WidgetPoint2D::restoreValue()
142 {
143   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
144   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
145       aData->attribute(attributeID()));
146
147 #ifdef _DEBUG
148   std::string _attr_name = myAttributeID;
149   double _X = aPoint->x();
150   double _Y = aPoint->y();
151 #endif
152   bool isBlocked = this->blockSignals(true);
153   myXSpin->setValue(aPoint->x());
154   myYSpin->setValue(aPoint->y());
155   this->blockSignals(isBlocked);
156   return true;
157 }
158
159 QWidget* PartSet_WidgetPoint2D::getControl() const
160 {
161   return myGroupBox;
162 }
163
164 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
165 {
166   QList<QWidget*> aControls;
167   aControls.append(myXSpin);
168   aControls.append(myYSpin);
169   return aControls;
170 }
171
172 //bool PartSet_WidgetPoint2D::initFromPrevious(ObjectPtr theObject)
173 //{
174 //  if (myOptionParam.length() == 0)
175 //    return false;
176 //  std::shared_ptr<ModelAPI_Data> aData = theObject->data();
177 //  std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
178 //      aData->attribute(myOptionParam));
179 //  if (aPoint) {
180 //    bool isBlocked = this->blockSignals(true);
181 //    myXSpin->setValue(aPoint->x());
182 //    myYSpin->setValue(aPoint->y());
183 //    this->blockSignals(isBlocked);
184 //
185 //    emit valuesChanged();
186 //    emit storedPoint2D(theObject, myOptionParam);
187 //    return true;
188 //  }
189 //  return false;
190 //}
191
192 void PartSet_WidgetPoint2D::activate()
193 {
194   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
195   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
196           this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
197   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
198           this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
199
200   QIntList aModes;
201   aModes << TopAbs_VERTEX;
202   myWorkshop->moduleConnector()->activateSubShapesSelection(aModes);
203 }
204
205 void PartSet_WidgetPoint2D::deactivate()
206 {
207   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
208   disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
209              this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
210   disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
211              this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
212   myWorkshop->moduleConnector()->deactivateSubShapesSelection();
213 }
214
215
216 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
217 {
218   XGUI_Selection* aSelection = myWorkshop->selector()->selection();
219   NCollection_List<TopoDS_Shape> aShapes;
220   std::list<ObjectPtr> aObjects;
221   aSelection->selectedShapes(aShapes, aObjects);
222   if (aShapes.Extent() > 0) {
223     TopoDS_Shape aShape = aShapes.First();
224     if (!aShape.IsNull()) {
225       if (aShape.ShapeType() == TopAbs_VERTEX) {
226         const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
227         if (!aVertex.IsNull()) {
228           // A case when point is taken from existing vertex
229           gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
230           double aX, aY;
231           PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
232           setPoint(aX, aY);
233           emit vertexSelected(aObjects.front(), aShape);
234           emit focusOutWidget(this);
235           return;
236         }
237       }
238     }
239   }
240   // A case when point is taken from mouse event
241   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
242   double aX, anY;
243   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
244   setPoint(aX, anY);
245
246   emit focusOutWidget(this);
247 }
248
249
250 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
251 {
252   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
253
254   double aX, anY;
255   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
256   setPoint(aX, anY);
257 }
258
259 double PartSet_WidgetPoint2D::x() const
260 {
261   return myXSpin->value();
262 }
263
264 double PartSet_WidgetPoint2D::y() const
265 {
266   return myYSpin->value();
267 }
268