Salome HOME
b6dc65443f1cdd964a9ff2dbb9d349aca4a9a351
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetDoubleValue.cpp
1 // Copyright (C) 2014-2022  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <Config_Keywords.h>
21 #include <Config_WidgetAPI.h>
22 #include <Events_Loop.h>
23
24 #include <ModelAPI_AttributeDouble.h>
25 #include <ModelAPI_Data.h>
26 #include <ModelAPI_Object.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Document.h>
29 #include <ModelAPI_ResultParameter.h>
30 #include <ModelAPI_AttributeDouble.h>
31 #include <ModelAPI_Tools.h>
32 #include <ModelAPI_Events.h>
33 #include <ModelAPI_AttributeString.h>
34
35 #include <ModuleBase_ParamSpinBox.h>
36 #include <ModuleBase_Tools.h>
37 #include <ModuleBase_WidgetDoubleValue.h>
38 #include <ModuleBase_IconFactory.h>
39
40 #include <QFormLayout>
41 #include <QLabel>
42 #include <QList>
43 #include <QObject>
44 #include <QPixmap>
45 #include <QString>
46
47 #include <cfloat>
48
49 #ifndef DBL_MAX
50 #define DBL_MAX 1.7976931348623158e+308
51 #endif
52 #ifdef _DEBUG
53 #include <iostream>
54 #endif
55
56 ModuleBase_WidgetDoubleValue::ModuleBase_WidgetDoubleValue(QWidget* theParent,
57                                                            const Config_WidgetAPI* theData)
58     : ModuleBase_ModelWidget(theParent, theData), myHasDefault(false)
59 {
60   QFormLayout* aControlLay = new QFormLayout(this);
61   ModuleBase_Tools::adjustMargins(aControlLay);
62
63   QString aLabelText = translate(theData->widgetLabel());
64   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
65   myLabel = new QLabel(aLabelText, this);
66   if (!aLabelIcon.isEmpty())
67     myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
68
69   bool aAcceptVariables = theData->getBooleanAttribute(DOUBLE_WDG_ACCEPT_EXPRESSIONS, true);
70
71   mySpinBox = new ModuleBase_ParamSpinBox(this);
72   mySpinBox->setAcceptVariables(aAcceptVariables);
73   QString anObjName = QString::fromStdString(attributeID());
74   mySpinBox->setObjectName(anObjName);
75
76   bool isOk = false;
77   std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
78   double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
79   if (isOk) {
80     mySpinBox->setMinimum(aMinVal);
81   } else {
82     mySpinBox->setMinimum(-DBL_MAX);
83   }
84
85   aProp = theData->getProperty(DOUBLE_WDG_MAX);
86   double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
87   if (isOk) {
88     mySpinBox->setMaximum(aMaxVal);
89   } else {
90     mySpinBox->setMaximum(DBL_MAX);
91   }
92
93   aProp = theData->getProperty(DOUBLE_WDG_STEP);
94   double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
95   if (isOk) {
96     double aMinStep = pow(10, -1. * (double) mySpinBox->decimals());
97     if(aStepVal < aMinStep){
98       aStepVal = aMinStep;
99     }
100     mySpinBox->setSingleStep(aStepVal);
101   }
102
103   myDefaultVal = QString::fromStdString(getDefaultValue()).toDouble(&myHasDefault);
104   if (myHasDefault)
105     mySpinBox->setValue(myDefaultVal);
106
107   QString aTTip = translate(theData->widgetTooltip());
108   mySpinBox->setToolTip(aTTip);
109   myLabel->setToolTip(aTTip);
110
111   aControlLay->addRow(myLabel, mySpinBox);
112   // we should listen textChanged signal as valueChanged do not send when text is modified
113   connect(mySpinBox, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
114   mySpinBox->setValueEnabled(isValueEnabled());
115 }
116
117 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
118 {
119 }
120
121 void ModuleBase_WidgetDoubleValue::activateCustom()
122 {
123   ModuleBase_ModelWidget::activateCustom();
124   QStringList aParameters;
125   ModuleBase_Tools::getParameters(aParameters);
126   mySpinBox->setCompletionList(aParameters);
127 }
128
129 bool ModuleBase_WidgetDoubleValue::resetCustom()
130 {
131   bool aDone = false;
132   if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
133     aDone = false;
134   } else {
135     bool isOk;
136     double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
137     // reset the value just if there is a default value definition in the XML definition
138     // if the value can not be found by the default value, do nothing
139     if (isOk) {
140       ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
141       storeValue();
142       aDone = true;
143     }
144   }
145   return aDone;
146 }
147
148 bool ModuleBase_WidgetDoubleValue::storeValueCustom()
149 {
150   DataPtr aData = myFeature->data();
151   AttributeDoublePtr aReal = aData->real(attributeID());
152   if (mySpinBox->hasVariable()) {
153     // Here is a text of a real value or an expression.
154     QString aText = mySpinBox->text();
155     if (aText.contains('=')) {
156       if (!myParameter.get()) {
157         myParameter = ModuleBase_Tools::createParameter(aText);
158         if (!myParameter.get()) {
159           aReal->setExpressionError("Parameter cannot be created");
160           aReal->setExpressionInvalid(true);
161           updateObject(myFeature);
162           return false;
163         } else if (aReal->expressionInvalid()) {
164           aReal->setExpressionError("");
165           aReal->setExpressionInvalid(false);
166         }
167       } else {
168         ModuleBase_Tools::editParameter(myParameter, aText);
169       }
170       aText = aText.split('=').at(0);
171     } else if (myParameter.get()){
172       // Nullyfy the parameter reference without deletion of the created
173       myParameter = FeaturePtr();
174     }
175     aReal->setText(aText.toStdWString());
176   } else {
177     // it is important to set the empty text value to the attribute before set the value
178     // because setValue tries to calculate the attribute value according to the
179     // attribute current text
180     if (aReal->expressionInvalid()) {
181       aReal->setExpressionError("");
182       aReal->setExpressionInvalid(false);
183     }
184     aReal->setText(L"");
185     aReal->setValue(mySpinBox->value());
186   }
187   updateObject(myFeature);
188   return true;
189 }
190
191 bool ModuleBase_WidgetDoubleValue::restoreValueCustom()
192 {
193   DataPtr aData = myFeature->data();
194   AttributeDoublePtr aRef = aData->real(attributeID());
195   std::wstring aTextRepr = aRef->text();
196   if (!aTextRepr.empty()) {
197     QString aText = QString::fromStdWString(aTextRepr);
198     ModuleBase_Tools::setSpinText(mySpinBox, aText);
199   }
200   else {
201     ModuleBase_Tools::setSpinValue(mySpinBox, aRef->isInitialized() ? aRef->value() : myDefaultVal);
202     if (aRef->isInitialized() && aRef->expressionInvalid()) {
203       aRef->setExpressionError("");
204       aRef->setExpressionInvalid(false);
205     }
206     if ((!aRef->isInitialized()) && myHasDefault)
207       aRef->setValue(myDefaultVal);
208   }
209   return true;
210 }
211
212 void ModuleBase_WidgetDoubleValue::selectContent()
213 {
214   mySpinBox->selectAll();
215 }
216
217 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
218 {
219   QList<QWidget*> aList;
220   aList.append(mySpinBox);
221   return aList;
222 }
223
224 bool ModuleBase_WidgetDoubleValue::processEnter()
225 {
226   bool isModified = getValueState() == ModifiedInPP;
227   if (isModified) {
228     emit valuesChanged();
229     mySpinBox->selectAll();
230   }
231   return isModified;
232 }
233
234 bool ModuleBase_WidgetDoubleValue::isModified() const
235 {
236   QString aText = mySpinBox->text();
237   if (aText.isEmpty())
238     return false;
239
240   if (myHasDefault) {
241     bool aOk = false;
242     double aVal = aText.toDouble(&aOk);
243     if (!aOk || aVal == myDefaultVal)
244       return false;
245   }
246   return true;
247 }