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