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);
90 findVariable(theText, aValue);
94 QString ModuleBase_ParamSpinBox::textFromValue (double theValue) const
96 if (hasVariable(myTextValue)){
99 return ModuleBase_DoubleSpinBox::textFromValue(theValue);
103 \brief This function is used to determine whether input is valid.
104 \param str currently entered value
105 \param pos cursor position in the string
106 \return validating operation result
108 QValidator::State ModuleBase_ParamSpinBox::validate(QString& str, int& pos) const
110 // Trying to interpret the current input text as a numeric value
111 if (!hasVariable(str))
112 return ModuleBase_DoubleSpinBox::validate(str, pos);
114 QValidator::State res = QValidator::Invalid;
116 // Considering the input text as a variable name
117 // Applying Python identifier syntax:
118 // either a string starting with a letter, or a string starting with
119 // an underscore followed by at least one alphanumeric character
120 if (isAcceptVariables()) {
121 QRegExp varNameMask("[_a-zA-Z][a-zA-Z0-9_]*");
122 if (varNameMask.exactMatch(str))
123 res = QValidator::Acceptable;
125 if (res == QValidator::Invalid) {
126 varNameMask.setPattern("_");
127 if (varNameMask.exactMatch(str))
128 res = QValidator::Intermediate;
135 \brief This function is used to set a current value for this spinbox.
136 \param value current value
138 void ModuleBase_ParamSpinBox::setValue(const double value)
140 ModuleBase_DoubleSpinBox::setValue(value);
142 myTextValue = ModuleBase_DoubleSpinBox::textFromValue(value);
146 \brief This function is used to set a text for this spinbox.
147 \param value current value
149 void ModuleBase_ParamSpinBox::setText(const QString& value)
151 lineEdit()->setText(value);
155 \brief Enables or disables variable names in the spin box.
156 By default, variable names are enabled.
157 \param flag If true, variable names are enabled.
159 void ModuleBase_ParamSpinBox::setAcceptVariables(const bool flag)
161 myAcceptVariables = flag;
165 \brief Returns true if the spin box accepts variable names.
167 bool ModuleBase_ParamSpinBox::isAcceptVariables() const
169 return myAcceptVariables;
172 bool ModuleBase_ParamSpinBox::hasVariable() const
174 return hasVariable(myTextValue);
177 bool ModuleBase_ParamSpinBox::hasVariable(const QString& theText) const
179 QRegExp varNameMask("([a-z]|[A-Z]|_).*");
180 return varNameMask.exactMatch(theText);
184 \brief This function is used to determine whether input is valid.
185 \return validating operation result
187 ModuleBase_ParamSpinBox::State ModuleBase_ParamSpinBox::isValid(const QString& theText,
188 double& theValue) const
190 if (hasVariable() && !findVariable(theText, theValue)) {
192 theValue = locale().toDouble(theText, &ok);
197 if (!checkRange(theValue)) {
205 \brief This function is used to check that string value lies within predefined range.
208 bool ModuleBase_ParamSpinBox::checkRange(const double theValue) const
210 return theValue >= minimum() && theValue <= maximum();
214 \brief This function is used to determine whether input is a variable name and to get its value.
215 \return status of search operation
217 bool ModuleBase_ParamSpinBox::findVariable(const QString& theName,
218 double& outValue) const
221 return ModelAPI_Tools::findVariable(theName.toStdString(), outValue);
225 \brief This function is called when the spinbox recieves key press event.
227 void ModuleBase_ParamSpinBox::keyPressEvent(QKeyEvent* e)
229 if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
230 QWidget::keyPressEvent(e);
232 ModuleBase_DoubleSpinBox::keyPressEvent(e);
237 \brief This function is called when the spinbox recieves show event.
239 void ModuleBase_ParamSpinBox::showEvent(QShowEvent* theEvent)
241 ModuleBase_DoubleSpinBox::showEvent(theEvent);
242 if (hasVariable(myTextValue)) {
243 setText(myTextValue);