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)
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 int aDefVal = QString::fromStdString(getDefaultValue()).toInt(&isOk);
96 mySpinBox->setValue(aDefVal);
99 QString aTTip = translate(theData->widgetTooltip());
100 mySpinBox->setToolTip(aTTip);
101 myLabel->setToolTip(aTTip);
103 aControlLay->addRow(myLabel, mySpinBox);
104 connect(mySpinBox, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
107 ModuleBase_WidgetIntValue::~ModuleBase_WidgetIntValue()
111 void ModuleBase_WidgetIntValue::activateCustom()
113 ModuleBase_ModelWidget::activateCustom();
114 QStringList aParameters;
115 ModuleBase_Tools::getParameters(aParameters);
116 mySpinBox->setCompletionList(aParameters);
119 bool ModuleBase_WidgetIntValue::resetCustom()
122 if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
126 int aDefValue = QString::fromStdString(getDefaultValue()).toInt(&isOk);
127 // reset the value just if there is a default value definition in the XML definition
128 // if the value can not be found by the default value, do nothing
130 ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
138 bool ModuleBase_WidgetIntValue::storeValueCustom()
140 DataPtr aData = myFeature->data();
141 AttributeIntegerPtr anAttribute = aData->integer(attributeID());
142 if (mySpinBox->hasVariable()) {
143 // Here is a text of a real value or an expression.
144 QString aText = mySpinBox->text();
145 if (aText.contains('=')) {
146 if (!myParameter.get()) {
147 myParameter = ModuleBase_Tools::createParameter(aText);
148 if (!myParameter.get()) {
149 anAttribute->setExpressionError("Parameter cannot be created");
150 anAttribute->setExpressionInvalid(true);
151 updateObject(myFeature);
153 } else if (anAttribute->expressionInvalid()) {
154 anAttribute->setExpressionError("");
155 anAttribute->setExpressionInvalid(false);
158 ModuleBase_Tools::editParameter(myParameter, aText);
160 aText = aText.split('=').at(0) + "=";
161 } else if (myParameter.get()) {
162 // Nullyfy the parameter reference without deletion of the created
163 myParameter = FeaturePtr();
165 anAttribute->setText(aText.toStdString());
167 // it is important to set the empty text value to the attribute before set the value
168 // because setValue tries to calculate the attribute value according to the
169 // attribute current text
170 if (anAttribute->expressionInvalid()) {
171 anAttribute->setExpressionError("");
172 anAttribute->setExpressionInvalid(false);
174 anAttribute->setText("");
175 anAttribute->setValue(mySpinBox->value());
177 updateObject(myFeature);
181 bool ModuleBase_WidgetIntValue::restoreValueCustom()
183 DataPtr aData = myFeature->data();
184 AttributeIntegerPtr anAttribute = aData->integer(attributeID());
185 std::string aTextRepr = anAttribute->text();
186 if (!aTextRepr.empty()) {
187 QString aText = QString::fromStdString(aTextRepr);
188 //if (aText.endsWith('=')) {
189 // if (!myParameter.get()) {
190 // QString aName = aText.left(aText.indexOf('=')).trimmed();
191 // myParameter = ModuleBase_Tools::findParameter(aName);
193 // /// If myParameter is empty then it was not created because of an error
194 // if (!myParameter.get())
197 // AttributeStringPtr aExprAttr = myParameter->string("expression");
198 // aText += aExprAttr->value().c_str();
200 ModuleBase_Tools::setSpinText(mySpinBox, aText);
202 ModuleBase_Tools::setSpinValue(mySpinBox,
203 anAttribute->isInitialized() ? anAttribute->value() : 0);
204 if (anAttribute->isInitialized() && anAttribute->expressionInvalid()) {
205 anAttribute->setExpressionError("");
206 anAttribute->setExpressionInvalid(false);
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();