Salome HOME
4bfda9ed88af3056e82806e180c2b9d7b7dddcba
[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
18 #include <QGroupBox>
19 #include <QGridLayout>
20 #include <QDoubleSpinBox>
21 #include <QLabel>
22 #include <QEvent>
23 #include <QKeyEvent>
24
25 #include <cfloat>
26 #include <climits>
27
28 ModuleBase_WidgetPoint2D::ModuleBase_WidgetPoint2D(QWidget* theParent,
29                                                    const Config_WidgetAPI* theData)
30 : ModuleBase_ModelWidget(theParent, theData)
31 {
32   myGroupBox = new QGroupBox(QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME)),
33                              theParent);
34   QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
35   aGroupLay->setContentsMargins(0, 0, 0, 0);
36   aGroupLay->setColumnStretch(1, 1);
37   {
38     QLabel* aLabel = new QLabel(myGroupBox);
39     aLabel->setText("X");
40     aLabel->setPixmap(QPixmap(":pictures/x_point.png"));
41     aGroupLay->addWidget(aLabel, 0, 0);
42
43     myXSpin = new QDoubleSpinBox(myGroupBox);
44     myXSpin->setMinimum(-DBL_MAX);
45     myXSpin->setMaximum(DBL_MAX);
46     myXSpin->setToolTip("X");
47     aGroupLay->addWidget(myXSpin, 0, 1);
48     
49     connect(myXSpin, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
50   }
51   {
52     QLabel* aLabel = new QLabel(myGroupBox);
53     aLabel->setText("Y");
54     aLabel->setPixmap(QPixmap(":pictures/y_point.png"));
55     aGroupLay->addWidget(aLabel, 1, 0);
56
57     myYSpin = new QDoubleSpinBox(myGroupBox);
58     myYSpin->setMinimum(-DBL_MAX);
59     myYSpin->setMaximum(DBL_MAX);
60     myYSpin->setToolTip("X");
61     aGroupLay->addWidget(myYSpin, 1, 1);
62
63     connect(myYSpin, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
64   }
65   myXSpin->installEventFilter(this);
66   myYSpin->installEventFilter(this);
67 }
68
69 ModuleBase_WidgetPoint2D::~ModuleBase_WidgetPoint2D()
70 {
71 }
72
73 bool ModuleBase_WidgetPoint2D::storeValue(FeaturePtr theFeature) const
74 {
75   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
76   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
77     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(attributeID()));
78
79   ModuleBase_WidgetPoint2D* that = (ModuleBase_WidgetPoint2D*) this;
80   bool isBlocked = that->blockSignals(true);
81   aPoint->setValue(myXSpin->value(), myYSpin->value());
82   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
83   that->blockSignals(isBlocked);
84
85   return true;
86 }
87
88 bool ModuleBase_WidgetPoint2D::restoreValue(FeaturePtr theFeature)
89 {
90   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
91   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
92     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(attributeID()));
93
94   bool isBlocked = this->blockSignals(true);
95   myXSpin->setValue(aPoint->x());
96   myYSpin->setValue(aPoint->y());
97   this->blockSignals(isBlocked);
98   return true;
99 }
100
101 QWidget* ModuleBase_WidgetPoint2D::getControl() const
102 {
103   return myGroupBox;
104 }
105
106 QList<QWidget*> ModuleBase_WidgetPoint2D::getControls() const
107 {
108   QList<QWidget*> aControls;
109   aControls.push_back(myXSpin);
110   aControls.push_back(myYSpin);
111
112   return aControls;
113 }
114
115 bool ModuleBase_WidgetPoint2D::eventFilter(QObject *theObject, QEvent *theEvent)
116 {
117   if (theObject == myXSpin || theObject == myYSpin) {
118     if (theEvent->type() == QEvent::KeyRelease) {
119       emit keyReleased(attributeID(), (QKeyEvent*) theEvent);
120       return true;
121     }
122   }
123   return ModuleBase_ModelWidget::eventFilter(theObject, theEvent);
124 }