1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModuleBase_WidgetDoubleValue.cpp
4 // Created: 04 June 2014
5 // Author: Vitaly Smetannikov
7 #include <Config_Keywords.h>
8 #include <Config_WidgetAPI.h>
10 #include <ModelAPI_AttributeDouble.h>
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_Object.h>
14 #include <ModuleBase_ParamSpinBox.h>
15 #include <ModuleBase_Tools.h>
16 #include <ModuleBase_WidgetDoubleValue.h>
18 #include <QFormLayout>
28 #define DBL_MAX 1.7976931348623158e+308
34 ModuleBase_WidgetDoubleValue::ModuleBase_WidgetDoubleValue(QWidget* theParent,
35 const Config_WidgetAPI* theData)
36 : ModuleBase_ModelWidget(theParent, theData)
38 QFormLayout* aControlLay = new QFormLayout(this);
39 ModuleBase_Tools::adjustMargins(aControlLay);
41 QString aLabelText = QString::fromStdString(theData->widgetLabel());
42 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
43 myLabel = new QLabel(aLabelText, this);
44 if (!aLabelIcon.isEmpty())
45 myLabel->setPixmap(QPixmap(aLabelIcon));
47 bool aAcceptVariables = theData->getBooleanAttribute(DOUBLE_WDG_ACCEPT_EXPRESSIONS, true);
49 mySpinBox = new ModuleBase_ParamSpinBox(this);
50 mySpinBox->setAcceptVariables(aAcceptVariables);
51 QString anObjName = QString::fromStdString(attributeID());
52 mySpinBox->setObjectName(anObjName);
55 std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
56 double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
58 mySpinBox->setMinimum(aMinVal);
60 mySpinBox->setMinimum(-DBL_MAX);
63 aProp = theData->getProperty(DOUBLE_WDG_MAX);
64 double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
66 mySpinBox->setMaximum(aMaxVal);
68 mySpinBox->setMaximum(DBL_MAX);
71 aProp = theData->getProperty(DOUBLE_WDG_STEP);
72 double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
74 double aMinStep = pow(10, -1. * (double) mySpinBox->decimals());
75 if(aStepVal < aMinStep){
78 mySpinBox->setSingleStep(aStepVal);
81 double aDefVal = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
83 mySpinBox->setValue(aDefVal);
86 QString aTTip = QString::fromStdString(theData->widgetTooltip());
87 mySpinBox->setToolTip(aTTip);
88 myLabel->setToolTip(aTTip);
90 aControlLay->addRow(myLabel, mySpinBox);
91 connect(mySpinBox, SIGNAL(valueChanged(const QString&)), this, SIGNAL(valuesModified()));
94 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
98 bool ModuleBase_WidgetDoubleValue::resetCustom()
101 if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
105 double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
106 // reset the value just if there is a default value definition in the XML definition
107 // if the value can not be found by the default value, do nothing
109 ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
117 bool ModuleBase_WidgetDoubleValue::storeValueCustom() const
119 DataPtr aData = myFeature->data();
120 AttributeDoublePtr aReal = aData->real(attributeID());
121 if (mySpinBox->hasVariable()) {
122 // Here is a text of a real value or an expression.
123 std::string aText = mySpinBox->text().toStdString();
124 aReal->setText(aText);
126 // it is important to set the empty text value to the attribute before set the value
127 // because setValue tries to calculate the attribute value according to the
128 // attribute current text
130 aReal->setValue(mySpinBox->value());
132 updateObject(myFeature);
136 bool ModuleBase_WidgetDoubleValue::restoreValueCustom()
138 DataPtr aData = myFeature->data();
139 AttributeDoublePtr aRef = aData->real(attributeID());
140 std::string aTextRepr = aRef->text();
141 if (!aTextRepr.empty()) {
142 ModuleBase_Tools::setSpinText(mySpinBox, QString::fromStdString(aTextRepr));
144 ModuleBase_Tools::setSpinValue(mySpinBox, aRef->value());
149 void ModuleBase_WidgetDoubleValue::selectContent()
151 mySpinBox->selectAll();
154 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
156 QList<QWidget*> aList;
157 aList.append(mySpinBox);
161 bool ModuleBase_WidgetDoubleValue::processEnter()
163 bool isModified = getValueState() == ModifiedInPP;
165 emit valuesChanged();
166 mySpinBox->selectAll();