]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetPoint2D.cpp
Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[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 #include <ModuleBase_WidgetValueFeature.h>
7
8 #include <Config_Keywords.h>
9 #include <Config_WidgetAPI.h>
10
11 #include <Events_Loop.h>
12 #include <Model_Events.h>
13
14 #include <ModelAPI_Feature.h>
15 #include <ModelAPI_Data.h>
16 #include <ModelAPI_Object.h>
17 #include <GeomDataAPI_Point2D.h>
18 #include <GeomAPI_Pnt2d.h>
19
20 #include <QGroupBox>
21 #include <QGridLayout>
22 #include <QDoubleSpinBox>
23 #include <QLabel>
24 #include <QEvent>
25 #include <QKeyEvent>
26
27 #include <cfloat>
28 #include <climits>
29
30 ModuleBase_WidgetPoint2D::ModuleBase_WidgetPoint2D(QWidget* theParent,
31                                                    const Config_WidgetAPI* theData)
32 : ModuleBase_ModelWidget(theParent, theData)
33 {
34   myOptionParam = theData->getProperty(PREVIOUS_FEATURE_PARAM);
35   myGroupBox = new QGroupBox(QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME)),
36                              theParent);
37   QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
38   aGroupLay->setContentsMargins(0, 0, 0, 0);
39   aGroupLay->setColumnStretch(1, 1);
40   {
41     QLabel* aLabel = new QLabel(myGroupBox);
42     aLabel->setText("X");
43     aLabel->setPixmap(QPixmap(":pictures/x_point.png"));
44     aGroupLay->addWidget(aLabel, 0, 0);
45
46     myXSpin = new QDoubleSpinBox(myGroupBox);
47     myXSpin->setMinimum(-DBL_MAX);
48     myXSpin->setMaximum(DBL_MAX);
49     myXSpin->setToolTip("X");
50     aGroupLay->addWidget(myXSpin, 0, 1);
51     
52     connect(myXSpin, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
53   }
54   {
55     QLabel* aLabel = new QLabel(myGroupBox);
56     aLabel->setText("Y");
57     aLabel->setPixmap(QPixmap(":pictures/y_point.png"));
58     aGroupLay->addWidget(aLabel, 1, 0);
59
60     myYSpin = new QDoubleSpinBox(myGroupBox);
61     myYSpin->setMinimum(-DBL_MAX);
62     myYSpin->setMaximum(DBL_MAX);
63     myYSpin->setToolTip("X");
64     aGroupLay->addWidget(myYSpin, 1, 1);
65
66     connect(myYSpin, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
67   }
68   myXSpin->installEventFilter(this);
69   myYSpin->installEventFilter(this);
70 }
71
72 ModuleBase_WidgetPoint2D::~ModuleBase_WidgetPoint2D()
73 {
74 }
75
76 bool ModuleBase_WidgetPoint2D::setValue(ModuleBase_WidgetValue* theValue)
77 {
78   bool isDone = false;
79   if (theValue) {
80     ModuleBase_WidgetValueFeature* aFeatureValue = 
81                          dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
82     if (aFeatureValue) {
83       setPoint(aFeatureValue->point());
84       isDone = true;
85     }
86   }
87   return isDone;
88 }
89
90 void ModuleBase_WidgetPoint2D::setPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
91 {
92
93   bool isBlocked = this->blockSignals(true);
94   myXSpin->setValue(thePoint->x());
95   myYSpin->setValue(thePoint->y());
96   this->blockSignals(isBlocked);
97
98   emit valuesChanged();
99 }
100
101 bool ModuleBase_WidgetPoint2D::storeValue(FeaturePtr theFeature) const
102 {
103   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
104   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
105     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(attributeID()));
106
107   ModuleBase_WidgetPoint2D* that = (ModuleBase_WidgetPoint2D*) this;
108   bool isBlocked = that->blockSignals(true);
109   aPoint->setValue(myXSpin->value(), myYSpin->value());
110   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
111   that->blockSignals(isBlocked);
112
113   return true;
114 }
115
116 bool ModuleBase_WidgetPoint2D::restoreValue(FeaturePtr theFeature)
117 {
118   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
119   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
120     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(attributeID()));
121
122   bool isBlocked = this->blockSignals(true);
123   myXSpin->setValue(aPoint->x());
124   myYSpin->setValue(aPoint->y());
125   this->blockSignals(isBlocked);
126   return true;
127 }
128
129 QWidget* ModuleBase_WidgetPoint2D::getControl() const
130 {
131   return myGroupBox;
132 }
133
134 QList<QWidget*> ModuleBase_WidgetPoint2D::getControls() const
135 {
136   QList<QWidget*> aControls;
137   aControls.push_back(myXSpin);
138   aControls.push_back(myYSpin);
139
140   return aControls;
141 }
142
143 bool ModuleBase_WidgetPoint2D::eventFilter(QObject *theObject, QEvent *theEvent)
144 {
145   if (theObject == myXSpin || theObject == myYSpin) {
146     if (theEvent->type() == QEvent::KeyRelease) {
147       QKeyEvent* aKeyEvent = (QKeyEvent*)theEvent;
148       if (aKeyEvent && aKeyEvent->key() == Qt::Key_Return) {
149         emit focusOutWidget(this);
150       }
151       emit keyReleased(attributeID(), (QKeyEvent*) theEvent);
152       return true;
153     }
154   }
155   return ModuleBase_ModelWidget::eventFilter(theObject, theEvent);
156 }
157
158 bool ModuleBase_WidgetPoint2D::initFromPrevious(FeaturePtr theFeature)
159 {
160   if (myOptionParam.length() == 0)
161     return false;
162   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
163   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
164     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(myOptionParam));
165   if (aPoint) {
166     bool isBlocked = this->blockSignals(true);
167     myXSpin->setValue(aPoint->x());
168     myYSpin->setValue(aPoint->y());
169     this->blockSignals(isBlocked);
170
171     emit valuesChanged();
172     emit storedPoint2D(theFeature, myOptionParam);
173     return true;
174   }
175   return false;
176 }