Salome HOME
9e4a2041e1f19fd0d7b156229f408c0bbbf763ad
[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   // we should listen textChanged signal as valueChanged do not send when text is modified
95   connect(mySpinBox, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
96   mySpinBox->setValueEnabled(isValueEnabled());
97 }
98
99 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
100 {
101 }
102
103 void ModuleBase_WidgetDoubleValue::activateCustom()
104 {
105   ModuleBase_ModelWidget::activateCustom();
106 #ifdef DEBUG_COMPLETE_WITH_PARAMETERS
107   QStringList aParameters;
108   ModuleBase_Tools::getParameters(aParameters);
109   mySpinBox->setCompletionList(aParameters);
110 #endif
111 }
112
113 bool ModuleBase_WidgetDoubleValue::resetCustom()
114 {
115   bool aDone = false;
116   if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
117     aDone = false;
118   } else {
119     bool isOk;
120     double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
121     // reset the value just if there is a default value definition in the XML definition
122     // if the value can not be found by the default value, do nothing
123     if (isOk) {
124       ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
125       storeValue();
126       aDone = true;
127     }
128   }
129   return aDone;
130 }
131
132 bool ModuleBase_WidgetDoubleValue::storeValueCustom()
133 {
134   DataPtr aData = myFeature->data();
135   AttributeDoublePtr aReal = aData->real(attributeID());
136   if (mySpinBox->hasVariable()) {
137     // Here is a text of a real value or an expression.
138     std::string aText = mySpinBox->text().toStdString();
139     aReal->setText(aText);
140   } else {
141     // it is important to set the empty text value to the attribute before set the value
142     // because setValue tries to calculate the attribute value according to the
143     // attribute current text
144     aReal->setText("");
145     aReal->setValue(mySpinBox->value());
146   }
147   updateObject(myFeature);
148   return true;
149 }
150
151 bool ModuleBase_WidgetDoubleValue::restoreValueCustom()
152 {
153   DataPtr aData = myFeature->data();
154   AttributeDoublePtr aRef = aData->real(attributeID());
155   std::string aTextRepr = aRef->text();
156   if (!aTextRepr.empty()) {
157     ModuleBase_Tools::setSpinText(mySpinBox, QString::fromStdString(aTextRepr));
158   } else {
159     ModuleBase_Tools::setSpinValue(mySpinBox, aRef->isInitialized() ? aRef->value() : 0);
160   }
161   return true;
162 }
163
164 void ModuleBase_WidgetDoubleValue::selectContent()
165 {
166   mySpinBox->selectAll();
167 }
168
169 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
170 {
171   QList<QWidget*> aList;
172   aList.append(mySpinBox);
173   return aList;
174 }
175
176 bool ModuleBase_WidgetDoubleValue::processEnter()
177 {
178   bool isModified = getValueState() == ModifiedInPP;
179   if (isModified) {
180     emit valuesChanged();
181     mySpinBox->selectAll();
182   }
183   return isModified;
184 }