1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModuleBase_Widgets.h
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>
13 #include <ModelAPI_Events.h>
15 #include <ModuleBase_ParamSpinBox.h>
16 #include <ModuleBase_Tools.h>
17 #include <ModuleBase_WidgetDoubleValue.h>
19 #include <QFormLayout>
29 #define DBL_MAX 1.7976931348623158e+308
35 ModuleBase_WidgetDoubleValue::ModuleBase_WidgetDoubleValue(QWidget* theParent,
36 const Config_WidgetAPI* theData,
37 const std::string& theParentId)
38 : ModuleBase_ModelWidget(theParent, theData, theParentId)
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 mySpinBox = new ModuleBase_ParamSpinBox(this);
50 QString anObjName = QString::fromStdString(attributeID());
51 mySpinBox->setObjectName(anObjName);
54 std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
55 double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
57 mySpinBox->setMinimum(aMinVal);
59 mySpinBox->setMinimum(-DBL_MAX);
62 aProp = theData->getProperty(DOUBLE_WDG_MAX);
63 double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
65 mySpinBox->setMaximum(aMaxVal);
67 mySpinBox->setMaximum(DBL_MAX);
70 aProp = theData->getProperty(DOUBLE_WDG_STEP);
71 double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
73 double aMinStep = pow(10, -1. * (double) mySpinBox->decimals());
74 if(aStepVal < aMinStep){
77 mySpinBox->setSingleStep(aStepVal);
80 double aDefVal = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
82 mySpinBox->setValue(aDefVal);
85 QString aTTip = QString::fromStdString(theData->widgetTooltip());
86 mySpinBox->setToolTip(aTTip);
88 aControlLay->addRow(myLabel, mySpinBox);
89 connect(mySpinBox, SIGNAL(valueChanged(const QString&)), this, SIGNAL(valuesChanged()));
92 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
96 void ModuleBase_WidgetDoubleValue::reset()
98 if (isComputedDefault() || mySpinBox->hasVariable()) {
100 //if (myFeature->compute(myAttributeID))
104 double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
105 // reset the value just if there is a default value definition in the XML definition
106 // if the double value can not be found by the default value, do nothing
108 ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
114 bool ModuleBase_WidgetDoubleValue::storeValueCustom() const
116 DataPtr aData = myFeature->data();
117 AttributeDoublePtr aReal = aData->real(attributeID());
118 if (!mySpinBox->hasVariable()) {
119 aReal->setValue(mySpinBox->value());
122 // Here is a text of a real value or an expression.
123 std::string aText = mySpinBox->text().toStdString();
124 aReal->setText(aText);
125 // Send it to evaluator to convert into the double and store in the attribute
126 static Events_ID anId = ModelAPI_AttributeEvalMessage::eventId();
127 std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
128 std::shared_ptr<ModelAPI_AttributeEvalMessage>(new ModelAPI_AttributeEvalMessage(anId, this));
129 aMessage->setAttribute(aData->attribute(attributeID()));
130 Events_Loop::loop()->send(aMessage);
132 updateObject(myFeature);
136 bool ModuleBase_WidgetDoubleValue::restoreValue()
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 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
151 QList<QWidget*> aList;
152 aList.append(mySpinBox);