Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/gui.git] / src / QDS / QDS_Validator.cxx
1 //  Copyright (C) 2007-2008  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.
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 #include "QDS_Validator.h"
23
24 /*!
25   \class QDS_IntegerValidator
26   \brief A validator for integer values.
27
28   This class provides validation of the strings entered by the user for datum with 'Integer'
29   value type according to datum properties: minimum value, maximum value, filter.
30
31   Inherits QIntValidator.
32 */
33
34 /*!
35   \brief Constructor.
36
37   Create empty validator object.
38
39   \param p parent object
40 */
41 QDS_IntegerValidator::QDS_IntegerValidator( QObject* p )
42 : QIntValidator( p )
43 {
44 }
45
46 /*!
47   \brief Constructor.
48   
49   Create validator object with filter string \a f.
50
51   \param f filter string
52   \param p parent object
53 */
54 QDS_IntegerValidator::QDS_IntegerValidator( const QString& f, QObject* p )
55 : QIntValidator( p ),
56   myFilter( f )
57 {
58 }
59
60 /*!
61   \brief Destructor.
62 */
63 QDS_IntegerValidator::~QDS_IntegerValidator()
64 {
65 }
66
67 /*!
68   \brief Validate the entered string \a input. 
69
70   Reimplemented from QIntValidator. 
71   
72   Perform standard check by QIntValidator::validate(). If returned state 
73   is not \c QValidator::Invalid and filter is specified then perform validation
74   with filter by QRegExpValidator.
75
76   Returns \c QValidator::Invalid if input is invalid according to this validator's rules,
77   \c QValidator::Intermediate if it is likely that a little more editing will make the
78   input acceptable and \c QValidator::Acceptable if the input is valid.
79
80   \param input string being validated
81   \param pos cursor position
82   \return validation state (\c QValidator::State)
83 */
84 QValidator::State QDS_IntegerValidator::validate( QString& input, int& pos ) const
85 {
86   State rgState = Acceptable;
87   State ivState = QIntValidator::validate( input, pos );
88   if ( ivState != Invalid && !myFilter.isEmpty() )
89     rgState = QRegExpValidator( QRegExp( myFilter ), 0 ).validate( input, pos );
90
91   ivState = qMin( ivState, rgState );
92
93   return ivState;
94 }
95
96 /*!
97   \class QDS_DoubleValidator
98   \brief A validator for floating point values.
99
100   This class provides validation of the strings entered by the user for datum with 'Float'
101   value type according to datum properties: minimum value, maximum value, precision, filter.
102
103   Inherits QDoubleValidator.
104 */
105
106 /*!
107   \brief Constructor.
108   
109   Create empty validator object.
110   
111   \param p parent object
112 */
113 QDS_DoubleValidator::QDS_DoubleValidator( QObject* p )
114 : QDoubleValidator( p )
115 {
116 }
117
118 /*!
119   \brief Constructor.
120   
121   Create validator object with filter string \a f.
122   
123   \param f filter string
124   \param p parent object
125 */
126 QDS_DoubleValidator::QDS_DoubleValidator( const QString& f, QObject* p )
127 : QDoubleValidator( p ),
128   myFilter( f )
129 {
130 }
131
132 /*!
133   \brief Destructor.
134 */
135 QDS_DoubleValidator::~QDS_DoubleValidator()
136 {
137 }
138
139 /*!
140   \brief Validate the entered string \a input. 
141
142   Reimplemented from QDoubleValidator. 
143   
144   Perform standard check by QDoubleValidator::validate(). If returned state 
145   is not \c QValidator::Invalid and filter is specified then perform validation
146   with filter by QRegExpValidator.
147
148   Returns \c QValidator::Invalid if input is invalid according to this validator's rules,
149   \c QValidator::Intermediate if it is likely that a little more editing will make the
150   input acceptable and \c QValidator::Acceptable if the input is valid.
151
152   \param input string being validated
153   \param pos cursor position
154   \return validation state (\c QValidator::State)
155 */
156 QValidator::State QDS_DoubleValidator::validate( QString& input, int& pos ) const
157 {
158   State rgState = Acceptable;
159   State dvState = QDoubleValidator::validate( input, pos );
160   if ( dvState != Invalid && !myFilter.isEmpty() )
161     rgState = QRegExpValidator( QRegExp( myFilter ), 0 ).validate( input, pos );
162
163   dvState = qMin( dvState, rgState );
164
165   return dvState;
166 }
167
168 /*!
169   \class QDS_StringValidator
170   \brief A validator for string values.
171
172   This class provides validation of the strings entered by the user for datum with 'String'
173   value type according to datum properties: format, filter.
174
175   Inherits from QValidator.
176 */
177
178 /*!
179   \brief Constructor.
180
181   Create empty validator object.
182
183   \param p parent object
184 */
185 QDS_StringValidator::QDS_StringValidator( QObject* p ) 
186 : QValidator( p ), 
187   myLen( -1 ) 
188 {
189 }
190
191 /*!
192   \brief Constructor. 
193
194   Create validator object with filter string \a f.
195
196   \param f filter string
197   \param p parent object
198 */
199 QDS_StringValidator::QDS_StringValidator( const QString& f, QObject* p ) 
200 : QValidator( p ), 
201   myFlags( f ), 
202   myLen( -1 ) 
203 {
204 }
205
206 /*!
207   \brief Constructor.
208
209   Create validator object with filter string \a ft and format flags \a fg.
210   \param ft filter string
211   \param fg format string
212   \param p parent object
213 */
214 QDS_StringValidator::QDS_StringValidator( const QString& ft, const QString& fg, QObject* p ) 
215 : QValidator( p ), 
216   myLen( -1 ), 
217   myFilter( ft ), 
218   myFlags( fg ) 
219 {
220 }
221
222 /*!
223   \brief Destructor.
224 */
225 QDS_StringValidator::~QDS_StringValidator() 
226 {
227 }
228
229 /*!
230   \brief Get maximum valid string length.
231
232   If maximum length is not specified, -1 is returned.
233
234   \return maximum string length
235 */
236 int QDS_StringValidator::length() const 
237
238   return myLen; 
239 }
240
241 /*!
242   \brief Set maximum valid string length.
243
244   If l <= 0 is not specified, maximum valid length is reset
245   (any string length is valid).
246
247   \param l maximum strin length
248 */
249 void QDS_StringValidator::setLength( const int l )
250 {
251   myLen = l;
252 }
253
254 /*!
255   \brief Validate the entered string \a input. 
256
257   Reimplemented from QValidator. 
258   
259   Convert specified string to the upper/lower case if the format flags contains
260   specificator 'u'/'l' correspondingly.  If valid string length is specified,
261   then check given string length. If filter is specified, then perform also 
262   validation with filter by QRegExpValidator.
263
264   Returns \c QValidator::Invalid if input is invalid according to this validator's rules,
265   \c QValidator::Intermediate if it is likely that a little more editing will make the
266   input acceptable and \c QValidator::Acceptable if the input is valid.
267
268   \param input string being validated
269   \param pos cursor position
270   \return validation state (\c QValidator::State)
271 */
272 QValidator::State QDS_StringValidator::validate( QString& input, int& pos ) const
273 {
274   if ( input.isEmpty() )
275     return Acceptable;
276
277   QString orig = input;
278   if ( myFlags.contains( 'u', Qt::CaseInsensitive ) )
279     input = input.toUpper();
280   if ( myFlags.contains( 'l', Qt::CaseInsensitive ) )
281     input = input.toLower();
282
283   State rgState = Acceptable;
284   State svState = orig == input ? Acceptable : Intermediate;
285
286   if ( myLen >= 0 && (int)input.length() > myLen )
287       svState = Intermediate;
288
289   if ( !myFilter.isEmpty() )
290     rgState = QRegExpValidator( QRegExp( myFilter ), 0 ).validate( input, pos );
291
292   svState = qMin( svState, rgState );
293
294   return svState;
295 }