Salome HOME
Double value control improved, the improvement is currently commented.
[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
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 //#define DEBUG_COMPLETE_WITH_PARAMETERS
35
36 ModuleBase_WidgetDoubleValue::ModuleBase_WidgetDoubleValue(QWidget* theParent,
37                                                            const Config_WidgetAPI* theData)
38     : ModuleBase_ModelWidget(theParent, theData)
39 {
40   QFormLayout* aControlLay = new QFormLayout(this);
41   ModuleBase_Tools::adjustMargins(aControlLay);
42
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));
48
49   bool aAcceptVariables = theData->getBooleanAttribute(DOUBLE_WDG_ACCEPT_EXPRESSIONS, true);
50
51   mySpinBox = new ModuleBase_ParamSpinBox(this);
52   mySpinBox->setAcceptVariables(aAcceptVariables);
53   QString anObjName = QString::fromStdString(attributeID());
54   mySpinBox->setObjectName(anObjName);
55
56   bool isOk = false;
57   std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
58   double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
59   if (isOk) {
60     mySpinBox->setMinimum(aMinVal);
61   } else {
62     mySpinBox->setMinimum(-DBL_MAX);
63   }
64
65   aProp = theData->getProperty(DOUBLE_WDG_MAX);
66   double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
67   if (isOk) {
68     mySpinBox->setMaximum(aMaxVal);
69   } else {
70     mySpinBox->setMaximum(DBL_MAX);
71   }
72
73   aProp = theData->getProperty(DOUBLE_WDG_STEP);
74   double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
75   if (isOk) {
76     double aMinStep = pow(10, -1. * (double) mySpinBox->decimals());
77     if(aStepVal < aMinStep){
78       aStepVal = aMinStep;
79     }
80     mySpinBox->setSingleStep(aStepVal);
81   }
82
83   double aDefVal = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
84   if (isOk) {
85     mySpinBox->setValue(aDefVal);
86   }
87
88   QString aTTip = QString::fromStdString(theData->widgetTooltip());
89   mySpinBox->setToolTip(aTTip);
90   myLabel->setToolTip(aTTip);
91
92   aControlLay->addRow(myLabel, mySpinBox);
93   connect(mySpinBox, SIGNAL(valueChanged(const QString&)), this, SIGNAL(valuesModified()));
94 }
95
96 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
97 {
98 }
99
100 void ModuleBase_WidgetDoubleValue::activateCustom()
101 {
102   ModuleBase_ModelWidget::activateCustom();
103 #ifdef DEBUG_COMPLETE_WITH_PARAMETERS
104   QStringList aParameters;
105   ModuleBase_Tools::getParameters(aParameters);
106   mySpinBox->setCompletionList(aParameters);
107 #endif
108 }
109
110 bool ModuleBase_WidgetDoubleValue::resetCustom()
111 {
112   bool aDone = false;
113   if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
114     aDone = false;
115   } else {
116     bool isOk;
117     double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
118     // reset the value just if there is a default value definition in the XML definition
119     // if the value can not be found by the default value, do nothing
120     if (isOk) {
121       ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
122       storeValue();
123       aDone = true;
124     }
125   }
126   return aDone;
127 }
128
129 bool ModuleBase_WidgetDoubleValue::storeValueCustom() const
130 {
131   DataPtr aData = myFeature->data();
132   AttributeDoublePtr aReal = aData->real(attributeID());
133   if (mySpinBox->hasVariable()) {
134     // Here is a text of a real value or an expression.
135     std::string aText = mySpinBox->text().toStdString();
136     aReal->setText(aText);
137   } else {
138     // it is important to set the empty text value to the attribute before set the value
139     // because setValue tries to calculate the attribute value according to the
140     // attribute current text
141     aReal->setText("");
142     aReal->setValue(mySpinBox->value());
143   }
144   updateObject(myFeature);
145   return true;
146 }
147
148 bool ModuleBase_WidgetDoubleValue::restoreValueCustom()
149 {
150   DataPtr aData = myFeature->data();
151   AttributeDoublePtr aRef = aData->real(attributeID());
152   std::string aTextRepr = aRef->text();
153   if (!aTextRepr.empty()) {
154     ModuleBase_Tools::setSpinText(mySpinBox, QString::fromStdString(aTextRepr));
155   } else {
156     ModuleBase_Tools::setSpinValue(mySpinBox, aRef->value());
157   }
158   return true;
159 }
160
161 void ModuleBase_WidgetDoubleValue::selectContent()
162 {
163   mySpinBox->selectAll();
164 }
165
166 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
167 {
168   QList<QWidget*> aList;
169   aList.append(mySpinBox);
170   return aList;
171 }
172
173 bool ModuleBase_WidgetDoubleValue::processEnter()
174 {
175   bool isModified = getValueState() == ModifiedInPP;
176   if (isModified) {
177     emit valuesChanged();
178     mySpinBox->selectAll();
179   }
180   return isModified;
181 }