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