1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModuleBase_WidgetIntValue.cpp
4 // Created: 04 June 2014
5 // Author: Vitaly Smetannikov
7 #include <ModuleBase_WidgetIntValue.h>
8 #include <ModuleBase_ParamSpinBox.h>
9 #include <ModuleBase_Tools.h>
10 #include <ModuleBase_ParamIntSpinBox.h>
12 #include <ModelAPI_AttributeInteger.h>
13 #include <ModelAPI_Data.h>
15 #include <Config_Keywords.h>
16 #include <Config_WidgetAPI.h>
18 #include <Events_Loop.h>
19 #include <ModelAPI_Events.h>
22 #include <QFormLayout>
30 #define INT_MAX 2147483647
37 ModuleBase_WidgetIntValue::ModuleBase_WidgetIntValue(QWidget* theParent,
38 const Config_WidgetAPI* theData)
39 : ModuleBase_ModelWidget(theParent, theData)
41 QFormLayout* aControlLay = new QFormLayout(this);
42 ModuleBase_Tools::adjustMargins(aControlLay);
44 QString aLabelText = QString::fromStdString(theData->widgetLabel());
45 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
46 myLabel = new QLabel(aLabelText, this);
47 if (!aLabelIcon.isEmpty())
48 myLabel->setPixmap(QPixmap(aLabelIcon));
50 mySpinBox = new ModuleBase_ParamIntSpinBox(this);
51 QString anObjName = QString::fromStdString(attributeID());
52 mySpinBox->setObjectName(anObjName);
55 std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
56 int aMinVal = QString::fromStdString(aProp).toInt(&isOk);
58 mySpinBox->setMinimum(aMinVal);
60 mySpinBox->setMinimum(-INT_MAX);
63 aProp = theData->getProperty(DOUBLE_WDG_MAX);
64 int aMaxVal = QString::fromStdString(aProp).toInt(&isOk);
66 mySpinBox->setMaximum(aMaxVal);
68 mySpinBox->setMaximum(INT_MAX);
71 aProp = theData->getProperty(DOUBLE_WDG_STEP);
72 int aStepVal = QString::fromStdString(aProp).toInt(&isOk);
74 mySpinBox->setSingleStep(aStepVal);
77 int aDefVal = QString::fromStdString(getDefaultValue()).toInt(&isOk);
79 mySpinBox->setValue(aDefVal);
82 QString aTTip = QString::fromStdString(theData->widgetTooltip());
83 mySpinBox->setToolTip(aTTip);
84 myLabel->setToolTip(aTTip);
86 aControlLay->addRow(myLabel, mySpinBox);
87 connect(mySpinBox, SIGNAL(valueChanged(int)), this, SIGNAL(valuesModified()));
90 ModuleBase_WidgetIntValue::~ModuleBase_WidgetIntValue()
94 bool ModuleBase_WidgetIntValue::resetCustom()
97 if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
101 int aDefValue = QString::fromStdString(getDefaultValue()).toInt(&isOk);
102 // reset the value just if there is a default value definition in the XML definition
103 // if the value can not be found by the default value, do nothing
105 ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
113 bool ModuleBase_WidgetIntValue::storeValueCustom() const
115 DataPtr aData = myFeature->data();
116 AttributeIntegerPtr anAttribute = aData->integer(attributeID());
117 if (mySpinBox->hasVariable()) {
118 // Here is a text of a real value or an expression.
119 std::string aText = mySpinBox->text().toStdString();
120 anAttribute->setText(aText);
122 // it is important to set the empty text value to the attribute before set the value
123 // because setValue tries to calculate the attribute value according to the
124 // attribute current text
125 anAttribute->setText("");
126 anAttribute->setValue(mySpinBox->value());
128 updateObject(myFeature);
132 bool ModuleBase_WidgetIntValue::restoreValueCustom()
134 DataPtr aData = myFeature->data();
135 AttributeIntegerPtr anAttribute = aData->integer(attributeID());
136 std::string aTextRepr = anAttribute->text();
137 if (!aTextRepr.empty()) {
138 ModuleBase_Tools::setSpinText(mySpinBox, QString::fromStdString(aTextRepr));
140 ModuleBase_Tools::setSpinValue(mySpinBox, anAttribute->value());
145 void ModuleBase_WidgetIntValue::selectContent()
147 mySpinBox->selectAll();
150 QList<QWidget*> ModuleBase_WidgetIntValue::getControls() const
152 QList<QWidget*> aList;
153 aList.append(mySpinBox);
157 bool ModuleBase_WidgetIntValue::processEnter()
159 bool isModified = getValueState() == ModifiedInPP;
161 emit valuesChanged();
162 mySpinBox->selectAll();