KDb

KDbFieldValidator.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2006 Jarosław Staniek <staniek@kde.org>
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "KDbFieldValidator.h"
21#include "KDbField.h"
22#include "KDbLongLongValidator.h"
23
24#include <QIntValidator>
25#include <QDoubleValidator>
26#include <QWidget>
27
28using namespace KDbUtils;
29
31 : KDbMultiValidator(parent)
32{
33//! @todo merge this code with KexiTableEdit code!
34//! @todo set maximum length validator
35//! @todo handle input mask (via QLineEdit::setInputMask()
36 const KDbField::Type t = field.type(); // cache: evaluating type of expressions can be expensive
38 QValidator *validator = nullptr;
39 const bool u = field.isUnsigned();
40 int bottom = 0, top = 0;
41 if (t == KDbField::Byte) {
42 bottom = u ? 0 : -0x80;
43 top = u ? 0xff : 0x7f;
44 } else if (t == KDbField::ShortInteger) {
45 bottom = u ? 0 : -0x8000;
46 top = u ? 0xffff : 0x7fff;
47 } else if (t == KDbField::Integer) {
48 bottom = u ? 0 : -0x7fffffff - 1;
49 top = u ? 0xffffffff : 0x7fffffff;
50 validator = new KDbLongLongValidator(bottom, top, nullptr);
51 } else if (t == KDbField::BigInteger) {
52//! @todo handle unsigned (using ULongLongValidator)
53 validator = new KDbLongLongValidator(nullptr);
54 }
55
56 if (!validator)
57 validator = new QIntValidator(bottom, top, nullptr); //the default
58 addSubvalidator(validator);
59 } else if (KDbField::isFPNumericType(t)) {
60 QValidator *validator;
61 if (t == KDbField::Float) {
62 if (field.isUnsigned()) //ok?
63 validator = new QDoubleValidator(0, 3.4e+38, field.scale(), nullptr);
64 else
65 validator = new QDoubleValidator((QObject*)nullptr);
66 } else {//double
67 if (field.isUnsigned()) //ok?
68 validator = new QDoubleValidator(0, 1.7e+308, field.scale(), nullptr);
69 else
70 validator = new QDoubleValidator((QObject*)nullptr);
71 }
72 addSubvalidator(validator);
73 } else if (t == KDbField::Date) {
74//! @todo add validator
75// QValidator *validator = new KDateValidator(this);
76// setValidator( validator );
77 } else if (t == KDbField::Time) {
78//! @todo add validator
79 } else if (t == KDbField::DateTime) {
80 } else if (t == KDbField::Boolean) {
81//! @todo add BooleanValidator
82 addSubvalidator(new QIntValidator(0, 1, nullptr));
83 }
84}
85
86KDbFieldValidator::~KDbFieldValidator()
87{
88}
KDbFieldValidator(const KDbField &field, QWidget *parent=nullptr)
Setups the validator for field. Does not keep a pointer to field.
Meta-data for a field.
Definition KDbField.h:72
int scale() const
Definition KDbField.cpp:291
@ Integer
Definition KDbField.h:90
@ Boolean
Definition KDbField.h:92
@ ShortInteger
Definition KDbField.h:89
@ BigInteger
Definition KDbField.h:91
bool isFPNumericType() const
Definition KDbField.h:335
Type type() const
Definition KDbField.cpp:379
bool isIntegerType() const
Definition KDbField.h:326
bool isUnsigned() const
if the type has the unsigned attribute
Definition KDbField.h:515
A validator for longlong data type.
A validator groupping multiple QValidators.
void addSubvalidator(QValidator *validator, bool owned=true)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 26 2024 11:59:38 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.