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

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