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 | |
52 | QT_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 | QRegularExpressionValidator 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, QRegularExpressionValidator, {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 | class QValidatorPrivate : public QObjectPrivate{ |
196 | Q_DECLARE_PUBLIC(QValidator) |
197 | public: |
198 | QValidatorPrivate() : QObjectPrivate() |
199 | { |
200 | } |
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 | |
211 | QValidator::QValidator(QObject * parent) |
212 | : QValidator(*new QValidatorPrivate, parent) |
213 | { |
214 | } |
215 | |
216 | /*! |
217 | Destroys the validator, freeing any storage and other resources |
218 | used. |
219 | */ |
220 | |
221 | QValidator::~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 | */ |
231 | QLocale QValidator::locale() const |
232 | { |
233 | Q_D(const QValidator); |
234 | return d->locale; |
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 | */ |
245 | void QValidator::setLocale(const QLocale &locale) |
246 | { |
247 | Q_D(QValidator); |
248 | if (d->locale != locale) { |
249 | d->locale = locale; |
250 | emit changed(); |
251 | } |
252 | } |
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 | |
286 | void 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, QRegularExpressionValidator, QLocale::toInt(), {Line Edits Example} |
325 | */ |
326 | |
327 | /*! |
328 | Constructs a validator with a \a parent object that |
329 | accepts all integers. |
330 | */ |
331 | |
332 | QIntValidator::QIntValidator(QObject * parent) |
333 | : QIntValidator(INT_MIN, INT_MAX, parent) |
334 | { |
335 | } |
336 | |
337 | |
338 | /*! |
339 | Constructs a validator with a \a parent, that accepts integers |
340 | from \a minimum to \a maximum inclusive. |
341 | */ |
342 | |
343 | QIntValidator::QIntValidator(int minimum, int maximum, |
344 | QObject * parent) |
345 | : QValidator(parent) |
346 | { |
347 | b = minimum; |
348 | t = maximum; |
349 | } |
350 | |
351 | |
352 | /*! |
353 | Destroys the validator. |
354 | */ |
355 | |
356 | QIntValidator::~QIntValidator() |
357 | { |
358 | // nothing |
359 | } |
360 | |
361 | |
362 | /*! |
363 | \fn QValidator::State QIntValidator::validate(QString &input, int &pos) const |
364 | |
365 | Returns \l Acceptable if the \a input is an integer within the |
366 | valid range. If \a input has at most as many digits as the top of the range, |
367 | or is a prefix of an integer in the valid range, returns \l Intermediate. |
368 | Otherwise, returns \l Invalid. |
369 | |
370 | If the valid range consists of just positive integers (e.g., 32 to 100) |
371 | and \a input is a negative integer, then Invalid is returned. (On the other |
372 | hand, if the range consists of negative integers (e.g., -100 to -32) and |
373 | \a input is a positive integer, then Intermediate is returned, because |
374 | the user might be just about to type the minus (especially for right-to-left |
375 | languages). |
376 | |
377 | Similarly, if the valid range is between 46 and 53, then 41 and 59 will be |
378 | evaluated as \l Intermediate, as otherwise the user wouldn't be able to |
379 | change a value from 49 to 51. |
380 | |
381 | \snippet code/src_gui_util_qvalidator.cpp 2 |
382 | |
383 | By default, the \a pos parameter is not used by this validator. |
384 | */ |
385 | |
386 | static int numDigits(qlonglong n) |
387 | { |
388 | if (n == 0) |
389 | return 1; |
390 | return (int)std::log10(double(n)) + 1; |
391 | } |
392 | |
393 | static qlonglong pow10(int exp) |
394 | { |
395 | qlonglong result = 1; |
396 | for (int i = 0; i < exp; ++i) |
397 | result *= 10; |
398 | return result; |
399 | } |
400 | |
401 | QValidator::State QIntValidator::validate(QString & input, int&) const |
402 | { |
403 | QByteArray buff; |
404 | if (!locale().d->m_data->validateChars(input, QLocaleData::IntegerMode, &buff, -1, |
405 | locale().numberOptions())) { |
406 | return Invalid; |
407 | } |
408 | |
409 | if (buff.isEmpty()) |
410 | return Intermediate; |
411 | |
412 | const bool startsWithMinus(buff[0] == '-'); |
413 | if (b >= 0 && startsWithMinus) |
414 | return Invalid; |
415 | |
416 | const bool startsWithPlus(buff[0] == '+'); |
417 | if (t < 0 && startsWithPlus) |
418 | return Invalid; |
419 | |
420 | if (buff.size() == 1 && (startsWithPlus || startsWithMinus)) |
421 | return Intermediate; |
422 | |
423 | bool ok; |
424 | qlonglong entered = QLocaleData::bytearrayToLongLong(buff.constData(), 10, &ok); |
425 | if (!ok) |
426 | return Invalid; |
427 | |
428 | if (entered >= b && entered <= t) { |
429 | locale().toInt(input, &ok); |
430 | return ok ? Acceptable : Intermediate; |
431 | } |
432 | |
433 | if (entered >= 0) { |
434 | // the -entered < b condition is necessary to allow people to type |
435 | // the minus last (e.g. for right-to-left languages) |
436 | // The buffLength > tLength condition validates values consisting |
437 | // of a number of digits equal to or less than the max value as intermediate. |
438 | |
439 | int buffLength = buff.size(); |
440 | if (startsWithPlus) |
441 | buffLength--; |
442 | const int tLength = t != 0 ? static_cast<int>(std::log10(qAbs(t))) + 1 : 1; |
443 | |
444 | return (entered > t && -entered < b && buffLength > tLength) ? Invalid : Intermediate; |
445 | } else { |
446 | return (entered < b) ? Invalid : Intermediate; |
447 | } |
448 | } |
449 | |
450 | /*! \reimp */ |
451 | void QIntValidator::fixup(QString &input) const |
452 | { |
453 | QByteArray buff; |
454 | if (!locale().d->m_data->validateChars(input, QLocaleData::IntegerMode, &buff, -1, |
455 | locale().numberOptions())) { |
456 | return; |
457 | } |
458 | bool ok; |
459 | qlonglong entered = QLocaleData::bytearrayToLongLong(buff.constData(), 10, &ok); |
460 | if (ok) |
461 | input = locale().toString(entered); |
462 | } |
463 | |
464 | /*! |
465 | Sets the range of the validator to only accept integers between \a |
466 | bottom and \a top inclusive. |
467 | */ |
468 | |
469 | void QIntValidator::setRange(int bottom, int top) |
470 | { |
471 | bool rangeChanged = false; |
472 | if (b != bottom) { |
473 | b = bottom; |
474 | rangeChanged = true; |
475 | emit bottomChanged(b); |
476 | } |
477 | |
478 | if (t != top) { |
479 | t = top; |
480 | rangeChanged = true; |
481 | emit topChanged(t); |
482 | } |
483 | |
484 | if (rangeChanged) |
485 | emit changed(); |
486 | } |
487 | |
488 | |
489 | /*! |
490 | \property QIntValidator::bottom |
491 | \brief the validator's lowest acceptable value |
492 | |
493 | By default, this property's value is derived from the lowest signed |
494 | integer available (typically -2147483647). |
495 | |
496 | \sa setRange() |
497 | */ |
498 | void QIntValidator::setBottom(int bottom) |
499 | { |
500 | setRange(bottom, top()); |
501 | } |
502 | |
503 | /*! |
504 | \property QIntValidator::top |
505 | \brief the validator's highest acceptable value |
506 | |
507 | By default, this property's value is derived from the highest signed |
508 | integer available (typically 2147483647). |
509 | |
510 | \sa setRange() |
511 | */ |
512 | void QIntValidator::setTop(int top) |
513 | { |
514 | setRange(bottom(), top); |
515 | } |
516 | |
517 | /*! |
518 | \internal |
519 | */ |
520 | QValidator::QValidator(QObjectPrivate &d, QObject *parent) |
521 | : QObject(d, parent) |
522 | { |
523 | } |
524 | |
525 | /*! |
526 | \internal |
527 | */ |
528 | QValidator::QValidator(QValidatorPrivate &d, QObject *parent) |
529 | : QObject(d, parent) |
530 | { |
531 | } |
532 | |
533 | class QDoubleValidatorPrivate : public QValidatorPrivate |
534 | { |
535 | Q_DECLARE_PUBLIC(QDoubleValidator) |
536 | public: |
537 | QDoubleValidatorPrivate() |
538 | : QValidatorPrivate() |
539 | , notation(QDoubleValidator::ScientificNotation) |
540 | { |
541 | } |
542 | |
543 | QDoubleValidator::Notation notation; |
544 | |
545 | QValidator::State validateWithLocale(QString & input, QLocaleData::NumberMode numMode, const QLocale &locale) const; |
546 | }; |
547 | |
548 | |
549 | /*! |
550 | \class QDoubleValidator |
551 | |
552 | \brief The QDoubleValidator class provides range checking of |
553 | floating-point numbers. |
554 | \inmodule QtGui |
555 | |
556 | QDoubleValidator provides an upper bound, a lower bound, and a |
557 | limit on the number of digits after the decimal point. It does not |
558 | provide a fixup() function. |
559 | |
560 | You can set the acceptable range in one call with setRange(), or |
561 | with setBottom() and setTop(). Set the number of decimal places |
562 | with setDecimals(). The validate() function returns the validation |
563 | state. |
564 | |
565 | QDoubleValidator uses its locale() to interpret the number. For example, |
566 | in the German locale, "1,234" will be accepted as the fractional number |
567 | 1.234. In Arabic locales, QDoubleValidator will accept Arabic digits. |
568 | |
569 | \note The QLocale::NumberOptions set on the locale() also affect the |
570 | way the number is interpreted. For example, since QLocale::RejectGroupSeparator |
571 | is not set by default, the validator will accept group separators. It is thus |
572 | recommended to use QLocale::toDouble() to obtain the numeric value. |
573 | |
574 | \sa QIntValidator, QRegularExpressionValidator, QLocale::toDouble(), {Line Edits Example} |
575 | */ |
576 | |
577 | /*! |
578 | \enum QDoubleValidator::Notation |
579 | \since 4.3 |
580 | This enum defines the allowed notations for entering a double. |
581 | |
582 | \value StandardNotation The string is written as a standard number |
583 | (i.e. 0.015). |
584 | \value ScientificNotation The string is written in scientific |
585 | form. It may have an exponent part(i.e. 1.5E-2). |
586 | */ |
587 | |
588 | /*! |
589 | Constructs a validator object with a \a parent object |
590 | that accepts any double. |
591 | */ |
592 | |
593 | QDoubleValidator::QDoubleValidator(QObject * parent) |
594 | : QDoubleValidator(-HUGE_VAL, HUGE_VAL, 1000, parent) |
595 | { |
596 | } |
597 | |
598 | |
599 | /*! |
600 | Constructs a validator object with a \a parent object. This |
601 | validator will accept doubles from \a bottom to \a top inclusive, |
602 | with up to \a decimals digits after the decimal point. |
603 | */ |
604 | |
605 | QDoubleValidator::QDoubleValidator(double bottom, double top, int decimals, |
606 | QObject * parent) |
607 | : QValidator(*new QDoubleValidatorPrivate , parent) |
608 | { |
609 | b = bottom; |
610 | t = top; |
611 | dec = decimals; |
612 | } |
613 | |
614 | |
615 | /*! |
616 | Destroys the validator. |
617 | */ |
618 | |
619 | QDoubleValidator::~QDoubleValidator() |
620 | { |
621 | } |
622 | |
623 | |
624 | /*! |
625 | \fn QValidator::State QDoubleValidator::validate(QString &input, int &pos) const |
626 | |
627 | Returns \l Acceptable if the string \a input contains a double |
628 | that is within the valid range and is in the correct format. |
629 | |
630 | Returns \l Intermediate if \a input contains a double that is |
631 | outside the range or is in the wrong format; e.g. is empty. |
632 | |
633 | Returns \l Invalid if the \a input is not a double or with too many |
634 | digits after the decimal point. |
635 | |
636 | Note: If the valid range consists of just positive doubles (e.g. 0.0 to 100.0) |
637 | and \a input is a negative double then \l Invalid is returned. If notation() |
638 | is set to StandardNotation, and the input contains more digits before the |
639 | decimal point than a double in the valid range may have, \l Invalid is returned. |
640 | If notation() is ScientificNotation, and the input is not in the valid range, |
641 | \l Intermediate is returned. The value may yet become valid by changing the exponent. |
642 | |
643 | By default, the \a pos parameter is not used by this validator. |
644 | */ |
645 | |
646 | #ifndef LLONG_MAX |
647 | # define LLONG_MAX Q_INT64_C(0x7fffffffffffffff) |
648 | #endif |
649 | |
650 | QValidator::State QDoubleValidator::validate(QString & input, int &) const |
651 | { |
652 | Q_D(const QDoubleValidator); |
653 | |
654 | QLocaleData::NumberMode numMode = QLocaleData::DoubleStandardMode; |
655 | switch (d->notation) { |
656 | case StandardNotation: |
657 | numMode = QLocaleData::DoubleStandardMode; |
658 | break; |
659 | case ScientificNotation: |
660 | numMode = QLocaleData::DoubleScientificMode; |
661 | break; |
662 | } |
663 | |
664 | return d->validateWithLocale(input, numMode, locale()); |
665 | } |
666 | |
667 | QValidator::State QDoubleValidatorPrivate::validateWithLocale(QString &input, QLocaleData::NumberMode numMode, const QLocale &locale) const |
668 | { |
669 | Q_Q(const QDoubleValidator); |
670 | QByteArray buff; |
671 | if (!locale.d->m_data->validateChars(input, numMode, &buff, q->dec, locale.numberOptions())) { |
672 | return QValidator::Invalid; |
673 | } |
674 | |
675 | if (buff.isEmpty()) |
676 | return QValidator::Intermediate; |
677 | |
678 | if (q->b >= 0 && buff.startsWith('-')) |
679 | return QValidator::Invalid; |
680 | |
681 | if (q->t < 0 && buff.startsWith('+')) |
682 | return QValidator::Invalid; |
683 | |
684 | bool ok = false; |
685 | double i = locale.toDouble(input, &ok); // returns 0.0 if !ok |
686 | if (i == qt_qnan()) |
687 | return QValidator::Invalid; |
688 | if (!ok) |
689 | return QValidator::Intermediate; |
690 | |
691 | if (i >= q->b && i <= q->t) |
692 | return QValidator::Acceptable; |
693 | |
694 | if (notation == QDoubleValidator::StandardNotation) { |
695 | double max = qMax(qAbs(q->b), qAbs(q->t)); |
696 | qlonglong v; |
697 | if (convertDoubleTo(max, &v)) { |
698 | qlonglong n = pow10(numDigits(v)); |
699 | // In order to get the highest possible number in the intermediate |
700 | // range we need to get 10 to the power of the number of digits |
701 | // after the decimal's and subtract that from the top number. |
702 | // |
703 | // For example, where q->dec == 2 and with a range of 0.0 - 9.0 |
704 | // then the minimum possible number is 0.00 and the maximum |
705 | // possible is 9.99. Therefore 9.999 and 10.0 should be seen as |
706 | // invalid. |
707 | if (qAbs(i) > (n - std::pow(10, -q->dec))) |
708 | return QValidator::Invalid; |
709 | } |
710 | } |
711 | |
712 | return QValidator::Intermediate; |
713 | } |
714 | |
715 | /*! |
716 | Sets the validator to accept doubles from \a minimum to \a maximum |
717 | inclusive, with at most \a decimals digits after the decimal |
718 | point. |
719 | */ |
720 | |
721 | void QDoubleValidator::setRange(double minimum, double maximum, int decimals) |
722 | { |
723 | bool rangeChanged = false; |
724 | if (b != minimum) { |
725 | b = minimum; |
726 | rangeChanged = true; |
727 | emit bottomChanged(b); |
728 | } |
729 | |
730 | if (t != maximum) { |
731 | t = maximum; |
732 | rangeChanged = true; |
733 | emit topChanged(t); |
734 | } |
735 | |
736 | if (dec != decimals) { |
737 | dec = decimals; |
738 | rangeChanged = true; |
739 | emit decimalsChanged(dec); |
740 | } |
741 | if (rangeChanged) |
742 | emit changed(); |
743 | } |
744 | |
745 | /*! |
746 | \property QDoubleValidator::bottom |
747 | \brief the validator's minimum acceptable value |
748 | |
749 | By default, this property contains a value of -infinity. |
750 | |
751 | \sa setRange() |
752 | */ |
753 | |
754 | void QDoubleValidator::setBottom(double bottom) |
755 | { |
756 | setRange(bottom, top(), decimals()); |
757 | } |
758 | |
759 | |
760 | /*! |
761 | \property QDoubleValidator::top |
762 | \brief the validator's maximum acceptable value |
763 | |
764 | By default, this property contains a value of infinity. |
765 | |
766 | \sa setRange() |
767 | */ |
768 | |
769 | void QDoubleValidator::setTop(double top) |
770 | { |
771 | setRange(bottom(), top, decimals()); |
772 | } |
773 | |
774 | /*! |
775 | \property QDoubleValidator::decimals |
776 | \brief the validator's maximum number of digits after the decimal point |
777 | |
778 | By default, this property contains a value of 1000. |
779 | |
780 | \sa setRange() |
781 | */ |
782 | |
783 | void QDoubleValidator::setDecimals(int decimals) |
784 | { |
785 | setRange(bottom(), top(), decimals); |
786 | } |
787 | |
788 | /*! |
789 | \property QDoubleValidator::notation |
790 | \since 4.3 |
791 | \brief the notation of how a string can describe a number |
792 | |
793 | By default, this property is set to ScientificNotation. |
794 | |
795 | \sa Notation |
796 | */ |
797 | |
798 | void QDoubleValidator::setNotation(Notation newNotation) |
799 | { |
800 | Q_D(QDoubleValidator); |
801 | if (d->notation != newNotation) { |
802 | d->notation = newNotation; |
803 | emit notationChanged(d->notation); |
804 | emit changed(); |
805 | } |
806 | } |
807 | |
808 | QDoubleValidator::Notation QDoubleValidator::notation() const |
809 | { |
810 | Q_D(const QDoubleValidator); |
811 | return d->notation; |
812 | } |
813 | |
814 | #if QT_CONFIG(regularexpression) |
815 | |
816 | /*! |
817 | \class QRegularExpressionValidator |
818 | \brief The QRegularExpressionValidator class is used to check a string |
819 | against a regular expression. |
820 | |
821 | \since 5.1 |
822 | |
823 | QRegularExpressionValidator uses a regular expression (regexp) to |
824 | determine whether an input string is \l Acceptable, \l |
825 | Intermediate, or \l Invalid. The regexp can either be supplied |
826 | when the QRegularExpressionValidator is constructed, or at a later time. |
827 | |
828 | If the regexp partially matches against the string, the result is |
829 | considered \l Intermediate. For example, "" and "A" are \l Intermediate for |
830 | the regexp \b{[A-Z][0-9]} (whereas "_" would be \l Invalid). |
831 | |
832 | QRegularExpressionValidator automatically wraps the regular expression in |
833 | the \c{\\A} and \c{\\z} anchors; in other words, it always attempts to do |
834 | an exact match. |
835 | |
836 | Example of use: |
837 | \snippet code/src_gui_util_qvalidator.cpp 5 |
838 | |
839 | Below we present some examples of validators. In practice they would |
840 | normally be associated with a widget as in the example above. |
841 | |
842 | \snippet code/src_gui_util_qvalidator.cpp 6 |
843 | |
844 | \sa QRegularExpression, QIntValidator, QDoubleValidator |
845 | */ |
846 | |
847 | class QRegularExpressionValidatorPrivate : public QValidatorPrivate |
848 | { |
849 | Q_DECLARE_PUBLIC(QRegularExpressionValidator) |
850 | |
851 | public: |
852 | QRegularExpression origRe; // the one set by the user |
853 | QRegularExpression usedRe; // the one actually used |
854 | void setRegularExpression(const QRegularExpression &re); |
855 | }; |
856 | |
857 | /*! |
858 | Constructs a validator with a \a parent object that accepts |
859 | any string (including an empty one) as valid. |
860 | */ |
861 | |
862 | QRegularExpressionValidator::QRegularExpressionValidator(QObject *parent) |
863 | : QValidator(*new QRegularExpressionValidatorPrivate, parent) |
864 | { |
865 | // origRe in the private will be an empty QRegularExpression, |
866 | // and therefore this validator will match any string. |
867 | } |
868 | |
869 | /*! |
870 | Constructs a validator with a \a parent object that |
871 | accepts all strings that match the regular expression \a re. |
872 | */ |
873 | |
874 | QRegularExpressionValidator::QRegularExpressionValidator(const QRegularExpression &re, QObject *parent) |
875 | : QRegularExpressionValidator(parent) |
876 | { |
877 | Q_D(QRegularExpressionValidator); |
878 | d->setRegularExpression(re); |
879 | } |
880 | |
881 | |
882 | /*! |
883 | Destroys the validator. |
884 | */ |
885 | |
886 | QRegularExpressionValidator::~QRegularExpressionValidator() |
887 | { |
888 | } |
889 | |
890 | /*! |
891 | Returns \l Acceptable if \a input is matched by the regular expression for |
892 | this validator, \l Intermediate if it has matched partially (i.e. could be |
893 | a valid match if additional valid characters are added), and \l Invalid if |
894 | \a input is not matched. |
895 | |
896 | In case the \a input is not matched, the \a pos parameter is set to |
897 | the length of the \a input parameter; otherwise, it is not modified. |
898 | |
899 | For example, if the regular expression is \b{\\w\\d\\d} (word-character, |
900 | digit, digit) then "A57" is \l Acceptable, "E5" is \l Intermediate, and |
901 | "+9" is \l Invalid. |
902 | |
903 | \sa QRegularExpression::match() |
904 | */ |
905 | |
906 | QValidator::State QRegularExpressionValidator::validate(QString &input, int &pos) const |
907 | { |
908 | Q_D(const QRegularExpressionValidator); |
909 | |
910 | // We want a validator with an empty QRegularExpression to match anything; |
911 | // since we're going to do an exact match (by using d->usedRe), first check if the rx is empty |
912 | // (and, if so, accept the input). |
913 | if (d->origRe.pattern().isEmpty()) |
914 | return Acceptable; |
915 | |
916 | const QRegularExpressionMatch m = d->usedRe.match(input, 0, QRegularExpression::PartialPreferCompleteMatch); |
917 | if (m.hasMatch()) { |
918 | return Acceptable; |
919 | } else if (input.isEmpty() || m.hasPartialMatch()) { |
920 | return Intermediate; |
921 | } else { |
922 | pos = input.size(); |
923 | return Invalid; |
924 | } |
925 | } |
926 | |
927 | /*! |
928 | \property QRegularExpressionValidator::regularExpression |
929 | \brief the regular expression used for validation |
930 | |
931 | By default, this property contains a regular expression with an empty |
932 | pattern (which therefore matches any string). |
933 | */ |
934 | |
935 | QRegularExpression QRegularExpressionValidator::regularExpression() const |
936 | { |
937 | Q_D(const QRegularExpressionValidator); |
938 | return d->origRe; |
939 | } |
940 | |
941 | void QRegularExpressionValidator::setRegularExpression(const QRegularExpression &re) |
942 | { |
943 | Q_D(QRegularExpressionValidator); |
944 | d->setRegularExpression(re); |
945 | } |
946 | |
947 | /*! |
948 | \internal |
949 | |
950 | Sets \a re as the regular expression. It wraps the regexp that's actually used |
951 | between \\A and \\z, therefore forcing an exact match. |
952 | */ |
953 | void QRegularExpressionValidatorPrivate::setRegularExpression(const QRegularExpression &re) |
954 | { |
955 | Q_Q(QRegularExpressionValidator); |
956 | |
957 | if (origRe != re) { |
958 | usedRe = origRe = re; // copies also the pattern options |
959 | usedRe.setPattern(QRegularExpression::anchoredPattern(re.pattern())); |
960 | emit q->regularExpressionChanged(re); |
961 | emit q->changed(); |
962 | } |
963 | } |
964 | |
965 | #endif // QT_CONFIG(regularexpression) |
966 | |
967 | QT_END_NAMESPACE |
968 | |
969 | #endif // QT_NO_VALIDATOR |
970 | |