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