• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeui
 

tdeui

  • tdeui
knumvalidator.cpp
1/**********************************************************************
2**
3**
4** KIntValidator, KFloatValidator:
5** Copyright (C) 1999 Glen Parker <glenebob@nwlink.com>
6** KDoubleValidator:
7** Copyright (c) 2002 Marc Mutz <mutz@kde.org>
8**
9** This library is free software; you can redistribute it and/or
10** modify it under the terms of the GNU Library General Public
11** License as published by the Free Software Foundation; either
12** version 2 of the License, or (at your option) any later version.
13**
14** This library is distributed in the hope that it will be useful,
15** but WITHOUT ANY WARRANTY; without even the implied warranty of
16** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17** Library General Public License for more details.
18**
19** You should have received a copy of the GNU Library General Public
20** License along with this library; if not, write to the Free
21** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22**
23*****************************************************************************/
24
25#include <tqwidget.h>
26#include <tqstring.h>
27
28#include "knumvalidator.h"
29#include <tdelocale.h>
30#include <tdeglobal.h>
31#include <kdebug.h>
32
34// Implementation of KIntValidator
35//
36
37KIntValidator::KIntValidator ( TQWidget * parent, int base, const char * name )
38 : TQValidator(parent, name)
39{
40 _base = base;
41 if (_base < 2) _base = 2;
42 if (_base > 36) _base = 36;
43
44 _min = _max = 0;
45}
46
47KIntValidator::KIntValidator ( int bottom, int top, TQWidget * parent, int base, const char * name )
48 : TQValidator(parent, name)
49{
50 _base = base;
51 if (_base > 36) _base = 36;
52
53 _min = bottom;
54 _max = top;
55}
56
57KIntValidator::~KIntValidator ()
58{}
59
60TQValidator::State KIntValidator::validate ( TQString &str, int & ) const
61{
62 bool ok;
63 int val = 0;
64 TQString newStr;
65
66 newStr = str.stripWhiteSpace();
67 if (_base > 10)
68 newStr = newStr.upper();
69
70 if (newStr == TQString::fromLatin1("-")) // a special case
71 if ((_min || _max) && _min >= 0)
72 ok = false;
73 else
74 return TQValidator::Acceptable;
75 else if (newStr.length())
76 val = newStr.toInt(&ok, _base);
77 else {
78 val = 0;
79 ok = true;
80 }
81
82 if (! ok)
83 return TQValidator::Invalid;
84
85 if ((! _min && ! _max) || (val >= _min && val <= _max))
86 return TQValidator::Acceptable;
87
88 if (_max && _min >= 0 && val < 0)
89 return TQValidator::Invalid;
90
91 return TQValidator::Valid;
92}
93
94void KIntValidator::fixup ( TQString &str ) const
95{
96 int dummy;
97 int val;
98 TQValidator::State state;
99
100 state = validate(str, dummy);
101
102 if (state == TQValidator::Invalid || state == TQValidator::Acceptable)
103 return;
104
105 if (! _min && ! _max)
106 return;
107
108 val = str.toInt(0, _base);
109
110 if (val < _min) val = _min;
111 if (val > _max) val = _max;
112
113 str.setNum(val, _base);
114}
115
116void KIntValidator::setRange ( int bottom, int top )
117{
118 _min = bottom;
119 _max = top;
120
121 if (_max < _min)
122 _max = _min;
123}
124
125void KIntValidator::setBase ( int base )
126{
127 _base = base;
128 if (_base < 2) _base = 2;
129}
130
131int KIntValidator::bottom () const
132{
133 return _min;
134}
135
136int KIntValidator::top () const
137{
138 return _max;
139}
140
141int KIntValidator::base () const
142{
143 return _base;
144}
145
146
148// Implementation of KFloatValidator
149//
150
151class KFloatValidatorPrivate
152{
153public:
154 KFloatValidatorPrivate()
155 {
156 }
157 ~KFloatValidatorPrivate()
158 {
159 }
160 bool acceptLocalizedNumbers;
161};
162
163
164KFloatValidator::KFloatValidator ( TQWidget * parent, const char * name )
165 : TQValidator(parent, name)
166{
167 d = new KFloatValidatorPrivate;
168 d->acceptLocalizedNumbers=false;
169 _min = _max = 0;
170}
171
172KFloatValidator::KFloatValidator ( double bottom, double top, TQWidget * parent, const char * name )
173 : TQValidator(parent, name)
174{
175 d = new KFloatValidatorPrivate;
176 d->acceptLocalizedNumbers=false;
177 _min = bottom;
178 _max = top;
179}
180
181KFloatValidator::KFloatValidator ( double bottom, double top, bool localeAware, TQWidget * parent, const char * name )
182 : TQValidator(parent, name)
183{
184 d = new KFloatValidatorPrivate;
185 d->acceptLocalizedNumbers = localeAware;
186 _min = bottom;
187 _max = top;
188}
189
190KFloatValidator::~KFloatValidator ()
191{
192 delete d;
193}
194
195void KFloatValidator::setAcceptLocalizedNumbers(bool _b)
196{
197 d->acceptLocalizedNumbers=_b;
198}
199
200bool KFloatValidator::acceptLocalizedNumbers() const
201{
202 return d->acceptLocalizedNumbers;
203}
204
205TQValidator::State KFloatValidator::validate ( TQString &str, int & ) const
206{
207 bool ok;
208 double val = 0;
209 TQString newStr;
210 newStr = str.stripWhiteSpace();
211
212 if (newStr == TQString::fromLatin1("-")) // a special case
213 if ((_min || _max) && _min >= 0)
214 ok = false;
215 else
216 return TQValidator::Acceptable;
217 else if (newStr == TQString::fromLatin1(".") || (d->acceptLocalizedNumbers && newStr==TDEGlobal::locale()->decimalSymbol())) // another special case
218 return TQValidator::Acceptable;
219 else if (newStr.length())
220 {
221 val = newStr.toDouble(&ok);
222 if(!ok && d->acceptLocalizedNumbers)
223 val= TDEGlobal::locale()->readNumber(newStr,&ok);
224 }
225 else {
226 val = 0;
227 ok = true;
228 }
229
230 if (! ok)
231 return TQValidator::Invalid;
232
233 if (( !_min && !_max) || (val >= _min && val <= _max))
234 return TQValidator::Acceptable;
235
236 if (_max && _min >= 0 && val < 0)
237 return TQValidator::Invalid;
238
239 if ( (_min || _max) && (val < _min || val > _max))
240 return TQValidator::Invalid;
241
242 return TQValidator::Valid;
243}
244
245void KFloatValidator::fixup ( TQString &str ) const
246{
247 int dummy;
248 double val;
249 TQValidator::State state;
250
251 state = validate(str, dummy);
252
253 if (state == TQValidator::Invalid || state == TQValidator::Acceptable)
254 return;
255
256 if (! _min && ! _max)
257 return;
258
259 val = str.toDouble();
260
261 if (val < _min) val = _min;
262 if (val > _max) val = _max;
263
264 str.setNum(val);
265}
266
267void KFloatValidator::setRange ( double bottom, double top )
268{
269 _min = bottom;
270 _max = top;
271
272 if (_max < _min)
273 _max = _min;
274}
275
276double KFloatValidator::bottom () const
277{
278 return _min;
279}
280
281double KFloatValidator::top () const
282{
283 return _max;
284}
285
286
287
288
290// Implementation of KDoubleValidator
291//
292
293class KDoubleValidator::Private {
294public:
295 Private( bool accept=true ) : acceptLocalizedNumbers( accept ) {}
296
297 bool acceptLocalizedNumbers;
298};
299
300KDoubleValidator::KDoubleValidator( TQObject * parent, const char * name )
301 : TQDoubleValidator( parent, name ), d( 0 )
302{
303 d = new Private();
304}
305
306KDoubleValidator::KDoubleValidator( double bottom, double top, int decimals,
307 TQObject * parent, const char * name )
308 : TQDoubleValidator( bottom, top, decimals, parent, name ), d( 0 )
309{
310 d = new Private();
311}
312
313KDoubleValidator::~KDoubleValidator()
314{
315 delete d;
316}
317
318bool KDoubleValidator::acceptLocalizedNumbers() const {
319 return d->acceptLocalizedNumbers;
320}
321
322void KDoubleValidator::setAcceptLocalizedNumbers( bool accept ) {
323 d->acceptLocalizedNumbers = accept;
324}
325
326TQValidator::State KDoubleValidator::validate( TQString & input, int & p ) const {
327 TQString s = input;
328 if ( acceptLocalizedNumbers() ) {
329 TDELocale * l = TDEGlobal::locale();
330 // ok, we have to re-format the number to have:
331 // 1. decimalSymbol == '.'
332 // 2. negativeSign == '-'
333 // 3. positiveSign == <empty>
334 // 4. thousandsSeparator() == <empty> (we don't check that there
335 // are exactly three decimals between each separator):
336 TQString d = l->decimalSymbol(),
337 n = l->negativeSign(),
338 p = l->positiveSign(),
339 t = l->thousandsSeparator();
340 // first, delete p's and t's:
341 if ( !p.isEmpty() )
342 for ( int idx = s.find( p ) ; idx >= 0 ; idx = s.find( p, idx ) )
343 s.remove( idx, p.length() );
344
345
346 if ( !t.isEmpty() )
347 for ( int idx = s.find( t ) ; idx >= 0 ; idx = s.find( t, idx ) )
348 s.remove( idx, t.length() );
349
350 // then, replace the d's and n's
351 if ( ( !n.isEmpty() && n.find('.') != -1 ) ||
352 ( !d.isEmpty() && d.find('-') != -1 ) ) {
353 // make sure we don't replace something twice:
354 kdWarning() << "KDoubleValidator: decimal symbol contains '-' or "
355 "negative sign contains '.' -> improve algorithm" << endl;
356 return Invalid;
357 }
358
359 if ( !d.isEmpty() && d != "." )
360 for ( int idx = s.find( d ) ; idx >= 0 ; idx = s.find( d, idx + 1 ) )
361 s.replace( idx, d.length(), '.');
362
363 if ( !n.isEmpty() && n != "-" )
364 for ( int idx = s.find( n ) ; idx >= 0 ; idx = s.find( n, idx + 1 ) )
365 s.replace( idx, n.length(), '-' );
366 }
367
368 return base::validate( s, p );
369}
370
371#include "knumvalidator.moc"
KDoubleValidator::~KDoubleValidator
virtual ~KDoubleValidator()
Destructs the validator.
Definition: knumvalidator.cpp:313
KDoubleValidator::KDoubleValidator
KDoubleValidator(TQObject *parent, const char *name=0)
Constuct a locale-aware KDoubleValidator with default range (whatever TQDoubleValidator uses for that...
Definition: knumvalidator.cpp:300
KDoubleValidator::setAcceptLocalizedNumbers
void setAcceptLocalizedNumbers(bool accept)
Sets whether to accept localized numbers (default: true)
Definition: knumvalidator.cpp:322
KDoubleValidator::validate
virtual TQValidator::State validate(TQString &input, int &pos) const
Overloaded for internal reasons.
Definition: knumvalidator.cpp:326
KDoubleValidator::acceptLocalizedNumbers
bool acceptLocalizedNumbers() const
Definition: knumvalidator.cpp:318
KFloatValidator::setRange
virtual void setRange(double bottom, double top)
Sets the minimum and maximum value allowed.
Definition: knumvalidator.cpp:267
KFloatValidator::fixup
virtual void fixup(TQString &) const
Fixes the text if possible, providing a valid string.
Definition: knumvalidator.cpp:245
KFloatValidator::setAcceptLocalizedNumbers
void setAcceptLocalizedNumbers(bool b)
Sets the validator to be locale aware if is true.
Definition: knumvalidator.cpp:195
KFloatValidator::top
virtual double top() const
Returns the current maximum value allowed.
Definition: knumvalidator.cpp:281
KFloatValidator::KFloatValidator
KFloatValidator(TQWidget *parent, const char *name=0)
Constructor.
Definition: knumvalidator.cpp:164
KFloatValidator::validate
virtual State validate(TQString &, int &) const
Validates the text, and return the result.
Definition: knumvalidator.cpp:205
KFloatValidator::~KFloatValidator
virtual ~KFloatValidator()
Destructs the validator.
Definition: knumvalidator.cpp:190
KFloatValidator::acceptLocalizedNumbers
bool acceptLocalizedNumbers() const
Returns true if the validator is locale aware.
Definition: knumvalidator.cpp:200
KFloatValidator::bottom
virtual double bottom() const
Returns the current minimum value allowed.
Definition: knumvalidator.cpp:276
KIntValidator::validate
virtual State validate(TQString &, int &) const
Validates the text, and return the result.
Definition: knumvalidator.cpp:60
KIntValidator::KIntValidator
KIntValidator(TQWidget *parent, int base=10, const char *name=0)
Constuctor.
Definition: knumvalidator.cpp:37
KIntValidator::base
virtual int base() const
Returns the current numeric base.
Definition: knumvalidator.cpp:141
KIntValidator::fixup
virtual void fixup(TQString &) const
Fixes the text if possible, providing a valid string.
Definition: knumvalidator.cpp:94
KIntValidator::top
virtual int top() const
Returns the current maximum value allowed.
Definition: knumvalidator.cpp:136
KIntValidator::setRange
virtual void setRange(int bottom, int top)
Sets the minimum and maximum values allowed.
Definition: knumvalidator.cpp:116
KIntValidator::bottom
virtual int bottom() const
Returns the current minimum value allowed.
Definition: knumvalidator.cpp:131
KIntValidator::setBase
virtual void setBase(int base)
Sets the numeric base value.
Definition: knumvalidator.cpp:125
KIntValidator::~KIntValidator
virtual ~KIntValidator()
Destructs the validator.
Definition: knumvalidator.cpp:57
TDEGlobal::locale
static TDELocale * locale()
TDELocale
TDELocale::positiveSign
TQString positiveSign() const
TDELocale::thousandsSeparator
TQString thousandsSeparator() const
TDELocale::decimalSymbol
TQString decimalSymbol() const
TDELocale::readNumber
double readNumber(const TQString &numStr, bool *ok=0) const
TDELocale::negativeSign
TQString negativeSign() const
kdWarning
kdbgstream kdWarning(int area=0)
endl
kndbgstream & endl(kndbgstream &s)
tdelocale.h

tdeui

Skip menu "tdeui"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdeui

Skip menu "tdeui"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeui by doxygen 1.9.4
This website is maintained by Timothy Pearson.