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 <ModelAPI_Events.h>
19 #include <QDoubleSpinBox>
24 #define DBL_MAX 1.7976931348623158e+308
28 ModuleBase_WidgetDoubleValue::ModuleBase_WidgetDoubleValue(QWidget* theParent,
29 const Config_WidgetAPI* theData,
30 const std::string& theParentId)
31 : ModuleBase_ModelWidget(theParent, theData, theParentId)
33 myContainer = new QWidget(theParent);
34 QHBoxLayout* aControlLay = new QHBoxLayout(myContainer);
35 aControlLay->setContentsMargins(0, 0, 0, 0);
37 QString aLabelText = QString::fromStdString(theData->widgetLabel());
38 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
39 myLabel = new QLabel(aLabelText, myContainer);
40 if (!aLabelIcon.isEmpty())
41 myLabel->setPixmap(QPixmap(aLabelIcon));
42 aControlLay->addWidget(myLabel);
44 mySpinBox = new QDoubleSpinBox(myContainer);
45 QString anObjName = QString::fromStdString(attributeID());
46 mySpinBox->setObjectName(anObjName);
49 std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
50 double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
52 mySpinBox->setMinimum(aMinVal);
54 mySpinBox->setMinimum(-DBL_MAX);
57 aProp = theData->getProperty(DOUBLE_WDG_MAX);
58 double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
60 mySpinBox->setMaximum(aMaxVal);
62 mySpinBox->setMaximum(DBL_MAX);
65 aProp = theData->getProperty(DOUBLE_WDG_STEP);
66 double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
68 mySpinBox->setSingleStep(aStepVal);
71 aProp = theData->getProperty(DOUBLE_WDG_DFLT);
72 double aDefVal = QString::fromStdString(aProp).toDouble(&isOk);
73 myHasDefaultValue = isOk;
75 mySpinBox->setValue(aDefVal);
78 QString aTTip = QString::fromStdString(theData->widgetTooltip());
79 mySpinBox->setToolTip(aTTip);
81 aControlLay->addWidget(mySpinBox);
82 aControlLay->setStretch(1, 1);
84 connect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
86 mySpinBox->installEventFilter(this);
89 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
93 bool ModuleBase_WidgetDoubleValue::storeValue(ObjectPtr theObject) const
95 DataPtr aData = theObject->data();
96 AttributeDoublePtr aReal = aData->real(attributeID());
97 if (aReal->value() != mySpinBox->value()) {
98 aReal->setValue(mySpinBox->value());
99 updateObject(theObject);
104 bool ModuleBase_WidgetDoubleValue::restoreValue(ObjectPtr theObject)
106 DataPtr aData = theObject->data();
107 AttributeDoublePtr aRef = aData->real(attributeID());
109 bool isBlocked = mySpinBox->blockSignals(true);
110 mySpinBox->setValue(aRef->value());
111 mySpinBox->blockSignals(isBlocked);
116 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
118 QList<QWidget*> aList;
119 aList.append(myLabel);
120 aList.append(mySpinBox);
124 bool ModuleBase_WidgetDoubleValue::eventFilter(QObject *theObject, QEvent *theEvent)
126 if (theObject == mySpinBox) {
127 if (theEvent->type() == QEvent::KeyRelease) {
128 QKeyEvent* aKeyEvent = (QKeyEvent*)theEvent;
129 if (aKeyEvent && aKeyEvent->key() == Qt::Key_Return) {
130 emit focusOutWidget(this);
132 emit keyReleased(attributeID(), (QKeyEvent*) theEvent);
136 return ModuleBase_ModelWidget::eventFilter(theObject, theEvent);