1 // Copyright (C) 2014-2016 CEA/DEN, EDF R&D
3 // File: ModuleBase_WidgetCheckDoubleValue.cpp
4 // Created: 16 June 2016
5 // Author: Clarisse Genrault (CEA)
7 #include <Config_Keywords.h>
8 #include <Config_WidgetAPI.h>
10 #include <ModelAPI_AttributeDouble.h>
11 #include <ModelAPI_AttributeBoolean.h>
12 #include <ModelAPI_Data.h>
13 #include <ModelAPI_Object.h>
15 #include <ModuleBase_ParamSpinBox.h>
16 #include <ModuleBase_Tools.h>
17 #include <ModuleBase_WidgetCheckDoubleValue.h>
18 #include <ModuleBase_IconFactory.h>
20 #include <QFormLayout>
30 #define DBL_MAX 1.7976931348623158e+308
36 //#define DEBUG_COMPLETE_WITH_PARAMETERS
38 ModuleBase_WidgetCheckDoubleValue::ModuleBase_WidgetCheckDoubleValue(QWidget* theParent,
39 const Config_WidgetAPI* theData)
40 : ModuleBase_ModelWidget(theParent, theData)
42 QFormLayout* aControlLay = new QFormLayout(this);
43 ModuleBase_Tools::adjustMargins(aControlLay);
45 myCheckAttributeId = theData->getProperty("id_check");
47 QString aCheckText = QString::fromStdString(theData->widgetLabel());
48 myCheckBox = new QCheckBox(aCheckText, this);
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 myCheckBox->setToolTip(aTTip);
93 myCheckBox->setChecked(false);
94 mySpinBox->setEnabled(false);
96 aControlLay->addRow(myCheckBox, mySpinBox);
97 connect(mySpinBox, SIGNAL(valueChanged(const QString&)), this, SIGNAL(valuesModified()));
98 connect(myCheckBox, SIGNAL(toggled(bool)), this, SIGNAL(valuesChanged()));
101 ModuleBase_WidgetCheckDoubleValue::~ModuleBase_WidgetCheckDoubleValue()
105 void ModuleBase_WidgetCheckDoubleValue::activateCustom()
107 ModuleBase_ModelWidget::activateCustom();
108 #ifdef DEBUG_COMPLETE_WITH_PARAMETERS
109 QStringList aParameters;
110 ModuleBase_Tools::getParameters(aParameters);
111 mySpinBox->setCompletionList(aParameters);
115 bool ModuleBase_WidgetCheckDoubleValue::resetCustom()
118 if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
122 double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
123 // reset the value just if there is a default value definition in the XML definition
124 // if the value can not be found by the default value, do nothing
126 ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
134 bool ModuleBase_WidgetCheckDoubleValue::storeValueCustom()
136 DataPtr aData = myFeature->data();
137 AttributeDoublePtr aReal = aData->real(attributeID());
138 AttributeBooleanPtr aBoolean = aData->boolean(myCheckAttributeId);
140 if (mySpinBox->hasVariable()) {
141 // Here is a text of a real value or an expression.
142 std::string aText = mySpinBox->text().toStdString();
143 aReal->setText(aText);
145 // it is important to set the empty text value to the attribute before set the value
146 // because setValue tries to calculate the attribute value according to the
147 // attribute current text
149 aReal->setValue(mySpinBox->value());
152 // The spinbox is enabled/disabled only if the checkbox is checked/unchecked
153 aBoolean->setValue(myCheckBox->isChecked());
154 mySpinBox->setEnabled(myCheckBox->isChecked());
156 updateObject(myFeature);
160 bool ModuleBase_WidgetCheckDoubleValue::restoreValueCustom()
162 DataPtr aData = myFeature->data();
163 AttributeDoublePtr aRef = aData->real(attributeID());
164 AttributeBooleanPtr aBoolean = aData->boolean(myCheckAttributeId);
165 std::string aTextRepr = aRef->text();
166 if (!aTextRepr.empty()) {
167 ModuleBase_Tools::setSpinText(mySpinBox, QString::fromStdString(aTextRepr));
169 ModuleBase_Tools::setSpinValue(mySpinBox, aRef->value());
172 // The spinbox is enabled/disabled only if the checkbox is checked/unchecked
173 myCheckBox->setChecked(aBoolean->value());
174 mySpinBox->setEnabled(aBoolean->value());
179 void ModuleBase_WidgetCheckDoubleValue::selectContent()
181 mySpinBox->selectAll();
184 QList<QWidget*> ModuleBase_WidgetCheckDoubleValue::getControls() const
186 QList<QWidget*> aList;
187 aList.append(mySpinBox);
188 aList.append(myCheckBox);
192 bool ModuleBase_WidgetCheckDoubleValue::processEnter()
194 bool isModified = getValueState() == ModifiedInPP;
196 emit valuesChanged();
197 mySpinBox->selectAll();