KDb

KDbValidator.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2004, 2006 JarosÅ‚aw Staniek <[email protected]>
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 
22 class Q_DECL_HIDDEN KDbValidator::Private
23 {
24 public:
25  Private()
26  : acceptsEmptyValue(false) {
27  }
28  bool acceptsEmptyValue;
29 };
30 
31 //-----------------------------------------------------------
32 
33 class Q_DECL_HIDDEN KDbMultiValidator::Private
34 {
35 public:
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 
49 KDbValidator::KDbValidator(QObject * parent)
50  : QValidator(parent)
51  , d(new Private)
52 {
53 }
54 
55 KDbValidator::~KDbValidator()
56 {
57  delete d;
58 }
59 
60 KDbValidator::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) {
67  }
68  return Error;
69  }
70  return Ok;
71  }
72  return internalCheck(valueName, v, message, details);
73 }
74 
75 KDbValidator::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 
86 {
88 }
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 
120 KDbMultiValidator::~KDbMultiValidator()
121 {
122  delete d;
123 }
124 
125 void KDbMultiValidator::addSubvalidator(QValidator* validator, bool owned)
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 
152 KDbValidator::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 
bool isNull() const const
QValidator::State validate(QString &input, int &pos) const override
void addSubvalidator(QValidator *validator, bool owned=true)
virtual void fixup(QString &input) const const
virtual QValidator::State validate(QString &input, int &pos) const const=0
A validator groupping multiple QValidators.
Definition: KDbValidator.h:119
KDbMultiValidator(QObject *parent=nullptr)
QVariant::Type type() const const
bool isEmpty() const const
A validator extending QValidator with offline-checking for value's validity.
Definition: KDbValidator.h:41
void fixup(QString &input) const override
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
static const QString messageColumnNotEmpty()
A generic error/warning message "... value has to be entered.".
bool acceptsEmptyValue() const
Result check(const QString &valueName, const QVariant &v, QString *message, QString *details)
void setAcceptsEmptyValue(bool set)
QValidator::State validate(QString &input, int &pos) const override
QObject * parent() const const
QString message
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jun 25 2022 06:21:34 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.