Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[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
9 #include <Config_Keywords.h>
10 #include <Config_WidgetAPI.h>
11
12 #include <Events_Loop.h>
13 #include <ModelAPI_Events.h>
14
15 #include <ModelAPI_Feature.h>
16 #include <ModelAPI_Data.h>
17 #include <ModelAPI_Object.h>
18 #include <GeomDataAPI_Point2D.h>
19 #include <GeomAPI_Pnt2d.h>
20
21 #include <QGroupBox>
22 #include <QGridLayout>
23 #include <QLabel>
24 #include <QEvent>
25 #include <QKeyEvent>
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   myGroupBox = new QGroupBox(QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME)),
37                              theParent);
38   QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
39   aGroupLay->setContentsMargins(2, 0, 2, 0);
40   aGroupLay->setColumnStretch(1, 1);
41   {
42     QLabel* aLabel = new QLabel(myGroupBox);
43     aLabel->setText("X");
44     aLabel->setPixmap(QPixmap(":pictures/x_point.png"));
45     aGroupLay->addWidget(aLabel, 0, 0);
46
47     myXSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
48     myXSpin->setMinimum(-DBL_MAX);
49     myXSpin->setMaximum(DBL_MAX);
50     myXSpin->setToolTip("X");
51     aGroupLay->addWidget(myXSpin, 0, 1);
52
53     connect(myXSpin, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
54   }
55   {
56     QLabel* aLabel = new QLabel(myGroupBox);
57     aLabel->setText("Y");
58     aLabel->setPixmap(QPixmap(":pictures/y_point.png"));
59     aGroupLay->addWidget(aLabel, 1, 0);
60
61     myYSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
62     myYSpin->setMinimum(-DBL_MAX);
63     myYSpin->setMaximum(DBL_MAX);
64     myYSpin->setToolTip("X");
65     aGroupLay->addWidget(myYSpin, 1, 1);
66
67     connect(myYSpin, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
68   }
69 }
70
71 ModuleBase_WidgetPoint2D::~ModuleBase_WidgetPoint2D()
72 {
73 }
74
75 bool ModuleBase_WidgetPoint2D::setValue(ModuleBase_WidgetValue* theValue)
76 {
77   bool isDone = false;
78   if (theValue) {
79     ModuleBase_WidgetValueFeature* aFeatureValue =
80         dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
81     if (aFeatureValue) {
82       boost::shared_ptr<GeomAPI_Pnt2d> aPoint = aFeatureValue->point();
83       if (aPoint) {
84         setPoint(aPoint);
85         isDone = true;
86       }
87     }
88   }
89   return isDone;
90 }
91
92 void ModuleBase_WidgetPoint2D::setPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
93 {
94
95   bool isBlocked = this->blockSignals(true);
96   myXSpin->setValue(thePoint->x());
97   myYSpin->setValue(thePoint->y());
98   this->blockSignals(isBlocked);
99
100   emit valuesChanged();
101 }
102
103 bool ModuleBase_WidgetPoint2D::storeValue() const
104 {
105   boost::shared_ptr<ModelAPI_Data> aData = myFeature->data();
106   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
107       aData->attribute(attributeID()));
108
109   ModuleBase_WidgetPoint2D* that = (ModuleBase_WidgetPoint2D*) this;
110   bool isBlocked = that->blockSignals(true);
111   aPoint->setValue(myXSpin->value(), myYSpin->value());
112   updateObject(myFeature);
113   that->blockSignals(isBlocked);
114
115   return true;
116 }
117
118 bool ModuleBase_WidgetPoint2D::restoreValue()
119 {
120   boost::shared_ptr<ModelAPI_Data> aData = myFeature->data();
121   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
122       aData->attribute(attributeID()));
123
124   bool isBlocked = this->blockSignals(true);
125   myXSpin->setValue(aPoint->x());
126   myYSpin->setValue(aPoint->y());
127   this->blockSignals(isBlocked);
128   return true;
129 }
130
131 QWidget* ModuleBase_WidgetPoint2D::getControl() const
132 {
133   return myGroupBox;
134 }
135
136 QList<QWidget*> ModuleBase_WidgetPoint2D::getControls() const
137 {
138   QList<QWidget*> aControls;
139   aControls.push_back(myXSpin);
140   aControls.push_back(myYSpin);
141
142   return aControls;
143 }
144
145 bool ModuleBase_WidgetPoint2D::initFromPrevious(ObjectPtr theObject)
146 {
147   if (myOptionParam.length() == 0)
148     return false;
149   boost::shared_ptr<ModelAPI_Data> aData = theObject->data();
150   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
151       aData->attribute(myOptionParam));
152   if (aPoint) {
153     bool isBlocked = this->blockSignals(true);
154     myXSpin->setValue(aPoint->x());
155     myYSpin->setValue(aPoint->y());
156     this->blockSignals(isBlocked);
157
158     emit valuesChanged();
159     emit storedPoint2D(theObject, myOptionParam);
160     return true;
161   }
162   return false;
163 }