qvalidator.cpp

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

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9