1 // File: ModuleBase_Widgets.h
2 // Created: 04 June 2014
3 // Author: Vitaly Smetannikov
5 #include <ModuleBase_WidgetDoubleValue.h>
7 #include <ModelAPI_AttributeDouble.h>
8 #include <ModelAPI_Data.h>
10 #include <Config_Keywords.h>
11 #include <Config_WidgetAPI.h>
13 #include <Events_Loop.h>
14 #include <Model_Events.h>
19 #include <QDoubleSpinBox>
24 ModuleBase_WidgetDoubleValue::ModuleBase_WidgetDoubleValue(QWidget* theParent, const Config_WidgetAPI* theData)
25 : ModuleBase_ModelWidget(theParent, theData)
27 myContainer = new QWidget(theParent);
28 QHBoxLayout* aControlLay = new QHBoxLayout(myContainer);
29 aControlLay->setContentsMargins(0, 0, 0, 0);
31 QString aLabelText = QString::fromStdString(theData->widgetLabel());
32 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
33 myLabel = new QLabel(aLabelText, myContainer);
34 myLabel->setPixmap(QPixmap(aLabelIcon));
35 aControlLay->addWidget(myLabel);
37 mySpinBox = new QDoubleSpinBox(myContainer);
38 QString anObjName = QString::fromStdString(attributeID());
39 mySpinBox->setObjectName(anObjName);
42 std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
43 double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
45 mySpinBox->setMinimum(aMinVal);
47 mySpinBox->setMinimum(-DBL_MAX);
50 aProp = theData->getProperty(DOUBLE_WDG_MAX);
51 double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
53 mySpinBox->setMaximum(aMaxVal);
55 mySpinBox->setMaximum(DBL_MAX);
58 aProp = theData->getProperty(DOUBLE_WDG_STEP);
59 double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
61 mySpinBox->setSingleStep(aStepVal);
64 aProp = theData->getProperty(DOUBLE_WDG_DFLT);
65 double aDefVal = QString::fromStdString(aProp).toDouble(&isOk);
67 mySpinBox->setValue(aDefVal);
70 QString aTTip = QString::fromStdString(theData->widgetTooltip());
71 mySpinBox->setToolTip(aTTip);
73 aControlLay->addWidget(mySpinBox);
74 aControlLay->setStretch(1, 1);
76 connect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
78 mySpinBox->installEventFilter(this);
81 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
85 bool ModuleBase_WidgetDoubleValue::storeValue(FeaturePtr theFeature) const
87 DataPtr aData = theFeature->data();
88 boost::shared_ptr<ModelAPI_AttributeDouble> aReal = aData->real(attributeID());
89 if (aReal->value() != mySpinBox->value()) {
90 aReal->setValue(mySpinBox->value());
91 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
96 bool ModuleBase_WidgetDoubleValue::restoreValue(FeaturePtr theFeature)
98 DataPtr aData = theFeature->data();
99 boost::shared_ptr<ModelAPI_AttributeDouble> aRef = aData->real(attributeID());
101 bool isBlocked = mySpinBox->blockSignals(true);
102 mySpinBox->setValue(aRef->value());
103 mySpinBox->blockSignals(isBlocked);
108 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
110 QList<QWidget*> aList;
111 aList.append(myLabel);
112 aList.append(mySpinBox);
116 bool ModuleBase_WidgetDoubleValue::eventFilter(QObject *theObject, QEvent *theEvent)
118 if (theObject == mySpinBox) {
119 if (theEvent->type() == QEvent::KeyRelease) {
120 emit keyReleased(attributeID(), (QKeyEvent*) theEvent);
124 return ModuleBase_ModelWidget::eventFilter(theObject, theEvent);