| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information ([email protected])
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the QtGui module of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at [email protected].
|
|---|
| 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 |
|
|---|
| 52 | QT_BEGIN_NAMESPACE
|
|---|
| 53 |
|
|---|
| 54 | /*!
|
|---|
| 55 | \class QValidator
|
|---|
| 56 | \brief The QValidator class provides validation of input text.
|
|---|
| 57 |
|
|---|
| 58 | \ingroup misc
|
|---|
| 59 | \mainclass
|
|---|
| 60 |
|
|---|
| 61 | The class itself is abstract. Two subclasses, \l QIntValidator and
|
|---|
| 62 | \l QDoubleValidator, provide basic numeric-range checking, and \l
|
|---|
| 63 | QRegExpValidator provides general checking using a custom regular
|
|---|
| 64 | expression.
|
|---|
| 65 |
|
|---|
| 66 | If the built-in validators aren't sufficient, you can subclass
|
|---|
| 67 | QValidator. The class has two virtual functions: validate() and
|
|---|
| 68 | fixup().
|
|---|
| 69 |
|
|---|
| 70 | \l validate() must be implemented by every subclass. It returns
|
|---|
| 71 | \l Invalid, \l Intermediate or \l Acceptable depending on whether
|
|---|
| 72 | its argument is valid (for the subclass's definition of valid).
|
|---|
| 73 |
|
|---|
| 74 | These three states require some explanation. An \l Invalid string
|
|---|
| 75 | is \e clearly invalid. \l Intermediate is less obvious: the
|
|---|
| 76 | concept of validity is difficult to apply when the string is
|
|---|
| 77 | incomplete (still being edited). QValidator defines \l Intermediate
|
|---|
| 78 | as the property of a string that is neither clearly invalid nor
|
|---|
| 79 | acceptable as a final result. \l Acceptable means that the string
|
|---|
| 80 | is acceptable as a final result. One might say that any string
|
|---|
| 81 | that is a plausible intermediate state during entry of an \l
|
|---|
| 82 | Acceptable string is \l Intermediate.
|
|---|
| 83 |
|
|---|
| 84 | Here are some examples:
|
|---|
| 85 |
|
|---|
| 86 | \list
|
|---|
| 87 |
|
|---|
| 88 | \i For a line edit that accepts integers from 10 to 1000 inclusive,
|
|---|
| 89 | 42 and 123 are \l Acceptable, the empty string and 5 are \l
|
|---|
| 90 | Intermediate, and "asdf" and 1114 is \l Invalid.
|
|---|
| 91 |
|
|---|
| 92 | \i For an editable combobox that accepts URLs, any well-formed URL
|
|---|
| 93 | is \l Acceptable, "http://qtsoftware.com/," is \l Intermediate
|
|---|
| 94 | (it might be a cut and paste action that accidentally took in a
|
|---|
| 95 | comma at the end), the empty string is \l Intermediate (the user
|
|---|
| 96 | might select and delete all of the text in preparation for entering
|
|---|
| 97 | a new URL) and "http:///./" is \l Invalid.
|
|---|
| 98 |
|
|---|
| 99 | \i For a spin box that accepts lengths, "11cm" and "1in" are \l
|
|---|
| 100 | Acceptable, "11" and the empty string are \l Intermediate, and
|
|---|
| 101 | "http://qtsoftware.com" and "hour" are \l Invalid.
|
|---|
| 102 |
|
|---|
| 103 | \endlist
|
|---|
| 104 |
|
|---|
| 105 | \l fixup() is provided for validators that can repair some user
|
|---|
| 106 | errors. The default implementation does nothing. QLineEdit, for
|
|---|
| 107 | example, will call fixup() if the user presses Enter (or Return)
|
|---|
| 108 | and the content is not currently valid. This allows the fixup()
|
|---|
| 109 | function the opportunity of performing some magic to make an \l
|
|---|
| 110 | Invalid string \l Acceptable.
|
|---|
| 111 |
|
|---|
| 112 | A validator has a locale, set with setLocale(). It is typically used
|
|---|
| 113 | to parse localized data. For example, QIntValidator and QDoubleValidator
|
|---|
| 114 | use it to parse localized representations of integers and doubles.
|
|---|
| 115 |
|
|---|
| 116 | QValidator is typically used with QLineEdit, QSpinBox and
|
|---|
| 117 | QComboBox.
|
|---|
| 118 |
|
|---|
| 119 | \sa QIntValidator, QDoubleValidator, QRegExpValidator, {Line Edits Example}
|
|---|
| 120 | */
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 | /*!
|
|---|
| 124 | \enum QValidator::State
|
|---|
| 125 |
|
|---|
| 126 | This enum type defines the states in which a validated string can
|
|---|
| 127 | exist.
|
|---|
| 128 |
|
|---|
| 129 | \value Invalid The string is \e clearly invalid.
|
|---|
| 130 | \value Intermediate The string is a plausible intermediate value.
|
|---|
| 131 | \value Acceptable The string is acceptable as a final result;
|
|---|
| 132 | i.e. it is valid.
|
|---|
| 133 |
|
|---|
| 134 | \omitvalue Valid
|
|---|
| 135 | */
|
|---|
| 136 |
|
|---|
| 137 | class QValidatorPrivate : public QObjectPrivate{
|
|---|
| 138 | Q_DECLARE_PUBLIC(QValidator)
|
|---|
| 139 | public:
|
|---|
| 140 | QValidatorPrivate() : QObjectPrivate()
|
|---|
| 141 | {
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | QLocale locale;
|
|---|
| 145 | };
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 | /*!
|
|---|
| 149 | Sets up the validator. The \a parent parameter is
|
|---|
| 150 | passed on to the QObject constructor.
|
|---|
| 151 | */
|
|---|
| 152 |
|
|---|
| 153 | QValidator::QValidator(QObject * parent)
|
|---|
| 154 | : QObject(*new QValidatorPrivate, parent)
|
|---|
| 155 | {
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | #ifdef QT3_SUPPORT
|
|---|
| 159 | /*!
|
|---|
| 160 | \obsolete
|
|---|
| 161 | Sets up the validator. The \a parent and \a name parameters are
|
|---|
| 162 | passed on to the QObject constructor.
|
|---|
| 163 | */
|
|---|
| 164 |
|
|---|
| 165 | QValidator::QValidator(QObject * parent, const char *name)
|
|---|
| 166 | : QObject(*new QValidatorPrivate, parent)
|
|---|
| 167 | {
|
|---|
| 168 | setObjectName(QString::fromAscii(name));
|
|---|
| 169 | }
|
|---|
| 170 | #endif
|
|---|
| 171 |
|
|---|
| 172 | /*!
|
|---|
| 173 | Destroys the validator, freeing any storage and other resources
|
|---|
| 174 | used.
|
|---|
| 175 | */
|
|---|
| 176 |
|
|---|
| 177 | QValidator::~QValidator()
|
|---|
| 178 | {
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | /*!
|
|---|
| 182 | Returns the locale for the validator. The locale is by default initialized to the same as QLocale().
|
|---|
| 183 |
|
|---|
| 184 | \sa setLocale()
|
|---|
| 185 | \sa QLocale::QLocale()
|
|---|
| 186 | */
|
|---|
| 187 | QLocale QValidator::locale() const
|
|---|
| 188 | {
|
|---|
| 189 | Q_D(const QValidator);
|
|---|
| 190 | return d->locale;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | /*!
|
|---|
| 194 | Sets the \a locale that will be used for the validator. Unless
|
|---|
| 195 | setLocale has been called, the validator will use the default
|
|---|
| 196 | locale set with QLocale::setDefault(). If a default locale has not
|
|---|
| 197 | been set, it is the operating system's locale.
|
|---|
| 198 |
|
|---|
| 199 | \sa locale() QLocale::setDefault()
|
|---|
| 200 | */
|
|---|
| 201 | void QValidator::setLocale(const QLocale &locale)
|
|---|
| 202 | {
|
|---|
| 203 | Q_D(QValidator);
|
|---|
| 204 | d->locale = locale;
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | /*!
|
|---|
| 208 | \fn QValidator::State QValidator::validate(QString &input, int &pos) const
|
|---|
| 209 |
|
|---|
| 210 | This virtual function returns \l Invalid if \a input is invalid
|
|---|
| 211 | according to this validator's rules, \l Intermediate if it
|
|---|
| 212 | is likely that a little more editing will make the input
|
|---|
| 213 | acceptable (e.g. the user types "4" into a widget which accepts
|
|---|
| 214 | integers between 10 and 99), and \l Acceptable if the input is
|
|---|
| 215 | valid.
|
|---|
| 216 |
|
|---|
| 217 | The function can change both \a input and \a pos (the cursor position)
|
|---|
| 218 | if required.
|
|---|
| 219 | */
|
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 | /*!
|
|---|
| 223 | \fn void QValidator::fixup(QString & input) const
|
|---|
| 224 |
|
|---|
| 225 | This function attempts to change \a input to be valid according to
|
|---|
| 226 | this validator's rules. It need not result in a valid string:
|
|---|
| 227 | callers of this function must re-test afterwards; the default does
|
|---|
| 228 | nothing.
|
|---|
| 229 |
|
|---|
| 230 | Reimplementations of this function can change \a input even if
|
|---|
| 231 | they do not produce a valid string. For example, an ISBN validator
|
|---|
| 232 | might want to delete every character except digits and "-", even
|
|---|
| 233 | if the result is still not a valid ISBN; a surname validator might
|
|---|
| 234 | want to remove whitespace from the start and end of the string,
|
|---|
| 235 | even if the resulting string is not in the list of accepted
|
|---|
| 236 | surnames.
|
|---|
| 237 | */
|
|---|
| 238 |
|
|---|
| 239 | void QValidator::fixup(QString &) const
|
|---|
| 240 | {
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 | /*!
|
|---|
| 245 | \class QIntValidator
|
|---|
| 246 | \brief The QIntValidator class provides a validator that ensures
|
|---|
| 247 | a string contains a valid integer within a specified range.
|
|---|
| 248 |
|
|---|
| 249 | \ingroup misc
|
|---|
| 250 |
|
|---|
| 251 | Example of use:
|
|---|
| 252 |
|
|---|
| 253 | \snippet doc/src/snippets/code/src_gui_widgets_qvalidator.cpp 0
|
|---|
| 254 |
|
|---|
| 255 | Below we present some examples of validators. In practice they would
|
|---|
| 256 | normally be associated with a widget as in the example above.
|
|---|
| 257 |
|
|---|
| 258 | \snippet doc/src/snippets/code/src_gui_widgets_qvalidator.cpp 1
|
|---|
| 259 |
|
|---|
| 260 | Notice that the value \c 999 returns Intermediate. Values
|
|---|
| 261 | consisting of a number of digits equal to or less than the max
|
|---|
| 262 | value are considered intermediate. This is intended because the
|
|---|
| 263 | digit that prevents a number to be in range is not necessarily the
|
|---|
| 264 | last digit typed. This also means that an intermediate number can
|
|---|
| 265 | have leading zeros.
|
|---|
| 266 |
|
|---|
| 267 | The minimum and maximum values are set in one call with setRange(),
|
|---|
| 268 | or individually with setBottom() and setTop().
|
|---|
| 269 |
|
|---|
| 270 | QIntValidator uses its locale() to interpret the number. For example,
|
|---|
| 271 | in Arabic locales, QIntValidator will accept Arabic digits. In addition,
|
|---|
| 272 | QIntValidator is always guaranteed to accept a number formatted according
|
|---|
| 273 | to the "C" locale.
|
|---|
| 274 |
|
|---|
| 275 | \sa QDoubleValidator, QRegExpValidator, {Line Edits Example}
|
|---|
| 276 | */
|
|---|
| 277 |
|
|---|
| 278 | /*!
|
|---|
| 279 | Constructs a validator with a \a parent object that
|
|---|
| 280 | accepts all integers.
|
|---|
| 281 | */
|
|---|
| 282 |
|
|---|
| 283 | QIntValidator::QIntValidator(QObject * parent)
|
|---|
| 284 | : QValidator(parent)
|
|---|
| 285 | {
|
|---|
| 286 | b = INT_MIN;
|
|---|
| 287 | t = INT_MAX;
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 |
|
|---|
| 291 | /*!
|
|---|
| 292 | Constructs a validator with a \a parent, that accepts integers
|
|---|
| 293 | from \a minimum to \a maximum inclusive.
|
|---|
| 294 | */
|
|---|
| 295 |
|
|---|
| 296 | QIntValidator::QIntValidator(int minimum, int maximum,
|
|---|
| 297 | QObject * parent)
|
|---|
| 298 | : QValidator(parent)
|
|---|
| 299 | {
|
|---|
| 300 | b = minimum;
|
|---|
| 301 | t = maximum;
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 | #ifdef QT3_SUPPORT
|
|---|
| 306 | /*!
|
|---|
| 307 | \obsolete
|
|---|
| 308 |
|
|---|
| 309 | Constructs a validator with a \a parent object and a \a name that
|
|---|
| 310 | accepts all integers.
|
|---|
| 311 | */
|
|---|
| 312 |
|
|---|
| 313 | QIntValidator::QIntValidator(QObject * parent, const char *name)
|
|---|
| 314 | : QValidator(parent)
|
|---|
| 315 | {
|
|---|
| 316 | setObjectName(QString::fromAscii(name));
|
|---|
| 317 | b = INT_MIN;
|
|---|
| 318 | t = INT_MAX;
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 |
|
|---|
| 322 | /*!
|
|---|
| 323 | \obsolete
|
|---|
| 324 |
|
|---|
| 325 | Constructs a validator called \a name with a \a parent, that
|
|---|
| 326 | accepts integers from \a minimum to \a maximum inclusive.
|
|---|
| 327 | */
|
|---|
| 328 |
|
|---|
| 329 | QIntValidator::QIntValidator(int minimum, int maximum,
|
|---|
| 330 | QObject * parent, const char* name)
|
|---|
| 331 | : QValidator(parent)
|
|---|
| 332 | {
|
|---|
| 333 | setObjectName(QString::fromAscii(name));
|
|---|
| 334 | b = minimum;
|
|---|
| 335 | t = maximum;
|
|---|
| 336 | }
|
|---|
| 337 | #endif
|
|---|
| 338 |
|
|---|
| 339 | /*!
|
|---|
| 340 | Destroys the validator.
|
|---|
| 341 | */
|
|---|
| 342 |
|
|---|
| 343 | QIntValidator::~QIntValidator()
|
|---|
| 344 | {
|
|---|
| 345 | // nothing
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 |
|
|---|
| 349 | /*!
|
|---|
| 350 | \fn QValidator::State QIntValidator::validate(QString &input, int &pos) const
|
|---|
| 351 |
|
|---|
| 352 | Returns \l Acceptable if the \a input is an integer within the
|
|---|
| 353 | valid range, \l Intermediate if the \a input is a prefix of an integer in the
|
|---|
| 354 | valid range, and \l Invalid otherwise.
|
|---|
| 355 |
|
|---|
| 356 | If the valid range consists of just positive integers (e.g., 32 to 100)
|
|---|
| 357 | and \a input is a negative integer, then Invalid is returned. (On the other
|
|---|
| 358 | hand, if the range consists of negative integers (e.g., -100 to -32) and
|
|---|
| 359 | \a input is a positive integer, then Intermediate is returned, because
|
|---|
| 360 | the user might be just about to type the minus (especially for right-to-left
|
|---|
| 361 | languages).
|
|---|
| 362 |
|
|---|
| 363 | \snippet doc/src/snippets/code/src_gui_widgets_qvalidator.cpp 2
|
|---|
| 364 |
|
|---|
| 365 | By default, the \a pos parameter is not used by this validator.
|
|---|
| 366 | */
|
|---|
| 367 |
|
|---|
| 368 | static int numDigits(qlonglong n)
|
|---|
| 369 | {
|
|---|
| 370 | if (n == 0)
|
|---|
| 371 | return 1;
|
|---|
| 372 | return (int)log10(double(n)) + 1;
|
|---|
| 373 | };
|
|---|
| 374 |
|
|---|
| 375 | static qlonglong pow10(int exp)
|
|---|
| 376 | {
|
|---|
| 377 | qlonglong result = 1;
|
|---|
| 378 | for (int i = 0; i < exp; ++i)
|
|---|
| 379 | result *= 10;
|
|---|
| 380 | return result;
|
|---|
| 381 | }
|
|---|
| 382 |
|
|---|
| 383 | QValidator::State QIntValidator::validate(QString & input, int&) const
|
|---|
| 384 | {
|
|---|
| 385 | QByteArray buff;
|
|---|
| 386 | if (!locale().d()->validateChars(input, QLocalePrivate::IntegerMode, &buff)) {
|
|---|
| 387 | QLocale cl(QLocale::C);
|
|---|
| 388 | if (!cl.d()->validateChars(input, QLocalePrivate::IntegerMode, &buff))
|
|---|
| 389 | return Invalid;
|
|---|
| 390 | }
|
|---|
| 391 |
|
|---|
| 392 | if (buff.isEmpty())
|
|---|
| 393 | return Intermediate;
|
|---|
| 394 |
|
|---|
| 395 | if (b >= 0 && buff.startsWith('-'))
|
|---|
| 396 | return Invalid;
|
|---|
| 397 |
|
|---|
| 398 | if (t < 0 && buff.startsWith('+'))
|
|---|
| 399 | return Invalid;
|
|---|
| 400 |
|
|---|
| 401 | if (buff.size() == 1 && (buff.at(0) == '+' || buff.at(0) == '-'))
|
|---|
| 402 | return Intermediate;
|
|---|
| 403 |
|
|---|
| 404 | bool ok, overflow;
|
|---|
| 405 | qlonglong entered = QLocalePrivate::bytearrayToLongLong(buff.constData(), 10, &ok, &overflow);
|
|---|
| 406 | if (overflow || !ok)
|
|---|
| 407 | return Invalid;
|
|---|
| 408 | if (entered >= b && entered <= t)
|
|---|
| 409 | return Acceptable;
|
|---|
| 410 |
|
|---|
| 411 | if (entered >= 0) {
|
|---|
| 412 | // the -entered < b condition is necessary to allow people to type
|
|---|
| 413 | // the minus last (e.g. for right-to-left languages)
|
|---|
| 414 | return (entered > t && -entered < b) ? Invalid : Intermediate;
|
|---|
| 415 | } else {
|
|---|
| 416 | return (entered < b) ? Invalid : Intermediate;
|
|---|
| 417 | }
|
|---|
| 418 | }
|
|---|
| 419 |
|
|---|
| 420 |
|
|---|
| 421 | /*!
|
|---|
| 422 | Sets the range of the validator to only accept integers between \a
|
|---|
| 423 | bottom and \a top inclusive.
|
|---|
| 424 | */
|
|---|
| 425 |
|
|---|
| 426 | void QIntValidator::setRange(int bottom, int top)
|
|---|
| 427 | {
|
|---|
| 428 | b = bottom;
|
|---|
| 429 | t = top;
|
|---|
| 430 | }
|
|---|
| 431 |
|
|---|
| 432 |
|
|---|
| 433 | /*!
|
|---|
| 434 | \property QIntValidator::bottom
|
|---|
| 435 | \brief the validator's lowest acceptable value
|
|---|
| 436 |
|
|---|
| 437 | By default, this property's value is derived from the lowest signed
|
|---|
| 438 | integer available (typically -2147483647).
|
|---|
| 439 |
|
|---|
| 440 | \sa setRange()
|
|---|
| 441 | */
|
|---|
| 442 | void QIntValidator::setBottom(int bottom)
|
|---|
| 443 | {
|
|---|
| 444 | setRange(bottom, top());
|
|---|
| 445 | }
|
|---|
| 446 |
|
|---|
| 447 | /*!
|
|---|
| 448 | \property QIntValidator::top
|
|---|
| 449 | \brief the validator's highest acceptable value
|
|---|
| 450 |
|
|---|
| 451 | By default, this property's value is derived from the highest signed
|
|---|
| 452 | integer available (typically 2147483647).
|
|---|
| 453 |
|
|---|
| 454 | \sa setRange()
|
|---|
| 455 | */
|
|---|
| 456 | void QIntValidator::setTop(int top)
|
|---|
| 457 | {
|
|---|
| 458 | setRange(bottom(), top);
|
|---|
| 459 | }
|
|---|
| 460 |
|
|---|
| 461 |
|
|---|
| 462 | #ifndef QT_NO_REGEXP
|
|---|
| 463 |
|
|---|
| 464 | /*!
|
|---|
| 465 | \internal
|
|---|
| 466 | */
|
|---|
| 467 | QValidator::QValidator(QObjectPrivate &d, QObject *parent)
|
|---|
| 468 | : QObject(d, parent)
|
|---|
| 469 | {
|
|---|
| 470 | }
|
|---|
| 471 |
|
|---|
| 472 | /*!
|
|---|
| 473 | \internal
|
|---|
| 474 | */
|
|---|
| 475 | QValidator::QValidator(QValidatorPrivate &d, QObject *parent)
|
|---|
| 476 | : QObject(d, parent)
|
|---|
| 477 | {
|
|---|
| 478 | }
|
|---|
| 479 |
|
|---|
| 480 | class QDoubleValidatorPrivate : public QValidatorPrivate
|
|---|
| 481 | {
|
|---|
| 482 | Q_DECLARE_PUBLIC(QDoubleValidator)
|
|---|
| 483 | public:
|
|---|
| 484 | QDoubleValidatorPrivate()
|
|---|
| 485 | : QValidatorPrivate()
|
|---|
| 486 | , notation(QDoubleValidator::ScientificNotation)
|
|---|
| 487 | {
|
|---|
| 488 | }
|
|---|
| 489 |
|
|---|
| 490 | QDoubleValidator::Notation notation;
|
|---|
| 491 | };
|
|---|
| 492 |
|
|---|
| 493 |
|
|---|
| 494 | /*!
|
|---|
| 495 | \class QDoubleValidator
|
|---|
| 496 |
|
|---|
| 497 | \brief The QDoubleValidator class provides range checking of
|
|---|
| 498 | floating-point numbers.
|
|---|
| 499 |
|
|---|
| 500 | \ingroup misc
|
|---|
| 501 |
|
|---|
| 502 | QDoubleValidator provides an upper bound, a lower bound, and a
|
|---|
| 503 | limit on the number of digits after the decimal point. It does not
|
|---|
| 504 | provide a fixup() function.
|
|---|
| 505 |
|
|---|
| 506 | You can set the acceptable range in one call with setRange(), or
|
|---|
| 507 | with setBottom() and setTop(). Set the number of decimal places
|
|---|
| 508 | with setDecimals(). The validate() function returns the validation
|
|---|
| 509 | state.
|
|---|
| 510 |
|
|---|
| 511 | QDoubleValidator uses its locale() to interpret the number. For example,
|
|---|
| 512 | in the German locale, "1,234" will be accepted as the fractional number
|
|---|
| 513 | 1.234. In Arabic locales, QDoubleValidator will accept Arabic digits.
|
|---|
| 514 |
|
|---|
| 515 | In addition, QDoubleValidator is always guaranteed to accept a number
|
|---|
| 516 | formatted according to the "C" locale. QDoubleValidator will not accept
|
|---|
| 517 | numbers with thousand-seperators.
|
|---|
| 518 |
|
|---|
| 519 | \sa QIntValidator, QRegExpValidator, {Line Edits Example}
|
|---|
| 520 | */
|
|---|
| 521 |
|
|---|
| 522 | /*!
|
|---|
| 523 | \enum QDoubleValidator::Notation
|
|---|
| 524 | \since 4.3
|
|---|
| 525 | This enum defines the allowed notations for entering a double.
|
|---|
| 526 |
|
|---|
| 527 | \value StandardNotation The string is written as a standard number
|
|---|
| 528 | (i.e. 0.015).
|
|---|
| 529 | \value ScientificNotation The string is written in scientific
|
|---|
| 530 | form. It may have an exponent part(i.e. 1.5E-2).
|
|---|
| 531 | */
|
|---|
| 532 |
|
|---|
| 533 | /*!
|
|---|
| 534 | Constructs a validator object with a \a parent object
|
|---|
| 535 | that accepts any double.
|
|---|
| 536 | */
|
|---|
| 537 |
|
|---|
| 538 | QDoubleValidator::QDoubleValidator(QObject * parent)
|
|---|
| 539 | : QValidator(*new QDoubleValidatorPrivate , parent)
|
|---|
| 540 | {
|
|---|
| 541 | b = -HUGE_VAL;
|
|---|
| 542 | t = HUGE_VAL;
|
|---|
| 543 | dec = 1000;
|
|---|
| 544 | }
|
|---|
| 545 |
|
|---|
| 546 |
|
|---|
| 547 | /*!
|
|---|
| 548 | Constructs a validator object with a \a parent object. This
|
|---|
| 549 | validator will accept doubles from \a bottom to \a top inclusive,
|
|---|
| 550 | with up to \a decimals digits after the decimal point.
|
|---|
| 551 | */
|
|---|
| 552 |
|
|---|
| 553 | QDoubleValidator::QDoubleValidator(double bottom, double top, int decimals,
|
|---|
| 554 | QObject * parent)
|
|---|
| 555 | : QValidator(*new QDoubleValidatorPrivate , parent)
|
|---|
| 556 | {
|
|---|
| 557 | b = bottom;
|
|---|
| 558 | t = top;
|
|---|
| 559 | dec = decimals;
|
|---|
| 560 | }
|
|---|
| 561 |
|
|---|
| 562 | #ifdef QT3_SUPPORT
|
|---|
| 563 | /*!
|
|---|
| 564 | \obsolete
|
|---|
| 565 |
|
|---|
| 566 | Constructs a validator object with a \a parent object and a \a name
|
|---|
| 567 | that accepts any double.
|
|---|
| 568 | */
|
|---|
| 569 |
|
|---|
| 570 | QDoubleValidator::QDoubleValidator(QObject * parent, const char *name)
|
|---|
| 571 | : QValidator(*new QDoubleValidatorPrivate , parent)
|
|---|
| 572 | {
|
|---|
| 573 | setObjectName(QString::fromAscii(name));
|
|---|
| 574 | b = -HUGE_VAL;
|
|---|
| 575 | t = HUGE_VAL;
|
|---|
| 576 | dec = 1000;
|
|---|
| 577 | }
|
|---|
| 578 |
|
|---|
| 579 |
|
|---|
| 580 | /*!
|
|---|
| 581 | \obsolete
|
|---|
| 582 |
|
|---|
| 583 | Constructs a validator object with a \a parent object, called \a
|
|---|
| 584 | name. This validator will accept doubles from \a bottom to \a top
|
|---|
| 585 | inclusive, with up to \a decimals digits after the decimal point.
|
|---|
| 586 | */
|
|---|
| 587 |
|
|---|
| 588 | QDoubleValidator::QDoubleValidator(double bottom, double top, int decimals,
|
|---|
| 589 | QObject * parent, const char* name)
|
|---|
| 590 | : QValidator(*new QDoubleValidatorPrivate, parent)
|
|---|
| 591 | {
|
|---|
| 592 | setObjectName(QString::fromAscii(name));
|
|---|
| 593 | b = bottom;
|
|---|
| 594 | t = top;
|
|---|
| 595 | dec = decimals;
|
|---|
| 596 | }
|
|---|
| 597 | #endif
|
|---|
| 598 |
|
|---|
| 599 | /*!
|
|---|
| 600 | Destroys the validator.
|
|---|
| 601 | */
|
|---|
| 602 |
|
|---|
| 603 | QDoubleValidator::~QDoubleValidator()
|
|---|
| 604 | {
|
|---|
| 605 | }
|
|---|
| 606 |
|
|---|
| 607 |
|
|---|
| 608 | /*!
|
|---|
| 609 | \fn QValidator::State QDoubleValidator::validate(QString &input, int &pos) const
|
|---|
| 610 |
|
|---|
| 611 | Returns \l Acceptable if the string \a input contains a double
|
|---|
| 612 | that is within the valid range and is in the correct format.
|
|---|
| 613 |
|
|---|
| 614 | Returns \l Intermediate if \a input contains a double that is
|
|---|
| 615 | outside the range or is in the wrong format; e.g. with too many
|
|---|
| 616 | digits after the decimal point or is empty.
|
|---|
| 617 |
|
|---|
| 618 | Returns \l Invalid if the \a input is not a double.
|
|---|
| 619 |
|
|---|
| 620 | Note: If the valid range consists of just positive doubles (e.g. 0.0 to 100.0)
|
|---|
| 621 | and \a input is a negative double then \l Invalid is returned. If notation()
|
|---|
| 622 | is set to StandardNotation, and the input contains more digits before the
|
|---|
| 623 | decimal point than a double in the valid range may have, \l Invalid is returned.
|
|---|
| 624 | If notation() is ScientificNotation, and the input is not in the valid range,
|
|---|
| 625 | \l Intermediate is returned. The value may yet become valid by changing the exponent.
|
|---|
| 626 |
|
|---|
| 627 | By default, the \a pos parameter is not used by this validator.
|
|---|
| 628 | */
|
|---|
| 629 |
|
|---|
| 630 | #ifndef LLONG_MAX
|
|---|
| 631 | # define LLONG_MAX Q_INT64_C(0x7fffffffffffffff)
|
|---|
| 632 | #endif
|
|---|
| 633 |
|
|---|
| 634 | QValidator::State QDoubleValidator::validate(QString & input, int &) const
|
|---|
| 635 | {
|
|---|
| 636 | Q_D(const QDoubleValidator);
|
|---|
| 637 |
|
|---|
| 638 | QLocalePrivate::NumberMode numMode = QLocalePrivate::DoubleStandardMode;
|
|---|
| 639 | switch (d->notation) {
|
|---|
| 640 | case StandardNotation:
|
|---|
| 641 | numMode = QLocalePrivate::DoubleStandardMode;
|
|---|
| 642 | break;
|
|---|
| 643 | case ScientificNotation:
|
|---|
| 644 | numMode = QLocalePrivate::DoubleScientificMode;
|
|---|
| 645 | break;
|
|---|
| 646 | };
|
|---|
| 647 |
|
|---|
| 648 | QByteArray buff;
|
|---|
| 649 | if (!locale().d()->validateChars(input, numMode, &buff, dec)) {
|
|---|
| 650 | QLocale cl(QLocale::C);
|
|---|
| 651 | if (!cl.d()->validateChars(input, numMode, &buff, dec))
|
|---|
| 652 | return Invalid;
|
|---|
| 653 | }
|
|---|
| 654 |
|
|---|
| 655 | if (buff.isEmpty())
|
|---|
| 656 | return Intermediate;
|
|---|
| 657 |
|
|---|
| 658 | if (b >= 0 && buff.startsWith('-'))
|
|---|
| 659 | return Invalid;
|
|---|
| 660 |
|
|---|
| 661 | if (t < 0 && buff.startsWith('+'))
|
|---|
| 662 | return Invalid;
|
|---|
| 663 |
|
|---|
| 664 | bool ok, overflow;
|
|---|
| 665 | double i = QLocalePrivate::bytearrayToDouble(buff.constData(), &ok, &overflow);
|
|---|
| 666 | if (overflow)
|
|---|
| 667 | return Invalid;
|
|---|
| 668 | if (!ok)
|
|---|
| 669 | return Intermediate;
|
|---|
| 670 |
|
|---|
| 671 | if (i >= b && i <= t)
|
|---|
| 672 | return Acceptable;
|
|---|
| 673 |
|
|---|
| 674 | if (d->notation == StandardNotation) {
|
|---|
| 675 | double max = qMax(qAbs(b), qAbs(t));
|
|---|
| 676 | if (max < LLONG_MAX) {
|
|---|
| 677 | qlonglong n = pow10(numDigits(qlonglong(max))) - 1;
|
|---|
| 678 | if (qAbs(i) > n)
|
|---|
| 679 | return Invalid;
|
|---|
| 680 | }
|
|---|
| 681 | }
|
|---|
| 682 |
|
|---|
| 683 | return Intermediate;
|
|---|
| 684 | }
|
|---|
| 685 |
|
|---|
| 686 |
|
|---|
| 687 | /*!
|
|---|
| 688 | Sets the validator to accept doubles from \a minimum to \a maximum
|
|---|
| 689 | inclusive, with at most \a decimals digits after the decimal
|
|---|
| 690 | point.
|
|---|
| 691 | */
|
|---|
| 692 |
|
|---|
| 693 | void QDoubleValidator::setRange(double minimum, double maximum, int decimals)
|
|---|
| 694 | {
|
|---|
| 695 | b = minimum;
|
|---|
| 696 | t = maximum;
|
|---|
| 697 | dec = decimals;
|
|---|
| 698 | }
|
|---|
| 699 |
|
|---|
| 700 | /*!
|
|---|
| 701 | \property QDoubleValidator::bottom
|
|---|
| 702 | \brief the validator's minimum acceptable value
|
|---|
| 703 |
|
|---|
| 704 | By default, this property contains a value of -infinity.
|
|---|
| 705 |
|
|---|
| 706 | \sa setRange()
|
|---|
| 707 | */
|
|---|
| 708 |
|
|---|
| 709 | void QDoubleValidator::setBottom(double bottom)
|
|---|
| 710 | {
|
|---|
| 711 | setRange(bottom, top(), decimals());
|
|---|
| 712 | }
|
|---|
| 713 |
|
|---|
| 714 |
|
|---|
| 715 | /*!
|
|---|
| 716 | \property QDoubleValidator::top
|
|---|
| 717 | \brief the validator's maximum acceptable value
|
|---|
| 718 |
|
|---|
| 719 | By default, this property contains a value of infinity.
|
|---|
| 720 |
|
|---|
| 721 | \sa setRange()
|
|---|
| 722 | */
|
|---|
| 723 |
|
|---|
| 724 | void QDoubleValidator::setTop(double top)
|
|---|
| 725 | {
|
|---|
| 726 | setRange(bottom(), top, decimals());
|
|---|
| 727 | }
|
|---|
| 728 |
|
|---|
| 729 | /*!
|
|---|
| 730 | \property QDoubleValidator::decimals
|
|---|
| 731 | \brief the validator's maximum number of digits after the decimal point
|
|---|
| 732 |
|
|---|
| 733 | By default, this property contains a value of 1000.
|
|---|
| 734 |
|
|---|
| 735 | \sa setRange()
|
|---|
| 736 | */
|
|---|
| 737 |
|
|---|
| 738 | void QDoubleValidator::setDecimals(int decimals)
|
|---|
| 739 | {
|
|---|
| 740 | setRange(bottom(), top(), decimals);
|
|---|
| 741 | }
|
|---|
| 742 |
|
|---|
| 743 | /*!
|
|---|
| 744 | \property QDoubleValidator::notation
|
|---|
| 745 | \since 4.3
|
|---|
| 746 | \brief the notation of how a string can describe a number
|
|---|
| 747 |
|
|---|
| 748 | By default, this property is set to ScientificNotation.
|
|---|
| 749 |
|
|---|
| 750 | \sa Notation
|
|---|
| 751 | */
|
|---|
| 752 |
|
|---|
| 753 | void QDoubleValidator::setNotation(Notation newNotation)
|
|---|
| 754 | {
|
|---|
| 755 | Q_D(QDoubleValidator);
|
|---|
| 756 | d->notation = newNotation;
|
|---|
| 757 | }
|
|---|
| 758 |
|
|---|
| 759 | QDoubleValidator::Notation QDoubleValidator::notation() const
|
|---|
| 760 | {
|
|---|
| 761 | Q_D(const QDoubleValidator);
|
|---|
| 762 | return d->notation;
|
|---|
| 763 | }
|
|---|
| 764 |
|
|---|
| 765 | /*!
|
|---|
| 766 | \class QRegExpValidator
|
|---|
| 767 | \brief The QRegExpValidator class is used to check a string
|
|---|
| 768 | against a regular expression.
|
|---|
| 769 |
|
|---|
| 770 | \ingroup misc
|
|---|
| 771 |
|
|---|
| 772 | QRegExpValidator uses a regular expression (regexp) to
|
|---|
| 773 | determine whether an input string is \l Acceptable, \l
|
|---|
| 774 | Intermediate, or \l Invalid. The regexp can either be supplied
|
|---|
| 775 | when the QRegExpValidator is constructed, or at a later time.
|
|---|
| 776 |
|
|---|
| 777 | When QRegExpValidator determines whether a string is \l Acceptable
|
|---|
| 778 | or not, the regexp is treated as if it begins with the start of string
|
|---|
| 779 | assertion (\bold{^}) and ends with the end of string assertion
|
|---|
| 780 | (\bold{$}); the match is against the entire input string, or from
|
|---|
| 781 | the given position if a start position greater than zero is given.
|
|---|
| 782 |
|
|---|
| 783 | If a string is a prefix of an \l Acceptable string, it is considered
|
|---|
| 784 | \l Intermediate. For example, "" and "A" are \l Intermediate for the
|
|---|
| 785 | regexp \bold{[A-Z][0-9]} (whereas "_" would be \l Invalid).
|
|---|
| 786 |
|
|---|
| 787 | For a brief introduction to Qt's regexp engine, see \l QRegExp.
|
|---|
| 788 |
|
|---|
| 789 | Example of use:
|
|---|
| 790 | \snippet doc/src/snippets/code/src_gui_widgets_qvalidator.cpp 3
|
|---|
| 791 |
|
|---|
| 792 | Below we present some examples of validators. In practice they would
|
|---|
| 793 | normally be associated with a widget as in the example above.
|
|---|
| 794 |
|
|---|
| 795 | \snippet doc/src/snippets/code/src_gui_widgets_qvalidator.cpp 4
|
|---|
| 796 |
|
|---|
| 797 | \sa QRegExp, QIntValidator, QDoubleValidator, {Settings Editor Example}
|
|---|
| 798 | */
|
|---|
| 799 |
|
|---|
| 800 | /*!
|
|---|
| 801 | Constructs a validator with a \a parent object that accepts
|
|---|
| 802 | any string (including an empty one) as valid.
|
|---|
| 803 | */
|
|---|
| 804 |
|
|---|
| 805 | QRegExpValidator::QRegExpValidator(QObject *parent)
|
|---|
| 806 | : QValidator(parent), r(QString::fromLatin1(".*"))
|
|---|
| 807 | {
|
|---|
| 808 | }
|
|---|
| 809 |
|
|---|
| 810 | /*!
|
|---|
| 811 | Constructs a validator with a \a parent object that
|
|---|
| 812 | accepts all strings that match the regular expression \a rx.
|
|---|
| 813 |
|
|---|
| 814 | The match is made against the entire string; e.g. if the regexp is
|
|---|
| 815 | \bold{[A-Fa-f0-9]+} it will be treated as \bold{^[A-Fa-f0-9]+$}.
|
|---|
| 816 | */
|
|---|
| 817 |
|
|---|
| 818 | QRegExpValidator::QRegExpValidator(const QRegExp& rx, QObject *parent)
|
|---|
| 819 | : QValidator(parent), r(rx)
|
|---|
| 820 | {
|
|---|
| 821 | }
|
|---|
| 822 |
|
|---|
| 823 | #ifdef QT3_SUPPORT
|
|---|
| 824 | /*!
|
|---|
| 825 | \obsolete
|
|---|
| 826 |
|
|---|
| 827 | Constructs a validator with a \a parent object and \a name that accepts
|
|---|
| 828 | any string (including an empty one) as valid.
|
|---|
| 829 | */
|
|---|
| 830 |
|
|---|
| 831 | QRegExpValidator::QRegExpValidator(QObject *parent, const char *name)
|
|---|
| 832 | : QValidator(parent), r(QString::fromLatin1(".*"))
|
|---|
| 833 | {
|
|---|
| 834 | setObjectName(QString::fromAscii(name));
|
|---|
| 835 | }
|
|---|
| 836 |
|
|---|
| 837 | /*!
|
|---|
| 838 | \obsolete
|
|---|
| 839 |
|
|---|
| 840 | Constructs a validator with a \a parent object and a \a name that
|
|---|
| 841 | accepts all strings that match the regular expression \a rx.
|
|---|
| 842 |
|
|---|
| 843 | The match is made against the entire string; e.g. if the regexp is
|
|---|
| 844 | \bold{[A-Fa-f0-9]+} it will be treated as \bold{^[A-Fa-f0-9]+$}.
|
|---|
| 845 | */
|
|---|
| 846 |
|
|---|
| 847 | QRegExpValidator::QRegExpValidator(const QRegExp& rx, QObject *parent,
|
|---|
| 848 | const char *name)
|
|---|
| 849 | : QValidator(parent), r(rx)
|
|---|
| 850 | {
|
|---|
| 851 | setObjectName(QString::fromAscii(name));
|
|---|
| 852 | }
|
|---|
| 853 | #endif
|
|---|
| 854 |
|
|---|
| 855 | /*!
|
|---|
| 856 | Destroys the validator.
|
|---|
| 857 | */
|
|---|
| 858 |
|
|---|
| 859 | QRegExpValidator::~QRegExpValidator()
|
|---|
| 860 | {
|
|---|
| 861 | }
|
|---|
| 862 |
|
|---|
| 863 | /*!
|
|---|
| 864 | Returns \l Acceptable if \a input is matched by the regular
|
|---|
| 865 | expression for this validator, \l Intermediate if it has matched
|
|---|
| 866 | partially (i.e. could be a valid match if additional valid
|
|---|
| 867 | characters are added), and \l Invalid if \a input is not matched.
|
|---|
| 868 |
|
|---|
| 869 | The \a pos parameter is set to the length of the \a input parameter.
|
|---|
| 870 |
|
|---|
| 871 | For example, if the regular expression is \bold{\\w\\d\\d}
|
|---|
| 872 | (word-character, digit, digit) then "A57" is \l Acceptable,
|
|---|
| 873 | "E5" is \l Intermediate, and "+9" is \l Invalid.
|
|---|
| 874 |
|
|---|
| 875 | \sa QRegExp::exactMatch()
|
|---|
| 876 | */
|
|---|
| 877 |
|
|---|
| 878 | QValidator::State QRegExpValidator::validate(QString &input, int& pos) const
|
|---|
| 879 | {
|
|---|
| 880 | if (r.exactMatch(input)) {
|
|---|
| 881 | return Acceptable;
|
|---|
| 882 | } else {
|
|---|
| 883 | if (const_cast<QRegExp &>(r).matchedLength() == input.size()) {
|
|---|
| 884 | return Intermediate;
|
|---|
| 885 | } else {
|
|---|
| 886 | pos = input.size();
|
|---|
| 887 | return Invalid;
|
|---|
| 888 | }
|
|---|
| 889 | }
|
|---|
| 890 | }
|
|---|
| 891 |
|
|---|
| 892 | /*!
|
|---|
| 893 | \property QRegExpValidator::regExp
|
|---|
| 894 | \brief the regular expression used for validation
|
|---|
| 895 |
|
|---|
| 896 | By default, this property contains a regular expression with the pattern \c{.*}
|
|---|
| 897 | that matches any string.
|
|---|
| 898 | */
|
|---|
| 899 |
|
|---|
| 900 | void QRegExpValidator::setRegExp(const QRegExp& rx)
|
|---|
| 901 | {
|
|---|
| 902 | r = rx;
|
|---|
| 903 | }
|
|---|
| 904 |
|
|---|
| 905 | #endif
|
|---|
| 906 |
|
|---|
| 907 | QT_END_NAMESPACE
|
|---|
| 908 |
|
|---|
| 909 | #endif // QT_NO_VALIDATOR
|
|---|