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 #define DBL_MAX 1.7976931348623158e+308
28 ModuleBase_WidgetDoubleValue::ModuleBase_WidgetDoubleValue(QWidget* theParent, const Config_WidgetAPI* theData)
29 : ModuleBase_ModelWidget(theParent, theData)
31 myContainer = new QWidget(theParent);
32 QHBoxLayout* aControlLay = new QHBoxLayout(myContainer);
33 aControlLay->setContentsMargins(0, 0, 0, 0);
35 QString aLabelText = QString::fromStdString(theData->widgetLabel());
36 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
37 myLabel = new QLabel(aLabelText, myContainer);
38 myLabel->setPixmap(QPixmap(aLabelIcon));
39 aControlLay->addWidget(myLabel);
41 mySpinBox = new QDoubleSpinBox(myContainer);
42 QString anObjName = QString::fromStdString(attributeID());
43 mySpinBox->setObjectName(anObjName);
46 std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
47 double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
49 mySpinBox->setMinimum(aMinVal);
51 mySpinBox->setMinimum(-DBL_MAX);
54 aProp = theData->getProperty(DOUBLE_WDG_MAX);
55 double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
57 mySpinBox->setMaximum(aMaxVal);
59 mySpinBox->setMaximum(DBL_MAX);
62 aProp = theData->getProperty(DOUBLE_WDG_STEP);
63 double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
65 mySpinBox->setSingleStep(aStepVal);
68 aProp = theData->getProperty(DOUBLE_WDG_DFLT);
69 double aDefVal = QString::fromStdString(aProp).toDouble(&isOk);
70 setHasDefaultValue(isOk);
72 mySpinBox->setValue(aDefVal);
75 QString aTTip = QString::fromStdString(theData->widgetTooltip());
76 mySpinBox->setToolTip(aTTip);
78 aControlLay->addWidget(mySpinBox);
79 aControlLay->setStretch(1, 1);
81 connect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
83 mySpinBox->installEventFilter(this);
86 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
90 bool ModuleBase_WidgetDoubleValue::storeValue(FeaturePtr theFeature) const
92 DataPtr aData = theFeature->data();
93 AttributeDoublePtr aReal = aData->real(attributeID());
94 if (aReal->value() != mySpinBox->value()) {
95 aReal->setValue(mySpinBox->value());
96 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
101 bool ModuleBase_WidgetDoubleValue::restoreValue(FeaturePtr theFeature)
103 DataPtr aData = theFeature->data();
104 AttributeDoublePtr aRef = aData->real(attributeID());
106 bool isBlocked = mySpinBox->blockSignals(true);
107 mySpinBox->setValue(aRef->value());
108 mySpinBox->blockSignals(isBlocked);
113 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
115 QList<QWidget*> aList;
116 aList.append(myLabel);
117 aList.append(mySpinBox);
121 bool ModuleBase_WidgetDoubleValue::eventFilter(QObject *theObject, QEvent *theEvent)
123 if (theObject == mySpinBox) {
124 if (theEvent->type() == QEvent::KeyRelease) {
125 emit keyReleased(attributeID(), (QKeyEvent*) theEvent);
129 return ModuleBase_ModelWidget::eventFilter(theObject, theEvent);