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