kexi

longlongvalidator.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2006 Jaroslaw Staniek <js@iidea.pl>
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 "longlongvalidator.h"
23
24#include <tqwidget.h>
25
26using namespace KexiUtils;
27
28LongLongValidator::LongLongValidator( TQWidget * parent, int base, const char * name )
29 : TQValidator(parent, name)
30 , m_min(0), m_max(0)
31{
32 setBase(base);
33}
34
35LongLongValidator::LongLongValidator( TQ_LLONG bottom, TQ_LLONG top, TQWidget * parent, int base, const char * name )
36 : TQValidator(parent, name)
37{
38 setBase(base);
39 setRange( bottom, top );
40}
41
42LongLongValidator::~LongLongValidator()
43{
44}
45
46TQValidator::State LongLongValidator::validate( TQString &str, int & ) const
47{
48 bool ok;
49 TQ_LLONG val = 0;
50 TQString newStr;
51
52 newStr = str.stripWhiteSpace();
53 if (m_base > 10)
54 newStr = newStr.upper();
55
56 if (newStr == TQString::fromLatin1("-")) {// a special case
57 if ((m_min || m_max) && m_min >= 0)
58 ok = false;
59 else
60 return TQValidator::Acceptable;
61 }
62 else if (!newStr.isEmpty())
63 val = newStr.toLongLong(&ok, m_base);
64 else {
65 val = 0;
66 ok = true;
67 }
68
69 if (! ok)
70 return TQValidator::Invalid;
71
72 if ((! m_min && ! m_max) || (val >= m_min && val <= m_max))
73 return TQValidator::Acceptable;
74
75 if (m_max && m_min >= 0 && val < 0)
76 return TQValidator::Invalid;
77
78 return TQValidator::Valid;
79}
80
81void LongLongValidator::fixup( TQString &str ) const
82{
83 int dummy;
84 TQ_LLONG val;
85 TQValidator::State state;
86
87 state = validate(str, dummy);
88
89 if (state == TQValidator::Invalid || state == TQValidator::Acceptable)
90 return;
91
92 if (! m_min && ! m_max)
93 return;
94
95 val = str.toLongLong(0, m_base);
96
97 if (val < m_min)
98 val = m_min;
99 if (val > m_max)
100 val = m_max;
101
102 str.setNum(val, m_base);
103}
104
105void LongLongValidator::setRange( TQ_LLONG bottom, TQ_LLONG top )
106{
107 m_min = bottom;
108 m_max = top;
109
110 if (m_max < m_min)
111 m_max = m_min;
112}
113
115{
116 m_base = base;
117 if (m_base < 2)
118 m_base = 2;
119 if (m_base > 36)
120 m_base = 36;
121}
122
124{
125 return m_min;
126}
127
129{
130 return m_max;
131}
132
134{
135 return m_base;
136}
virtual void setRange(TQ_LLONG bottom, TQ_LLONG top)
Sets the minimum and maximum values allowed.
virtual void fixup(TQString &) const
Fixes the text if possible, providing a valid string. The parameter may be modified.
virtual void setBase(int base)
Sets the numeric base value.
virtual State validate(TQString &, int &) const
Validates the text, and returns the result. Does not modify the parameters.
virtual TQ_LLONG top() const
virtual TQ_LLONG bottom() const
General Utils.
Definition identifier.h:27