Qt 4.8
qvalidator.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include <qdebug.h>
43 
44 #include "qvalidator.h"
45 #ifndef QT_NO_VALIDATOR
46 #include "private/qobject_p.h"
47 #include "private/qlocale_p.h"
48 
49 #include <limits.h>
50 #include <math.h>
51 
53 
137 public:
139  {
140  }
141 
143 };
144 
145 
152  : QObject(*new QValidatorPrivate, parent)
153 {
154 }
155 
156 #ifdef QT3_SUPPORT
157 
167  : QObject(*new QValidatorPrivate, parent)
168 {
170 }
171 #endif
172 
179 {
180 }
181 
189 {
190  Q_D(const QValidator);
191  return d->locale;
192 }
193 
203 {
204  Q_D(QValidator);
205  d->locale = locale;
206 }
207 
247 {
248 }
249 
250 
292  : QValidator(parent)
293 {
294  b = INT_MIN;
295  t = INT_MAX;
296 }
297 
298 
304 QIntValidator::QIntValidator(int minimum, int maximum,
305  QObject * parent)
306  : QValidator(parent)
307 {
308  b = minimum;
309  t = maximum;
310 }
311 
312 
313 #ifdef QT3_SUPPORT
314 
324 QIntValidator::QIntValidator(QObject * parent, const char *name)
325  : QValidator(parent)
326 {
328  b = INT_MIN;
329  t = INT_MAX;
330 }
331 
332 
343 QIntValidator::QIntValidator(int minimum, int maximum,
344  QObject * parent, const char* name)
345  : QValidator(parent)
346 {
348  b = minimum;
349  t = maximum;
350 }
351 #endif
352 
358 {
359  // nothing
360 }
361 
362 
385 static int numDigits(qlonglong n)
386 {
387  if (n == 0)
388  return 1;
389  return (int)log10(double(n)) + 1;
390 }
391 
392 static qlonglong pow10(int exp)
393 {
394  qlonglong result = 1;
395  for (int i = 0; i < exp; ++i)
396  result *= 10;
397  return result;
398 }
399 
401 {
402  QByteArray buff;
403  if (!locale().d()->validateChars(input, QLocalePrivate::IntegerMode, &buff)) {
404  QLocale cl(QLocale::C);
405  if (!cl.d()->validateChars(input, QLocalePrivate::IntegerMode, &buff))
406  return Invalid;
407  }
408 
409  if (buff.isEmpty())
410  return Intermediate;
411 
412  if (b >= 0 && buff.startsWith('-'))
413  return Invalid;
414 
415  if (t < 0 && buff.startsWith('+'))
416  return Invalid;
417 
418  if (buff.size() == 1 && (buff.at(0) == '+' || buff.at(0) == '-'))
419  return Intermediate;
420 
421  bool ok, overflow;
422  qlonglong entered = QLocalePrivate::bytearrayToLongLong(buff.constData(), 10, &ok, &overflow);
423  if (overflow || !ok)
424  return Invalid;
425 
426  if (entered >= b && entered <= t) {
427  locale().toInt(input, &ok, 10);
428  return ok ? Acceptable : Intermediate;
429  }
430 
431  if (entered >= 0) {
432  // the -entered < b condition is necessary to allow people to type
433  // the minus last (e.g. for right-to-left languages)
434  return (entered > t && -entered < b) ? Invalid : Intermediate;
435  } else {
436  return (entered < b) ? Invalid : Intermediate;
437  }
438 }
439 
441 void QIntValidator::fixup(QString &input) const
442 {
443  QByteArray buff;
444  if (!locale().d()->validateChars(input, QLocalePrivate::IntegerMode, &buff)) {
445  QLocale cl(QLocale::C);
446  if (!cl.d()->validateChars(input, QLocalePrivate::IntegerMode, &buff))
447  return;
448  }
449  bool ok, overflow;
450  qlonglong entered = QLocalePrivate::bytearrayToLongLong(buff.constData(), 10, &ok, &overflow);
451  if (ok && !overflow)
452  input = locale().toString(entered);
453 }
454 
461 {
462  b = bottom;
463  t = top;
464 }
465 
466 
480 {
481  setRange(bottom, top());
482 }
483 
497 {
498  setRange(bottom(), top);
499 }
500 
501 
502 #ifndef QT_NO_REGEXP
503 
508  : QObject(d, parent)
509 {
510 }
511 
516  : QObject(d, parent)
517 {
518 }
519 
521 {
523 public:
526  , notation(QDoubleValidator::ScientificNotation)
527  {
528  }
529 
531 
532  QValidator::State validateWithLocale(QString & input, QLocalePrivate::NumberMode numMode, const QLocale &locale) const;
533 };
534 
535 
585  : QValidator(*new QDoubleValidatorPrivate , parent)
586 {
587  b = -HUGE_VAL;
588  t = HUGE_VAL;
589  dec = 1000;
590 }
591 
592 
600  QObject * parent)
601  : QValidator(*new QDoubleValidatorPrivate , parent)
602 {
603  b = bottom;
604  t = top;
605  dec = decimals;
606 }
607 
608 #ifdef QT3_SUPPORT
609 
619 QDoubleValidator::QDoubleValidator(QObject * parent, const char *name)
620  : QValidator(*new QDoubleValidatorPrivate , parent)
621 {
623  b = -HUGE_VAL;
624  t = HUGE_VAL;
625  dec = 1000;
626 }
627 
628 
641  QObject * parent, const char* name)
642  : QValidator(*new QDoubleValidatorPrivate, parent)
643 {
645  b = bottom;
646  t = top;
647  dec = decimals;
648 }
649 #endif
650 
656 {
657 }
658 
659 
685 #ifndef LLONG_MAX
686 # define LLONG_MAX Q_INT64_C(0x7fffffffffffffff)
687 #endif
688 
690 {
691  Q_D(const QDoubleValidator);
692 
694  switch (d->notation) {
695  case StandardNotation:
697  break;
698  case ScientificNotation:
700  break;
701  }
702 
703  State currentLocaleValidation = d->validateWithLocale(input, numMode, locale());
704  if (currentLocaleValidation == Acceptable || locale().language() == QLocale::C)
705  return currentLocaleValidation;
706  State cLocaleValidation = d->validateWithLocale(input, numMode, QLocale(QLocale::C));
707  return qMax(currentLocaleValidation, cLocaleValidation);
708 }
709 
711 {
712  Q_Q(const QDoubleValidator);
713  QByteArray buff;
714  if (!locale.d()->validateChars(input, numMode, &buff, q->dec))
715  return QValidator::Invalid;
716 
717  if (buff.isEmpty())
719 
720  if (q->b >= 0 && buff.startsWith('-'))
721  return QValidator::Invalid;
722 
723  if (q->t < 0 && buff.startsWith('+'))
724  return QValidator::Invalid;
725 
726  bool ok, overflow;
727  double i = QLocalePrivate::bytearrayToDouble(buff.constData(), &ok, &overflow);
728  if (overflow)
729  return QValidator::Invalid;
730  if (!ok)
732 
733  if (i >= q->b && i <= q->t)
734  return QValidator::Acceptable;
735 
737  double max = qMax(qAbs(q->b), qAbs(q->t));
738  if (max < LLONG_MAX) {
739  qlonglong n = pow10(numDigits(qlonglong(max))) - 1;
740  if (qAbs(i) > n)
741  return QValidator::Invalid;
742  }
743  }
744 
746 }
747 
748 
755 void QDoubleValidator::setRange(double minimum, double maximum, int decimals)
756 {
757  b = minimum;
758  t = maximum;
759  dec = decimals;
760 }
761 
775 {
776  setRange(bottom, top(), decimals());
777 }
778 
779 
793 {
794  setRange(bottom(), top, decimals());
795 }
796 
810 {
811  setRange(bottom(), top(), decimals);
812 }
813 
828 {
830  d->notation = newNotation;
831 }
832 
834 {
835  Q_D(const QDoubleValidator);
836  return d->notation;
837 }
838 
880  : QValidator(parent), r(QString::fromLatin1(".*"))
881 {
882 }
883 
893  : QValidator(parent), r(rx)
894 {
895 }
896 
897 #ifdef QT3_SUPPORT
898 
908 QRegExpValidator::QRegExpValidator(QObject *parent, const char *name)
909  : QValidator(parent), r(QString::fromLatin1(".*"))
910 {
912 }
913 
928  const char *name)
929  : QValidator(parent), r(rx)
930 {
932 }
933 #endif
934 
940 {
941 }
942 
959 {
960  if (r.exactMatch(input)) {
961  return Acceptable;
962  } else {
963  if (const_cast<QRegExp &>(r).matchedLength() == input.size()) {
964  return Intermediate;
965  } else {
966  pos = input.size();
967  return Invalid;
968  }
969  }
970 }
971 
984 {
985  r = rx;
986 }
987 
988 #endif
989 
991 
992 #endif // QT_NO_VALIDATOR
double d
Definition: qnumeric_p.h:62
Notation
This enum defines the allowed notations for entering a double.
Definition: qvalidator.h:148
static qint64 bytearrayToLongLong(const char *num, int base, bool *ok, bool *overflow=0)
Definition: qlocale.cpp:3186
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static QString fromAscii(const char *, int size=-1)
Returns a QString initialized with the first size characters from the string str. ...
Definition: qstring.cpp:4276
QValidator::State validate(QString &, int &) const
Returns Acceptable if the input is an integer within the valid range, Intermediate if the input is a ...
Definition: qvalidator.cpp:400
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
void setTop(double)
Definition: qvalidator.cpp:792
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
virtual void fixup(QString &) const
This function attempts to change input to be valid according to this validator&#39;s rules.
Definition: qvalidator.cpp:246
QLocale locale() const
Returns the locale for the validator.
Definition: qvalidator.cpp:188
QValidator::State validateWithLocale(QString &input, QLocalePrivate::NumberMode numMode, const QLocale &locale) const
Definition: qvalidator.cpp:710
void setTop(int)
Definition: qvalidator.cpp:496
static double bytearrayToDouble(const char *num, bool *ok, bool *overflow=0)
Definition: qlocale.cpp:3134
The QString class provides a Unicode character string.
Definition: qstring.h:83
~QRegExpValidator()
Destroys the validator.
Definition: qvalidator.cpp:939
Q_DECL_CONSTEXPR T qAbs(const T &t)
Definition: qglobal.h:1201
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
bool startsWith(const QByteArray &a) const
Returns true if this byte array starts with byte array ba; otherwise returns false.
#define Q_D(Class)
Definition: qglobal.h:2482
void setNotation(Notation)
Definition: qvalidator.cpp:827
QString toString(qlonglong i) const
Returns a localized string representation of i.
Definition: qlocale.cpp:1295
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
virtual void setRange(int bottom, int top)
Sets the range of the validator to only accept integers between bottom and top inclusive.
Definition: qvalidator.cpp:460
void setObjectName(const QString &name)
Definition: qobject.cpp:1112
#define Q_Q(Class)
Definition: qglobal.h:2483
QValidator(QObject *parent=0)
Sets up the validator.
Definition: qvalidator.cpp:151
int decimals() const
Definition: qvalidator.h:163
QDoubleValidator::Notation notation
Definition: qvalidator.cpp:530
QLocale::Language language
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QValidator class provides validation of input text.
Definition: qvalidator.h:60
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
void setBottom(double)
Definition: qvalidator.cpp:774
static int numDigits(qlonglong n)
Definition: qvalidator.cpp:385
const char * name
virtual void setRange(double bottom, double top, int decimals=0)
Sets the validator to accept doubles from minimum to maximum inclusive, with at most decimals digits ...
Definition: qvalidator.cpp:755
~QIntValidator()
Destroys the validator.
Definition: qvalidator.cpp:357
QIntValidator(QObject *parent=0)
Constructs a validator with a parent object that accepts all integers.
Definition: qvalidator.cpp:291
QValidator::State validate(QString &, int &) const
Returns Acceptable if the string input contains a double that is within the valid range and is in the...
Definition: qvalidator.cpp:689
QDoubleValidator(QObject *parent=0)
Constructs a validator object with a parent object that accepts any double.
Definition: qvalidator.cpp:584
The State element defines configurations of objects and properties.
double top() const
Definition: qvalidator.h:162
#define LLONG_MAX
Definition: qvalidator.cpp:686
int bottom() const
Definition: qvalidator.h:114
~QValidator()
Destroys the validator, freeing any storage and other resources used.
Definition: qvalidator.cpp:178
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
void setBottom(int)
Definition: qvalidator.cpp:479
~QDoubleValidator()
Destroys the validator.
Definition: qvalidator.cpp:655
void setRegExp(const QRegExp &rx)
Definition: qvalidator.cpp:983
const QLocalePrivate * d() const
Definition: qlocale.cpp:761
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
void setDecimals(int)
Definition: qvalidator.cpp:809
int top() const
Definition: qvalidator.h:115
void fixup(QString &input) const
Reimplemented Function
Definition: qvalidator.cpp:441
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
Q_INVOKABLE QObject(QObject *parent=0)
Constructs an object with parent object parent.
Definition: qobject.cpp:753
void setLocale(const QLocale &locale)
Sets the locale that will be used for the validator.
Definition: qvalidator.cpp:202
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
virtual QValidator::State validate(QString &input, int &pos) const
Returns Acceptable if input is matched by the regular expression for this validator, Intermediate if it has matched partially (i.e.
Definition: qvalidator.cpp:958
static qlonglong pow10(int exp)
Definition: qvalidator.cpp:392
State
This enum type defines the states in which a validated string can exist.
Definition: qvalidator.h:67
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
QObject * parent
Definition: qobject.h:92
bool exactMatch(const QString &str) const
Returns true if str is matched exactly by this regular expression; otherwise returns false...
Definition: qregexp.cpp:4094
bool validateChars(const QString &str, NumberMode numMode, QByteArray *buff, int decDigits=-1) const
Definition: qlocale.cpp:2999
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
QRegExpValidator(QObject *parent=0)
Constructs a validator with a parent object that accepts any string (including an empty one) as valid...
Definition: qvalidator.cpp:879
Notation notation() const
int toInt(const QString &s, bool *ok=0, int base=0) const
Returns the int represented by the localized string s, using base base.
Definition: qlocale.cpp:1133
double bottom() const
Definition: qvalidator.h:161
char at(int i) const
Returns the character at index position i in the byte array.
Definition: qbytearray.h:413
qint64 qlonglong
Definition: qglobal.h:951
#define INT_MAX
The QDoubleValidator class provides range checking of floating-point numbers.
Definition: qvalidator.h:134