util/qvalidator.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/**************************************************************************** -
2** -
3** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -
4** Contact: http://www.qt-project.org/legal -
5** -
6** This file is part of the QtGui module of the Qt Toolkit. -
7** -
8** $QT_BEGIN_LICENSE:LGPL$ -
9** Commercial License Usage -
10** Licensees holding valid commercial Qt licenses may use this file in -
11** accordance with the commercial license agreement provided with the -
12** Software or, alternatively, in accordance with the terms contained in -
13** a written agreement between you and Digia. For licensing terms and -
14** conditions see http://qt.digia.com/licensing. For further information -
15** use the contact form at http://qt.digia.com/contact-us. -
16** -
17** GNU Lesser General Public License Usage -
18** Alternatively, this file may be used under the terms of the GNU Lesser -
19** General Public License version 2.1 as published by the Free Software -
20** Foundation and appearing in the file LICENSE.LGPL included in the -
21** packaging of this file. Please review the following information to -
22** ensure the GNU Lesser General Public License version 2.1 requirements -
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -
24** -
25** In addition, as a special exception, Digia gives you certain additional -
26** rights. These rights are described in the Digia Qt LGPL Exception -
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -
28** -
29** GNU General Public License Usage -
30** Alternatively, this file may be used under the terms of the GNU -
31** General Public License version 3.0 as published by the Free Software -
32** Foundation and appearing in the file LICENSE.GPL included in the -
33** packaging of this file. Please review the following information to -
34** ensure the GNU General Public License version 3.0 requirements will be -
35** met: http://www.gnu.org/copyleft/gpl.html. -
36** -
37** -
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 -
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 }
executed: }
Execution Count:714
714
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}
executed: }
Execution Count:549
549
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);
executed (the execution status of this line is deduced): const QValidatorPrivate * const d = d_func();
-
241 return d->locale;
executed: return d->locale;
Execution Count:605
605
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);
executed (the execution status of this line is deduced): QValidatorPrivate * const d = d_func();
-
255 if (d->locale != locale) {
evaluated: d->locale != locale
TRUEFALSE
yes
Evaluation Count:109
yes
Evaluation Count:6
6-109
256 d->locale = locale;
executed (the execution status of this line is deduced): d->locale = locale;
-
257 emit changed();
executed (the execution status of this line is deduced): changed();
-
258 }
executed: }
Execution Count:109
109
259}
executed: }
Execution Count:115
115
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 to be 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 \sa QDoubleValidator, QRegExpValidator, {Line Edits Example} -
327*/ -
328 -
329/*! -
330 Constructs a validator with a \a parent object that -
331 accepts all integers. -
332*/ -
333 -
334QIntValidator::QIntValidator(QObject * parent) -
335 : QValidator(parent) -
336{ -
337 b = INT_MIN;
executed (the execution status of this line is deduced): b = (-2147483647 - 1);
-
338 t = INT_MAX;
executed (the execution status of this line is deduced): t = 2147483647;
-
339}
executed: }
Execution Count:3
3
340 -
341 -
342/*! -
343 Constructs a validator with a \a parent, that accepts integers -
344 from \a minimum to \a maximum inclusive. -
345*/ -
346 -
347QIntValidator::QIntValidator(int minimum, int maximum, -
348 QObject * parent) -
349 : QValidator(parent) -
350{ -
351 b = minimum;
executed (the execution status of this line is deduced): b = minimum;
-
352 t = maximum;
executed (the execution status of this line is deduced): t = maximum;
-
353}
executed: }
Execution Count:140
140
354 -
355 -
356/*! -
357 Destroys the validator. -
358*/ -
359 -
360QIntValidator::~QIntValidator() -
361{ -
362 // nothing -
363} -
364 -
365 -
366/*! -
367 \fn QValidator::State QIntValidator::validate(QString &input, int &pos) const -
368 -
369 Returns \l Acceptable if the \a input is an integer within the -
370 valid range, \l Intermediate if the \a input is a prefix of an integer in the -
371 valid range, and \l Invalid otherwise. -
372 -
373 If the valid range consists of just positive integers (e.g., 32 to 100) -
374 and \a input is a negative integer, then Invalid is returned. (On the other -
375 hand, if the range consists of negative integers (e.g., -100 to -32) and -
376 \a input is a positive integer, then Intermediate is returned, because -
377 the user might be just about to type the minus (especially for right-to-left -
378 languages). -
379 -
380 \snippet code/src_gui_util_qvalidator.cpp 2 -
381 -
382 By default, the \a pos parameter is not used by this validator. -
383*/ -
384 -
385static int numDigits(qlonglong n) -
386{ -
387 if (n == 0)
evaluated: n == 0
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:32
2-32
388 return 1;
executed: return 1;
Execution Count:2
2
389 return (int)log10(double(n)) + 1;
executed: return (int)log10(double(n)) + 1;
Execution Count:32
32
390} -
391 -
392static qlonglong pow10(int exp) -
393{ -
394 qlonglong result = 1;
executed (the execution status of this line is deduced): qlonglong result = 1;
-
395 for (int i = 0; i < exp; ++i)
evaluated: i < exp
TRUEFALSE
yes
Evaluation Count:67
yes
Evaluation Count:34
34-67
396 result *= 10;
executed: result *= 10;
Execution Count:67
67
397 return result;
executed: return result;
Execution Count:34
34
398} -
399 -
400QValidator::State QIntValidator::validate(QString & input, int&) const -
401{ -
402 QByteArray buff;
executed (the execution status of this line is deduced): QByteArray buff;
-
403 if (!locale().d->validateChars(input, QLocalePrivate::IntegerMode, &buff)) {
evaluated: !locale().d->validateChars(input, QLocalePrivate::IntegerMode, &buff)
TRUEFALSE
yes
Evaluation Count:30
yes
Evaluation Count:195
30-195
404 return Invalid;
executed: return Invalid;
Execution Count:30
30
405 } -
406 -
407 if (buff.isEmpty())
evaluated: buff.isEmpty()
TRUEFALSE
yes
Evaluation Count:16
yes
Evaluation Count:179
16-179
408 return Intermediate;
executed: return Intermediate;
Execution Count:16
16
409 -
410 if (b >= 0 && buff.startsWith('-'))
evaluated: b >= 0
TRUEFALSE
yes
Evaluation Count:108
yes
Evaluation Count:71
evaluated: buff.startsWith('-')
TRUEFALSE
yes
Evaluation Count:13
yes
Evaluation Count:95
13-108
411 return Invalid;
executed: return Invalid;
Execution Count:13
13
412 -
413 if (t < 0 && buff.startsWith('+'))
evaluated: t < 0
TRUEFALSE
yes
Evaluation Count:25
yes
Evaluation Count:141
evaluated: buff.startsWith('+')
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:22
3-141
414 return Invalid;
executed: return Invalid;
Execution Count:3
3
415 -
416 if (buff.size() == 1 && (buff.at(0) == '+' || buff.at(0) == '-'))
evaluated: buff.size() == 1
TRUEFALSE
yes
Evaluation Count:54
yes
Evaluation Count:109
evaluated: buff.at(0) == '+'
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:52
evaluated: buff.at(0) == '-'
TRUEFALSE
yes
Evaluation Count:6
yes
Evaluation Count:46
2-109
417 return Intermediate;
executed: return Intermediate;
Execution Count:8
8
418 -
419 bool ok, overflow;
executed (the execution status of this line is deduced): bool ok, overflow;
-
420 qlonglong entered = QLocalePrivate::bytearrayToLongLong(buff.constData(), 10, &ok, &overflow);
executed (the execution status of this line is deduced): qlonglong entered = QLocalePrivate::bytearrayToLongLong(buff.constData(), 10, &ok, &overflow);
-
421 if (overflow || !ok)
evaluated: overflow
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:154
partially evaluated: !ok
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:154
0-154
422 return Invalid;
executed: return Invalid;
Execution Count:1
1
423 -
424 if (entered >= b && entered <= t) {
evaluated: entered >= b
TRUEFALSE
yes
Evaluation Count:128
yes
Evaluation Count:26
evaluated: entered <= t
TRUEFALSE
yes
Evaluation Count:96
yes
Evaluation Count:32
26-128
425 locale().toInt(input, &ok);
executed (the execution status of this line is deduced): locale().toInt(input, &ok);
-
426 return ok ? Acceptable : Intermediate;
executed: return ok ? Acceptable : Intermediate;
Execution Count:96
96
427 } -
428 -
429 if (entered >= 0) {
evaluated: entered >= 0
TRUEFALSE
yes
Evaluation Count:44
yes
Evaluation Count:14
14-44
430 // the -entered < b condition is necessary to allow people to type -
431 // the minus last (e.g. for right-to-left languages) -
432 return (entered > t && -entered < b) ? Invalid : Intermediate;
executed: return (entered > t && -entered < b) ? Invalid : Intermediate;
Execution Count:44
44
433 } else { -
434 return (entered < b) ? Invalid : Intermediate;
executed: return (entered < b) ? Invalid : Intermediate;
Execution Count:14
14
435 } -
436} -
437 -
438/*! \reimp */ -
439void QIntValidator::fixup(QString &input) const -
440{ -
441 QByteArray buff;
executed (the execution status of this line is deduced): QByteArray buff;
-
442 if (!locale().d->validateChars(input, QLocalePrivate::IntegerMode, &buff)) {
partially evaluated: !locale().d->validateChars(input, QLocalePrivate::IntegerMode, &buff)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:9
0-9
443 return;
never executed: return;
0
444 } -
445 bool ok, overflow;
executed (the execution status of this line is deduced): bool ok, overflow;
-
446 qlonglong entered = QLocalePrivate::bytearrayToLongLong(buff.constData(), 10, &ok, &overflow);
executed (the execution status of this line is deduced): qlonglong entered = QLocalePrivate::bytearrayToLongLong(buff.constData(), 10, &ok, &overflow);
-
447 if (ok && !overflow)
evaluated: ok
TRUEFALSE
yes
Evaluation Count:5
yes
Evaluation Count:4
partially evaluated: !overflow
TRUEFALSE
yes
Evaluation Count:5
no
Evaluation Count:0
0-5
448 input = locale().toString(entered);
executed: input = locale().toString(entered);
Execution Count:5
5
449}
executed: }
Execution Count:9
9
450 -
451/*! -
452 Sets the range of the validator to only accept integers between \a -
453 bottom and \a top inclusive. -
454*/ -
455 -
456void QIntValidator::setRange(int bottom, int top) -
457{ -
458 bool rangeChanged = false;
executed (the execution status of this line is deduced): bool rangeChanged = false;
-
459 if (b != bottom) {
evaluated: b != bottom
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:3
3
460 b = bottom;
executed (the execution status of this line is deduced): b = bottom;
-
461 rangeChanged = true;
executed (the execution status of this line is deduced): rangeChanged = true;
-
462 emit bottomChanged(b);
executed (the execution status of this line is deduced): bottomChanged(b);
-
463 }
executed: }
Execution Count:3
3
464 -
465 if (t != top) {
evaluated: t != top
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:3
3
466 t = top;
executed (the execution status of this line is deduced): t = top;
-
467 rangeChanged = true;
executed (the execution status of this line is deduced): rangeChanged = true;
-
468 emit topChanged(t);
executed (the execution status of this line is deduced): topChanged(t);
-
469 }
executed: }
Execution Count:3
3
470 -
471 if (rangeChanged)
evaluated: rangeChanged
TRUEFALSE
yes
Evaluation Count:5
yes
Evaluation Count:1
1-5
472 emit changed();
executed: changed();
Execution Count:5
5
473}
executed: }
Execution Count:6
6
474 -
475 -
476/*! -
477 \property QIntValidator::bottom -
478 \brief the validator's lowest acceptable value -
479 -
480 By default, this property's value is derived from the lowest signed -
481 integer available (typically -2147483647). -
482 -
483 \sa setRange() -
484*/ -
485void QIntValidator::setBottom(int bottom) -
486{ -
487 setRange(bottom, top());
executed (the execution status of this line is deduced): setRange(bottom, top());
-
488}
executed: }
Execution Count:1
1
489 -
490/*! -
491 \property QIntValidator::top -
492 \brief the validator's highest acceptable value -
493 -
494 By default, this property's value is derived from the highest signed -
495 integer available (typically 2147483647). -
496 -
497 \sa setRange() -
498*/ -
499void QIntValidator::setTop(int top) -
500{ -
501 setRange(bottom(), top);
executed (the execution status of this line is deduced): setRange(bottom(), top);
-
502}
executed: }
Execution Count:1
1
503 -
504 -
505#ifndef QT_NO_REGEXP -
506 -
507/*! -
508 \internal -
509*/ -
510QValidator::QValidator(QObjectPrivate &d, QObject *parent) -
511 : QObject(d, parent) -
512{ -
513}
never executed: }
0
514 -
515/*! -
516 \internal -
517*/ -
518QValidator::QValidator(QValidatorPrivate &d, QObject *parent) -
519 : QObject(d, parent) -
520{ -
521}
executed: }
Execution Count:165
165
522 -
523class QDoubleValidatorPrivate : public QValidatorPrivate -
524{ -
525 Q_DECLARE_PUBLIC(QDoubleValidator) -
526public: -
527 QDoubleValidatorPrivate() -
528 : QValidatorPrivate() -
529 , notation(QDoubleValidator::ScientificNotation) -
530 { -
531 }
executed: }
Execution Count:165
165
532 -
533 QDoubleValidator::Notation notation; -
534 -
535 QValidator::State validateWithLocale(QString & input, QLocalePrivate::NumberMode numMode, const QLocale &locale) const; -
536}; -
537 -
538 -
539/*! -
540 \class QDoubleValidator -
541 -
542 \brief The QDoubleValidator class provides range checking of -
543 floating-point numbers. -
544 \inmodule QtGui -
545 -
546 QDoubleValidator provides an upper bound, a lower bound, and a -
547 limit on the number of digits after the decimal point. It does not -
548 provide a fixup() function. -
549 -
550 You can set the acceptable range in one call with setRange(), or -
551 with setBottom() and setTop(). Set the number of decimal places -
552 with setDecimals(). The validate() function returns the validation -
553 state. -
554 -
555 QDoubleValidator uses its locale() to interpret the number. For example, -
556 in the German locale, "1,234" will be accepted as the fractional number -
557 1.234. In Arabic locales, QDoubleValidator will accept Arabic digits. -
558 -
559 \sa QIntValidator, QRegExpValidator, {Line Edits Example} -
560*/ -
561 -
562 /*! -
563 \enum QDoubleValidator::Notation -
564 \since 4.3 -
565 This enum defines the allowed notations for entering a double. -
566 -
567 \value StandardNotation The string is written as a standard number -
568 (i.e. 0.015). -
569 \value ScientificNotation The string is written in scientific -
570 form. It may have an exponent part(i.e. 1.5E-2). -
571*/ -
572 -
573/*! -
574 Constructs a validator object with a \a parent object -
575 that accepts any double. -
576*/ -
577 -
578QDoubleValidator::QDoubleValidator(QObject * parent) -
579 : QValidator(*new QDoubleValidatorPrivate , parent) -
580{ -
581 b = -HUGE_VAL;
never executed (the execution status of this line is deduced): b = -(__builtin_huge_val());
-
582 t = HUGE_VAL;
never executed (the execution status of this line is deduced): t = (__builtin_huge_val());
-
583 dec = 1000;
never executed (the execution status of this line is deduced): dec = 1000;
-
584}
never executed: }
0
585 -
586 -
587/*! -
588 Constructs a validator object with a \a parent object. This -
589 validator will accept doubles from \a bottom to \a top inclusive, -
590 with up to \a decimals digits after the decimal point. -
591*/ -
592 -
593QDoubleValidator::QDoubleValidator(double bottom, double top, int decimals, -
594 QObject * parent) -
595 : QValidator(*new QDoubleValidatorPrivate , parent) -
596{ -
597 b = bottom;
executed (the execution status of this line is deduced): b = bottom;
-
598 t = top;
executed (the execution status of this line is deduced): t = top;
-
599 dec = decimals;
executed (the execution status of this line is deduced): dec = decimals;
-
600}
executed: }
Execution Count:165
165
601 -
602 -
603/*! -
604 Destroys the validator. -
605*/ -
606 -
607QDoubleValidator::~QDoubleValidator() -
608{ -
609} -
610 -
611 -
612/*! -
613 \fn QValidator::State QDoubleValidator::validate(QString &input, int &pos) const -
614 -
615 Returns \l Acceptable if the string \a input contains a double -
616 that is within the valid range and is in the correct format. -
617 -
618 Returns \l Intermediate if \a input contains a double that is -
619 outside the range or is in the wrong format; e.g. with too many -
620 digits after the decimal point or is empty. -
621 -
622 Returns \l Invalid if the \a input is not a double. -
623 -
624 Note: If the valid range consists of just positive doubles (e.g. 0.0 to 100.0) -
625 and \a input is a negative double then \l Invalid is returned. If notation() -
626 is set to StandardNotation, and the input contains more digits before the -
627 decimal point than a double in the valid range may have, \l Invalid is returned. -
628 If notation() is ScientificNotation, and the input is not in the valid range, -
629 \l Intermediate is returned. The value may yet become valid by changing the exponent. -
630 -
631 By default, the \a pos parameter is not used by this validator. -
632*/ -
633 -
634#ifndef LLONG_MAX -
635# define LLONG_MAX Q_INT64_C(0x7fffffffffffffff) -
636#endif -
637 -
638QValidator::State QDoubleValidator::validate(QString & input, int &) const -
639{ -
640 Q_D(const QDoubleValidator);
executed (the execution status of this line is deduced): const QDoubleValidatorPrivate * const d = d_func();
-
641 -
642 QLocalePrivate::NumberMode numMode = QLocalePrivate::DoubleStandardMode;
executed (the execution status of this line is deduced): QLocalePrivate::NumberMode numMode = QLocalePrivate::DoubleStandardMode;
-
643 switch (d->notation) { -
644 case StandardNotation: -
645 numMode = QLocalePrivate::DoubleStandardMode;
executed (the execution status of this line is deduced): numMode = QLocalePrivate::DoubleStandardMode;
-
646 break;
executed: break;
Execution Count:156
156
647 case ScientificNotation: -
648 numMode = QLocalePrivate::DoubleScientificMode;
executed (the execution status of this line is deduced): numMode = QLocalePrivate::DoubleScientificMode;
-
649 break;
executed: break;
Execution Count:113
113
650 } -
651 -
652 return d->validateWithLocale(input, numMode, locale());
executed: return d->validateWithLocale(input, numMode, locale());
Execution Count:269
269
653} -
654 -
655QValidator::State QDoubleValidatorPrivate::validateWithLocale(QString &input, QLocalePrivate::NumberMode numMode, const QLocale &locale) const -
656{ -
657 Q_Q(const QDoubleValidator);
executed (the execution status of this line is deduced): const QDoubleValidator * const q = q_func();
-
658 QByteArray buff;
executed (the execution status of this line is deduced): QByteArray buff;
-
659 if (!locale.d->validateChars(input, numMode, &buff, q->dec))
evaluated: !locale.d->validateChars(input, numMode, &buff, q->dec)
TRUEFALSE
yes
Evaluation Count:91
yes
Evaluation Count:178
91-178
660 return QValidator::Invalid;
executed: return QValidator::Invalid;
Execution Count:91
91
661 -
662 if (buff.isEmpty())
evaluated: buff.isEmpty()
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:176
2-176
663 return QValidator::Intermediate;
executed: return QValidator::Intermediate;
Execution Count:2
2
664 -
665 if (q->b >= 0 && buff.startsWith('-'))
evaluated: q->b >= 0
TRUEFALSE
yes
Evaluation Count:123
yes
Evaluation Count:53
evaluated: buff.startsWith('-')
TRUEFALSE
yes
Evaluation Count:15
yes
Evaluation Count:108
15-123
666 return QValidator::Invalid;
executed: return QValidator::Invalid;
Execution Count:15
15
667 -
668 if (q->t < 0 && buff.startsWith('+'))
evaluated: q->t < 0
TRUEFALSE
yes
Evaluation Count:17
yes
Evaluation Count:144
evaluated: buff.startsWith('+')
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:13
4-144
669 return QValidator::Invalid;
executed: return QValidator::Invalid;
Execution Count:4
4
670 -
671 bool ok, overflow;
executed (the execution status of this line is deduced): bool ok, overflow;
-
672 double i = QLocalePrivate::bytearrayToDouble(buff.constData(), &ok, &overflow);
executed (the execution status of this line is deduced): double i = QLocalePrivate::bytearrayToDouble(buff.constData(), &ok, &overflow);
-
673 if (overflow)
partially evaluated: overflow
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:157
0-157
674 return QValidator::Invalid;
never executed: return QValidator::Invalid;
0
675 if (!ok)
evaluated: !ok
TRUEFALSE
yes
Evaluation Count:48
yes
Evaluation Count:109
48-109
676 return QValidator::Intermediate;
executed: return QValidator::Intermediate;
Execution Count:48
48
677 -
678 if (i >= q->b && i <= q->t)
evaluated: i >= q->b
TRUEFALSE
yes
Evaluation Count:95
yes
Evaluation Count:14
evaluated: i <= q->t
TRUEFALSE
yes
Evaluation Count:59
yes
Evaluation Count:36
14-95
679 return QValidator::Acceptable;
executed: return QValidator::Acceptable;
Execution Count:59
59
680 -
681 if (notation == QDoubleValidator::StandardNotation) {
evaluated: notation == QDoubleValidator::StandardNotation
TRUEFALSE
yes
Evaluation Count:34
yes
Evaluation Count:16
16-34
682 double max = qMax(qAbs(q->b), qAbs(q->t));
executed (the execution status of this line is deduced): double max = qMax(qAbs(q->b), qAbs(q->t));
-
683 if (max < LLONG_MAX) {
partially evaluated: max < 9223372036854775807LL
TRUEFALSE
yes
Evaluation Count:34
no
Evaluation Count:0
0-34
684 qlonglong n = pow10(numDigits(qlonglong(max))) - 1;
executed (the execution status of this line is deduced): qlonglong n = pow10(numDigits(qlonglong(max))) - 1;
-
685 if (qAbs(i) > n)
evaluated: qAbs(i) > n
TRUEFALSE
yes
Evaluation Count:7
yes
Evaluation Count:27
7-27
686 return QValidator::Invalid;
executed: return QValidator::Invalid;
Execution Count:7
7
687 }
executed: }
Execution Count:27
27
688 }
executed: }
Execution Count:27
27
689 -
690 return QValidator::Intermediate;
executed: return QValidator::Intermediate;
Execution Count:43
43
691} -
692 -
693 -
694/*! -
695 Sets the validator to accept doubles from \a minimum to \a maximum -
696 inclusive, with at most \a decimals digits after the decimal -
697 point. -
698*/ -
699 -
700void QDoubleValidator::setRange(double minimum, double maximum, int decimals) -
701{ -
702 bool rangeChanged = false;
executed (the execution status of this line is deduced): bool rangeChanged = false;
-
703 if (b != minimum) {
evaluated: b != minimum
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:5
3-5
704 b = minimum;
executed (the execution status of this line is deduced): b = minimum;
-
705 rangeChanged = true;
executed (the execution status of this line is deduced): rangeChanged = true;
-
706 emit bottomChanged(b);
executed (the execution status of this line is deduced): bottomChanged(b);
-
707 }
executed: }
Execution Count:3
3
708 -
709 if (t != maximum) {
evaluated: t != maximum
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:5
3-5
710 t = maximum;
executed (the execution status of this line is deduced): t = maximum;
-
711 rangeChanged = true;
executed (the execution status of this line is deduced): rangeChanged = true;
-
712 emit topChanged(t);
executed (the execution status of this line is deduced): topChanged(t);
-
713 }
executed: }
Execution Count:3
3
714 -
715 if (dec != decimals) {
evaluated: dec != decimals
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:5
3-5
716 dec = decimals;
executed (the execution status of this line is deduced): dec = decimals;
-
717 rangeChanged = true;
executed (the execution status of this line is deduced): rangeChanged = true;
-
718 emit decimalsChanged(dec);
executed (the execution status of this line is deduced): decimalsChanged(dec);
-
719 }
executed: }
Execution Count:3
3
720 if (rangeChanged)
evaluated: rangeChanged
TRUEFALSE
yes
Evaluation Count:7
yes
Evaluation Count:1
1-7
721 emit changed();
executed: changed();
Execution Count:7
7
722}
executed: }
Execution Count:8
8
723 -
724/*! -
725 \property QDoubleValidator::bottom -
726 \brief the validator's minimum acceptable value -
727 -
728 By default, this property contains a value of -infinity. -
729 -
730 \sa setRange() -
731*/ -
732 -
733void QDoubleValidator::setBottom(double bottom) -
734{ -
735 setRange(bottom, top(), decimals());
executed (the execution status of this line is deduced): setRange(bottom, top(), decimals());
-
736}
executed: }
Execution Count:1
1
737 -
738 -
739/*! -
740 \property QDoubleValidator::top -
741 \brief the validator's maximum acceptable value -
742 -
743 By default, this property contains a value of infinity. -
744 -
745 \sa setRange() -
746*/ -
747 -
748void QDoubleValidator::setTop(double top) -
749{ -
750 setRange(bottom(), top, decimals());
executed (the execution status of this line is deduced): setRange(bottom(), top, decimals());
-
751}
executed: }
Execution Count:1
1
752 -
753/*! -
754 \property QDoubleValidator::decimals -
755 \brief the validator's maximum number of digits after the decimal point -
756 -
757 By default, this property contains a value of 1000. -
758 -
759 \sa setRange() -
760*/ -
761 -
762void QDoubleValidator::setDecimals(int decimals) -
763{ -
764 setRange(bottom(), top(), decimals);
executed (the execution status of this line is deduced): setRange(bottom(), top(), decimals);
-
765}
executed: }
Execution Count:1
1
766 -
767/*! -
768 \property QDoubleValidator::notation -
769 \since 4.3 -
770 \brief the notation of how a string can describe a number -
771 -
772 By default, this property is set to ScientificNotation. -
773 -
774 \sa Notation -
775*/ -
776 -
777void QDoubleValidator::setNotation(Notation newNotation) -
778{ -
779 Q_D(QDoubleValidator);
executed (the execution status of this line is deduced): QDoubleValidatorPrivate * const d = d_func();
-
780 if (d->notation != newNotation) {
evaluated: d->notation != newNotation
TRUEFALSE
yes
Evaluation Count:157
yes
Evaluation Count:9
9-157
781 d->notation = newNotation;
executed (the execution status of this line is deduced): d->notation = newNotation;
-
782 emit notationChanged(d->notation);
executed (the execution status of this line is deduced): notationChanged(d->notation);
-
783 emit changed();
executed (the execution status of this line is deduced): changed();
-
784 }
executed: }
Execution Count:157
157
785}
executed: }
Execution Count:166
166
786 -
787QDoubleValidator::Notation QDoubleValidator::notation() const -
788{ -
789 Q_D(const QDoubleValidator);
executed (the execution status of this line is deduced): const QDoubleValidatorPrivate * const d = d_func();
-
790 return d->notation;
executed: return d->notation;
Execution Count:2
2
791} -
792 -
793/*! -
794 \class QRegExpValidator -
795 \brief The QRegExpValidator class is used to check a string -
796 against a regular expression. -
797 \inmodule QtGui -
798 -
799 QRegExpValidator uses a regular expression (regexp) to -
800 determine whether an input string is \l Acceptable, \l -
801 Intermediate, or \l Invalid. The regexp can either be supplied -
802 when the QRegExpValidator is constructed, or at a later time. -
803 -
804 When QRegExpValidator determines whether a string is \l Acceptable -
805 or not, the regexp is treated as if it begins with the start of string -
806 assertion (\b{^}) and ends with the end of string assertion -
807 (\b{$}); the match is against the entire input string, or from -
808 the given position if a start position greater than zero is given. -
809 -
810 If a string is a prefix of an \l Acceptable string, it is considered -
811 \l Intermediate. For example, "" and "A" are \l Intermediate for the -
812 regexp \b{[A-Z][0-9]} (whereas "_" would be \l Invalid). -
813 -
814 For a brief introduction to Qt's regexp engine, see \l QRegExp. -
815 -
816 Example of use: -
817 \snippet code/src_gui_util_qvalidator.cpp 3 -
818 -
819 Below we present some examples of validators. In practice they would -
820 normally be associated with a widget as in the example above. -
821 -
822 \snippet code/src_gui_util_qvalidator.cpp 4 -
823 -
824 \sa QRegExp, QIntValidator, QDoubleValidator, {Settings Editor Example} -
825*/ -
826 -
827/*! -
828 Constructs a validator with a \a parent object that accepts -
829 any string (including an empty one) as valid. -
830*/ -
831 -
832QRegExpValidator::QRegExpValidator(QObject *parent) -
833 : QValidator(parent), r(QString::fromLatin1(".*")) -
834{ -
835}
executed: }
Execution Count:11
11
836 -
837/*! -
838 Constructs a validator with a \a parent object that -
839 accepts all strings that match the regular expression \a rx. -
840 -
841 The match is made against the entire string; e.g. if the regexp is -
842 \b{[A-Fa-f0-9]+} it will be treated as \b{^[A-Fa-f0-9]+$}. -
843*/ -
844 -
845QRegExpValidator::QRegExpValidator(const QRegExp& rx, QObject *parent) -
846 : QValidator(parent), r(rx) -
847{ -
848}
never executed: }
0
849 -
850 -
851/*! -
852 Destroys the validator. -
853*/ -
854 -
855QRegExpValidator::~QRegExpValidator() -
856{ -
857} -
858 -
859/*! -
860 Returns \l Acceptable if \a input is matched by the regular -
861 expression for this validator, \l Intermediate if it has matched -
862 partially (i.e. could be a valid match if additional valid -
863 characters are added), and \l Invalid if \a input is not matched. -
864 -
865 Additionally, if \a input is not matched, the \a pos parameter is set to -
866 the length of the \a input parameter. -
867 -
868 For example, if the regular expression is \b{\\w\\d\\d} -
869 (word-character, digit, digit) then "A57" is \l Acceptable, -
870 "E5" is \l Intermediate, and "+9" is \l Invalid. -
871 -
872 \sa QRegExp::exactMatch() -
873*/ -
874 -
875QValidator::State QRegExpValidator::validate(QString &input, int& pos) const -
876{ -
877 QRegExp copy = r;
executed (the execution status of this line is deduced): QRegExp copy = r;
-
878 if (copy.exactMatch(input)) {
evaluated: copy.exactMatch(input)
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:7
4-7
879 return Acceptable;
executed: return Acceptable;
Execution Count:4
4
880 } else { -
881 if (copy.matchedLength() == input.size()) {
evaluated: copy.matchedLength() == input.size()
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:5
2-5
882 return Intermediate;
executed: return Intermediate;
Execution Count:2
2
883 } else { -
884 pos = input.size();
executed (the execution status of this line is deduced): pos = input.size();
-
885 return Invalid;
executed: return Invalid;
Execution Count:5
5
886 } -
887 } -
888} -
889 -
890/*! -
891 \property QRegExpValidator::regExp -
892 \brief the regular expression used for validation -
893 -
894 By default, this property contains a regular expression with the pattern \c{.*} -
895 that matches any string. -
896*/ -
897 -
898void QRegExpValidator::setRegExp(const QRegExp& rx) -
899{ -
900 if (r != rx) {
partially evaluated: r != rx
TRUEFALSE
yes
Evaluation Count:11
no
Evaluation Count:0
0-11
901 r = rx;
executed (the execution status of this line is deduced): r = rx;
-
902 emit regExpChanged(r);
executed (the execution status of this line is deduced): regExpChanged(r);
-
903 emit changed();
executed (the execution status of this line is deduced): changed();
-
904 }
executed: }
Execution Count:11
11
905}
executed: }
Execution Count:11
11
906 -
907#endif -
908 -
909QT_END_NAMESPACE -
910 -
911#endif // QT_NO_VALIDATOR -
912 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial