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 <ModuleBase_WidgetIntValue.h>
22 #include <ModuleBase_ParamSpinBox.h>
23 #include <ModuleBase_Tools.h>
24 #include <ModuleBase_ParamSpinBox.h>
25 #include <ModuleBase_IconFactory.h>
27 #include <ModelAPI_AttributeInteger.h>
28 #include <ModelAPI_Data.h>
29 #include <ModelAPI_AttributeString.h>
31 #include <Config_Keywords.h>
32 #include <Config_WidgetAPI.h>
34 #include <Events_Loop.h>
35 #include <ModelAPI_Events.h>
38 #include <QFormLayout>
46 #define INT_MAX 2147483647
53 ModuleBase_WidgetIntValue::ModuleBase_WidgetIntValue(QWidget* theParent,
54 const Config_WidgetAPI* theData)
55 : ModuleBase_ModelWidget(theParent, theData), myHasDefault(false)
57 QFormLayout* aControlLay = new QFormLayout(this);
58 ModuleBase_Tools::adjustMargins(aControlLay);
60 QString aLabelText = translate(theData->widgetLabel());
61 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
62 myLabel = new QLabel(aLabelText, this);
63 if (!aLabelIcon.isEmpty())
64 myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
66 mySpinBox = new ModuleBase_ParamSpinBox(this);
67 mySpinBox->setDecimals(0);
68 QString anObjName = QString::fromStdString(attributeID());
69 mySpinBox->setObjectName(anObjName);
72 std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
73 int aMinVal = QString::fromStdString(aProp).toInt(&isOk);
75 mySpinBox->setMinimum(aMinVal);
77 mySpinBox->setMinimum(-INT_MAX);
80 aProp = theData->getProperty(DOUBLE_WDG_MAX);
81 int aMaxVal = QString::fromStdString(aProp).toInt(&isOk);
83 mySpinBox->setMaximum(aMaxVal);
85 mySpinBox->setMaximum(INT_MAX);
88 aProp = theData->getProperty(DOUBLE_WDG_STEP);
89 int aStepVal = QString::fromStdString(aProp).toInt(&isOk);
91 mySpinBox->setSingleStep(aStepVal);
94 myDefVal = QString::fromStdString(getDefaultValue()).toInt(&myHasDefault);
96 mySpinBox->setValue(myDefVal);
98 QString aTTip = translate(theData->widgetTooltip());
99 mySpinBox->setToolTip(aTTip);
100 myLabel->setToolTip(aTTip);
102 aControlLay->addRow(myLabel, mySpinBox);
103 connect(mySpinBox, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
106 ModuleBase_WidgetIntValue::~ModuleBase_WidgetIntValue()
110 void ModuleBase_WidgetIntValue::activateCustom()
112 ModuleBase_ModelWidget::activateCustom();
113 QStringList aParameters;
114 ModuleBase_Tools::getParameters(aParameters);
115 mySpinBox->setCompletionList(aParameters);
118 bool ModuleBase_WidgetIntValue::resetCustom()
121 if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
125 int aDefValue = QString::fromStdString(getDefaultValue()).toInt(&isOk);
126 // reset the value just if there is a default value definition in the XML definition
127 // if the value can not be found by the default value, do nothing
129 ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
137 bool ModuleBase_WidgetIntValue::storeValueCustom()
139 DataPtr aData = myFeature->data();
140 AttributeIntegerPtr anAttribute = aData->integer(attributeID());
141 if (mySpinBox->hasVariable()) {
142 // Here is a text of a real value or an expression.
143 QString aText = mySpinBox->text();
144 if (aText.contains('=')) {
145 if (!myParameter.get()) {
146 myParameter = ModuleBase_Tools::createParameter(aText);
147 if (!myParameter.get()) {
148 anAttribute->setExpressionError("Parameter cannot be created");
149 anAttribute->setExpressionInvalid(true);
150 updateObject(myFeature);
152 } else if (anAttribute->expressionInvalid()) {
153 anAttribute->setExpressionError("");
154 anAttribute->setExpressionInvalid(false);
157 ModuleBase_Tools::editParameter(myParameter, aText);
159 aText = aText.split('=').at(0);
160 } else if (myParameter.get()) {
161 // Nullyfy the parameter reference without deletion of the created
162 myParameter = FeaturePtr();
164 anAttribute->setText(aText.toStdString());
166 // it is important to set the empty text value to the attribute before set the value
167 // because setValue tries to calculate the attribute value according to the
168 // attribute current text
169 if (anAttribute->expressionInvalid()) {
170 anAttribute->setExpressionError("");
171 anAttribute->setExpressionInvalid(false);
173 anAttribute->setText("");
174 anAttribute->setValue(mySpinBox->value());
176 updateObject(myFeature);
180 bool ModuleBase_WidgetIntValue::restoreValueCustom()
182 DataPtr aData = myFeature->data();
183 AttributeIntegerPtr anAttribute = aData->integer(attributeID());
184 std::string aTextRepr = anAttribute->text();
185 if (!aTextRepr.empty()) {
186 QString aText = QString::fromStdString(aTextRepr);
187 //if (aText.endsWith('=')) {
188 // if (!myParameter.get()) {
189 // QString aName = aText.left(aText.indexOf('=')).trimmed();
190 // myParameter = ModuleBase_Tools::findParameter(aName);
192 // /// If myParameter is empty then it was not created because of an error
193 // if (!myParameter.get())
196 // AttributeStringPtr aExprAttr = myParameter->string("expression");
197 // aText += aExprAttr->value().c_str();
199 ModuleBase_Tools::setSpinText(mySpinBox, aText);
201 ModuleBase_Tools::setSpinValue(mySpinBox,
202 anAttribute->isInitialized() ? anAttribute->value() : myDefVal);
203 if (anAttribute->isInitialized() && anAttribute->expressionInvalid()) {
204 anAttribute->setExpressionError("");
205 anAttribute->setExpressionInvalid(false);
207 if ((!anAttribute->isInitialized()) && myHasDefault)
208 anAttribute->setValue(myDefVal);
213 void ModuleBase_WidgetIntValue::selectContent()
215 mySpinBox->selectAll();
218 QList<QWidget*> ModuleBase_WidgetIntValue::getControls() const
220 QList<QWidget*> aList;
221 aList.append(mySpinBox);
225 bool ModuleBase_WidgetIntValue::processEnter()
227 bool isModified = getValueState() == ModifiedInPP;
229 emit valuesChanged();
230 mySpinBox->selectAll();