1 // File: ModuleBase_WidgetPoint2D.cpp
2 // Created: 25 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include <ModuleBase_WidgetPoint2D.h>
6 #include <ModuleBase_WidgetValueFeature.h>
8 #include <Config_Keywords.h>
9 #include <Config_WidgetAPI.h>
11 #include <Events_Loop.h>
12 #include <ModelAPI_Events.h>
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>
21 #include <QGridLayout>
22 #include <QDoubleSpinBox>
30 ModuleBase_WidgetPoint2D::ModuleBase_WidgetPoint2D(QWidget* theParent,
31 const Config_WidgetAPI* theData)
32 : ModuleBase_ModelWidget(theParent, theData)
34 myOptionParam = theData->getProperty(PREVIOUS_FEATURE_PARAM);
35 myGroupBox = new QGroupBox(QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME)),
37 QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
38 aGroupLay->setContentsMargins(0, 0, 0, 0);
39 aGroupLay->setColumnStretch(1, 1);
41 QLabel* aLabel = new QLabel(myGroupBox);
43 aLabel->setPixmap(QPixmap(":pictures/x_point.png"));
44 aGroupLay->addWidget(aLabel, 0, 0);
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);
52 connect(myXSpin, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
55 QLabel* aLabel = new QLabel(myGroupBox);
57 aLabel->setPixmap(QPixmap(":pictures/y_point.png"));
58 aGroupLay->addWidget(aLabel, 1, 0);
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);
66 connect(myYSpin, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
68 myXSpin->installEventFilter(this);
69 myYSpin->installEventFilter(this);
72 ModuleBase_WidgetPoint2D::~ModuleBase_WidgetPoint2D()
76 bool ModuleBase_WidgetPoint2D::setValue(ModuleBase_WidgetValue* theValue)
80 ModuleBase_WidgetValueFeature* aFeatureValue =
81 dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
83 boost::shared_ptr<GeomAPI_Pnt2d> aPoint = aFeatureValue->point();
93 void ModuleBase_WidgetPoint2D::setPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
96 bool isBlocked = this->blockSignals(true);
97 myXSpin->setValue(thePoint->x());
98 myYSpin->setValue(thePoint->y());
99 this->blockSignals(isBlocked);
101 emit valuesChanged();
104 bool ModuleBase_WidgetPoint2D::storeValue(ObjectPtr theObject) const
106 boost::shared_ptr<ModelAPI_Data> aData = theObject->data();
107 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
108 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(attributeID()));
110 ModuleBase_WidgetPoint2D* that = (ModuleBase_WidgetPoint2D*) this;
111 bool isBlocked = that->blockSignals(true);
112 aPoint->setValue(myXSpin->value(), myYSpin->value());
113 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
114 that->blockSignals(isBlocked);
119 bool ModuleBase_WidgetPoint2D::restoreValue(ObjectPtr theObject)
121 boost::shared_ptr<ModelAPI_Data> aData = theObject->data();
122 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
123 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(attributeID()));
125 bool isBlocked = this->blockSignals(true);
126 myXSpin->setValue(aPoint->x());
127 myYSpin->setValue(aPoint->y());
128 this->blockSignals(isBlocked);
132 QWidget* ModuleBase_WidgetPoint2D::getControl() const
137 QList<QWidget*> ModuleBase_WidgetPoint2D::getControls() const
139 QList<QWidget*> aControls;
140 aControls.push_back(myXSpin);
141 aControls.push_back(myYSpin);
146 bool ModuleBase_WidgetPoint2D::eventFilter(QObject *theObject, QEvent *theEvent)
148 if (theObject == myXSpin || theObject == myYSpin) {
149 if (theEvent->type() == QEvent::KeyRelease) {
150 QKeyEvent* aKeyEvent = (QKeyEvent*)theEvent;
151 if (aKeyEvent && aKeyEvent->key() == Qt::Key_Return) {
152 emit focusOutWidget(this);
154 emit keyReleased(attributeID(), (QKeyEvent*) theEvent);
158 return ModuleBase_ModelWidget::eventFilter(theObject, theEvent);
161 bool ModuleBase_WidgetPoint2D::initFromPrevious(ObjectPtr theObject)
163 if (myOptionParam.length() == 0)
165 boost::shared_ptr<ModelAPI_Data> aData = theObject->data();
166 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
167 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(myOptionParam));
169 bool isBlocked = this->blockSignals(true);
170 myXSpin->setValue(aPoint->x());
171 myYSpin->setValue(aPoint->y());
172 this->blockSignals(isBlocked);
174 emit valuesChanged();
175 emit storedPoint2D(theObject, myOptionParam);