]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetPoint2D.cpp
Salome HOME
Replace boost::shared_ptr<ModelAPI_Feature> on FeaturePtr
[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(FeaturePtr 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(FeaturePtr 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::canFocusTo(const std::string& theAttributeName)
99 {
100   return theAttributeName == myFeatureAttributeID;
101 }
102
103 void ModuleBase_WidgetPoint2D::focusTo()
104 {
105   if (!myXSpin->hasFocus() && !myYSpin->hasFocus())
106     myXSpin->setFocus();
107 }
108
109 QWidget* ModuleBase_WidgetPoint2D::getControl() const
110 {
111   return myGroupBox;
112 }
113
114 QList<QWidget*> ModuleBase_WidgetPoint2D::getControls() const
115 {
116   QList<QWidget*> aControls;
117   aControls.push_back(myXSpin);
118   aControls.push_back(myYSpin);
119
120   return aControls;
121 }
122
123 bool ModuleBase_WidgetPoint2D::eventFilter(QObject *theObject, QEvent *theEvent)
124 {
125   if (theObject == myXSpin || theObject == myYSpin) {
126     if (theEvent->type() == QEvent::KeyRelease) {
127       emit keyReleased(myFeatureAttributeID, (QKeyEvent*) theEvent);
128       return true;
129     }
130   }
131   return ModuleBase_ModelWidget::eventFilter(theObject, theEvent);
132 }