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_Expression.h>
29 #include <ModelAPI_AttributeString.h>
30 #include <ModelAPI_Session.h>
31 #include <ModelAPI_Document.h>
32 #include <ModelAPI_ResultParameter.h>
33 #include <ModelAPI_AttributeDouble.h>
34 #include <ModelAPI_Tools.h>
35 #include <ModelAPI_Events.h>
37 #include <ModuleBase_ParamSpinBox.h>
38 #include <ModuleBase_Tools.h>
39 #include <ModuleBase_WidgetDoubleValue.h>
40 #include <ModuleBase_IconFactory.h>
42 #include <QFormLayout>
52 #define DBL_MAX 1.7976931348623158e+308
58 #define DEBUG_COMPLETE_WITH_PARAMETERS
60 ModuleBase_WidgetDoubleValue::ModuleBase_WidgetDoubleValue(QWidget* theParent,
61 const Config_WidgetAPI* theData)
62 : ModuleBase_ModelWidget(theParent, theData)
64 QFormLayout* aControlLay = new QFormLayout(this);
65 ModuleBase_Tools::adjustMargins(aControlLay);
67 QString aLabelText = translate(theData->widgetLabel());
68 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
69 myLabel = new QLabel(aLabelText, this);
70 if (!aLabelIcon.isEmpty())
71 myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
73 bool aAcceptVariables = theData->getBooleanAttribute(DOUBLE_WDG_ACCEPT_EXPRESSIONS, true);
75 mySpinBox = new ModuleBase_ParamSpinBox(this);
76 mySpinBox->setAcceptVariables(aAcceptVariables);
77 QString anObjName = QString::fromStdString(attributeID());
78 mySpinBox->setObjectName(anObjName);
81 std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
82 double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
84 mySpinBox->setMinimum(aMinVal);
86 mySpinBox->setMinimum(-DBL_MAX);
89 aProp = theData->getProperty(DOUBLE_WDG_MAX);
90 double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
92 mySpinBox->setMaximum(aMaxVal);
94 mySpinBox->setMaximum(DBL_MAX);
97 aProp = theData->getProperty(DOUBLE_WDG_STEP);
98 double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
100 double aMinStep = pow(10, -1. * (double) mySpinBox->decimals());
101 if(aStepVal < aMinStep){
104 mySpinBox->setSingleStep(aStepVal);
107 double aDefVal = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
109 mySpinBox->setValue(aDefVal);
112 QString aTTip = translate(theData->widgetTooltip());
113 mySpinBox->setToolTip(aTTip);
114 myLabel->setToolTip(aTTip);
116 aControlLay->addRow(myLabel, mySpinBox);
117 // we should listen textChanged signal as valueChanged do not send when text is modified
118 connect(mySpinBox, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
119 mySpinBox->setValueEnabled(isValueEnabled());
122 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
126 void ModuleBase_WidgetDoubleValue::activateCustom()
128 ModuleBase_ModelWidget::activateCustom();
129 #ifdef DEBUG_COMPLETE_WITH_PARAMETERS
130 QStringList aParameters;
131 ModuleBase_Tools::getParameters(aParameters);
132 mySpinBox->setCompletionList(aParameters);
136 bool ModuleBase_WidgetDoubleValue::resetCustom()
139 if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
143 double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
144 // reset the value just if there is a default value definition in the XML definition
145 // if the value can not be found by the default value, do nothing
147 ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
155 bool ModuleBase_WidgetDoubleValue::storeValueCustom()
157 DataPtr aData = myFeature->data();
158 AttributeDoublePtr aReal = aData->real(attributeID());
159 if (mySpinBox->hasVariable()) {
160 // Here is a text of a real value or an expression.
161 QString aText = mySpinBox->text();
162 if (aText.contains('=')) {
163 if (!myParameter.get()) {
164 myParameter = createParameter(aText);
166 editParameter(aText);
168 aText = aText.split('=').at(0) + "=";
169 } else if (myParameter.get()){
170 // Nullyfy the parameter reference without deletion of the created
171 myParameter = FeaturePtr();
173 aReal->setText(aText.toStdString());
175 // it is important to set the empty text value to the attribute before set the value
176 // because setValue tries to calculate the attribute value according to the
177 // attribute current text
179 aReal->setValue(mySpinBox->value());
181 updateObject(myFeature);
185 bool ModuleBase_WidgetDoubleValue::restoreValueCustom()
187 DataPtr aData = myFeature->data();
188 AttributeDoublePtr aRef = aData->real(attributeID());
189 std::string aTextRepr = aRef->text();
190 if (!aTextRepr.empty()) {
191 QString aText = QString::fromStdString(aTextRepr);
192 if (aText.endsWith('=')) {
193 if (!myParameter.get()) {
194 QString aName = aText.left(aText.indexOf('=')).trimmed();
195 std::string aa = aName.toStdString();
196 myParameter = findParameter(aName);
198 AttributeStringPtr aExprAttr = myParameter->string("expression");
199 aText += aExprAttr->value().c_str();
201 ModuleBase_Tools::setSpinText(mySpinBox, aText);
203 ModuleBase_Tools::setSpinValue(mySpinBox, aRef->isInitialized() ? aRef->value() : 0);
208 void ModuleBase_WidgetDoubleValue::selectContent()
210 mySpinBox->selectAll();
213 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
215 QList<QWidget*> aList;
216 aList.append(mySpinBox);
220 bool ModuleBase_WidgetDoubleValue::processEnter()
222 bool isModified = getValueState() == ModifiedInPP;
224 emit valuesChanged();
225 mySpinBox->selectAll();
231 FeaturePtr ModuleBase_WidgetDoubleValue::createParameter(const QString& theText) const
233 FeaturePtr aParameter;
234 QStringList aList = theText.split("=");
235 QString aParamName = aList.at(0).trimmed();
237 if (isNameExist(aParamName)) {
241 if (!ModelAPI_Expression::isVariable(aParamName.toStdString())) {
245 QString aExpression = aList.at(1).trimmed();
246 if (aExpression.isEmpty()) {
250 SessionPtr aMgr = ModelAPI_Session::get();
251 std::shared_ptr<ModelAPI_Document> aDoc = aMgr->activeDocument();
253 aParameter = aDoc->addFeature("Parameter");
254 if (aParameter.get()) {
255 AttributeStringPtr aNameAttr = aParameter->string("variable");
256 aNameAttr->setValue(aParamName.toStdString());
258 AttributeStringPtr aExprAttr = aParameter->string("expression");
259 aExprAttr->setValue(aExpression.toStdString());
260 aParameter->execute();
262 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
263 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
268 bool ModuleBase_WidgetDoubleValue::isNameExist(const QString& theName) const
270 SessionPtr aMgr = ModelAPI_Session::get();
271 std::shared_ptr<ModelAPI_Document> aDoc = aMgr->activeDocument();
272 FeaturePtr aParamFeature;
273 int aNbFeatures = aDoc->numInternalFeatures();
274 std::string aName = theName.toStdString();
275 for (int i = 0; i < aNbFeatures; i++) {
276 aParamFeature = aDoc->internalFeature(i);
277 if (aParamFeature && aParamFeature->getKind() == "Parameter") {
278 if ((myParameter != aParamFeature) && (aParamFeature->name() == aName))
285 void ModuleBase_WidgetDoubleValue::editParameter(const QString& theText)
287 QStringList aList = theText.split("=");
288 QString aParamName = aList.at(0).trimmed();
290 QString aExpression = aList.at(1).trimmed();
291 if (aExpression.isEmpty()) {
295 if (isNameExist(aParamName)) {
298 AttributeStringPtr aNameAttr = myParameter->string("variable");
299 aNameAttr->setValue(aParamName.toStdString());
301 AttributeStringPtr aExprAttr = myParameter->string("expression");
302 aExprAttr->setValue(aExpression.toStdString());
303 myParameter->execute();
305 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
308 FeaturePtr ModuleBase_WidgetDoubleValue::findParameter(const QString& theName) const
310 SessionPtr aMgr = ModelAPI_Session::get();
311 std::shared_ptr<ModelAPI_Document> aDoc = aMgr->activeDocument();
312 FeaturePtr aParamFeature;
313 int aNbFeatures = aDoc->numInternalFeatures();
314 std::string aName = theName.toStdString();
315 for (int i = 0; i < aNbFeatures; i++) {
316 aParamFeature = aDoc->internalFeature(i);
317 if (aParamFeature && aParamFeature->getKind() == "Parameter") {
318 if (aParamFeature->name() == aName)
319 return aParamFeature;