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>
16 \class ModuleBase_ParamSpinBox
22 Constructs a spin box with 0.0 as minimum value and 99.99 as maximum value,
23 a step value of 1.0 and a precision of 2 decimal places.
24 The value is initially set to 0.00.
26 \param parent parent object
28 ModuleBase_ParamSpinBox::ModuleBase_ParamSpinBox(QWidget* theParent, int thePrecision)
29 : ModuleBase_DoubleSpinBox(theParent, thePrecision),
30 myAcceptVariables(true)
32 connectSignalsAndSlots();
38 ModuleBase_ParamSpinBox::~ModuleBase_ParamSpinBox()
43 \brief Perform \a steps increment/decrement steps.
45 Re-implemented to handle cases when Notebook variable
46 name is specified by the user as the widget text.
47 Otherwise, simply calls the base implementation.
49 \param steps number of increment/decrement steps
51 void ModuleBase_ParamSpinBox::stepBy(int steps)
56 ModuleBase_DoubleSpinBox::stepBy(steps);
60 \brief Connect signals and slots.
62 void ModuleBase_ParamSpinBox::connectSignalsAndSlots()
64 connect(this, SIGNAL(editingFinished()),
65 this, SLOT(onEditingFinished()));
67 connect(this, SIGNAL(valueChanged(const QString&)),
68 this, SLOT(onTextChanged(const QString&)));
70 //connect(lineEdit(), SIGNAL(textChanged(const QString&)),
71 // this, SLOT(onTextChanged(const QString&)));
73 //connect(lineEdit(), SIGNAL(textChanged(const QString&)),
74 // this, SIGNAL(textChanged(const QString&)));
78 \brief This function is called when editing is finished.
80 void ModuleBase_ParamSpinBox::onEditingFinished()
82 if (myTextValue.isNull())
89 \brief This function is called when value is changed.
91 void ModuleBase_ParamSpinBox::onTextChanged(const QString& text)
96 if (isValid(text, value) == Acceptable) {
97 myCorrectValue = text;
102 \brief Interpret text entered by the user as a value.
103 \param text text entered by the user
107 double ModuleBase_ParamSpinBox::valueFromText(const QString& theText) const
109 if (!hasVariable(theText))
110 return ModuleBase_DoubleSpinBox::valueFromText(theText);
113 findVariable(theText, aValue);
118 \brief This function is used to determine whether input is valid.
119 \param str currently entered value
120 \param pos cursor position in the string
121 \return validating operation result
123 QValidator::State ModuleBase_ParamSpinBox::validate(QString& str, int& pos) const
125 // Trying to interpret the current input text as a numeric value
126 if (!hasVariable(str))
127 return ModuleBase_DoubleSpinBox::validate(str, pos);
129 QValidator::State res = QValidator::Invalid;
131 // Considering the input text as a variable name
132 // Applying Python identifier syntax:
133 // either a string starting with a letter, or a string starting with
134 // an underscore followed by at least one alphanumeric character
135 if (isAcceptVariables()) {
136 QRegExp varNameMask("[_a-zA-Z][a-zA-Z0-9_]*");
137 if (varNameMask.exactMatch(str))
138 res = QValidator::Acceptable;
140 if (res == QValidator::Invalid) {
141 varNameMask.setPattern("_");
142 if (varNameMask.exactMatch(str))
143 res = QValidator::Intermediate;
150 \brief This function is used to set a current value for this spinbox.
151 \param value current value
153 void ModuleBase_ParamSpinBox::setValue(const double value)
155 ModuleBase_DoubleSpinBox::setValue(value);
157 myCorrectValue = ModuleBase_DoubleSpinBox::textFromValue(value);
158 myTextValue = myCorrectValue;
162 \brief This function is used to set a text for this spinbox.
163 \param value current value
165 void ModuleBase_ParamSpinBox::setText(const QString& value)
167 lineEdit()->setText(value);
171 \brief Enables or disables variable names in the spin box.
172 By default, variable names are enabled.
173 \param flag If true, variable names are enabled.
175 void ModuleBase_ParamSpinBox::setAcceptVariables(const bool flag)
177 myAcceptVariables = flag;
181 \brief Returns true if the spin box accepts variable names.
183 bool ModuleBase_ParamSpinBox::isAcceptVariables() const
185 return myAcceptVariables;
188 bool ModuleBase_ParamSpinBox::hasVariable() const
190 return hasVariable(text());
193 bool ModuleBase_ParamSpinBox::hasVariable(const QString& theText) const
195 QRegExp varNameMask("([a-z]|[A-Z]|_).*");
196 return varNameMask.exactMatch(theText);
200 \brief This function is used to determine whether input is valid.
201 \return validating operation result
203 ModuleBase_ParamSpinBox::State ModuleBase_ParamSpinBox::isValid(const QString& theText,
204 double& theValue) const
206 if (hasVariable() && !findVariable(theText, theValue)) {
208 theValue = locale().toDouble(theText, &ok);
213 if (!checkRange(theValue)) {
221 \brief This function is used to check that string value lies within predefined range.
224 bool ModuleBase_ParamSpinBox::checkRange(const double theValue) const
226 return theValue >= minimum() && theValue <= maximum();
230 \brief This function is used to determine whether input is a variable name and to get its value.
231 \return status of search operation
233 bool ModuleBase_ParamSpinBox::findVariable(const QString& theName,
234 double& outValue) const
237 SessionPtr aSession = ModelAPI_Session::get();
238 DocumentPtr aDocument = aSession->activeDocument();
239 ObjectPtr aParamObj = aDocument->objectByName(ModelAPI_ResultParameter::group(),
240 theName.toStdString());
241 ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
244 AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
245 outValue = aValueAttribute->value();
250 \brief This function is called when the spinbox recieves key press event.
252 void ModuleBase_ParamSpinBox::keyPressEvent(QKeyEvent* e)
254 if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
255 QWidget::keyPressEvent(e);
257 ModuleBase_DoubleSpinBox::keyPressEvent(e);
262 \brief This function is called when the spinbox recieves show event.
264 void ModuleBase_ParamSpinBox::showEvent(QShowEvent* theEvent)
266 ModuleBase_DoubleSpinBox::showEvent(theEvent);
267 //setText(myTextValue);