Salome HOME
Result attributes validators created
[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
8 #include <Config_Keywords.h>
9 #include <Config_WidgetAPI.h>
10
11 #include <Events_Loop.h>
12 #include <ModelAPI_Events.h>
13
14 #include <ModelAPI_Feature.h>
15 #include <ModelAPI_Data.h>
16 #include <ModelAPI_Object.h>
17 #include <GeomDataAPI_Point2D.h>
18 #include <GeomAPI_Pnt2d.h>
19
20 #include <QGroupBox>
21 #include <QGridLayout>
22 #include <QDoubleSpinBox>
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(0, 0, 0, 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 QDoubleSpinBox(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 QDoubleSpinBox(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(ObjectPtr theObject) const
106 {
107   boost::shared_ptr<ModelAPI_Data> aData = theObject->data();
108   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
109     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(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   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
115   that->blockSignals(isBlocked);
116
117   return true;
118 }
119
120 bool ModuleBase_WidgetPoint2D::restoreValue(ObjectPtr theObject)
121 {
122   boost::shared_ptr<ModelAPI_Data> aData = theObject->data();
123   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
124     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(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::eventFilter(QObject *theObject, QEvent *theEvent)
148 {
149   if (theObject == myXSpin || theObject == myYSpin) {
150     if (theEvent->type() == QEvent::KeyRelease) {
151       QKeyEvent* aKeyEvent = (QKeyEvent*)theEvent;
152       if (aKeyEvent && aKeyEvent->key() == Qt::Key_Return) {
153         emit focusOutWidget(this);
154       }
155       emit keyReleased(attributeID(), (QKeyEvent*) theEvent);
156       return true;
157     }
158   }
159   return ModuleBase_ModelWidget::eventFilter(theObject, theEvent);
160 }
161
162 bool ModuleBase_WidgetPoint2D::initFromPrevious(ObjectPtr theObject)
163 {
164   if (myOptionParam.length() == 0)
165     return false;
166   boost::shared_ptr<ModelAPI_Data> aData = theObject->data();
167   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
168     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(myOptionParam));
169   if (aPoint) {
170     bool isBlocked = this->blockSignals(true);
171     myXSpin->setValue(aPoint->x());
172     myYSpin->setValue(aPoint->y());
173     this->blockSignals(isBlocked);
174
175     emit valuesChanged();
176     emit storedPoint2D(theObject, myOptionParam);
177     return true;
178   }
179   return false;
180 }