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_ResultParameter.h>
8 #include <ModelAPI_AttributeDouble.h>
9 #include <ModelAPI_Tools.h>
16 #include <QApplication>
22 ModuleBase_ParamIntSpinBox::ModuleBase_ParamIntSpinBox(QWidget* theParent)
23 : ModuleBase_IntSpinBox(theParent),
24 myAcceptVariables(true)
26 connectSignalsAndSlots();
32 ModuleBase_ParamIntSpinBox::~ModuleBase_ParamIntSpinBox()
37 \brief Perform \a steps increment/decrement steps.
39 Re-implemented to handle cases when Notebook variable
40 name is specified by the user as the widget text.
41 Otherwise, simply calls the base implementation.
43 \param steps number of increment/decrement steps
45 void ModuleBase_ParamIntSpinBox::stepBy(int steps)
47 if ((!myTextValue.isEmpty()) && hasVariable())
50 ModuleBase_IntSpinBox::stepBy(steps);
54 \brief Connect signals and slots.
56 void ModuleBase_ParamIntSpinBox::connectSignalsAndSlots()
58 connect(this, SIGNAL(valueChanged(const QString&)),
59 this, SLOT(onTextChanged(const QString&)));
62 void ModuleBase_ParamIntSpinBox::onTextChanged(const QString& text)
67 int ModuleBase_ParamIntSpinBox::valueFromText(const QString& theText) const
69 if (!hasVariable(theText))
70 return ModuleBase_IntSpinBox::valueFromText(theText);
72 // small hack: return hash of the string to initiate valuesChanged signal
73 return qHash(theText);
76 QString ModuleBase_ParamIntSpinBox::textFromValue(int theValue) const
78 if ((!myTextValue.isEmpty()) && hasVariable(myTextValue)){
81 return ModuleBase_IntSpinBox::textFromValue(theValue);
85 \brief This function is used to determine whether input is valid.
86 \param str currently entered value
87 \param pos cursor position in the string
88 \return validating operation result
90 QValidator::State ModuleBase_ParamIntSpinBox::validate(QString& str, int& pos) const
92 // Trying to interpret the current input text as a numeric value
93 if (!hasVariable(str))
94 return ModuleBase_IntSpinBox::validate(str, pos);
96 QValidator::State res = QValidator::Invalid;
97 if (isAcceptVariables()) {
98 res = QValidator::Acceptable;
104 \brief This function is used to set a current value for this spinbox.
105 \param value current value
107 The new value is ignored if the spinbox has a variable.
109 void ModuleBase_ParamIntSpinBox::setValue(int value)
114 myTextValue = ModuleBase_IntSpinBox::textFromValue(value);
115 ModuleBase_IntSpinBox::setValue(value);
119 \brief This function is used to set a text for this spinbox.
120 \param value current value
122 void ModuleBase_ParamIntSpinBox::setText(const QString& value)
125 lineEdit()->setText(value);
129 \brief Enables or disables variable names in the spin box.
130 By default, variable names are enabled.
131 \param flag If true, variable names are enabled.
133 void ModuleBase_ParamIntSpinBox::setAcceptVariables(const bool flag)
135 myAcceptVariables = flag;
139 \brief Returns true if the spin box accepts variable names.
141 bool ModuleBase_ParamIntSpinBox::isAcceptVariables() const
143 return myAcceptVariables;
146 bool ModuleBase_ParamIntSpinBox::hasVariable() const
148 if (myTextValue.isEmpty())
150 return hasVariable(myTextValue);
153 bool ModuleBase_ParamIntSpinBox::hasVariable(const QString& theText) const
156 QLocale::c().toInt(theText, &ok);
161 \brief This function is used to determine whether input is valid.
162 \return validating operation result
164 ModuleBase_ParamIntSpinBox::State ModuleBase_ParamIntSpinBox::isValid(
165 const QString& theText, double& theValue) const
167 if (hasVariable() && !findVariable(theText, theValue)) {
169 theValue = locale().toInt(theText, &ok);
174 if (!checkRange(theValue)) {
182 \brief This function is used to check that string value lies within predefined range.
185 bool ModuleBase_ParamIntSpinBox::checkRange(const double theValue) const
187 return theValue >= minimum() && theValue <= maximum();
191 \brief This function is used to determine whether input is a variable name and to get its value.
192 \return status of search operation
194 bool ModuleBase_ParamIntSpinBox::findVariable(const QString& theName,
195 double& outValue) const
197 ResultParameterPtr aParam;
198 return ModelAPI_Tools::findVariable(FeaturePtr(), theName.toStdString(), outValue, aParam);
202 \brief This function is called when the spinbox receives show event.
204 void ModuleBase_ParamIntSpinBox::showEvent(QShowEvent* theEvent)
206 ModuleBase_IntSpinBox::showEvent(theEvent);
207 if ((!myTextValue.isEmpty()) && hasVariable(myTextValue)) {
208 setText(myTextValue);