1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <Config_Keywords.h>
22 #include <Config_WidgetAPI.h>
23 #include <Events_Loop.h>
25 #include <ModelAPI_AttributeDouble.h>
26 #include <ModelAPI_Data.h>
27 #include <ModelAPI_Object.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Document.h>
30 #include <ModelAPI_ResultParameter.h>
31 #include <ModelAPI_AttributeDouble.h>
32 #include <ModelAPI_Tools.h>
33 #include <ModelAPI_Events.h>
34 #include <ModelAPI_AttributeString.h>
36 #include <ModuleBase_ParamSpinBox.h>
37 #include <ModuleBase_Tools.h>
38 #include <ModuleBase_WidgetDoubleValue.h>
39 #include <ModuleBase_IconFactory.h>
41 #include <QFormLayout>
51 #define DBL_MAX 1.7976931348623158e+308
57 ModuleBase_WidgetDoubleValue::ModuleBase_WidgetDoubleValue(QWidget* theParent,
58 const Config_WidgetAPI* theData)
59 : ModuleBase_ModelWidget(theParent, theData), myHasDefault(false)
61 QFormLayout* aControlLay = new QFormLayout(this);
62 ModuleBase_Tools::adjustMargins(aControlLay);
64 QString aLabelText = translate(theData->widgetLabel());
65 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
66 myLabel = new QLabel(aLabelText, this);
67 if (!aLabelIcon.isEmpty())
68 myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
70 bool aAcceptVariables = theData->getBooleanAttribute(DOUBLE_WDG_ACCEPT_EXPRESSIONS, true);
72 mySpinBox = new ModuleBase_ParamSpinBox(this);
73 mySpinBox->setAcceptVariables(aAcceptVariables);
74 QString anObjName = QString::fromStdString(attributeID());
75 mySpinBox->setObjectName(anObjName);
78 std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
79 double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
81 mySpinBox->setMinimum(aMinVal);
83 mySpinBox->setMinimum(-DBL_MAX);
86 aProp = theData->getProperty(DOUBLE_WDG_MAX);
87 double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
89 mySpinBox->setMaximum(aMaxVal);
91 mySpinBox->setMaximum(DBL_MAX);
94 aProp = theData->getProperty(DOUBLE_WDG_STEP);
95 double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
97 double aMinStep = pow(10, -1. * (double) mySpinBox->decimals());
98 if(aStepVal < aMinStep){
101 mySpinBox->setSingleStep(aStepVal);
104 myDefaultVal = QString::fromStdString(getDefaultValue()).toDouble(&myHasDefault);
106 mySpinBox->setValue(myDefaultVal);
108 QString aTTip = translate(theData->widgetTooltip());
109 mySpinBox->setToolTip(aTTip);
110 myLabel->setToolTip(aTTip);
112 aControlLay->addRow(myLabel, mySpinBox);
113 // we should listen textChanged signal as valueChanged do not send when text is modified
114 connect(mySpinBox, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
115 mySpinBox->setValueEnabled(isValueEnabled());
118 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
122 void ModuleBase_WidgetDoubleValue::activateCustom()
124 ModuleBase_ModelWidget::activateCustom();
125 QStringList aParameters;
126 ModuleBase_Tools::getParameters(aParameters);
127 mySpinBox->setCompletionList(aParameters);
130 bool ModuleBase_WidgetDoubleValue::resetCustom()
133 if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
137 double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
138 // reset the value just if there is a default value definition in the XML definition
139 // if the value can not be found by the default value, do nothing
141 ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
149 bool ModuleBase_WidgetDoubleValue::storeValueCustom()
151 DataPtr aData = myFeature->data();
152 AttributeDoublePtr aReal = aData->real(attributeID());
153 if (mySpinBox->hasVariable()) {
154 // Here is a text of a real value or an expression.
155 QString aText = mySpinBox->text();
156 if (aText.contains('=')) {
157 if (!myParameter.get()) {
158 myParameter = ModuleBase_Tools::createParameter(aText);
159 if (!myParameter.get()) {
160 aReal->setExpressionError("Parameter cannot be created");
161 aReal->setExpressionInvalid(true);
162 updateObject(myFeature);
164 } else if (aReal->expressionInvalid()) {
165 aReal->setExpressionError("");
166 aReal->setExpressionInvalid(false);
169 ModuleBase_Tools::editParameter(myParameter, aText);
171 aText = aText.split('=').at(0);
172 } else if (myParameter.get()){
173 // Nullyfy the parameter reference without deletion of the created
174 myParameter = FeaturePtr();
176 aReal->setText(aText.toStdString());
178 // it is important to set the empty text value to the attribute before set the value
179 // because setValue tries to calculate the attribute value according to the
180 // attribute current text
181 if (aReal->expressionInvalid()) {
182 aReal->setExpressionError("");
183 aReal->setExpressionInvalid(false);
186 aReal->setValue(mySpinBox->value());
188 updateObject(myFeature);
192 bool ModuleBase_WidgetDoubleValue::restoreValueCustom()
194 DataPtr aData = myFeature->data();
195 AttributeDoublePtr aRef = aData->real(attributeID());
196 std::string aTextRepr = aRef->text();
197 if (!aTextRepr.empty()) {
198 QString aText = QString::fromStdString(aTextRepr);
199 //if (aText.endsWith('=')) {
200 // if (!myParameter.get()) {
201 // QString aName = aText.left(aText.indexOf('=')).trimmed();
202 // myParameter = ModuleBase_Tools::findParameter(aName);
204 // /// If myParameter is empty then it was not created because of an error
205 // if (!myParameter.get())
208 // AttributeStringPtr aExprAttr = myParameter->string("expression");
209 // aText += aExprAttr->value().c_str();
211 ModuleBase_Tools::setSpinText(mySpinBox, aText);
214 ModuleBase_Tools::setSpinValue(mySpinBox, aRef->isInitialized() ? aRef->value() : myDefaultVal);
215 if (aRef->isInitialized() && aRef->expressionInvalid()) {
216 aRef->setExpressionError("");
217 aRef->setExpressionInvalid(false);
219 if ((!aRef->isInitialized()) && myHasDefault)
220 aRef->setValue(myDefaultVal);
225 void ModuleBase_WidgetDoubleValue::selectContent()
227 mySpinBox->selectAll();
230 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
232 QList<QWidget*> aList;
233 aList.append(mySpinBox);
237 bool ModuleBase_WidgetDoubleValue::processEnter()
239 bool isModified = getValueState() == ModifiedInPP;
241 emit valuesChanged();
242 mySpinBox->selectAll();