File indexing completed on 2024-04-21 14:55:58

0001 /*
0002     Copyright (c) 2001 Marc Mutz <mutz@kde.org>
0003 
0004     This library 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; version 2.0
0007     of the License.
0008 
0009     This library 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
0015     License along with this library; if not, write to the Free
0016     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0017     02110-1301 USA
0018 */
0019 
0020 #include "kstringvalidator.h"
0021 
0022 #include <kdebug.h>
0023 
0024 class Q_DECL_HIDDEN KStringListValidator::Private
0025 {
0026 public:
0027     QStringList mStringList;
0028     bool mRejecting : 1;
0029     bool mFixupEnabled : 1;
0030 };
0031 
0032 //
0033 // KStringListValidator
0034 //
0035 KStringListValidator::KStringListValidator(const QStringList &list, bool rejecting,
0036         bool fixupEnabled, QObject *parent)
0037     : QValidator(parent),
0038       d(new Private)
0039 {
0040     d->mStringList = list;
0041     d->mRejecting = rejecting;
0042     d->mFixupEnabled = fixupEnabled;
0043 }
0044 
0045 KStringListValidator::~KStringListValidator()
0046 {
0047     delete d;
0048 }
0049 
0050 QValidator::State KStringListValidator::validate(QString &input, int &) const
0051 {
0052     if (input.isEmpty()) {
0053         return Intermediate;
0054     }
0055 
0056     if (isRejecting())   // anything not in mStringList is acceptable:
0057         if (!d->mStringList.contains(input)) {
0058             return Acceptable;
0059         } else {
0060             return Intermediate;
0061         }
0062     else // only what is in mStringList is acceptable:
0063         if (d->mStringList.contains(input)) {
0064             return Acceptable;
0065         } else
0066             for (QStringList::ConstIterator it = d->mStringList.constBegin(); it != d->mStringList.constEnd(); ++it)
0067                 if ((*it).startsWith(input) || input.startsWith(*it)) {
0068                     return Intermediate;
0069                 }
0070 
0071     return Invalid;
0072 }
0073 
0074 void KStringListValidator::fixup(QString &) const
0075 {
0076     if (!isFixupEnabled()) {
0077         return;
0078     }
0079 
0080     // warn (but only once!) about non-implemented fixup():
0081     static bool warn = true;
0082     if (warn) {
0083         kDebug() << "KStringListValidator::fixup() isn't yet implemented!";
0084         warn = false;
0085     }
0086 }
0087 
0088 void KStringListValidator::setRejecting(bool rejecting)
0089 {
0090     d->mRejecting = rejecting;
0091 }
0092 
0093 bool KStringListValidator::isRejecting() const
0094 {
0095     return d->mRejecting;
0096 }
0097 
0098 void KStringListValidator::setFixupEnabled(bool fixupEnabled)
0099 {
0100     d->mFixupEnabled = fixupEnabled;
0101 }
0102 
0103 bool KStringListValidator::isFixupEnabled() const
0104 {
0105     return d->mFixupEnabled;
0106 }
0107 
0108 void KStringListValidator::setStringList(const QStringList &list)
0109 {
0110     d->mStringList = list;
0111 }
0112 
0113 QStringList KStringListValidator::stringList() const
0114 {
0115     return d->mStringList;
0116 }
0117 
0118 #include "moc_kstringvalidator.cpp"