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>
24 #include <ModelAPI_AttributeDouble.h>
25 #include <ModelAPI_Data.h>
26 #include <ModelAPI_Object.h>
28 #include <ModuleBase_ParamSpinBox.h>
29 #include <ModuleBase_Tools.h>
30 #include <ModuleBase_WidgetDoubleValue.h>
31 #include <ModuleBase_IconFactory.h>
33 #include <QFormLayout>
43 #define DBL_MAX 1.7976931348623158e+308
49 //#define DEBUG_COMPLETE_WITH_PARAMETERS
51 ModuleBase_WidgetDoubleValue::ModuleBase_WidgetDoubleValue(QWidget* theParent,
52 const Config_WidgetAPI* theData)
53 : ModuleBase_ModelWidget(theParent, theData)
55 QFormLayout* aControlLay = new QFormLayout(this);
56 ModuleBase_Tools::adjustMargins(aControlLay);
58 QString aLabelText = translate(theData->widgetLabel());
59 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
60 myLabel = new QLabel(aLabelText, this);
61 if (!aLabelIcon.isEmpty())
62 myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
64 bool aAcceptVariables = theData->getBooleanAttribute(DOUBLE_WDG_ACCEPT_EXPRESSIONS, true);
66 mySpinBox = new ModuleBase_ParamSpinBox(this);
67 mySpinBox->setAcceptVariables(aAcceptVariables);
68 QString anObjName = QString::fromStdString(attributeID());
69 mySpinBox->setObjectName(anObjName);
72 std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
73 double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
75 mySpinBox->setMinimum(aMinVal);
77 mySpinBox->setMinimum(-DBL_MAX);
80 aProp = theData->getProperty(DOUBLE_WDG_MAX);
81 double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
83 mySpinBox->setMaximum(aMaxVal);
85 mySpinBox->setMaximum(DBL_MAX);
88 aProp = theData->getProperty(DOUBLE_WDG_STEP);
89 double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
91 double aMinStep = pow(10, -1. * (double) mySpinBox->decimals());
92 if(aStepVal < aMinStep){
95 mySpinBox->setSingleStep(aStepVal);
98 double aDefVal = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
100 mySpinBox->setValue(aDefVal);
103 QString aTTip = translate(theData->widgetTooltip());
104 mySpinBox->setToolTip(aTTip);
105 myLabel->setToolTip(aTTip);
107 aControlLay->addRow(myLabel, mySpinBox);
108 // we should listen textChanged signal as valueChanged do not send when text is modified
109 connect(mySpinBox, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
110 mySpinBox->setValueEnabled(isValueEnabled());
113 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
117 void ModuleBase_WidgetDoubleValue::activateCustom()
119 ModuleBase_ModelWidget::activateCustom();
120 #ifdef DEBUG_COMPLETE_WITH_PARAMETERS
121 QStringList aParameters;
122 ModuleBase_Tools::getParameters(aParameters);
123 mySpinBox->setCompletionList(aParameters);
127 bool ModuleBase_WidgetDoubleValue::resetCustom()
130 if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
134 double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
135 // reset the value just if there is a default value definition in the XML definition
136 // if the value can not be found by the default value, do nothing
138 ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
146 bool ModuleBase_WidgetDoubleValue::storeValueCustom()
148 DataPtr aData = myFeature->data();
149 AttributeDoublePtr aReal = aData->real(attributeID());
150 if (mySpinBox->hasVariable()) {
151 // Here is a text of a real value or an expression.
152 std::string aText = mySpinBox->text().toStdString();
153 aReal->setText(aText);
155 // it is important to set the empty text value to the attribute before set the value
156 // because setValue tries to calculate the attribute value according to the
157 // attribute current text
159 aReal->setValue(mySpinBox->value());
161 updateObject(myFeature);
165 bool ModuleBase_WidgetDoubleValue::restoreValueCustom()
167 DataPtr aData = myFeature->data();
168 AttributeDoublePtr aRef = aData->real(attributeID());
169 std::string aTextRepr = aRef->text();
170 if (!aTextRepr.empty()) {
171 ModuleBase_Tools::setSpinText(mySpinBox, QString::fromStdString(aTextRepr));
173 ModuleBase_Tools::setSpinValue(mySpinBox, aRef->isInitialized() ? aRef->value() : 0);
178 void ModuleBase_WidgetDoubleValue::selectContent()
180 mySpinBox->selectAll();
183 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
185 QList<QWidget*> aList;
186 aList.append(mySpinBox);
190 bool ModuleBase_WidgetDoubleValue::processEnter()
192 bool isModified = getValueState() == ModifiedInPP;
194 emit valuesChanged();
195 mySpinBox->selectAll();