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