| 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 | }
|
|---|
|
|---|