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