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