File indexing completed on 2024-06-09 16:09:45

0001 /* This file is part of the KDE project
0002    Copyright (C) 2006 Jarosław Staniek <staniek@kde.org>
0003 
0004    This program 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; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This program 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 License
0015    along with this program; see the file COPYING.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "KDbFieldValidator.h"
0021 #include "KDbField.h"
0022 #include "KDbLongLongValidator.h"
0023 
0024 #include <QIntValidator>
0025 #include <QDoubleValidator>
0026 #include <QWidget>
0027 
0028 using namespace KDbUtils;
0029 
0030 KDbFieldValidator::KDbFieldValidator(const KDbField &field, QWidget * parent)
0031         : KDbMultiValidator(parent)
0032 {
0033 //! @todo merge this code with KexiTableEdit code!
0034 //! @todo set maximum length validator
0035 //! @todo handle input mask (via QLineEdit::setInputMask()
0036     const KDbField::Type t = field.type(); // cache: evaluating type of expressions can be expensive
0037     if (KDbField::isIntegerType(t)) {
0038         QValidator *validator = nullptr;
0039         const bool u = field.isUnsigned();
0040         int bottom = 0, top = 0;
0041         if (t == KDbField::Byte) {
0042             bottom = u ? 0 : -0x80;
0043             top = u ? 0xff : 0x7f;
0044         } else if (t == KDbField::ShortInteger) {
0045             bottom = u ? 0 : -0x8000;
0046             top = u ? 0xffff : 0x7fff;
0047         } else if (t == KDbField::Integer) {
0048             bottom = u ? 0 : -0x7fffffff - 1;
0049             top = u ? 0xffffffff : 0x7fffffff;
0050             validator = new KDbLongLongValidator(bottom, top, nullptr);
0051         } else if (t == KDbField::BigInteger) {
0052 //! @todo handle unsigned (using ULongLongValidator)
0053             validator = new KDbLongLongValidator(nullptr);
0054         }
0055 
0056         if (!validator)
0057             validator = new QIntValidator(bottom, top, nullptr); //the default
0058         addSubvalidator(validator);
0059     } else if (KDbField::isFPNumericType(t)) {
0060         QValidator *validator;
0061         if (t == KDbField::Float) {
0062             if (field.isUnsigned()) //ok?
0063                 validator = new QDoubleValidator(0, 3.4e+38, field.scale(), nullptr);
0064             else
0065                 validator = new QDoubleValidator((QObject*)nullptr);
0066         } else {//double
0067             if (field.isUnsigned()) //ok?
0068                 validator = new QDoubleValidator(0, 1.7e+308, field.scale(), nullptr);
0069             else
0070                 validator = new QDoubleValidator((QObject*)nullptr);
0071         }
0072         addSubvalidator(validator);
0073     } else if (t == KDbField::Date) {
0074 //! @todo add validator
0075 //  QValidator *validator = new KDateValidator(this);
0076 //  setValidator( validator );
0077     } else if (t == KDbField::Time) {
0078 //! @todo add validator
0079     } else if (t == KDbField::DateTime) {
0080     } else if (t == KDbField::Boolean) {
0081 //! @todo add BooleanValidator
0082         addSubvalidator(new QIntValidator(0, 1, nullptr));
0083     }
0084 }
0085 
0086 KDbFieldValidator::~KDbFieldValidator()
0087 {
0088 }