1// Copyright (C) 2016 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#ifndef QVALIDATOR_H
6#define QVALIDATOR_H
7
8#include <QtGui/qtguiglobal.h>
9#include <QtCore/qobject.h>
10#include <QtCore/qstring.h>
11#if QT_CONFIG(regularexpression)
12# include <QtCore/qregularexpression.h>
13#endif
14#include <QtCore/qlocale.h>
15
16QT_BEGIN_NAMESPACE
17
18
19#ifndef QT_NO_VALIDATOR
20
21class QValidatorPrivate;
22
23class Q_GUI_EXPORT QValidator : public QObject
24{
25 Q_OBJECT
26public:
27 explicit QValidator(QObject * parent = nullptr);
28 ~QValidator();
29
30 enum State {
31 Invalid,
32 Intermediate,
33 Acceptable
34 };
35 Q_ENUM(State)
36
37 void setLocale(const QLocale &locale);
38 QLocale locale() const;
39
40 virtual State validate(QString &, int &) const = 0;
41 virtual void fixup(QString &) const;
42
43Q_SIGNALS:
44 void changed();
45
46protected:
47 QValidator(QObjectPrivate &d, QObject *parent);
48 QValidator(QValidatorPrivate &d, QObject *parent);
49
50private:
51 Q_DISABLE_COPY(QValidator)
52 Q_DECLARE_PRIVATE(QValidator)
53};
54
55class Q_GUI_EXPORT QIntValidator : public QValidator
56{
57 Q_OBJECT
58 Q_PROPERTY(int bottom READ bottom WRITE setBottom NOTIFY bottomChanged)
59 Q_PROPERTY(int top READ top WRITE setTop NOTIFY topChanged)
60
61public:
62 explicit QIntValidator(QObject * parent = nullptr);
63 QIntValidator(int bottom, int top, QObject *parent = nullptr);
64 ~QIntValidator();
65
66 QValidator::State validate(QString &, int &) const override;
67 void fixup(QString &input) const override;
68
69 void setBottom(int);
70 void setTop(int);
71 void setRange(int bottom, int top);
72
73 int bottom() const { return b; }
74 int top() const { return t; }
75Q_SIGNALS:
76 void bottomChanged(int bottom);
77 void topChanged(int top);
78
79private:
80 Q_DISABLE_COPY(QIntValidator)
81
82 int b;
83 int t;
84};
85
86class QDoubleValidatorPrivate;
87
88class Q_GUI_EXPORT QDoubleValidator : public QValidator
89{
90 Q_OBJECT
91 Q_PROPERTY(double bottom READ bottom WRITE setBottom NOTIFY bottomChanged)
92 Q_PROPERTY(double top READ top WRITE setTop NOTIFY topChanged)
93 Q_PROPERTY(int decimals READ decimals WRITE setDecimals NOTIFY decimalsChanged)
94 Q_PROPERTY(Notation notation READ notation WRITE setNotation NOTIFY notationChanged)
95
96public:
97 explicit QDoubleValidator(QObject * parent = nullptr);
98 QDoubleValidator(double bottom, double top, int decimals, QObject *parent = nullptr);
99 ~QDoubleValidator();
100
101 enum Notation {
102 StandardNotation,
103 ScientificNotation
104 };
105 Q_ENUM(Notation)
106 QValidator::State validate(QString &, int &) const override;
107 void fixup(QString &input) const override;
108
109 void setRange(double bottom, double top, int decimals);
110 void setRange(double bottom, double top);
111 void setBottom(double);
112 void setTop(double);
113 void setDecimals(int);
114 void setNotation(Notation);
115
116 double bottom() const { return b; }
117 double top() const { return t; }
118 int decimals() const { return dec; }
119 Notation notation() const;
120
121Q_SIGNALS:
122 void bottomChanged(double bottom);
123 void topChanged(double top);
124 void decimalsChanged(int decimals);
125 void notationChanged(QDoubleValidator::Notation notation);
126
127private:
128 Q_DECLARE_PRIVATE(QDoubleValidator)
129 Q_DISABLE_COPY(QDoubleValidator)
130
131 double b;
132 double t;
133 int dec;
134};
135
136#if QT_CONFIG(regularexpression)
137
138class QRegularExpressionValidatorPrivate;
139
140class Q_GUI_EXPORT QRegularExpressionValidator : public QValidator
141{
142 Q_OBJECT
143 Q_PROPERTY(QRegularExpression regularExpression READ regularExpression WRITE setRegularExpression NOTIFY regularExpressionChanged)
144
145public:
146 explicit QRegularExpressionValidator(QObject *parent = nullptr);
147 explicit QRegularExpressionValidator(const QRegularExpression &re, QObject *parent = nullptr);
148 ~QRegularExpressionValidator();
149
150 QValidator::State validate(QString &input, int &pos) const override;
151
152 QRegularExpression regularExpression() const;
153
154public Q_SLOTS:
155 void setRegularExpression(const QRegularExpression &re);
156
157Q_SIGNALS:
158 void regularExpressionChanged(const QRegularExpression &re);
159
160private:
161 Q_DISABLE_COPY(QRegularExpressionValidator)
162 Q_DECLARE_PRIVATE(QRegularExpressionValidator)
163};
164
165#endif // QT_CONFIG(regularexpression)
166
167#endif // QT_NO_VALIDATOR
168
169QT_END_NAMESPACE
170
171#endif // QVALIDATOR_H
172

source code of qtbase/src/gui/util/qvalidator.h