00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <qwidget.h>
00027 #include <qstring.h>
00028
00029 #include "knumvalidator.h"
00030 #include <klocale.h>
00031 #include <kglobal.h>
00032 #include <kdebug.h>
00033
00035
00036
00037
00038 KIntValidator::KIntValidator ( QWidget * parent, int base, const char * name )
00039 : QValidator(parent, name)
00040 {
00041 _base = base;
00042 if (_base < 2) _base = 2;
00043 if (_base > 36) _base = 36;
00044
00045 _min = _max = 0;
00046 }
00047
00048 KIntValidator::KIntValidator ( int bottom, int top, QWidget * parent, int base, const char * name )
00049 : QValidator(parent, name)
00050 {
00051 _base = base;
00052 if (_base > 36) _base = 36;
00053
00054 _min = bottom;
00055 _max = top;
00056 }
00057
00058 KIntValidator::~KIntValidator ()
00059 {}
00060
00061 QValidator::State KIntValidator::validate ( QString &str, int & ) const
00062 {
00063 bool ok;
00064 int val = 0;
00065 QString newStr;
00066
00067 newStr = str.stripWhiteSpace();
00068 if (_base > 10)
00069 newStr = newStr.upper();
00070
00071 if (newStr == QString::fromLatin1("-"))
00072 if ((_min || _max) && _min >= 0)
00073 ok = false;
00074 else
00075 return QValidator::Acceptable;
00076 else if (newStr.length())
00077 val = newStr.toInt(&ok, _base);
00078 else {
00079 val = 0;
00080 ok = true;
00081 }
00082
00083 if (! ok)
00084 return QValidator::Invalid;
00085
00086 if ((! _min && ! _max) || (val >= _min && val <= _max))
00087 return QValidator::Acceptable;
00088
00089 if (_max && _min >= 0 && val < 0)
00090 return QValidator::Invalid;
00091
00092 return QValidator::Valid;
00093 }
00094
00095 void KIntValidator::fixup ( QString &str ) const
00096 {
00097 int dummy;
00098 int val;
00099 QValidator::State state;
00100
00101 state = validate(str, dummy);
00102
00103 if (state == QValidator::Invalid || state == QValidator::Acceptable)
00104 return;
00105
00106 if (! _min && ! _max)
00107 return;
00108
00109 val = str.toInt(0, _base);
00110
00111 if (val < _min) val = _min;
00112 if (val > _max) val = _max;
00113
00114 str.setNum(val, _base);
00115 }
00116
00117 void KIntValidator::setRange ( int bottom, int top )
00118 {
00119 _min = bottom;
00120 _max = top;
00121
00122 if (_max < _min)
00123 _max = _min;
00124 }
00125
00126 void KIntValidator::setBase ( int base )
00127 {
00128 _base = base;
00129 if (_base < 2) _base = 2;
00130 }
00131
00132 int KIntValidator::bottom () const
00133 {
00134 return _min;
00135 }
00136
00137 int KIntValidator::top () const
00138 {
00139 return _max;
00140 }
00141
00142 int KIntValidator::base () const
00143 {
00144 return _base;
00145 }
00146
00147
00149
00150
00151
00152 class KFloatValidatorPrivate
00153 {
00154 public:
00155 KFloatValidatorPrivate()
00156 {
00157 }
00158 ~KFloatValidatorPrivate()
00159 {
00160 }
00161 bool acceptLocalizedNumbers;
00162 };
00163
00164
00165 KFloatValidator::KFloatValidator ( QWidget * parent, const char * name )
00166 : QValidator(parent, name)
00167 {
00168 d = new KFloatValidatorPrivate;
00169 d->acceptLocalizedNumbers=false;
00170 _min = _max = 0;
00171 }
00172
00173 KFloatValidator::KFloatValidator ( double bottom, double top, QWidget * parent, const char * name )
00174 : QValidator(parent, name)
00175 {
00176 d = new KFloatValidatorPrivate;
00177 d->acceptLocalizedNumbers=false;
00178 _min = bottom;
00179 _max = top;
00180 }
00181
00182 KFloatValidator::KFloatValidator ( double bottom, double top, bool localeAware, QWidget * parent, const char * name )
00183 : QValidator(parent, name)
00184 {
00185 d = new KFloatValidatorPrivate;
00186 d->acceptLocalizedNumbers = localeAware;
00187 _min = bottom;
00188 _max = top;
00189 }
00190
00191 KFloatValidator::~KFloatValidator ()
00192 {
00193 delete d;
00194 }
00195
00196 void KFloatValidator::setAcceptLocalizedNumbers(bool _b)
00197 {
00198 d->acceptLocalizedNumbers=_b;
00199 }
00200
00201 bool KFloatValidator::acceptLocalizedNumbers() const
00202 {
00203 return d->acceptLocalizedNumbers;
00204 }
00205
00206 #include <kdebug.h>
00207 QValidator::State KFloatValidator::validate ( QString &str, int & ) const
00208 {
00209 bool ok;
00210 double val = 0;
00211 QString newStr;
00212 newStr = str.stripWhiteSpace();
00213
00214 if (newStr == QString::fromLatin1("-"))
00215 if ((_min || _max) && _min >= 0)
00216 ok = false;
00217 else
00218 return QValidator::Acceptable;
00219 else if (newStr == QString::fromLatin1(".") || (d->acceptLocalizedNumbers && newStr==KGlobal::locale()->decimalSymbol()))
00220 return QValidator::Acceptable;
00221 else if (newStr.length())
00222 {
00223 val = newStr.toDouble(&ok);
00224 if(!ok && d->acceptLocalizedNumbers)
00225 val= KGlobal::locale()->readNumber(newStr,&ok);
00226 }
00227 else {
00228 val = 0;
00229 ok = true;
00230 }
00231
00232 if (! ok)
00233 return QValidator::Invalid;
00234
00235 if (( !_min && !_max) || (val >= _min && val <= _max))
00236 return QValidator::Acceptable;
00237
00238 if (_max && _min >= 0 && val < 0)
00239 return QValidator::Invalid;
00240
00241 if ( (_min || _max) && (val < _min || val > _max))
00242 return QValidator::Invalid;
00243
00244 return QValidator::Valid;
00245 }
00246
00247 void KFloatValidator::fixup ( QString &str ) const
00248 {
00249 int dummy;
00250 double val;
00251 QValidator::State state;
00252
00253 state = validate(str, dummy);
00254
00255 if (state == QValidator::Invalid || state == QValidator::Acceptable)
00256 return;
00257
00258 if (! _min && ! _max)
00259 return;
00260
00261 val = str.toDouble();
00262
00263 if (val < _min) val = _min;
00264 if (val > _max) val = _max;
00265
00266 str.setNum(val);
00267 }
00268
00269 void KFloatValidator::setRange ( double bottom, double top )
00270 {
00271 _min = bottom;
00272 _max = top;
00273
00274 if (_max < _min)
00275 _max = _min;
00276 }
00277
00278 double KFloatValidator::bottom () const
00279 {
00280 return _min;
00281 }
00282
00283 double KFloatValidator::top () const
00284 {
00285 return _max;
00286 }
00287
00288
00289
00290
00292
00293
00294
00295 class KDoubleValidator::Private {
00296 public:
00297 Private( bool accept=true ) : acceptLocalizedNumbers( accept ) {}
00298
00299 bool acceptLocalizedNumbers;
00300 };
00301
00302 KDoubleValidator::KDoubleValidator( QObject * parent, const char * name )
00303 : QDoubleValidator( parent, name ), d( 0 )
00304 {
00305 d = new Private();
00306 }
00307
00308 KDoubleValidator::KDoubleValidator( double bottom, double top, int decimals,
00309 QObject * parent, const char * name )
00310 : QDoubleValidator( bottom, top, decimals, parent, name ), d( 0 )
00311 {
00312 d = new Private();
00313 }
00314
00315 KDoubleValidator::~KDoubleValidator()
00316 {
00317 delete d;
00318 }
00319
00320 bool KDoubleValidator::acceptLocalizedNumbers() const {
00321 return d->acceptLocalizedNumbers;
00322 }
00323
00324 void KDoubleValidator::setAcceptLocalizedNumbers( bool accept ) {
00325 d->acceptLocalizedNumbers = accept;
00326 }
00327
00328 QValidator::State KDoubleValidator::validate( QString & input, int & p ) const {
00329 QString s = input;
00330 if ( acceptLocalizedNumbers() ) {
00331 KLocale * l = KGlobal::locale();
00332
00333
00334
00335
00336
00337
00338 QString d = l->decimalSymbol(),
00339 n = l->negativeSign(),
00340 p = l->positiveSign(),
00341 t = l->thousandsSeparator();
00342
00343 if ( !p.isEmpty() )
00344 for ( int idx = s.find( p ) ; idx >= 0 ; idx = s.find( p, idx ) )
00345 s.remove( idx, p.length() );
00346
00347
00348 if ( !t.isEmpty() )
00349 for ( int idx = s.find( t ) ; idx >= 0 ; idx = s.find( t, idx ) )
00350 s.remove( idx, t.length() );
00351
00352
00353 if ( ( !n.isEmpty() && n.find('.') != -1 ) ||
00354 ( !d.isEmpty() && d.find('-') != -1 ) ) {
00355
00356 kdWarning() << "KDoubleValidator: decimal symbol contains '-' or "
00357 "negative sign contains '.' -> improve algorithm" << endl;
00358 return Invalid;
00359 }
00360
00361 if ( !d.isEmpty() && d != "." )
00362 for ( int idx = s.find( d ) ; idx >= 0 ; idx = s.find( d, idx + 1 ) )
00363 s.replace( idx, d.length(), ".");
00364
00365 if ( !n.isEmpty() && n != "-" )
00366 for ( int idx = s.find( n ) ; idx >= 0 ; idx = s.find( n, idx + 1 ) )
00367 s.replace( idx, n.length(), "-" );
00368 }
00369
00370 return base::validate( s, p );
00371 }
00372
00373 #include "knumvalidator.moc"