Diffstat (limited to 'noncore/settings/networksettings/ppp/knumvalidator.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/settings/networksettings/ppp/knumvalidator.cpp | 376 |
1 files changed, 376 insertions, 0 deletions
diff --git a/noncore/settings/networksettings/ppp/knumvalidator.cpp b/noncore/settings/networksettings/ppp/knumvalidator.cpp new file mode 100644 index 0000000..44f7a60 --- a/dev/null +++ b/noncore/settings/networksettings/ppp/knumvalidator.cpp | |||
@@ -0,0 +1,376 @@ | |||
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 | #define i18n QObject::tr | ||
32 | //#include <kglobal.h> | ||
33 | //#include <kdebug.h> | ||
34 | |||
35 | /////////////////////////////////////////////////////////////// | ||
36 | // Implementation of KIntValidator | ||
37 | // | ||
38 | |||
39 | KIntValidator::KIntValidator ( QWidget * parent, int base, const char * name ) | ||
40 | : QValidator(parent, name) | ||
41 | { | ||
42 | _base = base; | ||
43 | if (_base < 2) _base = 2; | ||
44 | if (_base > 36) _base = 36; | ||
45 | |||
46 | _min = _max = 0; | ||
47 | } | ||
48 | |||
49 | KIntValidator::KIntValidator ( int bottom, int top, QWidget * parent, int base, const char * name ) | ||
50 | : QValidator(parent, name) | ||
51 | { | ||
52 | _base = base; | ||
53 | if (_base > 36) _base = 36; | ||
54 | |||
55 | _min = bottom; | ||
56 | _max = top; | ||
57 | } | ||
58 | |||
59 | KIntValidator::~KIntValidator () | ||
60 | {} | ||
61 | |||
62 | QValidator::State KIntValidator::validate ( QString &str, int & ) const | ||
63 | { | ||
64 | bool ok; | ||
65 | int val = 0; | ||
66 | QString newStr; | ||
67 | |||
68 | newStr = str.stripWhiteSpace(); | ||
69 | if (_base > 10) | ||
70 | newStr = newStr.upper(); | ||
71 | |||
72 | if (newStr == QString::fromLatin1("-")) // a special case | ||
73 | if ((_min || _max) && _min >= 0) | ||
74 | ok = false; | ||
75 | else | ||
76 | return QValidator::Acceptable; | ||
77 | else if (newStr.length()) | ||
78 | val = newStr.toInt(&ok, _base); | ||
79 | else { | ||
80 | val = 0; | ||
81 | ok = true; | ||
82 | } | ||
83 | |||
84 | if (! ok) | ||
85 | return QValidator::Invalid; | ||
86 | |||
87 | if ((! _min && ! _max) || (val >= _min && val <= _max)) | ||
88 | return QValidator::Acceptable; | ||
89 | |||
90 | if (_max && _min >= 0 && val < 0) | ||
91 | return QValidator::Invalid; | ||
92 | |||
93 | return QValidator::Valid; | ||
94 | } | ||
95 | |||
96 | void KIntValidator::fixup ( QString &str ) const | ||
97 | { | ||
98 | int dummy; | ||
99 | int val; | ||
100 | QValidator::State state; | ||
101 | |||
102 | state = validate(str, dummy); | ||
103 | |||
104 | if (state == QValidator::Invalid || state == QValidator::Acceptable) | ||
105 | return; | ||
106 | |||
107 | if (! _min && ! _max) | ||
108 | return; | ||
109 | |||
110 | val = str.toInt(0, _base); | ||
111 | |||
112 | if (val < _min) val = _min; | ||
113 | if (val > _max) val = _max; | ||
114 | |||
115 | str.setNum(val, _base); | ||
116 | } | ||
117 | |||
118 | void KIntValidator::setRange ( int bottom, int top ) | ||
119 | { | ||
120 | _min = bottom; | ||
121 | _max = top; | ||
122 | |||
123 | if (_max < _min) | ||
124 | _max = _min; | ||
125 | } | ||
126 | |||
127 | void KIntValidator::setBase ( int base ) | ||
128 | { | ||
129 | _base = base; | ||
130 | if (_base < 2) _base = 2; | ||
131 | } | ||
132 | |||
133 | int KIntValidator::bottom () const | ||
134 | { | ||
135 | return _min; | ||
136 | } | ||
137 | |||
138 | int KIntValidator::top () const | ||
139 | { | ||
140 | return _max; | ||
141 | } | ||
142 | |||
143 | int KIntValidator::base () const | ||
144 | { | ||
145 | return _base; | ||
146 | } | ||
147 | |||
148 | |||
149 | /////////////////////////////////////////////////////////////// | ||
150 | // Implementation of KFloatValidator | ||
151 | // | ||
152 | |||
153 | class KFloatValidatorPrivate | ||
154 | { | ||
155 | public: | ||
156 | KFloatValidatorPrivate() | ||
157 | { | ||
158 | } | ||
159 | ~KFloatValidatorPrivate() | ||
160 | { | ||
161 | } | ||
162 | bool acceptLocalizedNumbers; | ||
163 | }; | ||
164 | |||
165 | |||
166 | KFloatValidator::KFloatValidator ( QWidget * parent, const char * name ) | ||
167 | : QValidator(parent, name) | ||
168 | { | ||
169 | d = new KFloatValidatorPrivate; | ||
170 | d->acceptLocalizedNumbers=false; | ||
171 | _min = _max = 0; | ||
172 | } | ||
173 | |||
174 | KFloatValidator::KFloatValidator ( double bottom, double top, QWidget * parent, const char * name ) | ||
175 | : QValidator(parent, name) | ||
176 | { | ||
177 | d = new KFloatValidatorPrivate; | ||
178 | d->acceptLocalizedNumbers=false; | ||
179 | _min = bottom; | ||
180 | _max = top; | ||
181 | } | ||
182 | |||
183 | KFloatValidator::KFloatValidator ( double bottom, double top, bool localeAware, QWidget * parent, const char * name ) | ||
184 | : QValidator(parent, name) | ||
185 | { | ||
186 | d = new KFloatValidatorPrivate; | ||
187 | d->acceptLocalizedNumbers = localeAware; | ||
188 | _min = bottom; | ||
189 | _max = top; | ||
190 | } | ||
191 | |||
192 | KFloatValidator::~KFloatValidator () | ||
193 | { | ||
194 | delete d; | ||
195 | } | ||
196 | |||
197 | void KFloatValidator::setAcceptLocalizedNumbers(bool _b) | ||
198 | { | ||
199 | d->acceptLocalizedNumbers=_b; | ||
200 | } | ||
201 | |||
202 | bool KFloatValidator::acceptLocalizedNumbers() const | ||
203 | { | ||
204 | return d->acceptLocalizedNumbers; | ||
205 | } | ||
206 | |||
207 | //#include <kdebug.h> | ||
208 | QValidator::State KFloatValidator::validate ( QString &str, int & ) const | ||
209 | { | ||
210 | bool ok; | ||
211 | double val = 0; | ||
212 | QString newStr; | ||
213 | newStr = str.stripWhiteSpace(); | ||
214 | |||
215 | if (newStr == QString::fromLatin1("-")) // a special case | ||
216 | if ((_min || _max) && _min >= 0) | ||
217 | ok = false; | ||
218 | else | ||
219 | return QValidator::Acceptable; | ||
220 | else if (newStr == QString::fromLatin1(".") ) // FIXME :|| (d->acceptLocalizedNumbers && newStr==KGlobal::locale()->decimalSymbol())) // another special case | ||
221 | return QValidator::Acceptable; | ||
222 | else if (newStr.length()) | ||
223 | { | ||
224 | val = newStr.toDouble(&ok); | ||
225 | // if(!ok && d->acceptLocalizedNumbers) | ||
226 | // val= KGlobal::locale()->readNumber(newStr,&ok); | ||
227 | } | ||
228 | else { | ||
229 | val = 0; | ||
230 | ok = true; | ||
231 | } | ||
232 | |||
233 | if (! ok) | ||
234 | return QValidator::Invalid; | ||
235 | |||
236 | if (( !_min && !_max) || (val >= _min && val <= _max)) | ||
237 | return QValidator::Acceptable; | ||
238 | |||
239 | if (_max && _min >= 0 && val < 0) | ||
240 | return QValidator::Invalid; | ||
241 | |||
242 | if ( (_min || _max) && (val < _min || val > _max)) | ||
243 | return QValidator::Invalid; | ||
244 | |||
245 | return QValidator::Valid; | ||
246 | } | ||
247 | |||
248 | void KFloatValidator::fixup ( QString &str ) const | ||
249 | { | ||
250 | int dummy; | ||
251 | double val; | ||
252 | QValidator::State state; | ||
253 | |||
254 | state = validate(str, dummy); | ||
255 | |||
256 | if (state == QValidator::Invalid || state == QValidator::Acceptable) | ||
257 | return; | ||
258 | |||
259 | if (! _min && ! _max) | ||
260 | return; | ||
261 | |||
262 | val = str.toDouble(); | ||
263 | |||
264 | if (val < _min) val = _min; | ||
265 | if (val > _max) val = _max; | ||
266 | |||
267 | str.setNum(val); | ||
268 | } | ||
269 | |||
270 | void KFloatValidator::setRange ( double bottom, double top ) | ||
271 | { | ||
272 | _min = bottom; | ||
273 | _max = top; | ||
274 | |||
275 | if (_max < _min) | ||
276 | _max = _min; | ||
277 | } | ||
278 | |||
279 | double KFloatValidator::bottom () const | ||
280 | { | ||
281 | return _min; | ||
282 | } | ||
283 | |||
284 | double KFloatValidator::top () const | ||
285 | { | ||
286 | return _max; | ||
287 | } | ||
288 | |||
289 | |||
290 | |||
291 | |||
292 | /////////////////////////////////////////////////////////////// | ||
293 | // Implementation of KDoubleValidator | ||
294 | // | ||
295 | |||
296 | class KDoubleValidator::Private { | ||
297 | public: | ||
298 | Private( bool accept=false ) : acceptLocalizedNumbers( accept ) {} | ||
299 | |||
300 | bool acceptLocalizedNumbers; | ||
301 | }; | ||
302 | |||
303 | //KDoubleValidator::KDoubleValidator( QObject * parent, const char * name ) | ||
304 | KDoubleValidator::KDoubleValidator( QWidget * parent, const char * name ) | ||
305 | : QDoubleValidator( parent, name ), d( 0 ) | ||
306 | { | ||
307 | d = new Private(); | ||
308 | } | ||
309 | |||
310 | KDoubleValidator::KDoubleValidator( double bottom, double top, int decimals, | ||
311 | // QObject * parent, const char * name ) | ||
312 | QWidget * parent, const char * name ) | ||
313 | : QDoubleValidator( bottom, top, decimals, parent, name ), d( 0 ) | ||
314 | { | ||
315 | d = new Private(); | ||
316 | } | ||
317 | |||
318 | KDoubleValidator::~KDoubleValidator() | ||
319 | { | ||
320 | delete d; | ||
321 | } | ||
322 | |||
323 | bool KDoubleValidator::acceptLocalizedNumbers() const { | ||
324 | return d->acceptLocalizedNumbers; | ||
325 | } | ||
326 | |||
327 | void KDoubleValidator::setAcceptLocalizedNumbers( bool accept ) { | ||
328 | d->acceptLocalizedNumbers = accept; | ||
329 | } | ||
330 | |||
331 | QValidator::State KDoubleValidator::validate( QString & input, int & p ) const { | ||
332 | QString s = input; | ||
333 | if ( acceptLocalizedNumbers() ) { | ||
334 | // KLocale * l = KGlobal::locale(); | ||
335 | // ok, we have to re-format the number to have: | ||
336 | // 1. decimalSymbol == '.' | ||
337 | // 2. negativeSign == '-' | ||
338 | // 3. positiveSign == <empty> | ||
339 | // 4. thousandsSeparator() == <empty> (we don't check that there | ||
340 | // are exactly three decimals between each separator): | ||
341 | QString d = ".",//l->decimalSymbol(), | ||
342 | n = "-",//l->negativeSign(), | ||
343 | p = "", //l->positiveSign(), | ||
344 | t = "";//l->thousandsSeparator(); | ||
345 | // first, delete p's and t's: | ||
346 | if ( !p.isEmpty() ) | ||
347 | for ( int idx = s.find( p ) ; idx >= 0 ; idx = s.find( p, idx ) ) | ||
348 | s.remove( idx, p.length() ); | ||
349 | |||
350 | |||
351 | if ( !t.isEmpty() ) | ||
352 | for ( int idx = s.find( t ) ; idx >= 0 ; idx = s.find( t, idx ) ) | ||
353 | s.remove( idx, t.length() ); | ||
354 | |||
355 | // then, replace the d's and n's | ||
356 | if ( ( !n.isEmpty() && n.find('.') != -1 ) || | ||
357 | ( !d.isEmpty() && d.find('-') != -1 ) ) { | ||
358 | // make sure we don't replace something twice: | ||
359 | // kdWarning() << "KDoubleValidator: decimal symbol contains '-' or " | ||
360 | // "negative sign contains '.' -> improve algorithm" << endl; | ||
361 | return Invalid; | ||
362 | } | ||
363 | |||
364 | if ( !d.isEmpty() && d != "." ) | ||
365 | for ( int idx = s.find( d ) ; idx >= 0 ; idx = s.find( d, idx + 1 ) ) | ||
366 | s.replace( idx, d.length(), "."); | ||
367 | |||
368 | if ( !n.isEmpty() && n != "-" ) | ||
369 | for ( int idx = s.find( n ) ; idx >= 0 ; idx = s.find( n, idx + 1 ) ) | ||
370 | s.replace( idx, n.length(), "-" ); | ||
371 | } | ||
372 | |||
373 | return base::validate( s, p ); | ||
374 | } | ||
375 | |||
376 | //#include "knumvalidator.moc" | ||