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>
17 \class ModuleBase_ParamSpinBox
23 Constructs a spin box with 0.0 as minimum value and 99.99 as maximum value,
24 a step value of 1.0 and a precision of 2 decimal places.
25 The value is initially set to 0.00.
27 \param parent parent object
29 ModuleBase_ParamSpinBox::ModuleBase_ParamSpinBox(QWidget* theParent, int thePrecision)
30 : ModuleBase_DoubleSpinBox(theParent, thePrecision),
31 myAcceptVariables(true)
33 connectSignalsAndSlots();
39 ModuleBase_ParamSpinBox::~ModuleBase_ParamSpinBox()
44 \brief Perform \a steps increment/decrement steps.
46 Re-implemented to handle cases when Notebook variable
47 name is specified by the user as the widget text.
48 Otherwise, simply calls the base implementation.
50 \param steps number of increment/decrement steps
52 void ModuleBase_ParamSpinBox::stepBy(int steps)
57 ModuleBase_DoubleSpinBox::stepBy(steps);
61 \brief Connect signals and slots.
63 void ModuleBase_ParamSpinBox::connectSignalsAndSlots()
65 connect(this, SIGNAL(editingFinished()),
66 this, SLOT(onEditingFinished()));
68 connect(this, SIGNAL(valueChanged(const QString&)),
69 this, SLOT(onTextChanged(const QString&)));
71 //connect(lineEdit(), SIGNAL(textChanged(const QString&)),
72 // this, SLOT(onTextChanged(const QString&)));
74 //connect(lineEdit(), SIGNAL(textChanged(const QString&)),
75 // this, SIGNAL(textChanged(const QString&)));
79 \brief This function is called when editing is finished.
81 void ModuleBase_ParamSpinBox::onEditingFinished()
83 if (myTextValue.isNull())
90 \brief This function is called when value is changed.
92 void ModuleBase_ParamSpinBox::onTextChanged(const QString& text)
97 if (isValid(text, value) == Acceptable) {
98 myCorrectValue = text;
103 \brief Interpret text entered by the user as a value.
104 \param text text entered by the user
108 double ModuleBase_ParamSpinBox::valueFromText(const QString& theText) const
110 if (!hasVariable(theText))
111 return ModuleBase_DoubleSpinBox::valueFromText(theText);
114 findVariable(theText, aValue);
119 \brief This function is used to determine whether input is valid.
120 \param str currently entered value
121 \param pos cursor position in the string
122 \return validating operation result
124 QValidator::State ModuleBase_ParamSpinBox::validate(QString& str, int& pos) const
126 // Trying to interpret the current input text as a numeric value
127 if (!hasVariable(str))
128 return ModuleBase_DoubleSpinBox::validate(str, pos);
130 QValidator::State res = QValidator::Invalid;
132 // Considering the input text as a variable name
133 // Applying Python identifier syntax:
134 // either a string starting with a letter, or a string starting with
135 // an underscore followed by at least one alphanumeric character
136 if (isAcceptVariables()) {
137 QRegExp varNameMask("[_a-zA-Z][a-zA-Z0-9_]*");
138 if (varNameMask.exactMatch(str))
139 res = QValidator::Acceptable;
141 if (res == QValidator::Invalid) {
142 varNameMask.setPattern("_");
143 if (varNameMask.exactMatch(str))
144 res = QValidator::Intermediate;
151 \brief This function is used to set a current value for this spinbox.
152 \param value current value
154 void ModuleBase_ParamSpinBox::setValue(const double value)
156 ModuleBase_DoubleSpinBox::setValue(value);
158 myCorrectValue = ModuleBase_DoubleSpinBox::textFromValue(value);
159 myTextValue = myCorrectValue;
163 \brief This function is used to set a text for this spinbox.
164 \param value current value
166 void ModuleBase_ParamSpinBox::setText(const QString& value)
168 lineEdit()->setText(value);
172 \brief Enables or disables variable names in the spin box.
173 By default, variable names are enabled.
174 \param flag If true, variable names are enabled.
176 void ModuleBase_ParamSpinBox::setAcceptVariables(const bool flag)
178 myAcceptVariables = flag;
182 \brief Returns true if the spin box accepts variable names.
184 bool ModuleBase_ParamSpinBox::isAcceptVariables() const
186 return myAcceptVariables;
189 bool ModuleBase_ParamSpinBox::hasVariable() const
191 return hasVariable(text());
194 bool ModuleBase_ParamSpinBox::hasVariable(const QString& theText) const
196 QRegExp varNameMask("([a-z]|[A-Z]|_).*");
197 return varNameMask.exactMatch(theText);
201 \brief This function is used to determine whether input is valid.
202 \return validating operation result
204 ModuleBase_ParamSpinBox::State ModuleBase_ParamSpinBox::isValid(const QString& theText,
205 double& theValue) const
207 if (hasVariable() && !findVariable(theText, theValue)) {
209 theValue = locale().toDouble(theText, &ok);
214 if (!checkRange(theValue)) {
222 \brief This function is used to check that string value lies within predefined range.
225 bool ModuleBase_ParamSpinBox::checkRange(const double theValue) const
227 return theValue >= minimum() && theValue <= maximum();
231 \brief This function is used to determine whether input is a variable name and to get its value.
232 \return status of search operation
234 bool ModuleBase_ParamSpinBox::findVariable(const QString& theName,
235 double& outValue) const
238 return ModelAPI_Tools::findVariable(theName.toStdString(), outValue);
242 \brief This function is called when the spinbox recieves key press event.
244 void ModuleBase_ParamSpinBox::keyPressEvent(QKeyEvent* e)
246 if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
247 QWidget::keyPressEvent(e);
249 ModuleBase_DoubleSpinBox::keyPressEvent(e);
254 \brief This function is called when the spinbox recieves show event.
256 void ModuleBase_ParamSpinBox::showEvent(QShowEvent* theEvent)
258 ModuleBase_DoubleSpinBox::showEvent(theEvent);
259 if (hasVariable(myTextValue)) {
260 setText(myTextValue);