KDb

KDbValidator.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004, 2006 Jarosław Staniek <staniek@kde.org>
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "KDbValidator.h"
21
22class Q_DECL_HIDDEN KDbValidator::Private
23{
24public:
25 Private()
26 : acceptsEmptyValue(false) {
27 }
28 bool acceptsEmptyValue;
29};
30
31//-----------------------------------------------------------
32
33class Q_DECL_HIDDEN KDbMultiValidator::Private
34{
35public:
36 Private() {
37 }
38 ~Private() {
39 qDeleteAll(ownedSubValidators);
40 ownedSubValidators.clear();
41 }
42
43 QList<QValidator*> ownedSubValidators;
44 QList<QValidator*> subValidators;
45};
46
47//-----------------------------------------------------------
48
49KDbValidator::KDbValidator(QObject * parent)
50 : QValidator(parent)
51 , d(new Private)
52{
53}
54
55KDbValidator::~KDbValidator()
56{
57 delete d;
58}
59
60KDbValidator::Result KDbValidator::check(const QString &valueName, const QVariant& v,
61 QString *message, QString *details)
62{
63 if (v.isNull() || (v.type() == QVariant::String && v.toString().isEmpty())) {
64 if (!d->acceptsEmptyValue) {
65 if (message) {
66 *message = KDbValidator::messageColumnNotEmpty().arg(valueName);
67 }
68 return Error;
69 }
70 return Ok;
71 }
72 return internalCheck(valueName, v, message, details);
73}
74
75KDbValidator::Result KDbValidator::internalCheck(const QString &valueName,
76 const QVariant& value, QString *message, QString *details)
77{
78 Q_UNUSED(valueName);
79 Q_UNUSED(value);
80 Q_UNUSED(message);
81 Q_UNUSED(details);
82 return Error;
83}
84
89
91{
92 d->acceptsEmptyValue = set;
93}
94
96{
97 return d->acceptsEmptyValue;
98}
99
101{
102 return QLatin1String(QT_TR_NOOP("\"%1\" value has to be entered."));
103}
104
105//-----------------------------------------------------------
106
108 : KDbValidator(parent)
109 , d(new Private)
110{
111}
112
114 : KDbValidator(parent)
115 , d(new Private)
116{
117 addSubvalidator(validator);
118}
119
120KDbMultiValidator::~KDbMultiValidator()
121{
122 delete d;
123}
124
126{
127 if (!validator)
128 return;
129 d->subValidators.append(validator);
130 if (owned && !validator->parent())
131 d->ownedSubValidators.append(validator);
132}
133
135{
136 State s;
137 foreach(QValidator* validator, d->subValidators) {
138 s = validator->validate(input, pos);
139 if (s == Intermediate || s == Invalid)
140 return s;
141 }
142 return Acceptable;
143}
144
146{
147 foreach(QValidator* validator, d->subValidators) {
148 validator->fixup(input);
149 }
150}
151
152KDbValidator::Result KDbMultiValidator::internalCheck(
153 const QString &valueName, const QVariant& value,
154 QString *message, QString *details)
155{
156 Result r;
157 bool warning = false;
158 foreach(QValidator* validator, d->subValidators) {
159 if (dynamic_cast<KDbValidator*>(validator))
160 r = dynamic_cast<KDbValidator*>(validator)->internalCheck(valueName, value, message, details);
161 else
162 r = Ok; //ignore
163 if (r == Error)
164 return Error;
165 else if (r == Warning)
166 warning = true;
167 }
168 return warning ? Warning : Ok;
169}
170
A validator groupping multiple QValidators.
KDbMultiValidator(QObject *parent=nullptr)
QValidator::State validate(QString &input, int &pos) const override
void fixup(QString &input) const override
void addSubvalidator(QValidator *validator, bool owned=true)
A validator extending QValidator with offline-checking for value's validity.
static const QString messageColumnNotEmpty()
A generic error/warning message "... value has to be entered.".
QValidator::State validate(QString &input, int &pos) const override
bool acceptsEmptyValue() const
Result check(const QString &valueName, const QVariant &v, QString *message, QString *details)
void setAcceptsEmptyValue(bool set)
void append(QList< T > &&value)
QObject * parent() const const
QString arg(Args &&... args) const const
bool isEmpty() const const
virtual void fixup(QString &input) const const
virtual State validate(QString &input, int &pos) const const=0
Type type() const const
bool isNull() const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:20:59 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.