Salome HOME
Do not reset the value if the XML definition has no the default key for the attribute.
[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 <ModuleBase_WidgetDoubleValue.h>
8 #include <ModuleBase_ParamSpinBox.h>
9 #include <ModuleBase_Tools.h>
10
11 #include <ModelAPI_AttributeDouble.h>
12 #include <ModelAPI_AttributeString.h>
13 #include <ModelAPI_Data.h>
14
15 #include <Config_Keywords.h>
16 #include <Config_WidgetAPI.h>
17
18 #include <Events_Loop.h>
19 #include <ModelAPI_Events.h>
20
21 #include <QWidget>
22 #include <QFormLayout>
23 #include <QLabel>
24 #include <QEvent>
25 #include <QTimer>
26
27 #include <math.h>
28
29 #ifndef DBL_MAX
30 #define DBL_MAX 1.7976931348623158e+308 
31 #endif
32 #ifdef _DEBUG
33 #include <iostream>
34 #endif
35
36 ModuleBase_WidgetDoubleValue::ModuleBase_WidgetDoubleValue(QWidget* theParent,
37                                                            const Config_WidgetAPI* theData,
38                                                            const std::string& theParentId)
39     : ModuleBase_ModelWidget(theParent, theData, theParentId)
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(QPixmap(aLabelIcon));
49
50   mySpinBox = new ModuleBase_ParamSpinBox(this);
51   QString anObjName = QString::fromStdString(attributeID());
52   mySpinBox->setObjectName(anObjName);
53
54   bool isOk = false;
55   std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
56   double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
57   if (isOk) {
58     mySpinBox->setMinimum(aMinVal);
59   } else {
60     mySpinBox->setMinimum(-DBL_MAX);
61   }
62
63   aProp = theData->getProperty(DOUBLE_WDG_MAX);
64   double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
65   if (isOk) {
66     mySpinBox->setMaximum(aMaxVal);
67   } else {
68     mySpinBox->setMaximum(DBL_MAX);
69   }
70
71   aProp = theData->getProperty(DOUBLE_WDG_STEP);
72   double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
73   if (isOk) {
74     double aMinStep = pow(10, -1. * (double) mySpinBox->decimals());
75     if(aStepVal < aMinStep){
76       aStepVal = aMinStep;
77     }
78     mySpinBox->setSingleStep(aStepVal);
79   }
80
81   double aDefVal = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
82   if (isOk) {
83     mySpinBox->setValue(aDefVal);
84   }
85
86   QString aTTip = QString::fromStdString(theData->widgetTooltip());
87   mySpinBox->setToolTip(aTTip);
88
89   aControlLay->addRow(myLabel, mySpinBox);
90   connect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
91 }
92
93 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
94 {
95 }
96
97 void ModuleBase_WidgetDoubleValue::reset()
98 {
99   if (isComputedDefault()) {
100     return;
101     //if (myFeature->compute(myAttributeID))
102     //  restoreValue();
103   }
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, isOk ? aDefValue : 0.0);
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   aReal->setValue(mySpinBox->value());
121   std::string aTextRepr;
122   if (mySpinBox->hasVariable()) {
123     aTextRepr = mySpinBox->text().toStdString();
124   }
125   aReal->setText(aTextRepr);
126   updateObject(myFeature);
127   return true;
128 }
129
130 bool ModuleBase_WidgetDoubleValue::restoreValue()
131 {
132   DataPtr aData = myFeature->data();
133   AttributeDoublePtr aRef = aData->real(attributeID());
134   std::string aTextRepr = aRef->text();
135   if (!aTextRepr.empty()) {
136     mySpinBox->setText(QString::fromStdString(aTextRepr));
137   } else {
138     ModuleBase_Tools::setSpinValue(mySpinBox, aRef->value());
139   }
140   return true;
141 }
142
143 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
144 {
145   QList<QWidget*> aList;
146   aList.append(mySpinBox);
147   return aList;
148 }