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 const std::string& theParentId)
33 : ModuleBase_ModelWidget(theParent, theData, theParentId)
35 myOptionParam = theData->getProperty(PREVIOUS_FEATURE_PARAM);
36 myGroupBox = new QGroupBox(QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME)),
38 QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
39 aGroupLay->setContentsMargins(0, 0, 0, 0);
40 aGroupLay->setColumnStretch(1, 1);
42 QLabel* aLabel = new QLabel(myGroupBox);
44 aLabel->setPixmap(QPixmap(":pictures/x_point.png"));
45 aGroupLay->addWidget(aLabel, 0, 0);
47 myXSpin = new QDoubleSpinBox(myGroupBox);
48 myXSpin->setMinimum(-DBL_MAX);
49 myXSpin->setMaximum(DBL_MAX);
50 myXSpin->setToolTip("X");
51 aGroupLay->addWidget(myXSpin, 0, 1);
53 connect(myXSpin, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
56 QLabel* aLabel = new QLabel(myGroupBox);
58 aLabel->setPixmap(QPixmap(":pictures/y_point.png"));
59 aGroupLay->addWidget(aLabel, 1, 0);
61 myYSpin = new QDoubleSpinBox(myGroupBox);
62 myYSpin->setMinimum(-DBL_MAX);
63 myYSpin->setMaximum(DBL_MAX);
64 myYSpin->setToolTip("X");
65 aGroupLay->addWidget(myYSpin, 1, 1);
67 connect(myYSpin, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
69 myXSpin->installEventFilter(this);
70 myYSpin->installEventFilter(this);
73 ModuleBase_WidgetPoint2D::~ModuleBase_WidgetPoint2D()
77 bool ModuleBase_WidgetPoint2D::setValue(ModuleBase_WidgetValue* theValue)
81 ModuleBase_WidgetValueFeature* aFeatureValue =
82 dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
84 boost::shared_ptr<GeomAPI_Pnt2d> aPoint = aFeatureValue->point();
94 void ModuleBase_WidgetPoint2D::setPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
97 bool isBlocked = this->blockSignals(true);
98 myXSpin->setValue(thePoint->x());
99 myYSpin->setValue(thePoint->y());
100 this->blockSignals(isBlocked);
102 emit valuesChanged();
105 bool ModuleBase_WidgetPoint2D::storeValue() const
107 boost::shared_ptr<ModelAPI_Data> aData = myFeature->data();
108 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
109 aData->attribute(attributeID()));
111 ModuleBase_WidgetPoint2D* that = (ModuleBase_WidgetPoint2D*) this;
112 bool isBlocked = that->blockSignals(true);
113 double aX = myXSpin->value();
114 double aY = myYSpin->value();
115 aPoint->setValue(myXSpin->value(), myYSpin->value());
116 updateObject(myFeature);
117 that->blockSignals(isBlocked);
122 bool ModuleBase_WidgetPoint2D::restoreValue()
124 boost::shared_ptr<ModelAPI_Data> aData = myFeature->data();
125 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
126 aData->attribute(attributeID()));
128 bool isBlocked = this->blockSignals(true);
129 myXSpin->setValue(aPoint->x());
130 myYSpin->setValue(aPoint->y());
131 this->blockSignals(isBlocked);
135 QWidget* ModuleBase_WidgetPoint2D::getControl() const
140 QList<QWidget*> ModuleBase_WidgetPoint2D::getControls() const
142 QList<QWidget*> aControls;
143 aControls.push_back(myXSpin);
144 aControls.push_back(myYSpin);
149 bool ModuleBase_WidgetPoint2D::eventFilter(QObject *theObject, QEvent *theEvent)
151 if (theObject == myXSpin || theObject == myYSpin) {
152 if (theEvent->type() == QEvent::KeyRelease) {
153 QKeyEvent* aKeyEvent = (QKeyEvent*) theEvent;
154 if (aKeyEvent && aKeyEvent->key() == Qt::Key_Return) {
155 emit focusOutWidget(this);
157 emit keyReleased(attributeID(), (QKeyEvent*) theEvent);
161 return ModuleBase_ModelWidget::eventFilter(theObject, theEvent);
164 bool ModuleBase_WidgetPoint2D::initFromPrevious(ObjectPtr theObject)
166 if (myOptionParam.length() == 0)
168 boost::shared_ptr<ModelAPI_Data> aData = theObject->data();
169 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
170 aData->attribute(myOptionParam));
172 bool isBlocked = this->blockSignals(true);
173 myXSpin->setValue(aPoint->x());
174 myYSpin->setValue(aPoint->y());
175 this->blockSignals(isBlocked);
177 emit valuesChanged();
178 emit storedPoint2D(theObject, myOptionParam);