1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "ModuleBase_ParamIntSpinBox.h"
22 #include <ModelAPI_Session.h>
23 #include <ModelAPI_Document.h>
24 #include <ModelAPI_Feature.h>
25 #include <ModelAPI_ResultParameter.h>
26 #include <ModelAPI_AttributeDouble.h>
27 #include <ModelAPI_Tools.h>
34 #include <QApplication>
40 ModuleBase_ParamIntSpinBox::ModuleBase_ParamIntSpinBox(QWidget* theParent)
41 : ModuleBase_IntSpinBox(theParent),
42 myAcceptVariables(true)
44 connectSignalsAndSlots();
50 ModuleBase_ParamIntSpinBox::~ModuleBase_ParamIntSpinBox()
55 \brief Perform \a steps increment/decrement steps.
57 Re-implemented to handle cases when Notebook variable
58 name is specified by the user as the widget text.
59 Otherwise, simply calls the base implementation.
61 \param steps number of increment/decrement steps
63 void ModuleBase_ParamIntSpinBox::stepBy(int steps)
65 if ((!myTextValue.isEmpty()) && hasVariable())
68 ModuleBase_IntSpinBox::stepBy(steps);
72 \brief Connect signals and slots.
74 void ModuleBase_ParamIntSpinBox::connectSignalsAndSlots()
76 connect(this, SIGNAL(valueChanged(const QString&)),
77 this, SLOT(onTextChanged(const QString&)));
80 void ModuleBase_ParamIntSpinBox::onTextChanged(const QString& text)
85 int ModuleBase_ParamIntSpinBox::valueFromText(const QString& theText) const
87 if (!hasVariable(theText))
88 return ModuleBase_IntSpinBox::valueFromText(theText);
90 // small hack: return hash of the string to initiate valuesChanged signal
91 return qHash(theText);
94 QString ModuleBase_ParamIntSpinBox::textFromValue(int theValue) const
96 if ((!myTextValue.isEmpty()) && hasVariable(myTextValue)){
99 return ModuleBase_IntSpinBox::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_ParamIntSpinBox::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_IntSpinBox::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 The new value is ignored if the spinbox has a variable.
127 void ModuleBase_ParamIntSpinBox::setValue(int value)
132 myTextValue = ModuleBase_IntSpinBox::textFromValue(value);
133 ModuleBase_IntSpinBox::setValue(value);
137 \brief This function is used to set a text for this spinbox.
138 \param value current value
140 void ModuleBase_ParamIntSpinBox::setText(const QString& value)
143 lineEdit()->setText(value);
147 \brief Enables or disables variable names in the spin box.
148 By default, variable names are enabled.
149 \param flag If true, variable names are enabled.
151 void ModuleBase_ParamIntSpinBox::setAcceptVariables(const bool flag)
153 myAcceptVariables = flag;
157 \brief Returns true if the spin box accepts variable names.
159 bool ModuleBase_ParamIntSpinBox::isAcceptVariables() const
161 return myAcceptVariables;
164 bool ModuleBase_ParamIntSpinBox::hasVariable() const
166 if (myTextValue.isEmpty())
168 return hasVariable(myTextValue);
171 bool ModuleBase_ParamIntSpinBox::hasVariable(const QString& theText) const
174 QLocale::c().toInt(theText, &ok);
179 \brief This function is used to determine whether input is valid.
180 \return validating operation result
182 ModuleBase_ParamIntSpinBox::State ModuleBase_ParamIntSpinBox::isValid(
183 const QString& theText, double& theValue) const
185 if (hasVariable() && !findVariable(theText, theValue)) {
187 theValue = locale().toInt(theText, &ok);
192 if (!checkRange(theValue)) {
200 \brief This function is used to check that string value lies within predefined range.
203 bool ModuleBase_ParamIntSpinBox::checkRange(const double theValue) const
205 return theValue >= minimum() && theValue <= maximum();
209 \brief This function is used to determine whether input is a variable name and to get its value.
210 \return status of search operation
212 bool ModuleBase_ParamIntSpinBox::findVariable(const QString& theName,
213 double& outValue) const
215 ResultParameterPtr aParam;
216 return ModelAPI_Tools::findVariable(FeaturePtr(), theName.toStdString(), outValue, aParam);
220 \brief This function is called when the spinbox receives show event.
222 void ModuleBase_ParamIntSpinBox::showEvent(QShowEvent* theEvent)
224 ModuleBase_IntSpinBox::showEvent(theEvent);
225 if ((!myTextValue.isEmpty()) && hasVariable(myTextValue)) {
226 setText(myTextValue);