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