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(2, 0, 2, 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 aPoint->setValue(myXSpin->value(), myYSpin->value());
114 updateObject(myFeature);
115 that->blockSignals(isBlocked);
120 bool ModuleBase_WidgetPoint2D::restoreValue()
122 boost::shared_ptr<ModelAPI_Data> aData = myFeature->data();
123 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
124 aData->attribute(attributeID()));
126 bool isBlocked = this->blockSignals(true);
127 myXSpin->setValue(aPoint->x());
128 myYSpin->setValue(aPoint->y());
129 this->blockSignals(isBlocked);
133 QWidget* ModuleBase_WidgetPoint2D::getControl() const
138 QList<QWidget*> ModuleBase_WidgetPoint2D::getControls() const
140 QList<QWidget*> aControls;
141 aControls.push_back(myXSpin);
142 aControls.push_back(myYSpin);
147 bool ModuleBase_WidgetPoint2D::eventFilter(QObject *theObject, QEvent *theEvent)
149 if (theObject == myXSpin || theObject == myYSpin) {
150 if (theEvent->type() == QEvent::KeyRelease) {
151 QKeyEvent* aKeyEvent = (QKeyEvent*) theEvent;
152 if (aKeyEvent && (aKeyEvent->key() == Qt::Key_Return ||
153 aKeyEvent->key() == Qt::Key_Enter)) {
154 emit focusOutWidget(this);
156 emit keyReleased((QKeyEvent*) theEvent);
160 return ModuleBase_ModelWidget::eventFilter(theObject, theEvent);
163 bool ModuleBase_WidgetPoint2D::initFromPrevious(ObjectPtr theObject)
165 if (myOptionParam.length() == 0)
167 boost::shared_ptr<ModelAPI_Data> aData = theObject->data();
168 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
169 aData->attribute(myOptionParam));
171 bool isBlocked = this->blockSignals(true);
172 myXSpin->setValue(aPoint->x());
173 myYSpin->setValue(aPoint->y());
174 this->blockSignals(isBlocked);
176 emit valuesChanged();
177 emit storedPoint2D(theObject, myOptionParam);