File indexing completed on 2024-03-24 16:11:06

0001 /* This file is part of the KDE project
0002    Copyright (C) 2006-2016 Jarosław Staniek <staniek@kde.org>
0003 
0004    Based on KIntValidator code by Glen Parker <glenebob@nwlink.com>
0005 
0006    This program is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU Library General Public
0008    License as published by the Free Software Foundation; either
0009    version 2 of the License, or (at your option) any later version.
0010 
0011    This program is distributed in the hope that it will be useful,
0012    but WITHOUT ANY WARRANTY; without even the implied warranty of
0013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014    Library General Public License for more details.
0015 
0016    You should have received a copy of the GNU Library General Public License
0017    along with this program; see the file COPYING.  If not, write to
0018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019  * Boston, MA 02110-1301, USA.
0020 */
0021 
0022 #include "KDbLongLongValidator.h"
0023 
0024 #include <QWidget>
0025 
0026 class Q_DECL_HIDDEN KDbLongLongValidator::Private
0027 {
0028 public:
0029     Private()
0030         : min(0)
0031         , max(0)
0032     {
0033     }
0034     qint64 base;
0035     qint64 min;
0036     qint64 max;
0037 };
0038 
0039 KDbLongLongValidator::KDbLongLongValidator(QWidget * parent, int base)
0040         : QValidator(parent)
0041         , d(new Private)
0042 {
0043     setBase(base);
0044 }
0045 
0046 KDbLongLongValidator::KDbLongLongValidator(qint64 bottom, qint64 top,
0047                                            QWidget * parent, int base)
0048         : QValidator(parent)
0049         , d(new Private)
0050 {
0051     setBase(base);
0052     setRange(bottom, top);
0053 }
0054 
0055 KDbLongLongValidator::~KDbLongLongValidator()
0056 {
0057     delete d;
0058 }
0059 
0060 QValidator::State KDbLongLongValidator::validate(QString &str, int &) const
0061 {
0062     bool ok;
0063     qint64 val = 0;
0064     QString newStr;
0065 
0066     newStr = str.trimmed();
0067     if (d->base > 10)
0068         newStr = newStr.toUpper();
0069 
0070     if (newStr == QString::fromLatin1("-")) {// a special case
0071         if ((d->min || d->max) && d->min >= 0)
0072             ok = false;
0073         else
0074             return QValidator::Intermediate;
0075     } else if (!newStr.isEmpty())
0076         val = newStr.toLongLong(&ok, d->base);
0077     else {
0078         val = 0;
0079         ok = true;
0080     }
0081 
0082     if (! ok)
0083         return QValidator::Invalid;
0084 
0085     if ((! d->min && ! d->max) || (val >= d->min && val <= d->max))
0086         return QValidator::Acceptable;
0087 
0088     if (d->max && d->min >= 0 && val < 0)
0089         return QValidator::Invalid;
0090 
0091     return QValidator::Intermediate;
0092 }
0093 
0094 void KDbLongLongValidator::fixup(QString &str) const
0095 {
0096     int dummy;
0097     qint64 val;
0098     QValidator::State state;
0099 
0100     state = validate(str, dummy);
0101 
0102     if (state == QValidator::Invalid || state == QValidator::Acceptable)
0103         return;
0104 
0105     if (! d->min && ! d->max)
0106         return;
0107 
0108     val = str.toLongLong(nullptr, d->base);
0109 
0110     if (val < d->min)
0111         val = d->min;
0112     if (val > d->max)
0113         val = d->max;
0114 
0115     str.setNum(val, d->base);
0116 }
0117 
0118 void KDbLongLongValidator::setRange(qint64 bottom, qint64 top)
0119 {
0120     d->min = bottom;
0121     d->max = top;
0122 
0123     if (d->max < d->min)
0124         d->max = d->min;
0125 }
0126 
0127 void KDbLongLongValidator::setBase(int base)
0128 {
0129     d->base = base;
0130     if (d->base < 2)
0131         d->base = 2;
0132     if (d->base > 36)
0133         d->base = 36;
0134 }
0135 
0136 qint64 KDbLongLongValidator::bottom() const
0137 {
0138     return d->min;
0139 }
0140 
0141 qint64 KDbLongLongValidator::top() const
0142 {
0143     return d->max;
0144 }
0145 
0146 int KDbLongLongValidator::base() const
0147 {
0148     return d->base;
0149 }