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);
87 myLabel->setToolTip(aTTip);
89 aControlLay->addRow(myLabel, mySpinBox);
90 #ifdef APPLY_BY_ENTER_OR_TAB
91 // Apply widget value change by enter/tab event.
92 connect(mySpinBox, SIGNAL(editingFinished()), this, SIGNAL(valuesChanged()));
93 connect(mySpinBox, SIGNAL(valueChanged(int)), this, SIGNAL(valuesModified()));
95 connect(mySpinBox, SIGNAL(valueChanged(int)), this, SIGNAL(valuesChanged()));
99 ModuleBase_WidgetIntValue::~ModuleBase_WidgetIntValue()
103 bool ModuleBase_WidgetIntValue::resetCustom()
106 if (!isUseReset() || isComputedDefault()) {
110 int aDefValue = QString::fromStdString(getDefaultValue()).toInt(&isOk);
111 // reset the value just if there is a default value definition in the XML definition
112 // if the double value can not be found by the default value, do nothing
114 bool isBlocked = mySpinBox->blockSignals(true);
115 mySpinBox->setValue(isOk ? aDefValue : 0);
116 mySpinBox->blockSignals(isBlocked);
124 bool ModuleBase_WidgetIntValue::storeValueCustom() const
126 DataPtr aData = myFeature->data();
127 AttributeIntegerPtr aIntVal = aData->integer(attributeID());
128 int aVal = mySpinBox->value();
129 aIntVal->setValue(mySpinBox->value());
130 updateObject(myFeature);
134 bool ModuleBase_WidgetIntValue::restoreValueCustom()
136 DataPtr aData = myFeature->data();
137 AttributeIntegerPtr aRef = aData->integer(attributeID());
138 bool isBlocked = mySpinBox->blockSignals(true);
139 mySpinBox->setValue(aRef->value());
140 mySpinBox->blockSignals(isBlocked);
144 QList<QWidget*> ModuleBase_WidgetIntValue::getControls() const
146 QList<QWidget*> aList;
147 aList.append(mySpinBox);