Salome HOME
Avoid of reference to not-initialized attributes values
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetDoubleValue.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_WidgetDoubleValue.cpp
4 // Created:     04 June 2014
5 // Author:      Vitaly Smetannikov
6
7 #include <Config_Keywords.h>
8 #include <Config_WidgetAPI.h>
9
10 #include <ModelAPI_AttributeDouble.h>
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_Object.h>
13
14 #include <ModuleBase_ParamSpinBox.h>
15 #include <ModuleBase_Tools.h>
16 #include <ModuleBase_WidgetDoubleValue.h>
17 #include <ModuleBase_IconFactory.h>
18
19 #include <QFormLayout>
20 #include <QLabel>
21 #include <QList>
22 #include <QObject>
23 #include <QPixmap>
24 #include <QString>
25
26 #include <cfloat>
27
28 #ifndef DBL_MAX
29 #define DBL_MAX 1.7976931348623158e+308 
30 #endif
31 #ifdef _DEBUG
32 #include <iostream>
33 #endif
34
35 //#define DEBUG_COMPLETE_WITH_PARAMETERS
36
37 ModuleBase_WidgetDoubleValue::ModuleBase_WidgetDoubleValue(QWidget* theParent,
38                                                            const Config_WidgetAPI* theData)
39     : ModuleBase_ModelWidget(theParent, theData)
40 {
41   QFormLayout* aControlLay = new QFormLayout(this);
42   ModuleBase_Tools::adjustMargins(aControlLay);
43
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));
49
50   bool aAcceptVariables = theData->getBooleanAttribute(DOUBLE_WDG_ACCEPT_EXPRESSIONS, true);
51
52   mySpinBox = new ModuleBase_ParamSpinBox(this);
53   mySpinBox->setAcceptVariables(aAcceptVariables);
54   QString anObjName = QString::fromStdString(attributeID());
55   mySpinBox->setObjectName(anObjName);
56
57   bool isOk = false;
58   std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
59   double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
60   if (isOk) {
61     mySpinBox->setMinimum(aMinVal);
62   } else {
63     mySpinBox->setMinimum(-DBL_MAX);
64   }
65
66   aProp = theData->getProperty(DOUBLE_WDG_MAX);
67   double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
68   if (isOk) {
69     mySpinBox->setMaximum(aMaxVal);
70   } else {
71     mySpinBox->setMaximum(DBL_MAX);
72   }
73
74   aProp = theData->getProperty(DOUBLE_WDG_STEP);
75   double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
76   if (isOk) {
77     double aMinStep = pow(10, -1. * (double) mySpinBox->decimals());
78     if(aStepVal < aMinStep){
79       aStepVal = aMinStep;
80     }
81     mySpinBox->setSingleStep(aStepVal);
82   }
83
84   double aDefVal = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
85   if (isOk) {
86     mySpinBox->setValue(aDefVal);
87   }
88
89   QString aTTip = QString::fromStdString(theData->widgetTooltip());
90   mySpinBox->setToolTip(aTTip);
91   myLabel->setToolTip(aTTip);
92
93   aControlLay->addRow(myLabel, mySpinBox);
94   connect(mySpinBox, SIGNAL(valueChanged(const QString&)), this, SIGNAL(valuesModified()));
95   mySpinBox->setValueEnabled(isValueEnabled());
96 }
97
98 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
99 {
100 }
101
102 void ModuleBase_WidgetDoubleValue::activateCustom()
103 {
104   ModuleBase_ModelWidget::activateCustom();
105 #ifdef DEBUG_COMPLETE_WITH_PARAMETERS
106   QStringList aParameters;
107   ModuleBase_Tools::getParameters(aParameters);
108   mySpinBox->setCompletionList(aParameters);
109 #endif
110 }
111
112 bool ModuleBase_WidgetDoubleValue::resetCustom()
113 {
114   bool aDone = false;
115   if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
116     aDone = false;
117   } else {
118     bool isOk;
119     double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
120     // reset the value just if there is a default value definition in the XML definition
121     // if the value can not be found by the default value, do nothing
122     if (isOk) {
123       ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
124       storeValue();
125       aDone = true;
126     }
127   }
128   return aDone;
129 }
130
131 bool ModuleBase_WidgetDoubleValue::storeValueCustom()
132 {
133   DataPtr aData = myFeature->data();
134   AttributeDoublePtr aReal = aData->real(attributeID());
135   if (mySpinBox->hasVariable()) {
136     // Here is a text of a real value or an expression.
137     std::string aText = mySpinBox->text().toStdString();
138     aReal->setText(aText);
139   } else {
140     // it is important to set the empty text value to the attribute before set the value
141     // because setValue tries to calculate the attribute value according to the
142     // attribute current text
143     aReal->setText("");
144     aReal->setValue(mySpinBox->value());
145   }
146   updateObject(myFeature);
147   return true;
148 }
149
150 bool ModuleBase_WidgetDoubleValue::restoreValueCustom()
151 {
152   DataPtr aData = myFeature->data();
153   AttributeDoublePtr aRef = aData->real(attributeID());
154   std::string aTextRepr = aRef->text();
155   if (!aTextRepr.empty()) {
156     ModuleBase_Tools::setSpinText(mySpinBox, QString::fromStdString(aTextRepr));
157   } else {
158     ModuleBase_Tools::setSpinValue(mySpinBox, aRef->isInitialized() ? aRef->value() : 0);
159   }
160   return true;
161 }
162
163 void ModuleBase_WidgetDoubleValue::selectContent()
164 {
165   mySpinBox->selectAll();
166 }
167
168 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
169 {
170   QList<QWidget*> aList;
171   aList.append(mySpinBox);
172   return aList;
173 }
174
175 bool ModuleBase_WidgetDoubleValue::processEnter()
176 {
177   bool isModified = getValueState() == ModifiedInPP;
178   if (isModified) {
179     emit valuesChanged();
180     mySpinBox->selectAll();
181   }
182   return isModified;
183 }