]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetPoint2D.cpp
Salome HOME
01f9902eb5fa4918500cf931b6f3905b44eb8d50
[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 <GeomDataAPI_Point2D.h>
15
16 #include <QGroupBox>
17 #include <QGridLayout>
18 #include <QDoubleSpinBox>
19 #include <QLabel>
20 #include <QEvent>
21 #include <QKeyEvent>
22
23 #include <cfloat>
24 #include <climits>
25
26 ModuleBase_WidgetPoint2D::ModuleBase_WidgetPoint2D(QWidget* theParent, QString theTitle,
27                                                    const std::string& theFeatureAttributeID)
28 : ModuleBase_ModelWidget(theParent), myFeatureAttributeID(theFeatureAttributeID)
29 {
30   myGroupBox = new QGroupBox(theTitle, theParent);
31   QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
32   aGroupLay->setContentsMargins(0, 0, 0, 0);
33   aGroupLay->setColumnStretch(1, 1);
34   {
35     QLabel* aLabel = new QLabel(myGroupBox);
36     aLabel->setText("X");
37     aLabel->setPixmap(QPixmap(":pictures/x_point.png"));
38     aGroupLay->addWidget(aLabel, 0, 0);
39
40     myXSpin = new QDoubleSpinBox(myGroupBox);
41     myXSpin->setMinimum(-DBL_MAX);
42     myXSpin->setMaximum(DBL_MAX);
43     myXSpin->setToolTip("X");
44     aGroupLay->addWidget(myXSpin, 0, 1);
45     
46     connect(myXSpin, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
47   }
48   {
49     QLabel* aLabel = new QLabel(myGroupBox);
50     aLabel->setText("Y");
51     aLabel->setPixmap(QPixmap(":pictures/y_point.png"));
52     aGroupLay->addWidget(aLabel, 1, 0);
53
54     myYSpin = new QDoubleSpinBox(myGroupBox);
55     myYSpin->setMinimum(-DBL_MAX);
56     myYSpin->setMaximum(DBL_MAX);
57     myYSpin->setToolTip("X");
58     aGroupLay->addWidget(myYSpin, 1, 1);
59
60     connect(myYSpin, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
61   }
62   myXSpin->installEventFilter(this);
63   myYSpin->installEventFilter(this);
64 }
65
66 ModuleBase_WidgetPoint2D::~ModuleBase_WidgetPoint2D()
67 {
68 }
69
70 bool ModuleBase_WidgetPoint2D::storeValue(boost::shared_ptr<ModelAPI_Feature> theFeature)
71 {
72   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
73   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
74     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(myFeatureAttributeID));
75
76   bool isBlocked = this->blockSignals(true);
77   aPoint->setValue(myXSpin->value(), myYSpin->value());
78   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
79
80   this->blockSignals(isBlocked);
81   return true;
82 }
83
84 bool ModuleBase_WidgetPoint2D::restoreValue(boost::shared_ptr<ModelAPI_Feature> theFeature)
85 {
86   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
87   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
88     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(myFeatureAttributeID));
89
90   bool isBlocked = this->blockSignals(true);
91   myXSpin->setValue(aPoint->x());
92   myYSpin->setValue(aPoint->y());
93   this->blockSignals(isBlocked);
94   return true;
95 }
96
97 bool ModuleBase_WidgetPoint2D::focusTo(const std::string& theAttributeName)
98 {
99   if (theAttributeName != myFeatureAttributeID)
100     return false;
101
102   if (!myXSpin->hasFocus() && !myYSpin->hasFocus()) {
103     myXSpin->setFocus();
104   }
105
106   return true;
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 }