]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetPoint2D.cpp
Salome HOME
157ccb45d74fd5737bcdfd4387bc2dd7a2c090cc
[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() 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   double aX = myXSpin->value();
114   double aY = myYSpin->value();
115   aPoint->setValue(myXSpin->value(), myYSpin->value());
116   updateObject(myFeature);
117   that->blockSignals(isBlocked);
118
119   return true;
120 }
121
122 bool ModuleBase_WidgetPoint2D::restoreValue()
123 {
124   boost::shared_ptr<ModelAPI_Data> aData = myFeature->data();
125   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
126       aData->attribute(attributeID()));
127
128   bool isBlocked = this->blockSignals(true);
129   myXSpin->setValue(aPoint->x());
130   myYSpin->setValue(aPoint->y());
131   this->blockSignals(isBlocked);
132   return true;
133 }
134
135 QWidget* ModuleBase_WidgetPoint2D::getControl() const
136 {
137   return myGroupBox;
138 }
139
140 QList<QWidget*> ModuleBase_WidgetPoint2D::getControls() const
141 {
142   QList<QWidget*> aControls;
143   aControls.push_back(myXSpin);
144   aControls.push_back(myYSpin);
145
146   return aControls;
147 }
148
149 bool ModuleBase_WidgetPoint2D::eventFilter(QObject *theObject, QEvent *theEvent)
150 {
151   if (theObject == myXSpin || theObject == myYSpin) {
152     if (theEvent->type() == QEvent::KeyRelease) {
153       QKeyEvent* aKeyEvent = (QKeyEvent*) theEvent;
154       if (aKeyEvent && aKeyEvent->key() == Qt::Key_Return) {
155         emit focusOutWidget(this);
156       }
157       emit keyReleased(attributeID(), (QKeyEvent*) theEvent);
158       return true;
159     }
160   }
161   return ModuleBase_ModelWidget::eventFilter(theObject, theEvent);
162 }
163
164 bool ModuleBase_WidgetPoint2D::initFromPrevious(ObjectPtr theObject)
165 {
166   if (myOptionParam.length() == 0)
167     return false;
168   boost::shared_ptr<ModelAPI_Data> aData = theObject->data();
169   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
170       aData->attribute(myOptionParam));
171   if (aPoint) {
172     bool isBlocked = this->blockSignals(true);
173     myXSpin->setValue(aPoint->x());
174     myYSpin->setValue(aPoint->y());
175     this->blockSignals(isBlocked);
176
177     emit valuesChanged();
178     emit storedPoint2D(theObject, myOptionParam);
179     return true;
180   }
181   return false;
182 }