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   myXSpin->installEventFilter(this);
70   myYSpin->installEventFilter(this);
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   aPoint->setValue(myXSpin->value(), myYSpin->value());
114   updateObject(myFeature);
115   that->blockSignals(isBlocked);
116
117   return true;
118 }
119
120 bool ModuleBase_WidgetPoint2D::restoreValue()
121 {
122   boost::shared_ptr<ModelAPI_Data> aData = myFeature->data();
123   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
124       aData->attribute(attributeID()));
125
126   bool isBlocked = this->blockSignals(true);
127   myXSpin->setValue(aPoint->x());
128   myYSpin->setValue(aPoint->y());
129   this->blockSignals(isBlocked);
130   return true;
131 }
132
133 QWidget* ModuleBase_WidgetPoint2D::getControl() const
134 {
135   return myGroupBox;
136 }
137
138 QList<QWidget*> ModuleBase_WidgetPoint2D::getControls() const
139 {
140   QList<QWidget*> aControls;
141   aControls.push_back(myXSpin);
142   aControls.push_back(myYSpin);
143
144   return aControls;
145 }
146
147 bool ModuleBase_WidgetPoint2D::initFromPrevious(ObjectPtr theObject)
148 {
149   if (myOptionParam.length() == 0)
150     return false;
151   boost::shared_ptr<ModelAPI_Data> aData = theObject->data();
152   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
153       aData->attribute(myOptionParam));
154   if (aPoint) {
155     bool isBlocked = this->blockSignals(true);
156     myXSpin->setValue(aPoint->x());
157     myYSpin->setValue(aPoint->y());
158     this->blockSignals(isBlocked);
159
160     emit valuesChanged();
161     emit storedPoint2D(theObject, myOptionParam);
162     return true;
163   }
164   return false;
165 }