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 QValidator::State
KFloatValidator::validate (
QString &str,
int & )
const
00207
{
00208
bool ok;
00209
double val = 0;
00210
QString newStr;
00211 newStr = str.
stripWhiteSpace();
00212
00213
if (newStr == QString::fromLatin1(
"-"))
00214
if ((_min || _max) && _min >= 0)
00215 ok =
false;
00216
else
00217
return QValidator::Acceptable;
00218
else if (newStr == QString::fromLatin1(
".") || (d->acceptLocalizedNumbers && newStr==
KGlobal::locale()->
decimalSymbol()))
00219
return QValidator::Acceptable;
00220
else if (newStr.
length())
00221 {
00222 val = newStr.
toDouble(&ok);
00223
if(!ok && d->acceptLocalizedNumbers)
00224 val=
KGlobal::locale()->
readNumber(newStr,&ok);
00225 }
00226
else {
00227 val = 0;
00228 ok =
true;
00229 }
00230
00231
if (! ok)
00232
return QValidator::Invalid;
00233
00234
if (( !_min && !_max) || (val >= _min && val <= _max))
00235
return QValidator::Acceptable;
00236
00237
if (_max && _min >= 0 && val < 0)
00238
return QValidator::Invalid;
00239
00240
if ( (_min || _max) && (val < _min || val > _max))
00241
return QValidator::Invalid;
00242
00243
return QValidator::Valid;
00244 }
00245
00246 void KFloatValidator::fixup (
QString &str )
const
00247
{
00248
int dummy;
00249
double val;
00250 QValidator::State state;
00251
00252 state =
validate(str, dummy);
00253
00254
if (state == QValidator::Invalid || state == QValidator::Acceptable)
00255
return;
00256
00257
if (! _min && ! _max)
00258
return;
00259
00260 val = str.
toDouble();
00261
00262
if (val < _min) val = _min;
00263
if (val > _max) val = _max;
00264
00265 str.
setNum(val);
00266 }
00267
00268 void KFloatValidator::setRange (
double bottom,
double top )
00269 {
00270 _min = bottom;
00271 _max = top;
00272
00273
if (_max < _min)
00274 _max = _min;
00275 }
00276
00277 double KFloatValidator::bottom ()
const
00278
{
00279
return _min;
00280 }
00281
00282 double KFloatValidator::top ()
const
00283
{
00284
return _max;
00285 }
00286
00287
00288
00289
00291
00292
00293
00294
class KDoubleValidator::Private {
00295
public:
00296 Private(
bool accept=
true ) : acceptLocalizedNumbers( accept ) {}
00297
00298
bool acceptLocalizedNumbers;
00299 };
00300
00301 KDoubleValidator::KDoubleValidator(
QObject * parent,
const char * name )
00302 :
QDoubleValidator( parent, name ), d( 0 )
00303 {
00304 d =
new Private();
00305 }
00306
00307 KDoubleValidator::KDoubleValidator(
double bottom,
double top,
int decimals,
00308
QObject * parent,
const char * name )
00309 :
QDoubleValidator( bottom, top, decimals, parent, name ), d( 0 )
00310 {
00311 d =
new Private();
00312 }
00313
00314 KDoubleValidator::~KDoubleValidator()
00315 {
00316
delete d;
00317 }
00318
00319
bool KDoubleValidator::acceptLocalizedNumbers()
const {
00320
return d->acceptLocalizedNumbers;
00321 }
00322
00323 void KDoubleValidator::setAcceptLocalizedNumbers(
bool accept ) {
00324 d->acceptLocalizedNumbers = accept;
00325 }
00326
00327 QValidator::State
KDoubleValidator::validate(
QString & input,
int & p )
const {
00328
QString s = input;
00329
if (
acceptLocalizedNumbers() ) {
00330
KLocale * l =
KGlobal::locale();
00331
00332
00333
00334
00335
00336
00337
QString d = l->
decimalSymbol(),
00338 n = l->
negativeSign(),
00339 p = l->
positiveSign(),
00340 t = l->
thousandsSeparator();
00341
00342
if ( !p.isEmpty() )
00343
for (
int idx = s.
find( p ) ; idx >= 0 ; idx = s.
find( p, idx ) )
00344 s.
remove( idx, p.length() );
00345
00346
00347
if ( !t.isEmpty() )
00348
for (
int idx = s.
find( t ) ; idx >= 0 ; idx = s.
find( t, idx ) )
00349 s.
remove( idx, t.length() );
00350
00351
00352
if ( ( !n.isEmpty() && n.find(
'.') != -1 ) ||
00353 ( !d.
isEmpty() && d.
find(
'-') != -1 ) ) {
00354
00355
kdWarning() <<
"KDoubleValidator: decimal symbol contains '-' or "
00356
"negative sign contains '.' -> improve algorithm" <<
endl;
00357
return Invalid;
00358 }
00359
00360
if ( !d.
isEmpty() && d !=
"." )
00361
for (
int idx = s.
find( d ) ; idx >= 0 ; idx = s.
find( d, idx + 1 ) )
00362 s.
replace( idx, d.
length(),
".");
00363
00364
if ( !n.isEmpty() && n !=
"-" )
00365
for (
int idx = s.
find( n ) ; idx >= 0 ; idx = s.
find( n, idx + 1 ) )
00366 s.
replace( idx, n.length(),
"-" );
00367 }
00368
00369
return base::validate( s, p );
00370 }
00371
00372
#include "knumvalidator.moc"