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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
20 #include <Config_Keywords.h>
21 #include <Config_WidgetAPI.h>
23 #include <ModelAPI_AttributeDouble.h>
24 #include <ModelAPI_Data.h>
25 #include <ModelAPI_Object.h>
27 #include <ModuleBase_ParamSpinBox.h>
28 #include <ModuleBase_Tools.h>
29 #include <ModuleBase_WidgetDoubleValue.h>
30 #include <ModuleBase_IconFactory.h>
32 #include <QFormLayout>
42 #define DBL_MAX 1.7976931348623158e+308
48 //#define DEBUG_COMPLETE_WITH_PARAMETERS
50 ModuleBase_WidgetDoubleValue::ModuleBase_WidgetDoubleValue(QWidget* theParent,
51 const Config_WidgetAPI* theData)
52 : ModuleBase_ModelWidget(theParent, theData)
54 QFormLayout* aControlLay = new QFormLayout(this);
55 ModuleBase_Tools::adjustMargins(aControlLay);
57 QString aLabelText = translate(theData->widgetLabel());
58 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
59 myLabel = new QLabel(aLabelText, this);
60 if (!aLabelIcon.isEmpty())
61 myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
63 bool aAcceptVariables = theData->getBooleanAttribute(DOUBLE_WDG_ACCEPT_EXPRESSIONS, true);
65 mySpinBox = new ModuleBase_ParamSpinBox(this);
66 mySpinBox->setAcceptVariables(aAcceptVariables);
67 QString anObjName = QString::fromStdString(attributeID());
68 mySpinBox->setObjectName(anObjName);
71 std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
72 double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
74 mySpinBox->setMinimum(aMinVal);
76 mySpinBox->setMinimum(-DBL_MAX);
79 aProp = theData->getProperty(DOUBLE_WDG_MAX);
80 double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
82 mySpinBox->setMaximum(aMaxVal);
84 mySpinBox->setMaximum(DBL_MAX);
87 aProp = theData->getProperty(DOUBLE_WDG_STEP);
88 double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
90 double aMinStep = pow(10, -1. * (double) mySpinBox->decimals());
91 if(aStepVal < aMinStep){
94 mySpinBox->setSingleStep(aStepVal);
97 double aDefVal = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
99 mySpinBox->setValue(aDefVal);
102 QString aTTip = translate(theData->widgetTooltip());
103 mySpinBox->setToolTip(aTTip);
104 myLabel->setToolTip(aTTip);
106 aControlLay->addRow(myLabel, mySpinBox);
107 // we should listen textChanged signal as valueChanged do not send when text is modified
108 connect(mySpinBox, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
109 mySpinBox->setValueEnabled(isValueEnabled());
112 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
116 void ModuleBase_WidgetDoubleValue::activateCustom()
118 ModuleBase_ModelWidget::activateCustom();
119 #ifdef DEBUG_COMPLETE_WITH_PARAMETERS
120 QStringList aParameters;
121 ModuleBase_Tools::getParameters(aParameters);
122 mySpinBox->setCompletionList(aParameters);
126 bool ModuleBase_WidgetDoubleValue::resetCustom()
129 if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
133 double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
134 // reset the value just if there is a default value definition in the XML definition
135 // if the value can not be found by the default value, do nothing
137 ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
145 bool ModuleBase_WidgetDoubleValue::storeValueCustom()
147 DataPtr aData = myFeature->data();
148 AttributeDoublePtr aReal = aData->real(attributeID());
149 if (mySpinBox->hasVariable()) {
150 // Here is a text of a real value or an expression.
151 std::string aText = mySpinBox->text().toStdString();
152 aReal->setText(aText);
154 // it is important to set the empty text value to the attribute before set the value
155 // because setValue tries to calculate the attribute value according to the
156 // attribute current text
158 aReal->setValue(mySpinBox->value());
160 updateObject(myFeature);
164 bool ModuleBase_WidgetDoubleValue::restoreValueCustom()
166 DataPtr aData = myFeature->data();
167 AttributeDoublePtr aRef = aData->real(attributeID());
168 std::string aTextRepr = aRef->text();
169 if (!aTextRepr.empty()) {
170 ModuleBase_Tools::setSpinText(mySpinBox, QString::fromStdString(aTextRepr));
172 ModuleBase_Tools::setSpinValue(mySpinBox, aRef->isInitialized() ? aRef->value() : 0);
177 void ModuleBase_WidgetDoubleValue::selectContent()
179 mySpinBox->selectAll();
182 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
184 QList<QWidget*> aList;
185 aList.append(mySpinBox);
189 bool ModuleBase_WidgetDoubleValue::processEnter()
191 bool isModified = getValueState() == ModifiedInPP;
193 emit valuesChanged();
194 mySpinBox->selectAll();