Salome HOME
Issue #1330 correction for attribute ref attr list. Crash of rectangle creation
[modules/shaper.git] / src / ModuleBase / ModuleBase_ParamIntSpinBox.cpp
1 #include "ModuleBase_ParamIntSpinBox.h"
2
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>
8
9 #include <QKeyEvent>
10 #include <QLineEdit>
11 #include <QLocale>
12 #include <QRegExp>
13 #include <QToolTip>
14 #include <QApplication>
15
16 #include <string>
17 #include <iostream>
18
19
20 ModuleBase_ParamIntSpinBox::ModuleBase_ParamIntSpinBox(QWidget* theParent)
21     : ModuleBase_IntSpinBox(theParent),
22       myAcceptVariables(true)
23 {
24   connectSignalsAndSlots();
25 }
26
27 /*!
28  \brief Destructor.
29  */
30 ModuleBase_ParamIntSpinBox::~ModuleBase_ParamIntSpinBox()
31 {
32 }
33
34 /*!
35  \brief Perform \a steps increment/decrement steps.
36
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.
40
41  \param steps number of increment/decrement steps
42  */
43 void ModuleBase_ParamIntSpinBox::stepBy(int steps)
44 {
45   if ((!myTextValue.isEmpty()) && hasVariable())
46     return;
47
48   ModuleBase_IntSpinBox::stepBy(steps);
49 }
50
51 /*!
52  \brief Connect signals and slots.
53  */
54 void ModuleBase_ParamIntSpinBox::connectSignalsAndSlots()
55 {
56   connect(this, SIGNAL(valueChanged(const QString&)),
57           this, SLOT(onTextChanged(const QString&)));
58 }
59
60 void ModuleBase_ParamIntSpinBox::onTextChanged(const QString& text)
61 {
62   myTextValue = text;
63 }
64
65 int ModuleBase_ParamIntSpinBox::valueFromText(const QString& theText) const
66 {
67   if (!hasVariable(theText))
68     return ModuleBase_IntSpinBox::valueFromText(theText);
69
70   // small hack: return hash of the string to initiate valuesChanged signal
71   return qHash(theText);
72 }
73
74 QString ModuleBase_ParamIntSpinBox::textFromValue(int theValue) const
75 {
76   if ((!myTextValue.isEmpty()) && hasVariable(myTextValue)){
77     return myTextValue;
78   }
79   return ModuleBase_IntSpinBox::textFromValue(theValue);
80 }
81
82 /*!
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
87  */
88 QValidator::State ModuleBase_ParamIntSpinBox::validate(QString& str, int& pos) const
89 {
90   // Trying to interpret the current input text as a numeric value
91   if (!hasVariable(str))
92     return ModuleBase_IntSpinBox::validate(str, pos);
93
94   QValidator::State res = QValidator::Invalid;
95   if (isAcceptVariables()) {
96     res = QValidator::Acceptable;
97   }
98   return res;
99 }
100
101 /*!
102  \brief This function is used to set a current value for this spinbox.
103  \param value current value
104
105  The new value is ignored if the spinbox has a variable.
106  */
107 void ModuleBase_ParamIntSpinBox::setValue(int value)
108 {
109   if (hasVariable())
110     return;
111
112   myTextValue = ModuleBase_IntSpinBox::textFromValue(value);
113   ModuleBase_IntSpinBox::setValue(value);
114 }
115
116 /*!
117  \brief This function is used to set a text for this spinbox.
118  \param value current value
119  */
120 void ModuleBase_ParamIntSpinBox::setText(const QString& value)
121 {
122   myTextValue = value;
123   lineEdit()->setText(value);
124 }
125
126 /*!
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.
130  */
131 void ModuleBase_ParamIntSpinBox::setAcceptVariables(const bool flag)
132 {
133   myAcceptVariables = flag;
134 }
135
136 /*!
137  \brief Returns true if the spin box accepts variable names.
138  */
139 bool ModuleBase_ParamIntSpinBox::isAcceptVariables() const
140 {
141   return myAcceptVariables;
142 }
143
144 bool ModuleBase_ParamIntSpinBox::hasVariable() const
145 {
146   if (myTextValue.isEmpty())
147     return false;
148   return hasVariable(myTextValue);
149 }
150
151 bool ModuleBase_ParamIntSpinBox::hasVariable(const QString& theText) const
152 {
153   bool ok = false;
154   QLocale::c().toInt(theText, &ok);
155   return !ok;
156 }
157
158 /*!
159  \brief This function is used to determine whether input is valid.
160  \return validating operation result
161  */
162 ModuleBase_ParamIntSpinBox::State ModuleBase_ParamIntSpinBox::isValid(
163     const QString& theText, double& theValue) const
164 {
165   if (hasVariable() && !findVariable(theText, theValue)) {
166     bool ok = false;
167     theValue = locale().toInt(theText, &ok);
168     if (!ok) {
169       return NoVariable;
170     }
171   }
172   if (!checkRange(theValue)) {
173     return Invalid;
174   }
175
176   return Acceptable;
177 }
178
179 /*!
180  \brief This function is used to check that string value lies within predefined range.
181  \return check status
182  */
183 bool ModuleBase_ParamIntSpinBox::checkRange(const double theValue) const
184 {
185   return theValue >= minimum() && theValue <= maximum();
186 }
187
188 /*!
189  \brief This function is used to determine whether input is a variable name and to get its value.
190  \return status of search operation
191  */
192 bool ModuleBase_ParamIntSpinBox::findVariable(const QString& theName,
193                                               double& outValue) const
194 {
195   ResultParameterPtr aParam;
196   return ModelAPI_Tools::findVariable(theName.toStdString(), outValue, aParam);
197 }
198
199 /*!
200  \brief This function is called when the spinbox receives show event.
201  */
202 void ModuleBase_ParamIntSpinBox::showEvent(QShowEvent* theEvent)
203 {
204   ModuleBase_IntSpinBox::showEvent(theEvent);
205   if ((!myTextValue.isEmpty()) && hasVariable(myTextValue)) {
206     setText(myTextValue);
207   }
208 }