KDb

KDbIdentifierValidator.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003-2005 Jarosław Staniek <staniek@kde.org>
3 Copyright (C) 2005 Martin Ellis <martin.ellis@kdemail.net>
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this program; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include "KDbIdentifierValidator.h"
22#include "KDb.h"
23
24class Q_DECL_HIDDEN KDbIdentifierValidator::Private
25{
26public:
27 Private() : isLowerCaseForced(false) {}
28 bool isLowerCaseForced;
29private:
30 Q_DISABLE_COPY(Private)
31};
32
33KDbIdentifierValidator::KDbIdentifierValidator(QObject * parent)
34 : KDbValidator(parent), d(new Private)
35{
36}
37
38KDbIdentifierValidator::~KDbIdentifierValidator()
39{
40 delete d;
41}
42
43QValidator::State KDbIdentifierValidator::validate(QString& input, int& pos) const
44{
45 int i;
46 for (i = 0; i < input.length() && input.at(i) == QLatin1Char(' '); i++)
47 ;
48 pos -= i; //i chars will be removed from beginning
49 if (i < input.length() && input.at(i) >= QLatin1Char('0') && input.at(i) <= QLatin1Char('9'))
50 pos++; //_ will be added at the beginning
51 bool addspace = (input.right(1) == QLatin1String(" "));
52 input = d->isLowerCaseForced ? KDb::stringToIdentifier(input).toLower() : KDb::stringToIdentifier(input);
53 if (addspace)
54 input += QLatin1Char('_');
55 if (pos > input.length())
56 pos = input.length();
57 return (input.isEmpty() && !acceptsEmptyValue())
59 : Acceptable;
60}
61
62KDbValidator::Result KDbIdentifierValidator::internalCheck(
63 const QString &valueName, const QVariant& value,
64 QString *message, QString *details)
65{
66 Q_UNUSED(details);
67 if (KDb::isIdentifier(value.toString()))
68 return KDbValidator::Ok;
69 if (message) {
70 *message = KDb::identifierExpectedMessage(valueName, value);
71 }
72 return KDbValidator::Error;
73}
74
76{
77 return d->isLowerCaseForced;
78}
79
81{
82 d->isLowerCaseForced = set;
83}
Validates input for identifier.
void setLowerCaseForced(bool set)
If set is true, upper-case letters in the input are replaced to lower-case.
A validator extending QValidator with offline-checking for value's validity.
bool acceptsEmptyValue() const
KDB_EXPORT bool isIdentifier(const QString &s)
KDB_EXPORT QString identifierExpectedMessage(const QString &valueName, const QVariant &v)
KDB_EXPORT QString stringToIdentifier(const QString &s)
const QChar at(qsizetype position) const const
bool isEmpty() const const
qsizetype length() const const
QString right(qsizetype n) const const
QString toLower() const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:19:07 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.