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_ParamIntSpinBox.h>
25 #include <ModuleBase_IconFactory.h>
27 #include <ModelAPI_AttributeInteger.h>
28 #include <ModelAPI_Data.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)
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_ParamIntSpinBox(this);
66 QString anObjName = QString::fromStdString(attributeID());
67 mySpinBox->setObjectName(anObjName);
70 std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
71 int aMinVal = QString::fromStdString(aProp).toInt(&isOk);
73 mySpinBox->setMinimum(aMinVal);
75 mySpinBox->setMinimum(-INT_MAX);
78 aProp = theData->getProperty(DOUBLE_WDG_MAX);
79 int aMaxVal = QString::fromStdString(aProp).toInt(&isOk);
81 mySpinBox->setMaximum(aMaxVal);
83 mySpinBox->setMaximum(INT_MAX);
86 aProp = theData->getProperty(DOUBLE_WDG_STEP);
87 int aStepVal = QString::fromStdString(aProp).toInt(&isOk);
89 mySpinBox->setSingleStep(aStepVal);
92 int aDefVal = QString::fromStdString(getDefaultValue()).toInt(&isOk);
94 mySpinBox->setValue(aDefVal);
97 QString aTTip = translate(theData->widgetTooltip());
98 mySpinBox->setToolTip(aTTip);
99 myLabel->setToolTip(aTTip);
101 aControlLay->addRow(myLabel, mySpinBox);
102 connect(mySpinBox, SIGNAL(valueChanged(int)), this, SIGNAL(valuesModified()));
105 ModuleBase_WidgetIntValue::~ModuleBase_WidgetIntValue()
109 bool ModuleBase_WidgetIntValue::resetCustom()
112 if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
116 int aDefValue = QString::fromStdString(getDefaultValue()).toInt(&isOk);
117 // reset the value just if there is a default value definition in the XML definition
118 // if the value can not be found by the default value, do nothing
120 ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
128 bool ModuleBase_WidgetIntValue::storeValueCustom()
130 DataPtr aData = myFeature->data();
131 AttributeIntegerPtr anAttribute = aData->integer(attributeID());
132 if (mySpinBox->hasVariable()) {
133 // Here is a text of a real value or an expression.
134 std::string aText = mySpinBox->text().toStdString();
135 anAttribute->setText(aText);
137 // it is important to set the empty text value to the attribute before set the value
138 // because setValue tries to calculate the attribute value according to the
139 // attribute current text
140 anAttribute->setText("");
141 anAttribute->setValue(mySpinBox->value());
143 updateObject(myFeature);
147 bool ModuleBase_WidgetIntValue::restoreValueCustom()
149 DataPtr aData = myFeature->data();
150 AttributeIntegerPtr anAttribute = aData->integer(attributeID());
151 std::string aTextRepr = anAttribute->text();
152 if (!aTextRepr.empty()) {
153 ModuleBase_Tools::setSpinText(mySpinBox, QString::fromStdString(aTextRepr));
155 ModuleBase_Tools::setSpinValue(mySpinBox, anAttribute->value());
160 void ModuleBase_WidgetIntValue::selectContent()
162 mySpinBox->selectAll();
165 QList<QWidget*> ModuleBase_WidgetIntValue::getControls() const
167 QList<QWidget*> aList;
168 aList.append(mySpinBox);
172 bool ModuleBase_WidgetIntValue::processEnter()
174 bool isModified = getValueState() == ModifiedInPP;
176 emit valuesChanged();
177 mySpinBox->selectAll();