1 #include "ModuleBase_ParamSpinBox.h"
3 #include <ModelAPI_Session.h>
4 #include <ModelAPI_Document.h>
5 #include <ModelAPI_ResultParameter.h>
6 #include <ModelAPI_AttributeDouble.h>
7 #include <ModelAPI_Tools.h>
18 \class ModuleBase_ParamSpinBox
24 Constructs a spin box with 0.0 as minimum value and 99.99 as maximum value,
25 a step value of 1.0 and a precision of 2 decimal places.
26 The value is initially set to 0.00.
28 \param parent parent object
30 ModuleBase_ParamSpinBox::ModuleBase_ParamSpinBox(QWidget* theParent, int thePrecision)
31 : ModuleBase_DoubleSpinBox(theParent, thePrecision),
32 myAcceptVariables(true)
34 connectSignalsAndSlots();
40 ModuleBase_ParamSpinBox::~ModuleBase_ParamSpinBox()
45 \brief Perform \a steps increment/decrement steps.
47 Re-implemented to handle cases when Notebook variable
48 name is specified by the user as the widget text.
49 Otherwise, simply calls the base implementation.
51 \param steps number of increment/decrement steps
53 void ModuleBase_ParamSpinBox::stepBy(int steps)
58 ModuleBase_DoubleSpinBox::stepBy(steps);
62 \brief Connect signals and slots.
64 void ModuleBase_ParamSpinBox::connectSignalsAndSlots()
66 connect(this, SIGNAL(valueChanged(const QString&)),
67 this, SLOT(onTextChanged(const QString&)));
71 \brief This function is called when value is changed.
73 void ModuleBase_ParamSpinBox::onTextChanged(const QString& text)
79 \brief Interpret text entered by the user as a value.
80 \param text text entered by the user
84 double ModuleBase_ParamSpinBox::valueFromText(const QString& theText) const
86 if (!hasVariable(theText)) {
87 return ModuleBase_DoubleSpinBox::valueFromText(theText);
89 // small hack: return length of the string to iniiate valuesChanged signal
90 return theText.length();
93 QString ModuleBase_ParamSpinBox::textFromValue (double theValue) const
95 if (hasVariable(myTextValue)){
98 return ModuleBase_DoubleSpinBox::textFromValue(theValue);
102 \brief This function is used to determine whether input is valid.
103 \param str currently entered value
104 \param pos cursor position in the string
105 \return validating operation result
107 QValidator::State ModuleBase_ParamSpinBox::validate(QString& str, int& pos) const
109 // Trying to interpret the current input text as a numeric value
110 if (!hasVariable(str))
111 return ModuleBase_DoubleSpinBox::validate(str, pos);
113 QValidator::State res = QValidator::Invalid;
114 if (isAcceptVariables()) {
115 res = QValidator::Acceptable;
121 \brief This function is used to set a current value for this spinbox.
122 \param value current value
124 void ModuleBase_ParamSpinBox::setValue(const double value)
126 ModuleBase_DoubleSpinBox::setValue(value);
128 myTextValue = ModuleBase_DoubleSpinBox::textFromValue(value);
132 \brief This function is used to set a text for this spinbox.
133 \param value current value
135 void ModuleBase_ParamSpinBox::setText(const QString& value)
137 lineEdit()->setText(value);
141 \brief Enables or disables variable names in the spin box.
142 By default, variable names are enabled.
143 \param flag If true, variable names are enabled.
145 void ModuleBase_ParamSpinBox::setAcceptVariables(const bool flag)
147 myAcceptVariables = flag;
151 \brief Returns true if the spin box accepts variable names.
153 bool ModuleBase_ParamSpinBox::isAcceptVariables() const
155 return myAcceptVariables;
158 bool ModuleBase_ParamSpinBox::hasVariable() const
160 if (myTextValue.isEmpty())
162 return hasVariable(myTextValue);
165 bool ModuleBase_ParamSpinBox::hasVariable(const QString& theText) const
167 QRegExp varNameMask("[-+]?[0-9]*[.]?[0-9]+([eE][-+]?[0-9]+)?");
168 return !varNameMask.exactMatch(theText);
172 \brief This function is used to determine whether input is valid.
173 \return validating operation result
175 ModuleBase_ParamSpinBox::State ModuleBase_ParamSpinBox::isValid(const QString& theText,
176 double& theValue) const
178 if (hasVariable() && !findVariable(theText, theValue)) {
180 theValue = locale().toDouble(theText, &ok);
185 if (!checkRange(theValue)) {
193 \brief This function is used to check that string value lies within predefined range.
196 bool ModuleBase_ParamSpinBox::checkRange(const double theValue) const
198 return theValue >= minimum() && theValue <= maximum();
202 \brief This function is used to determine whether input is a variable name and to get its value.
203 \return status of search operation
205 bool ModuleBase_ParamSpinBox::findVariable(const QString& theName,
206 double& outValue) const
208 ResultParameterPtr aParam;
209 return ModelAPI_Tools::findVariable(theName.toStdString(), outValue, aParam);
213 \brief This function is called when the spinbox recieves key press event.
215 void ModuleBase_ParamSpinBox::keyPressEvent(QKeyEvent* e)
217 if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
218 QWidget::keyPressEvent(e);
220 ModuleBase_DoubleSpinBox::keyPressEvent(e);
225 \brief This function is called when the spinbox recieves show event.
227 void ModuleBase_ParamSpinBox::showEvent(QShowEvent* theEvent)
229 ModuleBase_DoubleSpinBox::showEvent(theEvent);
230 if (hasVariable(myTextValue)) {
231 setText(myTextValue);