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