KDb

KDbIdentifierValidator.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2003-2005 JarosÅ‚aw Staniek <[email protected]>
3  Copyright (C) 2005 Martin Ellis <[email protected]>
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 
24 class Q_DECL_HIDDEN KDbIdentifierValidator::Private
25 {
26 public:
27  Private() : isLowerCaseForced(false) {}
28  bool isLowerCaseForced;
29 private:
30  Q_DISABLE_COPY(Private)
31 };
32 
33 KDbIdentifierValidator::KDbIdentifierValidator(QObject * parent)
34  : KDbValidator(parent), d(new Private)
35 {
36 }
37 
38 KDbIdentifierValidator::~KDbIdentifierValidator()
39 {
40  delete d;
41 }
42 
43 QValidator::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())
58  ? Intermediate
59  : Acceptable;
60 }
61 
62 KDbValidator::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 }
void setLowerCaseForced(bool set)
If set is true, upper-case letters in the input are replaced to lower-case.
KDB_EXPORT QString identifierExpectedMessage(const QString &valueName, const QVariant &v)
Definition: KDb.cpp:2199
bool isEmpty() const const
int length() const const
Validates input for identifier.
QString toLower() const const
A validator extending QValidator with offline-checking for value's validity.
Definition: KDbValidator.h:41
KDB_EXPORT bool isIdentifier(const QString &s)
Definition: KDb.cpp:2133
QString right(int n) const const
bool acceptsEmptyValue() const
const QChar at(int position) const const
QString message
QString toString() const const
KDB_EXPORT QString stringToIdentifier(const QString &s)
Definition: KDb.cpp:2166
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jun 25 2022 06:21:33 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.