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>
11 #include <ModelAPI_AttributeInteger.h>
12 #include <ModelAPI_Data.h>
14 #include <Config_Keywords.h>
15 #include <Config_WidgetAPI.h>
17 #include <Events_Loop.h>
18 #include <ModelAPI_Events.h>
21 #include <QFormLayout>
30 #define INT_MAX 2147483647
37 //#define APPLY_BY_ENTER_OR_TAB
39 ModuleBase_WidgetIntValue::ModuleBase_WidgetIntValue(QWidget* theParent,
40 const Config_WidgetAPI* theData,
41 const std::string& theParentId)
42 : ModuleBase_ModelWidget(theParent, theData, theParentId)
44 QFormLayout* aControlLay = new QFormLayout(this);
45 ModuleBase_Tools::adjustMargins(aControlLay);
47 QString aLabelText = QString::fromStdString(theData->widgetLabel());
48 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
49 myLabel = new QLabel(aLabelText, this);
50 if (!aLabelIcon.isEmpty())
51 myLabel->setPixmap(QPixmap(aLabelIcon));
53 mySpinBox = new QSpinBox(this);
54 QString anObjName = QString::fromStdString(attributeID());
55 mySpinBox->setObjectName(anObjName);
58 std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
59 int aMinVal = QString::fromStdString(aProp).toInt(&isOk);
61 mySpinBox->setMinimum(aMinVal);
63 mySpinBox->setMinimum(-INT_MAX);
66 aProp = theData->getProperty(DOUBLE_WDG_MAX);
67 int aMaxVal = QString::fromStdString(aProp).toInt(&isOk);
69 mySpinBox->setMaximum(aMaxVal);
71 mySpinBox->setMaximum(INT_MAX);
74 aProp = theData->getProperty(DOUBLE_WDG_STEP);
75 int aStepVal = QString::fromStdString(aProp).toInt(&isOk);
77 mySpinBox->setSingleStep(aStepVal);
80 int aDefVal = QString::fromStdString(getDefaultValue()).toInt(&isOk);
82 mySpinBox->setValue(aDefVal);
85 QString aTTip = QString::fromStdString(theData->widgetTooltip());
86 mySpinBox->setToolTip(aTTip);
88 aControlLay->addRow(myLabel, mySpinBox);
89 #ifdef APPLY_BY_ENTER_OR_TAB
90 // Apply widget value change by enter/tab event.
91 connect(mySpinBox, SIGNAL(editingFinished()), this, SIGNAL(valuesChanged()));
93 connect(mySpinBox, SIGNAL(valueChanged(int)), this, SIGNAL(valuesChanged()));
97 ModuleBase_WidgetIntValue::~ModuleBase_WidgetIntValue()
101 bool ModuleBase_WidgetIntValue::reset()
104 if (!isUseReset() || isComputedDefault()) {
108 int aDefValue = QString::fromStdString(getDefaultValue()).toInt(&isOk);
109 // reset the value just if there is a default value definition in the XML definition
110 // if the double value can not be found by the default value, do nothing
112 bool isBlocked = mySpinBox->blockSignals(true);
113 mySpinBox->setValue(isOk ? aDefValue : 0);
114 mySpinBox->blockSignals(isBlocked);
122 bool ModuleBase_WidgetIntValue::storeValueCustom() const
124 DataPtr aData = myFeature->data();
125 AttributeIntegerPtr aIntVal = aData->integer(attributeID());
126 int aVal = mySpinBox->value();
127 aIntVal->setValue(mySpinBox->value());
128 updateObject(myFeature);
132 bool ModuleBase_WidgetIntValue::restoreValueCustom()
134 DataPtr aData = myFeature->data();
135 AttributeIntegerPtr aRef = aData->integer(attributeID());
136 bool isBlocked = mySpinBox->blockSignals(true);
137 mySpinBox->setValue(aRef->value());
138 mySpinBox->blockSignals(isBlocked);
142 QList<QWidget*> ModuleBase_WidgetIntValue::getControls() const
144 QList<QWidget*> aList;
145 aList.append(mySpinBox);