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),
31 myHasVariables(false),
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)
56 QString pref = prefix();
57 QString suff = suffix();
59 if (pref.length() && str.startsWith(pref))
60 str = str.right(str.length() - pref.length());
61 if (suff.length() && str.endsWith(suff))
62 str = str.left(str.length() - suff.length());
64 QRegExp varNameMask("([a-z]|[A-Z]|_).*");
65 if (varNameMask.exactMatch(str))
68 ModuleBase_DoubleSpinBox::stepBy(steps);
72 \brief Connect signals and slots.
74 void ModuleBase_ParamSpinBox::connectSignalsAndSlots()
76 connect(this, SIGNAL(editingFinished()),
77 this, SLOT(onEditingFinished()));
79 connect(this, SIGNAL(valueChanged(const QString&)),
80 this, SLOT(onTextChanged(const QString&)));
82 //connect(lineEdit(), SIGNAL(textChanged(const QString&)),
83 // this, SLOT(onTextChanged(const QString&)));
85 //connect(lineEdit(), SIGNAL(textChanged(const QString&)),
86 // this, SIGNAL(textChanged(const QString&)));
90 \brief This function is called when editing is finished.
92 void ModuleBase_ParamSpinBox::onEditingFinished()
94 if (myTextValue.isNull())
101 \brief This function is called when value is changed.
103 void ModuleBase_ParamSpinBox::onTextChanged(const QString& text)
108 if (isValid(text, value) == Acceptable) {
109 myCorrectValue = text;
110 myHasVariables = true;
112 myHasVariables = false;
117 \brief Interpret text entered by the user as a value.
118 \param text text entered by the user
122 double ModuleBase_ParamSpinBox::valueFromText(const QString& theText) const
125 if (isValid(theText, aValue) == Acceptable)
128 return defaultValue();
132 \brief This function is used by the spin box whenever it needs to display
135 \param val spin box value
136 \return text representation of the value
139 QString ModuleBase_ParamSpinBox::textFromValue(double val) const
141 return ModuleBase_DoubleSpinBox::textFromValue(val);
145 \brief This function is used to determine whether input is valid.
146 \param str currently entered value
147 \param pos cursor position in the string
148 \return validating operation result
150 QValidator::State ModuleBase_ParamSpinBox::validate(QString& str, int& pos) const
152 QValidator::State res = QValidator::Invalid;
154 // Considering the input text as a variable name
155 // Applying Python identifier syntax:
156 // either a string starting with a letter, or a string starting with
157 // an underscore followed by at least one alphanumeric character
158 if (isAcceptVariables()) {
159 QRegExp varNameMask("[_a-zA-Z][a-zA-Z0-9_]*");
160 if (varNameMask.exactMatch(str))
161 res = QValidator::Acceptable;
163 if (res == QValidator::Invalid) {
164 varNameMask.setPattern("_");
165 if (varNameMask.exactMatch(str))
166 res = QValidator::Intermediate;
170 // Trying to interpret the current input text as a numeric value
171 if (res == QValidator::Invalid)
172 res = ModuleBase_DoubleSpinBox::validate(str, pos);
178 \brief This function is used to set a default value for this spinbox.
179 \param value default value
181 void ModuleBase_ParamSpinBox::setDefaultValue(const double value)
183 myDefaultValue = value;
187 \brief This function is used to set a current value for this spinbox.
188 \param value current value
190 void ModuleBase_ParamSpinBox::setValue(const double value)
192 ModuleBase_DoubleSpinBox::setValue(value);
194 myCorrectValue = ModuleBase_DoubleSpinBox::textFromValue(value);
195 myTextValue = myCorrectValue;
199 \brief This function is used to set a text for this spinbox.
200 \param value current value
202 void ModuleBase_ParamSpinBox::setText(const QString& value)
204 lineEdit()->setText(value);
208 \brief Enables or disables variable names in the spin box.
209 By default, variable names are enabled.
210 \param flag If true, variable names are enabled.
212 void ModuleBase_ParamSpinBox::setAcceptVariables(const bool flag)
214 myAcceptVariables = flag;
218 \brief Returns true if the spin box accepts variable names.
220 bool ModuleBase_ParamSpinBox::isAcceptVariables() const
222 return myAcceptVariables;
225 bool ModuleBase_ParamSpinBox::hasVariables() const
227 return myHasVariables;
231 \brief This function is used to determine whether input is valid.
232 \return validating operation result
234 ModuleBase_ParamSpinBox::State ModuleBase_ParamSpinBox::isValid(const QString& theText,
235 double& theValue) const
237 if (!findVariable(theText, theValue)) {
239 theValue = locale().toDouble(theText, &ok);
244 if (!checkRange(theValue)) {
252 \brief This function return a default acceptable value (commonly, 0.0).
253 \return default acceptable value
255 double ModuleBase_ParamSpinBox::defaultValue() const
257 if (minimum() > myDefaultValue || maximum() < myDefaultValue)
260 return myDefaultValue;
264 \brief This function is used to check that string value lies within predefined range.
267 bool ModuleBase_ParamSpinBox::checkRange(const double theValue) const
269 return theValue >= minimum() && theValue <= maximum();
273 \brief This function is used to determine whether input is a variable name and to get its value.
274 \return status of search operation
276 bool ModuleBase_ParamSpinBox::findVariable(const QString& theName,
277 double& outValue) const
280 SessionPtr aSession = ModelAPI_Session::get();
281 DocumentPtr aDocument = aSession->activeDocument();
282 ObjectPtr aParamObj = aDocument->objectByName(ModelAPI_ResultParameter::group(),
283 theName.toStdString());
284 ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
287 AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
288 outValue = aValueAttribute->value();
293 \brief This function is called when the spinbox recieves key press event.
295 void ModuleBase_ParamSpinBox::keyPressEvent(QKeyEvent* e)
297 if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
298 QWidget::keyPressEvent(e);
300 ModuleBase_DoubleSpinBox::keyPressEvent(e);
305 \brief This function is called when the spinbox recieves show event.
307 void ModuleBase_ParamSpinBox::showEvent(QShowEvent*)
309 setText(myTextValue);