1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 #include "ModuleBase_ParamIntSpinBox.h"
5 #include <ModelAPI_Session.h>
6 #include <ModelAPI_Document.h>
7 #include <ModelAPI_Feature.h>
8 #include <ModelAPI_ResultParameter.h>
9 #include <ModelAPI_AttributeDouble.h>
10 #include <ModelAPI_Tools.h>
17 #include <QApplication>
23 ModuleBase_ParamIntSpinBox::ModuleBase_ParamIntSpinBox(QWidget* theParent)
24 : ModuleBase_IntSpinBox(theParent),
25 myAcceptVariables(true)
27 connectSignalsAndSlots();
33 ModuleBase_ParamIntSpinBox::~ModuleBase_ParamIntSpinBox()
38 \brief Perform \a steps increment/decrement steps.
40 Re-implemented to handle cases when Notebook variable
41 name is specified by the user as the widget text.
42 Otherwise, simply calls the base implementation.
44 \param steps number of increment/decrement steps
46 void ModuleBase_ParamIntSpinBox::stepBy(int steps)
48 if ((!myTextValue.isEmpty()) && hasVariable())
51 ModuleBase_IntSpinBox::stepBy(steps);
55 \brief Connect signals and slots.
57 void ModuleBase_ParamIntSpinBox::connectSignalsAndSlots()
59 connect(this, SIGNAL(valueChanged(const QString&)),
60 this, SLOT(onTextChanged(const QString&)));
63 void ModuleBase_ParamIntSpinBox::onTextChanged(const QString& text)
68 int ModuleBase_ParamIntSpinBox::valueFromText(const QString& theText) const
70 if (!hasVariable(theText))
71 return ModuleBase_IntSpinBox::valueFromText(theText);
73 // small hack: return hash of the string to initiate valuesChanged signal
74 return qHash(theText);
77 QString ModuleBase_ParamIntSpinBox::textFromValue(int theValue) const
79 if ((!myTextValue.isEmpty()) && hasVariable(myTextValue)){
82 return ModuleBase_IntSpinBox::textFromValue(theValue);
86 \brief This function is used to determine whether input is valid.
87 \param str currently entered value
88 \param pos cursor position in the string
89 \return validating operation result
91 QValidator::State ModuleBase_ParamIntSpinBox::validate(QString& str, int& pos) const
93 // Trying to interpret the current input text as a numeric value
94 if (!hasVariable(str))
95 return ModuleBase_IntSpinBox::validate(str, pos);
97 QValidator::State res = QValidator::Invalid;
98 if (isAcceptVariables()) {
99 res = QValidator::Acceptable;
105 \brief This function is used to set a current value for this spinbox.
106 \param value current value
108 The new value is ignored if the spinbox has a variable.
110 void ModuleBase_ParamIntSpinBox::setValue(int value)
115 myTextValue = ModuleBase_IntSpinBox::textFromValue(value);
116 ModuleBase_IntSpinBox::setValue(value);
120 \brief This function is used to set a text for this spinbox.
121 \param value current value
123 void ModuleBase_ParamIntSpinBox::setText(const QString& value)
126 lineEdit()->setText(value);
130 \brief Enables or disables variable names in the spin box.
131 By default, variable names are enabled.
132 \param flag If true, variable names are enabled.
134 void ModuleBase_ParamIntSpinBox::setAcceptVariables(const bool flag)
136 myAcceptVariables = flag;
140 \brief Returns true if the spin box accepts variable names.
142 bool ModuleBase_ParamIntSpinBox::isAcceptVariables() const
144 return myAcceptVariables;
147 bool ModuleBase_ParamIntSpinBox::hasVariable() const
149 if (myTextValue.isEmpty())
151 return hasVariable(myTextValue);
154 bool ModuleBase_ParamIntSpinBox::hasVariable(const QString& theText) const
157 QLocale::c().toInt(theText, &ok);
162 \brief This function is used to determine whether input is valid.
163 \return validating operation result
165 ModuleBase_ParamIntSpinBox::State ModuleBase_ParamIntSpinBox::isValid(
166 const QString& theText, double& theValue) const
168 if (hasVariable() && !findVariable(theText, theValue)) {
170 theValue = locale().toInt(theText, &ok);
175 if (!checkRange(theValue)) {
183 \brief This function is used to check that string value lies within predefined range.
186 bool ModuleBase_ParamIntSpinBox::checkRange(const double theValue) const
188 return theValue >= minimum() && theValue <= maximum();
192 \brief This function is used to determine whether input is a variable name and to get its value.
193 \return status of search operation
195 bool ModuleBase_ParamIntSpinBox::findVariable(const QString& theName,
196 double& outValue) const
198 ResultParameterPtr aParam;
199 return ModelAPI_Tools::findVariable(FeaturePtr(), theName.toStdString(), outValue, aParam);
203 \brief This function is called when the spinbox receives show event.
205 void ModuleBase_ParamIntSpinBox::showEvent(QShowEvent* theEvent)
207 ModuleBase_IntSpinBox::showEvent(theEvent);
208 if ((!myTextValue.isEmpty()) && hasVariable(myTextValue)) {
209 setText(myTextValue);