Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/gui.git] / src / QDS / QDS_Validator.cxx
1 // Copyright (C) 2005  CEA/DEN, EDF R&D, OPEN CASCADE, PRINCIPIA R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #include "QDS_Validator.h"
20
21 /*!
22   \class QDS_IntegerValidator
23
24   This class to provide validation of user inputted string for datum with 'Integer'
25   value type according to datum properties: minimum value, maximum value, filter.
26
27   Inherits from QIntValidator.
28 */
29
30 /*!
31   Constructor. Create empty validator object.
32 */
33 QDS_IntegerValidator::QDS_IntegerValidator( QObject* p )
34 : QIntValidator( p )
35 {
36 }
37
38 /*!
39   Constructor. Create validator object with filter string \af.
40 */
41 QDS_IntegerValidator::QDS_IntegerValidator( const QString& f, QObject* p )
42 : QIntValidator( p ),
43 myFilter( f )
44 {
45 }
46
47 /*!
48   Destructor.
49 */
50 QDS_IntegerValidator::~QDS_IntegerValidator()
51 {
52 }
53
54 /*!
55   Validate the inputed string \ainput. Reimplemented from QIntValidator. Perform
56   standard check by QIntValidator::validate(). If returned state is not Invalid and
57   filter specified then perform validation with filter by QRegExpValidator.
58
59   Returns Invalid if input is invalid according to this validator's rules,
60   Intermediate if it is likely that a little more editing will make the input acceptable
61   and Acceptable if the input is valid.
62 */
63 QValidator::State QDS_IntegerValidator::validate( QString& input, int& pos ) const
64 {
65   State rgState = Acceptable;
66   State ivState = QIntValidator::validate( input, pos );
67   if ( ivState != Invalid && !myFilter.isEmpty() )
68     rgState = QRegExpValidator( QRegExp( myFilter ), 0 ).validate( input, pos );
69
70   ivState = QMIN( ivState, rgState );
71
72   return ivState;
73 }
74
75 /*!
76   \class QDS_DoubleValidator
77
78   This class to provide validation of user inputted string for datum with 'Float'
79   value type according to datum properties: minimum value, maximum value, precision, filter.
80
81   Inherits from QDoubleValidator.
82 */
83
84 /*!
85   Constructor. Create empty validator object.
86 */
87 QDS_DoubleValidator::QDS_DoubleValidator( QObject* p )
88 : QDoubleValidator( p )
89 {
90 }
91
92 /*!
93   Constructor. Create validator object with filter string \af.
94 */
95 QDS_DoubleValidator::QDS_DoubleValidator( const QString& f, QObject* p )
96 : QDoubleValidator( p ),
97 myFilter( f )
98 {
99 }
100
101 /*!
102   Destructor.
103 */
104 QDS_DoubleValidator::~QDS_DoubleValidator()
105 {
106 }
107
108 /*!
109   Validate the inputed string \ainput. Reimplemented from QDoubleValidator. Perform
110   standard check by QDoubleValidator::validate(). If returned state is not Invalid and
111   filter specified then perform validation with filter by QRegExpValidator.
112
113   Returns Invalid if input is invalid according to this validator's rules,
114   Intermediate if it is likely that a little more editing will make the input acceptable
115   and Acceptable if the input is valid.
116 */
117 QValidator::State QDS_DoubleValidator::validate( QString& input, int& pos ) const
118 {
119   State rgState = Acceptable;
120   State dvState = QDoubleValidator::validate( input, pos );
121   if ( dvState != Invalid && !myFilter.isEmpty() )
122     rgState = QRegExpValidator( QRegExp( myFilter ), 0 ).validate( input, pos );
123
124   dvState = QMIN( dvState, rgState );
125
126   return dvState;
127 }
128
129 /*!
130   \class QDS_StringValidator
131
132   This class to provide validation of user inputted string for datum with 'String'
133   value type according to datum properties: format, filter.
134
135   Inherits from QDoubleValidator.
136 */
137
138 /*!
139   Constructor. Create empty validator object.
140 */
141 QDS_StringValidator::QDS_StringValidator( QObject* p ) 
142 : QValidator( p ), 
143 myLen( -1 ) 
144 {
145 }
146
147 /*!
148   Constructor. Create validator object with filter string \af.
149 */
150 QDS_StringValidator::QDS_StringValidator( const QString& f, QObject* p ) 
151 : QValidator( p ), 
152 myFlags( f ), 
153 myLen( -1 ) 
154 {
155 }
156
157 /*!
158   Constructor. Create validator object with filter string \aft and format flags \afg.
159 */
160 QDS_StringValidator::QDS_StringValidator( const QString& ft, const QString& fg, QObject* p ) 
161 : QValidator( p ), 
162 myLen( -1 ), 
163 myFilter( ft ), 
164 myFlags( fg ) 
165 {
166 }
167
168 /*!
169   Destructor.
170 */
171 QDS_StringValidator::~QDS_StringValidator() 
172 {
173 }
174
175 /*!
176   Returns valid string length. If length not specified -1 returned.
177 */
178 int QDS_StringValidator::length() const 
179
180   return myLen; 
181 }
182
183 /*!
184   Sets the valid string length \al. If value less than zero valid string
185   length not specified.
186 */
187 void QDS_StringValidator::setLength( const int l )
188 {
189   myLen = l;
190 }
191
192 /*!
193   Validate the inputed string \ainput. Reimplemented from QValidator. Convert specified
194   string to upper/lower case if the format flags contains specificator 'u'/'l'.
195   If valid string lenght is specified then check given string length. If
196   filter specified then perform validation with filter by QRegExpValidator.
197
198   Returns Invalid if input is invalid according to this validator's rules,
199   Intermediate if it is likely that a little more editing will make the input acceptable
200   and Acceptable if the input is valid.
201 */
202 QValidator::State QDS_StringValidator::validate( QString& input, int& pos ) const
203 {
204   if ( input.isEmpty() )
205     return Acceptable;
206
207   QString orig = input;
208   if ( myFlags.contains( 'u', false ) )
209     input = input.upper();
210   if ( myFlags.contains( 'l', false ) )
211     input = input.lower();
212
213   State rgState = Acceptable;
214   State svState = orig == input ? Acceptable : Intermediate;
215
216   if ( myLen >= 0 && (int)input.length() > myLen )
217       svState = Intermediate;
218
219   if ( !myFilter.isEmpty() )
220     rgState = QRegExpValidator( QRegExp( myFilter ), 0 ).validate( input, pos );
221
222   svState = QMIN( svState, rgState );
223
224   return svState;
225 }