Salome HOME
Copyright update 2022
[modules/gui.git] / src / QDS / QDS_Validator.cxx
1 // Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "QDS_Validator.h"
24
25 /*!
26   \class QDS_IntegerValidator
27   \brief A validator for integer values.
28
29   This class provides validation of the strings entered by the user for datum with 'Integer'
30   value type according to datum properties: minimum value, maximum value, filter.
31
32   Inherits QIntValidator.
33 */
34
35 /*!
36   \brief Constructor.
37
38   Create empty validator object.
39
40   \param p parent object
41 */
42 QDS_IntegerValidator::QDS_IntegerValidator( QObject* p )
43 : QIntValidator( p )
44 {
45 }
46
47 /*!
48   \brief Constructor.
49   
50   Create validator object with filter string \a f.
51
52   \param f filter string
53   \param p parent object
54 */
55 QDS_IntegerValidator::QDS_IntegerValidator( const QString& f, QObject* p )
56 : QIntValidator( p ),
57   myFilter( f )
58 {
59 }
60
61 /*!
62   \brief Destructor.
63 */
64 QDS_IntegerValidator::~QDS_IntegerValidator()
65 {
66 }
67
68 /*!
69   \brief Validate the entered string \a input. 
70
71   Reimplemented from QIntValidator. 
72   
73   Perform standard check by QIntValidator::validate(). If returned state 
74   is not \c QValidator::Invalid and filter is specified then perform validation
75   with filter by QRegExpValidator.
76
77   Returns \c QValidator::Invalid if input is invalid according to this validator's rules,
78   \c QValidator::Intermediate if it is likely that a little more editing will make the
79   input acceptable and \c QValidator::Acceptable if the input is valid.
80
81   \param input string being validated
82   \param pos cursor position
83   \return validation state (\c QValidator::State)
84 */
85 QValidator::State QDS_IntegerValidator::validate( QString& input, int& pos ) const
86 {
87   State rgState = Acceptable;
88   State ivState = QIntValidator::validate( input, pos );
89   if ( ivState != Invalid && !myFilter.isEmpty() )
90     rgState = QRegExpValidator( QRegExp( myFilter ), 0 ).validate( input, pos );
91
92   ivState = qMin( ivState, rgState );
93
94   return ivState;
95 }
96
97 /*!
98   \class QDS_DoubleValidator
99   \brief A validator for floating point values.
100
101   This class provides validation of the strings entered by the user for datum with 'Float'
102   value type according to datum properties: minimum value, maximum value, precision, filter.
103
104   Inherits QDoubleValidator.
105 */
106
107 /*!
108   \brief Constructor.
109   
110   Create empty validator object.
111   
112   \param p parent object
113 */
114 QDS_DoubleValidator::QDS_DoubleValidator( QObject* p )
115 : QDoubleValidator( p )
116 {
117 }
118
119 /*!
120   \brief Constructor.
121   
122   Create validator object with filter string \a f.
123   
124   \param f filter string
125   \param p parent object
126 */
127 QDS_DoubleValidator::QDS_DoubleValidator( const QString& f, QObject* p )
128 : QDoubleValidator( p ),
129   myFilter( f )
130 {
131 }
132
133 /*!
134   \brief Destructor.
135 */
136 QDS_DoubleValidator::~QDS_DoubleValidator()
137 {
138 }
139
140 /*!
141   \brief Validate the entered string \a input. 
142
143   Reimplemented from QDoubleValidator. 
144   
145   Perform standard check by QDoubleValidator::validate(). If returned state 
146   is not \c QValidator::Invalid and filter is specified then perform validation
147   with filter by QRegExpValidator.
148
149   Returns \c QValidator::Invalid if input is invalid according to this validator's rules,
150   \c QValidator::Intermediate if it is likely that a little more editing will make the
151   input acceptable and \c QValidator::Acceptable if the input is valid.
152
153   \param input string being validated
154   \param pos cursor position
155   \return validation state (\c QValidator::State)
156 */
157 QValidator::State QDS_DoubleValidator::validate( QString& input, int& pos ) const
158 {
159   State rgState = Acceptable;
160   State dvState = QDoubleValidator::validate( input, pos );
161   if ( dvState != Invalid && !myFilter.isEmpty() )
162     rgState = QRegExpValidator( QRegExp( myFilter ), 0 ).validate( input, pos );
163
164   dvState = qMin( dvState, rgState );
165
166   return dvState;
167 }
168
169 /*!
170   \class QDS_StringValidator
171   \brief A validator for string values.
172
173   This class provides validation of the strings entered by the user for datum with 'String'
174   value type according to datum properties: format, filter.
175
176   Inherits from QValidator.
177 */
178
179 /*!
180   \brief Constructor.
181
182   Create empty validator object.
183
184   \param p parent object
185 */
186 QDS_StringValidator::QDS_StringValidator( QObject* p ) 
187 : QValidator( p ), 
188   myLen( -1 ) 
189 {
190 }
191
192 /*!
193   \brief Constructor. 
194
195   Create validator object with filter string \a f.
196
197   \param f filter string
198   \param p parent object
199 */
200 QDS_StringValidator::QDS_StringValidator( const QString& f, QObject* p ) 
201 : QValidator( p ),
202   myLen( -1 ),
203   myFlags( f )
204 {
205 }
206
207 /*!
208   \brief Constructor.
209
210   Create validator object with filter string \a ft and format flags \a fg.
211   \param ft filter string
212   \param fg format string
213   \param p parent object
214 */
215 QDS_StringValidator::QDS_StringValidator( const QString& ft, const QString& fg, QObject* p ) 
216 : QValidator( p ),
217   myLen( -1 ),
218   myFlags( fg ),
219   myFilter( ft )
220 {
221 }
222
223 /*!
224   \brief Destructor.
225 */
226 QDS_StringValidator::~QDS_StringValidator() 
227 {
228 }
229
230 /*!
231   \brief Get maximum valid string length.
232
233   If maximum length is not specified, -1 is returned.
234
235   \return maximum string length
236 */
237 int QDS_StringValidator::length() const 
238
239   return myLen; 
240 }
241
242 /*!
243   \brief Set maximum valid string length.
244
245   If l <= 0 is not specified, maximum valid length is reset
246   (any string length is valid).
247
248   \param l maximum strin length
249 */
250 void QDS_StringValidator::setLength( const int l )
251 {
252   myLen = l;
253 }
254
255 /*!
256   \brief Validate the entered string \a input. 
257
258   Reimplemented from QValidator. 
259   
260   Convert specified string to the upper/lower case if the format flags contains
261   specificator 'u'/'l' correspondingly.  If valid string length is specified,
262   then check given string length. If filter is specified, then perform also 
263   validation with filter by QRegExpValidator.
264
265   Returns \c QValidator::Invalid if input is invalid according to this validator's rules,
266   \c QValidator::Intermediate if it is likely that a little more editing will make the
267   input acceptable and \c QValidator::Acceptable if the input is valid.
268
269   \param input string being validated
270   \param pos cursor position
271   \return validation state (\c QValidator::State)
272 */
273 QValidator::State QDS_StringValidator::validate( QString& input, int& pos ) const
274 {
275   if ( input.isEmpty() )
276     return Acceptable;
277
278   QString orig = input;
279   if ( myFlags.contains( 'u', Qt::CaseInsensitive ) )
280     input = input.toUpper();
281   if ( myFlags.contains( 'l', Qt::CaseInsensitive ) )
282     input = input.toLower();
283
284   State rgState = Acceptable;
285   State svState = orig == input ? Acceptable : Intermediate;
286
287   if ( myLen >= 0 && (int)input.length() > myLen )
288       svState = Intermediate;
289
290   if ( !myFilter.isEmpty() )
291     rgState = QRegExpValidator( QRegExp( myFilter ), 0 ).validate( input, pos );
292
293   svState = qMin( svState, rgState );
294
295   return svState;
296 }