Salome HOME
Correction of Boolean crash
[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 }
96
97 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
98 {
99 }
100
101 void ModuleBase_WidgetDoubleValue::activateCustom()
102 {
103   ModuleBase_ModelWidget::activateCustom();
104 #ifdef DEBUG_COMPLETE_WITH_PARAMETERS
105   QStringList aParameters;
106   ModuleBase_Tools::getParameters(aParameters);
107   mySpinBox->setCompletionList(aParameters);
108 #endif
109 }
110
111 bool ModuleBase_WidgetDoubleValue::resetCustom()
112 {
113   bool aDone = false;
114   if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
115     aDone = false;
116   } else {
117     bool isOk;
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
121     if (isOk) {
122       ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
123       storeValue();
124       aDone = true;
125     }
126   }
127   return aDone;
128 }
129
130 bool ModuleBase_WidgetDoubleValue::storeValueCustom()
131 {
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);
138   } else {
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
142     aReal->setText("");
143     aReal->setValue(mySpinBox->value());
144   }
145   updateObject(myFeature);
146   return true;
147 }
148
149 bool ModuleBase_WidgetDoubleValue::restoreValueCustom()
150 {
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));
156   } else {
157     ModuleBase_Tools::setSpinValue(mySpinBox, aRef->value());
158   }
159   return true;
160 }
161
162 void ModuleBase_WidgetDoubleValue::selectContent()
163 {
164   mySpinBox->selectAll();
165 }
166
167 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
168 {
169   QList<QWidget*> aList;
170   aList.append(mySpinBox);
171   return aList;
172 }
173
174 bool ModuleBase_WidgetDoubleValue::processEnter()
175 {
176   bool isModified = getValueState() == ModifiedInPP;
177   if (isModified) {
178     emit valuesChanged();
179     mySpinBox->selectAll();
180   }
181   return isModified;
182 }