1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <Config_Keywords.h>
21 #include <Config_WidgetAPI.h>
22 #include <Events_Loop.h>
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>
35 #include <ModuleBase_ParamSpinBox.h>
36 #include <ModuleBase_Tools.h>
37 #include <ModuleBase_WidgetDoubleValue.h>
38 #include <ModuleBase_IconFactory.h>
40 #include <QFormLayout>
50 #define DBL_MAX 1.7976931348623158e+308
56 ModuleBase_WidgetDoubleValue::ModuleBase_WidgetDoubleValue(QWidget* theParent,
57 const Config_WidgetAPI* theData)
58 : ModuleBase_ModelWidget(theParent, theData), myHasDefault(false)
60 QFormLayout* aControlLay = new QFormLayout(this);
61 ModuleBase_Tools::adjustMargins(aControlLay);
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));
69 bool aAcceptVariables = theData->getBooleanAttribute(DOUBLE_WDG_ACCEPT_EXPRESSIONS, true);
71 mySpinBox = new ModuleBase_ParamSpinBox(this);
72 mySpinBox->setAcceptVariables(aAcceptVariables);
73 QString anObjName = QString::fromStdString(attributeID());
74 mySpinBox->setObjectName(anObjName);
77 std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
78 double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
80 mySpinBox->setMinimum(aMinVal);
82 mySpinBox->setMinimum(-DBL_MAX);
85 aProp = theData->getProperty(DOUBLE_WDG_MAX);
86 double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
88 mySpinBox->setMaximum(aMaxVal);
90 mySpinBox->setMaximum(DBL_MAX);
93 aProp = theData->getProperty(DOUBLE_WDG_STEP);
94 double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
96 double aMinStep = pow(10, -1. * (double) mySpinBox->decimals());
97 if(aStepVal < aMinStep){
100 mySpinBox->setSingleStep(aStepVal);
103 myDefaultVal = QString::fromStdString(getDefaultValue()).toDouble(&myHasDefault);
105 mySpinBox->setValue(myDefaultVal);
107 QString aTTip = translate(theData->widgetTooltip());
108 mySpinBox->setToolTip(aTTip);
109 myLabel->setToolTip(aTTip);
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());
117 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
121 void ModuleBase_WidgetDoubleValue::activateCustom()
123 ModuleBase_ModelWidget::activateCustom();
124 QStringList aParameters;
125 ModuleBase_Tools::getParameters(aParameters);
126 mySpinBox->setCompletionList(aParameters);
129 bool ModuleBase_WidgetDoubleValue::resetCustom()
132 if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
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
140 ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
148 bool ModuleBase_WidgetDoubleValue::storeValueCustom()
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);
163 } else if (aReal->expressionInvalid()) {
164 aReal->setExpressionError("");
165 aReal->setExpressionInvalid(false);
168 ModuleBase_Tools::editParameter(myParameter, aText);
170 aText = aText.split('=').at(0);
171 } else if (myParameter.get()){
172 // Nullyfy the parameter reference without deletion of the created
173 myParameter = FeaturePtr();
175 aReal->setText(aText.toStdString());
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);
185 aReal->setValue(mySpinBox->value());
187 updateObject(myFeature);
191 bool ModuleBase_WidgetDoubleValue::restoreValueCustom()
193 DataPtr aData = myFeature->data();
194 AttributeDoublePtr aRef = aData->real(attributeID());
195 std::string aTextRepr = aRef->text();
196 if (!aTextRepr.empty()) {
197 QString aText = QString::fromStdString(aTextRepr);
198 //if (aText.endsWith('=')) {
199 // if (!myParameter.get()) {
200 // QString aName = aText.left(aText.indexOf('=')).trimmed();
201 // myParameter = ModuleBase_Tools::findParameter(aName);
203 // /// If myParameter is empty then it was not created because of an error
204 // if (!myParameter.get())
207 // AttributeStringPtr aExprAttr = myParameter->string("expression");
208 // aText += aExprAttr->value().c_str();
210 ModuleBase_Tools::setSpinText(mySpinBox, aText);
213 ModuleBase_Tools::setSpinValue(mySpinBox, aRef->isInitialized() ? aRef->value() : myDefaultVal);
214 if (aRef->isInitialized() && aRef->expressionInvalid()) {
215 aRef->setExpressionError("");
216 aRef->setExpressionInvalid(false);
218 if ((!aRef->isInitialized()) && myHasDefault)
219 aRef->setValue(myDefaultVal);
224 void ModuleBase_WidgetDoubleValue::selectContent()
226 mySpinBox->selectAll();
229 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
231 QList<QWidget*> aList;
232 aList.append(mySpinBox);
236 bool ModuleBase_WidgetDoubleValue::processEnter()
238 bool isModified = getValueState() == ModifiedInPP;
240 emit valuesChanged();
241 mySpinBox->selectAll();