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