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>
19 \class ModuleBase_ParamSpinBox
25 Constructs a spin box with 0.0 as minimum value and 99.99 as maximum value,
26 a step value of 1.0 and a precision of 2 decimal places.
27 The value is initially set to 0.00.
29 \param parent parent object
31 ModuleBase_ParamSpinBox::ModuleBase_ParamSpinBox(QWidget* theParent, int thePrecision)
32 : ModuleBase_DoubleSpinBox(theParent, thePrecision),
33 myAcceptVariables(true)
35 connectSignalsAndSlots();
41 ModuleBase_ParamSpinBox::~ModuleBase_ParamSpinBox()
46 \brief Perform \a steps increment/decrement steps.
48 Re-implemented to handle cases when Notebook variable
49 name is specified by the user as the widget text.
50 Otherwise, simply calls the base implementation.
52 \param steps number of increment/decrement steps
54 void ModuleBase_ParamSpinBox::stepBy(int steps)
59 ModuleBase_DoubleSpinBox::stepBy(steps);
63 \brief Connect signals and slots.
65 void ModuleBase_ParamSpinBox::connectSignalsAndSlots()
67 connect(this, SIGNAL(valueChanged(const QString&)),
68 this, SLOT(onTextChanged(const QString&)));
72 \brief This function is called when value is changed.
74 void ModuleBase_ParamSpinBox::onTextChanged(const QString& text)
80 \brief Interpret text entered by the user as a value.
81 \param text text entered by the user
85 double ModuleBase_ParamSpinBox::valueFromText(const QString& theText) const
87 if (!hasVariable(theText)) {
88 return ModuleBase_DoubleSpinBox::valueFromText(theText);
90 // small hack: return length of the string to iniiate valuesChanged signal
91 return theText.length();
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;
115 if (isAcceptVariables()) {
116 res = QValidator::Acceptable;
122 \brief This function is used to set a current value for this spinbox.
123 \param value current value
125 void ModuleBase_ParamSpinBox::setValue(const double value)
127 ModuleBase_DoubleSpinBox::setValue(value);
129 myTextValue = ModuleBase_DoubleSpinBox::textFromValue(value);
133 \brief This function is used to set a text for this spinbox.
134 \param value current value
136 void ModuleBase_ParamSpinBox::setText(const QString& value)
138 lineEdit()->setText(value);
142 \brief Enables or disables variable names in the spin box.
143 By default, variable names are enabled.
144 \param flag If true, variable names are enabled.
146 void ModuleBase_ParamSpinBox::setAcceptVariables(const bool flag)
148 myAcceptVariables = flag;
152 \brief Returns true if the spin box accepts variable names.
154 bool ModuleBase_ParamSpinBox::isAcceptVariables() const
156 return myAcceptVariables;
159 bool ModuleBase_ParamSpinBox::hasVariable() const
161 if (myTextValue.isEmpty())
163 return hasVariable(myTextValue);
166 bool ModuleBase_ParamSpinBox::hasVariable(const QString& theText) const
168 QString aPattern = QString("[-+]?[0-9]*[%1]?[0-9]+([eE][-+]?[0-9]+)?")
169 .arg(QLocale::system().decimalPoint());
170 QRegExp varNameMask(aPattern);
171 return !varNameMask.exactMatch(theText);
175 \brief This function is used to determine whether input is valid.
176 \return validating operation result
178 ModuleBase_ParamSpinBox::State ModuleBase_ParamSpinBox::isValid(const QString& theText,
179 double& theValue) const
181 if (hasVariable() && !findVariable(theText, theValue)) {
183 theValue = locale().toDouble(theText, &ok);
188 if (!checkRange(theValue)) {
196 \brief This function is used to check that string value lies within predefined range.
199 bool ModuleBase_ParamSpinBox::checkRange(const double theValue) const
201 return theValue >= minimum() && theValue <= maximum();
205 \brief This function is used to determine whether input is a variable name and to get its value.
206 \return status of search operation
208 bool ModuleBase_ParamSpinBox::findVariable(const QString& theName,
209 double& outValue) const
211 ResultParameterPtr aParam;
212 return ModelAPI_Tools::findVariable(theName.toStdString(), outValue, aParam);
216 \brief This function is called when the spinbox recieves key press event.
218 void ModuleBase_ParamSpinBox::keyPressEvent(QKeyEvent* e)
220 if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
221 QWidget::keyPressEvent(e);
223 ModuleBase_DoubleSpinBox::keyPressEvent(e);
228 \brief This function is called when the spinbox recieves show event.
230 void ModuleBase_ParamSpinBox::showEvent(QShowEvent* theEvent)
232 ModuleBase_DoubleSpinBox::showEvent(theEvent);
233 if (hasVariable(myTextValue)) {
234 setText(myTextValue);