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-z]|[A-Z])([a-z]|[A-Z]|[0-9]|_)*)|"
160 "(_([a-z]|[A-Z]|[0-9])+([a-z]|[A-Z]|[0-9]|_)*)");
161 if (varNameMask.exactMatch(str))
162 res = QValidator::Acceptable;
164 if (res == QValidator::Invalid) {
165 varNameMask.setPattern("_");
166 if (varNameMask.exactMatch(str))
167 res = QValidator::Intermediate;
171 // Trying to interpret the current input text as a numeric value
172 if (res == QValidator::Invalid)
173 res = ModuleBase_DoubleSpinBox::validate(str, pos);
179 \brief This function is used to set a default value for this spinbox.
180 \param value default value
182 void ModuleBase_ParamSpinBox::setDefaultValue(const double value)
184 myDefaultValue = value;
188 \brief This function is used to set a current value for this spinbox.
189 \param value current value
191 void ModuleBase_ParamSpinBox::setValue(const double value)
193 ModuleBase_DoubleSpinBox::setValue(value);
195 myCorrectValue = ModuleBase_DoubleSpinBox::textFromValue(value);
196 myTextValue = myCorrectValue;
200 \brief This function is used to set a text for this spinbox.
201 \param value current value
203 void ModuleBase_ParamSpinBox::setText(const QString& value)
205 lineEdit()->setText(value);
209 \brief Enables or disables variable names in the spin box.
210 By default, variable names are enabled.
211 \param flag If true, variable names are enabled.
213 void ModuleBase_ParamSpinBox::setAcceptVariables(const bool flag)
215 myAcceptVariables = flag;
219 \brief Returns true if the spin box accepts variable names.
221 bool ModuleBase_ParamSpinBox::isAcceptVariables() const
223 return myAcceptVariables;
226 bool ModuleBase_ParamSpinBox::hasVariables() const
228 return myHasVariables;
232 \brief This function is used to determine whether input is valid.
233 \return validating operation result
235 ModuleBase_ParamSpinBox::State ModuleBase_ParamSpinBox::isValid(const QString& theText,
236 double& theValue) const
238 if (!findVariable(theText, theValue)) {
240 theValue = locale().toDouble(theText, &ok);
245 if (!checkRange(theValue)) {
253 \brief This function return a default acceptable value (commonly, 0.0).
254 \return default acceptable value
256 double ModuleBase_ParamSpinBox::defaultValue() const
258 if (minimum() > myDefaultValue || maximum() < myDefaultValue)
261 return myDefaultValue;
265 \brief This function is used to check that string value lies within predefined range.
268 bool ModuleBase_ParamSpinBox::checkRange(const double theValue) const
270 return theValue >= minimum() && theValue <= maximum();
274 \brief This function is used to determine whether input is a variable name and to get its value.
275 \return status of search operation
277 bool ModuleBase_ParamSpinBox::findVariable(const QString& theName,
278 double& outValue) const
281 SessionPtr aSession = ModelAPI_Session::get();
282 DocumentPtr aDocument = aSession->activeDocument();
283 ObjectPtr aParamObj = aDocument->objectByName(ModelAPI_ResultParameter::group(),
284 theName.toStdString());
285 ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
288 AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
289 outValue = aValueAttribute->value();
294 \brief This function is called when the spinbox recieves key press event.
296 void ModuleBase_ParamSpinBox::keyPressEvent(QKeyEvent* e)
298 if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
299 QWidget::keyPressEvent(e);
301 ModuleBase_DoubleSpinBox::keyPressEvent(e);
306 \brief This function is called when the spinbox recieves show event.
308 void ModuleBase_ParamSpinBox::showEvent(QShowEvent*)
310 setText(myTextValue);