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&)));
69 //connect(lineEdit(), SIGNAL(textChanged(const QString&)),
70 // this, SLOT(onTextChanged(const QString&)));
72 //connect(lineEdit(), SIGNAL(textChanged(const QString&)),
73 // this, SIGNAL(textChanged(const QString&)));
77 \brief This function is called when value is changed.
79 void ModuleBase_ParamSpinBox::onTextChanged(const QString& text)
84 if (isValid(text, value) == Acceptable) {
85 myCorrectValue = text;
90 \brief Interpret text entered by the user as a value.
91 \param text text entered by the user
95 double ModuleBase_ParamSpinBox::valueFromText(const QString& theText) const
97 if (!hasVariable(theText))
98 return ModuleBase_DoubleSpinBox::valueFromText(theText);
101 findVariable(theText, aValue);
106 \brief This function is used to determine whether input is valid.
107 \param str currently entered value
108 \param pos cursor position in the string
109 \return validating operation result
111 QValidator::State ModuleBase_ParamSpinBox::validate(QString& str, int& pos) const
113 // Trying to interpret the current input text as a numeric value
114 if (!hasVariable(str))
115 return ModuleBase_DoubleSpinBox::validate(str, pos);
117 QValidator::State res = QValidator::Invalid;
119 // Considering the input text as a variable name
120 // Applying Python identifier syntax:
121 // either a string starting with a letter, or a string starting with
122 // an underscore followed by at least one alphanumeric character
123 if (isAcceptVariables()) {
124 QRegExp varNameMask("[_a-zA-Z][a-zA-Z0-9_]*");
125 if (varNameMask.exactMatch(str))
126 res = QValidator::Acceptable;
128 if (res == QValidator::Invalid) {
129 varNameMask.setPattern("_");
130 if (varNameMask.exactMatch(str))
131 res = QValidator::Intermediate;
138 \brief This function is used to set a current value for this spinbox.
139 \param value current value
141 void ModuleBase_ParamSpinBox::setValue(const double value)
143 ModuleBase_DoubleSpinBox::setValue(value);
145 myCorrectValue = ModuleBase_DoubleSpinBox::textFromValue(value);
146 myTextValue = myCorrectValue;
150 \brief This function is used to set a text for this spinbox.
151 \param value current value
153 void ModuleBase_ParamSpinBox::setText(const QString& value)
155 lineEdit()->setText(value);
159 \brief Enables or disables variable names in the spin box.
160 By default, variable names are enabled.
161 \param flag If true, variable names are enabled.
163 void ModuleBase_ParamSpinBox::setAcceptVariables(const bool flag)
165 myAcceptVariables = flag;
169 \brief Returns true if the spin box accepts variable names.
171 bool ModuleBase_ParamSpinBox::isAcceptVariables() const
173 return myAcceptVariables;
176 bool ModuleBase_ParamSpinBox::hasVariable() const
178 return hasVariable(text());
181 bool ModuleBase_ParamSpinBox::hasVariable(const QString& theText) const
183 QRegExp varNameMask("([a-z]|[A-Z]|_).*");
184 return varNameMask.exactMatch(theText);
188 \brief This function is used to determine whether input is valid.
189 \return validating operation result
191 ModuleBase_ParamSpinBox::State ModuleBase_ParamSpinBox::isValid(const QString& theText,
192 double& theValue) const
194 if (hasVariable() && !findVariable(theText, theValue)) {
196 theValue = locale().toDouble(theText, &ok);
201 if (!checkRange(theValue)) {
209 \brief This function is used to check that string value lies within predefined range.
212 bool ModuleBase_ParamSpinBox::checkRange(const double theValue) const
214 return theValue >= minimum() && theValue <= maximum();
218 \brief This function is used to determine whether input is a variable name and to get its value.
219 \return status of search operation
221 bool ModuleBase_ParamSpinBox::findVariable(const QString& theName,
222 double& outValue) const
225 return ModelAPI_Tools::findVariable(theName.toStdString(), outValue);
229 \brief This function is called when the spinbox recieves key press event.
231 void ModuleBase_ParamSpinBox::keyPressEvent(QKeyEvent* e)
233 if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
234 QWidget::keyPressEvent(e);
236 ModuleBase_DoubleSpinBox::keyPressEvent(e);
241 \brief This function is called when the spinbox recieves show event.
243 void ModuleBase_ParamSpinBox::showEvent(QShowEvent* theEvent)
245 ModuleBase_DoubleSpinBox::showEvent(theEvent);
246 if (hasVariable(myTextValue)) {
247 setText(myTextValue);