1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include <qdebug.h>
|
---|
43 |
|
---|
44 | #include "qvalidator.h"
|
---|
45 | #ifndef QT_NO_VALIDATOR
|
---|
46 | #include "private/qobject_p.h"
|
---|
47 | #include "private/qlocale_p.h"
|
---|
48 |
|
---|
49 | #include <limits.h>
|
---|
50 | #include <math.h>
|
---|
51 |
|
---|
52 | QT_BEGIN_NAMESPACE
|
---|
53 |
|
---|
54 | /*!
|
---|
55 | \class QValidator
|
---|
56 | \brief The QValidator class provides validation of input text.
|
---|
57 |
|
---|
58 | The class itself is abstract. Two subclasses, \l QIntValidator and
|
---|
59 | \l QDoubleValidator, provide basic numeric-range checking, and \l
|
---|
60 | QRegExpValidator provides general checking using a custom regular
|
---|
61 | expression.
|
---|
62 |
|
---|
63 | If the built-in validators aren't sufficient, you can subclass
|
---|
64 | QValidator. The class has two virtual functions: validate() and
|
---|
65 | fixup().
|
---|
66 |
|
---|
67 | \l validate() must be implemented by every subclass. It returns
|
---|
68 | \l Invalid, \l Intermediate or \l Acceptable depending on whether
|
---|
69 | its argument is valid (for the subclass's definition of valid).
|
---|
70 |
|
---|
71 | These three states require some explanation. An \l Invalid string
|
---|
72 | is \e clearly invalid. \l Intermediate is less obvious: the
|
---|
73 | concept of validity is difficult to apply when the string is
|
---|
74 | incomplete (still being edited). QValidator defines \l Intermediate
|
---|
75 | as the property of a string that is neither clearly invalid nor
|
---|
76 | acceptable as a final result. \l Acceptable means that the string
|
---|
77 | is acceptable as a final result. One might say that any string
|
---|
78 | that is a plausible intermediate state during entry of an \l
|
---|
79 | Acceptable string is \l Intermediate.
|
---|
80 |
|
---|
81 | Here are some examples:
|
---|
82 |
|
---|
83 | \list
|
---|
84 |
|
---|
85 | \i For a line edit that accepts integers from 10 to 1000 inclusive,
|
---|
86 | 42 and 123 are \l Acceptable, the empty string and 5 are \l
|
---|
87 | Intermediate, and "asdf" and 1114 is \l Invalid.
|
---|
88 |
|
---|
89 | \i For an editable combobox that accepts URLs, any well-formed URL
|
---|
90 | is \l Acceptable, "http://example.com/," is \l Intermediate
|
---|
91 | (it might be a cut and paste action that accidentally took in a
|
---|
92 | comma at the end), the empty string is \l Intermediate (the user
|
---|
93 | might select and delete all of the text in preparation for entering
|
---|
94 | a new URL) and "http:///./" is \l Invalid.
|
---|
95 |
|
---|
96 | \i For a spin box that accepts lengths, "11cm" and "1in" are \l
|
---|
97 | Acceptable, "11" and the empty string are \l Intermediate, and
|
---|
98 | "http://example.com" and "hour" are \l Invalid.
|
---|
99 |
|
---|
100 | \endlist
|
---|
101 |
|
---|
102 | \l fixup() is provided for validators that can repair some user
|
---|
103 | errors. The default implementation does nothing. QLineEdit, for
|
---|
104 | example, will call fixup() if the user presses Enter (or Return)
|
---|
105 | and the content is not currently valid. This allows the fixup()
|
---|
106 | function the opportunity of performing some magic to make an \l
|
---|
107 | Invalid string \l Acceptable.
|
---|
108 |
|
---|
109 | A validator has a locale, set with setLocale(). It is typically used
|
---|
110 | to parse localized data. For example, QIntValidator and QDoubleValidator
|
---|
111 | use it to parse localized representations of integers and doubles.
|
---|
112 |
|
---|
113 | QValidator is typically used with QLineEdit, QSpinBox and
|
---|
114 | QComboBox.
|
---|
115 |
|
---|
116 | \sa QIntValidator, QDoubleValidator, QRegExpValidator, {Line Edits Example}
|
---|
117 | */
|
---|
118 |
|
---|
119 |
|
---|
120 | /*!
|
---|
121 | \enum QValidator::State
|
---|
122 |
|
---|
123 | This enum type defines the states in which a validated string can
|
---|
124 | exist.
|
---|
125 |
|
---|
126 | \value Invalid The string is \e clearly invalid.
|
---|
127 | \value Intermediate The string is a plausible intermediate value.
|
---|
128 | \value Acceptable The string is acceptable as a final result;
|
---|
129 | i.e. it is valid.
|
---|
130 |
|
---|
131 | \omitvalue Valid
|
---|
132 | */
|
---|
133 |
|
---|
134 | class QValidatorPrivate : public QObjectPrivate{
|
---|
135 | Q_DECLARE_PUBLIC(QValidator)
|
---|
136 | public:
|
---|
137 | QValidatorPrivate() : QObjectPrivate()
|
---|
138 | {
|
---|
139 | }
|
---|
140 |
|
---|
141 | QLocale locale;
|
---|
142 | };
|
---|
143 |
|
---|
144 |
|
---|
145 | /*!
|
---|
146 | Sets up the validator. The \a parent parameter is
|
---|
147 | passed on to the QObject constructor.
|
---|
148 | */
|
---|
149 |
|
---|
150 | QValidator::QValidator(QObject * parent)
|
---|
151 | : QObject(*new QValidatorPrivate, parent)
|
---|
152 | {
|
---|
153 | }
|
---|
154 |
|
---|
155 | #ifdef QT3_SUPPORT
|
---|
156 | /*!
|
---|
157 | \obsolete
|
---|
158 | Sets up the validator. The \a parent and \a name parameters are
|
---|
159 | passed on to the QObject constructor.
|
---|
160 | */
|
---|
161 |
|
---|
162 | QValidator::QValidator(QObject * parent, const char *name)
|
---|
163 | : QObject(*new QValidatorPrivate, parent)
|
---|
164 | {
|
---|
165 | setObjectName(QString::fromAscii(name));
|
---|
166 | }
|
---|
167 | #endif
|
---|
168 |
|
---|
169 | /*!
|
---|
170 | Destroys the validator, freeing any storage and other resources
|
---|
171 | used.
|
---|
172 | */
|
---|
173 |
|
---|
174 | QValidator::~QValidator()
|
---|
175 | {
|
---|
176 | }
|
---|
177 |
|
---|
178 | /*!
|
---|
179 | Returns the locale for the validator. The locale is by default initialized to the same as QLocale().
|
---|
180 |
|
---|
181 | \sa setLocale()
|
---|
182 | \sa QLocale::QLocale()
|
---|
183 | */
|
---|
184 | QLocale QValidator::locale() const
|
---|
185 | {
|
---|
186 | Q_D(const QValidator);
|
---|
187 | return d->locale;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /*!
|
---|
191 | Sets the \a locale that will be used for the validator. Unless
|
---|
192 | setLocale has been called, the validator will use the default
|
---|
193 | locale set with QLocale::setDefault(). If a default locale has not
|
---|
194 | been set, it is the operating system's locale.
|
---|
195 |
|
---|
196 | \sa locale() QLocale::setDefault()
|
---|
197 | */
|
---|
198 | void QValidator::setLocale(const QLocale &locale)
|
---|
199 | {
|
---|
200 | Q_D(QValidator);
|
---|
201 | d->locale = locale;
|
---|
202 | }
|
---|
203 |
|
---|
204 | /*!
|
---|
205 | \fn QValidator::State QValidator::validate(QString &input, int &pos) const
|
---|
206 |
|
---|
207 | This virtual function returns \l Invalid if \a input is invalid
|
---|
208 | according to this validator's rules, \l Intermediate if it
|
---|
209 | is likely that a little more editing will make the input
|
---|
210 | acceptable (e.g. the user types "4" into a widget which accepts
|
---|
211 | integers between 10 and 99), and \l Acceptable if the input is
|
---|
212 | valid.
|
---|
213 |
|
---|
214 | The function can change both \a input and \a pos (the cursor position)
|
---|
215 | if required.
|
---|
216 | */
|
---|
217 |
|
---|
218 |
|
---|
219 | /*!
|
---|
220 | \fn void QValidator::fixup(QString & input) const
|
---|
221 |
|
---|
222 | This function attempts to change \a input to be valid according to
|
---|
223 | this validator's rules. It need not result in a valid string:
|
---|
224 | callers of this function must re-test afterwards; the default does
|
---|
225 | nothing.
|
---|
226 |
|
---|
227 | Reimplementations of this function can change \a input even if
|
---|
228 | they do not produce a valid string. For example, an ISBN validator
|
---|
229 | might want to delete every character except digits and "-", even
|
---|
230 | if the result is still not a valid ISBN; a surname validator might
|
---|
231 | want to remove whitespace from the start and end of the string,
|
---|
232 | even if the resulting string is not in the list of accepted
|
---|
233 | surnames.
|
---|
234 | */
|
---|
235 |
|
---|
236 | void QValidator::fixup(QString &) const
|
---|
237 | {
|
---|
238 | }
|
---|
239 |
|
---|
240 |
|
---|
241 | /*!
|
---|
242 | \class QIntValidator
|
---|
243 | \brief The QIntValidator class provides a validator that ensures
|
---|
244 | a string contains a valid integer within a specified range.
|
---|
245 |
|
---|
246 | Example of use:
|
---|
247 |
|
---|
248 | \snippet doc/src/snippets/code/src_gui_widgets_qvalidator.cpp 0
|
---|
249 |
|
---|
250 | Below we present some examples of validators. In practice they would
|
---|
251 | normally be associated with a widget as in the example above.
|
---|
252 |
|
---|
253 | \snippet doc/src/snippets/code/src_gui_widgets_qvalidator.cpp 1
|
---|
254 |
|
---|
255 | Notice that the value \c 999 returns Intermediate. Values
|
---|
256 | consisting of a number of digits equal to or less than the max
|
---|
257 | value are considered intermediate. This is intended because the
|
---|
258 | digit that prevents a number to be in range is not necessarily the
|
---|
259 | last digit typed. This also means that an intermediate number can
|
---|
260 | have leading zeros.
|
---|
261 |
|
---|
262 | The minimum and maximum values are set in one call with setRange(),
|
---|
263 | or individually with setBottom() and setTop().
|
---|
264 |
|
---|
265 | QIntValidator uses its locale() to interpret the number. For example,
|
---|
266 | in Arabic locales, QIntValidator will accept Arabic digits. In addition,
|
---|
267 | QIntValidator is always guaranteed to accept a number formatted according
|
---|
268 | to the "C" locale.
|
---|
269 |
|
---|
270 | \sa QDoubleValidator, QRegExpValidator, {Line Edits Example}
|
---|
271 | */
|
---|
272 |
|
---|
273 | /*!
|
---|
274 | Constructs a validator with a \a parent object that
|
---|
275 | accepts all integers.
|
---|
276 | */
|
---|
277 |
|
---|
278 | QIntValidator::QIntValidator(QObject * parent)
|
---|
279 | : QValidator(parent)
|
---|
280 | {
|
---|
281 | b = INT_MIN;
|
---|
282 | t = INT_MAX;
|
---|
283 | }
|
---|
284 |
|
---|
285 |
|
---|
286 | /*!
|
---|
287 | Constructs a validator with a \a parent, that accepts integers
|
---|
288 | from \a minimum to \a maximum inclusive.
|
---|
289 | */
|
---|
290 |
|
---|
291 | QIntValidator::QIntValidator(int minimum, int maximum,
|
---|
292 | QObject * parent)
|
---|
293 | : QValidator(parent)
|
---|
294 | {
|
---|
295 | b = minimum;
|
---|
296 | t = maximum;
|
---|
297 | }
|
---|
298 |
|
---|
299 |
|
---|
300 | #ifdef QT3_SUPPORT
|
---|
301 | /*!
|
---|
302 | \obsolete
|
---|
303 |
|
---|
304 | Constructs a validator with a \a parent object and a \a name that
|
---|
305 | accepts all integers.
|
---|
306 | */
|
---|
307 |
|
---|
308 | QIntValidator::QIntValidator(QObject * parent, const char *name)
|
---|
309 | : QValidator(parent)
|
---|
310 | {
|
---|
311 | setObjectName(QString::fromAscii(name));
|
---|
312 | b = INT_MIN;
|
---|
313 | t = INT_MAX;
|
---|
314 | }
|
---|
315 |
|
---|
316 |
|
---|
317 | /*!
|
---|
318 | \obsolete
|
---|
319 |
|
---|
320 | Constructs a validator called \a name with a \a parent, that
|
---|
321 | accepts integers from \a minimum to \a maximum inclusive.
|
---|
322 | */
|
---|
323 |
|
---|
324 | QIntValidator::QIntValidator(int minimum, int maximum,
|
---|
325 | QObject * parent, const char* name)
|
---|
326 | : QValidator(parent)
|
---|
327 | {
|
---|
328 | setObjectName(QString::fromAscii(name));
|
---|
329 | b = minimum;
|
---|
330 | t = maximum;
|
---|
331 | }
|
---|
332 | #endif
|
---|
333 |
|
---|
334 | /*!
|
---|
335 | Destroys the validator.
|
---|
336 | */
|
---|
337 |
|
---|
338 | QIntValidator::~QIntValidator()
|
---|
339 | {
|
---|
340 | // nothing
|
---|
341 | }
|
---|
342 |
|
---|
343 |
|
---|
344 | /*!
|
---|
345 | \fn QValidator::State QIntValidator::validate(QString &input, int &pos) const
|
---|
346 |
|
---|
347 | Returns \l Acceptable if the \a input is an integer within the
|
---|
348 | valid range, \l Intermediate if the \a input is a prefix of an integer in the
|
---|
349 | valid range, and \l Invalid otherwise.
|
---|
350 |
|
---|
351 | If the valid range consists of just positive integers (e.g., 32 to 100)
|
---|
352 | and \a input is a negative integer, then Invalid is returned. (On the other
|
---|
353 | hand, if the range consists of negative integers (e.g., -100 to -32) and
|
---|
354 | \a input is a positive integer, then Intermediate is returned, because
|
---|
355 | the user might be just about to type the minus (especially for right-to-left
|
---|
356 | languages).
|
---|
357 |
|
---|
358 | \snippet doc/src/snippets/code/src_gui_widgets_qvalidator.cpp 2
|
---|
359 |
|
---|
360 | By default, the \a pos parameter is not used by this validator.
|
---|
361 | */
|
---|
362 |
|
---|
363 | static int numDigits(qlonglong n)
|
---|
364 | {
|
---|
365 | if (n == 0)
|
---|
366 | return 1;
|
---|
367 | return (int)log10(double(n)) + 1;
|
---|
368 | }
|
---|
369 |
|
---|
370 | static qlonglong pow10(int exp)
|
---|
371 | {
|
---|
372 | qlonglong result = 1;
|
---|
373 | for (int i = 0; i < exp; ++i)
|
---|
374 | result *= 10;
|
---|
375 | return result;
|
---|
376 | }
|
---|
377 |
|
---|
378 | QValidator::State QIntValidator::validate(QString & input, int&) const
|
---|
379 | {
|
---|
380 | QByteArray buff;
|
---|
381 | if (!locale().d()->validateChars(input, QLocalePrivate::IntegerMode, &buff)) {
|
---|
382 | QLocale cl(QLocale::C);
|
---|
383 | if (!cl.d()->validateChars(input, QLocalePrivate::IntegerMode, &buff))
|
---|
384 | return Invalid;
|
---|
385 | }
|
---|
386 |
|
---|
387 | if (buff.isEmpty())
|
---|
388 | return Intermediate;
|
---|
389 |
|
---|
390 | if (b >= 0 && buff.startsWith('-'))
|
---|
391 | return Invalid;
|
---|
392 |
|
---|
393 | if (t < 0 && buff.startsWith('+'))
|
---|
394 | return Invalid;
|
---|
395 |
|
---|
396 | if (buff.size() == 1 && (buff.at(0) == '+' || buff.at(0) == '-'))
|
---|
397 | return Intermediate;
|
---|
398 |
|
---|
399 | bool ok, overflow;
|
---|
400 | qlonglong entered = QLocalePrivate::bytearrayToLongLong(buff.constData(), 10, &ok, &overflow);
|
---|
401 | if (overflow || !ok)
|
---|
402 | return Invalid;
|
---|
403 | if (entered >= b && entered <= t)
|
---|
404 | return Acceptable;
|
---|
405 |
|
---|
406 | if (entered >= 0) {
|
---|
407 | // the -entered < b condition is necessary to allow people to type
|
---|
408 | // the minus last (e.g. for right-to-left languages)
|
---|
409 | return (entered > t && -entered < b) ? Invalid : Intermediate;
|
---|
410 | } else {
|
---|
411 | return (entered < b) ? Invalid : Intermediate;
|
---|
412 | }
|
---|
413 | }
|
---|
414 |
|
---|
415 |
|
---|
416 | /*!
|
---|
417 | Sets the range of the validator to only accept integers between \a
|
---|
418 | bottom and \a top inclusive.
|
---|
419 | */
|
---|
420 |
|
---|
421 | void QIntValidator::setRange(int bottom, int top)
|
---|
422 | {
|
---|
423 | b = bottom;
|
---|
424 | t = top;
|
---|
425 | }
|
---|
426 |
|
---|
427 |
|
---|
428 | /*!
|
---|
429 | \property QIntValidator::bottom
|
---|
430 | \brief the validator's lowest acceptable value
|
---|
431 |
|
---|
432 | By default, this property's value is derived from the lowest signed
|
---|
433 | integer available (typically -2147483647).
|
---|
434 |
|
---|
435 | \sa setRange()
|
---|
436 | */
|
---|
437 | void QIntValidator::setBottom(int bottom)
|
---|
438 | {
|
---|
439 | setRange(bottom, top());
|
---|
440 | }
|
---|
441 |
|
---|
442 | /*!
|
---|
443 | \property QIntValidator::top
|
---|
444 | \brief the validator's highest acceptable value
|
---|
445 |
|
---|
446 | By default, this property's value is derived from the highest signed
|
---|
447 | integer available (typically 2147483647).
|
---|
448 |
|
---|
449 | \sa setRange()
|
---|
450 | */
|
---|
451 | void QIntValidator::setTop(int top)
|
---|
452 | {
|
---|
453 | setRange(bottom(), top);
|
---|
454 | }
|
---|
455 |
|
---|
456 |
|
---|
457 | #ifndef QT_NO_REGEXP
|
---|
458 |
|
---|
459 | /*!
|
---|
460 | \internal
|
---|
461 | */
|
---|
462 | QValidator::QValidator(QObjectPrivate &d, QObject *parent)
|
---|
463 | : QObject(d, parent)
|
---|
464 | {
|
---|
465 | }
|
---|
466 |
|
---|
467 | /*!
|
---|
468 | \internal
|
---|
469 | */
|
---|
470 | QValidator::QValidator(QValidatorPrivate &d, QObject *parent)
|
---|
471 | : QObject(d, parent)
|
---|
472 | {
|
---|
473 | }
|
---|
474 |
|
---|
475 | class QDoubleValidatorPrivate : public QValidatorPrivate
|
---|
476 | {
|
---|
477 | Q_DECLARE_PUBLIC(QDoubleValidator)
|
---|
478 | public:
|
---|
479 | QDoubleValidatorPrivate()
|
---|
480 | : QValidatorPrivate()
|
---|
481 | , notation(QDoubleValidator::ScientificNotation)
|
---|
482 | {
|
---|
483 | }
|
---|
484 |
|
---|
485 | QDoubleValidator::Notation notation;
|
---|
486 | };
|
---|
487 |
|
---|
488 |
|
---|
489 | /*!
|
---|
490 | \class QDoubleValidator
|
---|
491 |
|
---|
492 | \brief The QDoubleValidator class provides range checking of
|
---|
493 | floating-point numbers.
|
---|
494 |
|
---|
495 | QDoubleValidator provides an upper bound, a lower bound, and a
|
---|
496 | limit on the number of digits after the decimal point. It does not
|
---|
497 | provide a fixup() function.
|
---|
498 |
|
---|
499 | You can set the acceptable range in one call with setRange(), or
|
---|
500 | with setBottom() and setTop(). Set the number of decimal places
|
---|
501 | with setDecimals(). The validate() function returns the validation
|
---|
502 | state.
|
---|
503 |
|
---|
504 | QDoubleValidator uses its locale() to interpret the number. For example,
|
---|
505 | in the German locale, "1,234" will be accepted as the fractional number
|
---|
506 | 1.234. In Arabic locales, QDoubleValidator will accept Arabic digits.
|
---|
507 |
|
---|
508 | In addition, QDoubleValidator is always guaranteed to accept a number
|
---|
509 | formatted according to the "C" locale. QDoubleValidator will not accept
|
---|
510 | numbers with thousand-seperators.
|
---|
511 |
|
---|
512 | \sa QIntValidator, QRegExpValidator, {Line Edits Example}
|
---|
513 | */
|
---|
514 |
|
---|
515 | /*!
|
---|
516 | \enum QDoubleValidator::Notation
|
---|
517 | \since 4.3
|
---|
518 | This enum defines the allowed notations for entering a double.
|
---|
519 |
|
---|
520 | \value StandardNotation The string is written as a standard number
|
---|
521 | (i.e. 0.015).
|
---|
522 | \value ScientificNotation The string is written in scientific
|
---|
523 | form. It may have an exponent part(i.e. 1.5E-2).
|
---|
524 | */
|
---|
525 |
|
---|
526 | /*!
|
---|
527 | Constructs a validator object with a \a parent object
|
---|
528 | that accepts any double.
|
---|
529 | */
|
---|
530 |
|
---|
531 | QDoubleValidator::QDoubleValidator(QObject * parent)
|
---|
532 | : QValidator(*new QDoubleValidatorPrivate , parent)
|
---|
533 | {
|
---|
534 | b = -HUGE_VAL;
|
---|
535 | t = HUGE_VAL;
|
---|
536 | dec = 1000;
|
---|
537 | }
|
---|
538 |
|
---|
539 |
|
---|
540 | /*!
|
---|
541 | Constructs a validator object with a \a parent object. This
|
---|
542 | validator will accept doubles from \a bottom to \a top inclusive,
|
---|
543 | with up to \a decimals digits after the decimal point.
|
---|
544 | */
|
---|
545 |
|
---|
546 | QDoubleValidator::QDoubleValidator(double bottom, double top, int decimals,
|
---|
547 | QObject * parent)
|
---|
548 | : QValidator(*new QDoubleValidatorPrivate , parent)
|
---|
549 | {
|
---|
550 | b = bottom;
|
---|
551 | t = top;
|
---|
552 | dec = decimals;
|
---|
553 | }
|
---|
554 |
|
---|
555 | #ifdef QT3_SUPPORT
|
---|
556 | /*!
|
---|
557 | \obsolete
|
---|
558 |
|
---|
559 | Constructs a validator object with a \a parent object and a \a name
|
---|
560 | that accepts any double.
|
---|
561 | */
|
---|
562 |
|
---|
563 | QDoubleValidator::QDoubleValidator(QObject * parent, const char *name)
|
---|
564 | : QValidator(*new QDoubleValidatorPrivate , parent)
|
---|
565 | {
|
---|
566 | setObjectName(QString::fromAscii(name));
|
---|
567 | b = -HUGE_VAL;
|
---|
568 | t = HUGE_VAL;
|
---|
569 | dec = 1000;
|
---|
570 | }
|
---|
571 |
|
---|
572 |
|
---|
573 | /*!
|
---|
574 | \obsolete
|
---|
575 |
|
---|
576 | Constructs a validator object with a \a parent object, called \a
|
---|
577 | name. This validator will accept doubles from \a bottom to \a top
|
---|
578 | inclusive, with up to \a decimals digits after the decimal point.
|
---|
579 | */
|
---|
580 |
|
---|
581 | QDoubleValidator::QDoubleValidator(double bottom, double top, int decimals,
|
---|
582 | QObject * parent, const char* name)
|
---|
583 | : QValidator(*new QDoubleValidatorPrivate, parent)
|
---|
584 | {
|
---|
585 | setObjectName(QString::fromAscii(name));
|
---|
586 | b = bottom;
|
---|
587 | t = top;
|
---|
588 | dec = decimals;
|
---|
589 | }
|
---|
590 | #endif
|
---|
591 |
|
---|
592 | /*!
|
---|
593 | Destroys the validator.
|
---|
594 | */
|
---|
595 |
|
---|
596 | QDoubleValidator::~QDoubleValidator()
|
---|
597 | {
|
---|
598 | }
|
---|
599 |
|
---|
600 |
|
---|
601 | /*!
|
---|
602 | \fn QValidator::State QDoubleValidator::validate(QString &input, int &pos) const
|
---|
603 |
|
---|
604 | Returns \l Acceptable if the string \a input contains a double
|
---|
605 | that is within the valid range and is in the correct format.
|
---|
606 |
|
---|
607 | Returns \l Intermediate if \a input contains a double that is
|
---|
608 | outside the range or is in the wrong format; e.g. with too many
|
---|
609 | digits after the decimal point or is empty.
|
---|
610 |
|
---|
611 | Returns \l Invalid if the \a input is not a double.
|
---|
612 |
|
---|
613 | Note: If the valid range consists of just positive doubles (e.g. 0.0 to 100.0)
|
---|
614 | and \a input is a negative double then \l Invalid is returned. If notation()
|
---|
615 | is set to StandardNotation, and the input contains more digits before the
|
---|
616 | decimal point than a double in the valid range may have, \l Invalid is returned.
|
---|
617 | If notation() is ScientificNotation, and the input is not in the valid range,
|
---|
618 | \l Intermediate is returned. The value may yet become valid by changing the exponent.
|
---|
619 |
|
---|
620 | By default, the \a pos parameter is not used by this validator.
|
---|
621 | */
|
---|
622 |
|
---|
623 | #ifndef LLONG_MAX
|
---|
624 | # define LLONG_MAX Q_INT64_C(0x7fffffffffffffff)
|
---|
625 | #endif
|
---|
626 |
|
---|
627 | QValidator::State QDoubleValidator::validate(QString & input, int &) const
|
---|
628 | {
|
---|
629 | Q_D(const QDoubleValidator);
|
---|
630 |
|
---|
631 | QLocalePrivate::NumberMode numMode = QLocalePrivate::DoubleStandardMode;
|
---|
632 | switch (d->notation) {
|
---|
633 | case StandardNotation:
|
---|
634 | numMode = QLocalePrivate::DoubleStandardMode;
|
---|
635 | break;
|
---|
636 | case ScientificNotation:
|
---|
637 | numMode = QLocalePrivate::DoubleScientificMode;
|
---|
638 | break;
|
---|
639 | }
|
---|
640 |
|
---|
641 | QByteArray buff;
|
---|
642 | if (!locale().d()->validateChars(input, numMode, &buff, dec)) {
|
---|
643 | QLocale cl(QLocale::C);
|
---|
644 | if (!cl.d()->validateChars(input, numMode, &buff, dec))
|
---|
645 | return Invalid;
|
---|
646 | }
|
---|
647 |
|
---|
648 | if (buff.isEmpty())
|
---|
649 | return Intermediate;
|
---|
650 |
|
---|
651 | if (b >= 0 && buff.startsWith('-'))
|
---|
652 | return Invalid;
|
---|
653 |
|
---|
654 | if (t < 0 && buff.startsWith('+'))
|
---|
655 | return Invalid;
|
---|
656 |
|
---|
657 | bool ok, overflow;
|
---|
658 | double i = QLocalePrivate::bytearrayToDouble(buff.constData(), &ok, &overflow);
|
---|
659 | if (overflow)
|
---|
660 | return Invalid;
|
---|
661 | if (!ok)
|
---|
662 | return Intermediate;
|
---|
663 |
|
---|
664 | if (i >= b && i <= t)
|
---|
665 | return Acceptable;
|
---|
666 |
|
---|
667 | if (d->notation == StandardNotation) {
|
---|
668 | double max = qMax(qAbs(b), qAbs(t));
|
---|
669 | if (max < LLONG_MAX) {
|
---|
670 | qlonglong n = pow10(numDigits(qlonglong(max))) - 1;
|
---|
671 | if (qAbs(i) > n)
|
---|
672 | return Invalid;
|
---|
673 | }
|
---|
674 | }
|
---|
675 |
|
---|
676 | return Intermediate;
|
---|
677 | }
|
---|
678 |
|
---|
679 |
|
---|
680 | /*!
|
---|
681 | Sets the validator to accept doubles from \a minimum to \a maximum
|
---|
682 | inclusive, with at most \a decimals digits after the decimal
|
---|
683 | point.
|
---|
684 | */
|
---|
685 |
|
---|
686 | void QDoubleValidator::setRange(double minimum, double maximum, int decimals)
|
---|
687 | {
|
---|
688 | b = minimum;
|
---|
689 | t = maximum;
|
---|
690 | dec = decimals;
|
---|
691 | }
|
---|
692 |
|
---|
693 | /*!
|
---|
694 | \property QDoubleValidator::bottom
|
---|
695 | \brief the validator's minimum acceptable value
|
---|
696 |
|
---|
697 | By default, this property contains a value of -infinity.
|
---|
698 |
|
---|
699 | \sa setRange()
|
---|
700 | */
|
---|
701 |
|
---|
702 | void QDoubleValidator::setBottom(double bottom)
|
---|
703 | {
|
---|
704 | setRange(bottom, top(), decimals());
|
---|
705 | }
|
---|
706 |
|
---|
707 |
|
---|
708 | /*!
|
---|
709 | \property QDoubleValidator::top
|
---|
710 | \brief the validator's maximum acceptable value
|
---|
711 |
|
---|
712 | By default, this property contains a value of infinity.
|
---|
713 |
|
---|
714 | \sa setRange()
|
---|
715 | */
|
---|
716 |
|
---|
717 | void QDoubleValidator::setTop(double top)
|
---|
718 | {
|
---|
719 | setRange(bottom(), top, decimals());
|
---|
720 | }
|
---|
721 |
|
---|
722 | /*!
|
---|
723 | \property QDoubleValidator::decimals
|
---|
724 | \brief the validator's maximum number of digits after the decimal point
|
---|
725 |
|
---|
726 | By default, this property contains a value of 1000.
|
---|
727 |
|
---|
728 | \sa setRange()
|
---|
729 | */
|
---|
730 |
|
---|
731 | void QDoubleValidator::setDecimals(int decimals)
|
---|
732 | {
|
---|
733 | setRange(bottom(), top(), decimals);
|
---|
734 | }
|
---|
735 |
|
---|
736 | /*!
|
---|
737 | \property QDoubleValidator::notation
|
---|
738 | \since 4.3
|
---|
739 | \brief the notation of how a string can describe a number
|
---|
740 |
|
---|
741 | By default, this property is set to ScientificNotation.
|
---|
742 |
|
---|
743 | \sa Notation
|
---|
744 | */
|
---|
745 |
|
---|
746 | void QDoubleValidator::setNotation(Notation newNotation)
|
---|
747 | {
|
---|
748 | Q_D(QDoubleValidator);
|
---|
749 | d->notation = newNotation;
|
---|
750 | }
|
---|
751 |
|
---|
752 | QDoubleValidator::Notation QDoubleValidator::notation() const
|
---|
753 | {
|
---|
754 | Q_D(const QDoubleValidator);
|
---|
755 | return d->notation;
|
---|
756 | }
|
---|
757 |
|
---|
758 | /*!
|
---|
759 | \class QRegExpValidator
|
---|
760 | \brief The QRegExpValidator class is used to check a string
|
---|
761 | against a regular expression.
|
---|
762 |
|
---|
763 | QRegExpValidator uses a regular expression (regexp) to
|
---|
764 | determine whether an input string is \l Acceptable, \l
|
---|
765 | Intermediate, or \l Invalid. The regexp can either be supplied
|
---|
766 | when the QRegExpValidator is constructed, or at a later time.
|
---|
767 |
|
---|
768 | When QRegExpValidator determines whether a string is \l Acceptable
|
---|
769 | or not, the regexp is treated as if it begins with the start of string
|
---|
770 | assertion (\bold{^}) and ends with the end of string assertion
|
---|
771 | (\bold{$}); the match is against the entire input string, or from
|
---|
772 | the given position if a start position greater than zero is given.
|
---|
773 |
|
---|
774 | If a string is a prefix of an \l Acceptable string, it is considered
|
---|
775 | \l Intermediate. For example, "" and "A" are \l Intermediate for the
|
---|
776 | regexp \bold{[A-Z][0-9]} (whereas "_" would be \l Invalid).
|
---|
777 |
|
---|
778 | For a brief introduction to Qt's regexp engine, see \l QRegExp.
|
---|
779 |
|
---|
780 | Example of use:
|
---|
781 | \snippet doc/src/snippets/code/src_gui_widgets_qvalidator.cpp 3
|
---|
782 |
|
---|
783 | Below we present some examples of validators. In practice they would
|
---|
784 | normally be associated with a widget as in the example above.
|
---|
785 |
|
---|
786 | \snippet doc/src/snippets/code/src_gui_widgets_qvalidator.cpp 4
|
---|
787 |
|
---|
788 | \sa QRegExp, QIntValidator, QDoubleValidator, {Settings Editor Example}
|
---|
789 | */
|
---|
790 |
|
---|
791 | /*!
|
---|
792 | Constructs a validator with a \a parent object that accepts
|
---|
793 | any string (including an empty one) as valid.
|
---|
794 | */
|
---|
795 |
|
---|
796 | QRegExpValidator::QRegExpValidator(QObject *parent)
|
---|
797 | : QValidator(parent), r(QString::fromLatin1(".*"))
|
---|
798 | {
|
---|
799 | }
|
---|
800 |
|
---|
801 | /*!
|
---|
802 | Constructs a validator with a \a parent object that
|
---|
803 | accepts all strings that match the regular expression \a rx.
|
---|
804 |
|
---|
805 | The match is made against the entire string; e.g. if the regexp is
|
---|
806 | \bold{[A-Fa-f0-9]+} it will be treated as \bold{^[A-Fa-f0-9]+$}.
|
---|
807 | */
|
---|
808 |
|
---|
809 | QRegExpValidator::QRegExpValidator(const QRegExp& rx, QObject *parent)
|
---|
810 | : QValidator(parent), r(rx)
|
---|
811 | {
|
---|
812 | }
|
---|
813 |
|
---|
814 | #ifdef QT3_SUPPORT
|
---|
815 | /*!
|
---|
816 | \obsolete
|
---|
817 |
|
---|
818 | Constructs a validator with a \a parent object and \a name that accepts
|
---|
819 | any string (including an empty one) as valid.
|
---|
820 | */
|
---|
821 |
|
---|
822 | QRegExpValidator::QRegExpValidator(QObject *parent, const char *name)
|
---|
823 | : QValidator(parent), r(QString::fromLatin1(".*"))
|
---|
824 | {
|
---|
825 | setObjectName(QString::fromAscii(name));
|
---|
826 | }
|
---|
827 |
|
---|
828 | /*!
|
---|
829 | \obsolete
|
---|
830 |
|
---|
831 | Constructs a validator with a \a parent object and a \a name that
|
---|
832 | accepts all strings that match the regular expression \a rx.
|
---|
833 |
|
---|
834 | The match is made against the entire string; e.g. if the regexp is
|
---|
835 | \bold{[A-Fa-f0-9]+} it will be treated as \bold{^[A-Fa-f0-9]+$}.
|
---|
836 | */
|
---|
837 |
|
---|
838 | QRegExpValidator::QRegExpValidator(const QRegExp& rx, QObject *parent,
|
---|
839 | const char *name)
|
---|
840 | : QValidator(parent), r(rx)
|
---|
841 | {
|
---|
842 | setObjectName(QString::fromAscii(name));
|
---|
843 | }
|
---|
844 | #endif
|
---|
845 |
|
---|
846 | /*!
|
---|
847 | Destroys the validator.
|
---|
848 | */
|
---|
849 |
|
---|
850 | QRegExpValidator::~QRegExpValidator()
|
---|
851 | {
|
---|
852 | }
|
---|
853 |
|
---|
854 | /*!
|
---|
855 | Returns \l Acceptable if \a input is matched by the regular
|
---|
856 | expression for this validator, \l Intermediate if it has matched
|
---|
857 | partially (i.e. could be a valid match if additional valid
|
---|
858 | characters are added), and \l Invalid if \a input is not matched.
|
---|
859 |
|
---|
860 | The \a pos parameter is set to the length of the \a input parameter.
|
---|
861 |
|
---|
862 | For example, if the regular expression is \bold{\\w\\d\\d}
|
---|
863 | (word-character, digit, digit) then "A57" is \l Acceptable,
|
---|
864 | "E5" is \l Intermediate, and "+9" is \l Invalid.
|
---|
865 |
|
---|
866 | \sa QRegExp::exactMatch()
|
---|
867 | */
|
---|
868 |
|
---|
869 | QValidator::State QRegExpValidator::validate(QString &input, int& pos) const
|
---|
870 | {
|
---|
871 | if (r.exactMatch(input)) {
|
---|
872 | return Acceptable;
|
---|
873 | } else {
|
---|
874 | if (const_cast<QRegExp &>(r).matchedLength() == input.size()) {
|
---|
875 | return Intermediate;
|
---|
876 | } else {
|
---|
877 | pos = input.size();
|
---|
878 | return Invalid;
|
---|
879 | }
|
---|
880 | }
|
---|
881 | }
|
---|
882 |
|
---|
883 | /*!
|
---|
884 | \property QRegExpValidator::regExp
|
---|
885 | \brief the regular expression used for validation
|
---|
886 |
|
---|
887 | By default, this property contains a regular expression with the pattern \c{.*}
|
---|
888 | that matches any string.
|
---|
889 | */
|
---|
890 |
|
---|
891 | void QRegExpValidator::setRegExp(const QRegExp& rx)
|
---|
892 | {
|
---|
893 | r = rx;
|
---|
894 | }
|
---|
895 |
|
---|
896 | #endif
|
---|
897 |
|
---|
898 | QT_END_NAMESPACE
|
---|
899 |
|
---|
900 | #endif // QT_NO_VALIDATOR
|
---|