File indexing completed on 2024-05-19 04:42:56

0001 /* This file is part of the KDE project
0002    Copyright (C) 2004, 2006 Jarosław Staniek <staniek@kde.org>
0003 
0004    This program is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This program is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this program; see the file COPYING.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "KDbValidator.h"
0021 
0022 class Q_DECL_HIDDEN KDbValidator::Private
0023 {
0024 public:
0025     Private()
0026             : acceptsEmptyValue(false) {
0027     }
0028     bool acceptsEmptyValue;
0029 };
0030 
0031 //-----------------------------------------------------------
0032 
0033 class Q_DECL_HIDDEN KDbMultiValidator::Private
0034 {
0035 public:
0036     Private() {
0037     }
0038     ~Private() {
0039         qDeleteAll(ownedSubValidators);
0040         ownedSubValidators.clear();
0041     }
0042 
0043     QList<QValidator*> ownedSubValidators;
0044     QList<QValidator*> subValidators;
0045 };
0046 
0047 //-----------------------------------------------------------
0048 
0049 KDbValidator::KDbValidator(QObject * parent)
0050         : QValidator(parent)
0051         , d(new Private)
0052 {
0053 }
0054 
0055 KDbValidator::~KDbValidator()
0056 {
0057     delete d;
0058 }
0059 
0060 KDbValidator::Result KDbValidator::check(const QString &valueName, const QVariant& v,
0061                                    QString *message, QString *details)
0062 {
0063     if (v.isNull() || (v.type() == QVariant::String && v.toString().isEmpty())) {
0064         if (!d->acceptsEmptyValue) {
0065             if (message) {
0066                 *message = KDbValidator::messageColumnNotEmpty().arg(valueName);
0067             }
0068             return Error;
0069         }
0070         return Ok;
0071     }
0072     return internalCheck(valueName, v, message, details);
0073 }
0074 
0075 KDbValidator::Result KDbValidator::internalCheck(const QString &valueName,
0076         const QVariant& value, QString *message, QString *details)
0077 {
0078     Q_UNUSED(valueName);
0079     Q_UNUSED(value);
0080     Q_UNUSED(message);
0081     Q_UNUSED(details);
0082     return Error;
0083 }
0084 
0085 QValidator::State KDbValidator::validate(QString & , int &) const
0086 {
0087     return QValidator::Acceptable;
0088 }
0089 
0090 void KDbValidator::setAcceptsEmptyValue(bool set)
0091 {
0092     d->acceptsEmptyValue = set;
0093 }
0094 
0095 bool KDbValidator::acceptsEmptyValue() const
0096 {
0097     return d->acceptsEmptyValue;
0098 }
0099 
0100 const QString KDbValidator::messageColumnNotEmpty()
0101 {
0102     return QLatin1String(QT_TR_NOOP("\"%1\" value has to be entered."));
0103 }
0104 
0105 //-----------------------------------------------------------
0106 
0107 KDbMultiValidator::KDbMultiValidator(QObject* parent)
0108         : KDbValidator(parent)
0109         , d(new Private)
0110 {
0111 }
0112 
0113 KDbMultiValidator::KDbMultiValidator(QValidator *validator, QObject * parent)
0114         : KDbValidator(parent)
0115         , d(new Private)
0116 {
0117     addSubvalidator(validator);
0118 }
0119 
0120 KDbMultiValidator::~KDbMultiValidator()
0121 {
0122     delete d;
0123 }
0124 
0125 void KDbMultiValidator::addSubvalidator(QValidator* validator, bool owned)
0126 {
0127     if (!validator)
0128         return;
0129     d->subValidators.append(validator);
0130     if (owned && !validator->parent())
0131         d->ownedSubValidators.append(validator);
0132 }
0133 
0134 QValidator::State KDbMultiValidator::validate(QString & input, int & pos) const
0135 {
0136     State s;
0137     foreach(QValidator* validator, d->subValidators) {
0138         s = validator->validate(input, pos);
0139         if (s == Intermediate || s == Invalid)
0140             return s;
0141     }
0142     return Acceptable;
0143 }
0144 
0145 void KDbMultiValidator::fixup(QString & input) const
0146 {
0147     foreach(QValidator* validator, d->subValidators) {
0148         validator->fixup(input);
0149     }
0150 }
0151 
0152 KDbValidator::Result KDbMultiValidator::internalCheck(
0153     const QString &valueName, const QVariant& value,
0154     QString *message, QString *details)
0155 {
0156     Result r;
0157     bool warning = false;
0158     foreach(QValidator* validator, d->subValidators) {
0159         if (dynamic_cast<KDbValidator*>(validator))
0160             r = dynamic_cast<KDbValidator*>(validator)->internalCheck(valueName, value, message, details);
0161         else
0162             r = Ok; //ignore
0163         if (r == Error)
0164             return Error;
0165         else if (r == Warning)
0166             warning = true;
0167     }
0168     return warning ? Warning : Ok;
0169 }
0170