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 <ModuleBase_WidgetIntValue.h>
21 #include <ModuleBase_ParamSpinBox.h>
22 #include <ModuleBase_Tools.h>
23 #include <ModuleBase_ParamIntSpinBox.h>
24 #include <ModuleBase_IconFactory.h>
26 #include <ModelAPI_AttributeInteger.h>
27 #include <ModelAPI_Data.h>
29 #include <Config_Keywords.h>
30 #include <Config_WidgetAPI.h>
32 #include <Events_Loop.h>
33 #include <ModelAPI_Events.h>
36 #include <QFormLayout>
44 #define INT_MAX 2147483647
51 ModuleBase_WidgetIntValue::ModuleBase_WidgetIntValue(QWidget* theParent,
52 const Config_WidgetAPI* theData)
53 : ModuleBase_ModelWidget(theParent, theData)
55 QFormLayout* aControlLay = new QFormLayout(this);
56 ModuleBase_Tools::adjustMargins(aControlLay);
58 QString aLabelText = translate(theData->widgetLabel());
59 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
60 myLabel = new QLabel(aLabelText, this);
61 if (!aLabelIcon.isEmpty())
62 myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
64 mySpinBox = new ModuleBase_ParamIntSpinBox(this);
65 QString anObjName = QString::fromStdString(attributeID());
66 mySpinBox->setObjectName(anObjName);
69 std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
70 int aMinVal = QString::fromStdString(aProp).toInt(&isOk);
72 mySpinBox->setMinimum(aMinVal);
74 mySpinBox->setMinimum(-INT_MAX);
77 aProp = theData->getProperty(DOUBLE_WDG_MAX);
78 int aMaxVal = QString::fromStdString(aProp).toInt(&isOk);
80 mySpinBox->setMaximum(aMaxVal);
82 mySpinBox->setMaximum(INT_MAX);
85 aProp = theData->getProperty(DOUBLE_WDG_STEP);
86 int aStepVal = QString::fromStdString(aProp).toInt(&isOk);
88 mySpinBox->setSingleStep(aStepVal);
91 int aDefVal = QString::fromStdString(getDefaultValue()).toInt(&isOk);
93 mySpinBox->setValue(aDefVal);
96 QString aTTip = translate(theData->widgetTooltip());
97 mySpinBox->setToolTip(aTTip);
98 myLabel->setToolTip(aTTip);
100 aControlLay->addRow(myLabel, mySpinBox);
101 connect(mySpinBox, SIGNAL(valueChanged(int)), this, SIGNAL(valuesModified()));
104 ModuleBase_WidgetIntValue::~ModuleBase_WidgetIntValue()
108 bool ModuleBase_WidgetIntValue::resetCustom()
111 if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
115 int aDefValue = QString::fromStdString(getDefaultValue()).toInt(&isOk);
116 // reset the value just if there is a default value definition in the XML definition
117 // if the value can not be found by the default value, do nothing
119 ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
127 bool ModuleBase_WidgetIntValue::storeValueCustom()
129 DataPtr aData = myFeature->data();
130 AttributeIntegerPtr anAttribute = aData->integer(attributeID());
131 if (mySpinBox->hasVariable()) {
132 // Here is a text of a real value or an expression.
133 std::string aText = mySpinBox->text().toStdString();
134 anAttribute->setText(aText);
136 // it is important to set the empty text value to the attribute before set the value
137 // because setValue tries to calculate the attribute value according to the
138 // attribute current text
139 anAttribute->setText("");
140 anAttribute->setValue(mySpinBox->value());
142 updateObject(myFeature);
146 bool ModuleBase_WidgetIntValue::restoreValueCustom()
148 DataPtr aData = myFeature->data();
149 AttributeIntegerPtr anAttribute = aData->integer(attributeID());
150 std::string aTextRepr = anAttribute->text();
151 if (!aTextRepr.empty()) {
152 ModuleBase_Tools::setSpinText(mySpinBox, QString::fromStdString(aTextRepr));
154 ModuleBase_Tools::setSpinValue(mySpinBox, anAttribute->value());
159 void ModuleBase_WidgetIntValue::selectContent()
161 mySpinBox->selectAll();
164 QList<QWidget*> ModuleBase_WidgetIntValue::getControls() const
166 QList<QWidget*> aList;
167 aList.append(mySpinBox);
171 bool ModuleBase_WidgetIntValue::processEnter()
173 bool isModified = getValueState() == ModifiedInPP;
175 emit valuesChanged();
176 mySpinBox->selectAll();