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>
14 #include <QApplication>
20 ModuleBase_ParamSpinBox::ModuleBase_ParamSpinBox(QWidget* theParent, int thePrecision)
21 : ModuleBase_DoubleSpinBox(theParent, thePrecision),
22 myAcceptVariables(true)
24 connectSignalsAndSlots();
30 ModuleBase_ParamSpinBox::~ModuleBase_ParamSpinBox()
35 \brief Perform \a steps increment/decrement steps.
37 Re-implemented to handle cases when Notebook variable
38 name is specified by the user as the widget text.
39 Otherwise, simply calls the base implementation.
41 \param steps number of increment/decrement steps
43 void ModuleBase_ParamSpinBox::stepBy(int steps)
45 if ((!myTextValue.isEmpty()) && hasVariable())
48 ModuleBase_DoubleSpinBox::stepBy(steps);
52 \brief Connect signals and slots.
54 void ModuleBase_ParamSpinBox::connectSignalsAndSlots()
56 connect(this, SIGNAL(valueChanged(const QString&)),
57 this, SLOT(onTextChanged(const QString&)));
60 void ModuleBase_ParamSpinBox::onTextChanged(const QString& text)
65 double ModuleBase_ParamSpinBox::valueFromText(const QString& theText) const
67 if (!hasVariable(theText))
68 return ModuleBase_DoubleSpinBox::valueFromText(theText);
70 // small hack: return hash of the string to initiate valuesChanged signal
71 return qHash(theText);
74 QString ModuleBase_ParamSpinBox::textFromValue (double theValue) const
76 if ((!myTextValue.isEmpty()) && hasVariable(myTextValue)){
79 return ModuleBase_DoubleSpinBox::textFromValue(theValue);
83 \brief This function is used to determine whether input is valid.
84 \param str currently entered value
85 \param pos cursor position in the string
86 \return validating operation result
88 QValidator::State ModuleBase_ParamSpinBox::validate(QString& str, int& pos) const
90 // Trying to interpret the current input text as a numeric value
91 if (!hasVariable(str))
92 return ModuleBase_DoubleSpinBox::validate(str, pos);
94 QValidator::State res = QValidator::Invalid;
95 if (isAcceptVariables()) {
96 res = QValidator::Acceptable;
102 \brief This function is used to set a current value for this spinbox.
103 \param value current value
105 The new value is ignored if the spinbox has a variable.
107 void ModuleBase_ParamSpinBox::setValue(const double value)
112 myTextValue = ModuleBase_DoubleSpinBox::textFromValue(value);
113 ModuleBase_DoubleSpinBox::setValue(value);
117 \brief This function is used to set a text for this spinbox.
118 \param value current value
120 void ModuleBase_ParamSpinBox::setText(const QString& value)
123 lineEdit()->setText(value);
127 \brief Enables or disables variable names in the spin box.
128 By default, variable names are enabled.
129 \param flag If true, variable names are enabled.
131 void ModuleBase_ParamSpinBox::setAcceptVariables(const bool flag)
133 myAcceptVariables = flag;
137 \brief Returns true if the spin box accepts variable names.
139 bool ModuleBase_ParamSpinBox::isAcceptVariables() const
141 return myAcceptVariables;
144 bool ModuleBase_ParamSpinBox::hasVariable() const
146 if (myTextValue.isEmpty())
148 return hasVariable(myTextValue);
151 bool ModuleBase_ParamSpinBox::hasVariable(const QString& theText) const
153 //const QString aDigitPattern = QString("[-+]?[0-9]*[%1]?[0-9]*([eE][-+]?[0-9]+)?");
155 //bool aHasDigit = false;
157 // QRegExp varNameMask(aDigitPattern.arg("."));
158 // aHasDigit = varNameMask.exactMatch(theText);
162 // QRegExp varNameMask(aDigitPattern.arg(","));
163 // aHasDigit = varNameMask.exactMatch(theText);
165 bool isDouble = false;
166 QLocale::c().toDouble(theText, &isDouble);
168 // theText.toDouble(&isDouble);
170 // QLocale aLoc; // create default locale
171 // QChar aDecPnt = aLoc.decimalPoint();
172 // if (aDecPnt == '.')
173 // isDouble = theText.contains(aDecPnt) || (!theText.contains(','));
174 // else if (aDecPnt == ',')
175 // isDouble = theText.contains(aDecPnt) || (!theText.contains('.'));
181 \brief This function is used to determine whether input is valid.
182 \return validating operation result
184 ModuleBase_ParamSpinBox::State ModuleBase_ParamSpinBox::isValid(const QString& theText,
185 double& theValue) const
187 if (hasVariable() && !findVariable(theText, theValue)) {
189 theValue = locale().toDouble(theText, &ok);
194 if (!checkRange(theValue)) {
202 \brief This function is used to check that string value lies within predefined range.
205 bool ModuleBase_ParamSpinBox::checkRange(const double theValue) const
207 return theValue >= minimum() && theValue <= maximum();
211 \brief This function is used to determine whether input is a variable name and to get its value.
212 \return status of search operation
214 bool ModuleBase_ParamSpinBox::findVariable(const QString& theName,
215 double& outValue) const
217 ResultParameterPtr aParam;
218 return ModelAPI_Tools::findVariable(theName.toStdString(), outValue, aParam);
222 \brief This function is called when the spinbox receives key press event.
224 //void ModuleBase_ParamSpinBox::keyPressEvent(QKeyEvent* e)
226 // if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
227 // QWidget::keyPressEvent(e);
229 // ModuleBase_DoubleSpinBox::keyPressEvent(e);
234 \brief This function is called when the spinbox receives show event.
236 void ModuleBase_ParamSpinBox::showEvent(QShowEvent* theEvent)
238 ModuleBase_DoubleSpinBox::showEvent(theEvent);
239 if ((!myTextValue.isEmpty()) && hasVariable(myTextValue)) {
240 setText(myTextValue);