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>
17 #include <ModuleBase_IconFactory.h>
19 #include <QFormLayout>
29 #define DBL_MAX 1.7976931348623158e+308
35 //#define DEBUG_COMPLETE_WITH_PARAMETERS
37 ModuleBase_WidgetDoubleValue::ModuleBase_WidgetDoubleValue(QWidget* theParent,
38 const Config_WidgetAPI* theData)
39 : ModuleBase_ModelWidget(theParent, theData)
41 QFormLayout* aControlLay = new QFormLayout(this);
42 ModuleBase_Tools::adjustMargins(aControlLay);
44 QString aLabelText = QString::fromStdString(theData->widgetLabel());
45 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
46 myLabel = new QLabel(aLabelText, this);
47 if (!aLabelIcon.isEmpty())
48 myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
50 bool aAcceptVariables = theData->getBooleanAttribute(DOUBLE_WDG_ACCEPT_EXPRESSIONS, true);
52 mySpinBox = new ModuleBase_ParamSpinBox(this);
53 mySpinBox->setAcceptVariables(aAcceptVariables);
54 QString anObjName = QString::fromStdString(attributeID());
55 mySpinBox->setObjectName(anObjName);
58 std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
59 double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
61 mySpinBox->setMinimum(aMinVal);
63 mySpinBox->setMinimum(-DBL_MAX);
66 aProp = theData->getProperty(DOUBLE_WDG_MAX);
67 double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
69 mySpinBox->setMaximum(aMaxVal);
71 mySpinBox->setMaximum(DBL_MAX);
74 aProp = theData->getProperty(DOUBLE_WDG_STEP);
75 double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
77 double aMinStep = pow(10, -1. * (double) mySpinBox->decimals());
78 if(aStepVal < aMinStep){
81 mySpinBox->setSingleStep(aStepVal);
84 double aDefVal = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
86 mySpinBox->setValue(aDefVal);
89 QString aTTip = QString::fromStdString(theData->widgetTooltip());
90 mySpinBox->setToolTip(aTTip);
91 myLabel->setToolTip(aTTip);
93 aControlLay->addRow(myLabel, mySpinBox);
94 connect(mySpinBox, SIGNAL(valueChanged(const QString&)), this, SIGNAL(valuesModified()));
97 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
101 void ModuleBase_WidgetDoubleValue::activateCustom()
103 ModuleBase_ModelWidget::activateCustom();
104 #ifdef DEBUG_COMPLETE_WITH_PARAMETERS
105 QStringList aParameters;
106 ModuleBase_Tools::getParameters(aParameters);
107 mySpinBox->setCompletionList(aParameters);
111 bool ModuleBase_WidgetDoubleValue::resetCustom()
114 if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
118 double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
119 // reset the value just if there is a default value definition in the XML definition
120 // if the value can not be found by the default value, do nothing
122 ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
130 bool ModuleBase_WidgetDoubleValue::storeValueCustom()
132 DataPtr aData = myFeature->data();
133 AttributeDoublePtr aReal = aData->real(attributeID());
134 if (mySpinBox->hasVariable()) {
135 // Here is a text of a real value or an expression.
136 std::string aText = mySpinBox->text().toStdString();
137 aReal->setText(aText);
139 // it is important to set the empty text value to the attribute before set the value
140 // because setValue tries to calculate the attribute value according to the
141 // attribute current text
143 aReal->setValue(mySpinBox->value());
145 updateObject(myFeature);
149 bool ModuleBase_WidgetDoubleValue::restoreValueCustom()
151 DataPtr aData = myFeature->data();
152 AttributeDoublePtr aRef = aData->real(attributeID());
153 std::string aTextRepr = aRef->text();
154 if (!aTextRepr.empty()) {
155 ModuleBase_Tools::setSpinText(mySpinBox, QString::fromStdString(aTextRepr));
157 ModuleBase_Tools::setSpinValue(mySpinBox, aRef->value());
162 void ModuleBase_WidgetDoubleValue::selectContent()
164 mySpinBox->selectAll();
167 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
169 QList<QWidget*> aList;
170 aList.append(mySpinBox);
174 bool ModuleBase_WidgetDoubleValue::processEnter()
176 bool isModified = getValueState() == ModifiedInPP;
178 emit valuesChanged();
179 mySpinBox->selectAll();