Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom.git into Dev_1.1.0
[modules/shaper.git] / src / ModuleBase / ModuleBase_ParamSpinBox.cpp
1 #include "ModuleBase_ParamSpinBox.h"
2
3 #include <ModelAPI_Session.h>
4 #include <ModelAPI_Document.h>
5 #include <ModelAPI_ResultParameter.h>
6 #include <ModelAPI_AttributeDouble.h>
7
8 #include <QKeyEvent>
9 #include <QLineEdit>
10 #include <QToolTip>
11 #include <QRegExp>
12
13 #include <string>
14
15 /*!
16  \class ModuleBase_ParamSpinBox
17  */
18
19 /*!
20  \brief Constructor.
21
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.
25
26  \param parent parent object
27  */
28 ModuleBase_ParamSpinBox::ModuleBase_ParamSpinBox(QWidget* theParent, int thePrecision)
29     : ModuleBase_DoubleSpinBox(theParent, thePrecision),
30       myAcceptVariables(true),
31       myHasVariables(false),
32       myDefaultValue(0.)
33 {
34   connectSignalsAndSlots();
35 }
36
37 /*!
38  \brief Destructor.
39  */
40 ModuleBase_ParamSpinBox::~ModuleBase_ParamSpinBox()
41 {
42 }
43
44 /*!
45  \brief Perform \a steps increment/decrement steps.
46
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.
50
51  \param steps number of increment/decrement steps
52  */
53 void ModuleBase_ParamSpinBox::stepBy(int steps)
54 {
55   QString str = text();
56   QString pref = prefix();
57   QString suff = suffix();
58
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());
63
64   QRegExp varNameMask("([a-z]|[A-Z]|_).*");
65   if (varNameMask.exactMatch(str))
66     return;
67
68   ModuleBase_DoubleSpinBox::stepBy(steps);
69 }
70
71 /*!
72  \brief Connect signals and slots.
73  */
74 void ModuleBase_ParamSpinBox::connectSignalsAndSlots()
75 {
76   connect(this, SIGNAL(editingFinished()),
77           this, SLOT(onEditingFinished()));
78
79   connect(this, SIGNAL(valueChanged(const QString&)),
80           this, SLOT(onTextChanged(const QString&)));
81
82   //connect(lineEdit(), SIGNAL(textChanged(const QString&)),
83   //        this,       SLOT(onTextChanged(const QString&)));
84
85   //connect(lineEdit(), SIGNAL(textChanged(const QString&)),
86   //        this,       SIGNAL(textChanged(const QString&)));
87 }
88
89 /*!
90  \brief This function is called when editing is finished.
91  */
92 void ModuleBase_ParamSpinBox::onEditingFinished()
93 {
94   if (myTextValue.isNull())
95     myTextValue = text();
96
97   setText(myTextValue);
98 }
99
100 /*!
101  \brief This function is called when value is changed.
102  */
103 void ModuleBase_ParamSpinBox::onTextChanged(const QString& text)
104 {
105   myTextValue = text;
106
107   double value = 0;
108   if (isValid(text, value) == Acceptable) {
109     myCorrectValue = text;
110     myHasVariables = true;
111   } else {
112     myHasVariables = false;
113   }
114 }
115
116 /*!
117  \brief Interpret text entered by the user as a value.
118  \param text text entered by the user
119  \return mapped value
120  \sa textFromValue()
121  */
122 double ModuleBase_ParamSpinBox::valueFromText(const QString& theText) const
123 {
124   double aValue = 0;
125   if (isValid(theText, aValue) == Acceptable)
126     return aValue;
127
128   return defaultValue();
129 }
130
131 /*!
132  \brief This function is used by the spin box whenever it needs to display
133  the given value.
134
135  \param val spin box value
136  \return text representation of the value
137  \sa valueFromText()
138  */
139 QString ModuleBase_ParamSpinBox::textFromValue(double val) const
140 {
141   return ModuleBase_DoubleSpinBox::textFromValue(val);
142 }
143
144 /*!
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
149  */
150 QValidator::State ModuleBase_ParamSpinBox::validate(QString& str, int& pos) const
151 {
152   QValidator::State res = QValidator::Invalid;
153
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;
162
163     if (res == QValidator::Invalid) {
164       varNameMask.setPattern("_");
165       if (varNameMask.exactMatch(str))
166         res = QValidator::Intermediate;
167     }
168   }
169
170   // Trying to interpret the current input text as a numeric value
171   if (res == QValidator::Invalid)
172     res = ModuleBase_DoubleSpinBox::validate(str, pos);
173
174   return res;
175 }
176
177 /*!
178  \brief This function is used to set a default value for this spinbox.
179  \param value default value
180  */
181 void ModuleBase_ParamSpinBox::setDefaultValue(const double value)
182 {
183   myDefaultValue = value;
184 }
185
186 /*!
187  \brief This function is used to set a current value for this spinbox.
188  \param value current value
189  */
190 void ModuleBase_ParamSpinBox::setValue(const double value)
191 {
192   ModuleBase_DoubleSpinBox::setValue(value);
193
194   myCorrectValue = ModuleBase_DoubleSpinBox::textFromValue(value);
195   myTextValue = myCorrectValue;
196 }
197
198 /*!
199  \brief This function is used to set a text for this spinbox.
200  \param value current value
201  */
202 void ModuleBase_ParamSpinBox::setText(const QString& value)
203 {
204   lineEdit()->setText(value);
205 }
206
207 /*!
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.
211  */
212 void ModuleBase_ParamSpinBox::setAcceptVariables(const bool flag)
213 {
214   myAcceptVariables = flag;
215 }
216
217 /*!
218  \brief Returns true if the spin box accepts variable names.
219  */
220 bool ModuleBase_ParamSpinBox::isAcceptVariables() const
221 {
222   return myAcceptVariables;
223 }
224
225 bool ModuleBase_ParamSpinBox::hasVariables() const
226 {
227   return myHasVariables;
228 }
229
230 /*!
231  \brief This function is used to determine whether input is valid.
232  \return validating operation result
233  */
234 ModuleBase_ParamSpinBox::State ModuleBase_ParamSpinBox::isValid(const QString& theText,
235                                                                 double& theValue) const
236 {
237   if (!findVariable(theText, theValue)) {
238     bool ok = false;
239     theValue = locale().toDouble(theText, &ok);
240     if (!ok) {
241       return NoVariable;
242     }
243   }
244   if (!checkRange(theValue)) {
245     return Invalid;
246   }
247
248   return Acceptable;
249 }
250
251 /*!
252  \brief This function return a default acceptable value (commonly, 0.0).
253  \return default acceptable value
254  */
255 double ModuleBase_ParamSpinBox::defaultValue() const
256 {
257   if (minimum() > myDefaultValue || maximum() < myDefaultValue)
258     return minimum();
259
260   return myDefaultValue;
261 }
262
263 /*!
264  \brief This function is used to check that string value lies within predefined range.
265  \return check status
266  */
267 bool ModuleBase_ParamSpinBox::checkRange(const double theValue) const
268 {
269   return theValue >= minimum() && theValue <= maximum();
270 }
271
272 /*!
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
275  */
276 bool ModuleBase_ParamSpinBox::findVariable(const QString& theName,
277                                            double& outValue) const
278 {
279
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);
285   if(!aParam.get())
286     return false;
287   AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
288   outValue = aValueAttribute->value();
289   return true;
290 }
291
292 /*!
293  \brief This function is called when the spinbox recieves key press event.
294  */
295 void ModuleBase_ParamSpinBox::keyPressEvent(QKeyEvent* e)
296 {
297   if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
298     QWidget::keyPressEvent(e);
299   } else {
300     ModuleBase_DoubleSpinBox::keyPressEvent(e);
301   }
302 }
303
304 /*!
305  \brief This function is called when the spinbox recieves show event.
306  */
307 void ModuleBase_ParamSpinBox::showEvent(QShowEvent*)
308 {
309   setText(myTextValue);
310 }