Salome HOME
Fix for issue #562 : correct update by dependencies in the parameters
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetDoubleValue.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_Widgets.h
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
18 #include <QFormLayout>
19 #include <QLabel>
20 #include <QList>
21 #include <QObject>
22 #include <QPixmap>
23 #include <QString>
24
25 #include <cfloat>
26
27 #ifndef DBL_MAX
28 #define DBL_MAX 1.7976931348623158e+308 
29 #endif
30 #ifdef _DEBUG
31 #include <iostream>
32 #endif
33
34 ModuleBase_WidgetDoubleValue::ModuleBase_WidgetDoubleValue(QWidget* theParent,
35                                                            const Config_WidgetAPI* theData,
36                                                            const std::string& theParentId)
37     : ModuleBase_ModelWidget(theParent, theData, theParentId)
38 {
39   QFormLayout* aControlLay = new QFormLayout(this);
40   ModuleBase_Tools::adjustMargins(aControlLay);
41
42   QString aLabelText = QString::fromStdString(theData->widgetLabel());
43   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
44   myLabel = new QLabel(aLabelText, this);
45   if (!aLabelIcon.isEmpty())
46     myLabel->setPixmap(QPixmap(aLabelIcon));
47
48   mySpinBox = new ModuleBase_ParamSpinBox(this);
49   QString anObjName = QString::fromStdString(attributeID());
50   mySpinBox->setObjectName(anObjName);
51
52   bool isOk = false;
53   std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
54   double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
55   if (isOk) {
56     mySpinBox->setMinimum(aMinVal);
57   } else {
58     mySpinBox->setMinimum(-DBL_MAX);
59   }
60
61   aProp = theData->getProperty(DOUBLE_WDG_MAX);
62   double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
63   if (isOk) {
64     mySpinBox->setMaximum(aMaxVal);
65   } else {
66     mySpinBox->setMaximum(DBL_MAX);
67   }
68
69   aProp = theData->getProperty(DOUBLE_WDG_STEP);
70   double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
71   if (isOk) {
72     double aMinStep = pow(10, -1. * (double) mySpinBox->decimals());
73     if(aStepVal < aMinStep){
74       aStepVal = aMinStep;
75     }
76     mySpinBox->setSingleStep(aStepVal);
77   }
78
79   double aDefVal = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
80   if (isOk) {
81     mySpinBox->setValue(aDefVal);
82   }
83
84   QString aTTip = QString::fromStdString(theData->widgetTooltip());
85   mySpinBox->setToolTip(aTTip);
86
87   aControlLay->addRow(myLabel, mySpinBox);
88   connect(mySpinBox, SIGNAL(valueChanged(const QString&)), this, SIGNAL(valuesChanged()));
89 }
90
91 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
92 {
93 }
94
95 void ModuleBase_WidgetDoubleValue::reset()
96 {
97   if (!isUseReset())
98     return;
99
100   if (isComputedDefault() || mySpinBox->hasVariable()) {
101     return;
102     //if (myFeature->compute(myAttributeID))
103     //  restoreValue();
104   } else {
105     bool isOk;
106     double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
107     // reset the value just if there is a default value definition in the XML definition
108     // if the double value can not be found by the default value, do nothing
109     if (isOk) {
110       ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
111       storeValueCustom();
112     }
113   }
114 }
115
116 bool ModuleBase_WidgetDoubleValue::storeValueCustom() const
117 {
118   DataPtr aData = myFeature->data();
119   AttributeDoublePtr aReal = aData->real(attributeID());
120   if (!mySpinBox->hasVariable()) {
121     aReal->setValue(mySpinBox->value());
122     aReal->setText("");
123   } else {
124     // Here is a text of a real value or an expression.
125     std::string aText = mySpinBox->text().toStdString();
126     aReal->setText(aText);
127   }
128   updateObject(myFeature);
129   return true;
130 }
131
132 bool ModuleBase_WidgetDoubleValue::restoreValue()
133 {
134   DataPtr aData = myFeature->data();
135   AttributeDoublePtr aRef = aData->real(attributeID());
136   std::string aTextRepr = aRef->text();
137   if (!aTextRepr.empty()) {
138     ModuleBase_Tools::setSpinText(mySpinBox, QString::fromStdString(aTextRepr));
139   } else {
140     ModuleBase_Tools::setSpinValue(mySpinBox, aRef->value());
141   }
142   return true;
143 }
144
145 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
146 {
147   QList<QWidget*> aList;
148   aList.append(mySpinBox);
149   return aList;
150 }