Salome HOME
#refs 76 - reported by Hervé Legrand: Edit Sketch as Constructions child - crash
[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
9 #include <Events_Loop.h>
10 #include <Model_Events.h>
11
12 #include <ModelAPI_Feature.h>
13 #include <ModelAPI_Data.h>
14 #include <ModelAPI_Object.h>
15 #include <GeomDataAPI_Point2D.h>
16
17 #include <QGroupBox>
18 #include <QGridLayout>
19 #include <QDoubleSpinBox>
20 #include <QLabel>
21 #include <QEvent>
22 #include <QKeyEvent>
23
24 #include <cfloat>
25 #include <climits>
26
27 ModuleBase_WidgetPoint2D::ModuleBase_WidgetPoint2D(QWidget* theParent, QString theTitle,
28                                                    const std::string& theFeatureAttributeID)
29 : ModuleBase_ModelWidget(theParent), myFeatureAttributeID(theFeatureAttributeID)
30 {
31   myGroupBox = new QGroupBox(theTitle, theParent);
32   QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
33   aGroupLay->setContentsMargins(0, 0, 0, 0);
34   aGroupLay->setColumnStretch(1, 1);
35   {
36     QLabel* aLabel = new QLabel(myGroupBox);
37     aLabel->setText("X");
38     aLabel->setPixmap(QPixmap(":pictures/x_point.png"));
39     aGroupLay->addWidget(aLabel, 0, 0);
40
41     myXSpin = new QDoubleSpinBox(myGroupBox);
42     myXSpin->setMinimum(-DBL_MAX);
43     myXSpin->setMaximum(DBL_MAX);
44     myXSpin->setToolTip("X");
45     aGroupLay->addWidget(myXSpin, 0, 1);
46     
47     connect(myXSpin, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
48   }
49   {
50     QLabel* aLabel = new QLabel(myGroupBox);
51     aLabel->setText("Y");
52     aLabel->setPixmap(QPixmap(":pictures/y_point.png"));
53     aGroupLay->addWidget(aLabel, 1, 0);
54
55     myYSpin = new QDoubleSpinBox(myGroupBox);
56     myYSpin->setMinimum(-DBL_MAX);
57     myYSpin->setMaximum(DBL_MAX);
58     myYSpin->setToolTip("X");
59     aGroupLay->addWidget(myYSpin, 1, 1);
60
61     connect(myYSpin, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
62   }
63   myXSpin->installEventFilter(this);
64   myYSpin->installEventFilter(this);
65 }
66
67 ModuleBase_WidgetPoint2D::~ModuleBase_WidgetPoint2D()
68 {
69 }
70
71 bool ModuleBase_WidgetPoint2D::storeValue(boost::shared_ptr<ModelAPI_Feature> theFeature)
72 {
73   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
74   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
75     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(myFeatureAttributeID));
76
77   bool isBlocked = this->blockSignals(true);
78   aPoint->setValue(myXSpin->value(), myYSpin->value());
79   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
80
81   this->blockSignals(isBlocked);
82   return true;
83 }
84
85 bool ModuleBase_WidgetPoint2D::restoreValue(boost::shared_ptr<ModelAPI_Feature> theFeature)
86 {
87   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
88   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
89     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(myFeatureAttributeID));
90
91   bool isBlocked = this->blockSignals(true);
92   myXSpin->setValue(aPoint->x());
93   myYSpin->setValue(aPoint->y());
94   this->blockSignals(isBlocked);
95   return true;
96 }
97
98 bool ModuleBase_WidgetPoint2D::focusTo(const std::string& theAttributeName)
99 {
100   if (theAttributeName != myFeatureAttributeID)
101     return false;
102
103   if (!myXSpin->hasFocus() && !myYSpin->hasFocus()) {
104     myXSpin->setFocus();
105   }
106
107   return true;
108 }
109
110 QWidget* ModuleBase_WidgetPoint2D::getControl() const
111 {
112   return myGroupBox;
113 }
114
115 QList<QWidget*> ModuleBase_WidgetPoint2D::getControls() const
116 {
117   QList<QWidget*> aControls;
118   aControls.push_back(myXSpin);
119   aControls.push_back(myYSpin);
120
121   return aControls;
122 }
123
124 bool ModuleBase_WidgetPoint2D::eventFilter(QObject *theObject, QEvent *theEvent)
125 {
126   if (theObject == myXSpin || theObject == myYSpin) {
127     if (theEvent->type() == QEvent::KeyRelease) {
128       emit keyReleased(myFeatureAttributeID, (QKeyEvent*) theEvent);
129       return true;
130     }
131   }
132   return ModuleBase_ModelWidget::eventFilter(theObject, theEvent);
133 }