KDb

KDbLongLongValidator.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2006-2016 Jarosław Staniek <staniek@kde.org>
3
4 Based on KIntValidator code by Glen Parker <glenebob@nwlink.com>
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this program; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20*/
21
22#include "KDbLongLongValidator.h"
23
24#include <QWidget>
25
26class Q_DECL_HIDDEN KDbLongLongValidator::Private
27{
28public:
29 Private()
30 : min(0)
31 , max(0)
32 {
33 }
34 qint64 base;
35 qint64 min;
36 qint64 max;
37};
38
39KDbLongLongValidator::KDbLongLongValidator(QWidget * parent, int base)
40 : QValidator(parent)
41 , d(new Private)
42{
43 setBase(base);
44}
45
46KDbLongLongValidator::KDbLongLongValidator(qint64 bottom, qint64 top,
47 QWidget * parent, int base)
48 : QValidator(parent)
49 , d(new Private)
50{
51 setBase(base);
52 setRange(bottom, top);
53}
54
55KDbLongLongValidator::~KDbLongLongValidator()
56{
57 delete d;
58}
59
61{
62 bool ok;
63 qint64 val = 0;
64 QString newStr;
65
66 newStr = str.trimmed();
67 if (d->base > 10)
68 newStr = newStr.toUpper();
69
70 if (newStr == QString::fromLatin1("-")) {// a special case
71 if ((d->min || d->max) && d->min >= 0)
72 ok = false;
73 else
75 } else if (!newStr.isEmpty())
76 val = newStr.toLongLong(&ok, d->base);
77 else {
78 val = 0;
79 ok = true;
80 }
81
82 if (! ok)
84
85 if ((! d->min && ! d->max) || (val >= d->min && val <= d->max))
87
88 if (d->max && d->min >= 0 && val < 0)
90
92}
93
95{
96 int dummy;
97 qint64 val;
99
100 state = validate(str, dummy);
101
102 if (state == QValidator::Invalid || state == QValidator::Acceptable)
103 return;
104
105 if (! d->min && ! d->max)
106 return;
107
108 val = str.toLongLong(nullptr, d->base);
109
110 if (val < d->min)
111 val = d->min;
112 if (val > d->max)
113 val = d->max;
114
115 str.setNum(val, d->base);
116}
117
118void KDbLongLongValidator::setRange(qint64 bottom, qint64 top)
119{
120 d->min = bottom;
121 d->max = top;
122
123 if (d->max < d->min)
124 d->max = d->min;
125}
126
128{
129 d->base = base;
130 if (d->base < 2)
131 d->base = 2;
132 if (d->base > 36)
133 d->base = 36;
134}
135
137{
138 return d->min;
139}
140
142{
143 return d->max;
144}
145
147{
148 return d->base;
149}
A validator for longlong data type.
virtual void setRange(qint64 bottom, qint64 top)
Sets the minimum and maximum values allowed.
void fixup(QString &) const override
Fixes the text if possible, providing a valid string. The parameter may be modified.
State validate(QString &, int &) const override
Validates the text, and returns the result. Does not modify the parameters.
virtual qint64 top() const
virtual void setBase(int base)
Sets the numeric base value.
virtual qint64 bottom() const
QString fromLatin1(QByteArrayView str)
bool isEmpty() const const
QString & setNum(double n, char format, int precision)
qlonglong toLongLong(bool *ok, int base) const const
QString toUpper() const const
QString trimmed() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:38:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.