]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetPoint2D.cpp
Salome HOME
623eaae6392f1c774103ac809ef28750d29cdce3
[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   if (!aData) {
75      ObjectPtr anObj = boost::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
76      if (anObj) aData = anObj->featureRef()->data();
77   }
78   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
79     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(myFeatureAttributeID));
80
81   bool isBlocked = this->blockSignals(true);
82   aPoint->setValue(myXSpin->value(), myYSpin->value());
83   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
84
85   this->blockSignals(isBlocked);
86   return true;
87 }
88
89 bool ModuleBase_WidgetPoint2D::restoreValue(boost::shared_ptr<ModelAPI_Feature> theFeature)
90 {
91   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
92   if (!aData) {
93      ObjectPtr anObj = boost::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
94      if (anObj) aData = anObj->featureRef()->data();
95   }
96   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
97     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(myFeatureAttributeID));
98
99   bool isBlocked = this->blockSignals(true);
100   myXSpin->setValue(aPoint->x());
101   myYSpin->setValue(aPoint->y());
102   this->blockSignals(isBlocked);
103   return true;
104 }
105
106 bool ModuleBase_WidgetPoint2D::focusTo(const std::string& theAttributeName)
107 {
108   if (theAttributeName != myFeatureAttributeID)
109     return false;
110
111   if (!myXSpin->hasFocus() && !myYSpin->hasFocus()) {
112     myXSpin->setFocus();
113   }
114
115   return true;
116 }
117
118 QWidget* ModuleBase_WidgetPoint2D::getControl() const
119 {
120   return myGroupBox;
121 }
122
123 QList<QWidget*> ModuleBase_WidgetPoint2D::getControls() const
124 {
125   QList<QWidget*> aControls;
126   aControls.push_back(myXSpin);
127   aControls.push_back(myYSpin);
128
129   return aControls;
130 }
131
132 bool ModuleBase_WidgetPoint2D::eventFilter(QObject *theObject, QEvent *theEvent)
133 {
134   if (theObject == myXSpin || theObject == myYSpin) {
135     if (theEvent->type() == QEvent::KeyRelease) {
136       emit keyReleased(myFeatureAttributeID, (QKeyEvent*) theEvent);
137       return true;
138     }
139   }
140   return ModuleBase_ModelWidget::eventFilter(theObject, theEvent);
141 }