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);
71 mySpinBox->setValue(aDefVal);
74 QString aTTip = QString::fromStdString(theData->widgetTooltip());
75 mySpinBox->setToolTip(aTTip);
77 aControlLay->addWidget(mySpinBox);
78 aControlLay->setStretch(1, 1);
80 connect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
82 mySpinBox->installEventFilter(this);
85 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
89 bool ModuleBase_WidgetDoubleValue::storeValue(FeaturePtr theFeature) const
91 DataPtr aData = theFeature->data();
92 AttributeDoublePtr aReal = aData->real(attributeID());
93 if (aReal->value() != mySpinBox->value()) {
94 aReal->setValue(mySpinBox->value());
95 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
100 bool ModuleBase_WidgetDoubleValue::restoreValue(FeaturePtr theFeature)
102 DataPtr aData = theFeature->data();
103 AttributeDoublePtr aRef = aData->real(attributeID());
105 bool isBlocked = mySpinBox->blockSignals(true);
106 mySpinBox->setValue(aRef->value());
107 mySpinBox->blockSignals(isBlocked);
112 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
114 QList<QWidget*> aList;
115 aList.append(myLabel);
116 aList.append(mySpinBox);
120 bool ModuleBase_WidgetDoubleValue::eventFilter(QObject *theObject, QEvent *theEvent)
122 if (theObject == mySpinBox) {
123 if (theEvent->type() == QEvent::KeyRelease) {
124 emit keyReleased(attributeID(), (QKeyEvent*) theEvent);
128 return ModuleBase_ModelWidget::eventFilter(theObject, theEvent);