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 <ModuleBase_WidgetIntValue.h>
21 #include <ModuleBase_ParamSpinBox.h>
22 #include <ModuleBase_Tools.h>
23 #include <ModuleBase_ParamSpinBox.h>
24 #include <ModuleBase_IconFactory.h>
26 #include <ModelAPI_AttributeInteger.h>
27 #include <ModelAPI_Data.h>
28 #include <ModelAPI_AttributeString.h>
30 #include <Config_Keywords.h>
31 #include <Config_WidgetAPI.h>
33 #include <Events_Loop.h>
34 #include <ModelAPI_Events.h>
37 #include <QFormLayout>
45 #define INT_MAX 2147483647
52 ModuleBase_WidgetIntValue::ModuleBase_WidgetIntValue(QWidget* theParent,
53 const Config_WidgetAPI* theData)
54 : ModuleBase_ModelWidget(theParent, theData), myHasDefault(false)
56 QFormLayout* aControlLay = new QFormLayout(this);
57 ModuleBase_Tools::adjustMargins(aControlLay);
59 QString aLabelText = translate(theData->widgetLabel());
60 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
61 myLabel = new QLabel(aLabelText, this);
62 if (!aLabelIcon.isEmpty())
63 myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
65 mySpinBox = new ModuleBase_ParamSpinBox(this);
66 mySpinBox->setDecimals(0);
67 QString anObjName = QString::fromStdString(attributeID());
68 mySpinBox->setObjectName(anObjName);
71 std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
72 int aMinVal = QString::fromStdString(aProp).toInt(&isOk);
74 mySpinBox->setMinimum(aMinVal);
76 mySpinBox->setMinimum(-INT_MAX);
79 aProp = theData->getProperty(DOUBLE_WDG_MAX);
80 int aMaxVal = QString::fromStdString(aProp).toInt(&isOk);
82 mySpinBox->setMaximum(aMaxVal);
84 mySpinBox->setMaximum(INT_MAX);
87 aProp = theData->getProperty(DOUBLE_WDG_STEP);
88 int aStepVal = QString::fromStdString(aProp).toInt(&isOk);
90 mySpinBox->setSingleStep(aStepVal);
93 myDefVal = QString::fromStdString(getDefaultValue()).toInt(&myHasDefault);
95 mySpinBox->setValue(myDefVal);
97 QString aTTip = translate(theData->widgetTooltip());
98 mySpinBox->setToolTip(aTTip);
99 myLabel->setToolTip(aTTip);
101 aControlLay->addRow(myLabel, mySpinBox);
102 connect(mySpinBox, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
105 ModuleBase_WidgetIntValue::~ModuleBase_WidgetIntValue()
109 void ModuleBase_WidgetIntValue::activateCustom()
111 ModuleBase_ModelWidget::activateCustom();
112 QStringList aParameters;
113 ModuleBase_Tools::getParameters(aParameters);
114 mySpinBox->setCompletionList(aParameters);
117 bool ModuleBase_WidgetIntValue::resetCustom()
120 if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
124 int aDefValue = QString::fromStdString(getDefaultValue()).toInt(&isOk);
125 // reset the value just if there is a default value definition in the XML definition
126 // if the value can not be found by the default value, do nothing
128 ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
136 bool ModuleBase_WidgetIntValue::storeValueCustom()
138 DataPtr aData = myFeature->data();
139 AttributeIntegerPtr anAttribute = aData->integer(attributeID());
140 if (mySpinBox->hasVariable()) {
141 // Here is a text of a real value or an expression.
142 QString aText = mySpinBox->text();
143 if (aText.contains('=')) {
144 if (!myParameter.get()) {
145 myParameter = ModuleBase_Tools::createParameter(aText);
146 if (!myParameter.get()) {
147 anAttribute->setExpressionError("Parameter cannot be created");
148 anAttribute->setExpressionInvalid(true);
149 updateObject(myFeature);
151 } else if (anAttribute->expressionInvalid()) {
152 anAttribute->setExpressionError("");
153 anAttribute->setExpressionInvalid(false);
156 ModuleBase_Tools::editParameter(myParameter, aText);
158 aText = aText.split('=').at(0);
159 } else if (myParameter.get()) {
160 // Nullyfy the parameter reference without deletion of the created
161 myParameter = FeaturePtr();
163 anAttribute->setText(aText.toStdString());
165 // it is important to set the empty text value to the attribute before set the value
166 // because setValue tries to calculate the attribute value according to the
167 // attribute current text
168 if (anAttribute->expressionInvalid()) {
169 anAttribute->setExpressionError("");
170 anAttribute->setExpressionInvalid(false);
172 anAttribute->setText("");
173 anAttribute->setValue(mySpinBox->value());
175 updateObject(myFeature);
179 bool ModuleBase_WidgetIntValue::restoreValueCustom()
181 DataPtr aData = myFeature->data();
182 AttributeIntegerPtr anAttribute = aData->integer(attributeID());
183 std::string aTextRepr = anAttribute->text();
184 if (!aTextRepr.empty()) {
185 QString aText = QString::fromStdString(aTextRepr);
186 //if (aText.endsWith('=')) {
187 // if (!myParameter.get()) {
188 // QString aName = aText.left(aText.indexOf('=')).trimmed();
189 // myParameter = ModuleBase_Tools::findParameter(aName);
191 // /// If myParameter is empty then it was not created because of an error
192 // if (!myParameter.get())
195 // AttributeStringPtr aExprAttr = myParameter->string("expression");
196 // aText += aExprAttr->value().c_str();
198 ModuleBase_Tools::setSpinText(mySpinBox, aText);
200 ModuleBase_Tools::setSpinValue(mySpinBox,
201 anAttribute->isInitialized() ? anAttribute->value() : myDefVal);
202 if (anAttribute->isInitialized() && anAttribute->expressionInvalid()) {
203 anAttribute->setExpressionError("");
204 anAttribute->setExpressionInvalid(false);
206 if ((!anAttribute->isInitialized()) && myHasDefault)
207 anAttribute->setValue(myDefVal);
212 void ModuleBase_WidgetIntValue::selectContent()
214 mySpinBox->selectAll();
217 QList<QWidget*> ModuleBase_WidgetIntValue::getControls() const
219 QList<QWidget*> aList;
220 aList.append(mySpinBox);
224 bool ModuleBase_WidgetIntValue::processEnter()
226 bool isModified = getValueState() == ModifiedInPP;
228 emit valuesChanged();
229 mySpinBox->selectAll();
234 bool ModuleBase_WidgetIntValue::isModified() const
236 QString aText = mySpinBox->text();
242 int aVal = aText.toInt(&aOk);
243 if (!aOk || aVal == myDefVal)