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 //#define DEBUG_COMPLETE_WITH_PARAMETERS
36 ModuleBase_WidgetDoubleValue::ModuleBase_WidgetDoubleValue(QWidget* theParent,
37 const Config_WidgetAPI* theData)
38 : ModuleBase_ModelWidget(theParent, theData)
40 QFormLayout* aControlLay = new QFormLayout(this);
41 ModuleBase_Tools::adjustMargins(aControlLay);
43 QString aLabelText = QString::fromStdString(theData->widgetLabel());
44 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
45 myLabel = new QLabel(aLabelText, this);
46 if (!aLabelIcon.isEmpty())
47 myLabel->setPixmap(QPixmap(aLabelIcon));
49 bool aAcceptVariables = theData->getBooleanAttribute(DOUBLE_WDG_ACCEPT_EXPRESSIONS, true);
51 mySpinBox = new ModuleBase_ParamSpinBox(this);
52 mySpinBox->setAcceptVariables(aAcceptVariables);
53 QString anObjName = QString::fromStdString(attributeID());
54 mySpinBox->setObjectName(anObjName);
57 std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
58 double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
60 mySpinBox->setMinimum(aMinVal);
62 mySpinBox->setMinimum(-DBL_MAX);
65 aProp = theData->getProperty(DOUBLE_WDG_MAX);
66 double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
68 mySpinBox->setMaximum(aMaxVal);
70 mySpinBox->setMaximum(DBL_MAX);
73 aProp = theData->getProperty(DOUBLE_WDG_STEP);
74 double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
76 double aMinStep = pow(10, -1. * (double) mySpinBox->decimals());
77 if(aStepVal < aMinStep){
80 mySpinBox->setSingleStep(aStepVal);
83 double aDefVal = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
85 mySpinBox->setValue(aDefVal);
88 QString aTTip = QString::fromStdString(theData->widgetTooltip());
89 mySpinBox->setToolTip(aTTip);
90 myLabel->setToolTip(aTTip);
92 aControlLay->addRow(myLabel, mySpinBox);
93 connect(mySpinBox, SIGNAL(valueChanged(const QString&)), this, SIGNAL(valuesModified()));
96 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
100 void ModuleBase_WidgetDoubleValue::activateCustom()
102 ModuleBase_ModelWidget::activateCustom();
103 #ifdef DEBUG_COMPLETE_WITH_PARAMETERS
104 QStringList aParameters;
105 ModuleBase_Tools::getParameters(aParameters);
106 mySpinBox->setCompletionList(aParameters);
110 bool ModuleBase_WidgetDoubleValue::resetCustom()
113 if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
117 double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
118 // reset the value just if there is a default value definition in the XML definition
119 // if the value can not be found by the default value, do nothing
121 ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
129 bool ModuleBase_WidgetDoubleValue::storeValueCustom()
131 DataPtr aData = myFeature->data();
132 AttributeDoublePtr aReal = aData->real(attributeID());
133 if (mySpinBox->hasVariable()) {
134 // Here is a text of a real value or an expression.
135 std::string aText = mySpinBox->text().toStdString();
136 aReal->setText(aText);
138 // it is important to set the empty text value to the attribute before set the value
139 // because setValue tries to calculate the attribute value according to the
140 // attribute current text
142 aReal->setValue(mySpinBox->value());
144 updateObject(myFeature);
148 bool ModuleBase_WidgetDoubleValue::restoreValueCustom()
150 DataPtr aData = myFeature->data();
151 AttributeDoublePtr aRef = aData->real(attributeID());
152 std::string aTextRepr = aRef->text();
153 if (!aTextRepr.empty()) {
154 ModuleBase_Tools::setSpinText(mySpinBox, QString::fromStdString(aTextRepr));
156 ModuleBase_Tools::setSpinValue(mySpinBox, aRef->value());
161 void ModuleBase_WidgetDoubleValue::selectContent()
163 mySpinBox->selectAll();
166 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
168 QList<QWidget*> aList;
169 aList.append(mySpinBox);
173 bool ModuleBase_WidgetDoubleValue::processEnter()
175 bool isModified = getValueState() == ModifiedInPP;
177 emit valuesChanged();
178 mySpinBox->selectAll();