[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-core-devel
Subject: [patch] K{Int|Double}Validator + new classes
From: Jarosław_Staniek <js () iidea ! pl>
Date: 2006-10-26 8:50:57
Message-ID: 200610261151.55815.js () iidea ! pl
[Download RAW message or body]
Cleaned up a bit (e.g. use setBase() where reasonable); fixed indentation.
Related proposals:
1. What about having KNumValidator<type> template where 'type' can be: long,
ulong, qlonglong, qulonglong, etc? Especiall the long long types can be
useful.
(the only place where there's a switch() needed in a QString::toInt()
replacement - I can have it implemented internally).
2. KDecimalValidator - a validator that handles a pair of integers, so you'll
get full precision, including control over decimal places.
KFloatValidator is not precise in the practice (it's just a property of float
type), and neither KDoubleValidator sometimes.
--
regards / pozdrawiam, Jaroslaw Staniek
Sponsored by OpenOffice Polska (http://www.openoffice.com.pl/en) to work on
Kexi & KOffice: http://www.kexi-project.org, http://www.koffice.org
KDE3 & KDE4 Libraries for MS Windows: http://kdelibs.com, http://www.kde.org
["knumvalidator.patch" (text/x-diff)]
Index: knumvalidator.cpp
===================================================================
--- knumvalidator.cpp (wersja 599190)
+++ knumvalidator.cpp (kopia robocza)
@@ -35,23 +35,16 @@
//
KIntValidator::KIntValidator ( QWidget * parent, int base )
- : QValidator(parent)
+ : QValidator(parent), _min(0), _max(0)
{
- _base = base;
- if (_base < 2) _base = 2;
- if (_base > 36) _base = 36;
-
- _min = _max = 0;
+ setBase(base);
}
KIntValidator::KIntValidator ( int bottom, int top, QWidget * parent, int base )
: QValidator(parent)
{
- _base = base;
- if (_base > 36) _base = 36;
-
- _min = bottom;
- _max = top;
+ setBase(base);
+ setRange(bottom, top);
}
KIntValidator::~KIntValidator ()
@@ -67,12 +60,13 @@
if (_base > 10)
newStr = newStr.toUpper();
- if (newStr == QLatin1String("-")) // a special case
+ if (newStr == QLatin1String("-")) {// a special case
if ((_min || _max) && _min >= 0)
ok = false;
else
return QValidator::Acceptable;
- else if (newStr.length())
+ }
+ else if (!newStr.isEmpty())
val = newStr.toInt(&ok, _base);
else {
val = 0;
@@ -126,6 +120,7 @@
{
_base = base;
if (_base < 2) _base = 2;
+ if (_base > 36) _base = 36;
}
int KIntValidator::bottom () const
@@ -162,11 +157,10 @@
KFloatValidator::KFloatValidator ( QWidget * parent )
- : QValidator(parent)
+ : QValidator(parent), _min(0), _max(0)
{
d = new KFloatValidatorPrivate;
d->acceptLocalizedNumbers=false;
- _min = _max = 0;
}
KFloatValidator::KFloatValidator ( double bottom, double top, QWidget * parent )
@@ -174,8 +168,7 @@
{
d = new KFloatValidatorPrivate;
d->acceptLocalizedNumbers=false;
- _min = bottom;
- _max = top;
+ setRange(bottom, top);
}
KFloatValidator::KFloatValidator ( double bottom, double top, bool localeAware, \
QWidget * parent ) @@ -183,8 +176,7 @@
{
d = new KFloatValidatorPrivate;
d->acceptLocalizedNumbers = localeAware;
- _min = bottom;
- _max = top;
+ setRange(bottom, top);
}
KFloatValidator::~KFloatValidator ()
@@ -209,14 +201,15 @@
QString newStr;
newStr = str.trimmed();
- if (newStr == QLatin1String("-")) // a special case
+ if (newStr == QLatin1String("-")) {// a special case
if ((_min || _max) && _min >= 0)
ok = false;
else
return QValidator::Acceptable;
+ }
else if (newStr == QLatin1String(".") || (d->acceptLocalizedNumbers && \
newStr==KGlobal::locale()->decimalSymbol())) // another special case return \
QValidator::Acceptable;
- else if (newStr.length())
+ else if (!newStr.isEmpty())
{
val = newStr.toDouble(&ok);
if(!ok && d->acceptLocalizedNumbers)
@@ -304,7 +297,7 @@
}
KDoubleValidator::KDoubleValidator( double bottom, double top, int decimals,
- QObject * parent )
+ QObject * parent )
: QDoubleValidator( bottom, top, decimals, parent ), d( 0 )
{
d = new Private();
@@ -312,7 +305,7 @@
KDoubleValidator::~KDoubleValidator()
{
- delete d;
+ delete d;
}
bool KDoubleValidator::acceptLocalizedNumbers() const {
@@ -340,29 +333,29 @@
// first, delete p's and t's:
if ( !p.isEmpty() )
for ( int idx = s.indexOf( p ) ; idx >= 0 ; idx = s.indexOf( p, idx ) )
- s.remove( idx, p.length() );
+ s.remove( idx, p.length() );
if ( !t.isEmpty() )
for ( int idx = s.indexOf( t ) ; idx >= 0 ; idx = s.indexOf( t, idx ) )
- s.remove( idx, t.length() );
+ s.remove( idx, t.length() );
// then, replace the d's and n's
if ( ( !n.isEmpty() && n.indexOf('.') != -1 ) ||
- ( !d.isEmpty() && d.indexOf('-') != -1 ) ) {
+ ( !d.isEmpty() && d.indexOf('-') != -1 ) ) {
// make sure we don't replace something twice:
kWarning() << "KDoubleValidator: decimal symbol contains '-' or "
- "negative sign contains '.' -> improve algorithm" << endl;
+ "negative sign contains '.' -> improve algorithm" << endl;
return Invalid;
}
if ( !d.isEmpty() && d != "." )
for ( int idx = s.indexOf( d ) ; idx >= 0 ; idx = s.indexOf( d, idx + 1 ) )
- s.replace( idx, d.length(), '.');
+ s.replace( idx, d.length(), '.');
if ( !n.isEmpty() && n != "-" )
for ( int idx = s.indexOf( n ) ; idx >= 0 ; idx = s.indexOf( n, idx + 1 ) )
- s.replace( idx, n.length(), '-' );
+ s.replace( idx, n.length(), '-' );
}
return base::validate( s, p );
Index: knumvalidator.h
===================================================================
--- knumvalidator.h (wersja 599190)
+++ knumvalidator.h (kopia robocza)
@@ -63,11 +63,12 @@
*/
virtual void fixup ( QString & ) const;
/**
- * Sets the minimum and maximum values allowed.
+ * Sets the minimum and maximum values allowed.
+ * If @p top is greater than @p bottom, it is set to the value of @p bottom.
*/
virtual void setRange ( int bottom, int top );
/**
- * Sets the numeric base value.
+ * Sets the numeric base value. @p base must be between 2 and 36.
*/
virtual void setBase ( int base );
/**
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic