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 = translate(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 = translate(theData->widgetTooltip());
90 mySpinBox->setToolTip(aTTip);
91 myLabel->setToolTip(aTTip);
93 aControlLay->addRow(myLabel, mySpinBox);
94 // we should listen textChanged signal as valueChanged do not send when text is modified
95 connect(mySpinBox, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
96 mySpinBox->setValueEnabled(isValueEnabled());
99 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
103 void ModuleBase_WidgetDoubleValue::activateCustom()
105 ModuleBase_ModelWidget::activateCustom();
106 #ifdef DEBUG_COMPLETE_WITH_PARAMETERS
107 QStringList aParameters;
108 ModuleBase_Tools::getParameters(aParameters);
109 mySpinBox->setCompletionList(aParameters);
113 bool ModuleBase_WidgetDoubleValue::resetCustom()
116 if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
120 double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
121 // reset the value just if there is a default value definition in the XML definition
122 // if the value can not be found by the default value, do nothing
124 ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
132 bool ModuleBase_WidgetDoubleValue::storeValueCustom()
134 DataPtr aData = myFeature->data();
135 AttributeDoublePtr aReal = aData->real(attributeID());
136 if (mySpinBox->hasVariable()) {
137 // Here is a text of a real value or an expression.
138 std::string aText = mySpinBox->text().toStdString();
139 aReal->setText(aText);
141 // it is important to set the empty text value to the attribute before set the value
142 // because setValue tries to calculate the attribute value according to the
143 // attribute current text
145 aReal->setValue(mySpinBox->value());
147 updateObject(myFeature);
151 bool ModuleBase_WidgetDoubleValue::restoreValueCustom()
153 DataPtr aData = myFeature->data();
154 AttributeDoublePtr aRef = aData->real(attributeID());
155 std::string aTextRepr = aRef->text();
156 if (!aTextRepr.empty()) {
157 ModuleBase_Tools::setSpinText(mySpinBox, QString::fromStdString(aTextRepr));
159 ModuleBase_Tools::setSpinValue(mySpinBox, aRef->isInitialized() ? aRef->value() : 0);
164 void ModuleBase_WidgetDoubleValue::selectContent()
166 mySpinBox->selectAll();
169 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
171 QList<QWidget*> aList;
172 aList.append(mySpinBox);
176 bool ModuleBase_WidgetDoubleValue::processEnter()
178 bool isModified = getValueState() == ModifiedInPP;
180 emit valuesChanged();
181 mySpinBox->selectAll();