Salome HOME
Fixed problem with too many executions of extrusion wit hexternal sketch edges
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetPoint2D.cpp
1 // File:        ModuleBase_WidgetPoint2D.cpp
2 // Created:     25 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <ModuleBase_WidgetPoint2D.h>
6 #include <ModuleBase_WidgetValueFeature.h>
7 #include <ModuleBase_DoubleSpinBox.h>
8 #include <ModuleBase_Tools.h>
9
10 #include <Config_Keywords.h>
11 #include <Config_WidgetAPI.h>
12
13 #include <Events_Loop.h>
14 #include <ModelAPI_Events.h>
15
16 #include <ModelAPI_Feature.h>
17 #include <ModelAPI_Data.h>
18 #include <ModelAPI_Object.h>
19 #include <GeomDataAPI_Point2D.h>
20 #include <GeomAPI_Pnt2d.h>
21
22 #include <QGroupBox>
23 #include <QGridLayout>
24 #include <QLabel>
25 #include <QEvent>
26
27 #include <cfloat>
28 #include <climits>
29
30 ModuleBase_WidgetPoint2D::ModuleBase_WidgetPoint2D(QWidget* theParent,
31                                                    const Config_WidgetAPI* theData,
32                                                    const std::string& theParentId)
33     : ModuleBase_ModelWidget(theParent, theData, theParentId)
34 {
35   myOptionParam = theData->getProperty(PREVIOUS_FEATURE_PARAM);
36   QString aPageName = QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME));
37   myGroupBox = new QGroupBox(aPageName, theParent);
38   myGroupBox->setFlat(false);
39
40   QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
41   ModuleBase_Tools::adjustMargins(aGroupLay);
42   aGroupLay->setColumnStretch(1, 1);
43   {
44     QLabel* aLabel = new QLabel(myGroupBox);
45     aLabel->setText("X");
46     aLabel->setPixmap(QPixmap(":pictures/x_point.png"));
47     aGroupLay->addWidget(aLabel, 0, 0);
48
49     myXSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
50     myXSpin->setMinimum(-DBL_MAX);
51     myXSpin->setMaximum(DBL_MAX);
52     myXSpin->setToolTip("X");
53     aGroupLay->addWidget(myXSpin, 0, 1);
54
55     connect(myXSpin, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
56   }
57   {
58     QLabel* aLabel = new QLabel(myGroupBox);
59     aLabel->setText("Y");
60     aLabel->setPixmap(QPixmap(":pictures/y_point.png"));
61     aGroupLay->addWidget(aLabel, 1, 0);
62
63     myYSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
64     myYSpin->setMinimum(-DBL_MAX);
65     myYSpin->setMaximum(DBL_MAX);
66     myYSpin->setToolTip("X");
67     aGroupLay->addWidget(myYSpin, 1, 1);
68
69     connect(myYSpin, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
70   }
71 }
72
73 ModuleBase_WidgetPoint2D::~ModuleBase_WidgetPoint2D()
74 {
75 }
76
77 bool ModuleBase_WidgetPoint2D::setValue(ModuleBase_WidgetValue* theValue)
78 {
79   bool isDone = false;
80   if (theValue) {
81     ModuleBase_WidgetValueFeature* aFeatureValue =
82         dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
83     if (aFeatureValue) {
84       boost::shared_ptr<GeomAPI_Pnt2d> aPoint = aFeatureValue->point();
85       if (aPoint) {
86         setPoint(aPoint);
87         isDone = true;
88       }
89     }
90   }
91   return isDone;
92 }
93
94 void ModuleBase_WidgetPoint2D::setPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
95 {
96
97   bool isBlocked = this->blockSignals(true);
98   myXSpin->setValue(thePoint->x());
99   myYSpin->setValue(thePoint->y());
100   this->blockSignals(isBlocked);
101
102   emit valuesChanged();
103 }
104
105 bool ModuleBase_WidgetPoint2D::storeValue() const
106 {
107   boost::shared_ptr<ModelAPI_Data> aData = myFeature->data();
108   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
109       aData->attribute(attributeID()));
110   
111   ModuleBase_WidgetPoint2D* that = (ModuleBase_WidgetPoint2D*) this;
112   bool isBlocked = that->blockSignals(true);
113   bool isImmutable = aPoint->setImmutable(true);
114 #ifdef _DEBUG
115   std::string _attr_name = myAttributeID;
116   double _X = myXSpin->value();
117   double _Y = myYSpin->value();
118 #endif
119   aPoint->setValue(myXSpin->value(), myYSpin->value());
120   updateObject(myFeature);
121   aPoint->setImmutable(isImmutable);
122   that->blockSignals(isBlocked);
123
124   return true;
125 }
126
127 bool ModuleBase_WidgetPoint2D::restoreValue()
128 {
129   boost::shared_ptr<ModelAPI_Data> aData = myFeature->data();
130   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
131       aData->attribute(attributeID()));
132
133 #ifdef _DEBUG
134   std::string _attr_name = myAttributeID;
135   double _X = aPoint->x();
136   double _Y = aPoint->y();
137 #endif
138   bool isBlocked = this->blockSignals(true);
139   myXSpin->setValue(aPoint->x());
140   myYSpin->setValue(aPoint->y());
141   this->blockSignals(isBlocked);
142   return true;
143 }
144
145 QWidget* ModuleBase_WidgetPoint2D::getControl() const
146 {
147   return myGroupBox;
148 }
149
150 QList<QWidget*> ModuleBase_WidgetPoint2D::getControls() const
151 {
152   QList<QWidget*> aControls;
153   aControls.push_back(myXSpin);
154   aControls.push_back(myYSpin);
155
156   return aControls;
157 }
158
159 bool ModuleBase_WidgetPoint2D::initFromPrevious(ObjectPtr theObject)
160 {
161   if (myOptionParam.length() == 0)
162     return false;
163   boost::shared_ptr<ModelAPI_Data> aData = theObject->data();
164   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
165       aData->attribute(myOptionParam));
166   if (aPoint) {
167     bool isBlocked = this->blockSignals(true);
168     myXSpin->setValue(aPoint->x());
169     myYSpin->setValue(aPoint->y());
170     this->blockSignals(isBlocked);
171
172     emit valuesChanged();
173     emit storedPoint2D(theObject, myOptionParam);
174     return true;
175   }
176   return false;
177 }