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
27 ModuleBase_WidgetDoubleValue::ModuleBase_WidgetDoubleValue(QWidget* theParent,
28 const Config_WidgetAPI* theData,
29 const std::string& theParentId)
30 : ModuleBase_ModelWidget(theParent, theData, theParentId)
32 myContainer = new QWidget(theParent);
33 QHBoxLayout* aControlLay = new QHBoxLayout(myContainer);
34 aControlLay->setContentsMargins(0, 0, 0, 0);
36 QString aLabelText = QString::fromStdString(theData->widgetLabel());
37 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
38 myLabel = new QLabel(aLabelText, myContainer);
39 if (!aLabelIcon.isEmpty())
40 myLabel->setPixmap(QPixmap(aLabelIcon));
41 aControlLay->addWidget(myLabel);
43 mySpinBox = new QDoubleSpinBox(myContainer);
44 QString anObjName = QString::fromStdString(attributeID());
45 mySpinBox->setObjectName(anObjName);
48 std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
49 double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
51 mySpinBox->setMinimum(aMinVal);
53 mySpinBox->setMinimum(-DBL_MAX);
56 aProp = theData->getProperty(DOUBLE_WDG_MAX);
57 double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
59 mySpinBox->setMaximum(aMaxVal);
61 mySpinBox->setMaximum(DBL_MAX);
64 aProp = theData->getProperty(DOUBLE_WDG_STEP);
65 double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
67 mySpinBox->setSingleStep(aStepVal);
70 aProp = theData->getProperty(DOUBLE_WDG_DFLT);
71 double aDefVal = QString::fromStdString(aProp).toDouble(&isOk);
72 myHasDefaultValue = isOk;
74 mySpinBox->setValue(aDefVal);
77 QString aTTip = QString::fromStdString(theData->widgetTooltip());
78 mySpinBox->setToolTip(aTTip);
80 aControlLay->addWidget(mySpinBox);
81 aControlLay->setStretch(1, 1);
83 connect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
85 mySpinBox->installEventFilter(this);
88 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
92 bool ModuleBase_WidgetDoubleValue::storeValue() const
94 DataPtr aData = myFeature->data();
95 AttributeDoublePtr aReal = aData->real(attributeID());
96 if (aReal->value() != mySpinBox->value()) {
97 aReal->setValue(mySpinBox->value());
98 updateObject(myFeature);
103 bool ModuleBase_WidgetDoubleValue::restoreValue()
105 DataPtr aData = myFeature->data();
106 AttributeDoublePtr aRef = aData->real(attributeID());
108 bool isBlocked = mySpinBox->blockSignals(true);
109 mySpinBox->setValue(aRef->value());
110 mySpinBox->blockSignals(isBlocked);
115 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
117 QList<QWidget*> aList;
118 aList.append(myLabel);
119 aList.append(mySpinBox);
123 bool ModuleBase_WidgetDoubleValue::eventFilter(QObject *theObject, QEvent *theEvent)
125 if (theObject == mySpinBox) {
126 if (theEvent->type() == QEvent::KeyRelease) {
127 QKeyEvent* aKeyEvent = (QKeyEvent*) theEvent;
128 if (aKeyEvent && aKeyEvent->key() == Qt::Key_Return) {
129 emit focusOutWidget(this);
131 emit keyReleased(attributeID(), (QKeyEvent*) theEvent);
135 return ModuleBase_ModelWidget::eventFilter(theObject, theEvent);